feat: Add network balance

This commit is contained in:
Anthony Laibe 2022-07-01 10:30:09 +02:00 committed by Anthony Laibe
parent c7d2157d20
commit d4d9797eec
6 changed files with 52 additions and 21 deletions

View File

@ -34,6 +34,9 @@ proc init*(self: Controller) =
self.events.on(SIGNAL_WALLET_ACCOUNT_NETWORK_ENABLED_UPDATED) do(e: Args):
self.delegate.refreshNetworks()
self.events.on(SIGNAL_WALLET_ACCOUNT_TOKENS_REBUILT) do(e:Args):
self.delegate.refreshNetworks()
proc getNetworks*(self: Controller): seq[NetworkDto] =
return self.networkService.getNetworks()
@ -45,3 +48,6 @@ proc areTestNetworksEnabled*(self: Controller): bool =
proc toggleTestNetworksEnabled*(self: Controller) =
self.walletAccountService.toggleTestNetworksEnabled()
proc getNetworkCurrencyBalance*(self: Controller, network: NetworkDto): float64 =
return self.walletAccountService.getNetworkCurrencyBalance(network)

View File

@ -15,6 +15,7 @@ type
iconUrl: string
chainColor: string
shortName: string
balance: float64
proc initItem*(
chainId: int,
@ -30,6 +31,7 @@ proc initItem*(
iconUrl: string,
chainColor: string,
shortName: string,
balance: float64,
): Item =
result.chainId = chainId
result.nativeCurrencyDecimals = nativeCurrencyDecimals
@ -44,6 +46,7 @@ proc initItem*(
result.iconUrl = iconUrl
result.chainColor = chainColor
result.shortName = shortName
result.balance = balance
proc `$`*(self: Item): string =
result = fmt"""NetworkItem(
@ -60,6 +63,7 @@ proc `$`*(self: Item): string =
iconUrl:{self.iconUrl},
shortName: {self.shortName},
chainColor: {self.chainColor},
balance: {self.balance},
]"""
proc getChainId*(self: Item): int =
@ -100,3 +104,6 @@ proc getShortName*(self: Item): string =
proc getChainColor*(self: Item): string =
return self.chainColor
proc getBalance*(self: Item): float64 =
return self.balance

View File

@ -17,6 +17,7 @@ type
IconUrl
ChainColor
ShortName
Balance
QtObject:
type
@ -65,6 +66,7 @@ QtObject:
ModelRole.IconUrl.int:"iconUrl",
ModelRole.ShortName.int: "shortName",
ModelRole.ChainColor.int: "chainColor",
ModelRole.Balance.int: "balance",
}.toTable
method data(self: Model, index: QModelIndex, role: int): QVariant =
@ -104,6 +106,8 @@ QtObject:
result = newQVariant(item.getShortName())
of ModelRole.ChainColor:
result = newQVariant(item.getChainColor())
of ModelRole.Balance:
result = newQVariant(item.getBalance())
proc setItems*(self: Model, items: seq[Item]) =
self.beginResetModel()

View File

@ -1,4 +1,4 @@
import NimQml
import Tables, NimQml
import ../io_interface as delegate_interface
import io_interface, view, controller
import ../../../global/global_singleton
@ -39,7 +39,10 @@ method delete*(self: Module) =
self.controller.delete
method refreshNetworks*(self: Module) =
let networks = self.controller.getNetworks()
let networks = newTable[NetworkDto, float64]()
for network in self.controller.getNetworks():
networks[network] = self.controller.getNetworkCurrencyBalance(network)
self.view.load(networks)
method load*(self: Module) =

View File

@ -1,4 +1,4 @@
import NimQml, sequtils, sugar
import Tables, NimQml, sequtils, sugar
import ../../../../app_service/service/network/dto
import ./io_interface
@ -68,8 +68,10 @@ QtObject:
read = getEnabled
notify = enabledChanged
proc load*(self: View, networks: seq[NetworkDto]) =
let items = networks.map(n => initItem(
proc load*(self: View, networks: TableRef[NetworkDto, float64]) =
var items: seq[Item] = @[]
for n, balance in networks.pairs:
items.add(initItem(
n.chainId,
n.nativeCurrencyDecimals,
n.layer,
@ -83,7 +85,9 @@ QtObject:
n.iconUrl,
n.chainColor,
n.shortName,
balance,
))
self.layer1.setItems(items.filter(i => i.getLayer() == 1))
self.layer2.setItems(items.filter(i => i.getLayer() == 2))
self.enabled.setItems(items.filter(i => i.getIsEnabled()))

View File

@ -421,3 +421,10 @@ QtObject:
proc onIsWalletEnabledChanged*(self: Service) {.slot.} =
self.buildAllTokens()
proc getNetworkCurrencyBalance*(self: Service, network: NetworkDto): float64 =
for walletAccount in toSeq(self.walletAccounts.values):
for token in walletAccount.tokens:
if token.balancesPerChain.hasKey(network.chainId):
let balance = token.balancesPerChain[network.chainId]
result += balance.currencyBalance