From d74b27f95146f3db68e8de4125211f2002e9402b Mon Sep 17 00:00:00 2001 From: Julien Eluard Date: Mon, 12 Feb 2018 10:09:12 +0100 Subject: [PATCH] [ISSUE #3241] Do not use hardcoded gas price Signed-off-by: Eric Dvorsak --- src/status_im/ui/screens/db.cljs | 11 ----------- src/status_im/ui/screens/wallet/events.cljs | 17 +++++++++++++++++ src/status_im/ui/screens/wallet/navigation.cljs | 12 +++++++++--- .../ui/screens/wallet/send/events.cljs | 7 ++++--- src/status_im/utils/ethereum/core.cljs | 6 ++++-- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/status_im/ui/screens/db.cljs b/src/status_im/ui/screens/db.cljs index 0f74c35afc..881b7dd9b1 100644 --- a/src/status_im/ui/screens/db.cljs +++ b/src/status_im/ui/screens/db.cljs @@ -3,7 +3,6 @@ (:require [cljs.spec.alpha :as spec] [status-im.constants :as constants] [status-im.utils.platform :as platform] - [status-im.utils.ethereum.core :as ethereum] status-im.ui.screens.accounts.db status-im.ui.screens.contacts.db status-im.ui.screens.qr-scanner.db @@ -18,15 +17,6 @@ status-im.ui.screens.browser.db status-im.ui.screens.add-new.db)) -(defn gas-default [symbol] - {:gas (ethereum/estimate-gas symbol) - :gas-price ethereum/default-gas-price}) - -(def transaction-send-default - (let [symbol :ETH] - (merge (gas-default symbol) - {:symbol symbol}))) - ;; initial state of app-db (def app-db {:current-public-key nil :status-module-initialized? (or platform/ios? js/goog.DEBUG) @@ -46,7 +36,6 @@ :tags [] :sync-state :done :wallet.transactions constants/default-wallet-transactions - :wallet {:send-transaction transaction-send-default} :wallet-selected-asset {} :prices {} :notifications {} diff --git a/src/status_im/ui/screens/wallet/events.cljs b/src/status_im/ui/screens/wallet/events.cljs index 35b90fe69d..92174e1fd9 100644 --- a/src/status_im/ui/screens/wallet/events.cljs +++ b/src/status_im/ui/screens/wallet/events.cljs @@ -79,6 +79,11 @@ #(re-frame/dispatch [success-event %]) #(re-frame/dispatch [error-event %])))) +(reg-fx + :update-gas-price + (fn [{:keys [web3 success-event edit?]}] + (ethereum/gas-price web3 #(re-frame/dispatch [success-event %2 edit?])))) + ;; Handlers (handlers/register-handler-fx @@ -202,3 +207,15 @@ :content (i18n/label :t/transactions-delete-content) :confirm-button-text (i18n/label :t/confirm) :on-accept #(re-frame/dispatch [:wallet/discard-unsigned-transaction transaction-id])}})) + +(handlers/register-handler-fx + :wallet/update-gas-price + (fn [{:keys [db]} [_ edit?]] + {:update-gas-price {:web3 (:web3 db) + :success-event :wallet/update-gas-price-success + :edit? edit?}})) + +(handlers/register-handler-db + :wallet/update-gas-price-success + (fn [db [_ price edit?]] + (assoc-in db [:wallet (if edit? :edit :send-transaction) :gas-price] price))) diff --git a/src/status_im/ui/screens/wallet/navigation.cljs b/src/status_im/ui/screens/wallet/navigation.cljs index 47ec962627..f39d28e461 100644 --- a/src/status_im/ui/screens/wallet/navigation.cljs +++ b/src/status_im/ui/screens/wallet/navigation.cljs @@ -1,6 +1,5 @@ (ns status-im.ui.screens.wallet.navigation (:require [re-frame.core :as re-frame] - [status-im.ui.screens.db :as db] [status-im.ui.screens.navigation :as navigation] [status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.tokens :as tokens])) @@ -15,16 +14,23 @@ (re-frame/dispatch [:update-transactions]) db) +(def transaction-send-default + (let [symbol :ETH] + {:gas (ethereum/estimate-gas symbol) + :symbol symbol})) + + (defmethod navigation/preload-data! :wallet-request-transaction [db [event]] (if (= event :navigate-back) db (-> db (update :wallet dissoc :request-transaction) - (assoc-in [:wallet :send-transaction] db/transaction-send-default)))) + (assoc-in [:wallet :send-transaction] transaction-send-default)))) (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] db/transaction-send-default))) + (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 eda633a363..5cb06ac78d 100644 --- a/src/status_im/ui/screens/wallet/send/events.cljs +++ b/src/status_im/ui/screens/wallet/send/events.cljs @@ -291,6 +291,7 @@ (handlers/register-handler-fx :wallet.send/reset-gas-default (fn [{:keys [db]}] - {:db (update-in db [:wallet :edit] - merge - (db/gas-default (get-in db [:wallet :send-transaction :symbol])))})) + {:dispatch [:wallet/update-gas-price true] + :db (update-in db [:wallet :edit] + assoc + :gas (ethereum/estimate-gas (get-in db [:wallet :send-transaction :symbol])))})) diff --git a/src/status_im/utils/ethereum/core.cljs b/src/status_im/utils/ethereum/core.cljs index cc628e8e74..855ebad80f 100644 --- a/src/status_im/utils/ethereum/core.cljs +++ b/src/status_im/utils/ethereum/core.cljs @@ -83,10 +83,12 @@ (.sendTransaction (.-eth web3) (clj->js params) cb)) (def default-transaction-gas (money/bignumber 21000)) -(def default-gas-price (money/->wei :gwei 21)) + +(defn gas-price [web3 cb] + (.getGasPrice (.-eth web3) cb)) (defn estimate-gas [symbol] (if (tokens/ethereum? symbol) default-transaction-gas ;; TODO(jeluard) Rely on estimateGas call - (.times default-transaction-gas 5))) \ No newline at end of file + (.times default-transaction-gas 5)))