@@ -3,10 +3,8 @@ package com.gemwallet.android.data.repositories.assets
33import com.gemwallet.android.blockchain.services.BalancesService
44import com.gemwallet.android.data.service.store.database.BalancesDao
55import com.gemwallet.android.data.service.store.database.entities.DbBalance
6- import com.gemwallet.android.data.service.store.database.entities.mergeDelegation
7- import com.gemwallet.android.data.service.store.database.entities.mergeNative
86import com.gemwallet.android.data.service.store.database.entities.toDTO
9- import com.gemwallet.android.data.service.store.database.entities.toRecord
7+ import com.gemwallet.android.ext.toIdentifier
108import com.gemwallet.android.model.AssetBalance
119import com.wallet.core.primitives.Account
1210import com.wallet.core.primitives.Asset
@@ -22,52 +20,106 @@ class UpdateBalances(
2220 suspend fun updateBalances (
2321 walletId : String ,
2422 account : Account ,
25- tokens : List <Asset >
23+ tokens : List <Asset >,
2624 ): List <AssetBalance > = withContext(Dispatchers .IO ) {
2725 val updatedAt = System .currentTimeMillis()
2826
29- val getNative = async { updateNativeBalance(walletId, account, updatedAt) }
30-
31- val getDelegation = async { balancesService.getDelegationBalances(account) }
32-
27+ val getNative = async { balancesService.getNativeBalances(account) }
28+ val getStake = async { balancesService.getStakeBalances(account) }
3329 val getTokens = async { updateTokensBalance(walletId, account, tokens, updatedAt) }
3430
3531 val native = getNative.await()
36- val delegation = getDelegation .await()
37- val fullNative = mergeNativeBalances(native, delegation?.toRecord(walletId, account.address, updatedAt) )
32+ val stake = getStake .await()
33+ val tokenResults = getTokens.await( )
3834
39- val tokens = getTokens.await( )
35+ val nativeResult = updateNativeBalance(walletId, account, updatedAt, native, stake )
4036
41- listOfNotNull(fullNative ) + tokens
37+ listOfNotNull(nativeResult ) + tokenResults
4238 }
4339
44- private suspend fun updateNativeBalance (walletId : String , account : Account , updatedAt : Long ): DbBalance ? {
45- val prevBalance =
46- balancesDao.getByAccount(walletId, account.address, account.chain.string)
47- val nativeBalance = balancesService.getNativeBalances(account)
48- val dbNativeBalance = DbBalance .mergeNative(
49- prevBalance,
50- nativeBalance?.toRecord(walletId, account.address, updatedAt),
51- )
52- dbNativeBalance?.let { runCatching { balancesDao.insert(it) } }
53- return dbNativeBalance
40+ private fun updateNativeBalance (
41+ walletId : String ,
42+ account : Account ,
43+ updatedAt : Long ,
44+ native : AssetBalance ? ,
45+ stake : AssetBalance ? ,
46+ ): AssetBalance ? {
47+ val assetId = account.chain.string
48+
49+ if (native == null && stake == null ) {
50+ return balancesDao.getByAccount(walletId, account.address, assetId)?.toDTO()
51+ }
52+
53+ ensureRowExists(walletId, account.address, assetId, updatedAt)
54+
55+ native?.let {
56+ balancesDao.updateCoinBalance(
57+ walletId = walletId,
58+ address = account.address,
59+ assetId = assetId,
60+ available = it.balance.available,
61+ availableAmount = it.balanceAmount.available,
62+ reserved = it.balance.reserved,
63+ reservedAmount = it.balanceAmount.reserved,
64+ isActive = it.isActive,
65+ updatedAt = updatedAt,
66+ )
67+ }
68+
69+ stake?.let {
70+ balancesDao.updateStakeBalance(
71+ walletId = walletId,
72+ address = account.address,
73+ assetId = assetId,
74+ staked = it.balance.staked,
75+ stakedAmount = it.balanceAmount.staked,
76+ frozen = it.balance.frozen,
77+ frozenAmount = it.balanceAmount.frozen,
78+ locked = it.balance.locked,
79+ lockedAmount = it.balanceAmount.locked,
80+ pending = it.balance.pending,
81+ pendingAmount = it.balanceAmount.pending,
82+ rewards = it.balance.rewards,
83+ rewardsAmount = it.balanceAmount.rewards,
84+ votes = it.metadata?.votes?.toLong() ? : 0L ,
85+ energyAvailable = it.metadata?.energyAvailable?.toLong() ? : 0L ,
86+ energyTotal = it.metadata?.energyTotal?.toLong() ? : 0L ,
87+ bandwidthAvailable = it.metadata?.bandwidthAvailable?.toLong() ? : 0L ,
88+ bandwidthTotal = it.metadata?.bandwidthTotal?.toLong() ? : 0L ,
89+ updatedAt = updatedAt,
90+ )
91+ }
92+
93+ return balancesDao.getByAccount(walletId, account.address, assetId)?.toDTO()
5494 }
5595
56- private suspend fun updateTokensBalance (walletId : String , account : Account , tokens : List <Asset >, updatedAt : Long ): List <AssetBalance > {
96+ private suspend fun updateTokensBalance (
97+ walletId : String ,
98+ account : Account ,
99+ tokens : List <Asset >,
100+ updatedAt : Long ,
101+ ): List <AssetBalance > {
57102 if (tokens.none { it.id.tokenId != null }) return emptyList()
58103 val balances = balancesService.getTokensBalances(account, tokens)
59- runCatching {
60- val record = balances.map {
61- it.toRecord(walletId, account.address, updatedAt)
62- }
63- balancesDao.insert(record)
104+ for (balance in balances) {
105+ val assetId = balance.asset.id.toIdentifier()
106+ ensureRowExists(walletId, account.address, assetId, updatedAt)
107+ balancesDao.updateTokenBalance(
108+ walletId = walletId,
109+ address = account.address,
110+ assetId = assetId,
111+ available = balance.balance.available,
112+ availableAmount = balance.balanceAmount.available,
113+ isActive = balance.isActive,
114+ updatedAt = updatedAt,
115+ )
64116 }
65117 return balances
66118 }
67119
68- private suspend fun mergeNativeBalances ( native : DbBalance ? , delegation : DbBalance ? ): AssetBalance ? = withContext( Dispatchers . IO ) {
69- val dbFullBalance = DbBalance .mergeDelegation(native, delegation)
70- dbFullBalance?. let { runCatching { balancesDao.insert(it) } }
71- dbFullBalance?.toDTO( )
120+ private fun ensureRowExists ( walletId : String , address : String , assetId : String , updatedAt : Long ) {
121+ balancesDao.insertIgnore(
122+ DbBalance (assetId = assetId, walletId = walletId, accountAddress = address, updatedAt = updatedAt)
123+ )
72124 }
73- }
125+ }
0 commit comments