feat: update balances when receiving a transaction
This commit is contained in:
parent
d4e0ef6619
commit
057cdc08da
|
@ -25,7 +25,7 @@ proc delete*(self: NodeController) =
|
||||||
|
|
||||||
proc init*(self: NodeController) =
|
proc init*(self: NodeController) =
|
||||||
self.status.events.on(SignalType.Wallet.event) do(e:Args):
|
self.status.events.on(SignalType.Wallet.event) do(e:Args):
|
||||||
self.view.setLastMessage(WalletSignal(e).content)
|
self.view.setLastMessage($WalletSignal(e).blockNumber)
|
||||||
|
|
||||||
self.status.events.on(SignalType.DiscoverySummary.event) do(e:Args):
|
self.status.events.on(SignalType.DiscoverySummary.event) do(e:Args):
|
||||||
self.status.network.peerSummaryChange(DiscoverySummarySignal(e).enodes)
|
self.status.network.peerSummaryChange(DiscoverySummarySignal(e).enodes)
|
||||||
|
|
|
@ -47,4 +47,9 @@ proc init*(self: WalletController) =
|
||||||
|
|
||||||
self.status.events.on(SignalType.Wallet.event) do(e:Args):
|
self.status.events.on(SignalType.Wallet.event) do(e:Args):
|
||||||
var data = WalletSignal(e)
|
var data = WalletSignal(e)
|
||||||
debug "New signal received"
|
if data.eventType == "newblock":
|
||||||
|
for acc in data.accounts:
|
||||||
|
self.status.wallet.updateAccount(acc)
|
||||||
|
# TODO: show notification
|
||||||
|
# TODO: data.eventType == history, reorg, recent-history-fetching, recent-history-ready:
|
||||||
|
# see status-react/src/status_im/ethereum/subscriptions.cljs
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import json, options, httpclient, json, json_serialization, strformat, stint, strutils, sequtils, chronicles, parseutils, tables
|
import json, json, options, json_serialization, stint, chronicles
|
||||||
import core, types, utils
|
import core, types, utils, strutils, strformat
|
||||||
from nim_status import validateMnemonic, startWallet
|
from nim_status import validateMnemonic, startWallet
|
||||||
import ../wallet/account
|
import ../wallet/account
|
||||||
import ./contracts as contractMethods
|
import ./contracts as contractMethods
|
||||||
import eth/common/eth_types
|
import eth/common/eth_types
|
||||||
import ./types
|
import ./types
|
||||||
|
import ../signals/types as signal_types
|
||||||
|
|
||||||
proc getWalletAccounts*(): seq[WalletAccount] =
|
proc getWalletAccounts*(): seq[WalletAccount] =
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import NimQml, eventemitter, tables, json, chronicles, strutils, json_serialization
|
import NimQml, eventemitter, tables, json, chronicles, strutils, json_serialization
|
||||||
import ../libstatus/types as status_types
|
import ../libstatus/types as status_types
|
||||||
import types, messages, discovery, whisperFilter, envelopes, expired
|
import types, messages, discovery, whisperFilter, envelopes, expired, wallet
|
||||||
import ../status
|
import ../status
|
||||||
|
|
||||||
logScope:
|
logScope:
|
||||||
|
@ -56,7 +56,7 @@ QtObject:
|
||||||
of SignalType.WhisperFilterAdded:
|
of SignalType.WhisperFilterAdded:
|
||||||
signal = whisperFilter.fromEvent(jsonSignal)
|
signal = whisperFilter.fromEvent(jsonSignal)
|
||||||
of SignalType.Wallet:
|
of SignalType.Wallet:
|
||||||
signal = WalletSignal(content: $jsonSignal)
|
signal = wallet.fromEvent(jsonSignal)
|
||||||
of SignalType.NodeLogin:
|
of SignalType.NodeLogin:
|
||||||
signal = Json.decode($jsonSignal, NodeSignal)
|
signal = Json.decode($jsonSignal, NodeSignal)
|
||||||
of SignalType.DiscoverySummary:
|
of SignalType.DiscoverySummary:
|
||||||
|
|
|
@ -15,6 +15,11 @@ type NodeSignal* = ref object of Signal
|
||||||
|
|
||||||
type WalletSignal* = ref object of Signal
|
type WalletSignal* = ref object of Signal
|
||||||
content*: string
|
content*: string
|
||||||
|
eventType*: string
|
||||||
|
blockNumber*: int
|
||||||
|
accounts*: seq[string]
|
||||||
|
# newTransactions*: ???
|
||||||
|
erc20*: bool
|
||||||
|
|
||||||
type EnvelopeSentSignal* = ref object of Signal
|
type EnvelopeSentSignal* = ref object of Signal
|
||||||
messageIds*: seq[string]
|
messageIds*: seq[string]
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
import json
|
||||||
|
import types
|
||||||
|
|
||||||
|
proc fromEvent*(jsonSignal: JsonNode): Signal =
|
||||||
|
var signal:WalletSignal = WalletSignal()
|
||||||
|
if jsonSignal["event"].kind != JNull:
|
||||||
|
signal.eventType = jsonSignal["event"]["type"].getStr
|
||||||
|
signal.blockNumber = jsonSignal["event"]["blockNumber"].getInt
|
||||||
|
signal.erc20 = jsonSignal["event"]["erc20"].getBool
|
||||||
|
signal.accounts = @[]
|
||||||
|
for account in jsonSignal["event"]["accounts"]:
|
||||||
|
signal.accounts.add(account.getStr)
|
||||||
|
result = signal
|
||||||
|
|
|
@ -107,12 +107,12 @@ proc generateAccountConfiguredAssets*(self: WalletModel, accountAddress: string)
|
||||||
assets.add(existingToken)
|
assets.add(existingToken)
|
||||||
assets
|
assets
|
||||||
|
|
||||||
proc populateAccount*(self: WalletModel, walletAccount: var WalletAccount, balance: string) =
|
proc populateAccount*(self: WalletModel, walletAccount: var WalletAccount, balance: string, refreshCache: bool = false) =
|
||||||
var assets: seq[Asset] = self.generateAccountConfiguredAssets(walletAccount.address)
|
var assets: seq[Asset] = self.generateAccountConfiguredAssets(walletAccount.address)
|
||||||
walletAccount.balance = fmt"{balance} {self.defaultCurrency}"
|
walletAccount.balance = fmt"{balance} {self.defaultCurrency}"
|
||||||
walletAccount.assetList = assets
|
walletAccount.assetList = assets
|
||||||
walletAccount.realFiatBalance = 0.0
|
walletAccount.realFiatBalance = 0.0
|
||||||
updateBalance(walletAccount, self.getDefaultCurrency())
|
updateBalance(walletAccount, self.getDefaultCurrency(), refreshCache)
|
||||||
|
|
||||||
proc newAccount*(self: WalletModel, name: string, address: string, iconColor: string, balance: string, publicKey: string): WalletAccount =
|
proc newAccount*(self: WalletModel, name: string, address: string, iconColor: string, balance: string, publicKey: string): WalletAccount =
|
||||||
var assets: seq[Asset] = self.generateAccountConfiguredAssets(address)
|
var assets: seq[Asset] = self.generateAccountConfiguredAssets(address)
|
||||||
|
@ -125,9 +125,16 @@ proc initAccounts*(self: WalletModel) =
|
||||||
let accounts = status_wallet.getWalletAccounts()
|
let accounts = status_wallet.getWalletAccounts()
|
||||||
for account in accounts:
|
for account in accounts:
|
||||||
var acc = WalletAccount(account)
|
var acc = WalletAccount(account)
|
||||||
|
|
||||||
self.populateAccount(acc, "")
|
self.populateAccount(acc, "")
|
||||||
self.accounts.add(acc)
|
self.accounts.add(acc)
|
||||||
|
|
||||||
|
proc updateAccount*(self: WalletModel, address: string) =
|
||||||
|
for acc in self.accounts.mitems:
|
||||||
|
if acc.address == address:
|
||||||
|
self.populateAccount(acc, "", true)
|
||||||
|
self.events.emit("accountsUpdated", Args())
|
||||||
|
|
||||||
proc getTotalFiatBalance*(self: WalletModel): string =
|
proc getTotalFiatBalance*(self: WalletModel): string =
|
||||||
var newBalance = 0.0
|
var newBalance = 0.0
|
||||||
fmt"{self.totalBalance:.2f} {self.defaultCurrency}"
|
fmt"{self.totalBalance:.2f} {self.defaultCurrency}"
|
||||||
|
|
|
@ -36,14 +36,13 @@ proc getEthBalance(address: string): string =
|
||||||
var balance = status_wallet.getBalance(address)
|
var balance = status_wallet.getBalance(address)
|
||||||
result = status_wallet.hex2token(balance, 18)
|
result = status_wallet.hex2token(balance, 18)
|
||||||
|
|
||||||
proc getBalance*(symbol: string, accountAddress: string, tokenAddress: string): string =
|
proc getBalance*(symbol: string, accountAddress: string, tokenAddress: string, refreshCache: bool): string =
|
||||||
let cacheKey = fmt"{symbol}-{accountAddress}-{tokenAddress}"
|
let cacheKey = fmt"{symbol}-{accountAddress}-{tokenAddress}"
|
||||||
if balanceManager.tokenBalances.isCached(cacheKey):
|
if not refreshCache and balanceManager.tokenBalances.isCached(cacheKey):
|
||||||
return balanceManager.tokenBalances.get(cacheKey)
|
return balanceManager.tokenBalances.get(cacheKey)
|
||||||
|
|
||||||
if symbol == "ETH":
|
if symbol == "ETH":
|
||||||
let ethBalance = getEthBalance(accountAddress)
|
let ethBalance = getEthBalance(accountAddress)
|
||||||
balanceManager.tokenBalances.cacheValue(cacheKey, ethBalance)
|
|
||||||
return ethBalance
|
return ethBalance
|
||||||
|
|
||||||
result = $status_tokens.getTokenBalance(tokenAddress, accountAddress)
|
result = $status_tokens.getTokenBalance(tokenAddress, accountAddress)
|
||||||
|
@ -59,21 +58,21 @@ proc convertValue*(balance: string, fromCurrency: string, toCurrency: string): f
|
||||||
balanceManager.pricePairs.cacheValue(cacheKey, fiat_crypto_price)
|
balanceManager.pricePairs.cacheValue(cacheKey, fiat_crypto_price)
|
||||||
parseFloat(balance) * parseFloat(fiat_crypto_price)
|
parseFloat(balance) * parseFloat(fiat_crypto_price)
|
||||||
|
|
||||||
proc updateBalance*(asset: Asset, currency: string) =
|
proc updateBalance*(asset: Asset, currency: string, refreshCache: bool) =
|
||||||
var token_balance = getBalance(asset.symbol, asset.accountAddress, asset.address)
|
var token_balance = getBalance(asset.symbol, asset.accountAddress, asset.address, refreshCache)
|
||||||
let fiat_balance = convertValue(token_balance, asset.symbol, currency)
|
let fiat_balance = convertValue(token_balance, asset.symbol, currency)
|
||||||
asset.value = token_balance
|
asset.value = token_balance
|
||||||
asset.fiatBalanceDisplay = fmt"{fiat_balance:.2f} {currency}"
|
asset.fiatBalanceDisplay = fmt"{fiat_balance:.2f} {currency}"
|
||||||
asset.fiatBalance = fmt"{fiat_balance:.2f}"
|
asset.fiatBalance = fmt"{fiat_balance:.2f}"
|
||||||
|
|
||||||
proc updateBalance*(account: WalletAccount, currency: string) =
|
proc updateBalance*(account: WalletAccount, currency: string, refreshCache: bool = false) =
|
||||||
try:
|
try:
|
||||||
let eth_balance = getBalance("ETH", account.address, "")
|
let eth_balance = getBalance("ETH", account.address, "", refreshCache)
|
||||||
let usd_balance = convertValue(eth_balance, "ETH", currency)
|
let usd_balance = convertValue(eth_balance, "ETH", currency)
|
||||||
var totalAccountBalance = usd_balance
|
var totalAccountBalance = usd_balance
|
||||||
account.realFiatBalance = totalAccountBalance
|
account.realFiatBalance = totalAccountBalance
|
||||||
account.balance = fmt"{totalAccountBalance:.2f} {currency}"
|
account.balance = fmt"{totalAccountBalance:.2f} {currency}"
|
||||||
for asset in account.assetList:
|
for asset in account.assetList:
|
||||||
updateBalance(asset, currency)
|
updateBalance(asset, currency, refreshCache)
|
||||||
except RpcException:
|
except RpcException:
|
||||||
error "Error in updateBalance", message = getCurrentExceptionMsg()
|
error "Error in updateBalance", message = getCurrentExceptionMsg()
|
||||||
|
|
Loading…
Reference in New Issue