fix(wallet): "Not enough assets" case in send screen (#21425)

* Make `->bignumbers` more general

* Fix case when there are not enough assets to pay gas fees:
  - Fix the UI to match Figma
  - Fix logic to handle the case
  - Perform refactoring to surrounding code
This commit is contained in:
Ulises Manuel 2024-10-16 13:45:21 -06:00 committed by GitHub
parent d58553f67a
commit 711fd19967
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 312 additions and 280 deletions

View File

@ -28,8 +28,21 @@
{:fx [[:dispatch [:wallet/stop-get-suggested-routes]] {:fx [[:dispatch [:wallet/stop-get-suggested-routes]]
[:dispatch [:wallet/clean-suggested-routes]]]})) [:dispatch [:wallet/clean-suggested-routes]]]}))
(rf/reg-event-fx :wallet/suggested-routes-success (defn- add-not-enough-assets-data
(fn [{:keys [db]} [suggested-routes-data]] [send-data chosen-route]
(-> send-data
(assoc :route chosen-route
:loading-suggested-routes? false
:suggested-routes {:best []}
:enough-assets? false)
(update :sender-network-values send-utils/reset-loading-network-amounts-to-zero)
(update :receiver-network-values send-utils/reset-loading-network-amounts-to-zero)))
(rf/reg-event-fx
:wallet/suggested-routes-success
(fn [{:keys [db]} [suggested-routes-data enough-assets?]]
(if-not enough-assets?
{:db (update-in db [:wallet :ui :send] add-not-enough-assets-data (:best suggested-routes-data))}
(let [chosen-route (:best suggested-routes-data) (let [chosen-route (:best suggested-routes-data)
{:keys [token collectible token-display-name {:keys [token collectible token-display-name
receiver-networks receiver-networks
@ -98,25 +111,25 @@
:sender-network-values sender-network-values :sender-network-values sender-network-values
:receiver-network-values receiver-network-values :receiver-network-values receiver-network-values
:network-links network-links :network-links network-links
:loading-suggested-routes? false)}))) :loading-suggested-routes? false
:enough-assets? true)}))))
(rf/reg-event-fx :wallet/suggested-routes-error (rf/reg-event-fx
:wallet/suggested-routes-error
(fn [{:keys [db]} [error-message]] (fn [{:keys [db]} [error-message]]
(let [cleaned-sender-network-values (-> (get-in db [:wallet :ui :send :sender-network-values])
(send-utils/reset-loading-network-amounts-to-zero))
cleaned-receiver-network-values (-> (get-in db [:wallet :ui :send :receiver-network-values])
(send-utils/reset-loading-network-amounts-to-zero))]
{:db (-> db {:db (-> db
(update-in [:wallet :ui :send] dissoc :route) (update-in [:wallet :ui :send] dissoc :route)
(assoc-in [:wallet :ui :send :sender-network-values] cleaned-sender-network-values) (update-in [:wallet :ui :send :sender-network-values]
(assoc-in [:wallet :ui :send :receiver-network-values] cleaned-receiver-network-values) send-utils/reset-loading-network-amounts-to-zero)
(update-in [:wallet :ui :send :receiver-network-values]
send-utils/reset-loading-network-amounts-to-zero)
(assoc-in [:wallet :ui :send :loading-suggested-routes?] false) (assoc-in [:wallet :ui :send :loading-suggested-routes?] false)
(assoc-in [:wallet :ui :send :suggested-routes] {:best []})) (assoc-in [:wallet :ui :send :suggested-routes] {:best []}))
:fx [[:dispatch :fx [[:dispatch
[:toasts/upsert [:toasts/upsert
{:id :send-transaction-error {:id :send-transaction-error
:type :negative :type :negative
:text error-message}]]]}))) :text error-message}]]]}))
(rf/reg-event-fx :wallet/clean-send-address (rf/reg-event-fx :wallet/clean-send-address
(fn [{:keys [db]}] (fn [{:keys [db]}]
@ -378,8 +391,7 @@
from-locked-amounts bridge-to-chain-id] from-locked-amounts bridge-to-chain-id]
:or {token updated-token}} (get-in db [:wallet :ui :send]) :or {token updated-token}} (get-in db [:wallet :ui :send])
test-networks-enabled? (get-in db [:profile/profile :test-networks-enabled?]) test-networks-enabled? (get-in db [:profile/profile :test-networks-enabled?])
networks ((if test-networks-enabled? :test :prod) networks (get-in db [:wallet :networks (if test-networks-enabled? :test :prod)])
(get-in db [:wallet :networks]))
network-chain-ids (map :chain-id networks) network-chain-ids (map :chain-id networks)
token-decimal (when token (:decimals token)) token-decimal (when token (:decimals token))
token-id (utils/format-token-id token collectible) token-id (utils/format-token-id token collectible)
@ -509,40 +521,43 @@
[routes] [routes]
(map data-store/new->old-route-path routes)) (map data-store/new->old-route-path routes))
(def ^:private best-routes-fix
(comp ->old-route-paths
remove-invalid-bonder-fees-routes
remove-multichain-routes))
(def ^:private candidates-fix
(comp ->old-route-paths remove-invalid-bonder-fees-routes))
(defn- fix-routes
[data]
(-> data
(data-store/rpc->suggested-routes)
(update :best best-routes-fix)
(update :candidates candidates-fix)))
(rf/reg-event-fx (rf/reg-event-fx
:wallet/handle-suggested-routes :wallet/handle-suggested-routes
(fn [{:keys [db]} [data]] (fn [{:keys [db]} [data]]
(let [swap? (get-in db [:wallet :ui :swap]) (let [{send :send swap? :swap} (-> db :wallet :ui)
{:keys [code details] :as error-response} (-> data :ErrorResponse) skip-processing-routes? (:skip-processing-suggested-routes? send)]
skip-processing-suggested-routes? (get-in db (when (or swap? (not skip-processing-routes?))
[:wallet :ui :send (let [{error-code :code
:skip-processing-suggested-routes?]) :as error} (:ErrorResponse data)
process-response? (and (not swap?) enough-assets? (not (and (:Best data) (= error-code "WR-002")))
(not skip-processing-suggested-routes?))] failure? (and error enough-assets? (not swap?))
(if (and (not swap?) error-response) error-message (if (zero? error-code) "An error occurred" (:details error))]
(let [error-message (if (= code "0") "An error occurred" details)] (when failure?
(log/error "failed to get suggested routes (async)" (log/error "failed to get suggested routes (async)"
{:event :wallet/handle-suggested-routes {:event :wallet/handle-suggested-routes
:error error-message}) :error error-message}))
{:fx [(cond {:fx [[:dispatch
swap? (cond
[:dispatch [:wallet/swap-proposal-error error-message]] (and failure? swap?) [:wallet/swap-proposal-error error-message]
process-response? failure? [:wallet/suggested-routes-error error-message]
[:dispatch [:wallet/suggested-routes-error error-message]])]}) swap? [:wallet/swap-proposal-success (fix-routes data)]
(let [best-routes-fix (comp ->old-route-paths :else [:wallet/suggested-routes-success (fix-routes data)
remove-invalid-bonder-fees-routes enough-assets?])]]})))))
remove-multichain-routes)
candidates-fix (comp ->old-route-paths
remove-invalid-bonder-fees-routes)
routes (-> data
(data-store/rpc->suggested-routes)
(update :best best-routes-fix)
(update :candidates candidates-fix))]
{:fx [(cond
swap?
[:dispatch [:wallet/swap-proposal-success routes]]
process-response?
[:dispatch [:wallet/suggested-routes-success routes]])]})))))
(rf/reg-event-fx :wallet/add-authorized-transaction (rf/reg-event-fx :wallet/add-authorized-transaction
(fn [{:keys [db]} [transaction]] (fn [{:keys [db]} [transaction]]

View File

@ -41,6 +41,8 @@
:mixedcase-address "0x7bcDfc75c431" :mixedcase-address "0x7bcDfc75c431"
:public-key "0x04371e2d9d66b82f056bc128064" :public-key "0x04371e2d9d66b82f056bc128064"
:removed false} :removed false}
:wallet/current-viewing-account-color :purple
:wallet/wallet-send-enough-assets? true
:wallet/wallet-send-token {:symbol :eth :wallet/wallet-send-token {:symbol :eth
:networks [{:source 879 :networks [{:source 879
:short-name "eth" :short-name "eth"

View File

@ -140,6 +140,26 @@
:else (rf/dispatch [:wallet/stop-and-clean-suggested-routes]))) :else (rf/dispatch [:wallet/stop-and-clean-suggested-routes])))
(defn- get-fee-formatted
[route]
(when-let [native-currency-symbol (-> route first :from :native-currency-symbol)]
(rf/sub [:wallet/wallet-send-fee-fiat-formatted native-currency-symbol])))
(defn- insufficient-asset-amount?
[{:keys [token-symbol owned-eth-token input-state no-routes-found? limit-exceeded?
sender-network-values enough-assets?]}]
(let [eth-selected? (= token-symbol (string/upper-case constants/mainnet-short-name))
zero-owned-eth? (money/equal-to (:total-balance owned-eth-token) 0)
input-at-max-owned-amount? (money/equal-to
(controlled-input/value-bn input-state)
(controlled-input/upper-limit-bn input-state))
exceeded-input? (if eth-selected?
input-at-max-owned-amount?
zero-owned-eth?)]
(and (or no-routes-found? limit-exceeded?)
(seq sender-network-values)
(or exceeded-input? (not enough-assets?)))))
(defn view (defn view
;; crypto-decimals, limit-crypto and initial-crypto-currency? args are needed ;; crypto-decimals, limit-crypto and initial-crypto-currency? args are needed
;; for component tests only ;; for component tests only
@ -157,7 +177,8 @@
(let [view-id (rf/sub [:view-id]) (let [view-id (rf/sub [:view-id])
active-screen? (= view-id current-screen-id) active-screen? (= view-id current-screen-id)
bottom (safe-area/get-bottom) bottom (safe-area/get-bottom)
[crypto-currency? set-crypto-currency] (rn/use-state initial-crypto-currency?) [crypto-currency?
set-crypto-currency] (rn/use-state initial-crypto-currency?)
handle-on-confirm (fn [amount] handle-on-confirm (fn [amount]
(rf/dispatch [:wallet/set-token-amount-to-send (rf/dispatch [:wallet/set-token-amount-to-send
{:amount amount {:amount amount
@ -185,8 +206,7 @@
[input-state set-input-state] (rn/use-state controlled-input/init-state) [input-state set-input-state] (rn/use-state controlled-input/init-state)
clear-input! #(set-input-state controlled-input/delete-all) clear-input! #(set-input-state controlled-input/delete-all)
currency-symbol (rf/sub [:profile/currency-symbol]) currency-symbol (rf/sub [:profile/currency-symbol])
loading-routes? (rf/sub loading-routes? (rf/sub [:wallet/wallet-send-loading-suggested-routes?])
[:wallet/wallet-send-loading-suggested-routes?])
route (rf/sub [:wallet/wallet-send-route]) route (rf/sub [:wallet/wallet-send-route])
on-confirm (or default-on-confirm handle-on-confirm) on-confirm (or default-on-confirm handle-on-confirm)
crypto-decimals (or token-decimals default-crypto-decimals) crypto-decimals (or token-decimals default-crypto-decimals)
@ -199,9 +219,6 @@
input-value (controlled-input/input-value input-state) input-value (controlled-input/input-value input-state)
valid-input? (not (or (controlled-input/empty-value? input-state) valid-input? (not (or (controlled-input/empty-value? input-state)
(controlled-input/input-error input-state))) (controlled-input/input-error input-state)))
confirm-disabled? (or (nil? route)
(empty? route)
(not valid-input?))
amount-in-crypto (if crypto-currency? amount-in-crypto (if crypto-currency?
input-value input-value
(number/remove-trailing-zeroes (number/remove-trailing-zeroes
@ -213,24 +230,15 @@
(min token-decimals 6))) (min token-decimals 6)))
" " " "
token-symbol) token-symbol)
first-route (first route)
native-currency-symbol (when-not confirm-disabled?
(get-in first-route
[:from :native-currency-symbol]))
fee-formatted (when native-currency-symbol
(rf/sub [:wallet/wallet-send-fee-fiat-formatted
native-currency-symbol]))
show-select-asset-sheet #(rf/dispatch show-select-asset-sheet #(rf/dispatch
[:show-bottom-sheet [:show-bottom-sheet
{:content (fn [] {:content (fn []
[select-asset-bottom-sheet [select-asset-bottom-sheet
clear-input!])}]) clear-input!])}])
sender-network-values (rf/sub sender-network-values (rf/sub [:wallet/wallet-send-sender-network-values])
[:wallet/wallet-send-sender-network-values]) receiver-network-values (rf/sub [:wallet/wallet-send-receiver-network-values])
receiver-network-values (rf/sub
[:wallet/wallet-send-receiver-network-values])
tx-type (rf/sub [:wallet/wallet-send-tx-type]) tx-type (rf/sub [:wallet/wallet-send-tx-type])
token-not-supported-in-receiver-networks? (and (not= tx-type :tx/bridge) unsupported-token-in-receiver? (and (not= tx-type :tx/bridge)
(->> receiver-network-values (->> receiver-network-values
(remove #(= (:type %) :add)) (remove #(= (:type %) :add))
(every? #(= (:type %) :not-available)))) (every? #(= (:type %) :not-available))))
@ -238,44 +246,42 @@
routes (when suggested-routes routes (when suggested-routes
(or (:best suggested-routes) [])) (or (:best suggested-routes) []))
no-routes-found? (and no-routes-found? (and
(every-network-value-is-zero? (every-network-value-is-zero? sender-network-values)
sender-network-values) (some? routes)
(not (nil? routes))
(not loading-routes?) (not loading-routes?)
(not token-not-supported-in-receiver-networks?)) (not unsupported-token-in-receiver?))
receiver-networks (rf/sub [:wallet/wallet-send-receiver-networks]) receiver-networks (rf/sub [:wallet/wallet-send-receiver-networks])
receiver-preferred-networks (rf/sub receiver-preferred-networks (rf/sub [:wallet/wallet-send-receiver-preferred-networks])
[:wallet/wallet-send-receiver-preferred-networks]) receiver-preferred-network? (set receiver-preferred-networks)
receiver-preferred-networks-set (set receiver-preferred-networks) sending-to-unpreferred-networks? (some (comp not receiver-preferred-network?)
sending-to-unpreferred-networks? (not (every? (fn [receiver-selected-network] receiver-networks)
(contains?
receiver-preferred-networks-set
receiver-selected-network))
receiver-networks))
input-error (controlled-input/input-error input-state) input-error (controlled-input/input-error input-state)
limit-exceeded? (controlled-input/upper-limit-exceeded? input-state) limit-exceeded? (controlled-input/upper-limit-exceeded? input-state)
should-try-again? (and (not limit-exceeded?) no-routes-found?)
current-address (rf/sub [:wallet/current-viewing-account-address]) current-address (rf/sub [:wallet/current-viewing-account-address])
current-color (rf/sub [:wallet/current-viewing-account-color])
enough-assets? (rf/sub [:wallet/wallet-send-enough-assets?])
owned-eth-token (rf/sub [:wallet/token-by-symbol owned-eth-token (rf/sub [:wallet/token-by-symbol
(string/upper-case (string/upper-case constants/mainnet-short-name)
constants/mainnet-short-name)
enabled-from-chain-ids]) enabled-from-chain-ids])
not-enough-asset? (and not-enough-asset? (insufficient-asset-amount?
(or no-routes-found? limit-exceeded?) {:enough-assets? enough-assets?
(not-empty sender-network-values) :token-symbol token-symbol
(if (= token-symbol :owned-eth-token owned-eth-token
(string/upper-case :input-state input-state
constants/mainnet-short-name)) :no-routes-found? no-routes-found?
(money/equal-to :limit-exceeded? limit-exceeded?
(controlled-input/value-bn input-state) :sender-network-values sender-network-values})
(controlled-input/upper-limit-bn input-state)) should-try-again? (and (not limit-exceeded?)
(money/equal-to (:total-balance no-routes-found?
owned-eth-token) (not not-enough-asset?))
0))) show-no-routes? (and (or no-routes-found? limit-exceeded?)
show-no-routes? (and
(or no-routes-found? limit-exceeded?)
(not-empty sender-network-values) (not-empty sender-network-values)
(not not-enough-asset?)) (not not-enough-asset?))
confirm-disabled? (or (nil? route)
(empty? route)
(not valid-input?))
fee-formatted (when (or (not confirm-disabled?) not-enough-asset?)
(get-fee-formatted route))
request-fetch-routes (fn [bounce-duration-ms] request-fetch-routes (fn [bounce-duration-ms]
(fetch-routes (fetch-routes
{:amount amount-in-crypto {:amount amount-in-crypto
@ -365,21 +371,23 @@
{:token token-by-symbol {:token token-by-symbol
:send-amount-in-crypto amount-in-crypto :send-amount-in-crypto amount-in-crypto
:valid-input? valid-input? :valid-input? valid-input?
:token-not-supported-in-receiver-networks? token-not-supported-in-receiver-networks? :token-not-supported-in-receiver-networks? unsupported-token-in-receiver?
:current-screen-id current-screen-id :current-screen-id current-screen-id
:request-fetch-routes request-fetch-routes}] :request-fetch-routes request-fetch-routes}]
(when (and (not loading-routes?) (when (and (not loading-routes?)
sender-network-values sender-network-values
token-not-supported-in-receiver-networks?) unsupported-token-in-receiver?)
[token-not-available token-symbol receiver-networks token-networks]) [token-not-available token-symbol receiver-networks token-networks])
(when (and (not no-routes-found?) (or loading-routes? route)) (when not-enough-asset?
[not-enough-asset])
(when (or (and (not no-routes-found?) (or loading-routes? route))
not-enough-asset?)
[estimated-fees [estimated-fees
{:loading-routes? loading-routes? {:loading-routes? loading-routes?
:fees fee-formatted :fees fee-formatted
:amount amount-text}]) :amount amount-text}])
(cond (when show-no-routes?
show-no-routes? [no-routes-found] [no-routes-found])
not-enough-asset? [not-enough-asset])
[quo/bottom-actions [quo/bottom-actions
{:actions :one-action {:actions :one-action
:button-one-label (if should-try-again? :button-one-label (if should-try-again?
@ -387,8 +395,10 @@
button-one-label) button-one-label)
:button-one-props (merge (when-not should-try-again? :button-one-props (merge (when-not should-try-again?
button-one-props) button-one-props)
{:disabled? (or loading-routes? {:disabled? (or not-enough-asset?
(and (not should-try-again?) confirm-disabled?)) loading-routes?
(and (not should-try-again?)
confirm-disabled?))
:on-press (cond :on-press (cond
should-try-again? should-try-again?
#(rf/dispatch [:wallet/start-get-suggested-routes #(rf/dispatch [:wallet/start-get-suggested-routes
@ -397,7 +407,8 @@
sending-to-unpreferred-networks? sending-to-unpreferred-networks?
#(show-unpreferred-networks-alert on-confirm) #(show-unpreferred-networks-alert on-confirm)
:else :else
#(on-confirm amount-in-crypto))} #(on-confirm amount-in-crypto))
:customization-color current-color}
(when should-try-again? (when should-try-again?
{:type :grey}))}] {:type :grey}))}]
[quo/numbered-keyboard [quo/numbered-keyboard

View File

@ -126,6 +126,11 @@
:<- [:wallet/wallet-send] :<- [:wallet/wallet-send]
:-> :route) :-> :route)
(rf/reg-sub
:wallet/wallet-send-enough-assets?
:<- [:wallet/wallet-send]
:-> :enough-assets?)
(rf/reg-sub (rf/reg-sub
:wallet/wallet-send-token :wallet/wallet-send-token
:<- [:wallet/wallet-send] :<- [:wallet/wallet-send]
@ -133,10 +138,10 @@
:<- [:wallet/wallet-send-disabled-from-chain-ids] :<- [:wallet/wallet-send-disabled-from-chain-ids]
(fn [[wallet-send networks disabled-from-chain-ids]] (fn [[wallet-send networks disabled-from-chain-ids]]
(let [token (:token wallet-send) (let [token (:token wallet-send)
disabled-from-chain-ids? (set disabled-from-chain-ids)
enabled-from-chain-ids (->> networks enabled-from-chain-ids (->> networks
(filter #(not (contains? (set disabled-from-chain-ids)
(:chain-id %))))
(map :chain-id) (map :chain-id)
(remove disabled-from-chain-ids?)
set)] set)]
(some-> token (some-> token
(assoc :networks (network-utils/network-list token networks) (assoc :networks (network-utils/network-list token networks)

View File

@ -47,11 +47,10 @@
(if (bignumber? n) n (bignumber n))) (if (bignumber? n) n (bignumber n)))
(defn ->bignumbers (defn ->bignumbers
[n1 n2] [& numbers]
(when-let [bn1 (->bignumber n1)] (let [transformed-numbers (map ->bignumber numbers)]
(when-let [bn2 (->bignumber n2)] (when (every? bignumber? transformed-numbers)
(when (and (bignumber? bn1) (bignumber? bn2)) transformed-numbers)))
[bn1 bn2]))))
(defn greater-than-or-equals (defn greater-than-or-equals
[^js n1 ^js n2] [^js n1 ^js n2]