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.events.on(SIGNAL_WALLET_ACCOUNT_NETWORK_ENABLED_UPDATED) do(e: Args):
self.delegate.refreshNetworks() self.delegate.refreshNetworks()
self.events.on(SIGNAL_WALLET_ACCOUNT_TOKENS_REBUILT) do(e:Args):
self.delegate.refreshNetworks()
proc getNetworks*(self: Controller): seq[NetworkDto] = proc getNetworks*(self: Controller): seq[NetworkDto] =
return self.networkService.getNetworks() return self.networkService.getNetworks()
@ -44,4 +47,7 @@ proc areTestNetworksEnabled*(self: Controller): bool =
return self.settingsService.areTestNetworksEnabled() return self.settingsService.areTestNetworksEnabled()
proc toggleTestNetworksEnabled*(self: Controller) = proc toggleTestNetworksEnabled*(self: Controller) =
self.walletAccountService.toggleTestNetworksEnabled() self.walletAccountService.toggleTestNetworksEnabled()
proc getNetworkCurrencyBalance*(self: Controller, network: NetworkDto): float64 =
return self.walletAccountService.getNetworkCurrencyBalance(network)

View File

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

View File

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

View File

@ -1,4 +1,4 @@
import NimQml import Tables, NimQml
import ../io_interface as delegate_interface import ../io_interface as delegate_interface
import io_interface, view, controller import io_interface, view, controller
import ../../../global/global_singleton import ../../../global/global_singleton
@ -39,7 +39,10 @@ method delete*(self: Module) =
self.controller.delete self.controller.delete
method refreshNetworks*(self: Module) = 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) self.view.load(networks)
method load*(self: Module) = 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 ../../../../app_service/service/network/dto
import ./io_interface import ./io_interface
@ -68,22 +68,26 @@ QtObject:
read = getEnabled read = getEnabled
notify = enabledChanged notify = enabledChanged
proc load*(self: View, networks: seq[NetworkDto]) = proc load*(self: View, networks: TableRef[NetworkDto, float64]) =
let items = networks.map(n => initItem( var items: seq[Item] = @[]
n.chainId, for n, balance in networks.pairs:
n.nativeCurrencyDecimals, items.add(initItem(
n.layer, n.chainId,
n.chainName, n.nativeCurrencyDecimals,
n.rpcURL, n.layer,
n.blockExplorerURL, n.chainName,
n.nativeCurrencyName, n.rpcURL,
n.nativeCurrencySymbol, n.blockExplorerURL,
n.isTest, n.nativeCurrencyName,
n.enabled, n.nativeCurrencySymbol,
n.iconUrl, n.isTest,
n.chainColor, n.enabled,
n.shortName, n.iconUrl,
)) n.chainColor,
n.shortName,
balance,
))
self.layer1.setItems(items.filter(i => i.getLayer() == 1)) self.layer1.setItems(items.filter(i => i.getLayer() == 1))
self.layer2.setItems(items.filter(i => i.getLayer() == 2)) self.layer2.setItems(items.filter(i => i.getLayer() == 2))
self.enabled.setItems(items.filter(i => i.getIsEnabled())) self.enabled.setItems(items.filter(i => i.getIsEnabled()))

View File

@ -420,4 +420,11 @@ QtObject:
self.threadpool.start(arg) self.threadpool.start(arg)
proc onIsWalletEnabledChanged*(self: Service) {.slot.} = proc onIsWalletEnabledChanged*(self: Service) {.slot.} =
self.buildAllTokens() 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