fix total account balance

fix total account balance

cleanup
This commit is contained in:
Iuri Matias 2020-06-09 19:09:02 -04:00
parent 826c0ed46d
commit 19eb7eb520
3 changed files with 26 additions and 14 deletions

View File

@ -15,8 +15,10 @@ type WalletModel* = ref object
accounts*: seq[Account]
defaultCurrency*: string
tokens*: JsonNode
totalBalance*: float
proc getDefaultCurrency*(self: WalletModel): string
proc calculateTotalFiatBalance*(self: WalletModel)
proc newWalletModel*(events: EventEmitter): WalletModel =
result = WalletModel()
@ -24,14 +26,19 @@ proc newWalletModel*(events: EventEmitter): WalletModel =
result.tokens = %* []
result.events = events
result.defaultCurrency = ""
result.totalBalance = 0.0
proc initEvents*(self: WalletModel) =
self.events.on("currencyChanged") do(e: Args):
self.defaultCurrency = self.getDefaultCurrency()
for account in self.accounts:
updateBalance(account, self.getDefaultCurrency())
self.calculateTotalFiatBalance()
self.events.emit("accountsUpdated", Args())
self.events.on("newAccountAdded") do(e: Args):
self.calculateTotalFiatBalance()
proc delete*(self: WalletModel) =
discard
@ -45,18 +52,18 @@ proc setDefaultCurrency*(self: WalletModel, currency: string) =
discard status_settings.saveSettings("currency", currency)
self.events.emit("currencyChanged", CurrencyArgs(currency: currency))
proc generateAccountConfiguredAssets*(self: WalletModel): seq[Asset] =
proc generateAccountConfiguredAssets*(self: WalletModel, accountAddress: string): seq[Asset] =
var assets: seq[Asset] = @[]
var asset = Asset(name:"Ethereum", symbol: "ETH", value: "0.0", fiatValue: "0.0", image: fmt"../../img/token-icons/eth.svg", hasIcon: true)
var asset = Asset(name:"Ethereum", symbol: "ETH", value: "0.0", fiatValue: "0.0", image: fmt"../../img/token-icons/eth.svg", hasIcon: true, accountAddress: accountAddress)
assets.add(asset)
for token in self.tokens:
var symbol = token["symbol"].getStr
var existingToken = Asset(name: token["name"].getStr, symbol: symbol, value: fmt"0.0", fiatValue: "$0.0", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg", hasIcon: true)
var existingToken = Asset(name: token["name"].getStr, symbol: symbol, value: fmt"0.0", fiatValue: "$0.0", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg", hasIcon: true, accountAddress: accountAddress)
assets.add(existingToken)
assets
proc newAccount*(self: WalletModel, name: string, address: string, iconColor: string, balance: string): Account =
var assets: seq[Asset] = self.generateAccountConfiguredAssets()
var assets: seq[Asset] = self.generateAccountConfiguredAssets(address)
var account = Account(name: name, address: address, iconColor: iconColor, balance: fmt"{balance} {self.defaultCurrency}", assetList: assets, realFiatBalance: 0.0)
updateBalance(account, self.getDefaultCurrency())
account
@ -67,10 +74,16 @@ proc initAccounts*(self: WalletModel) =
for account in accounts:
var account = self.newAccount(account.name, account.address, account.color, "")
self.accounts.add(account)
self.calculateTotalFiatBalance
proc getTotalFiatBalance*(self: WalletModel): string =
var newBalance = 0.0
fmt"{newBalance:.2f} {self.defaultCurrency}"
fmt"{self.totalBalance:.2f} {self.defaultCurrency}"
proc calculateTotalFiatBalance*(self: WalletModel) =
self.totalBalance = 0.0
for account in self.accounts:
self.totalBalance += account.realFiatBalance
proc addNewGeneratedAccount(self: WalletModel, generatedAccount: GeneratedAccount, password: string, accountName: string, color: string, accountType: string, isADerivedAccount = true) =
generatedAccount.name = accountName
@ -103,6 +116,6 @@ proc hasAsset*(self: WalletModel, account: string, symbol: string): bool =
proc toggleAsset*(self: WalletModel, symbol: string, enable: bool, address: string, name: string, decimals: int, color: string) =
self.tokens = addOrRemoveToken(enable, address, name, symbol, decimals, color)
for account in self.accounts:
account.assetList = self.generateAccountConfiguredAssets()
account.assetList = self.generateAccountConfiguredAssets(account.address)
updateBalance(account, self.getDefaultCurrency())
self.events.emit("assetChanged", Args())

View File

@ -4,7 +4,7 @@ type CurrencyArgs* = ref object of Args
currency*: string
type Asset* = ref object
name*, symbol*, value*, fiatValue*, image*: string
name*, symbol*, value*, fiatValue*, image*, accountAddress*: string
hasIcon*: bool
type Account* = ref object

View File

@ -20,14 +20,12 @@ proc getPrice(crypto: string, fiat: string): string =
if balanceManager.pricePairs.hasKey(fiat):
return balanceManager.pricePairs[fiat]
var url: string = fmt"https://min-api.cryptocompare.com/data/price?fsym={crypto}&tsyms={fiat}"
echo url
let client = newHttpClient()
client.headers = newHttpHeaders({ "Content-Type": "application/json" })
let response = client.request(url)
echo $response.body
result = $parseJson(response.body)[fiat.toUpper]
balanceManager.pricePairs[fiat] = result
# balanceManager.pricePairs[fiat] = result
except Exception as e:
echo "error getting price"
echo e.msg
@ -35,7 +33,7 @@ proc getPrice(crypto: string, fiat: string): string =
proc getEthBalance(address: string): string =
var balance = status_wallet.getBalance(address)
result = status_wallet.hex2Eth(balance)
balanceManager.tokenBalances["ETH"] = result
# balanceManager.tokenBalances["ETH"] = result
proc getBalance*(symbol: string, accountAddress: string): string =
if balanceManager.tokenBalances.hasKey(symbol):
@ -45,7 +43,7 @@ proc getBalance*(symbol: string, accountAddress: string): string =
return getEthBalance(accountAddress)
var token: AssetConfig = getTokenConfig(symbol)
result = $status_tokens.getTokenBalance(token.address, accountAddress)
balanceManager.tokenBalances[symbol] = result
# balanceManager.tokenBalances[symbol] = result
proc getFiatValue*(crypto_balance: string, crypto_symbol: string, fiat_symbol: string): float =
if crypto_balance == "0.0": return 0.0
@ -53,15 +51,16 @@ proc getFiatValue*(crypto_balance: string, crypto_symbol: string, fiat_symbol: s
parseFloat(crypto_balance) * parseFloat(fiat_crypto_price)
proc updateBalance*(asset: Asset, currency: string) =
var token_balance = getBalance(asset.symbol, "0xf977814e90da44bfa03b6295a0616a897441acec")
var token_balance = getBalance(asset.symbol, asset.accountAddress)
let fiat_balance = getFiatValue(token_balance, asset.symbol, currency)
asset.value = token_balance
asset.fiatValue = fmt"{fiat_Balance:.2f} {currency}"
asset.fiatValue = fmt"{fiat_balance:.2f} {currency}"
proc updateBalance*(account: Account, currency: string) =
let eth_balance = getBalance("ETH", account.address)
let usd_balance = getFiatValue(eth_balance, "ETH", currency)
var totalAccountBalance = usd_balance
account.realFiatBalance = totalAccountBalance
account.balance = fmt"{totalAccountBalance:.2f} {currency}"
for asset in account.assetList:
updateBalance(asset, currency)