[#4874]: Specified gas price is automatically reverted to default one if open Assets or any screen to specify Recipient

Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
Aleksandr Pantiukhov 2018-06-21 17:50:54 +02:00 committed by Julien Eluard
parent d12c05d6f7
commit fafd50775b
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
3 changed files with 47 additions and 35 deletions

View File

@ -1,6 +1,7 @@
(ns status-im.ui.screens.wallet.choose-recipient.events (ns status-im.ui.screens.wallet.choose-recipient.events
(:require [status-im.constants :as constants] (:require [status-im.constants :as constants]
[status-im.i18n :as i18n] [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.core :as ethereum]
[status-im.utils.ethereum.eip681 :as eip681] [status-im.utils.ethereum.eip681 :as eip681]
[status-im.utils.handlers :as handlers] [status-im.utils.handlers :as handlers]
@ -17,14 +18,17 @@
{:pre [(not (nil? address))]} {:pre [(not (nil? address))]}
(update-in (update-in
db [:wallet :send-transaction] db [:wallet :send-transaction]
#(cond-> (assoc % :to address :to-name name :whisper-identity whisper-identity) (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) value (assoc :amount value)
symbol (assoc :symbol symbol) symbol (assoc :symbol symbol)
gas (assoc :gas (money/bignumber gas)) (and gas symbol-changed?) (assoc :gas (money/bignumber gas))
gasPrice (assoc :gas-price (money/bignumber gasPrice))
from-chat? (assoc :from-chat? from-chat?) from-chat? (assoc :from-chat? from-chat?)
(and symbol (not gasPrice)) (and gasPrice symbol-changed?)
(assoc :gas-price (ethereum/estimate-gas symbol))))) (assoc :gas-price (money/bignumber gasPrice))
(and symbol (not gasPrice) symbol-changed?)
(assoc :gas-price (ethereum/estimate-gas symbol)))))))
(defn- extract-details (defn- extract-details
"First try to parse as EIP681 URI, if not assume this is an address directly. "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}))) {:address s :chain-id chain-id})))
;; NOTE(janherich) - whenever changing assets, we want to clear the previusly set amount/amount-text ;; 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 (-> fx
(merge (send.events/update-gas-price db))
(assoc-in [:db :wallet :send-transaction :amount] nil) (assoc-in [:db :wallet :send-transaction :amount] nil)
(assoc-in [:db :wallet :send-transaction :amount-text] nil) (assoc-in [:db :wallet :send-transaction :amount-text] nil)
(assoc-in [:db :wallet :send-transaction :asset-error] (assoc-in [:db :wallet :send-transaction :asset-error]
@ -54,8 +59,7 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:wallet/fill-request-from-url :wallet/fill-request-from-url
(fn [{{:keys [network] :as db} :db} [_ data origin]] (fn [{{:keys [network] :as db} :db} [_ data origin]]
(let [{:keys [view-id]} db (let [current-chain-id (get-in constants/default-networks [network :config :NetworkId])
current-chain-id (get-in constants/default-networks [network :config :NetworkId])
{:keys [address chain-id] :as details} (extract-details data current-chain-id) {:keys [address chain-id] :as details} (extract-details data current-chain-id)
valid-network? (boolean (= current-chain-id chain-id)) valid-network? (boolean (= current-chain-id chain-id))
previous-state (get-in db [:wallet :send-transaction]) previous-state (get-in db [:wallet :send-transaction])
@ -63,19 +67,19 @@
new-symbol (:symbol details) new-symbol (:symbol details)
old-amount (:amount previous-state) old-amount (:amount previous-state)
new-amount (:value details) 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 (cond-> {:db db
:dispatch [:navigate-back]} :dispatch [:navigate-back]}
(and address (= :choose-recipient view-id)) (assoc :dispatch [:navigate-back])
(and address valid-network?) (update :db #(fill-request-details % details)) (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) (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 ;; 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 ;; 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. ;; 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 ;; 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) ;; (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})) (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})))))) (and address (not valid-network?)) (assoc :show-error (i18n/label :t/wallet-invalid-chain-id {:data data :chain current-chain-id}))))))

View File

@ -32,7 +32,8 @@
(defmethod navigation/preload-data! :wallet-send-transaction (defmethod navigation/preload-data! :wallet-send-transaction
[db [event]] [db [event]]
(re-frame/dispatch [:wallet/update-gas-price])
(if (= event :navigate-back) (if (= event :navigate-back)
db 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))))

View File

@ -100,15 +100,34 @@
(fn [{:keys [db]} [_ amount symbol decimals]] (fn [{:keys [db]} [_ amount symbol decimals]]
{:db (set-and-validate-amount-db 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 (handlers/register-handler-fx
:wallet.send/set-symbol :wallet.send/set-symbol
(fn [{:keys [db]} [_ symbol]] (fn [{:keys [db]} [_ symbol]]
{:db (-> db (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 :symbol] symbol)
(assoc-in [:wallet :send-transaction :amount] nil) (assoc-in [:wallet :send-transaction :amount] nil)
(assoc-in [:wallet :send-transaction :amount-text] nil) (assoc-in [:wallet :send-transaction :amount-text] nil)
(assoc-in [:wallet :send-transaction :asset-error] nil) (assoc-in [:wallet :send-transaction :asset-error] nil))}
(assoc-in [:wallet :send-transaction :gas] (ethereum/estimate-gas symbol)))})) (not= old-symbol symbol) (recalculate-gas symbol)))))
(handlers/register-handler-fx (handlers/register-handler-fx
:wallet.send/toggle-advanced :wallet.send/toggle-advanced
@ -313,18 +332,6 @@
:masked-password password :masked-password password
:on-completed on-transactions-modal-completed}}))) :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] (defn sign-transaction-modal [{:keys [db]} default-gas-price]
;;TODO(goranjovic) - unify send-transaction and unsigned-transaction ;;TODO(goranjovic) - unify send-transaction and unsigned-transaction
(let [{:keys [id password] :as send-transaction} (get-in db [:wallet :send-transaction]) (let [{:keys [id password] :as send-transaction} (get-in db [:wallet :send-transaction])