From 5118a5095dbbd73d6b2dc8469dc82be62595169e Mon Sep 17 00:00:00 2001 From: Lungu Cristian Date: Tue, 22 Oct 2024 15:55:37 +0300 Subject: [PATCH] Fix networks on select asset screen (#21403) * fix: filter tokens by balance on select asset * feat: added network-list-with-positive-balance * fix: swaps error when using networks with balance * fix: hide send/bridge/swap when no token balance --- .../wallet/common/token_value/view.cljs | 12 ++++--- .../wallet/common/utils/networks.cljs | 31 +++++++++++++------ .../wallet/swap/select_asset_to_pay/view.cljs | 2 +- src/status_im/subs/wallet/wallet.cljs | 30 ++++++++++++------ 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/status_im/contexts/wallet/common/token_value/view.cljs b/src/status_im/contexts/wallet/common/token_value/view.cljs index 8ed86f306b..36330a1656 100644 --- a/src/status_im/contexts/wallet/common/token_value/view.cljs +++ b/src/status_im/contexts/wallet/common/token_value/view.cljs @@ -77,12 +77,13 @@ (defn token-value-drawer [token watch-only? entry-point] (let [token-symbol (:token token) - token-data (first (rf/sub [:wallet/current-viewing-account-tokens-filtered - {:query token-symbol}])) + token-data (rf/sub [:wallet/token-by-symbol token-symbol]) selected-account (rf/sub [:wallet/current-viewing-account-address]) token-owners (rf/sub [:wallet/operable-addresses-with-token-symbol token-symbol]) testnet-mode? (rf/sub [:profile/test-networks-enabled?]) + account-owns-token? (rf/sub [:wallet/current-account-owns-token token-symbol]) receive-token-symbol (if (= token-symbol "SNT") "ETH" "SNT") + token-owned? (if selected-account account-owns-token? (seq token-owners)) asset-to-receive (rf/sub [:wallet/token-by-symbol receive-token-symbol]) params (cond-> {:start-flow? true :owners token-owners @@ -103,11 +104,12 @@ (action-hide))] (not watch-only?) (concat [(action-buy) - (when (seq token-owners) + (when token-owned? (action-send params entry-point)) (action-receive selected-account) - (action-swap params) - (when (seq token-owners) + (when token-owned? + (action-swap params)) + (when token-owned? (action-bridge (assoc params :bridge-disabled? (send-utils/bridge-disabled? token-symbol))))]))]])) diff --git a/src/status_im/contexts/wallet/common/utils/networks.cljs b/src/status_im/contexts/wallet/common/utils/networks.cljs index 591ace7e77..3084e0bb72 100644 --- a/src/status_im/contexts/wallet/common/utils/networks.cljs +++ b/src/status_im/contexts/wallet/common/utils/networks.cljs @@ -1,8 +1,10 @@ (ns status-im.contexts.wallet.common.utils.networks - (:require [clojure.string :as string] - [quo.foundations.resources :as resources] - [status-im.constants :as constants] - [utils.number])) + (:require + [clojure.string :as string] + [quo.foundations.resources :as resources] + [status-im.constants :as constants] + [utils.money :as money] + [utils.number])) (def ^:private last-comma-followed-by-text-to-end-regex #",\s(?=[^,]+$)") @@ -63,12 +65,21 @@ (defn network-list [{:keys [balances-per-chain]} networks] - (into #{} - (mapv (fn [chain-id] - (first (filter #(or (= (:chain-id %) chain-id) - (= (:related-chain-id %) chain-id)) - networks))) - (keys balances-per-chain)))) + (->> balances-per-chain + keys + (map (fn [chain-id] + (first (filter #(or (= (:chain-id %) chain-id) + (= (:related-chain-id %) chain-id)) + networks)))) + set)) + +(defn network-list-with-positive-balance + "Same as `network-list`, but only returns the networks that have a positive token balance" + [{:keys [balances-per-chain] :as token} networks] + (as-> balances-per-chain $ + (filter #(-> % second :raw-balance (money/greater-than 0)) $) + (assoc token :balances-per-chain $) + (network-list $ networks))) (defn get-default-chain-ids-by-mode [{:keys [test-networks-enabled? is-goerli-enabled?]}] diff --git a/src/status_im/contexts/wallet/swap/select_asset_to_pay/view.cljs b/src/status_im/contexts/wallet/swap/select_asset_to_pay/view.cljs index 190e4fd742..f78a1154f2 100644 --- a/src/status_im/contexts/wallet/swap/select_asset_to_pay/view.cljs +++ b/src/status_im/contexts/wallet/swap/select_asset_to_pay/view.cljs @@ -26,7 +26,7 @@ (let [pay-token-symbol (:symbol token) asset-to-receive (if (= pay-token-symbol "SNT") eth-token snt-token)] (rf/dispatch [:wallet.swap/start - {:asset-to-pay token + {:asset-to-pay {:symbol pay-token-symbol} :asset-to-receive asset-to-receive :open-new-screen? false}])))] [:<> diff --git a/src/status_im/subs/wallet/wallet.cljs b/src/status_im/subs/wallet/wallet.cljs index f80e680316..a7d21e3bef 100644 --- a/src/status_im/subs/wallet/wallet.cljs +++ b/src/status_im/subs/wallet/wallet.cljs @@ -450,17 +450,19 @@ (map (fn [token] (assoc token - :bridge-disabled? (and (= tx-type :tx/bridge) - (send-utils/bridge-disabled? (:symbol - token))) - :networks (cond->> (network-utils/network-list token - networks) - chain-ids - (filter #(some #{(:chain-id %)} chain-ids))) + :bridge-disabled? (and (= tx-type :tx/bridge) + (send-utils/bridge-disabled? (:symbol + token))) + :networks (cond->> + (network-utils/network-list-with-positive-balance + token + networks) + chain-ids + (filter #(some #{(:chain-id %)} chain-ids))) :available-balance (utils/calculate-total-token-balance token) - :total-balance (utils/calculate-total-token-balance - token - chain-ids)))) + :total-balance (utils/calculate-total-token-balance + token + chain-ids)))) (filter (fn [{:keys [networks]}] (pos? (count networks)))) (remove #(when hide-token-fn (hide-token-fn constants/swap-tokens-my %)))) @@ -575,6 +577,14 @@ tokens)) addresses-tokens))) +(rf/reg-sub + :wallet/current-account-owns-token + (fn [[_ token-symbol]] + [(rf/subscribe [:wallet/current-viewing-account-address]) + (rf/subscribe [:wallet/operable-addresses-with-token-symbol token-symbol])]) + (fn [[address addresses-with-token]] + (-> addresses-with-token set (contains? address)))) + (rf/reg-sub :wallet/account-tab :<- [:wallet/ui]