Updated on 2026-08-14

This commit is contained in:
Tangem 2022-10-18 03:05:37 +05:00
commit 49798fd8cd
9 changed files with 43 additions and 38 deletions

View file

@ -134,7 +134,6 @@ class TapWalletManager {
}
dispatchOnMain(WalletAction.LoadWallet())
dispatchOnMain(WalletAction.LoadFiatRate())
}
private suspend fun loadMultiWalletData(scanResponse: ScanResponse) {
@ -174,6 +173,7 @@ class TapWalletManager {
walletManagers = listOf(primaryWalletManager),
save = false,
),
WalletAction.LoadFiatRate(),
)
}
}
@ -202,6 +202,7 @@ class TapWalletManager {
)
}
checkIfDerivationsAreMissing(blockchainNetworks, scanResponse)
store.dispatch(WalletAction.LoadFiatRate(coinsList = userTokens))
}
}
@ -218,7 +219,6 @@ class TapWalletManager {
}
store.dispatch(WalletAction.LoadWallet())
store.dispatch(WalletAction.LoadFiatRate())
}
}

View file

@ -5,7 +5,6 @@ import com.tangem.common.services.Result
import com.tangem.network.api.tangemTech.TangemTechService
import com.tangem.network.api.tangemTech.UserTokensResponse
import com.tangem.tap.domain.NoDataError
import com.tangem.tap.features.wallet.models.Currency
class UserTokensNetworkService(private val tangemTechService: TangemTechService) {
suspend fun getUserTokens(userId: String): Result<UserTokensResponse> {
@ -22,9 +21,7 @@ class UserTokensNetworkService(private val tangemTechService: TangemTechService)
}
}
suspend fun saveUserTokens(userId: String, tokens: List<Currency>): Result<Unit> {
val tokensResponse = tokens.map { it.toTokenResponse() }
val data = UserTokensResponse(tokens = tokensResponse)
return tangemTechService.putUserTokens(userId, data)
suspend fun saveUserTokens(userId: String, tokens: UserTokensResponse): Result<Unit> {
return tangemTechService.putUserTokens(userId, tokens)
}
}

View file

