From cc24ff1b8276f15dc0d6795ce18b20aabbae84e4 Mon Sep 17 00:00:00 2001 From: Igor Mandrigin Date: Tue, 22 May 2018 14:06:50 +0200 Subject: [PATCH] [#4361] Don't autocorrect incomplete and erroneous entries for transaction amounts. Signed-off-by: Igor Mandrigin --- src/status_im/ui/screens/wallet/request/db.cljs | 3 ++- .../ui/screens/wallet/request/events.cljs | 1 + .../ui/screens/wallet/request/views.cljs | 15 +++++++++++---- src/status_im/ui/screens/wallet/send/db.cljs | 3 ++- src/status_im/ui/screens/wallet/send/events.cljs | 1 + src/status_im/ui/screens/wallet/send/views.cljs | 15 +++++++++++---- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/status_im/ui/screens/wallet/request/db.cljs b/src/status_im/ui/screens/wallet/request/db.cljs index 80067e38cc..0608c7b245 100644 --- a/src/status_im/ui/screens/wallet/request/db.cljs +++ b/src/status_im/ui/screens/wallet/request/db.cljs @@ -5,7 +5,8 @@ (spec/def ::amount (spec/nilable money/valid?)) (spec/def ::amount-error (spec/nilable string?)) +(spec/def ::amount-text (spec/nilable string?)) (spec/def ::symbol (spec/nilable keyword?)) (spec/def :wallet/request-transaction (allowed-keys - :opt-un [::amount ::amount-error ::symbol])) \ No newline at end of file + :opt-un [::amount ::amount-error ::amount-text ::symbol])) diff --git a/src/status_im/ui/screens/wallet/request/events.cljs b/src/status_im/ui/screens/wallet/request/events.cljs index b03219b4c4..7f32125ba2 100644 --- a/src/status_im/ui/screens/wallet/request/events.cljs +++ b/src/status_im/ui/screens/wallet/request/events.cljs @@ -38,4 +38,5 @@ (let [{:keys [value error]} (wallet-db/parse-amount amount)] {:db (-> db (assoc-in [:wallet :request-transaction :amount] (money/ether->wei value)) + (assoc-in [:wallet :request-transaction :amount-text] amount) (assoc-in [:wallet :request-transaction :amount-error] error))}))) diff --git a/src/status_im/ui/screens/wallet/request/views.cljs b/src/status_im/ui/screens/wallet/request/views.cljs index 1969aca4a8..81b52a8c4d 100644 --- a/src/status_im/ui/screens/wallet/request/views.cljs +++ b/src/status_im/ui/screens/wallet/request/views.cljs @@ -27,7 +27,7 @@ (views/defview send-transaction-request [] ;; TODO(jeluard) both send and request flows should be merged (views/letsubs [{:keys [to to-name whisper-identity]} [:wallet.send/transaction] - {:keys [amount amount-error]} [:wallet.request/transaction] + {:keys [amount amount-error amount-text]} [:wallet.request/transaction] scroll (atom nil)] [comp/simple-screen {:avoid-keyboard? true} [comp/toolbar (i18n/label :t/new-request)] @@ -42,10 +42,17 @@ [components/asset-selector {:disabled? true :symbol :ETH}] [components/amount-selector {:error amount-error - :input-options {:default-value (str (money/to-fixed (money/wei->ether amount))) - :max-length 21 + :input-options {:max-length 21 :on-focus (fn [] (when @scroll (utils/set-timeout #(.scrollToEnd @scroll) 100))) - :on-change-text #(re-frame/dispatch [:wallet.request/set-and-validate-amount %])}}]]] + :on-change-text #(re-frame/dispatch [:wallet.request/set-and-validate-amount %]) + ;; (similarly to status-im.ui.screens.wallet.send.views `send-transaction-panel`) + ;; We only auto-correct and prettify user's input when it is valid and positive. + ;; Otherwise, user might want to fix his input and autocorrection will give more harm than good. + ;; Positive check is because we don't want to replace unfinished 0.000 with just plain 0, that is annoying and + ;; potentially dangerous on this screen (e.g. sending 7 ETH instead of 0.0007) + :default-value (if (pos? amount) + (str (money/to-fixed (money/wei->ether amount))) + amount-text)}}]]] [bottom-buttons/bottom-buttons styles/bottom-buttons nil ;; Force a phantom button to ensure consistency with other transaction screens which define 2 buttons [button/button {:disabled? (not (and to amount)) diff --git a/src/status_im/ui/screens/wallet/send/db.cljs b/src/status_im/ui/screens/wallet/send/db.cljs index 14583b206d..469a47d8e7 100644 --- a/src/status_im/ui/screens/wallet/send/db.cljs +++ b/src/status_im/ui/screens/wallet/send/db.cljs @@ -7,6 +7,7 @@ (spec/def ::to (spec/nilable string?)) (spec/def ::to-name (spec/nilable string?)) (spec/def ::amount-error (spec/nilable string?)) +(spec/def ::amount-text (spec/nilable string?)) (spec/def ::password (spec/nilable string?)) (spec/def ::wrong-password? (spec/nilable boolean?)) (spec/def ::id (spec/nilable string?)) @@ -26,7 +27,7 @@ (spec/def ::method (spec/nilable string?)) (spec/def :wallet/send-transaction (allowed-keys - :opt-un [::amount ::to ::to-name ::amount-error ::password + :opt-un [::amount ::to ::to-name ::amount-error ::amount-text ::password ::waiting-signal? ::signing? ::id ::later? ::camera-flashlight ::in-progress? ::wrong-password? ::from-chat? ::symbol ::advanced? diff --git a/src/status_im/ui/screens/wallet/send/events.cljs b/src/status_im/ui/screens/wallet/send/events.cljs index ed725f2cf6..5f94fba8dd 100644 --- a/src/status_im/ui/screens/wallet/send/events.cljs +++ b/src/status_im/ui/screens/wallet/send/events.cljs @@ -74,6 +74,7 @@ (let [{:keys [value error]} (wallet.db/parse-amount amount)] {:db (-> db (assoc-in [:wallet :send-transaction :amount] (money/ether->wei value)) + (assoc-in [:wallet :send-transaction :amount-text] amount) (assoc-in [:wallet :send-transaction :amount-error] error))}))) (handlers/register-handler-fx diff --git a/src/status_im/ui/screens/wallet/send/views.cljs b/src/status_im/ui/screens/wallet/send/views.cljs index 09032b2db8..4f0742eec6 100644 --- a/src/status_im/ui/screens/wallet/send/views.cljs +++ b/src/status_im/ui/screens/wallet/send/views.cljs @@ -199,7 +199,7 @@ (reset! timeout (utils/set-timeout #(re-frame/dispatch [:wallet.send/set-and-validate-amount amount]) 500)))) (defn- send-transaction-panel [{:keys [modal? transaction scroll advanced? symbol]}] - (let [{:keys [amount amount-error signing? to to-name sufficient-funds? in-progress? from-chat?]} transaction + (let [{:keys [amount amount-text amount-error signing? to to-name sufficient-funds? in-progress? from-chat?]} transaction timeout (atom nil)] [wallet.components/simple-screen {:avoid-keyboard? (not modal?) :status-bar-type (if modal? :modal-wallet :wallet)} @@ -221,10 +221,17 @@ [components/amount-selector {:disabled? modal? :error (or amount-error (when-not sufficient-funds? (i18n/label :t/wallet-insufficient-funds))) - :input-options {:default-value (str (money/to-fixed (money/wei->ether amount))) - :max-length 21 + :input-options {:max-length 21 :on-focus (fn [] (when (and scroll @scroll) (utils/set-timeout #(.scrollToEnd @scroll) 100))) - :on-change-text (update-amount-fn timeout)}}] + :on-change-text (update-amount-fn timeout) + ;; (similarly to status-im.ui.screens.wallet.request.views `send-transaction-request` view) + ;; We only auto-correct and prettify user's input when it is valid and positive. + ;; Otherwise, user might want to fix his input and autocorrection will give more harm than good. + ;; Positive check is because we don't want to replace unfinished 0.000 with just plain 0, that is annoying and + ;; potentially dangerous on this screen (e.g. sending 7 ETH instead of 0.0007) + :default-value (if (pos? amount) + (str (money/to-fixed (money/wei->ether amount))) + amount-text)}}] [advanced-options advanced? transaction modal? scroll]]] (if signing? [signing-buttons