Fixed amount validation regression

This commit is contained in:
Julien Eluard 2017-10-11 16:52:32 +02:00 committed by Roman Volosovskyi
parent 1df074c100
commit 0a94d10559
3 changed files with 27 additions and 25 deletions

View File

@ -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}]

View File

@ -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")

View File

@ -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"))))
(is (= "1.1" (money/normalize "1,1"))))