From fafd50775b7cad88ed2cfad27de5954b7d01323e Mon Sep 17 00:00:00 2001 From: Aleksandr Pantiukhov Date: Thu, 21 Jun 2018 17:50:54 +0200 Subject: [PATCH] [#4874]: Specified gas price is automatically reverted to default one if open Assets or any screen to specify Recipient Signed-off-by: Julien Eluard --- .../wallet/choose_recipient/events.cljs | 34 ++++++++------- .../ui/screens/wallet/navigation.cljs | 5 ++- .../ui/screens/wallet/send/events.cljs | 43 +++++++++++-------- 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/status_im/ui/screens/wallet/choose_recipient/events.cljs b/src/status_im/ui/screens/wallet/choose_recipient/events.cljs index b4d59b39c7..b8c988abcd 100644 --- a/src/status_im/ui/screens/wallet/choose_recipient/events.cljs +++ b/src/status_im/ui/screens/wallet/choose_recipient/events.cljs @@ -1,6 +1,7 @@ (ns status-im.ui.screens.wallet.choose-recipient.events (:require [status-im.constants :as constants] [status-im.i18n :as i18n] + [status-im.ui.screens.wallet.send.events :as send.events] [status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.eip681 :as eip681] [status-im.utils.handlers :as handlers] @@ -17,14 +18,17 @@ {:pre [(not (nil? address))]} (update-in db [:wallet :send-transaction] - #(cond-> (assoc % :to address :to-name name :whisper-identity whisper-identity) - value (assoc :amount value) - symbol (assoc :symbol symbol) - gas (assoc :gas (money/bignumber gas)) - gasPrice (assoc :gas-price (money/bignumber gasPrice)) - from-chat? (assoc :from-chat? from-chat?) - (and symbol (not gasPrice)) - (assoc :gas-price (ethereum/estimate-gas symbol))))) + (fn [{old-symbol :symbol :as old-transaction}] + (let [symbol-changed? (not= old-symbol symbol)] + (cond-> (assoc old-transaction :to address :to-name name :whisper-identity whisper-identity) + value (assoc :amount value) + symbol (assoc :symbol symbol) + (and gas symbol-changed?) (assoc :gas (money/bignumber gas)) + from-chat? (assoc :from-chat? from-chat?) + (and gasPrice symbol-changed?) + (assoc :gas-price (money/bignumber gasPrice)) + (and symbol (not gasPrice) symbol-changed?) + (assoc :gas-price (ethereum/estimate-gas symbol))))))) (defn- extract-details "First try to parse as EIP681 URI, if not assume this is an address directly. @@ -36,8 +40,9 @@ {:address s :chain-id chain-id}))) ;; NOTE(janherich) - whenever changing assets, we want to clear the previusly set amount/amount-text -(defn changed-asset [fx old-symbol new-symbol] +(defn changed-asset [{:keys [db] :as fx} old-symbol new-symbol] (-> fx + (merge (send.events/update-gas-price db)) (assoc-in [:db :wallet :send-transaction :amount] nil) (assoc-in [:db :wallet :send-transaction :amount-text] nil) (assoc-in [:db :wallet :send-transaction :asset-error] @@ -54,8 +59,7 @@ (handlers/register-handler-fx :wallet/fill-request-from-url (fn [{{:keys [network] :as db} :db} [_ data origin]] - (let [{:keys [view-id]} db - current-chain-id (get-in constants/default-networks [network :config :NetworkId]) + (let [current-chain-id (get-in constants/default-networks [network :config :NetworkId]) {:keys [address chain-id] :as details} (extract-details data current-chain-id) valid-network? (boolean (= current-chain-id chain-id)) previous-state (get-in db [:wallet :send-transaction]) @@ -63,19 +67,19 @@ new-symbol (:symbol details) old-amount (:amount previous-state) new-amount (:value details) - new-gas (:gas details)] + new-gas (:gas details) + symbol-changed? (and old-symbol new-symbol (not= old-symbol new-symbol))] (cond-> {:db db :dispatch [:navigate-back]} - (and address (= :choose-recipient view-id)) (assoc :dispatch [:navigate-back]) (and address valid-network?) (update :db #(fill-request-details % details)) - (and old-symbol new-symbol (not= old-symbol new-symbol)) (changed-asset old-symbol new-symbol) + symbol-changed? (changed-asset old-symbol new-symbol) (and old-amount new-amount (not= old-amount new-amount)) (changed-amount-warning old-amount new-amount) ;; NOTE(goranjovic) - the next line is there is because QR code scanning switches the amount to ETH ;; automatically, so we need to update the gas limit accordingly. The check for origin screen is there ;; so that we wouldn't also switch gas limit to ETH specific if the user pastes address as text. ;; We need to check if address is defined so that we wouldn't trigger this behavior when invalid QR is scanned ;; (e.g. whisper-id) - (and address (= origin :qr) (not new-gas)) (use-default-eth-gas) + (and address (= origin :qr) (not new-gas) symbol-changed?) (use-default-eth-gas) (not address) (assoc :show-error (i18n/label :t/wallet-invalid-address {:data data})) (and address (not valid-network?)) (assoc :show-error (i18n/label :t/wallet-invalid-chain-id {:data data :chain current-chain-id})))))) diff --git a/src/status_im/ui/screens/wallet/navigation.cljs b/src/status_im/ui/screens/wallet/navigation.cljs index 81feaad915..9ffab3b76e 100644 --- a/src/status_im/ui/screens/wallet/navigation.cljs +++ b/src/status_im/ui/screens/wallet/navigation.cljs @@ -32,7 +32,8 @@ (defmethod navigation/preload-data! :wallet-send-transaction [db [event]] - (re-frame/dispatch [:wallet/update-gas-price]) (if (= event :navigate-back) db - (assoc-in db [:wallet :send-transaction] transaction-send-default))) + (do + (re-frame/dispatch [:wallet/update-gas-price]) + (assoc-in db [:wallet :send-transaction] transaction-send-default)))) diff --git a/src/status_im/ui/screens/wallet/send/events.cljs b/src/status_im/ui/screens/wallet/send/events.cljs index 4f4fa72167..76ab2aa1a0 100644 --- a/src/status_im/ui/screens/wallet/send/events.cljs +++ b/src/status_im/ui/screens/wallet/send/events.cljs @@ -100,15 +100,34 @@ (fn [{:keys [db]} [_ amount symbol decimals]] {:db (set-and-validate-amount-db db amount symbol decimals)})) +(defn update-gas-price + ([db edit? success-event] + {:update-gas-price {:web3 (:web3 db) + :success-event (or success-event :wallet/update-gas-price-success) + :edit? edit?}}) + ([db edit?] (update-gas-price db edit? :wallet/update-gas-price-success)) + ([db] (update-gas-price db false :wallet/update-gas-price-success))) + +(defn recalculate-gas [{:keys [db] :as fx} symbol] + (-> fx + (assoc-in [:db :wallet :send-transaction :gas] (ethereum/estimate-gas symbol)) + (merge (update-gas-price db)))) + +(handlers/register-handler-fx + :wallet/update-gas-price + (fn [{:keys [db]} [_ edit?]] + (update-gas-price db edit?))) + (handlers/register-handler-fx :wallet.send/set-symbol (fn [{:keys [db]} [_ symbol]] - {:db (-> db - (assoc-in [:wallet :send-transaction :symbol] symbol) - (assoc-in [:wallet :send-transaction :amount] nil) - (assoc-in [:wallet :send-transaction :amount-text] nil) - (assoc-in [:wallet :send-transaction :asset-error] nil) - (assoc-in [:wallet :send-transaction :gas] (ethereum/estimate-gas symbol)))})) + (let [old-symbol (get-in db [:wallet :send-transaction :symbol])] + (cond-> {:db (-> db + (assoc-in [:wallet :send-transaction :symbol] symbol) + (assoc-in [:wallet :send-transaction :amount] nil) + (assoc-in [:wallet :send-transaction :amount-text] nil) + (assoc-in [:wallet :send-transaction :asset-error] nil))} + (not= old-symbol symbol) (recalculate-gas symbol))))) (handlers/register-handler-fx :wallet.send/toggle-advanced @@ -313,18 +332,6 @@ :masked-password password :on-completed on-transactions-modal-completed}}))) -(defn update-gas-price - ([db edit? success-event] - {:update-gas-price {:web3 (:web3 db) - :success-event (or success-event :wallet/update-gas-price-success) - :edit? edit?}}) - ([db edit?] (update-gas-price db edit? :wallet/update-gas-price-success))) - -(handlers/register-handler-fx - :wallet/update-gas-price - (fn [{:keys [db]} [_ edit?]] - (update-gas-price db edit?))) - (defn sign-transaction-modal [{:keys [db]} default-gas-price] ;;TODO(goranjovic) - unify send-transaction and unsigned-transaction (let [{:keys [id password] :as send-transaction} (get-in db [:wallet :send-transaction])