Skip to content

Commit

Permalink
[wallet] Show cached balance before fetching latest
Browse files Browse the repository at this point in the history
  • Loading branch information
rasom committed May 25, 2021
1 parent f5baee5 commit 58d3581
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/status_im/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
:app-state "active"
:wallet wallet.db/default-wallet
:wallet/all-tokens {}
:prices {}
:peers-count 0
:node-info {}
:peers-summary []
Expand Down
1 change: 1 addition & 0 deletions src/status_im/ethereum/json_rpc.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
"wallet_getTransfersByAddress" {}
"wallet_watchTransaction" {}
"wallet_checkRecentHistory" {}
"wallet_getCachedBalances" {}
"wallet_storePendingTransaction" {}
"wallet_deletePendingTransaction" {}
"wallet_getPendingTransactions" {}
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/multiaccounts/login/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
(wallet/set-max-block (get (first accounts) :address) 0)

:else
(wallet/update-balances nil scan-all-tokens?))
(wallet/get-cached-balances scan-all-tokens?))
(when-not (get db :wallet/new-account)
(wallet/restart-wallet-service nil))
(when-not (utils.mobile-sync/syncing-allowed? cofx)
Expand Down
13 changes: 7 additions & 6 deletions src/status_im/utils/prices.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
;;NOTE(this check is to allow value conversion for sidechains with native currencies listed on cryptocompare
;; under a symbol different than display symbol. Specific use case xDAI and POA.
(if mainnet?
(into {} (for [[from entries] (:RAW (types/json->clj resp))]
{from (into {} (for [[to entry] entries]
{to {:from (name from)
:to (name to)
:price (:PRICE entry)
:last-day (:OPEN24HOUR entry)}}))}))
(when-let [RAW (:RAW (types/json->clj resp))]
(into {} (for [[from entries] RAW]
{from (into {} (for [[to entry] entries]
{to {:from (name from)
:to (name to)
:price (:PRICE entry)
:last-day (:OPEN24HOUR entry)}}))})))
(into {} (for [[_ entries] (:RAW (types/json->clj resp))]
{:ETH (into {} (for [[to entry] entries]
{to {:from "ETH"
Expand Down
36 changes: 35 additions & 1 deletion src/status_im/wallet/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@
(defn assoc-error-message [db error-type err]
(assoc-in db [:wallet :errors error-type] (or err :unknown-error)))

(re-frame/reg-fx
:wallet/get-cached-balances
(fn [{:keys [addresses on-success on-error]}]
(json-rpc/call
{:method "wallet_getCachedBalances"
:params [addresses]
:on-success on-success
:on-error on-error})))

(fx/defn get-cached-balances
[{:keys [db]} scan-all-tokens?]
(let [addresses (map (comp string/lower-case :address)
(get db :multiaccount/accounts))]
{:wallet/get-cached-balances
{:addresses addresses
:on-success #(re-frame/dispatch [::set-cached-balances addresses % scan-all-tokens?])
:on-error #(re-frame/dispatch [::on-get-cached-balance-fail % scan-all-tokens?])}}))

(fx/defn on-update-balance-fail
{:events [::update-balance-fail]}
[{:keys [db]} err]
Expand All @@ -60,7 +78,7 @@
(fx/defn on-update-token-balance-fail
{:events [::update-token-balance-fail]}
[{:keys [db]} err]
(log/debug "Unable to get tokens balances: " err)
(log/debug "on-update-token-balance-fail: " err)
{:db (assoc-error-message db :balance-update :error-unable-to-get-token-balance)})

(fx/defn open-transaction-details
Expand Down Expand Up @@ -257,6 +275,12 @@
#{}))
{}))))))

(fx/defn on-get-cached-balance-fail
{:events [::on-get-cached-balance-fail]}
[{:keys [db] :as cofx} err scan-all-tokens?]
(log/warn "Can't fetch cached balances" err)
(update-balances cofx nil scan-all-tokens?))

(defn- set-checked [tokens-id token-id checked?]
(let [tokens-id (or tokens-id #{})]
(if checked?
Expand All @@ -270,6 +294,16 @@
[:wallet :accounts (eip55/address->checksum address) :balance :ETH]
(money/bignumber balance))})

(fx/defn set-cached-balances
{:events [::set-cached-balances]}
[cofx addresses balances scan-all-tokens?]
(apply fx/merge
cofx
(update-balances nil scan-all-tokens?)
(map (fn [{:keys [address balance]}]
(update-balance address balance))
balances)))

(defn has-empty-balances? [db]
(some #(nil? (get-in % [:balance :ETH]))
(get-in db [:wallet :accounts])))
Expand Down
6 changes: 3 additions & 3 deletions status-go-version.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh <tag>' instead",
"owner": "status-im",
"repo": "status-go",
"version": "v0.79.3",
"commit-sha1": "71f66f68064e9897cd17b6bcecba426a91405034",
"src-sha256": "1npbsy1fs50k6lajhl70yah9dhj7zf2a8x3mqr70lvn2w5qprcy5"
"version": "v0.79.4",
"commit-sha1": "d1dc5f1e272b72d84a8c6bee1fe09de6c7d6fd1e",
"src-sha256": "04nszlhn33p0szqpd115h50gcgw3jm3yjrvqv028ry1zzn4r8k6a"
}

0 comments on commit 58d3581

Please sign in to comment.