From 1faa35bfd986573e1d3f5c8fc7d60892e31505fa Mon Sep 17 00:00:00 2001 From: Andrey Shovkoplyas Date: Thu, 29 Mar 2018 15:46:03 +0300 Subject: [PATCH] fixed Empty Gas limit and Gas price Signed-off-by: Andrey Shovkoplyas --- src/status_im/constants.cljs | 2 + src/status_im/translations/en.cljs | 1 + src/status_im/ui/screens/wallet/events.cljs | 22 +++++++- .../ui/screens/wallet/send/events.cljs | 51 +++++++++++-------- .../ui/screens/wallet/send/subs.cljs | 4 +- src/status_im/utils/ethereum/core.cljs | 3 ++ 6 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/status_im/constants.cljs b/src/status_im/constants.cljs index 0dc61de3c8..65a252836f 100644 --- a/src/status_im/constants.cljs +++ b/src/status_im/constants.cljs @@ -26,6 +26,8 @@ (def default-network config/default-network) +(def contract-address "0x0000000000000000000000000000000000000000") + (def default-wallet-transactions {:filters {:type [{:id :inbound :label (i18n/label :t/incoming) :checked? true} diff --git a/src/status_im/translations/en.cljs b/src/status_im/translations/en.cljs index a66fdbf529..840f75532b 100644 --- a/src/status_im/translations/en.cljs +++ b/src/status_im/translations/en.cljs @@ -438,6 +438,7 @@ :receive-transaction "Receive transaction" :new-transaction "New Transaction" :transaction-history "Transaction History" + :new-contract "New Contract" ;; Wallet Send :wallet-choose-recipient "Choose Recipient" diff --git a/src/status_im/ui/screens/wallet/events.cljs b/src/status_im/ui/screens/wallet/events.cljs index c50aaffc54..6f3523deac 100644 --- a/src/status_im/ui/screens/wallet/events.cljs +++ b/src/status_im/ui/screens/wallet/events.cljs @@ -9,7 +9,8 @@ [status-im.utils.prices :as prices] [status-im.utils.transactions :as transactions] [taoensso.timbre :as log] - status-im.ui.screens.wallet.request.events)) + status-im.ui.screens.wallet.request.events + [status-im.utils.money :as money])) (defn get-balance [{:keys [web3 account-id on-success on-error]}] (if (and web3 account-id) @@ -84,6 +85,11 @@ (fn [{:keys [web3 success-event edit?]}] (ethereum/gas-price web3 #(re-frame/dispatch [success-event %2 edit?])))) +(reg-fx + :update-estimated-gas + (fn [{:keys [web3 obj success-event]}] + (ethereum/estimate-gas-web3 web3 (clj->js obj) #(re-frame/dispatch [success-event %2])))) + ;; Handlers (handlers/register-handler-fx @@ -220,7 +226,19 @@ (fn [db [_ price edit?]] (assoc-in db [:wallet (if edit? :edit :send-transaction) :gas-price] price))) +(handlers/register-handler-fx + :wallet/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-db + :wallet/update-estimated-gas-success + (fn [db [_ gas]] + (assoc-in db [:wallet :send-transaction :gas] (money/bignumber gas)))) + (handlers/register-handler-fx :wallet/show-error (fn [] - {:show-error (i18n/label :t/wallet-error)})) + {:show-error (i18n/label :t/wallet-error)})) \ No newline at end of file diff --git a/src/status_im/ui/screens/wallet/send/events.cljs b/src/status_im/ui/screens/wallet/send/events.cljs index 5cb06ac78d..67d90e22e0 100644 --- a/src/status_im/ui/screens/wallet/send/events.cljs +++ b/src/status_im/ui/screens/wallet/send/events.cljs @@ -106,22 +106,31 @@ transaction {:id id :from from :to to + :to-name (when (= to constants/contract-address) + (i18n/label :t/new-contract)) :symbol symbol :value (money/bignumber (or value 0)) :data data - :gas (money/bignumber (money/to-decimal gas)) - :gas-price (money/bignumber (money/to-decimal gasPrice)) + :gas (when (seq gas) + (money/bignumber (money/to-decimal gas))) + :gas-price (when (seq gasPrice) + (money/bignumber (money/to-decimal gasPrice))) :timestamp now :message-id message_id} - sending-from-chat? (not (get-in db [:wallet :send-transaction :waiting-signal?])) + sending-from-bot-or-dapp? (not (get-in db [:wallet :send-transaction :waiting-signal?])) new-db (assoc-in db [:wallet :transactions-unsigned id] transaction) sending-db {:id id - :from-chat? sending-from-chat?}] - (if sending-from-chat? - ;;SENDING FROM CHAT - {: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))))] [:navigate-to-modal :wallet-send-transaction-modal]]} - ;;SEND SCREEN WAITING SIGNAL + :from-chat? sending-from-bot-or-dapp?}] + (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))))] + [:navigate-to-modal :wallet-send-transaction-modal] + (when-not (seq gas) + [:wallet/update-estimated-gas transaction]) + (when-not (seq gasPrice) + [:wallet/update-gas-price])]} + ;;WALLET SEND SCREEN WAITING SIGNAL (let [{:keys [later? password]} (get-in db [:wallet :send-transaction]) new-db' (update-in new-db [:wallet :send-transaction] merge sending-db)] ; just update sending state as we are in wallet flow (if later? @@ -181,14 +190,14 @@ (if (and error (string? error) (not (string/blank? error))) ;; ignore error here, error will be handled in :transaction-failed {:db db'} (merge - {:db (-> db' - (assoc-in [:wallet :transactions hash] (prepare-unconfirmed-transaction db now hash id)) - (update-in [:wallet :transactions-unsigned] dissoc id) - (update-in [:wallet :send-transaction] merge clear-send-properties))} - (if modal? - {:dispatch [:navigate-back] - :dispatch-later [{:ms 400 :dispatch [:navigate-to-modal :wallet-transaction-sent-modal]}]} - {:dispatch [:navigate-to :wallet-transaction-sent]})))))) + {:db (-> db' + (assoc-in [:wallet :transactions hash] (prepare-unconfirmed-transaction db now hash id)) + (update-in [:wallet :transactions-unsigned] dissoc id) + (update-in [:wallet :send-transaction] merge clear-send-properties))} + (if modal? + {:dispatch [:navigate-back] + :dispatch-later [{:ms 400 :dispatch [:navigate-to-modal :wallet-transaction-sent-modal]}]} + {:dispatch [:navigate-to :wallet-transaction-sent]})))))) (defn on-transactions-modal-completed [raw-results] (let [results (:results (types/json->clj raw-results))] @@ -199,7 +208,7 @@ :wallet/sign-transaction (fn [{{:keys [web3] :accounts/keys [accounts current-account-id] :as db} :db} [_ later?]] - (let [db' (assoc-in db [:wallet :send-transaction :wrong-password?] false) + (let [db' (assoc-in db [:wallet :send-transaction :wrong-password?] false) network (:network db) {:keys [amount id password to symbol gas gas-price]} (get-in db [:wallet :send-transaction])] (if id @@ -292,6 +301,6 @@ :wallet.send/reset-gas-default (fn [{:keys [db]}] {:dispatch [:wallet/update-gas-price true] - :db (update-in db [:wallet :edit] - assoc - :gas (ethereum/estimate-gas (get-in db [:wallet :send-transaction :symbol])))})) + :db (update-in db [:wallet :edit] + assoc + :gas (ethereum/estimate-gas (get-in db [:wallet :send-transaction :symbol])))})) diff --git a/src/status_im/ui/screens/wallet/send/subs.cljs b/src/status_im/ui/screens/wallet/send/subs.cljs index 195c255983..56af2a0b8b 100644 --- a/src/status_im/ui/screens/wallet/send/subs.cljs +++ b/src/status_im/ui/screens/wallet/send/subs.cljs @@ -58,7 +58,9 @@ (when-let [unsigned-transaction (get unsigned-transactions (:id send-transaction))] (merge send-transaction - unsigned-transaction)))) + unsigned-transaction + {:gas (or (:gas unsigned-transaction) (:gas send-transaction)) + :gas-price (or (:gas-price unsigned-transaction) (:gas-price send-transaction))})))) (re-frame/reg-sub :wallet/edit :<- [:wallet] diff --git a/src/status_im/utils/ethereum/core.cljs b/src/status_im/utils/ethereum/core.cljs index 855ebad80f..77a4017752 100644 --- a/src/status_im/utils/ethereum/core.cljs +++ b/src/status_im/utils/ethereum/core.cljs @@ -87,6 +87,9 @@ (defn gas-price [web3 cb] (.getGasPrice (.-eth web3) cb)) +(defn estimate-gas-web3 [web3 obj cb] + (.estimateGas (.-eth web3) obj cb)) + (defn estimate-gas [symbol] (if (tokens/ethereum? symbol) default-transaction-gas