Skip to content

Commit

Permalink
[performance] improve wallet update performances
Browse files Browse the repository at this point in the history
`wallet-autoconfig-token` is a very expensive call on mainnet
because it checks the balance of every known token.

it is called:
- when wallet is refreshed by pulling
- when user goes on any wallet screen

this PR changes that by:
- calling it only when the wallet is initialized and there is no
visible-token configuration

it only calls update-wallet when a new transaction arrives
  • Loading branch information
yenda committed May 20, 2019
1 parent a62440e commit 42d2425
Show file tree
Hide file tree
Showing 25 changed files with 751 additions and 731 deletions.
8 changes: 3 additions & 5 deletions src/status_im/accounts/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
(:require [re-frame.core :as re-frame]
[status-im.accounts.update.core :as accounts.update]
[status-im.i18n :as i18n]
[status-im.ui.screens.navigation :as navigation]
[status-im.native-module.core :as native-module]
[status-im.ui.screens.wallet.settings.models :as wallet.settings.models]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.build :as build]
[status-im.utils.config :as config]
[status-im.utils.utils :as utils]
[status-im.utils.fx :as fx]
[status-im.utils.platform :as platform]
[status-im.utils.build :as build]))
[status-im.utils.utils :as utils]))

(re-frame/reg-fx
::chaos-mode-changed
Expand Down Expand Up @@ -44,7 +43,6 @@
[{:keys [db] :as cofx} modal?]
(fx/merge cofx
(continue-after-wallet-onboarding modal?)
(wallet.settings.models/wallet-autoconfig-tokens)
(accounts.update/account-update {:wallet-set-up-passed? true} {})))

(fx/defn update-dev-server-state
Expand Down
6 changes: 3 additions & 3 deletions src/status_im/accounts/login/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
[status-im.ethereum.transactions.core :as transactions]
[status-im.fleet.core :as fleet]
[status-im.i18n :as i18n]
[status-im.models.wallet :as models.wallet]
[status-im.native-module.core :as status]
[status-im.node.core :as node]
[status-im.protocol.core :as protocol]
Expand All @@ -22,6 +21,7 @@
[status-im.utils.security :as security]
[status-im.utils.types :as types]
[status-im.utils.universal-links.core :as universal-links]
[status-im.wallet.core :as wallet]
[taoensso.timbre :as log]))

(def rpc-endpoint "https://goerli.infura.io/v3/f315575765b14720b32382a61a89341a")
Expand Down Expand Up @@ -84,10 +84,10 @@

(fx/defn initialize-wallet [cofx]
(fx/merge cofx
(models.wallet/initialize-tokens)
(wallet/initialize-tokens)
(transactions/initialize)
(ethereum.subscriptions/initialize)
(models.wallet/update-wallet)))
(wallet/update-wallet)))

