diff --git a/src/app/wallet/core.nim b/src/app/wallet/core.nim index fc6c6d86fa..a1673da745 100644 --- a/src/app/wallet/core.nim +++ b/src/app/wallet/core.nim @@ -1,5 +1,5 @@ import NimQml -# import eventemitter +import eventemitter import strformat import strutils import chronicles @@ -14,7 +14,6 @@ import ../../signals/types import ../../status/wallet import ../../status/status - type WalletController* = ref object of SignalSubscriber status: Status view*: WalletView @@ -31,28 +30,14 @@ proc delete*(self: WalletController) = delete self.variant proc init*(self: WalletController) = - let accounts = status_wallet.getAccounts() + self.status.events.on("currencyChanged") do(e: Args): + echo "currency changed" - var totalAccountBalance: float = 0 - - const symbol = "ETH" - let defaultCurrency = self.status.wallet.getDefaultCurrency() - for address in accounts: - let eth_balance = self.status.wallet.getEthBalance(address) - # TODO get all user assets and add them to balance - let usd_balance = self.status.wallet.getFiatValue(eth_balance, symbol, defaultCurrency) - - totalAccountBalance = totalAccountBalance + usd_balance - - let assetList = newAssetList() - let asset = Asset(name:"Ethereum", symbol: symbol, value: fmt"{eth_balance:.6}", fiatValue: "$" & fmt"{usd_balance:.2f}", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg") - assetList.addAssetToList(asset) - - let account = Account(name: "Status Account", address: address, iconColor: "", balance: fmt"{totalAccountBalance:.2f} {defaultCurrency}", assetList: assetList, realFiatBalance: totalAccountBalance) + self.status.wallet.initAccounts() + var accounts = self.status.wallet.accounts + for account in accounts: self.view.addAccountToList(account) - self.view.setDefaultAccount(accounts[0]) - method onSignal(self: WalletController, data: Signal) = debug "New signal received" discard diff --git a/src/app/wallet/view.nim b/src/app/wallet/view.nim index e7d6b145db..73b48fea28 100644 --- a/src/app/wallet/view.nim +++ b/src/app/wallet/view.nim @@ -15,7 +15,6 @@ QtObject: accounts*: AccountList currentAssetList*: AssetList currentAccount: AccountItemView - defaultAccount: string status: Status totalFiatBalance: float @@ -30,7 +29,7 @@ QtObject: result.status = status result.accounts = newAccountList() result.currentAccount = newAccountItemView() - result.currentAssetList = newAssetList() # Temporarily set to an empty list + result.currentAssetList = newAssetList() result.setup proc currentAccountChanged*(self: WalletView) {.signal.} @@ -56,8 +55,8 @@ QtObject: proc getCurrentAssetList(self: WalletView): QVariant {.slot.} = return newQVariant(self.currentAssetList) - proc setCurrentAssetList*(self: WalletView, assetList: AssetList) = - self.currentAssetList = assetList + proc setCurrentAssetList*(self: WalletView, assetList: seq[Asset]) = + self.currentAssetList.setNewData(assetList) self.currentAssetListChanged() QtProperty[QVariant] assets: @@ -111,11 +110,8 @@ QtObject: proc onSendTransaction*(self: WalletView, from_value: string, to: string, value: string, password: string): string {.slot.} = result = self.status.wallet.sendTransaction(from_value, to, value, password) - proc setDefaultAccount*(self: WalletView, account: string) = - self.defaultAccount = account - proc getDefaultAccount*(self: WalletView): string {.slot.} = - return self.defaultAccount + self.currentAccount.address proc defaultCurrency*(self: WalletView): string {.slot.} = self.status.wallet.getDefaultCurrency() diff --git a/src/app/wallet/views/account_item.nim b/src/app/wallet/views/account_item.nim index 670d6b18d2..265c5e38c8 100644 --- a/src/app/wallet/views/account_item.nim +++ b/src/app/wallet/views/account_item.nim @@ -1,11 +1,7 @@ import NimQml import std/wrapnils import ./asset_list - -type Account* = ref object - name*, address*, iconColor*, balance*: string - realFiatBalance*: float - assetList*: AssetList +import ../../../status/wallet QtObject: type AccountItemView* = ref object of QObject diff --git a/src/app/wallet/views/asset_list.nim b/src/app/wallet/views/asset_list.nim index d1e15b3f12..508de4c4cc 100644 --- a/src/app/wallet/views/asset_list.nim +++ b/src/app/wallet/views/asset_list.nim @@ -53,3 +53,8 @@ QtObject: self.beginInsertRows(newQModelIndex(), self.assets.len, self.assets.len) self.assets.add(asset) self.endInsertRows() + + proc setNewData*(self: AssetList, assetList: seq[Asset]) = + self.beginResetModel() + self.assets = assetList + self.endResetModel() diff --git a/src/status/status.nim b/src/status/status.nim index a7212d97ab..3b78436b50 100644 --- a/src/status/status.nim +++ b/src/status/status.nim @@ -23,7 +23,7 @@ proc newStatusInstance*(): Status = result.events = createEventEmitter() result.chat = chat.newChatModel(result.events) result.accounts = accounts.newAccountModel() - result.wallet = wallet.newWalletModel() + result.wallet = wallet.newWalletModel(result.events) result.node = node.newNodeModel() result.mailservers = mailservers.newMailserverModel(result.events) diff --git a/src/status/wallet.nim b/src/status/wallet.nim index 7ca33ca730..2829a4e471 100644 --- a/src/status/wallet.nim +++ b/src/status/wallet.nim @@ -11,12 +11,19 @@ type CurrencyArgs* = ref object of Args type Asset* = ref object name*, symbol*, value*, fiatValue*, image*: string +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 newWalletModel*(): WalletModel = +proc newWalletModel*(events: EventEmitter): WalletModel = result = WalletModel() - result.events = createEventEmitter() + result.events = events proc delete*(self: WalletModel) = discard @@ -52,3 +59,23 @@ 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 initAccounts*(self: WalletModel) = + let accounts = status_wallet.getAccounts() + + var totalAccountBalance: float = 0 + const symbol = "ETH" + let defaultCurrency = self.getDefaultCurrency() + + for address in accounts: + let eth_balance = self.getEthBalance(address) + let usd_balance = self.getFiatValue(eth_balance, symbol, defaultCurrency) + + totalAccountBalance = totalAccountBalance + usd_balance + + var asset = Asset(name:"Ethereum", symbol: symbol, value: fmt"{eth_balance:.6}", fiatValue: "$" & fmt"{usd_balance:.2f}", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg") + 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) + self.accounts.add(account)