From 0a94d10559d0c1316466c30ddd43e67eae467b66 Mon Sep 17 00:00:00 2001 From: Julien Eluard Date: Wed, 11 Oct 2017 16:52:32 +0200 Subject: [PATCH] Fixed amount validation regression --- .../ui/screens/wallet/send/views.cljs | 6 ++--- src/status_im/utils/money.cljs | 25 +++++++++++-------- test/cljs/status_im/test/utils/money.cljs | 21 +++++++--------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/status_im/ui/screens/wallet/send/views.cljs b/src/status_im/ui/screens/wallet/send/views.cljs index 0780ae7398..7ac6353a92 100644 --- a/src/status_im/ui/screens/wallet/send/views.cljs +++ b/src/status_im/ui/screens/wallet/send/views.cljs @@ -91,8 +91,8 @@ [components/button-text (i18n/label :t/transactions-sign-transaction)] [vector-icons/icon :icons/forward {:color :white :container-style wallet.styles/forward-icon-container}]]]])) -(defn- sufficient-funds? [amount-in-eth balance] - (<= (money/str->float amount-in-eth) (money/wei->ether balance))) +(defn sufficient-funds? [amount-in-eth balance] + (.greaterThanOrEqualTo balance (money/bignumber (money/to-wei amount-in-eth)))) (defn request-camera-permissions [] (when platform/android? @@ -110,7 +110,7 @@ to-address [:get-in [:wallet/send-transaction :to-address]] to-name [:get-in [:wallet/send-transaction :to-name]] in-progress? [:get-in [:wallet/send-transaction :in-progress?]]] - (let [sufficient-funds? (sufficient-funds? amount balance)] + (let [sufficient-funds? (or (nil? amount) (sufficient-funds? amount balance))] [react/keyboard-avoiding-view wallet.styles/wallet-modal-container [react/view components.styles/flex [status-bar/status-bar {:type :wallet}] diff --git a/src/status_im/utils/money.cljs b/src/status_im/utils/money.cljs index bce94c04da..255e7efbbb 100644 --- a/src/status_im/utils/money.cljs +++ b/src/status_im/utils/money.cljs @@ -28,22 +28,27 @@ (string/replace (string/trim str) #"," "."))) (defn bignumber [n] - (dependencies/Web3.prototype.toBigNumber (str n))) - -(defn str->float [str] (when str - (.toNumber (bignumber (normalize str))))) + (try + (dependencies/Web3.prototype.toBigNumber (str n)) + (catch :default err nil)))) (defn valid? [str] - (try (> (str->float str) 0) - (catch :default err false))) - + (when str + (when-let [bn (bignumber (normalize str))] + (.greaterThanOrEqualTo bn 0)))) (defn to-wei [str] - (dependencies/Web3.prototype.toWei str "ether")) + (when str + (try + (dependencies/Web3.prototype.toWei (normalize str) "ether") + (catch :default err nil)))) -(defn to-decimal [value] - (dependencies/Web3.prototype.toDecimal value)) +(defn to-decimal [str] + (when str + (try + (dependencies/Web3.prototype.toDecimal (normalize str)) + (catch :default err nil)))) (def eth-units {:wei (bignumber "1") diff --git a/test/cljs/status_im/test/utils/money.cljs b/test/cljs/status_im/test/utils/money.cljs index e6c15c3f46..fcc299235d 100644 --- a/test/cljs/status_im/test/utils/money.cljs +++ b/test/cljs/status_im/test/utils/money.cljs @@ -11,22 +11,19 @@ "0.111122223333441239")))) (deftest valid? - (is (false? (money/valid? nil))) - (is (false? (money/valid? "a"))) - (is (false? (money/valid? "-1"))) - (is (false? (money/valid? "1a"))) + (is (not (true? (money/valid? nil)))) + (is (not (true? (money/valid? "a")))) + (is (not (true? (money/valid? "-1")))) + (is (not (true? (money/valid? "1a")))) + (is (not (true? (money/valid? "0,,")))) (is (true? (money/valid? "1"))) (is (true? (money/valid? "1.1"))) - (is (true? (money/valid? "1,1")))) + (is (true? (money/valid? "1,1"))) + (is (true? (money/valid? "0.00000000000000000000001"))) + (is (true? (money/valid? "0.0000000000000000000000000001")))) (deftest normalize (is (= nil (money/normalize nil))) (is (= "1" (money/normalize " 1 "))) (is (= "1.1" (money/normalize "1.1"))) - (is (= "1.1" (money/normalize "1,1")))) - -(deftest str->float - (is (= nil (money/str->float nil))) - (is (= 1 (money/str->float " 1 "))) - (is (= 1.1 (money/str->float "1.1"))) - (is (= 1.1 (money/str->float "1,1")))) \ No newline at end of file + (is (= "1.1" (money/normalize "1,1")))) \ No newline at end of file