Skip to content

Commit

Permalink
Fix tokens balances fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeeei authored and kozarezvlad committed Dec 10, 2024
1 parent 27bde28 commit 5068da7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -79,11 +78,19 @@ internal class KaspaWalletManager(
transactionBuilder.unspentOutputs = response.unspentOutputs
}

private fun updateWalletTokens(tokensInfo: List<KaspaKRC20InfoResponse>) {
private fun updateWalletTokens(tokensInfo: Map<Token, Result<KaspaKRC20InfoResponse>>) {
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)
}
}
}
}

Expand All @@ -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 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import com.tangem.blockchain.extensions.Result
import java.math.BigDecimal

interface KaspaKRC20NetworkProvider : NetworkProvider {
suspend fun getBalances(address: String, tokens: List<Token>): Result<List<KaspaKRC20InfoResponse>>
suspend fun getBalances(address: String, tokens: List<Token>): Result<Map<Token, Result<KaspaKRC20InfoResponse>>>
}

data class KaspaKRC20InfoResponse(
val token: Token,
val balance: BigDecimal,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class KaspaKRC20NetworkService(providers: List<KaspaKRC20NetworkProvider>) : Kas
override val baseUrl: String
get() = multiNetworkProvider.currentProvider.baseUrl

override suspend fun getBalances(address: String, tokens: List<Token>): Result<List<KaspaKRC20InfoResponse>> {
override suspend fun getBalances(
address: String,
tokens: List<Token>,
): Result<Map<Token, Result<KaspaKRC20InfoResponse>>> {
return multiNetworkProvider.performRequest { getBalances(address, tokens) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Token>): Result<List<KaspaKRC20InfoResponse>> {
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<Token>,
): Result<Map<Token, Result<KaspaKRC20InfoResponse>>> {
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() },
)
}
}
}

0 comments on commit 5068da7

Please sign in to comment.