Skip to content

Commit

Permalink
always estimating gas for all transactions uniformly
Browse files Browse the repository at this point in the history
  • Loading branch information
goranjovic committed Feb 6, 2019
1 parent 5b27c00 commit cd06caf
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 32 deletions.
22 changes: 10 additions & 12 deletions src/status_im/ui/screens/wallet/send/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/status_im/ui/screens/wallet/send/views/amount.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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]]
Expand Down
31 changes: 21 additions & 10 deletions src/status_im/ui/screens/wallet/send/views/common.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -427,21 +430,29 @@
(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))

(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))))
2 changes: 1 addition & 1 deletion src/status_im/ui/screens/wallet/send/views/overview.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions src/status_im/utils/ethereum/erc20.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit cd06caf

Please sign in to comment.