Updated on 2026-08-14

This commit is contained in:
Tangem 2024-07-02 21:20:02 +08:00
parent 5f42e80618
commit efc8c003bd
5 changed files with 111 additions and 82 deletions

View file

@ -87,8 +87,11 @@ internal object TokensDataModule {
@Provides
@Singleton
fun provideDefaultMarketCoinsRepository(assetsStore: AssetsStore): MarketCryptoCurrencyRepository {
return DefaultMarketCryptoCurrencyRepository(assetsStore)
fun provideDefaultMarketCoinsRepository(
assetsStore: AssetsStore,
coroutineDispatcherProvider: CoroutineDispatcherProvider,
): MarketCryptoCurrencyRepository {
return DefaultMarketCryptoCurrencyRepository(assetsStore, coroutineDispatcherProvider)
}
@Provides

View file

@ -302,6 +302,7 @@ internal class DefaultCurrenciesRepository(
networkId: Network.ID,
derivationPath: Network.DerivationPath,
): CryptoCurrency.Coin {
return withContext(dispatchers.io) {
val userWallet = getUserWallet(userWalletId)
ensureIsCorrectUserWallet(userWallet = userWallet, isMultiCurrencyWalletExpected = true)
@ -321,7 +322,8 @@ internal class DefaultCurrenciesRepository(
val coin = responseCurrenciesFactory.createCurrency(storedCoin, userWallet.scanResponse)
return coin as? CryptoCurrency.Coin ?: error("Unable to create currency")
coin as? CryptoCurrency.Coin ?: error("Unable to create currency")
}
}
override fun isTokensGrouped(userWalletId: UserWalletId): Flow<Boolean> {
@ -382,8 +384,9 @@ internal class DefaultCurrenciesRepository(
}
override suspend fun getFeePaidCurrency(userWalletId: UserWalletId, currency: CryptoCurrency): FeePaidCurrency {
return withContext(dispatchers.io) {
val blockchain = Blockchain.fromId(currency.network.id.value)
return when (val feePaidCurrency = blockchain.feePaidCurrency()) {
when (val feePaidCurrency = blockchain.feePaidCurrency()) {
FeePaidSdkCurrency.Coin -> FeePaidCurrency.Coin
FeePaidSdkCurrency.SameCurrency -> FeePaidCurrency.SameCurrency
is FeePaidSdkCurrency.Token -> {
@ -407,6 +410,7 @@ internal class DefaultCurrenciesRepository(
is FeePaidSdkCurrency.FeeResource -> FeePaidCurrency.FeeResource(currency = feePaidCurrency.currency)
}
}
}
override fun createTokenCurrency(cryptoCurrency: CryptoCurrency.Token, network: Network): CryptoCurrency.Token {
return CryptoCurrencyFactory().createToken(

View file

@ -5,9 +5,12 @@ import com.tangem.datasource.local.token.AssetsStore
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.repository.MarketCryptoCurrencyRepository
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import kotlinx.coroutines.withContext
class DefaultMarketCryptoCurrencyRepository(
private val assetsStore: AssetsStore,
private val dispatchers: CoroutineDispatcherProvider,
) : MarketCryptoCurrencyRepository {
override suspend fun isExchangeable(userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency): Boolean {
@ -15,11 +18,16 @@ class DefaultMarketCryptoCurrencyRepository(
}
private suspend fun getExchangeableFlag(userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency): Boolean {
val contractAddress = (cryptoCurrency as? CryptoCurrency.Token)?.contractAddress ?: EMPTY_CONTRACT_ADDRESS_VALUE
return withContext(dispatchers.io) {
val contractAddress =
(cryptoCurrency as? CryptoCurrency.Token)?.contractAddress ?: EMPTY_CONTRACT_ADDRESS_VALUE
return assetsStore.getSyncOrNull(userWalletId)?.find {
val asset = assetsStore.getSyncOrNull(userWalletId)?.find {
it.network == cryptoCurrency.network.backendId &&
it.contractAddress.equals(contractAddress, ignoreCase = true)
}?.exchangeAvailable ?: false
}
asset?.exchangeAvailable ?: false
}
}
}

View file

@ -35,18 +35,21 @@ class DefaultTxHistoryRepository(
private val sdkPageConverter by lazy { SdkPageConverter() }
override suspend fun getTxHistoryItemsCount(userWalletId: UserWalletId, currency: CryptoCurrency): Int {
return withContext(dispatchers.io) {
val userWallet = getUserWallet(userWalletId)
val state = walletManagersFacade.getTxHistoryState(
userWalletId = userWallet.walletId,
currency = currency,
)
return when (state) {
when (state) {
is TxHistoryState.Failed.FetchError -> throw TxHistoryStateError.DataError(state.exception)
is TxHistoryState.NotImplemented -> throw TxHistoryStateError.TxHistoryNotImplemented
is TxHistoryState.Success.Empty -> throw TxHistoryStateError.EmptyTxHistories
is TxHistoryState.Success.HasTransactions -> state.txCount
}
}
}
override fun getTxHistoryItems(
userWalletId: UserWalletId,

View file

@ -87,6 +87,7 @@ class DefaultWalletManagersFacade(
Blockchain.fromId(it.id.value) to it.derivationPath.value
}
withContext(dispatchers.io) {
walletManagersStore.remove(userWalletId) { walletManager ->
val wallet = walletManager.wallet
val blockchainToDerivationPath = wallet.blockchain to wallet.publicKey.derivationPath?.rawPath
@ -94,6 +95,7 @@ class DefaultWalletManagersFacade(
blockchainToDerivationPath in blockchainsToDerivationPaths
}
}
}
override suspend fun removeTokens(userWalletId: UserWalletId, tokens: Set<CryptoCurrency.Token>) {
if (tokens.isEmpty()) return
@ -110,11 +112,12 @@ class DefaultWalletManagersFacade(
network: Network,
networkTokens: List<CryptoCurrency.Token>,
) {
withContext(dispatchers.io) {
val walletManager = walletManagersStore.getSyncOrNull(
userWalletId = userWalletId,
blockchain = Blockchain.fromId(network.id.value),
derivationPath = network.derivationPath.value,
) ?: return
) ?: return@withContext
val tokensToRemove = sdkTokenConverter.convertList(networkTokens)
tokensToRemove.forEach { token ->
@ -123,6 +126,7 @@ class DefaultWalletManagersFacade(
walletManagersStore.store(userWalletId, walletManager)
}
}
override suspend fun updatePendingTransactions(
userWalletId: UserWalletId,
@ -576,12 +580,16 @@ class DefaultWalletManagersFacade(
userWalletId: UserWalletId,
currency: CryptoCurrency,
): AssetRequirementsCondition? {
return withContext(dispatchers.io) {
val walletManager = getOrCreateWalletManager(userWalletId = userWalletId, network = currency.network)
val currencyType = cryptoCurrencyTypeConverter.convert(currency)
if (walletManager !is AssetRequirementsManager || !walletManager.hasRequirements(currencyType)) return null
if (walletManager !is AssetRequirementsManager || !walletManager.hasRequirements(currencyType)) {
return@withContext null
}
val condition = walletManager.requirementsCondition(currencyType) ?: return null
return requirementsConditionConverter.convert(condition)
val condition = walletManager.requirementsCondition(currencyType) ?: return@withContext null
requirementsConditionConverter.convert(condition)
}
}
override suspend fun associateAsset(
@ -589,15 +597,18 @@ class DefaultWalletManagersFacade(
currency: CryptoCurrency,
signer: CommonSigner,
): SimpleResult {
return withContext(dispatchers.io) {
val walletManager = getOrCreateWalletManager(userWalletId = userWalletId, network = currency.network)
val currencyType = cryptoCurrencyTypeConverter.convert(currency)
if (walletManager !is AssetRequirementsManager) {
return SimpleResult.Failure(
return@withContext SimpleResult.Failure(
BlockchainSdkError.CustomError("WalletManager is not implemented AssetRequirementsManager"),
)
}
return walletManager.fulfillRequirements(currencyType, signer)
walletManager.fulfillRequirements(currencyType, signer)
}
}
override suspend fun checkUtxoConsolidationAvailability(userWalletId: UserWalletId, network: Network): Boolean {