(fx/defn user-login [{:keys [db] :as cofx} create-database?]
(let [{:keys [address password]} (accounts.db/credentials cofx)]
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/chat/commands/impl/transactions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
(defview send-status [tx-hash outgoing]
(letsubs [{:keys [exists? confirmed?]} [:chats/transaction-status tx-hash]]
[react/touchable-highlight {:on-press #(when exists?
(re-frame/dispatch [:show-transaction-details tx-hash]))}
(re-frame/dispatch [:wallet.ui/show-transaction-details tx-hash]))}
[react/view transactions-styles/command-send-status-container
[vector-icons/icon (if confirmed?
:tiny-icons/tiny-check
Expand Down
6 changes: 1 addition & 5 deletions src/status_im/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,7 @@

(defn default-account-settings []
{:web3-opt-in? true
:wallet {:visible-tokens {:testnet #{:STT :HND}
:mainnet #{:SNT}
:rinkeby #{:MOKSHA :KDO}
:xdai #{}
:poa #{}}}})
:wallet {:visible-tokens {}}})

(def currencies
{:aed {:id :aed :code "AED" :display-name (i18n/label :t/currency-display-name-aed) :symbol "د.إ"}
Expand Down
17 changes: 11 additions & 6 deletions src/status_im/ethereum/transactions/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.fx :as fx]
[status-im.utils.types :as types]
[status-im.wallet.core :as wallet]
[taoensso.timbre :as log]))

(def confirmations-count-threshold 12)
Expand Down Expand Up @@ -180,14 +181,18 @@
;; -----------------------------------------------

(fx/defn new
[{:keys [db]} {:keys [hash] :as transaction}]
{:db (assoc-in db [:wallet :transactions hash] transaction)})
[{:keys [db] :as cofx} {:keys [hash] :as transaction}]
(fx/merge cofx
{:db (assoc-in db [:wallet :transactions hash] transaction)}
wallet/update-wallet))

(fx/defn handle-history
[{:keys [db]} transactions]
{:db (update-in db
[:wallet :transactions]
#(merge transactions %))})
[{:keys [db] :as cofx} transactions]
(fx/merge cofx
{:db (update-in db
[:wallet :transactions]
#(merge transactions %))}
wallet/update-wallet))

(fx/defn handle-token-history
[{:keys [db]} transactions]
Expand Down
95 changes: 95 additions & 0 deletions src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
[status-im.utils.handlers :as handlers]
[status-im.utils.logging.core :as logging]
[status-im.utils.utils :as utils]
[status-im.wallet.core :as wallet]
[status-im.wallet.db :as wallet.db]
[status-im.web3.core :as web3]
[taoensso.timbre :as log]
Expand Down Expand Up @@ -2133,6 +2134,12 @@
(ethereum.transactions/new cofx transaction)))

;; wallet events

(handlers/register-handler-fx
:wallet.ui/pull-to-refresh
(fn [cofx _]
(wallet/update-wallet cofx)))

(handlers/register-handler-fx
:wallet.transactions/add-filter
(fn [{:keys [db]} [_ id]]
Expand All @@ -2148,3 +2155,91 @@
(fn [{:keys [db]} _]
{:db (assoc-in db [:wallet :filters]
wallet.db/default-wallet-filters)}))

(handlers/register-handler-fx
:wallet.settings/toggle-visible-token
(fn [cofx [_ symbol checked?]]
(wallet/toggle-visible-token cofx symbol checked?)))

(handlers/register-handler-fx
:wallet/token-found
(fn [cofx [_ symbol balance]]
(wallet/configure-token-balance-and-visibility cofx symbol balance)))

(handlers/register-handler-fx
:TODO.remove/update-wallet
(fn [cofx _]
(wallet/update-wallet cofx)))

(handlers/register-handler-fx
:wallet.settings.ui/navigate-back-pressed
(fn [cofx [_ on-close]]
(fx/merge cofx
(when on-close
{:dispatch on-close})
(navigation/navigate-back)
(wallet/update-wallet))))

(handlers/register-handler-fx
:wallet.callback/update-balance-success
(fn [cofx [_ balance]]
(wallet/update-balance cofx balance)))

(handlers/register-handler-fx
:wallet.callback/update-balance-fail
(fn [cofx [_ err]]
(wallet/on-update-balance-fail cofx err)))

(handlers/register-handler-fx
:wallet.callback/update-token-balance-success
(fn [cofx [_ symbol balance]]
(wallet/update-token-balance cofx symbol balance)))

(handlers/register-handler-fx
:wallet.callback/update-token-balance-fail
(fn [cofx [_ symbol err]]
(wallet/on-update-token-balance-fail cofx symbol err)))

(handlers/register-handler-fx
:wallet.callback/update-prices-success
(fn [cofx [_ prices]]
(wallet/update-prices cofx prices)))

(handlers/register-handler-fx
:wallet.callback/update-prices-fail
(fn [cofx [_ err]]
(wallet/on-update-prices-fail cofx err)))

(handlers/register-handler-fx
:wallet.ui/show-transaction-details
(fn [cofx [_ hash]]
(wallet/open-transaction-details cofx hash)))

(handlers/register-handler-fx
:wallet/show-sign-transaction
(fn [cofx [_ {:keys [id method]} from-chat?]]
(wallet/open-send-transaction-modal cofx id method from-chat?)))

(handlers/register-handler-fx
:wallet/update-gas-price-success
(fn [cofx [_ price edit?]]
(wallet/update-gas-price cofx price edit?)))

(handlers/register-handler-fx
:TODO.remove/update-estimated-gas
(fn [{:keys [db]} [_ obj]]
{:update-estimated-gas {:web3 (:web3 db)
:obj obj
:success-event :wallet/update-estimated-gas-success}}))

(handlers/register-handler-fx
:wallet/update-estimated-gas-success
(fn [cofx [_ gas]]
(wallet/update-estimated-gas-price cofx gas)))

(handlers/register-handler-fx
:wallet.setup.ui/navigate-back-pressed
(fn [{:keys [db] :as cofx}]
(fx/merge cofx
{:db (assoc-in db [:wallet :send-transaction] {})}
(navigation/navigate-back))))
12 changes: 6 additions & 6 deletions src/status_im/extensions/capacities/ethereum.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
(:require [clojure.string :as string]
[status-im.constants :as constants]
[status-im.i18n :as i18n]
[status-im.models.wallet :as models.wallet]
[status-im.utils.hex :as hex]
[status-im.native-module.core :as status]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.ethereum.abi-spec :as abi-spec]
[status-im.utils.fx :as fx]
[status-im.utils.ethereum.ens :as ens]
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.ens :as ens]
[status-im.utils.fx :as fx]
[status-im.utils.handlers :as handlers]
[status-im.utils.hex :as hex]
[status-im.utils.money :as money]
[status-im.utils.types :as types]
[status-im.native-module.core :as status]))
[status-im.wallet.core :as wallet]))

(handlers/register-handler-fx
:extensions/wallet-ui-on-success
Expand Down Expand Up @@ -72,7 +72,7 @@
(let [tx-object (assoc (select-keys arguments [:to :gas :gas-price :value :nonce])
:data (when (and method params) (abi-spec/encode method params)))
transaction (prepare-extension-transaction tx-object (:contacts/contacts db) on-success on-failure)]
(models.wallet/open-modal-wallet-for-transaction db transaction tx-object)))
(wallet/open-modal-wallet-for-transaction db transaction tx-object)))

(handlers/register-handler-fx
:extensions/ethereum-send-transaction
Expand Down
32 changes: 16 additions & 16 deletions src/status_im/hardwallet/core.cljs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
(ns status-im.hardwallet.core
(:require [re-frame.core :as re-frame]
status-im.hardwallet.fx
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.config :as config]
[status-im.utils.fx :as fx]
[status-im.utils.platform :as platform]
[taoensso.timbre :as log]
[status-im.i18n :as i18n]
[status-im.utils.types :as types]
(:require [clojure.string :as string]
[re-frame.core :as re-frame]
[status-im.accounts.create.core :as accounts.create]
[status-im.accounts.login.core :as accounts.login]
[status-im.accounts.logout.core :as accounts.logout]
[status-im.accounts.recover.core :as accounts.recover]
[status-im.data-store.accounts :as accounts-store]
[status-im.i18n :as i18n]
[status-im.node.core :as node]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.config :as config]
[status-im.utils.datetime :as utils.datetime]
[status-im.data-store.accounts :as accounts-store]
[status-im.utils.ethereum.core :as ethereum]
[clojure.string :as string]
[status-im.accounts.login.core :as accounts.login]
[status-im.accounts.recover.core :as accounts.recover]
[status-im.models.wallet :as models.wallet]
[status-im.utils.ethereum.mnemonic :as mnemonic]
[status-im.accounts.logout.core :as accounts.logout]))
[status-im.utils.fx :as fx]
[status-im.utils.platform :as platform]
[status-im.utils.types :as types]
[status-im.wallet.core :as wallet]
[taoensso.timbre :as log]
status-im.hardwallet.fx))

(def default-pin "000000")

Expand Down Expand Up @@ -54,7 +54,7 @@
(if navigate-to-browser?
(fx/merge cofx
{:db (assoc-in db [:hardwallet :on-card-connected] nil)}
(models.wallet/discard-transaction)
(wallet/discard-transaction)
(navigation/navigate-to-cofx :browser nil))
(if (= :enter-pin-login (:view-id db))
(navigation/navigate-to-clean cofx :accounts nil)
Expand Down
Loading

0 comments on commit 42d2425

Please sign in to comment.