add/remove & update configured tokens on the fly
add/remove & update configured tokens on the fly add wallet methods generate configured assets correct show selected tokens toggle assets add/remove configured tokens on the fly cleanup fix shown wallet
This commit is contained in:
parent
c609a00784
commit
5371f22e70
|
@ -37,16 +37,15 @@ proc init*(self: WalletController) =
|
|||
self.view.setTotalFiatBalance(self.status.wallet.getTotalFiatBalance())
|
||||
|
||||
self.status.events.on("accountsUpdated") do(e: Args):
|
||||
self.view.totalFiatBalanceChanged()
|
||||
self.view.currentAccountChanged()
|
||||
self.view.accountListChanged()
|
||||
self.view.accounts.forceUpdate()
|
||||
self.view.currentAssetList.forceUpdate()
|
||||
self.view.updateView()
|
||||
|
||||
self.status.events.on("newAccountAdded") do(e: Args):
|
||||
var account = AccountArgs(e)
|
||||
self.view.accounts.addAccountToList(account.account)
|
||||
|
||||
self.status.events.on("assetChanged") do(e: Args):
|
||||
self.view.updateView()
|
||||
|
||||
method onSignal(self: WalletController, data: Signal) =
|
||||
debug "New signal received"
|
||||
discard
|
||||
|
|
|
@ -124,3 +124,13 @@ QtObject:
|
|||
|
||||
proc hasAsset*(self: WalletView, account: string, symbol: string): bool {.slot.} =
|
||||
self.status.wallet.hasAsset(account, symbol)
|
||||
|
||||
proc toggleAsset*(self: WalletView, symbol: string, checked: bool, address: string, name: string, decimals: int, color: string) {.slot.} =
|
||||
self.status.wallet.toggleAsset(symbol, checked, address, name, decimals, color)
|
||||
|
||||
proc updateView*(self: WalletView) =
|
||||
self.totalFiatBalanceChanged()
|
||||
self.currentAccountChanged()
|
||||
self.accountListChanged()
|
||||
self.accounts.forceUpdate()
|
||||
self.setCurrentAssetList(self.currentAccount.account.assetList)
|
||||
|
|
|
@ -25,8 +25,8 @@ proc callPrivateRPC*(methodName: string, payload = %* []): string =
|
|||
result = $response
|
||||
if parseJSON(result).hasKey("error"):
|
||||
error "rpc response error", result = result
|
||||
except:
|
||||
error "error doing rpc request", methodName = methodName
|
||||
except Exception as e:
|
||||
error "error doing rpc request", methodName = methodName, exception=e.msg
|
||||
|
||||
proc sendTransaction*(inputJSON: string, password: string): string =
|
||||
var hashed_password = "0x" & $keccak_256.digest(password)
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import core as status
|
||||
import json
|
||||
import chronicles
|
||||
|
||||
logScope:
|
||||
topics = "wallet"
|
||||
|
||||
proc getCustomTokens*(): JsonNode =
|
||||
let payload = %* []
|
||||
let response = status.callPrivateRPC("wallet_getCustomTokens", payload).parseJson
|
||||
if response["result"].kind == JNull:
|
||||
return %* []
|
||||
return response["result"]
|
||||
|
||||
proc addCustomToken*(address: string, name: string, symbol: string, decimals: int, color: string): string =
|
||||
let payload = %* [{"address": address, "name": name, "symbol": symbol, "decimals": decimals, "color": color}]
|
||||
status.callPrivateRPC("wallet_addCustomToken", payload)
|
||||
|
||||
proc removeCustomToken*(address: string): string =
|
||||
let payload = %* [address]
|
||||
status.callPrivateRPC("wallet_deleteCustomToken", payload)
|
||||
|
||||
proc getTokensBalances*(accounts: openArray[string], tokens: openArray[string]): JsonNode =
|
||||
let payload = %* [accounts, tokens]
|
||||
let response = status.callPrivateRPC("wallet_getTokensBalances", payload).parseJson
|
||||
if response["result"].kind == JNull:
|
||||
return %* {}
|
||||
response["result"]
|
|
@ -6,6 +6,7 @@ import libstatus/wallet as status_wallet
|
|||
import libstatus/settings as status_settings
|
||||
import libstatus/accounts as status_accounts
|
||||
import chronicles
|
||||
import libstatus/tokens as status_tokens
|
||||
|
||||
type CurrencyArgs* = ref object of Args
|
||||
currency*: string
|
||||
|
@ -25,6 +26,7 @@ type WalletModel* = ref object
|
|||
events*: EventEmitter
|
||||
accounts*: seq[Account]
|
||||
defaultCurrency*: string
|
||||
tokens*: JsonNode
|
||||
|
||||
proc updateBalance*(self: Account)
|
||||
proc getDefaultCurrency*(self: WalletModel): string
|
||||
|
@ -32,11 +34,12 @@ proc getDefaultCurrency*(self: WalletModel): string
|
|||
proc newWalletModel*(events: EventEmitter): WalletModel =
|
||||
result = WalletModel()
|
||||
result.accounts = @[]
|
||||
result.tokens = %* []
|
||||
result.events = events
|
||||
result.defaultCurrency = ""
|
||||
|
||||
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:
|
||||
account.updateBalance()
|
||||
|
@ -79,7 +82,10 @@ proc getFiatValue*(eth_balance: string, symbol: string, fiat_symbol: string): fl
|
|||
fiat_balance
|
||||
|
||||
proc hasAsset*(self: WalletModel, account: string, symbol: string): bool =
|
||||
(symbol == "DAI") or (symbol == "OMG")
|
||||
for token in self.tokens:
|
||||
if symbol == token["symbol"].getStr:
|
||||
return true
|
||||
return false
|
||||
|
||||
proc updateBalance*(self: Account) =
|
||||
let defaultCurrency = getDefaultCurrency()
|
||||
|
@ -89,7 +95,19 @@ proc updateBalance*(self: Account) =
|
|||
var totalAccountBalance = usd_balance
|
||||
self.balance = fmt"{totalAccountBalance:.2f} {defaultCurrency}"
|
||||
|
||||
proc generateAccountConfiguredAssets*(self: WalletModel): seq[Asset] =
|
||||
var assets: seq[Asset] = @[]
|
||||
var symbol = "ETH"
|
||||
var asset = Asset(name:"Ethereum", symbol: symbol, value: fmt"0.0", fiatValue: "$" & fmt"0.0", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg")
|
||||
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")
|
||||
assets.add(existingToken)
|
||||
assets
|
||||
|
||||
proc initAccounts*(self: WalletModel) =
|
||||
self.tokens = status_tokens.getCustomTokens()
|
||||
let accounts = status_wallet.getWalletAccounts()
|
||||
|
||||
var totalAccountBalance: float = 0
|
||||
|
@ -103,9 +121,7 @@ proc initAccounts*(self: WalletModel) =
|
|||
|
||||
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 assets: seq[Asset] = self.generateAccountConfiguredAssets()
|
||||
|
||||
var account = Account(name: account.name, address: address, iconColor: account.color, balance: "", assetList: assets, realFiatBalance: totalAccountBalance)
|
||||
account.updateBalance()
|
||||
|
@ -115,8 +131,8 @@ proc getTotalFiatBalance*(self: WalletModel): string =
|
|||
var newBalance = 0.0
|
||||
fmt"{newBalance:.2f} {self.defaultCurrency}"
|
||||
|
||||
# TODO get a real address that we unlock with the password
|
||||
proc generateNewAccount*(self: WalletModel, password: string, accountName: string, color: string) =
|
||||
|
||||
let accounts = status_accounts.generateAddresses(1)
|
||||
var generatedAccount = accounts[0]
|
||||
generatedAccount.name = accountName
|
||||
|
@ -129,10 +145,20 @@ proc generateNewAccount*(self: WalletModel, password: string, accountName: strin
|
|||
|
||||
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 assets: seq[Asset] = self.generateAccountConfiguredAssets()
|
||||
var account = Account(name: accountName, address: generatedAccount.derived.defaultWallet.address, iconColor: color, balance: fmt"0.00 {self.defaultCurrency}", assetList: assets, realFiatBalance: 0.0)
|
||||
|
||||
self.accounts.add(account)
|
||||
self.events.emit("newAccountAdded", AccountArgs(account: account))
|
||||
|
||||
proc toggleAsset*(self: WalletModel, symbol: string, enable: bool, address: string, name: string, decimals: int, color: string) =
|
||||
if enable:
|
||||
discard status_tokens.addCustomToken(address, name, symbol, decimals, color)
|
||||
else:
|
||||
discard status_tokens.removeCustomToken(address)
|
||||
self.tokens = status_tokens.getCustomTokens()
|
||||
for account in self.accounts:
|
||||
var assets: seq[Asset] = self.generateAccountConfiguredAssets()
|
||||
account.assetList = assets
|
||||
self.events.emit("assetChanged", Args())
|
||||
|
|
|
@ -79,14 +79,14 @@ Item {
|
|||
anchors.left: parent.left
|
||||
anchors.leftMargin: 10
|
||||
width: parent.width
|
||||
height: 40
|
||||
property bool isVisible: searchBox.searchStr == "" || name.toLowerCase().includes(searchBox.searchStr.toLowerCase()) || symbol.toLowerCase().includes(searchBox.searchStr.toLowerCase())
|
||||
visible: isVisible ? true : false
|
||||
visible: isVisible && symbol !== "" ? true : false
|
||||
height: isVisible && symbol !== "" ? 40 : 0
|
||||
|
||||
Image {
|
||||
id: assetInfoImage
|
||||
width: 36
|
||||
height: 36
|
||||
height: isVisible && symbol !== "" ? 36 : 0
|
||||
source: hasIcon ? "../../../img/tokens/" + symbol + ".png" : "../../../img/tokens/0-native.png"
|
||||
anchors.left: parent.left
|
||||
anchors.leftMargin: 0
|
||||
|
@ -117,6 +117,7 @@ Item {
|
|||
checked: walletModel.hasAsset("0x123", symbol)
|
||||
anchors.right: parent.right
|
||||
anchors.rightMargin: 10
|
||||
onClicked: walletModel.toggleAsset(symbol, assetCheck.checked, address, name, decimals, "eeeeee")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,11 @@ import Qt.labs.platform 1.1
|
|||
|
||||
ListModel {
|
||||
|
||||
// due to weird bug in which the first checkbox doesn't check
|
||||
ListElement {
|
||||
symbol: ""
|
||||
}
|
||||
|
||||
ListElement {
|
||||
symbol: "DAI"
|
||||
name: "Dai Stablecoin"
|
||||
|
|
Loading…
Reference in New Issue