diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/KaspaWalletManager.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/KaspaWalletManager.kt index 1f4e3fe7..c1ddd372 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/KaspaWalletManager.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/KaspaWalletManager.kt @@ -15,8 +15,7 @@ import com.tangem.blockchain.common.transaction.TransactionFee import com.tangem.blockchain.common.transaction.TransactionSendResult import com.tangem.blockchain.common.trustlines.AssetRequirementsCondition import com.tangem.blockchain.common.trustlines.AssetRequirementsManager -import com.tangem.blockchain.extensions.Result -import com.tangem.blockchain.extensions.SimpleResult +import com.tangem.blockchain.extensions.* import com.tangem.blockchain.extensions.map import com.tangem.common.CompletionResult import com.tangem.common.extensions.toCompressedPublicKey @@ -53,7 +52,7 @@ internal class KaspaWalletManager( val tokensBalances = if (cardTokens.isNotEmpty()) { async { krc20NetworkProvider.getBalances(wallet.address, cardTokens.toList()) }.await() } else { - Result.Success(emptyList()) + Result.Success(emptyMap()) } val coinBalance = coinBalanceDeferred.await() @@ -79,11 +78,19 @@ internal class KaspaWalletManager( transactionBuilder.unspentOutputs = response.unspentOutputs } - private fun updateWalletTokens(tokensInfo: List) { + private fun updateWalletTokens(tokensInfo: Map>) { tokensInfo.forEach { result -> - val token = result.token - val balance = result.balance - wallet.setAmount(balance, amountType = AmountType.Token(token)) + val token = result.key + val amountType = AmountType.Token(token) + when (val response = result.value) { + is Result.Success -> { + val balance = response.data.balance + wallet.setAmount(balance, amountType) + } + is Result.Failure -> { + wallet.changeAmountValue(amountType, null, null) + } + } } } @@ -101,7 +108,6 @@ internal class KaspaWalletManager( return when (val type = transactionData.amount.type) { is AmountType.Coin -> sendCoinTransaction(transactionData, signer) is AmountType.Token -> { - updateUnspentOutputs() val incompleteTokenTransaction = getIncompleteTokenTransaction(type.token) if (incompleteTokenTransaction != null && incompleteTokenTransaction.amountValue == transactionData.amount.value && diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20NetworkProvider.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20NetworkProvider.kt index 5db956a4..94e8f36a 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20NetworkProvider.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20NetworkProvider.kt @@ -6,10 +6,9 @@ import com.tangem.blockchain.extensions.Result import java.math.BigDecimal interface KaspaKRC20NetworkProvider : NetworkProvider { - suspend fun getBalances(address: String, tokens: List): Result> + suspend fun getBalances(address: String, tokens: List): Result>> } data class KaspaKRC20InfoResponse( - val token: Token, val balance: BigDecimal, ) diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20NetworkService.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20NetworkService.kt index 75c734c8..1c2256b5 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20NetworkService.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20NetworkService.kt @@ -10,7 +10,10 @@ class KaspaKRC20NetworkService(providers: List) : Kas override val baseUrl: String get() = multiNetworkProvider.currentProvider.baseUrl - override suspend fun getBalances(address: String, tokens: List): Result> { + override suspend fun getBalances( + address: String, + tokens: List, + ): Result>> { return multiNetworkProvider.performRequest { getBalances(address, tokens) } } } diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20RestApiNetworkProvider.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20RestApiNetworkProvider.kt index 33d7e84a..3c8e801a 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20RestApiNetworkProvider.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20RestApiNetworkProvider.kt @@ -16,30 +16,31 @@ open class KaspaKRC20RestApiNetworkProvider(override val baseUrl: String) : Kasp } private val decimals = Blockchain.Kaspa.decimals() - override suspend fun getBalances(address: String, tokens: List): Result> { - return try { - coroutineScope { - val tokenBalancesDeferred = tokens.associateWith { token -> - async { retryIO { api.getBalance(address, token.contractAddress).result.first() } } - } - - val tokenBalanceResponses = tokenBalancesDeferred.mapValues { it.value.await() } - - Result.Success( - tokenBalanceResponses.map { + override suspend fun getBalances( + address: String, + tokens: List, + ): Result>> { + return coroutineScope { + val tokenBalancesDeferred = tokens.associateWith { token -> + async { + retryIO { try { - KaspaKRC20InfoResponse( - token = it.key, - balance = it.value.balance!!.toBigDecimal().movePointLeft(decimals), + val response = api.getBalance(address, token.contractAddress).result.first() + Result.Success( + KaspaKRC20InfoResponse( + balance = response.balance!!.toBigDecimal().movePointLeft(decimals), + ), ) - } catch (exception: Exception) { - throw exception.toBlockchainSdkError() + } catch (e: Exception) { + Result.Failure(e.toBlockchainSdkError()) } - }, - ) + } + } } - } catch (exception: Exception) { - Result.Failure(exception.toBlockchainSdkError()) + + Result.Success( + tokenBalancesDeferred.mapValues { it.value.await() }, + ) } } }