Updated on 2026-08-14

This commit is contained in:
Tangem 2024-04-24 15:18:48 +08:00
parent a3f2c421bf
commit d174ada664
20 changed files with 71 additions and 189 deletions

View file

@ -2,85 +2,20 @@ package com.tangem.tap.proxy
import com.tangem.blockchain.common.AmountType
import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchain.common.Token
import com.tangem.blockchain.common.WalletManager
import com.tangem.blockchainsdk.utils.fromNetworkId
import com.tangem.blockchainsdk.utils.toCoinId
import com.tangem.blockchainsdk.utils.toNetworkId
import com.tangem.datasource.local.userwallet.UserWalletsStore
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.repository.CurrenciesRepository
import com.tangem.domain.walletmanager.WalletManagersFacade
import com.tangem.domain.wallets.legacy.UserWalletsListManager
import com.tangem.lib.crypto.UserWalletManager
import com.tangem.lib.crypto.models.Currency
import com.tangem.lib.crypto.models.Currency.NativeToken
import com.tangem.lib.crypto.models.Currency.NonNativeToken
import com.tangem.lib.crypto.models.ProxyAmount
import com.tangem.tap.userWalletsListManager
import timber.log.Timber
import java.math.BigDecimal
class UserWalletManagerImpl(
private val walletManagersFacade: WalletManagersFacade,
private val currenciesRepository: CurrenciesRepository,
private val userWalletsStore: UserWalletsStore,
private val userWalletsListManager: UserWalletsListManager,
) : UserWalletManager {
override suspend fun getUserTokens(
networkId: String,
derivationPath: String?,
isExcludeCustom: Boolean,
): List<Currency> {
// FIXME: Find user wallet by ID
val userWallet = requireNotNull(userWalletsStore.selectedUserWalletOrNull) {
"No user wallet selected"
}
return currenciesRepository.getMultiCurrencyWalletCurrenciesSync(userWallet.walletId)
.filter {
val checkCustom = if (isExcludeCustom) {
!it.isCustom
} else {
true
}
val blockchain = Blockchain.fromId(it.network.id.value)
blockchain.toNetworkId() == networkId &&
checkCustom &&
it.network.derivationPath.value == derivationPath
}
.map {
val blockchain = Blockchain.fromId(it.network.id.value)
if (it is CryptoCurrency.Token) {
NonNativeToken(
id = it.id.rawCurrencyId ?: "",
name = it.name,
symbol = it.symbol,
networkId = blockchain.toNetworkId(),
contractAddress = it.contractAddress,
decimalCount = it.decimals,
)
} else {
NativeToken(
id = it.id.rawCurrencyId ?: "",
name = it.name,
symbol = it.symbol,
networkId = blockchain.toNetworkId(),
)
}
}
}
override fun getNativeTokenForNetwork(networkId: String): Currency {
val blockchain = requireNotNull(Blockchain.fromNetworkId(networkId)) { "blockchain not found" }
return NativeToken(
id = blockchain.toCoinId(),
name = blockchain.fullName,
symbol = blockchain.currency,
networkId = networkId,
)
}
override fun getWalletId(): String {
val selectedUserWallet = requireNotNull(
userWalletsListManager.selectedUserWalletSync,
@ -88,18 +23,6 @@ class UserWalletManagerImpl(
return selectedUserWallet.walletId.stringValue
}
override suspend fun isTokenAdded(currency: Currency, derivationPath: String?): Boolean {
val blockchain = requireNotNull(Blockchain.fromNetworkId(currency.networkId)) { "blockchain not found" }
return try {
val walletManager = getActualWalletManager(blockchain, derivationPath)
walletManager.cardTokens.any {
it.id == currency.id
}
} catch (e: IllegalArgumentException) {
false
}
}
override suspend fun hideAllTokens() {
// FIXME: Used only in Tester Actions
Timber.w("Not implemented")
@ -119,37 +42,6 @@ class UserWalletManagerImpl(
?.hash
}
override suspend fun getCurrentWalletTokensBalance(
networkId: String,
extraTokens: List<Currency>,
derivationPath: String?,
): Map<String, ProxyAmount> {
val blockchain = requireNotNull(Blockchain.fromNetworkId(networkId)) { "blockchain not found" }
val walletManager = getActualWalletManager(blockchain, derivationPath)
// workaround for get balance for tokens that doesn't exist in wallet
val extraTokensToLoadBalance = extraTokens
.filterIsInstance<NonNativeToken>()
.map {
it.toSdkToken()
}
.filter {
!walletManager.cardTokens.contains(it)
}
walletManager.addTokens(extraTokensToLoadBalance)
walletManager.update()
val balances = walletManager.wallet.amounts.map { entry ->
val amount = entry.value
amount.currencySymbol to ProxyAmount(
amount.currencySymbol,
amount.value ?: BigDecimal.ZERO,
amount.decimals,
)
}.toMap()
extraTokensToLoadBalance.forEach { walletManager.removeToken(it) }
return balances
}
override suspend fun getNativeTokenBalance(networkId: String, derivationPath: String?): ProxyAmount? {
val blockchain = requireNotNull(Blockchain.fromNetworkId(networkId)) { "blockchain not found" }
val walletManager = getActualWalletManager(blockchain, derivationPath)
@ -179,14 +71,4 @@ class UserWalletManagerImpl(
"No wallet manager found"
}
}
}
private fun NonNativeToken.toSdkToken(): Token {
return Token(
id = this.id,
name = this.name,
symbol = this.symbol,
contractAddress = this.contractAddress,
decimals = this.decimalCount,
)
}