bug #3441 - considering gas costs when validating send transaction against balance

Signed-off-by: Goran Jovic <goranjovic@gmail.com>
This commit is contained in:
Goran Jovic 2018-07-06 16:51:03 +02:00
parent ab81d95126
commit 2cdac26cda
No known key found for this signature in database
GPG Key ID: D429D1A9B2EB8A8E
4 changed files with 41 additions and 20 deletions

View File

@ -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 chat message layout for right-to-left languages
- Fixed parsing of messages containing multiple dots (elipsis) - Fixed parsing of messages containing multiple dots (elipsis)
- Fixed Webview: Screen cut off when using ERC dEX DApp [#3131] - 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 ## [0.9.22] - 2018-07-09
### Added ### Added

View File

@ -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-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" :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-funds "Insufficient funds"
:wallet-insufficient-gas "Not enough ETH for gas"
:receive "Receive" :receive "Receive"
:request-qr-legend "Share this code to receive assets" :request-qr-legend "Share this code to receive assets"
:send-request "Send request" :send-request "Send request"

View File

@ -75,15 +75,32 @@
send-transaction) send-transaction)
edit))) 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 (re-frame/reg-sub :wallet.send/transaction
:<- [::send-transaction] :<- [::send-transaction]
:<- [:balance] :<- [:balance]
(fn [[{:keys [amount symbol] :as transaction} balance]] (fn [[{:keys [amount symbol] :as transaction} balance]]
(-> transaction (-> transaction
(models.wallet/add-max-fee) (models.wallet/add-max-fee)
(assoc :sufficient-funds? (check-sufficient-funds balance symbol amount)
(or (nil? amount) (check-sufficient-gas balance symbol amount))))
(money/sufficient-funds? amount (get balance symbol)))))))
(re-frame/reg-sub :wallet.send/unsigned-transaction (re-frame/reg-sub :wallet.send/unsigned-transaction
:<- [::unsigned-transaction] :<- [::unsigned-transaction]
@ -91,9 +108,10 @@
:<- [:balance] :<- [:balance]
(fn [[{:keys [value to symbol] :as transaction} contacts balance]] (fn [[{:keys [value to symbol] :as transaction} contacts balance]]
(when transaction (when transaction
(let [contact (contacts (utils.hex/normalize-hex to)) (let [contact (contacts (utils.hex/normalize-hex to))]
sufficient-funds? (money/sufficient-funds? value (get balance symbol))] (-> transaction
(cond-> (assoc (models.wallet/add-max-fee transaction) (assoc :amount value
:amount value :to-name (:name contact))
:sufficient-funds? sufficient-funds?) (models.wallet/add-max-fee)
contact (assoc :to-name (:name contact))))))) (check-sufficient-funds balance symbol value)
(check-sufficient-gas balance symbol value))))))

View File

@ -73,16 +73,16 @@
(i18n/label sign-label) (i18n/label sign-label)
[vector-icons/icon :icons/forward {:color :white}]]])) [vector-icons/icon :icons/forward {:color :white}]]]))
(defn- sign-enabled? [amount-error to amount] (defn- sign-enabled? [amount-error to amount modal?]
(and (and
(nil? amount-error) (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)))) (not (nil? amount))))
;; "Sign Later" and "Sign Transaction >" buttons ;; "Sign Later" and "Sign Transaction >" buttons
(defn- sign-button [amount-error to amount sufficient-funds? modal?] (defn- sign-button [amount-error to amount sufficient-funds? sufficient-gas? modal?]
(let [sign-enabled? (sign-enabled? amount-error to amount) (let [sign-enabled? (sign-enabled? amount-error to amount modal?)
immediate-sign-enabled? (or modal? (and sign-enabled? sufficient-funds?))] immediate-sign-enabled? (and sign-enabled? sufficient-funds? sufficient-gas?)]
[bottom-buttons/bottom-buttons [bottom-buttons/bottom-buttons
styles/sign-buttons styles/sign-buttons
[react/view] [react/view]
@ -187,7 +187,7 @@
(:invalid? gas-price-edit))} (:invalid? gas-price-edit))}
(i18n/label :t/done)]]]]))) (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 [react/view
[wallet.components/cartouche {:on-press #(do (re-frame/dispatch [:wallet.send/clear-gas]) [wallet.components/cartouche {:on-press #(do (re-frame/dispatch [:wallet.send/clear-gas])
(re-frame/dispatch [:navigate-to-modal :wallet-transaction-fee]))} (re-frame/dispatch [:navigate-to-modal :wallet-transaction-fee]))}
@ -212,11 +212,11 @@
:key :wallet-advanced}] :key :wallet-advanced}]
[vector-icons/icon (if advanced? :icons/up :icons/down) {:color :white}]]]] [vector-icons/icon (if advanced? :icons/up :icons/down) {:color :white}]]]]
(when advanced? (when advanced?
[advanced-cartouche transaction modal?])]) [advanced-cartouche transaction])])
(defn- send-transaction-panel [{:keys [modal? transaction scroll advanced? network]}] (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? (let [{:keys [amount amount-text amount-error asset-error signing? to to-name sufficient-funds? sufficient-gas?
from-chat? symbol]} transaction in-progress? from-chat? symbol]} transaction
{:keys [decimals] :as token} (tokens/asset-for (ethereum/network->chain-keyword network) symbol) {:keys [decimals] :as token} (tokens/asset-for (ethereum/network->chain-keyword network) symbol)
timeout (atom nil)] timeout (atom nil)]
[wallet.components/simple-screen {:avoid-keyboard? (not modal?) [wallet.components/simple-screen {:avoid-keyboard? (not modal?)
@ -239,7 +239,8 @@
:symbol symbol}] :symbol symbol}]
[components/amount-selector {:disabled? (or from-chat? modal?) [components/amount-selector {:disabled? (or from-chat? modal?)
:error (or amount-error :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 amount
:amount-text amount-text :amount-text amount-text
:input-options {:max-length 21 :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/cancel-signing-modal] [:wallet/discard-transaction]))
#(re-frame/dispatch (if modal? [:wallet/sign-transaction-modal] [:wallet/sign-transaction])) #(re-frame/dispatch (if modal? [:wallet/sign-transaction-modal] [:wallet/sign-transaction]))
:t/transactions-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? (when signing?
[sign-panel :t/signing-phrase-description in-progress?]) [sign-panel :t/signing-phrase-description in-progress?])
(when in-progress? [react/view styles/processing-view])]])) (when in-progress? [react/view styles/processing-view])]]))