refactor(@desktop/wallet): tokens removed from `WalletAccountDto` and cached separately in wallet accounts service

This commit is contained in:
Sale Djenic 2023-08-02 15:50:03 +02:00 committed by saledjenic
parent 838aa215e0
commit 23426f184b
13 changed files with 126 additions and 87 deletions

View File

@ -59,3 +59,9 @@ proc getCurrencyFormat*(self: Controller, symbol: string): CurrencyFormatDto =
proc areTestNetworksEnabled*(self: Controller): bool = proc areTestNetworksEnabled*(self: Controller): bool =
return self.walletAccountService.areTestNetworksEnabled() return self.walletAccountService.areTestNetworksEnabled()
proc getTokensByAddress*(self: Controller, address: string): seq[WalletTokenDto] =
return self.walletAccountService.getTokensByAddress(address)
proc getCurrencyBalance*(self: Controller, address: string, chainIds: seq[int], currency: string): float64 =
return self.walletAccountService.getCurrencyBalance(address, chainIds, currency)

View File

@ -68,18 +68,19 @@ proc switchAccount*(self: Module, accountIndex: int) =
let enabledChainIds = self.controller.getEnabledChainIds() let enabledChainIds = self.controller.getEnabledChainIds()
let areTestNetworksEnabled = self.controller.areTestNetworksEnabled() let areTestNetworksEnabled = self.controller.areTestNetworksEnabled()
let currencyFormat = self.controller.getCurrencyFormat(currency) let currencyFormat = self.controller.getCurrencyFormat(currency)
let currencyBalance = self.controller.getCurrencyBalance(walletAccount.address, enabledChainIds, currency)
let tokens = self.controller.getTokensByAddress(walletAccount.address)
let accountItem = walletAccountToWalletAccountsItem( let accountItem = walletAccountToWalletAccountsItem(
walletAccount, walletAccount,
keycardAccount, keycardAccount,
enabledChainIds, currencyBalance,
currency,
currencyFormat, currencyFormat,
areTestNetworksEnabled areTestNetworksEnabled
) )
self.view.setData(accountItem) self.view.setData(accountItem)
self.setAssets(walletAccount.tokens) self.setAssets(tokens)
method load*(self: Module) = method load*(self: Module) =
singletonInstance.engine.setRootContextProperty("browserSectionCurrentAccount", newQVariant(self.view)) singletonInstance.engine.setRootContextProperty("browserSectionCurrentAccount", newQVariant(self.view))
@ -131,7 +132,10 @@ proc onTokensRebuilt(self: Module, accountsTokens: OrderedTable[string, seq[Wall
proc onCurrencyFormatsUpdated(self: Module) = proc onCurrencyFormatsUpdated(self: Module) =
# Update assets # Update assets
let walletAccount = self.controller.getWalletAccount(self.currentAccountIndex) let walletAccount = self.controller.getWalletAccount(self.currentAccountIndex)
self.setAssets(walletAccount.tokens) if walletAccount.isNil:
return
let tokens = self.controller.getTokensByAddress(walletAccount.address)
self.setAssets(tokens)
method findTokenSymbolByAddress*(self: Module, address: string): string = method findTokenSymbolByAddress*(self: Module, address: string): string =
return self.controller.findTokenSymbolByAddress(address) return self.controller.findTokenSymbolByAddress(address)

View File

@ -67,3 +67,6 @@ proc updateWalletAccountTestPreferredChains*(self: Controller, address, preferre
proc areTestNetworksEnabled*(self: Controller): bool = proc areTestNetworksEnabled*(self: Controller): bool =
return self.walletAccountService.areTestNetworksEnabled() return self.walletAccountService.areTestNetworksEnabled()
proc getCurrencyBalance*(self: Controller, address: string, chainIds: seq[int], currency: string): float64 =
return self.walletAccountService.getCurrencyBalance(address, chainIds, currency)

View File

@ -49,16 +49,16 @@ method filterChanged*(self: Module, addresses: seq[string], chainIds: seq[int])
let areTestNetworksEnabled = self.controller.areTestNetworksEnabled() let areTestNetworksEnabled = self.controller.areTestNetworksEnabled()
let items = walletAccounts.map(w => (block: let items = walletAccounts.map(w => (block:
let keycardAccount = self.controller.isKeycardAccount(w) let keycardAccount = self.controller.isKeycardAccount(w)
let currencyBalance = self.controller.getCurrencyBalance(w.address, chainIds, currency)
walletAccountToWalletAccountsItem( walletAccountToWalletAccountsItem(
w, w,
keycardAccount, keycardAccount,
chainIds, currencyBalance,
currency,
currencyFormat, currencyFormat,
areTestNetworksEnabled areTestNetworksEnabled
) )
)) ))
self.view.setItems(items) self.view.setItems(items)
method load*(self: Module) = method load*(self: Module) =
singletonInstance.engine.setRootContextProperty("walletSectionAccounts", newQVariant(self.view)) singletonInstance.engine.setRootContextProperty("walletSectionAccounts", newQVariant(self.view))

View File

@ -12,7 +12,7 @@ type
networkService: network_service.Service networkService: network_service.Service
tokenService: token_service.Service tokenService: token_service.Service
currencyService: currency_service.Service currencyService: currency_service.Service
proc newController*( proc newController*(
delegate: io_interface.AccessInterface, delegate: io_interface.AccessInterface,
walletAccountService: wallet_account_service.Service, walletAccountService: wallet_account_service.Service,
@ -39,7 +39,7 @@ proc getWalletAccountsByAddresses*(self: Controller, addresses: seq[string]): se
proc getWalletTokensByAddresses*(self: Controller, addresses: seq[string]): seq[wallet_account_service.WalletTokenDto] = proc getWalletTokensByAddresses*(self: Controller, addresses: seq[string]): seq[wallet_account_service.WalletTokenDto] =
return self.walletAccountService.getTokensByAddresses(addresses) return self.walletAccountService.getTokensByAddresses(addresses)
proc getChainIds*(self: Controller): seq[int] = proc getChainIds*(self: Controller): seq[int] =
return self.networkService.getNetworks().map(n => n.chainId) return self.networkService.getNetworks().map(n => n.chainId)
proc getCurrentCurrency*(self: Controller): string = proc getCurrentCurrency*(self: Controller): string =

View File

@ -59,7 +59,7 @@ method load*(self: Module) =
self.events.on(SIGNAL_WALLET_ACCOUNT_TOKENS_REBUILT) do(e:Args): self.events.on(SIGNAL_WALLET_ACCOUNT_TOKENS_REBUILT) do(e:Args):
let arg = TokensPerAccountArgs(e) let arg = TokensPerAccountArgs(e)
self.onTokensRebuilt(arg.hasBalanceCache, arg.hasMarketValuesCache) self.onTokensRebuilt(arg.hasBalanceCache, arg.hasMarketValuesCache)
self.events.on(SIGNAL_NETWORK_DISCONNECTED) do(e: Args): self.events.on(SIGNAL_NETWORK_DISCONNECTED) do(e: Args):
if self.view.getAssetsModel().getCount() == 0: if self.view.getAssetsModel().getCount() == 0:
self.setLoadingAssets() self.setLoadingAssets()
@ -86,16 +86,16 @@ proc setAssetsAndBalance(self: Module, tokens: seq[WalletTokenDto], enabledChain
let items = tokens.map(t => walletTokenToItem(t, chainIds, enabledChainIds, currency, currencyFormat, self.controller.getCurrencyFormat(t.symbol))) let items = tokens.map(t => walletTokenToItem(t, chainIds, enabledChainIds, currency, currencyFormat, self.controller.getCurrencyFormat(t.symbol)))
let totalCurrencyBalanceForAllAssets = tokens.map(t => t.getCurrencyBalance(enabledChainIds, currency)).foldl(a + b, 0.0) let totalCurrencyBalanceForAllAssets = tokens.map(t => t.getCurrencyBalance(enabledChainIds, currency)).foldl(a + b, 0.0)
self.view.getAssetsModel().setItems(items) self.view.getAssetsModel().setItems(items)
method filterChanged*(self: Module, addresses: seq[string], chainIds: seq[int]) = method filterChanged*(self: Module, addresses: seq[string], chainIds: seq[int]) =
let walletAccounts = self.controller.getWalletAccountsByAddresses(addresses) let walletAccounts = self.controller.getWalletAccountsByAddresses(addresses)
let accountItem = walletAccountToWalletAssetsItem(walletAccounts[0]) let accountItem = walletAccountToWalletAssetsItem(walletAccounts[0])
self.view.setData(accountItem) self.view.setData(accountItem)
if walletAccounts[0].tokens.len == 0 and walletAccounts[0].assetsLoading: if walletAccounts[0].assetsLoading:
self.setLoadingAssets() self.setLoadingAssets()
else: else:
let walletTokens = self.controller.getWalletTokensByAddresses(addresses) let walletTokens = self.controller.getWalletTokensByAddresses(addresses)

View File

@ -104,3 +104,9 @@ proc suggestedFees*(self: Controller, chainId: int): string =
proc areTestNetworksEnabled*(self: Controller): bool = proc areTestNetworksEnabled*(self: Controller): bool =
return self.walletAccountService.areTestNetworksEnabled() return self.walletAccountService.areTestNetworksEnabled()
proc getTokensByAddress*(self: Controller, address: string): seq[WalletTokenDto] =
return self.walletAccountService.getTokensByAddress(address)
proc getCurrencyBalance*(self: Controller, address: string, chainIds: seq[int], currency: string): float64 =
return self.walletAccountService.getCurrencyBalance(address, chainIds, currency)

View File

@ -65,14 +65,18 @@ method refreshWalletAccounts*(self: Module) =
let areTestNetworksEnabled = self.controller.areTestNetworksEnabled() let areTestNetworksEnabled = self.controller.areTestNetworksEnabled()
let items = walletAccounts.map(w => (block: let items = walletAccounts.map(w => (block:
let tokens = self.controller.getTokensByAddress(w.address)
let tokenFormats = collect(initTable()): let tokenFormats = collect(initTable()):
for t in w.tokens: {t.symbol: self.controller.getCurrencyFormat(t.symbol)} for t in tokens: {t.symbol: self.controller.getCurrencyFormat(t.symbol)}
let currencyBalance = self.controller.getCurrencyBalance(w.address, enabledChainIds, currency)
walletAccountToWalletSendAccountItem( walletAccountToWalletSendAccountItem(
w, w,
tokens,
chainIds, chainIds,
enabledChainIds, enabledChainIds,
currency, currency,
currencyBalance,
currencyFormat, currencyFormat,
tokenFormats, tokenFormats,
areTestNetworksEnabled, areTestNetworksEnabled,

View File

@ -44,15 +44,15 @@ proc walletAccountToWalletAccountItem*(w: WalletAccountDto, keycardAccount: bool
w.testPreferredChainIds w.testPreferredChainIds
) )
proc walletAccountToWalletAccountsItem*(w: WalletAccountDto, keycardAccount: bool, enabledChainIds: seq[int], currency: string, proc walletAccountToWalletAccountsItem*(w: WalletAccountDto, keycardAccount: bool,
currencyFormat: CurrencyFormatDto, areTestNetworksEnabled: bool): wallet_accounts_item.Item = currencyBalance: float64, currencyFormat: CurrencyFormatDto, areTestNetworksEnabled: bool): wallet_accounts_item.Item =
return wallet_accounts_item.initItem( return wallet_accounts_item.initItem(
w.name, w.name,
w.address, w.address,
w.path, w.path,
w.colorId, w.colorId,
w.walletType, w.walletType,
currencyAmountToItem(w.getCurrencyBalance(enabledChainIds, currency), currencyFormat), currencyAmountToItem(currencyBalance, currencyFormat),
w.emoji, w.emoji,
w.keyUid, w.keyUid,
w.createdAt, w.createdAt,
@ -101,11 +101,11 @@ proc walletTokenToItem*(
loading = false loading = false
) )
proc walletAccountToWalletSendAccountItem*(w: WalletAccountDto, chainIds: seq[int], enabledChainIds: seq[int], currency: string, proc walletAccountToWalletSendAccountItem*(w: WalletAccountDto, tokens: seq[WalletTokenDto], chainIds: seq[int], enabledChainIds: seq[int], currency: string,
currencyFormat: CurrencyFormatDto, tokenFormats: Table[string, CurrencyFormatDto], areTestNetworksEnabled: bool): wallet_send_account_item.AccountItem = currencyBalance: float64, currencyFormat: CurrencyFormatDto, tokenFormats: Table[string, CurrencyFormatDto], areTestNetworksEnabled: bool): wallet_send_account_item.AccountItem =
let assets = token_model.newModel() let assets = token_model.newModel()
assets.setItems( assets.setItems(
w.tokens.map(t => walletTokenToItem(t, chainIds, enabledChainIds, currency, currencyFormat, tokenFormats[t.symbol])) tokens.map(t => walletTokenToItem(t, chainIds, enabledChainIds, currency, currencyFormat, tokenFormats[t.symbol]))
) )
return wallet_send_account_item.newAccountItem( return wallet_send_account_item.newAccountItem(
w.name, w.name,
@ -114,7 +114,7 @@ proc walletAccountToWalletSendAccountItem*(w: WalletAccountDto, chainIds: seq[in
w.emoji, w.emoji,
w.walletType, w.walletType,
assets, assets,
currencyAmountToItem(w.getCurrencyBalance(enabledChainIds, currency), currencyFormat), currencyAmountToItem(currencyBalance, currencyFormat),
areTestNetworksEnabled, areTestNetworksEnabled,
w.prodPreferredChainIds, w.prodPreferredChainIds,
w.testPreferredChainIds w.testPreferredChainIds

View File

@ -631,7 +631,7 @@ proc authenticateUser*(self: Controller, keyUid = "") =
proc getWalletAccounts*(self: Controller): seq[wallet_account_service.WalletAccountDto] = proc getWalletAccounts*(self: Controller): seq[wallet_account_service.WalletAccountDto] =
if not serviceApplicable(self.walletAccountService): if not serviceApplicable(self.walletAccountService):
return return
return self.walletAccountService.getAccounts() return self.walletAccountService.getWalletAccounts()
proc getKeypairs*(self: Controller): seq[wallet_account_service.KeypairDto] = proc getKeypairs*(self: Controller): seq[wallet_account_service.KeypairDto] =
if not serviceApplicable(self.walletAccountService): if not serviceApplicable(self.walletAccountService):

View File

@ -257,7 +257,7 @@ QtObject:
self.events.emit(SIGNAL_COMMUNITY_TOKEN_DEPLOY_STATUS, data) self.events.emit(SIGNAL_COMMUNITY_TOKEN_DEPLOY_STATUS, data)
except Exception as e: except Exception as e:
error "Error processing Collectible deployment pending transaction event", msg=e.msg, receivedData error "Error processing Collectible deployment pending transaction event", msg=e.msg, receivedData
self.events.on(PendingTransactionTypeDto.AirdropCommunityToken.event) do(e: Args): self.events.on(PendingTransactionTypeDto.AirdropCommunityToken.event) do(e: Args):
let receivedData = TransactionMinedArgs(e) let receivedData = TransactionMinedArgs(e)
@ -374,7 +374,7 @@ QtObject:
except RpcException as e: except RpcException as e:
error "Error deploying contract", message = getCurrentExceptionMsg() error "Error deploying contract", message = getCurrentExceptionMsg()
let data = CommunityTokenDeployedStatusArgs(communityId: communityId, let data = CommunityTokenDeployedStatusArgs(communityId: communityId,
deployState: DeployState.Failed) deployState: DeployState.Failed)
self.events.emit(SIGNAL_COMMUNITY_TOKEN_DEPLOY_STATUS, data) self.events.emit(SIGNAL_COMMUNITY_TOKEN_DEPLOY_STATUS, data)
@ -696,14 +696,10 @@ QtObject:
error "Error computing eth value", msg = e.msg error "Error computing eth value", msg = e.msg
proc getWalletBalanceForChain(self:Service, walletAddress: string, chainId: int): float = proc getWalletBalanceForChain(self:Service, walletAddress: string, chainId: int): float =
let wallet = self.walletAccountService.getAccountByAddress(walletAddress.toLower()) let tokens = self.walletAccountService.getTokensByAddress(walletAddress.toLower())
var balance = 0.0
let tokens = wallet.tokens
for token in tokens: for token in tokens:
if token.symbol == ethSymbol: if token.symbol == ethSymbol:
balance = token.balancesPerChain[chainId].balance return token.balancesPerChain[chainId].balance
break
return balance
proc createComputeFeeArgsFromEthAndBalance(self: Service, ethValue: float, balance: float): ComputeFeeArgs = proc createComputeFeeArgsFromEthAndBalance(self: Service, ethValue: float, balance: float): ComputeFeeArgs =
let fiatValue = self.getFiatValue(ethValue, ethSymbol) let fiatValue = self.getFiatValue(ethValue, ethSymbol)

View File

@ -1,4 +1,4 @@
import tables, json, strformat, sequtils, sugar, strutils import tables, json, strformat, strutils
import token_dto import token_dto
@ -27,7 +27,6 @@ type
walletType*: string walletType*: string
isWallet*: bool isWallet*: bool
isChat*: bool isChat*: bool
tokens*: seq[WalletTokenDto]
emoji*: string emoji*: string
ens*: string ens*: string
assetsLoading*: bool assetsLoading*: bool
@ -76,13 +75,11 @@ proc `$`*(self: WalletAccountDto): string =
walletType: {self.walletType}, walletType: {self.walletType},
isChat: {self.isChat}, isChat: {self.isChat},
emoji: {self.emoji}, emoji: {self.emoji},
assetsLoading: {self.assetsLoading},
hasBalanceCache: {self.hasBalanceCache}, hasBalanceCache: {self.hasBalanceCache},
hasMarketValuesCache: {self.hasMarketValuesCache}, hasMarketValuesCache: {self.hasMarketValuesCache},
removed: {self.removed} removed: {self.removed},
operable: {self.operable} operable: {self.operable},
prodPreferredChainIds: {self.prodPreferredChainIds} prodPreferredChainIds: {self.prodPreferredChainIds},
testPreferredChainIds: {self.testPreferredChainIds} testPreferredChainIds: {self.testPreferredChainIds}
]""" ]"""
proc getCurrencyBalance*(self: WalletAccountDto, chainIds: seq[int], currency: string): float64 =
return self.tokens.map(t => t.getCurrencyBalance(chainIds, currency)).foldl(a + b, 0.0)

View File

@ -141,6 +141,7 @@ QtObject:
networkService: network_service.Service networkService: network_service.Service
currencyService: currency_service.Service currencyService: currency_service.Service
walletAccounts: OrderedTable[string, WalletAccountDto] walletAccounts: OrderedTable[string, WalletAccountDto]
accountsTokens*: Table[string, seq[WalletTokenDto]] ## [address, seq[WalletTokenDto]]
# Forward declaration # Forward declaration
proc buildAllTokens(self: Service, accounts: seq[string], store: bool) proc buildAllTokens(self: Service, accounts: seq[string], store: bool)
@ -236,9 +237,9 @@ QtObject:
proc storeTokensForAccount*(self: Service, address: string, tokens: seq[WalletTokenDto], areBalancesCached: bool, areMarketValuesCached: bool) = proc storeTokensForAccount*(self: Service, address: string, tokens: seq[WalletTokenDto], areBalancesCached: bool, areMarketValuesCached: bool) =
if self.walletAccounts.hasKey(address): if self.walletAccounts.hasKey(address):
deepCopy(self.walletAccounts[address].tokens, tokens)
self.walletAccounts[address].hasBalanceCache = areBalancesCached self.walletAccounts[address].hasBalanceCache = areBalancesCached
self.walletAccounts[address].hasMarketValuesCache = areMarketValuesCached self.walletAccounts[address].hasMarketValuesCache = areMarketValuesCached
self.accountsTokens[address] = tokens
proc allBalancesForAllTokensHaveError(tokens: seq[WalletTokenDto]): bool = proc allBalancesForAllTokensHaveError(tokens: seq[WalletTokenDto]): bool =
for token in tokens: for token in tokens:
@ -266,40 +267,47 @@ QtObject:
return true return true
return false return false
proc updateReceivedTokens*(self: Service, address: string, tokens: var seq[WalletTokenDto]) = proc walletAccountsContainsAddress(self: Service, address: string): bool =
if not self.walletAccounts.hasKey(address) or
self.walletAccounts[address].tokens.len == 0:
return
let allBalancesForAllTokensHaveError = allBalancesForAllTokensHaveError(tokens)
let allMarketValuesForAllTokensHaveError = allMarketValuesForAllTokensHaveError(tokens)
for waToken in self.walletAccounts[address].tokens:
for token in tokens.mitems:
if waToken.name == token.name:
if allBalancesForAllTokensHaveError:
token.balancesPerChain = waToken.balancesPerChain
if allMarketValuesForAllTokensHaveError:
token.marketValuesPerCurrency = waToken.marketValuesPerCurrency
proc walletAccountsContainsAddress*(self: Service, address: string): bool =
return self.walletAccounts.hasKey(address) return self.walletAccounts.hasKey(address)
proc getAccountByAddress*(self: Service, address: string): WalletAccountDto = proc getAccountByAddress*(self: Service, address: string): WalletAccountDto =
result = WalletAccountDto()
if not self.walletAccountsContainsAddress(address): if not self.walletAccountsContainsAddress(address):
return return
result = self.walletAccounts[address] return self.walletAccounts[address]
proc updateReceivedTokens*(self: Service, address: string, tokens: var seq[WalletTokenDto]) =
let acc = self.getAccountByAddress(address)
if acc.isNil or not self.accountsTokens.hasKey(address):
return
let allBalancesForAllTokensHaveError = allBalancesForAllTokensHaveError(tokens)
let allMarketValuesForAllTokensHaveError = allMarketValuesForAllTokensHaveError(tokens)
for storedToken in self.accountsTokens[address]:
for token in tokens.mitems:
if storedToken.name == token.name:
if allBalancesForAllTokensHaveError:
token.balancesPerChain = storedToken.balancesPerChain
if allMarketValuesForAllTokensHaveError:
token.marketValuesPerCurrency = storedToken.marketValuesPerCurrency
proc getAccountsByAddresses*(self: Service, addresses: seq[string]): seq[WalletAccountDto] = proc getAccountsByAddresses*(self: Service, addresses: seq[string]): seq[WalletAccountDto] =
for address in addresses: for address in addresses:
result.add(self.getAccountByAddress(address)) let acc = self.getAccountByAddress(address)
if acc.isNil:
continue
result.add(acc)
proc getTokensByAddress*(self: Service, address: string): seq[WalletTokenDto] =
if not self.accountsTokens.hasKey(address):
return
return self.accountsTokens[address]
proc getTokensByAddresses*(self: Service, addresses: seq[string]): seq[WalletTokenDto] = proc getTokensByAddresses*(self: Service, addresses: seq[string]): seq[WalletTokenDto] =
var tokens = initTable[string, WalletTokenDto]() var tokens = initTable[string, WalletTokenDto]()
for address in addresses: for address in addresses:
let walletAccount = self.getAccountByAddress(address) if not self.accountsTokens.hasKey(address):
for token in walletAccount.tokens: continue
for token in self.accountsTokens[address]:
if not tokens.hasKey(token.symbol): if not tokens.hasKey(token.symbol):
let newToken = token.copyToken() let newToken = token.copyToken()
tokens[token.symbol] = newToken tokens[token.symbol] = newToken
@ -327,6 +335,7 @@ QtObject:
proc init*(self: Service) = proc init*(self: Service) =
try: try:
let chainId = self.networkService.getNetworkForEns().chainId
let accounts = self.getAccounts() let accounts = self.getAccounts()
for account in accounts: for account in accounts:
let account = account # TODO https://github.com/nim-lang/Nim/issues/16740 let account = account # TODO https://github.com/nim-lang/Nim/issues/16740
@ -399,6 +408,7 @@ QtObject:
return return
proc addNewAccountToLocalStoreAndNotify(self: Service, notify: bool = true) = proc addNewAccountToLocalStoreAndNotify(self: Service, notify: bool = true) =
let chainId = self.networkService.getNetworkForEns().chainId
let accounts = self.getAccounts() let accounts = self.getAccounts()
var newAccount: WalletAccountDto var newAccount: WalletAccountDto
var found = false var found = false
@ -587,8 +597,7 @@ QtObject:
except Exception as e: except Exception as e:
error "error: ", procName="deleteKeypair", errName = e.name, errDesription = e.msg error "error: ", procName="deleteKeypair", errName = e.name, errDesription = e.msg
proc getCurrency*(self: Service): string =
return self.settingsService.getCurrency()
proc updateCurrency*(self: Service, newCurrency: string) = proc updateCurrency*(self: Service, newCurrency: string) =
discard self.settingsService.saveCurrency(newCurrency) discard self.settingsService.saveCurrency(newCurrency)
@ -610,6 +619,9 @@ QtObject:
return false return false
try: try:
var account = self.getAccountByAddress(address) var account = self.getAccountByAddress(address)
if account.isNil:
error "on account for given address", procName="updateWalletAccount"
return false
let response = status_go_accounts.updateAccount(accountName, account.address, account.path, account.publicKey, let response = status_go_accounts.updateAccount(accountName, account.address, account.path, account.publicKey,
account.keyUid, account.walletType, colorId, emoji, account.isWallet, account.isChat, account.prodPreferredChainIds, account.testPreferredChainIds) account.keyUid, account.walletType, colorId, emoji, account.isWallet, account.isChat, account.prodPreferredChainIds, account.testPreferredChainIds)
if not response.error.isNil: if not response.error.isNil:
@ -627,6 +639,9 @@ QtObject:
return false return false
try: try:
var account = self.getAccountByAddress(address) var account = self.getAccountByAddress(address)
if account.isNil:
error "on account for given address", procName="updateWalletAccount"
return false
let response = status_go_accounts.updateAccount(account.name, account.address, account.path, account.publicKey, let response = status_go_accounts.updateAccount(account.name, account.address, account.path, account.publicKey,
account.keyUid, account.walletType, account.colorId, account.emoji, account.isWallet, account.isChat, preferredChainIds, account.testPreferredChainIds) account.keyUid, account.walletType, account.colorId, account.emoji, account.isWallet, account.isChat, preferredChainIds, account.testPreferredChainIds)
if not response.error.isNil: if not response.error.isNil:
@ -644,6 +659,9 @@ QtObject:
return false return false
try: try:
var account = self.getAccountByAddress(address) var account = self.getAccountByAddress(address)
if account.isNil:
error "on account for given address", procName="updateWalletAccount"
return false
let response = status_go_accounts.updateAccount(account.name, account.address, account.path, account.publicKey, let response = status_go_accounts.updateAccount(account.name, account.address, account.path, account.publicKey,
account.keyUid, account.walletType, account.colorId, account.emoji, account.isWallet, account.isChat, account.prodPreferredChainIds, preferredChainIds) account.keyUid, account.walletType, account.colorId, account.emoji, account.isWallet, account.isChat, account.prodPreferredChainIds, preferredChainIds)
if not response.error.isNil: if not response.error.isNil:
@ -822,41 +840,45 @@ QtObject:
) )
self.threadpool.start(arg) self.threadpool.start(arg)
proc getCurrency*(self: Service): string =
return self.settingsService.getCurrency()
proc getCurrentCurrencyIfEmpty(self: Service, currency = ""): string = proc getCurrentCurrencyIfEmpty(self: Service, currency = ""): string =
if currency != "": if currency != "":
return currency return currency
else: else:
return self.getCurrency() return self.getCurrency()
proc getNetworkCurrencyBalance*(self: Service, network: NetworkDto, currency: string = ""): float64 = proc getCurrencyBalance*(self: Service, address: string, chainIds: seq[int], currency: string): float64 =
let accounts = self.getWalletAccounts() if not self.accountsTokens.hasKey(address):
for walletAccount in accounts: return
result += walletAccount.getCurrencyBalance(@[network.chainId], self.getCurrentCurrencyIfEmpty(currency)) return self.accountsTokens[address].map(t => t.getCurrencyBalance(chainIds, currency)).foldl(a + b, 0.0)
proc getTotalCurrencyBalance*(self: Service, addresses: seq[string], currency: string = ""): float64 =
let chainIds = self.networkService.getNetworks().filter(a => a.enabled).map(a => a.chainId)
let accounts = self.getWalletAccounts().filter(w => addresses.contains(w.address))
return accounts.map(a => self.getCurrencyBalance(a.address, chainIds, self.getCurrentCurrencyIfEmpty(currency))).foldl(a + b, 0.0)
proc findTokenSymbolByAddress*(self: Service, address: string): string = proc findTokenSymbolByAddress*(self: Service, address: string): string =
return self.tokenService.findTokenSymbolByAddress(address) return self.tokenService.findTokenSymbolByAddress(address)
proc getOrFetchBalanceForAddressInPreferredCurrency*(self: Service, address: string): tuple[balance: float64, fetched: bool] = proc getOrFetchBalanceForAddressInPreferredCurrency*(self: Service, address: string): tuple[balance: float64, fetched: bool] =
if self.walletAccountsContainsAddress(address): let acc = self.getAccountByAddress(address)
let chainIds = self.networkService.getNetworks().map(n => n.chainId) if acc.isNil:
result.balance = self.getAccountByAddress(address).getCurrencyBalance(chainIds, self.getCurrentCurrencyIfEmpty())
result.fetched = true
else:
self.buildAllTokens(@[address], store = false) self.buildAllTokens(@[address], store = false)
result.balance = 0.0 result.balance = 0.0
result.fetched = false result.fetched = false
return
proc getTotalCurrencyBalance*(self: Service, addresses: seq[string], currency: string = ""): float64 = let chainIds = self.networkService.getNetworks().map(n => n.chainId)
let chainIds = self.networkService.getNetworks().filter(a => a.enabled).map(a => a.chainId) result.balance = self.getCurrencyBalance(acc.address, chainIds, self.getCurrentCurrencyIfEmpty())
let accounts = self.getWalletAccounts().filter(w => addresses.contains(w.address)) result.fetched = true
return accounts.map(a => a.getCurrencyBalance(chainIds, self.getCurrentCurrencyIfEmpty(currency))).foldl(a + b, 0.0)
proc getTokenBalanceOnChain*(self: Service, address: string, chainId: int, symbol: string): float64 = proc getTokenBalanceOnChain*(self: Service, address: string, chainId: int, symbol: string): float64 =
let account = self.getAccountByAddress(address) if not self.accountsTokens.hasKey(address):
for token in account.tokens: return 0.0
for token in self.accountsTokens[address]:
if token.symbol == symbol and token.balancesPerChain.hasKey(chainId): if token.symbol == symbol and token.balancesPerChain.hasKey(chainId):
return token.balancesPerChain[chainId].balance return token.balancesPerChain[chainId].balance
return 0.0 return 0.0
proc addKeycardOrAccountsAsync*(self: Service, keycard: KeycardDto, accountsComingFromKeycard: bool = false) = proc addKeycardOrAccountsAsync*(self: Service, keycard: KeycardDto, accountsComingFromKeycard: bool = false) =
@ -1076,12 +1098,13 @@ QtObject:
proc allAccountsTokenBalance*(self: Service, symbol: string): float64 = proc allAccountsTokenBalance*(self: Service, symbol: string): float64 =
var totalTokenBalance = 0.0 var totalTokenBalance = 0.0
for walletAccount in self.getWalletAccounts: for walletAccount in self.getWalletAccounts():
if walletAccount.walletType != WalletTypeWatch: if walletAccount.walletType == WalletTypeWatch or
for token in walletAccount.tokens: not self.accountsTokens.hasKey(walletAccount.address):
if token.symbol == symbol: continue
totalTokenBalance += token.getTotalBalanceOfSupportedChains() for token in self.accountsTokens[walletAccount.address]:
if token.symbol == symbol:
totalTokenBalance += token.getTotalBalanceOfSupportedChains()
return totalTokenBalance return totalTokenBalance
proc isIncludeWatchOnlyAccount*(self: Service): bool = proc isIncludeWatchOnlyAccount*(self: Service): bool =