[ISSUE #2449] Make sure DApps transaction amount is never nil

This commit is contained in:
Julien Eluard 2017-11-29 10:01:09 +01:00 committed by Julien Eluard
parent 7f2085fb14
commit cfa14ff6b3
3 changed files with 6 additions and 9 deletions

View File

@ -22,23 +22,20 @@
;; TODO(oskarth): spec for balance as BigNumber ;; TODO(oskarth): spec for balance as BigNumber
;; TODO(oskarth): Spec for prices as as: {:from ETH, :to USD, :price 290.11, :last-day 304.17} ;; TODO(oskarth): Spec for prices as as: {:from ETH, :to USD, :price 290.11, :last-day 304.17}
(defn- empty-amount? [amount]
(or (nil? amount) (= amount "") (= amount "0") (re-matches #"0[,.]0*$" amount)))
(defn- too-precise-amount? [amount] (defn- too-precise-amount? [amount]
(let [amount-splited (string/split amount #"[.]")] (let [amount-splited (string/split amount #"[.]")]
(and (= (count amount-splited) 2) (> (count (last amount-splited)) 18)))) (and (= (count amount-splited) 2) (> (count (last amount-splited)) 18))))
(defn parse-amount [amount] (defn parse-amount [amount]
(when-not (empty-amount? amount) (when-not (empty? amount)
(let [normalized-amount (money/normalize amount) (let [normalized-amount (money/normalize amount)
value (money/bignumber normalized-amount)] value (money/bignumber normalized-amount)]
(cond (cond
(not (money/valid? value)) (not (money/valid? value))
{:error (i18n/label :t/validation-amount-invalid-number)} {:error (i18n/label :t/validation-amount-invalid-number) :value value}
(too-precise-amount? normalized-amount) (too-precise-amount? normalized-amount)
{:error (i18n/label :t/validation-amount-is-too-precise)} {:error (i18n/label :t/validation-amount-is-too-precise) :value value}
:else :else
{:value value})))) {:value value}))))

View File

@ -81,7 +81,7 @@
transaction {:id id transaction {:id id
:from from :from from
:to to :to to
:value (money/bignumber value) :value (money/bignumber (or value 0))
:data data :data data
:gas (money/to-decimal gas) :gas (money/to-decimal gas)
:gas-price (money/to-decimal gasPrice) :gas-price (money/to-decimal gasPrice)
@ -93,7 +93,7 @@
:from-chat? sending-from-chat?}] :from-chat? sending-from-chat?}]
(if sending-from-chat? (if sending-from-chat?
;;SENDING FROM CHAT ;;SENDING FROM CHAT
{:db (assoc-in new-db [:wallet :send-transaction] sending-db) ; we need to completly reset sending state here {:db (assoc-in new-db [:wallet :send-transaction] sending-db) ; we need to completely reset sending state here
:dispatch [:navigate-to-modal :wallet-send-transaction-modal]} :dispatch [:navigate-to-modal :wallet-send-transaction-modal]}
;;SEND SCREEN WAITING SIGNAL ;;SEND SCREEN WAITING SIGNAL
(let [{:keys [later? password]} (get-in db [:wallet :send-transaction]) (let [{:keys [later? password]} (get-in db [:wallet :send-transaction])

View File

@ -126,7 +126,7 @@
(when-not sufficient-funds? (i18n/label :t/wallet-insufficient-funds))) (when-not sufficient-funds? (i18n/label :t/wallet-insufficient-funds)))
:input-options {:auto-focus true :input-options {:auto-focus true
:on-focus (fn [] (when @scroll (js/setTimeout #(.scrollToEnd @scroll) 100))) :on-focus (fn [] (when @scroll (js/setTimeout #(.scrollToEnd @scroll) 100)))
:default-value (str (money/wei->ether amount)) :default-value (str (money/to-fixed (money/wei->ether amount)))
:on-change-text #(re-frame/dispatch [:wallet.send/set-and-validate-amount %])}}] :on-change-text #(re-frame/dispatch [:wallet.send/set-and-validate-amount %])}}]
[react/view wallet.styles/choose-currency-container [react/view wallet.styles/choose-currency-container
[components/choose-currency wallet.styles/choose-currency]]]]] [components/choose-currency wallet.styles/choose-currency]]]]]