update balances & currency across walet; move account generation

update total fiat balance so it can auto update

update account list when needed

force update of asset list

add account in the model

add 0x0 address instead of invalid one

ensure asset list is updated
This commit is contained in:
Iuri Matias 2020-06-05 07:01:43 -04:00
parent 1b086b006d
commit 1fb9be3d7d
5 changed files with 54 additions and 27 deletions

View File

@ -34,9 +34,18 @@ proc init*(self: WalletController) =
var accounts = self.status.wallet.accounts var accounts = self.status.wallet.accounts
for account in accounts: for account in accounts:
self.view.addAccountToList(account) self.view.addAccountToList(account)
self.view.setTotalFiatBalance(self.status.wallet.getTotalFiatBalance())
self.status.events.on("accountsUpdated") do(e: Args): self.status.events.on("accountsUpdated") do(e: Args):
self.view.totalFiatBalanceChanged()
self.view.currentAccountChanged() self.view.currentAccountChanged()
self.view.accountListChanged()
self.view.accounts.forceUpdate()
self.view.currentAssetList.forceUpdate()
self.status.events.on("newAccountAdded") do(e: Args):
var account = AccountArgs(e)
self.view.accounts.addAccountToList(account.account)
method onSignal(self: WalletController, data: Signal) = method onSignal(self: WalletController, data: Signal) =
debug "New signal received" debug "New signal received"

View File

