From cd06caf6d530353c831b93cf6d77204c13fd566e Mon Sep 17 00:00:00 2001 From: Goran Jovic Date: Wed, 6 Feb 2019 14:27:19 +0100 Subject: [PATCH] always estimating gas for all transactions uniformly --- .../ui/screens/wallet/send/events.cljs | 22 ++++++------- .../ui/screens/wallet/send/views/amount.cljs | 6 ++-- .../ui/screens/wallet/send/views/common.cljs | 31 +++++++++++++------ .../screens/wallet/send/views/overview.cljs | 2 +- src/status_im/utils/ethereum/erc20.cljs | 15 +++++---- 5 files changed, 44 insertions(+), 32 deletions(-) diff --git a/src/status_im/ui/screens/wallet/send/events.cljs b/src/status_im/ui/screens/wallet/send/events.cljs index 9440d32b94bc..1c654980d353 100644 --- a/src/status_im/ui/screens/wallet/send/events.cljs +++ b/src/status_im/ui/screens/wallet/send/events.cljs @@ -22,25 +22,23 @@ [status-im.utils.money :as money] [status-im.utils.security :as security] [status-im.utils.types :as types] - [status-im.utils.utils :as utils])) + [status-im.utils.utils :as utils] + [taoensso.timbre :as log])) (def wrong-password-error-code 5) ;;;; FX -(defn- send-ethers [params on-completed password] - (status/send-transaction (types/clj->json params) - password - on-completed)) - -(defn- send-tokens [{:keys [from to value gas gasPrice]} token on-completed password] - (let [contract (:address token)] - (erc20/transfer contract from to value gas gasPrice password on-completed))) +(defn get-tx-params [{:keys [from to value gas gasPrice] :as params} symbol coin] + (if (= :ETH symbol) + params + (erc20/transfer-tx (:address coin) from to (or value 0) gas gasPrice))) (defn send-transaction! [params symbol coin on-completed password] - (if (= :ETH symbol) - (send-ethers params on-completed password) - (send-tokens params coin on-completed password))) + (let [tx-params (get-tx-params params symbol coin)] + (status/send-transaction (types/clj->json tx-params) + password + on-completed))) (handlers/register-handler-fx :wallet/add-unconfirmed-transaction diff --git a/src/status_im/ui/screens/wallet/send/views/amount.cljs b/src/status_im/ui/screens/wallet/send/views/amount.cljs index fc5869d4fedf..8aad414c2949 100644 --- a/src/status_im/ui/screens/wallet/send/views/amount.cljs +++ b/src/status_im/ui/screens/wallet/send/views/amount.cljs @@ -171,8 +171,7 @@ (common/anim-ref-send @network-fees-modal-ref :open!)) close-network-fees! #(common/anim-ref-send @network-fees-modal-ref :close!)] (when-not (common/optimal-gas-present? transaction) - (common/refresh-optimal-gas - web3 (some :symbol [transaction native-currency]) tx-atom)) + (common/refresh-optimal-gas web3 tx-atom all-tokens network)) (fn [{:keys [balance network prices fiat-currency native-currency all-tokens modal?]}] (let [symbol (some :symbol [@tx-atom native-currency]) @@ -212,7 +211,8 @@ (if-not (= symbol (:symbol @tx-atom)) (update-amount-field nil)) (swap! tx-atom assoc :symbol symbol) - (common/refresh-optimal-gas web3 symbol tx-atom)) + (common/refresh-optimal-gas web3 tx-atom + all-tokens network)) (re-frame/dispatch [:navigate-back]))}]) :underlay-color colors/white-transparent} [show-current-asset coin]] diff --git a/src/status_im/ui/screens/wallet/send/views/common.cljs b/src/status_im/ui/screens/wallet/send/views/common.cljs index eb022462e916..0c273f942281 100644 --- a/src/status_im/ui/screens/wallet/send/views/common.cljs +++ b/src/status_im/ui/screens/wallet/send/views/common.cljs @@ -12,8 +12,11 @@ [status-im.i18n :as i18n] [clojure.string :as string] [status-im.ui.components.tooltip.views :as tooltip] + [status-im.ui.screens.wallet.send.events :as events] [status-im.utils.ethereum.core :as ethereum] - [status-im.utils.dimensions :as dimensions])) + [status-im.utils.dimensions :as dimensions] + [taoensso.timbre :as log] + [status-im.utils.ethereum.tokens :as tokens])) (defn toolbar [flow title chat-id] (let [action (if (#{:chat :dapp} flow) actions/close-white actions/back-white)] @@ -427,13 +430,21 @@ (some-> (token->fiat-conversion prices token fiat-currency gas-ether-price) (money/with-precision 3))) -(defn fetch-optimal-gas [web3 symbol cb] - (ethereum/gas-price - web3 - (fn [_ gas-price] - (when gas-price - (cb {:optimal-gas (ethereum/estimate-gas symbol) - :optimal-gas-price gas-price}))))) +(defn fetch-optimal-gas [web3 tx-atom all-tokens network cb] + (let [chain (ethereum/network->chain-keyword network) + symbol (:symbol @tx-atom) + coin (tokens/symbol->token all-tokens chain symbol)] + (ethereum/gas-price + web3 + (fn [_ gas-price] + (when gas-price + (ethereum/estimate-gas-web3 + web3 + (clj->js (events/get-tx-params @tx-atom symbol coin)) + (fn [_ gas] + (when gas + (cb {:optimal-gas (money/bignumber gas) + :optimal-gas-price (money/bignumber gas-price)}))))))))) (defn optimal-gas-present? [{:keys [optimal-gas optimal-gas-price]}] (and optimal-gas optimal-gas-price)) @@ -441,7 +452,7 @@ (defn current-gas [{:keys [gas gas-price optimal-gas optimal-gas-price]}] {:gas (or gas optimal-gas) :gas-price (or gas-price optimal-gas-price)}) -(defn refresh-optimal-gas [web3 symbol tx-atom] - (fetch-optimal-gas web3 symbol +(defn refresh-optimal-gas [web3 tx-atom all-tokens network] + (fetch-optimal-gas web3 tx-atom all-tokens network (fn [res] (swap! tx-atom merge res)))) diff --git a/src/status_im/ui/screens/wallet/send/views/overview.cljs b/src/status_im/ui/screens/wallet/send/views/overview.cljs index c5d01f0e21f6..4eb4163c3922 100644 --- a/src/status_im/ui/screens/wallet/send/views/overview.cljs +++ b/src/status_im/ui/screens/wallet/send/views/overview.cljs @@ -192,7 +192,7 @@ close-network-fees! #(common/anim-ref-send @network-fees-modal-ref :close!) modal? (= :dapp flow)] (when-not (common/optimal-gas-present? transaction) - (common/refresh-optimal-gas web3 (some :symbol [transaction native-currency]) tx-atom)) + (common/refresh-optimal-gas web3 tx-atom all-tokens chain)) (fn [] (let [transaction @tx-atom gas-gas-price->fiat diff --git a/src/status_im/utils/ethereum/erc20.cljs b/src/status_im/utils/ethereum/erc20.cljs index 39799464441b..3c8c98a22daa 100644 --- a/src/status_im/utils/ethereum/erc20.cljs +++ b/src/status_im/utils/ethereum/erc20.cljs @@ -19,7 +19,8 @@ [status-im.native-module.core :as status] [status-im.utils.security :as security] [status-im.js-dependencies :as dependencies] - [status-im.utils.types :as types]) + [status-im.utils.types :as types] + [taoensso.timbre :as log]) (:refer-clojure :exclude [name symbol])) (def utils dependencies/web3-utils) @@ -112,12 +113,14 @@ (defn balance-of [web3 contract address cb] (.balanceOf (get-instance web3 contract) address cb)) +(defn transfer-tx [contract from to value gas gas-price] + (merge (ethereum/call-params contract "transfer(address,uint256)" to value) + {:from from + :gas gas + :gasPrice gas-price})) + (defn transfer [contract from to value gas gas-price password on-completed] - (status/send-transaction (types/clj->json - (merge (ethereum/call-params contract "transfer(address,uint256)" to value) - {:from from - :gas gas - :gasPrice gas-price})) + (status/send-transaction (types/clj->json (transfer-tx contract from to value gas gas-price)) password on-completed))