fix(wallet)_: token supported networks (#21451)

This commit 
- fixes the networks/chains supported by the token based on the token list fetched from status-go instead of relying on the balance-per-chain map as status-go returns balance for chains only if there is a positive balance
- adds supported-networks key to token data map for network details

Signed-off-by: Mohamed Javid <19339952+smohamedjavid@users.noreply.github.com>
This commit is contained in:
Mohamed Javid 2024-10-24 16:00:18 +05:30 committed by GitHub
parent 65cdb0dfe7
commit c62d7d501e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 306 additions and 241 deletions

View File

@ -15,7 +15,7 @@
(defn- bridge-token-component
[]
(fn [{:keys [chain-id network-name]} {:keys [networks] :as token}]
(fn [{:keys [chain-id network-name]} {:keys [supported-networks] :as token}]
(let [network (rf/sub [:wallet/network-details-by-chain-id chain-id])
currency (rf/sub [:profile/currency])
currency-symbol (rf/sub [:profile/currency-symbol])
@ -28,7 +28,8 @@
fiat-formatted (utils/get-standard-fiat-format crypto-value
currency-symbol
fiat-value)
token-available-on-network? (network-utils/token-available-on-network? networks chain-id)]
token-available-on-network? (network-utils/token-available-on-network? supported-networks
chain-id)]
[quo/network-list
{:label (name network-name)
:network-image (quo.resources/get-network (:network-name network))
@ -49,7 +50,10 @@
mainnet (first network-details)
layer-2-networks (rest network-details)
account-token (some #(when (= token-symbol (:symbol %)) %) tokens)
account-token (when account-token (assoc account-token :networks (:networks token)))
account-token (when account-token
(assoc account-token
:networks (:networks token)
:supported-networks (:supported-networks token)))
bridge-to-title (i18n/label :t/bridge-to
{:name (string/upper-case (str token-symbol))})]

View File

@ -522,7 +522,8 @@
[tokens networks chain-ids]
(map (fn [token]
(assoc token
:networks (network-utils/network-list token networks)
:networks (network-utils/network-list-with-positive-balance token networks)
:supported-networks (network-utils/network-list token networks)
:available-balance (calculate-total-token-balance token)
:total-balance (calculate-total-token-balance token chain-ids)))
tokens))

View File

@ -72,11 +72,32 @@
(update :testPreferredChainIds chain-ids-set->string)
(dissoc :watch-only? :default-account? :operable? :tokens :collectibles)))
(defn add-missing-balance-per-chain-values
"Adds any missing chain balance (0) to the balance-per-chain map based on token supported chains as
status-go returns balance for that chain only if the balance is positive."
[balances-per-chain supported-chains]
(let [zero-balance-per-chain (fn [chain-id]
{:chain-id chain-id
:raw-balance (money/->bignumber 0)
:balance "0"})]
(reduce
(fn [result chain-id]
(if (contains? result chain-id)
result
(assoc result chain-id (zero-balance-per-chain chain-id))))
balances-per-chain
supported-chains)))
(defn- rpc->balances-per-chain
[token]
[token supported-chains]
(-> token
(update :balances-per-chain update-vals #(update % :raw-balance money/bignumber))
(update :balances-per-chain update-keys (comp utils.number/parse-int name))))
(update :balances-per-chain update-keys (comp utils.number/parse-int name))
(update :balances-per-chain #(add-missing-balance-per-chain-values % supported-chains))))
(defn- update-balances-per-chain
[tokens supported-chains-by-token-symbol]
(mapv #(rpc->balances-per-chain % (get supported-chains-by-token-symbol (:symbol %))) tokens))
(defn- remove-tokens-with-empty-values
[tokens]
@ -85,12 +106,12 @@
tokens))
(defn rpc->tokens
[tokens]
[tokens supported-chains-by-token-symbol]
(-> tokens
(update-keys name)
(update-vals #(cske/transform-keys transforms/->kebab-case-keyword %))
(update-vals remove-tokens-with-empty-values)
(update-vals #(mapv rpc->balances-per-chain %))))
(update-vals #(update-balances-per-chain % supported-chains-by-token-symbol))))
(defn rpc->network
[network]

View File

@ -229,9 +229,12 @@
(rf/reg-event-fx
:wallet/store-wallet-token
(fn [{:keys [db]} [address raw-tokens-data]]
(let [tokens (data-store/rpc->tokens raw-tokens-data)
(let [supported-chains-by-token-symbol (get-in db [:wallet :tokens :supported-chains-by-symbol])
tokens (data-store/rpc->tokens raw-tokens-data
supported-chains-by-token-symbol)
add-tokens (fn [stored-accounts tokens-per-account]
(reduce-kv (fn [accounts address tokens-data]
(reduce-kv
(fn [accounts address tokens-data]
(if (contains? accounts address)
(update accounts address assoc :tokens tokens-data)
accounts))

View File

@ -53,7 +53,7 @@
token-decimals (if collectible 0 (:decimals token))
native-token? (and token (= token-display-name "ETH"))
routes-available? (pos? (count chosen-route))
token-networks (:networks token)
token-networks (:supported-networks token)
token-networks-ids (when token-networks
(map #(:chain-id %) token-networks))
from-network-amounts-by-chain (send-utils/network-amounts-by-chain
@ -200,7 +200,11 @@
token-data (or token
(when (and token-symbol unique-owner)
(some #(when (= (:symbol %) token-symbol) %)
unique-owner-tokens)))]
unique-owner-tokens)))
test-networks-enabled? (get-in db [:profile/profile :test-networks-enabled?])
network-details (-> (get-in db
[:wallet :networks (if test-networks-enabled? :test :prod)])
(network-utils/sorted-networks-with-details))]
(when (or token-data token-symbol)
{:db (cond-> db
:always (update-in [:wallet :ui :send]
@ -211,7 +215,11 @@
token-symbol (assoc-in [:wallet :ui :send :token-symbol] token-symbol)
token-data (update-in [:wallet :ui :send]
#(assoc %
:token token-data
:token (assoc token-data
:supported-networks
(network-utils/network-list
token-data
network-details))
:token-display-name (:symbol token-data)))
unique-owner (assoc-in [:wallet :current-viewing-account-address] unique-owner)
entry-point (assoc-in [:wallet :ui :send :entry-point] entry-point))
@ -230,7 +238,7 @@
(rf/reg-event-fx
:wallet/edit-token-to-send
(fn [{:keys [db]} [token]]
(let [{token-networks :networks
(let [{token-networks :supported-networks
token-symbol :symbol} token
receiver-networks (get-in db [:wallet :ui :send :receiver-networks])
token-networks-ids (map :chain-id token-networks)
@ -426,7 +434,7 @@
{:balances-per-chain balances-per-chain
:disabled-chain-ids disabled-from-chain-ids
:only-with-balance? false}))
token-networks-ids (when token (map #(:chain-id %) (:networks token)))
token-networks-ids (when token (map #(:chain-id %) (:supported-networks token)))
sender-network-values (when sender-token-available-networks-for-suggested-routes
(send-utils/loading-network-amounts
{:valid-networks
@ -708,7 +716,11 @@
;; account, so we extract the token data from the picked account.
(let [token (utils/get-token-from-account db token-symbol address)]
(assoc token
:networks (network-utils/network-list token network-details)
:networks (network-utils/network-list-with-positive-balance
token
network-details)
:supported-networks (network-utils/network-list token
network-details)
:total-balance (utils/calculate-total-token-balance token))))
bridge-tx? (= tx-type :tx/bridge)
flow-id (if bridge-tx?

View File

@ -82,6 +82,9 @@
token {:symbol "ETH"
:name "Ether"
:networks #{{:chain-id 421614}
{:chain-id 11155420}
{:chain-id 11155111}}
:supported-networks #{{:chain-id 421614}
{:chain-id 11155420}
{:chain-id 11155111}}}
receiver-networks [421614 11155420]]

View File

@ -27,7 +27,7 @@
receiver-preferred-networks (rf/sub
[:wallet/wallet-send-receiver-preferred-networks])
{token-symbol :symbol
token-networks :networks} (rf/sub [:wallet/wallet-send-token])
token-networks :supported-networks} (rf/sub [:wallet/wallet-send-token])
token-chain-ids-set (set (mapv #(:chain-id %) token-networks))
[selected-receiver-networks
set-selected-receiver-networks] (rn/use-state receiver-networks)

View File

@ -65,7 +65,11 @@
:tokens
(filter #(= token-symbol (:symbol %)))
first)]
(assoc token :networks (network-utils/network-list token networks))))
(assoc token
:networks
(network-utils/network-list-with-positive-balance
token
networks))))
(defn select-default-asset-to-receive
"Selects an asset to receive if it was not provided explicitly.

View File

@ -53,7 +53,17 @@
vals
(map :symbol)
set
vec)]
vec)
by-symbol-vals (-> tokens :by-symbol vals)
supported-chains-by-symbol (reduce
(fn [result
{token-symbol :symbol
:keys [chain-id]}]
(if (contains? result token-symbol)
(update result token-symbol conj chain-id)
(assoc result token-symbol #{chain-id})))
{}
by-symbol-vals)]
{:fx [[:effects.wallet.tokens/fetch-market-values
{:symbols symbols
:currency constants/profile-default-currency
@ -71,8 +81,9 @@
:db (-> db
(assoc-in [:wallet :tokens]
{:sources (:sources tokens)
:supported-chains-by-symbol supported-chains-by-symbol
:by-address (-> tokens :by-address vals)
:by-symbol (-> tokens :by-symbol vals)})
:by-symbol by-symbol-vals})
(assoc-in [:wallet :ui :loading]
{:token-list false
:market-values true

View File

@ -94,7 +94,8 @@
:type :erc20
:verified? true
:chain-id 1
:image nil})}
:image nil})
:supported-chains-by-symbol {"ETH" #{1} "SNT" #{1}}}
:ui {:loading {:token-list false
:market-values true
:details true
@ -114,7 +115,8 @@
:version "1.0.0"
:tokens-count 2}]
:by-address nil
:by-symbol nil}))))
:by-symbol nil
:supported-chains-by-symbol {}}))))
(testing "response contains two lists"
(let [cofx {:db {:wallet {:networks {:prod [{:chain-id 1}]}}}}
data [{:name "native"
@ -204,7 +206,8 @@
:type :erc20
:verified? false
:chain-id 1
:image nil})}))))
:image nil})
:supported-chains-by-symbol {"ETH" #{1} "SNT" #{1} "ANOTHER1" #{1}}}))))
(testing "second list contains a token that replaces the one that was added before"
(let [cofx {:db {:wallet {:networks {:prod [{:chain-id 1}]}}}}
data [{:name "native"
@ -272,4 +275,5 @@
:type :erc20
:verified? false
:chain-id 1
:image nil})})))))
:image nil})
:supported-chains-by-symbol {"ETH" #{1} "ANOTHER1" #{1}}})))))

View File

@ -144,7 +144,8 @@
(remove disabled-from-chain-ids?)
set)]
(some-> token
(assoc :networks (network-utils/network-list token networks)
(assoc :networks (network-utils/network-list-with-positive-balance token networks)
:supported-networks (network-utils/network-list token networks)
:available-balance (utils/calculate-total-token-balance token)
:total-balance (utils/calculate-total-token-balance
token
@ -459,6 +460,7 @@
networks)
chain-ids
(filter #(some #{(:chain-id %)} chain-ids)))
:supported-networks (network-utils/network-list token networks)
:available-balance (utils/calculate-total-token-balance token)
:total-balance (utils/calculate-total-token-balance
token