@ -16,7 +16,7 @@ QtObject:
currentAssetList*: AssetList currentAssetList*: AssetList
currentAccount: AccountItemView currentAccount: AccountItemView
status: Status status: Status
totalFiatBalance: float totalFiatBalance: string
proc delete(self: WalletView) = proc delete(self: WalletView) =
self.QAbstractListModel.delete self.QAbstractListModel.delete
@ -30,10 +30,13 @@ QtObject:
result.accounts = newAccountList() result.accounts = newAccountList()
result.currentAccount = newAccountItemView() result.currentAccount = newAccountItemView()
result.currentAssetList = newAssetList() result.currentAssetList = newAssetList()
result.totalFiatBalance = ""
result.setup result.setup
proc currentAccountChanged*(self: WalletView) {.signal.} proc currentAccountChanged*(self: WalletView) {.signal.}
proc setCurrentAssetList*(self: WalletView, assetList: seq[Asset])
proc setCurrentAccountByIndex*(self: WalletView, index: int) {.slot.} = proc setCurrentAccountByIndex*(self: WalletView, index: int) {.slot.} =
if(self.accounts.rowCount() == 0): return if(self.accounts.rowCount() == 0): return
@ -41,6 +44,7 @@ QtObject:
if self.currentAccount.address == selectedAccount.address: return if self.currentAccount.address == selectedAccount.address: return
self.currentAccount.setAccountItem(selectedAccount) self.currentAccount.setAccountItem(selectedAccount)
self.currentAccountChanged() self.currentAccountChanged()
self.setCurrentAssetList(selectedAccount.assetList)
proc getCurrentAccount*(self: WalletView): QVariant {.slot.} = proc getCurrentAccount*(self: WalletView): QVariant {.slot.} =
result = newQVariant(self.currentAccount) result = newQVariant(self.currentAccount)
@ -66,17 +70,17 @@ QtObject:
proc totalFiatBalanceChanged*(self: WalletView) {.signal.} proc totalFiatBalanceChanged*(self: WalletView) {.signal.}
proc getTotalFiatBalance(self: WalletView): QVariant {.slot.} = proc getTotalFiatBalance(self: WalletView): string {.slot.} =
return newQVariant(fmt"{self.totalFiatBalance:.2f} USD") # TODO use user's currency self.status.wallet.getTotalFiatBalance()
proc setTotalFiatBalance*(self: WalletView, newBalance: float) = proc setTotalFiatBalance*(self: WalletView, newBalance: string) =
self.totalFiatBalance = newBalance self.totalFiatBalance = newBalance
self.totalFiatBalanceChanged() self.totalFiatBalanceChanged()
QtProperty[QVariant] totalFiatBalance: QtProperty[string] totalFiatBalance:
read = getTotalFiatBalance read = getTotalFiatBalance
write = setTotalFiatBalance write = setTotalFiatBalance
notify = currentAssetListChanged notify = totalFiatBalanceChanged
proc accountListChanged*(self: WalletView) {.signal.} proc accountListChanged*(self: WalletView) {.signal.}
@ -86,20 +90,10 @@ QtObject:
if (self.accounts.rowCount == 1): if (self.accounts.rowCount == 1):
self.setCurrentAssetList(account.assetList) self.setCurrentAssetList(account.assetList)
self.setCurrentAccountByIndex(0) self.setCurrentAccountByIndex(0)
self.setTotalFiatBalance(account.realFiatBalance + self.totalFiatBalance)
self.accountListChanged() self.accountListChanged()
proc generateNewAccount*(self: WalletView, password: string, accountName: string, color: string) {.slot.} = proc generateNewAccount*(self: WalletView, password: string, accountName: string, color: string) {.slot.} =
# TODO move all this to the model to add a real account self.status.wallet.generateNewAccount(password, accountName, color)
# let assetList = newAssetList()
var assetList: seq[Asset] = @[]
let symbol = "ETH"
let asset = Asset(name:"Ethereum", symbol: symbol, value: fmt"0", fiatValue: "$0.00", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg")
# assetList.addAssetToList(asset)
let defaultCurrency = "USD" # TODO get real default
# TODO get a real address that we unlock with the password
let account = Account(name: accountName, address: "0x0r329ru298u392r", iconColor: color, balance: fmt"0.00 {defaultCurrency}", assetList: assetList, realFiatBalance: 0.0)
self.addAccountToList(account)
proc getAccountList(self: WalletView): QVariant {.slot.} = proc getAccountList(self: WalletView): QVariant {.slot.} =
return newQVariant(self.accounts) return newQVariant(self.accounts)

View File

@ -5,15 +5,7 @@ import ./asset_list
import ./account_item import ./account_item
import ../../../status/wallet import ../../../status/wallet
const accountColors* = [ const accountColors* = ["#9B832F", "#D37EF4", "#1D806F", "#FA6565", "#7CDA00", "#887af9", "#8B3131"]
"#9B832F",
"#D37EF4",
"#1D806F",
"#FA6565",
"#7CDA00",
"#887af9",
"#8B3131"
]
type type
AccountRoles {.pure.} = enum AccountRoles {.pure.} = enum
@ -68,3 +60,7 @@ QtObject:
self.beginInsertRows(newQModelIndex(), self.accounts.len, self.accounts.len) self.beginInsertRows(newQModelIndex(), self.accounts.len, self.accounts.len)
self.accounts.add(account) self.accounts.add(account)
self.endInsertRows() self.endInsertRows()
proc forceUpdate*(self: AccountList) =
self.beginResetModel()
self.endResetModel()

View File

@ -58,3 +58,7 @@ QtObject:
self.beginResetModel() self.beginResetModel()
self.assets = assetList self.assets = assetList
self.endResetModel() self.endResetModel()
proc forceUpdate*(self: AssetList) =
self.beginResetModel()
self.endResetModel()

View File

@ -16,19 +16,26 @@ type Account* = ref object
realFiatBalance*: float realFiatBalance*: float
assetList*: seq[Asset] assetList*: seq[Asset]
type AccountArgs* = ref object of Args
account*: Account
type WalletModel* = ref object type WalletModel* = ref object
events*: EventEmitter events*: EventEmitter
accounts*: seq[Account] accounts*: seq[Account]
defaultCurrency*: string
proc updateBalance*(self: Account) proc updateBalance*(self: Account)
proc getDefaultCurrency*(self: WalletModel): string
proc newWalletModel*(events: EventEmitter): WalletModel = proc newWalletModel*(events: EventEmitter): WalletModel =
result = WalletModel() result = WalletModel()
result.accounts = @[] result.accounts = @[]
result.events = events result.events = events
result.defaultCurrency = ""
proc initEvents*(self: WalletModel) = proc initEvents*(self: WalletModel) =
self.events.on("currencyChanged") do(e: Args): self.events.on("currencyChanged") do(e: Args):
self.defaultCurrency = self.getDefaultCurrency()
for account in self.accounts: for account in self.accounts:
account.updateBalance() account.updateBalance()
self.events.emit("accountsUpdated", Args()) self.events.emit("accountsUpdated", Args())
@ -100,3 +107,20 @@ proc initAccounts*(self: WalletModel) =
var account = Account(name: "Status Account", address: address, iconColor: "", balance: "", assetList: assets, realFiatBalance: totalAccountBalance) var account = Account(name: "Status Account", address: address, iconColor: "", balance: "", assetList: assets, realFiatBalance: totalAccountBalance)
account.updateBalance() account.updateBalance()
self.accounts.add(account) self.accounts.add(account)
proc getTotalFiatBalance*(self: WalletModel): string =
var newBalance = 0.0
fmt"{newBalance:.2f} {self.defaultCurrency}"
proc generateNewAccount*(self: WalletModel, password: string, accountName: string, color: string) =
# TODO get a real address that we unlock with the password
var symbol = "SNT"
var asset = Asset(name:"Status", symbol: symbol, value: fmt"0.0", fiatValue: "$" & fmt"0.0", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg")
var assets: seq[Asset] = @[]
assets.add(asset)
var account = Account(name: accountName, address: "0x0000000000000000000000000000000000000000", iconColor: color, balance: fmt"0.00 {self.defaultCurrency}", assetList: assets, realFiatBalance: 0.0)
self.accounts.add(account)
self.events.emit("newAccountAdded", AccountArgs(account: account))