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
This commit is contained in:
Lungu Cristian 2024-10-22 15:55:37 +03:00 committed by GitHub
parent 01c5cf2498
commit 5118a5095d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 26 deletions

View File

@ -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))))]))]]))

View File

@ -1,7 +1,9 @@
(ns status-im.contexts.wallet.common.utils.networks
(:require [clojure.string :as string]
(: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]
(->> balances-per-chain
keys
(map (fn [chain-id]
(first (filter #(or (= (:chain-id %) chain-id)
(= (:related-chain-id %) chain-id))
networks)))
(keys balances-per-chain))))
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?]}]

View File

@ -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}])))]
[:<>

View File

@ -453,7 +453,9 @@
:bridge-disabled? (and (= tx-type :tx/bridge)
(send-utils/bridge-disabled? (:symbol
token)))
:networks (cond->> (network-utils/network-list token
:networks (cond->>
(network-utils/network-list-with-positive-balance
token
networks)
chain-ids
(filter #(some #{(:chain-id %)} chain-ids)))
@ -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]