Updated on 2026-08-14

This commit is contained in:
Tangem 2022-04-21 09:01:58 +03:00
parent f076b8a8f5
commit e51be03285
2 changed files with 34 additions and 45 deletions

View file

@ -1,17 +1,14 @@
package com.tangem.tap.domain
import com.tangem.blockchain.common.*
import com.tangem.common.extensions.guard
import com.tangem.common.services.Result
import com.tangem.domain.common.ScanResponse
import com.tangem.domain.common.TapWorkarounds.derivationStyle
import com.tangem.domain.common.TapWorkarounds.isStart2Coin
import com.tangem.domain.common.TapWorkarounds.isTestCard
import com.tangem.domain.common.extensions.toCoinId
import com.tangem.domain.common.extensions.withMainContext
import com.tangem.network.api.tangemTech.TangemTechService
import com.tangem.tap.common.ThrottlerWithValues
import com.tangem.tap.common.extensions.dispatchDebugErrorNotification
import com.tangem.tap.common.extensions.dispatchOnMain
import com.tangem.tap.common.extensions.safeUpdate
import com.tangem.tap.common.redux.global.FiatCurrencyName
@ -122,30 +119,22 @@ class TapWalletManager {
}
val toUpdateCurrencies = currencies.filter { !fiatRatesThrottler.isStillThrottled(it) }
val toUpdateIds = toUpdateCurrencies.mapNotNull { it.id }.distinct()
val toUpdateIds = toUpdateCurrencies.mapNotNull { it.coinId }.distinct()
if (toUpdateIds.isEmpty()) return
//TODO: refactoring: move fiatRatesThrottler to the TangemTechRepository
when (val result = tangemTechService.coins.prices(fiatCurrency, toUpdateIds)) {
is Result.Success -> {
val missedCurrencies = mutableListOf<String>()
val updatedCurrencies = mutableMapOf<Currency, Result<BigDecimal>?>()
result.data.prices.forEach { (name, value) ->
val currency = toUpdateCurrencies.firstOrNull { it.id == name }.guard {
missedCurrencies.add(name)
return@forEach
}
val priceResult = Result.Success(value.toBigDecimal())
updatedCurrencies[currency] = priceResult
fiatRatesThrottler.updateThrottlingTo(currency)
fiatRatesThrottler.setValue(currency, priceResult)
val priceResultList: Map<String, Result<BigDecimal>> = result.data.prices.mapValues {
Result.Success(it.value.toBigDecimal())
}
if (missedCurrencies.isNotEmpty()) {
val missedNames = missedCurrencies.joinToString(", ")
store.dispatchDebugErrorNotification("Not found currencies to update: [$missedNames]")
val updatedCurrencies = mutableMapOf<Currency, Result<BigDecimal>?>()
toUpdateCurrencies.forEach { currency ->
priceResultList[currency.coinId]?.let {
updatedCurrencies[currency] = it
fiatRatesThrottler.updateThrottlingTo(currency)
fiatRatesThrottler.setValue(currency, it)
}
}
handleFiatRatesResult(updatedCurrencies)
}
@ -328,10 +317,4 @@ class TapWalletManager {
fun Wallet.getFirstToken(): Token? {
return getTokens().toList().getOrNull(0)
}
val Currency.id: String?
get() = when (this) {
is Currency.Blockchain -> blockchain.toCoinId()
is Currency.Token -> token.id
}
}

View file

@ -5,6 +5,7 @@ import com.tangem.blockchain.common.*
import com.tangem.blockchain.common.address.AddressType
import com.tangem.blockchain.extensions.isAboveZero
import com.tangem.common.extensions.isZero
import com.tangem.domain.common.extensions.toCoinId
import com.tangem.domain.features.addCustomToken.CustomCurrency
import com.tangem.tap.common.entities.Button
import com.tangem.tap.common.extensions.toQrCode
@ -60,7 +61,7 @@ data class WalletState(
val shouldShowDetails: Boolean =
primaryWallet?.currencyData?.status != BalanceStatus.EmptyCard &&
primaryWallet?.currencyData?.status != BalanceStatus.UnknownBlockchain
primaryWallet?.currencyData?.status != BalanceStatus.UnknownBlockchain
val blockchains: List<Blockchain>
get() = wallets.mapNotNull { it.walletManager?.wallet?.blockchain }
@ -87,8 +88,8 @@ data class WalletState(
if (blockchain == null) return null
return walletsData.find {
it.currency is Currency.Blockchain &&
it.currency.blockchain == blockchain.blockchain &&
it.currency.derivationPath == blockchain.derivationPath
it.currency.blockchain == blockchain.blockchain &&
it.currency.derivationPath == blockchain.derivationPath
}
}
@ -96,7 +97,7 @@ data class WalletState(
if (currency == null) return null
return wallets.firstOrNull {
it.blockchainNetwork.derivationPath == currency.derivationPath &&
(it.blockchainNetwork.blockchain == currency.blockchain)
(it.blockchainNetwork.blockchain == currency.blockchain)
}
}
@ -111,7 +112,7 @@ data class WalletState(
if (blockchainNetwork == null) return null
return wallets.firstOrNull {
it.blockchainNetwork.derivationPath == blockchainNetwork.derivationPath &&
(it.blockchainNetwork.blockchain == blockchainNetwork.blockchain)
(it.blockchainNetwork.blockchain == blockchainNetwork.blockchain)
}
}
@ -141,23 +142,23 @@ data class WalletState(
if (walletData.currency is Currency.Blockchain) {
return wallet.recentTransactions.toPendingTransactions(wallet.address).isEmpty() &&
wallet.amounts.toSendableAmounts().isEmpty()
wallet.amounts.toSendableAmounts().isEmpty()
} else if (walletData.currency is Currency.Token) (
return wallet.recentTransactions.toPendingTransactionsForToken(
walletData.currency.token, wallet.address
).isEmpty()
&& wallet.amounts[AmountType.Token(token = walletData.currency.token)]
?.isAboveZero() != true
)
return wallet.recentTransactions.toPendingTransactionsForToken(
walletData.currency.token, wallet.address
).isEmpty()
&& wallet.amounts[AmountType.Token(token = walletData.currency.token)]
?.isAboveZero() != true
)
}
return false
}
private fun isPrimaryCurrency(walletData: WalletData): Boolean {
return (walletData.currency is Currency.Blockchain &&
walletData.currency.blockchain == store.state.walletState.primaryBlockchain)
|| (walletData.currency is Currency.Token &&
walletData.currency.token == store.state.walletState.primaryToken)
walletData.currency.blockchain == store.state.walletState.primaryBlockchain)
|| (walletData.currency is Currency.Token &&
walletData.currency.token == store.state.walletState.primaryToken)
}
fun replaceWalletInWallets(wallet: WalletStore?): List<WalletStore> {
@ -214,7 +215,7 @@ data class WalletState(
return if (walletData.currency is Currency.Blockchain) {
val walletStores = wallets.filterNot {
it.blockchainNetwork.blockchain == walletData.currency.blockchain
&& it.blockchainNetwork.derivationPath == walletData.currency.derivationPath
&& it.blockchainNetwork.derivationPath == walletData.currency.derivationPath
}
copy(wallets = walletStores)
} else {
@ -371,6 +372,11 @@ data class WalletRent(
sealed interface Currency {
val coinId: String?
get() = when (this) {
is Blockchain -> blockchain.toCoinId()
is Token -> token.id
}
val blockchain: com.tangem.blockchain.common.Blockchain
val currencySymbol: CryptoCurrencyName
val derivationPath: String?
@ -450,7 +456,7 @@ data class WalletStore(
fun updateWallets(walletDataList: List<WalletData>): WalletStore {
val relevantWalletDataList = walletDataList.filter {
it.currency.blockchain == blockchainNetwork.blockchain &&
it.currency.derivationPath == blockchainNetwork.derivationPath
it.currency.derivationPath == blockchainNetwork.derivationPath
}.toMutableList()
val updatedWalletDataList = walletsData.map { walletData ->
val matchingWalletData = relevantWalletDataList.find { it.currency == walletData.currency }