diff --git a/src/app/wallet/core.nim b/src/app/wallet/core.nim index a1673da745..7846cc5df1 100644 --- a/src/app/wallet/core.nim +++ b/src/app/wallet/core.nim @@ -30,14 +30,14 @@ proc delete*(self: WalletController) = delete self.variant proc init*(self: WalletController) = - self.status.events.on("currencyChanged") do(e: Args): - echo "currency changed" - self.status.wallet.initAccounts() var accounts = self.status.wallet.accounts for account in accounts: self.view.addAccountToList(account) + self.status.events.on("accountsUpdated") do(e: Args): + self.view.currentAccountChanged() + method onSignal(self: WalletController, data: Signal) = debug "New signal received" discard diff --git a/src/status/status.nim b/src/status/status.nim index 3b78436b50..8172a2c210 100644 --- a/src/status/status.nim +++ b/src/status/status.nim @@ -24,6 +24,7 @@ proc newStatusInstance*(): Status = result.chat = chat.newChatModel(result.events) result.accounts = accounts.newAccountModel() result.wallet = wallet.newWalletModel(result.events) + result.wallet.initEvents() result.node = node.newNodeModel() result.mailservers = mailservers.newMailserverModel(result.events) diff --git a/src/status/wallet.nim b/src/status/wallet.nim index 2829a4e471..ceb695d407 100644 --- a/src/status/wallet.nim +++ b/src/status/wallet.nim @@ -15,23 +15,31 @@ type Account* = ref object name*, address*, iconColor*, balance*: string realFiatBalance*: float assetList*: seq[Asset] - # assetList*: AssetList type WalletModel* = ref object events*: EventEmitter accounts*: seq[Account] +proc updateBalance*(self: Account) + proc newWalletModel*(events: EventEmitter): WalletModel = result = WalletModel() + result.accounts = @[] result.events = events +proc initEvents*(self: WalletModel) = + self.events.on("currencyChanged") do(e: Args): + for account in self.accounts: + account.updateBalance() + self.events.emit("accountsUpdated", Args()) + proc delete*(self: WalletModel) = discard proc sendTransaction*(self: WalletModel, from_value: string, to: string, value: string, password: string): string = status_wallet.sendTransaction(from_value, to, value, password) -proc getEthBalance*(self: WalletModel, address: string): string = +proc getEthBalance*(address: string): string = var balance = status_wallet.getBalance(address) echo(fmt"balance in hex: {balance}") @@ -40,14 +48,18 @@ proc getEthBalance*(self: WalletModel, address: string): string = echo(fmt"balance in eth: {eth_value}") eth_value -proc getDefaultCurrency*(self: WalletModel): string = +proc getDefaultCurrency*(): string = status_settings.getSettings().parseJSON()["result"]["currency"].getStr +proc getDefaultCurrency*(self: WalletModel): string = + getDefaultCurrency() + proc setDefaultCurrency*(self: WalletModel, currency: string) = discard status_settings.saveSettings("currency", currency) self.events.emit("currencyChanged", CurrencyArgs(currency: currency)) -proc getFiatValue*(self: WalletModel, eth_balance: string, symbol: string, fiat_symbol: string): float = +proc getFiatValue*(eth_balance: string, symbol: string, fiat_symbol: string): float = + if eth_balance == "0.0": return 0.0 # 3. get usd price of 1 eth var fiat_eth_price = status_wallet.getPrice("ETH", fiat_symbol) echo(fmt"fiat_price: {fiat_eth_price}") @@ -60,16 +72,24 @@ proc getFiatValue*(self: WalletModel, eth_balance: string, symbol: string, fiat_ proc hasAsset*(self: WalletModel, account: string, symbol: string): bool = (symbol == "DAI") or (symbol == "OMG") +proc updateBalance*(self: Account) = + let defaultCurrency = getDefaultCurrency() + const symbol = "ETH" + let eth_balance = getEthBalance(self.address) + let usd_balance = getFiatValue(eth_balance, symbol, defaultCurrency) + var totalAccountBalance = usd_balance + self.balance = fmt"{totalAccountBalance:.2f} {defaultCurrency}" + proc initAccounts*(self: WalletModel) = let accounts = status_wallet.getAccounts() var totalAccountBalance: float = 0 const symbol = "ETH" - let defaultCurrency = self.getDefaultCurrency() + let defaultCurrency = getDefaultCurrency() for address in accounts: - let eth_balance = self.getEthBalance(address) - let usd_balance = self.getFiatValue(eth_balance, symbol, defaultCurrency) + let eth_balance = getEthBalance(address) + let usd_balance = getFiatValue(eth_balance, symbol, defaultCurrency) totalAccountBalance = totalAccountBalance + usd_balance @@ -77,5 +97,6 @@ proc initAccounts*(self: WalletModel) = var assets: seq[Asset] = @[] assets.add(asset) - var account = Account(name: "Status Account", address: address, iconColor: "", balance: fmt"{totalAccountBalance:.2f} {defaultCurrency}", assetList: assets, realFiatBalance: totalAccountBalance) + var account = Account(name: "Status Account", address: address, iconColor: "", balance: "", assetList: assets, realFiatBalance: totalAccountBalance) + account.updateBalance() self.accounts.add(account)