trigger new balance when currency change happens

trigger new balance when currency change happens

update current account balance on currency change
This commit is contained in:
Iuri Matias 2020-06-04 15:40:32 -04:00
parent b4aae8fa3b
commit 0dcf257f0c
3 changed files with 33 additions and 11 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)