From 21fedcbb2bc4d40b3a1027006abf89517ad4f9f3 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 25 May 2022 10:40:49 +0200 Subject: [PATCH] feat(@wallet): aggregate token balance --- .../browser_section/current_account/view.nim | 4 +- .../main/wallet_section/accounts/module.nim | 4 +- .../wallet_section/current_account/view.nim | 4 +- .../service/wallet_account/dto.nim | 12 ++-- .../service/wallet_account/service.nim | 70 +++++++++++++++---- vendor/status-go | 2 +- 6 files changed, 70 insertions(+), 26 deletions(-) diff --git a/src/app/modules/main/browser_section/current_account/view.nim b/src/app/modules/main/browser_section/current_account/view.nim index 1e3dd11db5..776a74f800 100644 --- a/src/app/modules/main/browser_section/current_account/view.nim +++ b/src/app/modules/main/browser_section/current_account/view.nim @@ -157,9 +157,9 @@ proc setData*(self: View, dto: wallet_account_service.WalletAccountDto) = dto.tokens.map(t => token_item.initItem( t.name, t.symbol, - t.balance, + t.balance.chainBalance, t.address, - t.currencyBalance, + t.balance.currencyBalance, )) ) self.assets = assets diff --git a/src/app/modules/main/wallet_section/accounts/module.nim b/src/app/modules/main/wallet_section/accounts/module.nim index cf9e1b2941..e7fda92a4a 100644 --- a/src/app/modules/main/wallet_section/accounts/module.nim +++ b/src/app/modules/main/wallet_section/accounts/module.nim @@ -46,9 +46,9 @@ method refreshWalletAccounts*(self: Module) = w.tokens.map(t => token_item.initItem( t.name, t.symbol, - t.balance, + t.balance.chainBalance, t.address, - t.currencyBalance, + t.balance.currencyBalance, )) ) diff --git a/src/app/modules/main/wallet_section/current_account/view.nim b/src/app/modules/main/wallet_section/current_account/view.nim index 1e11e03a26..926c654168 100644 --- a/src/app/modules/main/wallet_section/current_account/view.nim +++ b/src/app/modules/main/wallet_section/current_account/view.nim @@ -171,9 +171,9 @@ QtObject: dto.tokens.map(t => token_item.initItem( t.name, t.symbol, - t.balance, + t.balance.chainBalance, t.address, - t.currencyBalance, + t.balance.currencyBalance, )) ) self.assets = assets diff --git a/src/app_service/service/wallet_account/dto.nim b/src/app_service/service/wallet_account/dto.nim index 263fdc5f1c..a13059afef 100644 --- a/src/app_service/service/wallet_account/dto.nim +++ b/src/app_service/service/wallet_account/dto.nim @@ -1,7 +1,11 @@ -import json, sequtils, sugar +import tables, json, sequtils, sugar include ../../common/json_utils +type BalanceDto* = ref object of RootObj + chainBalance*: float64 + currencyBalance*: float64 + type WalletTokenDto* = ref object of RootObj name*: string @@ -11,8 +15,8 @@ type hasIcon*: bool color*: string isCustom*: bool - balance*: float64 - currencyBalance*: float64 + balance*: BalanceDto + balances*: Table[int, BalanceDto] type WalletAccountDto* = ref object of RootObj @@ -69,4 +73,4 @@ proc toWalletAccountDto*(jsonObj: JsonNode): WalletAccountDto = discard jsonObj.getProp("derived-from", result.derivedfrom) proc getCurrencyBalance*(self: WalletAccountDto): float64 = - return self.tokens.map(t => t.currencyBalance).foldl(a + b, 0.0) + return self.tokens.map(t => t.balance.currencyBalance).foldl(a + b, 0.0) diff --git a/src/app_service/service/wallet_account/service.nim b/src/app_service/service/wallet_account/service.nim index dc4c8abaf2..905b027c19 100644 --- a/src/app_service/service/wallet_account/service.nim +++ b/src/app_service/service/wallet_account/service.nim @@ -127,35 +127,72 @@ QtObject: self: Service, account: WalletAccountDto, prices: Table[string, float], - balances: JsonNode + tokenBalances: JsonNode ): seq[WalletTokenDto] = + var groupedNetwork = initTable[string, seq[NetworkDto]]() for network in self.networkService.getEnabledNetworks(): - let balance = fetchNativeChainBalance(network, account.address) + if not groupedNetwork.hasKey(network.nativeCurrencyName): + groupedNetwork[network.nativeCurrencyName] = @[] + + groupedNetwork[network.nativeCurrencyName].add(network) + for currencyName, networks in groupedNetwork.pairs: + var balances = initTable[int, BalanceDto]() + for network in networks: + let chainBalance = fetchNativeChainBalance(network, account.address) + balances[network.chainId] = BalanceDto( + chainBalance: chainBalance, + currencyBalance: chainBalance * prices[network.nativeCurrencySymbol] + ) + + let totalChainBalance = toSeq(balances.values).map(x => x.chainBalance).foldl(a + b) + let balance = BalanceDto( + chainBalance: totalChainBalance, + currencyBalance: totalChainBalance * prices[networks[0].nativeCurrencySymbol] + ) result.add(WalletTokenDto( - name: network.chainName, + name: currencyName, address: "0x0000000000000000000000000000000000000000", - symbol: network.nativeCurrencySymbol, - decimals: network.nativeCurrencyDecimals, + symbol: networks[0].nativeCurrencySymbol, + decimals: networks[0].nativeCurrencyDecimals, hasIcon: true, color: "blue", isCustom: false, balance: balance, - currencyBalance: balance * prices[network.nativeCurrencySymbol] + balances: balances )) + var groupedToken = initTable[string, seq[TokenDto]]() for token in self.tokenService.getVisibleTokens(): - let balance = parsefloat(hex2Balance(balances{token.addressAsString()}.getStr, token.decimals)) + if not groupedToken.hasKey(token.symbol): + groupedToken[token.symbol] = @[] + groupedToken[token.symbol].add(token) + + for symbol, tokens in groupedToken.pairs: + var balances = initTable[int, BalanceDto]() + for token in tokens: + let chainBalance = parsefloat(hex2Balance(tokenBalances{token.addressAsString()}.getStr, token.decimals)) + balances[token.chainId] = BalanceDto( + chainBalance: chainBalance, + currencyBalance: chainBalance * prices[symbol] + ) + + let totalChainBalance = toSeq(balances.values).map(x => x.chainBalance).foldl(a + b) + let balance = BalanceDto( + chainBalance: totalChainBalance, + currencyBalance: totalChainBalance * prices[symbol] + ) + result.add( WalletTokenDto( - name: token.name, - address: $token.address, - symbol: token.symbol, - decimals: token.decimals, - hasIcon: token.hasIcon, - color: token.color, - isCustom: token.isCustom, + name: tokens[0].name, + address: $tokens[0].address, + symbol: symbol, + decimals: tokens[0].decimals, + hasIcon: tokens[0].hasIcon, + color: tokens[0].color, + isCustom: tokens[0].isCustom, balance: balance, - currencyBalance: balance * prices[token.symbol] + balances: balances ) ) @@ -190,6 +227,9 @@ QtObject: symbols.add(token.symbol) var prices = initTable[string, float]() + if symbols.len == 0: + return prices + try: let response = backend.fetchPrices(symbols, currency) for (symbol, value) in response.result.pairs: diff --git a/vendor/status-go b/vendor/status-go index 78cba969cc..97a99d6254 160000 --- a/vendor/status-go +++ b/vendor/status-go @@ -1 +1 @@ -Subproject commit 78cba969cc90f463184509c6bde9bcb44d7bb3f9 +Subproject commit 97a99d625425b4e705116ba75c9091ebfd118485