fix: add and handle RpcExceptions for wallet functions that would not allow login when offline

This commit is contained in:
Richard Ramos 2020-07-16 15:36:11 -04:00 committed by Iuri Matias
parent 0278379495
commit 9c9abe8ef9
3 changed files with 23 additions and 9 deletions

View File

@ -100,7 +100,11 @@ proc sendTransaction*(from_address: string, to: string, assetAddress: string, va
proc getBalance*(address: string): string =
let payload = %* [address, "latest"]
parseJson(callPrivateRPC("eth_getBalance", payload))["result"].str
let response = parseJson(callPrivateRPC("eth_getBalance", payload))
if response.hasKey("error"):
raise newException(RpcException, "Error getting balance: " & $response["error"])
else:
result = response["result"].str
proc hex2Eth*(input: string): string =
var value = fromHex(Stuint[256], input)

View File

@ -12,6 +12,9 @@ import wallet/collectibles
export account, collectibles
export Transaction
logScope:
topics = "wallet-model"
type WalletModel* = ref object
events*: EventEmitter
accounts*: seq[WalletAccount]

View File

@ -1,9 +1,13 @@
import strformat, strutils, stint, httpclient, json
import strformat, strutils, stint, httpclient, json, chronicles
import ../libstatus/wallet as status_wallet
import ../libstatus/tokens as status_tokens
import ../libstatus/types as status_types
import ../utils/cache
import account
logScope:
topics = "balance-manager"
type BalanceManager* = ref object
pricePairs: CachedValues
tokenBalances: CachedValues
@ -62,10 +66,13 @@ proc updateBalance*(asset: Asset, currency: string) =
asset.fiatValue = fmt"{fiat_balance:.2f} {currency}"
proc updateBalance*(account: WalletAccount, 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)
try:
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)
except RpcException:
error "Error in updateBalance", message = getCurrentExceptionMsg()