fix(wallet)_: Error on sending ERC1155 collectible (#21209)

This commit:
- fixes crash while sending an ERC115 collectible
- fixes routes are fetched if we enter 0 in the input

Signed-off-by: Mohamed Javid <19339952+smohamedjavid@users.noreply.github.com>
This commit is contained in:
Mohamed Javid 2024-09-13 17:56:53 +05:30 committed by GitHub
parent 77ff5b473b
commit 5d6b46a914
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 46 deletions

View File

@ -14,7 +14,11 @@
[state] [state]
(:value state)) (:value state))
(defn numeric-value (defn value-numeric
[state]
(or (parse-double (input-value state)) 0))
(defn value-bn
[state] [state]
(money/bignumber (input-value state))) (money/bignumber (input-value state)))
@ -27,28 +31,32 @@
(assoc state :error? error?)) (assoc state :error? error?))
(defn upper-limit (defn upper-limit
[state]
(:upper-limit state))
(defn upper-limit-bn
[state] [state]
(money/bignumber (:upper-limit state))) (money/bignumber (:upper-limit state)))
(defn lower-limit (defn lower-limit
[state]
(:lower-limit state))
(defn lower-limit-bn
[state] [state]
(money/bignumber (:lower-limit state))) (money/bignumber (:lower-limit state)))
(defn upper-limit-exceeded? (defn upper-limit-exceeded?
[state] [state]
(let [num-value (numeric-value state)] (and (upper-limit state)
(and (when (money/bignumber? (value-bn state))
(upper-limit state) (money/greater-than (value-bn state) (upper-limit-bn state)))))
(when (money/bignumber? num-value)
(money/greater-than (numeric-value state) (upper-limit state))))))
(defn- lower-limit-exceeded? (defn- lower-limit-exceeded?
[state] [state]
(let [num-value (numeric-value state)] (and (lower-limit state)
(and (when (money/bignumber? (value-bn state))
(lower-limit state) (money/less-than (value-bn state) (lower-limit-bn state)))))
(when (money/bignumber? num-value)
(money/less-than (numeric-value state) (lower-limit state))))))
(defn- recheck-errorness (defn- recheck-errorness
[state] [state]
@ -62,7 +70,7 @@
(assoc :value value) (assoc :value value)
recheck-errorness)) recheck-errorness))
(defn set-numeric-value (defn set-value-numeric
[state value] [state value]
(set-input-value state (str value))) (set-input-value state (str value)))
@ -81,11 +89,11 @@
(defn increase (defn increase
[state] [state]
(set-input-value state (str (money/add (numeric-value state) 1)))) (set-input-value state (str (money/add (value-bn state) 1))))
(defn decrease (defn decrease
[state] [state]
(set-input-value state (str (money/add (numeric-value state) -1)))) (set-input-value state (str (money/add (value-bn state) -1))))
(def ^:private not-digits-or-dot-pattern (def ^:private not-digits-or-dot-pattern
#"[^0-9+\.]") #"[^0-9+\.]")
@ -122,10 +130,12 @@
state)) state))
(defn delete-last (defn delete-last
[state] ([state]
(let [value (input-value state) (delete-last state ""))
new-value (subs value 0 (dec (count value)))] ([state default-value]
(set-input-value state new-value))) (let [value (input-value state)
new-value (subs value 0 (dec (count value)))]
(set-input-value state (if (string/blank? new-value) default-value new-value)))))
(defn delete-all (defn delete-all
[state] [state]
@ -133,7 +143,7 @@
(defn empty-value? (defn empty-value?
[state] [state]
(string/blank? (:value state))) (or (string/blank? (:value state)) (<= (value-numeric state) 0)))
(defn- fiat->crypto (defn- fiat->crypto
[value conversion-rate] [value conversion-rate]

View File

@ -262,8 +262,8 @@
(string/upper-case (string/upper-case
constants/mainnet-short-name)) constants/mainnet-short-name))
(money/equal-to (money/equal-to
(controlled-input/numeric-value input-state) (controlled-input/value-bn input-state)
(controlled-input/upper-limit input-state)) (controlled-input/upper-limit-bn input-state))
(money/equal-to (:total-balance (money/equal-to (:total-balance
owned-eth-token) owned-eth-token)
0))) 0)))
@ -338,10 +338,10 @@
{:limit (if crypto-currency? {:limit (if crypto-currency?
(utils/prettify-crypto-balance (utils/prettify-crypto-balance
(or (clj->js token-symbol) "") (or (clj->js token-symbol) "")
(controlled-input/upper-limit input-state) (controlled-input/upper-limit-bn input-state)
conversion-rate) conversion-rate)
(utils/prettify-balance currency-symbol (utils/prettify-balance currency-symbol
(controlled-input/upper-limit (controlled-input/upper-limit-bn
input-state)))}) input-state)))})
:status (when (controlled-input/input-error input-state) :error)}]}] :status (when (controlled-input/input-error input-state) :error)}]}]
[routes/view [routes/view

View File

@ -11,25 +11,26 @@
(defn view (defn view
[] []
(let [on-close (rn/use-callback (let [on-close (rn/use-callback
#(rf/dispatch [:wallet/collectible-amount-navigate-back])) #(rf/dispatch [:wallet/collectible-amount-navigate-back]))
send-transaction-data (rf/sub [:wallet/wallet-send]) send-transaction-data (rf/sub [:wallet/wallet-send])
collectible (:collectible send-transaction-data) collectible (:collectible send-transaction-data)
balance (utils/collectible-balance collectible) balance (utils/collectible-balance collectible)
[value set-value] (rn/use-state (-> controlled-input/init-state [input-state set-input-state] (rn/use-state (-> controlled-input/init-state
(controlled-input/set-numeric-value 1) (controlled-input/set-value-numeric 1)
(controlled-input/set-lower-limit 1))) (controlled-input/set-lower-limit 1)))
preview-uri (get-in collectible [:preview-url :uri]) preview-uri (get-in collectible [:preview-url :uri])
incorrect-value? (controlled-input/input-error value) incorrect-value? (controlled-input/input-error input-state)
increase-value (rn/use-callback #(set-value controlled-input/increase)) increase-value (rn/use-callback #(set-input-state controlled-input/increase))
decrease-value (rn/use-callback #(set-value controlled-input/decrease)) decrease-value (rn/use-callback #(set-input-state controlled-input/decrease))
delete-character (rn/use-callback #(set-value controlled-input/delete-last)) delete-character (rn/use-callback
add-character (rn/use-callback (fn [] (set-input-state #(controlled-input/delete-last % "1"))))
(fn [c] add-character (rn/use-callback
(set-value #(controlled-input/add-character % c))))] (fn [c]
(set-input-state #(controlled-input/add-character % c))))]
(rn/use-effect (rn/use-effect
(fn [] (fn []
(set-value #(controlled-input/set-upper-limit % balance))) (set-input-state #(controlled-input/set-upper-limit % balance)))
[balance]) [balance])
[rn/view [rn/view
[account-switcher/view [account-switcher/view
@ -47,9 +48,9 @@
:status (if incorrect-value? :error :default) :status (if incorrect-value? :error :default)
:container-style style/network-tags-container}] :container-style style/network-tags-container}]
[quo/amount-input [quo/amount-input
{:max-value (controlled-input/upper-limit value) {:max-value (controlled-input/upper-limit input-state)
:min-value (controlled-input/lower-limit value) :min-value (controlled-input/lower-limit input-state)
:value (controlled-input/numeric-value value) :value (controlled-input/value-numeric input-state)
:on-inc-press increase-value :on-inc-press increase-value
:on-dec-press decrease-value :on-dec-press decrease-value
:container-style style/amount-input-container :container-style style/amount-input-container
@ -59,7 +60,7 @@
:button-one-props {:on-press #(rf/dispatch :button-one-props {:on-press #(rf/dispatch
[:wallet/set-collectible-amount-to-send [:wallet/set-collectible-amount-to-send
{:stack-id :screen/wallet.select-collectible-amount {:stack-id :screen/wallet.select-collectible-amount
:amount (controlled-input/numeric-value value)}]) :amount (controlled-input/value-numeric input-state)}])
:disabled? incorrect-value?} :disabled? incorrect-value?}
:button-one-label (i18n/label :t/confirm)}] :button-one-label (i18n/label :t/confirm)}]
[quo/numbered-keyboard [quo/numbered-keyboard

View File

@ -74,7 +74,7 @@
approval-amount-required (rf/sub [:wallet/swap-proposal-approval-amount-required]) approval-amount-required (rf/sub [:wallet/swap-proposal-approval-amount-required])
currency-symbol (rf/sub [:profile/currency-symbol]) currency-symbol (rf/sub [:profile/currency-symbol])
approval-transaction-status (rf/sub [:wallet/swap-approval-transaction-status]) approval-transaction-status (rf/sub [:wallet/swap-approval-transaction-status])
pay-input-num-value (controlled-input/numeric-value input-state) pay-input-num-value (controlled-input/value-numeric input-state)
pay-input-amount (controlled-input/input-value input-state) pay-input-amount (controlled-input/input-value input-state)
pay-token-symbol (:symbol asset-to-pay) pay-token-symbol (:symbol asset-to-pay)
pay-token-decimals (:decimals asset-to-pay) pay-token-decimals (:decimals asset-to-pay)
@ -231,7 +231,7 @@
network (rf/sub [:wallet/swap-network]) network (rf/sub [:wallet/swap-network])
pay-input-amount (controlled-input/input-value pay-input-state) pay-input-amount (controlled-input/input-value pay-input-state)
pay-token-decimals (:decimals asset-to-pay) pay-token-decimals (:decimals asset-to-pay)
pay-input-num-value (controlled-input/numeric-value pay-input-state) pay-input-num-value (controlled-input/value-numeric pay-input-state)
pay-token-balance-selected-chain (get-in asset-to-pay pay-token-balance-selected-chain (get-in asset-to-pay
[:balances-per-chain [:balances-per-chain
(:chain-id network) :balance] (:chain-id network) :balance]