diff --git a/src/app/modules/main/wallet_section/assets/controller.nim b/src/app/modules/main/wallet_section/assets/controller.nim index a51d31ac4e..c4afeb213b 100644 --- a/src/app/modules/main/wallet_section/assets/controller.nim +++ b/src/app/modules/main/wallet_section/assets/controller.nim @@ -34,8 +34,11 @@ proc delete*(self: Controller) = proc init*(self: Controller) = discard -proc getWalletAccountByAddress*(self: Controller, address: string): wallet_account_service.WalletAccountDto = - return self.walletAccountService.getAccountByAddress(address) +proc getWalletAccountsByAddresses*(self: Controller, addresses: seq[string]): seq[wallet_account_service.WalletAccountDto] = + return self.walletAccountService.getAccountsByAddresses(addresses) + +proc getWalletTokensByAddresses*(self: Controller, addresses: seq[string]): seq[wallet_account_service.WalletTokenDto] = + return self.walletAccountService.getTokensByAddresses(addresses) proc getChainIds*(self: Controller): seq[int] = return self.networkService.getNetworks().map(n => n.chainId) diff --git a/src/app/modules/main/wallet_section/assets/module.nim b/src/app/modules/main/wallet_section/assets/module.nim index b77e12c03e..769b47452f 100644 --- a/src/app/modules/main/wallet_section/assets/module.nim +++ b/src/app/modules/main/wallet_section/assets/module.nim @@ -91,15 +91,16 @@ proc setAssetsAndBalance(self: Module, tokens: seq[WalletTokenDto], enabledChain self.view.getAssetsModel().setItems(items) method filterChanged*(self: Module, addresses: seq[string], chainIds: seq[int]) = - let walletAccount = self.controller.getWalletAccountByAddress(addresses[0]) + let walletAccounts = self.controller.getWalletAccountsByAddresses(addresses) - let accountItem = walletAccountToWalletAssetsItem(walletAccount) + let accountItem = walletAccountToWalletAssetsItem(walletAccounts[0]) self.view.setData(accountItem) - if walletAccount.tokens.len == 0 and walletAccount.assetsLoading: + if walletAccounts[0].tokens.len == 0 and walletAccounts[0].assetsLoading: self.setLoadingAssets() else: - self.setAssetsAndBalance(walletAccount.tokens, chainIds) + let walletTokens = self.controller.getWalletTokensByAddresses(addresses) + self.setAssetsAndBalance(walletTokens, chainIds) proc onTokensRebuilt(self: Module, hasBalanceCache: bool, hasMarketValuesCache: bool) = self.view.setAssetsLoading(false) diff --git a/src/app/modules/shared/wallet_utils.nim b/src/app/modules/shared/wallet_utils.nim index 60043344ef..080d1f9f3c 100644 --- a/src/app/modules/shared/wallet_utils.nim +++ b/src/app/modules/shared/wallet_utils.nim @@ -70,8 +70,10 @@ proc walletAccountToWalletAssetsItem*(w: WalletAccountDto): wallet_assets_item.I w.assetsLoading, ) -proc walletTokenToItem*(t: WalletTokenDto, chainIds: seq[int], enabledChainIds: seq[int], currency: string, - currencyFormat: CurrencyFormatDto, tokenFormat: CurrencyFormatDto): token_item.Item = +proc walletTokenToItem*( + t: WalletTokenDto, chainIds: seq[int], enabledChainIds: seq[int], currency: string, + currencyFormat: CurrencyFormatDto, tokenFormat: CurrencyFormatDto +): token_item.Item = let marketValues = t.marketValuesPerCurrency.getOrDefault(currency) return token_item.initItem( t.name, diff --git a/src/app_service/service/wallet_account/dto.nim b/src/app_service/service/wallet_account/dto.nim index b429248216..722c2800b0 100644 --- a/src/app_service/service/wallet_account/dto.nim +++ b/src/app_service/service/wallet_account/dto.nim @@ -178,6 +178,23 @@ proc `$`*(self: WalletAccountDto): string = proc getCurrencyBalance*(self: BalanceDto, currencyPrice: float64): float64 = return self.balance * currencyPrice +proc copyToken*(self: WalletTokenDto): WalletTokenDto = + result = WalletTokenDto() + result.name = self.name + result.symbol = self.symbol + result.decimals = self.decimals + result.color = self.color + result.description = self.description + result.assetWebsiteUrl = self.assetWebsiteUrl + result.builtOn = self.builtOn + + result.balancesPerChain = initTable[int, BalanceDto]() + for chainId, balanceDto in self.balancesPerChain: + result.balancesPerChain[chainId] = balanceDto + result.marketValuesPerCurrency = initTable[string, TokenMarketValuesDto]() + for chainId, tokenMarketValuesDto in self.marketValuesPerCurrency: + result.marketValuesPerCurrency[chainId] = tokenMarketValuesDto + proc getAddress*(self: WalletTokenDto): string = for balance in self.balancesPerChain.values: return balance.address diff --git a/src/app_service/service/wallet_account/service.nim b/src/app_service/service/wallet_account/service.nim index 6d012bcdd1..6c8fee99f6 100644 --- a/src/app_service/service/wallet_account/service.nim +++ b/src/app_service/service/wallet_account/service.nim @@ -274,6 +274,30 @@ QtObject: return result = self.walletAccounts[address] + proc getAccountsByAddresses*(self: Service, addresses: seq[string]): seq[WalletAccountDto] = + for address in addresses: + result.add(self.getAccountByAddress(address)) + + proc getTokensByAddresses*(self: Service, addresses: seq[string]): seq[WalletTokenDto] = + var tokens = initTable[string, WalletTokenDto]() + for address in addresses: + let walletAccount = self.getAccountByAddress(address) + for token in walletAccount.tokens: + if not tokens.hasKey(token.symbol): + let newToken = token.copyToken() + tokens[token.symbol] = newToken + continue + + for chainId, balanceDto in token.balancesPerChain: + if not tokens[token.symbol].balancesPerChain.hasKey(chainId): + tokens[token.symbol].balancesPerChain[chainId] = balanceDto + continue + + tokens[token.symbol].balancesPerChain[chainId].balance += balanceDto.balance + + result = toSeq(tokens.values) + result.sort(priorityTokenCmp) + proc getWalletAccounts*(self: Service): seq[WalletAccountDto] = result = toSeq(self.walletAccounts.values)