From 9c9abe8ef95f5df34024c361e17a8daeb0d6aa11 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 16 Jul 2020 15:36:11 -0400 Subject: [PATCH] fix: add and handle RpcExceptions for wallet functions that would not allow login when offline --- src/status/libstatus/wallet.nim | 6 +++++- src/status/wallet.nim | 3 +++ src/status/wallet/balance_manager.nim | 23 +++++++++++++++-------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/status/libstatus/wallet.nim b/src/status/libstatus/wallet.nim index 4b2a8c6f8d..80aad15ae3 100644 --- a/src/status/libstatus/wallet.nim +++ b/src/status/libstatus/wallet.nim @@ -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) diff --git a/src/status/wallet.nim b/src/status/wallet.nim index df45c5c1f2..0483841dec 100644 --- a/src/status/wallet.nim +++ b/src/status/wallet.nim @@ -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] diff --git a/src/status/wallet/balance_manager.nim b/src/status/wallet/balance_manager.nim index 28595c983e..7355526054 100644 --- a/src/status/wallet/balance_manager.nim +++ b/src/status/wallet/balance_manager.nim @@ -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()