feat(@wallet): aggregate token balance
This commit is contained in:
parent
c9910ee38b
commit
21fedcbb2b
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
))
|
||||
)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 78cba969cc90f463184509c6bde9bcb44d7bb3f9
|
||||
Subproject commit 97a99d625425b4e705116ba75c9091ebfd118485
|
Loading…
Reference in New Issue