diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a9ec69ae6..fef5a473af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed chat message layout for right-to-left languages - Fixed parsing of messages containing multiple dots (elipsis) - Fixed Webview: Screen cut off when using ERC dEX DApp [#3131] +- Fixed gas validation: Showing message when total amount being sent plus the max gas cost exceed the balance [#3441] ## [0.9.22] - 2018-07-09 ### Added diff --git a/src/status_im/translations/en.cljs b/src/status_im/translations/en.cljs index 60ccb2113e..500bf80de2 100644 --- a/src/status_im/translations/en.cljs +++ b/src/status_im/translations/en.cljs @@ -455,6 +455,7 @@ :signing-message-phrase-description "Sign the message by entering your password. Make sure that the words above match your secret signing phrase" :signing-phrase-description "Sign the transaction by entering your password. Make sure that the words above match your secret signing phrase" :wallet-insufficient-funds "Insufficient funds" + :wallet-insufficient-gas "Not enough ETH for gas" :receive "Receive" :request-qr-legend "Share this code to receive assets" :send-request "Send request" diff --git a/src/status_im/ui/screens/wallet/send/subs.cljs b/src/status_im/ui/screens/wallet/send/subs.cljs index 7c5fd3dc08..1bb7f9b9fa 100644 --- a/src/status_im/ui/screens/wallet/send/subs.cljs +++ b/src/status_im/ui/screens/wallet/send/subs.cljs @@ -75,15 +75,32 @@ send-transaction) edit))) +(defn check-sufficient-funds [transaction balance symbol amount] + (assoc transaction :sufficient-funds? + (or (nil? amount) + (money/sufficient-funds? amount (get balance symbol))))) + +(defn check-sufficient-gas [transaction balance symbol amount] + (assoc transaction :sufficient-gas? + (or (nil? amount) + (let [available-ether (get balance :ETH) + available-for-gas (if (= :ETH symbol) + (.minus available-ether (money/bignumber amount)) + available-ether)] + (money/sufficient-funds? (-> transaction + :max-fee + money/bignumber + (money/formatted->internal :ETH 18)) + (money/bignumber available-for-gas)))))) + (re-frame/reg-sub :wallet.send/transaction :<- [::send-transaction] :<- [:balance] (fn [[{:keys [amount symbol] :as transaction} balance]] (-> transaction (models.wallet/add-max-fee) - (assoc :sufficient-funds? - (or (nil? amount) - (money/sufficient-funds? amount (get balance symbol))))))) + (check-sufficient-funds balance symbol amount) + (check-sufficient-gas balance symbol amount)))) (re-frame/reg-sub :wallet.send/unsigned-transaction :<- [::unsigned-transaction] @@ -91,9 +108,10 @@ :<- [:balance] (fn [[{:keys [value to symbol] :as transaction} contacts balance]] (when transaction - (let [contact (contacts (utils.hex/normalize-hex to)) - sufficient-funds? (money/sufficient-funds? value (get balance symbol))] - (cond-> (assoc (models.wallet/add-max-fee transaction) - :amount value - :sufficient-funds? sufficient-funds?) - contact (assoc :to-name (:name contact))))))) + (let [contact (contacts (utils.hex/normalize-hex to))] + (-> transaction + (assoc :amount value + :to-name (:name contact)) + (models.wallet/add-max-fee) + (check-sufficient-funds balance symbol value) + (check-sufficient-gas balance symbol value)))))) diff --git a/src/status_im/ui/screens/wallet/send/views.cljs b/src/status_im/ui/screens/wallet/send/views.cljs index 20d468533f..46c3b1fcc8 100644 --- a/src/status_im/ui/screens/wallet/send/views.cljs +++ b/src/status_im/ui/screens/wallet/send/views.cljs @@ -73,16 +73,16 @@ (i18n/label sign-label) [vector-icons/icon :icons/forward {:color :white}]]])) -(defn- sign-enabled? [amount-error to amount] +(defn- sign-enabled? [amount-error to amount modal?] (and (nil? amount-error) - (not (nil? to)) (not= to "") + (or modal? (not (nil? to)) (not= to "")) ;;NOTE(goranjovic) - contract creation will have empty `to` (not (nil? amount)))) ;; "Sign Later" and "Sign Transaction >" buttons -(defn- sign-button [amount-error to amount sufficient-funds? modal?] - (let [sign-enabled? (sign-enabled? amount-error to amount) - immediate-sign-enabled? (or modal? (and sign-enabled? sufficient-funds?))] +(defn- sign-button [amount-error to amount sufficient-funds? sufficient-gas? modal?] + (let [sign-enabled? (sign-enabled? amount-error to amount modal?) + immediate-sign-enabled? (and sign-enabled? sufficient-funds? sufficient-gas?)] [bottom-buttons/bottom-buttons styles/sign-buttons [react/view] @@ -187,7 +187,7 @@ (:invalid? gas-price-edit))} (i18n/label :t/done)]]]]))) -(defn- advanced-cartouche [{:keys [max-fee gas gas-price]} modal?] +(defn- advanced-cartouche [{:keys [max-fee gas gas-price]}] [react/view [wallet.components/cartouche {:on-press #(do (re-frame/dispatch [:wallet.send/clear-gas]) (re-frame/dispatch [:navigate-to-modal :wallet-transaction-fee]))} @@ -212,11 +212,11 @@ :key :wallet-advanced}] [vector-icons/icon (if advanced? :icons/up :icons/down) {:color :white}]]]] (when advanced? - [advanced-cartouche transaction modal?])]) + [advanced-cartouche transaction])]) (defn- send-transaction-panel [{:keys [modal? transaction scroll advanced? network]}] - (let [{:keys [amount amount-text amount-error asset-error signing? to to-name sufficient-funds? in-progress? - from-chat? symbol]} transaction + (let [{:keys [amount amount-text amount-error asset-error signing? to to-name sufficient-funds? sufficient-gas? + in-progress? from-chat? symbol]} transaction {:keys [decimals] :as token} (tokens/asset-for (ethereum/network->chain-keyword network) symbol) timeout (atom nil)] [wallet.components/simple-screen {:avoid-keyboard? (not modal?) @@ -239,7 +239,8 @@ :symbol symbol}] [components/amount-selector {:disabled? (or from-chat? modal?) :error (or amount-error - (when-not sufficient-funds? (i18n/label :t/wallet-insufficient-funds))) + (when-not sufficient-funds? (i18n/label :t/wallet-insufficient-funds)) + (when-not sufficient-gas? (i18n/label :t/wallet-insufficient-gas))) :amount amount :amount-text amount-text :input-options {:max-length 21 @@ -251,7 +252,7 @@ #(re-frame/dispatch (if modal? [:wallet/cancel-signing-modal] [:wallet/discard-transaction])) #(re-frame/dispatch (if modal? [:wallet/sign-transaction-modal] [:wallet/sign-transaction])) :t/transactions-sign-transaction] - [sign-button amount-error to amount sufficient-funds? modal?]) + [sign-button amount-error to amount sufficient-funds? sufficient-gas? modal?]) (when signing? [sign-panel :t/signing-phrase-description in-progress?]) (when in-progress? [react/view styles/processing-view])]]))