Updated on 2026-08-14

This commit is contained in:
Tangem 2026-04-13 19:48:06 +03:00
parent 27e9400b2a
commit b38ebb3995
16 changed files with 196 additions and 71 deletions

View file

@ -43,6 +43,7 @@ internal class CommonNetworkStatusFetcher @Inject constructor(
userWalletId: UserWalletId,
network: Network,
networkCurrencies: Set<CryptoCurrency>,
xpub: String? = null,
): Either<Throwable, Unit> {
return Either.catchOn(dispatchers.default) {
val result = withContext(dispatchers.io) {
@ -52,6 +53,7 @@ internal class CommonNetworkStatusFetcher @Inject constructor(
extraTokens = networkCurrencies
.filterIsInstance<CryptoCurrency.Token>()
.toSet(),
xpub = xpub,
)
}

View file

@ -2,16 +2,18 @@ package com.tangem.data.networks.multi
import arrow.core.raise.catch
import arrow.core.raise.ensure
import com.tangem.domain.common.tokens.CardCryptoCurrencyFactory
import com.tangem.data.networks.fetcher.CommonNetworkStatusFetcher
import com.tangem.data.networks.store.NetworksStatusesStore
import com.tangem.data.networks.store.setSourceAsCache
import com.tangem.data.networks.store.setSourceAsOnlyCache
import com.tangem.data.dynamicaddresses.DynamicAddressesInitializer
import com.tangem.domain.common.tokens.CardCryptoCurrencyFactory
import com.tangem.domain.core.utils.eitherOn
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.models.network.Network
import com.tangem.domain.networks.multi.MultiNetworkStatusFetcher
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import com.tangem.utils.logging.TangemLogger
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
@ -27,11 +29,11 @@ import javax.inject.Inject
*
[REDACTED_AUTHOR]
*/
@Suppress("LongParameterList")
internal class DefaultMultiNetworkStatusFetcher @Inject constructor(
private val networksStatusesStore: NetworksStatusesStore,
private val cardCryptoCurrencyFactory: CardCryptoCurrencyFactory,
private val commonNetworkStatusFetcher: CommonNetworkStatusFetcher,
private val dynamicAddressesInitializer: DynamicAddressesInitializer,
private val dispatchers: CoroutineDispatcherProvider,
) : MultiNetworkStatusFetcher {
@ -50,6 +52,14 @@ internal class DefaultMultiNetworkStatusFetcher @Inject constructor(
},
)
val xpubByNetwork = catch(
block = { dynamicAddressesInitializer.getXpubs(params.userWalletId, params.networks) },
catch = { error ->
TangemLogger.e("Failed to build XPUBs for restore", error)
emptyMap()
},
)
val result = coroutineScope {
params.networks
.map { network ->
@ -58,6 +68,7 @@ internal class DefaultMultiNetworkStatusFetcher @Inject constructor(
userWalletId = params.userWalletId,
network = network,
networkCurrencies = networksCurrencies[network].orEmpty().toSet(),
xpub = xpubByNetwork[network],
)
}
}

View file

@ -3,6 +3,7 @@ package com.tangem.data.networks.multi
import arrow.core.Either
import arrow.core.left
import com.tangem.common.test.domain.token.MockCryptoCurrencyFactory
import com.tangem.data.dynamicaddresses.DynamicAddressesInitializer
import com.tangem.domain.common.tokens.CardCryptoCurrencyFactory
import com.tangem.data.networks.fetcher.CommonNetworkStatusFetcher
import com.tangem.data.networks.store.NetworksStatusesStore
@ -27,17 +28,21 @@ internal class DefaultMultiNetworkStatusFetcherTest {
private val networksStatusesStore: NetworksStatusesStore = mockk(relaxUnitFun = true)
private val cardCryptoCurrencyFactory: CardCryptoCurrencyFactory = mockk()
private val commonNetworkStatusFetcher: CommonNetworkStatusFetcher = mockk()
private val dynamicAddressesInitializer: DynamicAddressesInitializer = mockk()
private val fetcher = DefaultMultiNetworkStatusFetcher(
networksStatusesStore = networksStatusesStore,
cardCryptoCurrencyFactory = cardCryptoCurrencyFactory,
commonNetworkStatusFetcher = commonNetworkStatusFetcher,
dynamicAddressesInitializer = dynamicAddressesInitializer,
dispatchers = TestingCoroutineDispatcherProvider(),
)
@BeforeEach
fun resetMocks() {
clearMocks(networksStatusesStore, cardCryptoCurrencyFactory, commonNetworkStatusFetcher)
clearMocks(networksStatusesStore, cardCryptoCurrencyFactory, commonNetworkStatusFetcher, dynamicAddressesInitializer)
// No dynamic addresses restore by default
coEvery { dynamicAddressesInitializer.getXpubs(any(), any()) } returns emptyMap()
}
@Test
@ -62,6 +67,7 @@ internal class DefaultMultiNetworkStatusFetcherTest {
userWalletId = params.userWalletId,
network = ethereum.network,
networkCurrencies = setOf(ethereum),
xpub = null,
)
} returns ethereumFetcherResult
@ -70,6 +76,7 @@ internal class DefaultMultiNetworkStatusFetcherTest {
userWalletId = params.userWalletId,
network = cardano.network,
networkCurrencies = setOf(cardano),
xpub = null,
)
} returns cardanoFetcherResult
@ -87,11 +94,13 @@ internal class DefaultMultiNetworkStatusFetcherTest {
userWalletId = params.userWalletId,
network = ethereum.network,
networkCurrencies = setOf(ethereum),
xpub = null,
)
commonNetworkStatusFetcher.fetch(
userWalletId = params.userWalletId,
network = cardano.network,
networkCurrencies = setOf(cardano),
xpub = null,
)
}
@ -122,6 +131,7 @@ internal class DefaultMultiNetworkStatusFetcherTest {
userWalletId = params.userWalletId,
network = ethereum.network,
networkCurrencies = setOf(ethereum),
xpub = null,
)
} returns ethereumFetcherResult
@ -130,6 +140,7 @@ internal class DefaultMultiNetworkStatusFetcherTest {
userWalletId = params.userWalletId,
network = cardano.network,
networkCurrencies = setOf(cardano),
xpub = null,
)
} returns cardanoFetcherResult
@ -147,11 +158,13 @@ internal class DefaultMultiNetworkStatusFetcherTest {
userWalletId = params.userWalletId,
network = ethereum.network,
networkCurrencies = setOf(ethereum),
xpub = null,
)
commonNetworkStatusFetcher.fetch(
userWalletId = params.userWalletId,
network = cardano.network,
networkCurrencies = setOf(cardano),
xpub = null,
)
}
@ -182,6 +195,7 @@ internal class DefaultMultiNetworkStatusFetcherTest {
userWalletId = params.userWalletId,
network = ethereum.network,
networkCurrencies = setOf(ethereum),
xpub = null,
)
} returns ethereumFetcherResult
@ -190,6 +204,7 @@ internal class DefaultMultiNetworkStatusFetcherTest {
userWalletId = params.userWalletId,
network = cardano.network,
networkCurrencies = setOf(cardano),
xpub = null,
)
} returns cardanoFetcherResult
@ -207,11 +222,13 @@ internal class DefaultMultiNetworkStatusFetcherTest {
userWalletId = params.userWalletId,
network = ethereum.network,
networkCurrencies = setOf(ethereum),
xpub = null,
)
commonNetworkStatusFetcher.fetch(
userWalletId = params.userWalletId,
network = cardano.network,
networkCurrencies = setOf(cardano),
xpub = null,
)
}
@ -245,7 +262,7 @@ internal class DefaultMultiNetworkStatusFetcherTest {
networksStatusesStore.setSourceAsOnlyCache(params.userWalletId, params.networks)
}
coVerify(inverse = true) { commonNetworkStatusFetcher.fetch(any(), any(), any()) }
coVerify(inverse = true) { commonNetworkStatusFetcher.fetch(any(), any(), any(), any()) }
}
private companion object {