From d2811d6c45ca8cb1d605af5eac565c5a6d5d52c6 Mon Sep 17 00:00:00 2001 From: Eric Dvorsak Date: Mon, 16 Apr 2018 17:19:10 +0200 Subject: [PATCH] [fix 3821] fix update-price to only update visible tokens --- src/status_im/ui/screens/subs.cljs | 1 - .../ui/screens/wallet/components/views.cljs | 12 ++--- src/status_im/ui/screens/wallet/events.cljs | 47 ++++++++-------- .../ui/screens/wallet/navigation.cljs | 2 +- .../ui/screens/wallet/send/events.cljs | 2 +- .../ui/screens/wallet/settings/subs.cljs | 11 ---- .../ui/screens/wallet/settings/views.cljs | 2 +- src/status_im/ui/screens/wallet/subs.cljs | 45 ++++++++++++---- src/status_im/ui/screens/wallet/views.cljs | 54 +++++++++---------- 9 files changed, 90 insertions(+), 86 deletions(-) delete mode 100644 src/status_im/ui/screens/wallet/settings/subs.cljs diff --git a/src/status_im/ui/screens/subs.cljs b/src/status_im/ui/screens/subs.cljs index 60f4c1c1ea..e0d75d50bd 100644 --- a/src/status_im/ui/screens/subs.cljs +++ b/src/status_im/ui/screens/subs.cljs @@ -10,7 +10,6 @@ status-im.ui.screens.wallet.subs status-im.ui.screens.wallet.request.subs status-im.ui.screens.wallet.send.subs - status-im.ui.screens.wallet.settings.subs status-im.ui.screens.wallet.transactions.subs status-im.ui.screens.network-settings.subs status-im.ui.screens.offline-messaging-settings.subs diff --git a/src/status_im/ui/screens/wallet/components/views.cljs b/src/status_im/ui/screens/wallet/components/views.cljs index 269c564c5f..77ed7c24ba 100644 --- a/src/status_im/ui/screens/wallet/components/views.cljs +++ b/src/status_im/ui/screens/wallet/components/views.cljs @@ -37,7 +37,7 @@ :request :wallet.request/set-symbol (throw (str "Unknown type: " k)))) -(defn- render-token [{:keys [symbol name icon decimals]} balance type] +(defn- render-token [{:keys [symbol name icon decimals amount]} type] [list/touchable-item #(do (re-frame/dispatch [(type->handler type) symbol]) (re-frame/dispatch [:navigate-back])) [react/view @@ -49,19 +49,17 @@ name] [react/text {:force-uppercase? true} (clojure.core/name symbol)]] - [list/item-secondary (wallet.utils/format-amount (symbol balance) decimals)]]]]]) + [list/item-secondary (wallet.utils/format-amount amount decimals)]]]]]) (views/defview assets [type] - (views/letsubs [network [:network] - visible-tokens [:wallet.settings/visible-tokens] - balance [:balance]] + (views/letsubs [assets [:wallet/visible-assets-with-amount]] [components/simple-screen [components/toolbar (i18n/label :t/wallet-assets)] [react/view {:style (assoc components.styles/flex :background-color :white)} [list/flat-list {:default-separator? true - :data (concat [tokens/ethereum] (wallet/current-tokens visible-tokens network)) + :data assets :key-fn (comp str :symbol) - :render-fn #(render-token % balance type)}]]])) + :render-fn #(render-token % type)}]]])) (defn send-assets [] [assets :send]) diff --git a/src/status_im/ui/screens/wallet/events.cljs b/src/status_im/ui/screens/wallet/events.cljs index 6f3523deac..40075b2a19 100644 --- a/src/status_im/ui/screens/wallet/events.cljs +++ b/src/status_im/ui/screens/wallet/events.cljs @@ -91,30 +91,31 @@ (ethereum/estimate-gas-web3 web3 (clj->js obj) #(re-frame/dispatch [success-event %2])))) ;; Handlers - (handlers/register-handler-fx :update-wallet - (fn [{{:keys [web3 accounts/current-account-id network network-status] :as db} :db} [_ symbols]] - (when (not= network-status :offline) - {:get-balance {:web3 web3 - :account-id current-account-id - :success-event :update-balance-success - :error-event :update-balance-fail} - :get-tokens-balance {:web3 web3 - :account-id current-account-id - :symbols symbols - :chain (ethereum/network->chain-keyword network) - :success-event :update-token-balance-success - :error-event :update-token-balance-fail} - :get-prices {:from "ETH" - :to "USD" - :success-event :update-prices-success - :error-event :update-prices-fail} - :db (-> db - (clear-error-message :prices-update) - (clear-error-message :balance-update) - (assoc-in [:wallet :balance-loading?] true) - (assoc :prices-loading? true))}))) + (fn [{{:keys [web3 accounts/current-account-id network network-status] :as db} :db} _] + (let [chain (ethereum/network->chain-keyword network) + symbols (get-in db [:accounts/accounts current-account-id :settings :wallet :visible-tokens chain])] + (when (not= network-status :offline) + {:get-balance {:web3 web3 + :account-id current-account-id + :success-event :update-balance-success + :error-event :update-balance-fail} + :get-tokens-balance {:web3 web3 + :account-id current-account-id + :symbols symbols + :chain chain + :success-event :update-token-balance-success + :error-event :update-token-balance-fail} + :get-prices {:from "ETH" + :to "USD" + :success-event :update-prices-success + :error-event :update-prices-fail} + :db (-> db + (clear-error-message :prices-update) + (clear-error-message :balance-update) + (assoc-in [:wallet :balance-loading?] true) + (assoc :prices-loading? true))})))) (handlers/register-handler-fx :update-transactions @@ -241,4 +242,4 @@ (handlers/register-handler-fx :wallet/show-error (fn [] - {:show-error (i18n/label :t/wallet-error)})) \ No newline at end of file + {:show-error (i18n/label :t/wallet-error)})) diff --git a/src/status_im/ui/screens/wallet/navigation.cljs b/src/status_im/ui/screens/wallet/navigation.cljs index f39d28e461..5cc95cdd2f 100644 --- a/src/status_im/ui/screens/wallet/navigation.cljs +++ b/src/status_im/ui/screens/wallet/navigation.cljs @@ -6,7 +6,7 @@ (defmethod navigation/preload-data! :wallet [db _] - (re-frame/dispatch [:update-wallet (map :symbol (tokens/tokens-for (ethereum/network->chain-keyword (:network db))))]) + (re-frame/dispatch [:update-wallet]) (assoc-in db [:wallet :current-tab] 0)) (defmethod navigation/preload-data! :transactions-history diff --git a/src/status_im/ui/screens/wallet/send/events.cljs b/src/status_im/ui/screens/wallet/send/events.cljs index 67d90e22e0..2adb86cd7c 100644 --- a/src/status_im/ui/screens/wallet/send/events.cljs +++ b/src/status_im/ui/screens/wallet/send/events.cljs @@ -124,7 +124,7 @@ (if sending-from-bot-or-dapp? ;;SENDING FROM BOT (CHAT) OR DAPP {:db (assoc-in new-db [:wallet :send-transaction] sending-db) ; we need to completely reset sending state here - :dispatch-n [[:update-wallet (map :symbol (tokens/tokens-for (ethereum/network->chain-keyword (:network db))))] + :dispatch-n [[:update-wallet] [:navigate-to-modal :wallet-send-transaction-modal] (when-not (seq gas) [:wallet/update-estimated-gas transaction]) diff --git a/src/status_im/ui/screens/wallet/settings/subs.cljs b/src/status_im/ui/screens/wallet/settings/subs.cljs deleted file mode 100644 index a07c4aade9..0000000000 --- a/src/status_im/ui/screens/wallet/settings/subs.cljs +++ /dev/null @@ -1,11 +0,0 @@ -(ns status-im.ui.screens.wallet.settings.subs - (:require [re-frame.core :as re-frame] - [status-im.utils.ethereum.core :as ethereum])) - -;; A set of visible-tokens for current network -(re-frame/reg-sub :wallet.settings/visible-tokens - :<- [:network] - :<- [:get-current-account] - (fn [[network current-account]] - (let [chain (ethereum/network->chain-keyword network)] - (get-in current-account [:settings :wallet :visible-tokens chain])))) \ No newline at end of file diff --git a/src/status_im/ui/screens/wallet/settings/views.cljs b/src/status_im/ui/screens/wallet/settings/views.cljs index ed5aa8259d..30db1b9d42 100644 --- a/src/status_im/ui/screens/wallet/settings/views.cljs +++ b/src/status_im/ui/screens/wallet/settings/views.cljs @@ -23,7 +23,7 @@ (defview manage-assets [] (letsubs [network [:network] - visible-tokens [:wallet.settings/visible-tokens]] + visible-tokens [:wallet/visible-tokens-symbols]] [react/view (merge components.styles/flex {:background-color :white}) [status-bar/status-bar {:type :modal-wallet}] [toolbar/toolbar #_{} {:style wallet.styles/toolbar} diff --git a/src/status_im/ui/screens/wallet/subs.cljs b/src/status_im/ui/screens/wallet/subs.cljs index 076bd110c3..1feb016a97 100644 --- a/src/status_im/ui/screens/wallet/subs.cljs +++ b/src/status_im/ui/screens/wallet/subs.cljs @@ -1,25 +1,27 @@ (ns status-im.ui.screens.wallet.subs - (:require [re-frame.core :refer [reg-sub subscribe]] - [status-im.utils.money :as money])) + (:require [re-frame.core :as re-frame] + [status-im.utils.money :as money] + [status-im.utils.ethereum.core :as ethereum] + [status-im.utils.ethereum.tokens :as tokens])) -(reg-sub :wallet +(re-frame/reg-sub :wallet (fn [db] (:wallet db))) -(reg-sub :balance +(re-frame/reg-sub :balance :<- [:wallet] (fn [wallet] (:balance wallet))) -(reg-sub :price +(re-frame/reg-sub :price (fn [db] (get-in db [:prices :price]))) -(reg-sub :last-day +(re-frame/reg-sub :last-day (fn [db] (get-in db [:prices :last-day]))) -(reg-sub :portfolio-value +(re-frame/reg-sub :portfolio-value :<- [:balance] :<- [:price] (fn [[balance price]] @@ -30,21 +32,42 @@ str) "..."))) -(reg-sub :prices-loading? +(re-frame/reg-sub :prices-loading? (fn [db] (:prices-loading? db))) -(reg-sub :wallet/balance-loading? +(re-frame/reg-sub :wallet/balance-loading? :<- [:wallet] (fn [wallet] (:balance-loading? wallet))) -(reg-sub :wallet/error-message? +(re-frame/reg-sub :wallet/error-message? :<- [:wallet] (fn [wallet] (or (get-in wallet [:errors :balance-update]) (get-in wallet [:errors :prices-update])))) -(reg-sub :get-wallet-unread-messages-number +(re-frame/reg-sub :get-wallet-unread-messages-number (fn [db] 0)) + +(re-frame/reg-sub :wallet/visible-tokens-symbols + :<- [:network] + :<- [:get-current-account] + (fn [[network current-account]] + (let [chain (ethereum/network->chain-keyword network)] + (get-in current-account [:settings :wallet :visible-tokens chain])))) + +(re-frame/reg-sub :wallet/visible-assets + :<- [:network] + :<- [:wallet/visible-tokens-symbols] + (fn [[network visible-tokens-symbols]] + (conj (filter #(contains? visible-tokens-symbols (:symbol %)) + (tokens/tokens-for (ethereum/network->chain-keyword network))) + tokens/ethereum))) + +(re-frame/reg-sub :wallet/visible-assets-with-amount + :<- [:balance] + :<- [:wallet/visible-assets] + (fn [[balance visible-assets]] + (map #(assoc % :amount (get balance (:symbol %))) visible-assets))) diff --git a/src/status_im/ui/screens/wallet/views.cljs b/src/status_im/ui/screens/wallet/views.cljs index 066a4daaef..d73b990d68 100644 --- a/src/status_im/ui/screens/wallet/views.cljs +++ b/src/status_im/ui/screens/wallet/views.cljs @@ -63,37 +63,31 @@ :number-of-lines 1} (clojure.core/name symbol)]]]]) -(defn current-tokens [visible-tokens network] - (filter #(contains? visible-tokens (:symbol %)) (tokens/tokens-for (ethereum/network->chain-keyword network)))) -(defn- asset-section [network balance visible-tokens] - (let [tokens (current-tokens visible-tokens network) - assets (map #(assoc % :amount (get balance (:symbol %))) (concat [tokens/ethereum] tokens))] - [react/view styles/asset-section - [react/text {:style styles/asset-section-title} (i18n/label :t/wallet-assets)] - [list/flat-list - {:default-separator? true - :scroll-enabled false - :key-fn (comp str :symbol) - :data assets - :render-fn render-asset}]])) + +(defn- asset-section [assets] + [react/view styles/asset-section + [react/text {:style styles/asset-section-title} (i18n/label :t/wallet-assets)] + [list/flat-list + {:default-separator? true + :scroll-enabled false + :key-fn (comp str :symbol) + :data assets + :render-fn render-asset}]]) (views/defview wallet [] - (views/letsubs [network [:network] - balance [:balance] - visible-tokens [:wallet.settings/visible-tokens] + (views/letsubs [assets [:wallet/visible-assets-with-amount] portfolio-value [:portfolio-value]] - (let [symbols (map :symbol (current-tokens visible-tokens network))] - [react/view styles/main-section - [toolbar-view] - [react/scroll-view {:content-container-style styles/scrollable-section - :refresh-control - (reagent/as-element - [react/refresh-control {:on-refresh #(re-frame/dispatch [:update-wallet symbols]) - :tint-color :white - :refreshing false}])} - [react/view {:style styles/scroll-top}] ;; Hack to allow different colors for top / bottom scroll view] - [total-section portfolio-value] - [list/action-list actions - {:container-style styles/action-section}] - [asset-section network balance visible-tokens]]]))) + [react/view styles/main-section + [toolbar-view] + [react/scroll-view {:content-container-style styles/scrollable-section + :refresh-control + (reagent/as-element + [react/refresh-control {:on-refresh #(re-frame/dispatch [:update-wallet]) + :tint-color :white + :refreshing false}])} + [react/view {:style styles/scroll-top}] ;; Hack to allow different colors for top / bottom scroll view] + [total-section portfolio-value] + [list/action-list actions + {:container-style styles/action-section}] + [asset-section assets]]]))