From ca81bfebcde9437a89d6e5fddb21969e372ba234 Mon Sep 17 00:00:00 2001 From: Brian Sztamfater Date: Mon, 27 May 2024 12:15:50 -0300 Subject: [PATCH] fix: show network card with zero value on the sender side if the router does not return a route for that network (#20152) Signed-off-by: Brian Sztamfater --- .../contexts/wallet/send/events.cljs | 78 ++++++++++++------- .../wallet/send/input_amount/view.cljs | 8 +- .../contexts/wallet/send/routes/view.cljs | 12 ++- src/status_im/contexts/wallet/send/utils.cljs | 19 ++++- 4 files changed, 77 insertions(+), 40 deletions(-) diff --git a/src/status_im/contexts/wallet/send/events.cljs b/src/status_im/contexts/wallet/send/events.cljs index 960544fcc5..a0ace8e571 100644 --- a/src/status_im/contexts/wallet/send/events.cljs +++ b/src/status_im/contexts/wallet/send/events.cljs @@ -56,14 +56,20 @@ native-token? :receiver? true}) to-network-values-for-ui (send-utils/network-values-for-ui to-network-amounts-by-chain) + sender-possible-chain-ids (mapv :chain-id sender-network-values) sender-network-values (if routes-available? (send-utils/network-amounts - {:network-values from-network-values-for-ui + {:network-values + (if (= tx-type :tx/bridge) + from-network-values-for-ui + (send-utils/add-zero-values-to-network-values + from-network-values-for-ui + sender-possible-chain-ids)) :disabled-chain-ids disabled-from-chain-ids - :receiver-networks receiver-networks + :receiver-networks receiver-networks :token-networks-ids token-networks-ids - :tx-type tx-type - :receiver? false}) + :tx-type tx-type + :receiver? false}) (send-utils/reset-loading-network-amounts-to-zero sender-network-values)) receiver-network-values (if routes-available? @@ -322,9 +328,9 @@ (when (empty? receiver-network-values) :receiver-network-values)))}))) (rf/reg-event-fx :wallet/get-suggested-routes - (fn [{:keys [db now]} [{:keys [amount]}]] + (fn [{:keys [db now]} [{:keys [amount updated-token]}]] (let [wallet-address (get-in db [:wallet :current-viewing-account-address]) - token (get-in db [:wallet :ui :send :token]) + token (or updated-token (get-in db [:wallet :ui :send :token])) transaction-type (get-in db [:wallet :ui :send :tx-type]) collectible (get-in db [:wallet :ui :send :collectible]) to-address (get-in db [:wallet :ui :send :to-address]) @@ -356,32 +362,41 @@ :tx/bridge constants/send-type-bridge constants/send-type-transfer) balances-per-chain (when token (:balances-per-chain token)) - token-available-networks-for-suggested-routes + sender-token-available-networks-for-suggested-routes (when token (send-utils/token-available-networks-for-suggested-routes {:balances-per-chain balances-per-chain :disabled-chain-ids - disabled-from-chain-ids})) + disabled-from-chain-ids + :only-with-balance? true})) + receiver-token-available-networks-for-suggested-routes + (when token + (send-utils/token-available-networks-for-suggested-routes {:balances-per-chain + balances-per-chain + :disabled-chain-ids + disabled-from-chain-ids + :only-with-balance? false})) token-networks-ids (when token (mapv #(:chain-id %) (:networks token))) - sender-network-values (when token-available-networks-for-suggested-routes + sender-network-values (when sender-token-available-networks-for-suggested-routes (send-utils/loading-network-amounts - {:valid-networks (if (= transaction-type :tx/bridge) - (filter - #(not= bridge-to-chain-id %) - token-available-networks-for-suggested-routes) - token-available-networks-for-suggested-routes) + {:valid-networks + (if (= transaction-type :tx/bridge) + (remove #(= bridge-to-chain-id %) + sender-token-available-networks-for-suggested-routes) + sender-token-available-networks-for-suggested-routes) :disabled-chain-ids disabled-from-chain-ids - :receiver-networks receiver-networks + :receiver-networks receiver-networks :token-networks-ids token-networks-ids - :tx-type transaction-type - :receiver? false})) - receiver-network-values (when token-available-networks-for-suggested-routes + :tx-type transaction-type + :receiver? false})) + receiver-network-values (when receiver-token-available-networks-for-suggested-routes (send-utils/loading-network-amounts - {:valid-networks (if (= transaction-type :tx/bridge) - (filter - #(= bridge-to-chain-id %) - token-available-networks-for-suggested-routes) - token-available-networks-for-suggested-routes) + {:valid-networks + (if (= transaction-type :tx/bridge) + (filter + #(= bridge-to-chain-id %) + receiver-token-available-networks-for-suggested-routes) + receiver-token-available-networks-for-suggested-routes) :disabled-chain-ids disabled-from-chain-ids :receiver-networks receiver-networks :token-networks-ids token-networks-ids @@ -398,13 +413,16 @@ network-preferences gas-rates from-locked-amount]] - {:db (-> db - (assoc-in [:wallet :ui :send :amount] amount) - (assoc-in [:wallet :ui :send :loading-suggested-routes?] true) - (assoc-in [:wallet :ui :send :sender-network-values] sender-network-values) - (assoc-in [:wallet :ui :send :receiver-network-values] receiver-network-values) - (assoc-in [:wallet :ui :send :suggested-routes-call-timestamp] now) - (update-in [:wallet :ui :send] dissoc :network-links)) + {:db (cond-> db + :always (assoc-in [:wallet :ui :send :amount] amount) + :always (assoc-in [:wallet :ui :send :loading-suggested-routes?] true) + :always (assoc-in [:wallet :ui :send :sender-network-values] + sender-network-values) + :always (assoc-in [:wallet :ui :send :receiver-network-values] + receiver-network-values) + :always (assoc-in [:wallet :ui :send :suggested-routes-call-timestamp] now) + :always (update-in [:wallet :ui :send] dissoc :network-links) + token (assoc-in [:wallet :ui :send :token] token)) :json-rpc/call [{:method "wallet_getSuggestedRoutes" :params request-params :on-success (fn [suggested-routes] diff --git a/src/status_im/contexts/wallet/send/input_amount/view.cljs b/src/status_im/contexts/wallet/send/input_amount/view.cljs index 57769d2e9d..faabf6b4f0 100644 --- a/src/status_im/contexts/wallet/send/input_amount/view.cljs +++ b/src/status_im/contexts/wallet/send/input_amount/view.cljs @@ -161,7 +161,8 @@ enabled-from-chain-ids (rf/sub [:wallet/wallet-send-enabled-from-chain-ids]) {token-balance :total-balance - available-balance :available-balance} (rf/sub [:wallet/token-by-symbol + available-balance :available-balance + :as token-by-symbol} (rf/sub [:wallet/token-by-symbol (str token-symbol) enabled-from-chain-ids]) currency-symbol (rf/sub [:profile/currency-symbol]) @@ -337,7 +338,7 @@ (number/remove-trailing-zeroes new-value)))))) :on-token-press show-select-asset-sheet}] [routes/view - {:token token + {:token token-by-symbol :input-value input-amount :value amount :valid-input? valid-input? @@ -373,7 +374,8 @@ conversion-rate) 2))] (rf/dispatch [:wallet/get-suggested-routes - {:amount amount}])) + {:amount amount + :updated-token token-by-symbol}])) sending-to-unpreferred-networks? #(show-unpreferred-networks-alert on-confirm) :else diff --git a/src/status_im/contexts/wallet/send/routes/view.cljs b/src/status_im/contexts/wallet/send/routes/view.cljs index 3ed222b598..fe47fb939a 100644 --- a/src/status_im/contexts/wallet/send/routes/view.cljs +++ b/src/status_im/contexts/wallet/send/routes/view.cljs @@ -17,10 +17,12 @@ (def network-link-2x-height 111) (defn- fetch-routes - [{:keys [amount bounce-duration-ms valid-input?]}] + [{:keys [amount bounce-duration-ms token valid-input?]}] (if valid-input? (debounce/debounce-and-dispatch - [:wallet/get-suggested-routes {:amount amount}] + [:wallet/get-suggested-routes + {:amount amount + :updated-token token}] bounce-duration-ms) (rf/dispatch [:wallet/clean-suggested-routes]))) @@ -203,14 +205,16 @@ (fetch-routes {:amount value :valid-input? valid-input? - :bounce-duration-ms 2000}))) + :bounce-duration-ms 2000 + :token token}))) [input-value valid-input?]) (rn/use-effect #(when (and active-screen? (> (count token-available-networks-for-suggested-routes) 0)) (fetch-routes {:amount value :valid-input? valid-input? - :bounce-duration-ms 0})) + :bounce-duration-ms 0 + :token token})) [disabled-from-chain-ids]) [rn/scroll-view {:content-container-style style/routes-container} (when show-routes? diff --git a/src/status_im/contexts/wallet/send/utils.cljs b/src/status_im/contexts/wallet/send/utils.cljs index ae0ae17625..8d628e6a8c 100644 --- a/src/status_im/contexts/wallet/send/utils.cljs +++ b/src/status_im/contexts/wallet/send/utils.cljs @@ -67,12 +67,25 @@ {} amounts)) +(defn add-zero-values-to-network-values + [network-values all-possible-chain-ids] + (reduce + (fn [acc chain-id] + (let [route-value (get network-values chain-id)] + (assoc acc chain-id (or route-value (money/bignumber "0"))))) + {} + all-possible-chain-ids)) + (defn token-available-networks-for-suggested-routes - [{:keys [balances-per-chain disabled-chain-ids]}] + [{:keys [balances-per-chain disabled-chain-ids only-with-balance?]}] (let [disabled-set (set disabled-chain-ids)] (->> balances-per-chain - (filter (fn [[_ {:keys [chain-id]}]] - (not (contains? disabled-set chain-id)))) + (filter (fn [[_ {:keys [chain-id raw-balance]}]] + (and (not (contains? disabled-set chain-id)) + (or (not only-with-balance?) + (and only-with-balance? + (money/bignumber? raw-balance) + (money/greater-than raw-balance (money/bignumber "0"))))))) (map first)))) (def ^:private network-priority-score