@ -8,6 +8,7 @@ import com.tangem.common.extensions.toHexString
import com.tangem.common.services.Result
import com.tangem.domain.common.extensions.calculateHmacSha256
import com.tangem.network.api.tangemTech.TangemTechService
import com.tangem.network.api.tangemTech.UserTokensResponse
import com.tangem.tap.common.AndroidFileReader
import com.tangem.tap.domain.NoDataError
import com.tangem.tap.domain.tokens.models.BlockchainNetwork
@ -36,7 +37,7 @@ class UserTokensRepository(
return when (val networkResult = networkService.getUserTokens(userId)) {
is Result.Success -> {
val tokens = networkResult.data.tokens.map { Currency.fromTokenResponse(it) }
storageService.saveUserTokens(card.getUserId(), tokens)
storageService.saveUserTokens(card.getUserId(), tokens.toUserTokensResponse())
tokens
}
is Result.Failure -> {
@ -46,13 +47,24 @@ class UserTokensRepository(
}
suspend fun saveUserTokens(card: Card, tokens: List<Currency>) {
networkService.saveUserTokens(card.getUserId(), tokens)
storageService.saveUserTokens(card.getUserId(), tokens)
val userTokens = tokens.toUserTokensResponse()
networkService.saveUserTokens(card.getUserId(), userTokens)
storageService.saveUserTokens(card.getUserId(), userTokens)
}
suspend fun removeUserTokens(card: Card) {
networkService.saveUserTokens(card.getUserId(), emptyList())
storageService.saveUserTokens(card.getUserId(), emptyList())
val userTokens = emptyList<Currency>().toUserTokensResponse()
networkService.saveUserTokens(card.getUserId(), userTokens)
storageService.saveUserTokens(card.getUserId(), userTokens)
}
private fun List<Currency>.toUserTokensResponse(): UserTokensResponse {
val tokensResponse = this.map { it.toTokenResponse() }
return UserTokensResponse(
tokens = tokensResponse,
group = GROUP_DEFAULT_VALUE,
sort = SORT_DEFAULT_VALUE,
)
}
fun loadBlockchainsToDerive(card: Card): List<BlockchainNetwork> {
@ -77,7 +89,8 @@ class UserTokensRepository(
return when (error) {
is NoDataError -> {
val tokens = storageService.getUserTokens(card)
coroutineScope { launch { networkService.saveUserTokens(userId = userId, tokens = tokens) } }
val userTokens = tokens.toUserTokensResponse()
coroutineScope { launch { networkService.saveUserTokens(userId = userId, tokens = userTokens) } }
tokens
}
else -> {
@ -97,6 +110,8 @@ class UserTokensRepository(
}
companion object {
const val SORT_DEFAULT_VALUE = "manual"
const val GROUP_DEFAULT_VALUE = "none"
fun init(context: Context, tangemTechService: TangemTechService): UserTokensRepository {
val fileReader = AndroidFileReader(context)
val oldUserTokensRepository = OldUserTokensRepository(

View file

@ -34,10 +34,8 @@ class UserTokensStorageService(
return blockchainNetworks.flatMap { it.toCurrencies() }
}
fun saveUserTokens(userId: String, tokens: List<Currency>) {
val tokensResponse = tokens.map { it.toTokenResponse() }
val data = UserTokensResponse(tokens = tokensResponse)
val json = userTokensAdapter.toJson(data)
fun saveUserTokens(userId: String, tokens: UserTokensResponse) {
val json = userTokensAdapter.toJson(tokens)
fileReader.rewriteFile(json, getFileNameForUserTokens(userId))
}

View file

@ -278,7 +278,7 @@ class TokensMiddleware {
val factory = store.state.globalState.tapWalletManager.walletManagerFactory
val derivationStyle = scanResponse.card.derivationStyle
val addActions = currencyList.mapNotNull { currency ->
val addActions = currencyList.mapIndexedNotNull { index, currency ->
when (currency) {
is Currency.Blockchain -> {
val derivationPath = currency.derivationPath?.let { DerivationPath(it) }
@ -293,12 +293,12 @@ class TokensMiddleware {
scanResponse = scanResponse,
blockchain = currency.blockchain,
derivationParams = derivationParams,
) ?: return@mapNotNull null
) ?: return@mapIndexedNotNull null
val blockchainNetwork = BlockchainNetwork.fromWalletManager(walletManager)
WalletAction.MultiWallet.AddBlockchain(
blockchain = blockchainNetwork,
walletManager = walletManager,
save = true,
save = index == currencyList.lastIndex,
)
}
is Currency.Token -> {
@ -309,7 +309,7 @@ class TokensMiddleware {
WalletAction.MultiWallet.AddToken(
token = currency.token,
blockchain = blockchainNetwork,
save = true,
save = index == currencyList.lastIndex,
)
}
}

View file

@ -59,9 +59,9 @@ sealed interface Currency {
fun isToken(): Boolean = this is Token
fun toTokenResponse(): TokenResponse {
return TokenResponse(
id = coinId ?: "",
id = coinId,
networkId = blockchain.toNetworkId(),
derivationPath = derivationPath ?: DERIVATION_PATH_RAW_VALUE,
derivationPath = derivationPath,
name = currencyName,
symbol = currencySymbol,
decimals = decimals,
@ -70,7 +70,6 @@ sealed interface Currency {
}
companion object {
private const val DERIVATION_PATH_RAW_VALUE = "m/44/0'/0/0"
fun fromBlockchainNetwork(
blockchainNetwork: BlockchainNetwork,
token: com.tangem.blockchain.common.Token? = null,
@ -112,11 +111,6 @@ sealed interface Currency {
}
fun fromTokenResponse(tokenResponse: TokenResponse): Currency {
val derivationPath = if (tokenResponse.derivationPath == DERIVATION_PATH_RAW_VALUE) {
null
} else {
tokenResponse.derivationPath
}
return when {
tokenResponse.contractAddress != null -> Token(
com.tangem.blockchain.common.Token(
@ -127,11 +121,11 @@ sealed interface Currency {
id = tokenResponse.id,
),
blockchain = com.tangem.blockchain.common.Blockchain.fromNetworkId(tokenResponse.networkId)!!,
derivationPath = derivationPath,
derivationPath = tokenResponse.derivationPath,
)
else -> Blockchain(
blockchain = com.tangem.blockchain.common.Blockchain.fromNetworkId(tokenResponse.networkId)!!,
derivationPath = derivationPath,
derivationPath = tokenResponse.derivationPath,
)
}
}

View file

@ -3,7 +3,7 @@ ext.versions = [
build_gradle : '7.1.3',
// tangem_card_sdk : 'develop-164',
tangem_card_sdk : '0.0.1',
// tangem_blockchain_sdk: 'develop-122',
// tangem_blockchain_sdk: 'develop-128',
tangem_blockchain_sdk: '0.0.1',
]

View file

@ -44,7 +44,8 @@ fun Blockchain.Companion.fromNetworkId(networkId: String): Blockchain? {
"polkadot" -> Blockchain.Polkadot
"polkadot/test" -> Blockchain.PolkadotTestnet
"kusama" -> Blockchain.Kusama
"optimistic-ethereum" -> Blockchain.Optimism
// "optimistic-ethereum" -> Blockchain.Optimism
"optimistic-ethereum" -> null //TODO: Optimism is disabled until next release
"optimistic-ethereum/test" -> Blockchain.OptimismTestnet
"dash" -> Blockchain.Dash
"wxdai" -> Blockchain.SaltPay
@ -57,7 +58,7 @@ fun Blockchain.toNetworkId(): String {
return when (this) {
Blockchain.Unknown -> "unknown"
Blockchain.Arbitrum -> "arbitrum-one"
Blockchain.ArbitrumTestnet -> "arbitrum/test"
Blockchain.ArbitrumTestnet -> "arbitrum-one/test"
Blockchain.Avalanche -> "avalanche"
Blockchain.AvalancheTestnet -> "avalanche/test"
Blockchain.Binance -> "binancecoin"

View file

@ -51,15 +51,15 @@ data class GeoResponse(
data class UserTokensResponse(
val version: Int = 0,
val group: String = "",
val sort: String = "",
val group: String? = null,
val sort: String? = null,
val tokens: List<TokenResponse> = emptyList(),
) : TangemTechResponse
data class TokenResponse(
val id: String,
val id: String? = null,
val networkId: String,
val derivationPath: String,
val derivationPath: String? = null,
val name: String,
val symbol: String,
val decimals: Int,