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,9 +522,10 @@
[tokens networks chain-ids]
(map (fn [token]
(assoc token
:networks (network-utils/network-list token networks)
:available-balance (calculate-total-token-balance token)
:total-balance (calculate-total-token-balance token chain-ids)))
: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))
(defn estimated-time-format

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,14 +229,17 @@
(rf/reg-event-fx
:wallet/store-wallet-token
(fn [{:keys [db]} [address raw-tokens-data]]
(let [tokens (data-store/rpc->tokens raw-tokens-data)
add-tokens (fn [stored-accounts tokens-per-account]
(reduce-kv (fn [accounts address tokens-data]
(if (contains? accounts address)
(update accounts address assoc :tokens tokens-data)
accounts))
stored-accounts
tokens-per-account))]
(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]
(if (contains? accounts address)
(update accounts address assoc :tokens tokens-data)
accounts))
stored-accounts
tokens-per-account))]
{:db (-> db
(update-in [:wallet :accounts] add-tokens tokens)
(assoc-in [:wallet :ui :tokens-loading address] false))})))

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
@ -190,17 +190,21 @@
;; `token` is a map extracted from the sender, but in the wallet home page we don't know the
;; sender yet, so we only provide the `token-symbol`, later in
;; `:wallet/select-from-account` the `token` key will be set.
(let [{:keys [networks]} token
receiver-networks (get-in db [:wallet :ui :send :receiver-networks])
token-networks-ids (map :chain-id networks)
unsupported-token? (not-any? (set receiver-networks) token-networks-ids)
unique-owner (when (= (count owners) 1)
(first owners))
unique-owner-tokens (get-in db [:wallet :accounts unique-owner :tokens])
token-data (or token
(when (and token-symbol unique-owner)
(some #(when (= (:symbol %) token-symbol) %)
unique-owner-tokens)))]
(let [{:keys [networks]} token
receiver-networks (get-in db [:wallet :ui :send :receiver-networks])
token-networks-ids (map :chain-id networks)
unsupported-token? (not-any? (set receiver-networks) token-networks-ids)
unique-owner (when (= (count owners) 1)
(first owners))
unique-owner-tokens (get-in db [:wallet :accounts unique-owner :tokens])
token-data (or token
(when (and token-symbol unique-owner)
(some #(when (= (:symbol %) token-symbol) %)
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

@ -79,11 +79,14 @@
(h/deftest-event :wallet/edit-token-to-send
[event-id dispatch]
(let [token-symbol "ETH"
token {:symbol "ETH"
:name "Ether"
:networks #{{:chain-id 421614}
{:chain-id 11155420}
{:chain-id 11155111}}}
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]]
(testing "can be called with :token"
(let [initial-db {:wallet {:ui {:send {:receiver-networks receiver-networks

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

@ -20,40 +20,50 @@
(defn store-token-list
[{:keys [db]} [{:keys [data]}]]
(let [chain-ids (chain/chain-ids db)
tokens (reduce (fn [{:keys [by-address by-symbol] :as data}
{:keys [name source version tokens]}]
(-> data
(update :sources
conj
{:name name
:source source
:version version
:tokens-count (count tokens)})
(update :by-address
merge
(tokens-data/tokens-by-address
{:added-tokens by-address
:source-name name
:tokens tokens
:chain-ids chain-ids}))
(update :by-symbol
merge
(tokens-data/tokens-by-symbol
{:added-tokens by-symbol
:source-name name
:tokens tokens
:chain-ids chain-ids}))))
{:sources []
:by-address {}
:by-symbol {}}
data)
symbols (->> tokens
:by-symbol
vals
(map :symbol)
set
vec)]
(let [chain-ids (chain/chain-ids db)
tokens (reduce (fn [{:keys [by-address by-symbol] :as data}
{:keys [name source version tokens]}]
(-> data
(update :sources
conj
{:name name
:source source
:version version
:tokens-count (count tokens)})
(update :by-address
merge
(tokens-data/tokens-by-address
{:added-tokens by-address
:source-name name
:tokens tokens
:chain-ids chain-ids}))
(update :by-symbol
merge
(tokens-data/tokens-by-symbol
{:added-tokens by-symbol
:source-name name
:tokens tokens
:chain-ids chain-ids}))))
{:sources []
:by-address {}
:by-symbol {}}
data)
symbols (->> tokens
:by-symbol
vals
(map :symbol)
set
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
@ -70,9 +80,10 @@
:on-error [:wallet.tokens/fetch-prices-failed]}]]
:db (-> db
(assoc-in [:wallet :tokens]
{:sources (:sources tokens)
:by-address (-> tokens :by-address vals)
:by-symbol (-> tokens :by-symbol vals)})
{:sources (:sources tokens)
:supported-chains-by-symbol supported-chains-by-symbol
:by-address (-> tokens :by-address vals)
:by-symbol by-symbol-vals})
(assoc-in [:wallet :ui :loading]
{:token-list false
:market-values true

View File

@ -47,10 +47,10 @@
(match?
(:db (tokens.events/store-token-list cofx [{:data data}]))
{:wallet {:networks {:prod [{:chain-id 1}]}
:tokens {:sources [{:name "native"
:source "native"
:version "1.0.0"
:tokens-count 2}]
:tokens {:sources [{:name "native"
:source "native"
:version "1.0.0"
:tokens-count 2}]
:by-address '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-0x0000000000000000000000000000000000000000"
@ -73,28 +73,29 @@
:verified? true
:chain-id 1
:image nil})
:by-symbol '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-ETH"
:community-id nil
:symbol "ETH"
:sources ["native"]
:name "Ether"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000001"
:decimals 18
:key "1-SNT"
:community-id nil
:symbol "SNT"
:sources ["native"]
:name "Status Network Token"
:type :erc20
:verified? true
:chain-id 1
:image nil})}
:by-symbol '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-ETH"
:community-id nil
:symbol "ETH"
:sources ["native"]
:name "Ether"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000001"
:decimals 18
:key "1-SNT"
:community-id nil
:symbol "SNT"
:sources ["native"]
:name "Status Network Token"
:type :erc20
:verified? true
:chain-id 1
:image nil})
:supported-chains-by-symbol {"ETH" #{1} "SNT" #{1}}}
:ui {:loading {:token-list false
:market-values true
:details true
@ -109,12 +110,13 @@
:db
:wallet
:tokens)
{:sources [{:name "native"
:source "native"
:version "1.0.0"
:tokens-count 2}]
:by-address nil
:by-symbol nil}))))
{:sources [{:name "native"
:source "native"
:version "1.0.0"
:tokens-count 2}]
:by-address 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"
@ -131,80 +133,81 @@
:db
:wallet
:tokens)
{:sources [{:name "native"
:source "native"
:version "1.0.0"
:tokens-count 2}
{:name "second"
:source "second"
:version "1.0.0"
:tokens-count 2}]
:by-address '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-0x0000000000000000000000000000000000000000"
:community-id nil
:symbol "ETH"
:sources ["native"]
:name "Ether"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000001"
:decimals 18
:key "1-0x0000000000000000000000000000000000000001"
:community-id nil
:symbol "SNT"
:sources ["native" "second"]
:name "Status Network Token"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000002"
:decimals 18
:key "1-0x0000000000000000000000000000000000000002"
:community-id nil
:symbol "ANOTHER1"
:sources ["second"]
:name "Another Token 1"
:type :erc20
:verified? false
:chain-id 1
:image nil})
:by-symbol '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-ETH"
:community-id nil
:symbol "ETH"
:sources ["native"]
:name "Ether"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000001"
:decimals 18
:key "1-SNT"
:community-id nil
:symbol "SNT"
:sources ["native" "second"]
:name "Status Network Token"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000002"
:decimals 18
:key "1-ANOTHER1"
:community-id nil
:symbol "ANOTHER1"
:sources ["second"]
:name "Another Token 1"
:type :erc20
:verified? false
:chain-id 1
:image nil})}))))
{:sources [{:name "native"
:source "native"
:version "1.0.0"
:tokens-count 2}
{:name "second"
:source "second"
:version "1.0.0"
:tokens-count 2}]
:by-address '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-0x0000000000000000000000000000000000000000"
:community-id nil
:symbol "ETH"
:sources ["native"]
:name "Ether"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000001"
:decimals 18
:key "1-0x0000000000000000000000000000000000000001"
:community-id nil
:symbol "SNT"
:sources ["native" "second"]
:name "Status Network Token"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000002"
:decimals 18
:key "1-0x0000000000000000000000000000000000000002"
:community-id nil
:symbol "ANOTHER1"
:sources ["second"]
:name "Another Token 1"
:type :erc20
:verified? false
:chain-id 1
:image nil})
:by-symbol '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-ETH"
:community-id nil
:symbol "ETH"
:sources ["native"]
:name "Ether"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000001"
:decimals 18
:key "1-SNT"
:community-id nil
:symbol "SNT"
:sources ["native" "second"]
:name "Status Network Token"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000002"
:decimals 18
:key "1-ANOTHER1"
:community-id nil
:symbol "ANOTHER1"
:sources ["second"]
:name "Another Token 1"
:type :erc20
:verified? false
:chain-id 1
: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"
@ -221,55 +224,56 @@
:db
:wallet
:tokens)
{:sources [{:name "native"
:source "native"
:version "1.0.0"
:tokens-count 2}
{:name "second"
:source "second"
:version "1.0.0"
:tokens-count 1}]
:by-address '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-0x0000000000000000000000000000000000000000"
:community-id nil
:symbol "ANOTHER1"
:sources ["native" "second"]
:name "Another Token 2"
:type :erc20
:verified? false
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000002"
:decimals 18
:key "1-0x0000000000000000000000000000000000000002"
:community-id nil
:symbol "ANOTHER1"
:sources ["native"]
:name "Another Token 1"
:type :erc20
:verified? false
:chain-id 1
:image nil})
:by-symbol '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-ETH"
:community-id nil
:symbol "ETH"
:sources ["native"]
:name "Ether"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-ANOTHER1"
:community-id nil
:symbol "ANOTHER1"
:sources ["native" "second"]
:name "Another Token 2"
:type :erc20
:verified? false
:chain-id 1
:image nil})})))))
{:sources [{:name "native"
:source "native"
:version "1.0.0"
:tokens-count 2}
{:name "second"
:source "second"
:version "1.0.0"
:tokens-count 1}]
:by-address '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-0x0000000000000000000000000000000000000000"
:community-id nil
:symbol "ANOTHER1"
:sources ["native" "second"]
:name "Another Token 2"
:type :erc20
:verified? false
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000002"
:decimals 18
:key "1-0x0000000000000000000000000000000000000002"
:community-id nil
:symbol "ANOTHER1"
:sources ["native"]
:name "Another Token 1"
:type :erc20
:verified? false
:chain-id 1
:image nil})
:by-symbol '({:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-ETH"
:community-id nil
:symbol "ETH"
:sources ["native"]
:name "Ether"
:type :erc20
:verified? true
:chain-id 1
:image nil}
{:address "0x0000000000000000000000000000000000000000"
:decimals 18
:key "1-ANOTHER1"
:community-id nil
:symbol "ANOTHER1"
:sources ["native" "second"]
:name "Another Token 2"
:type :erc20
:verified? false
:chain-id 1
:image nil})
:supported-chains-by-symbol {"ETH" #{1} "ANOTHER1" #{1}}})))))

View File

@ -144,11 +144,12 @@
(remove disabled-from-chain-ids?)
set)]
(some-> token
(assoc :networks (network-utils/network-list token networks)
:available-balance (utils/calculate-total-token-balance token)
:total-balance (utils/calculate-total-token-balance
token
enabled-from-chain-ids))))))
(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
enabled-from-chain-ids))))))
(rf/reg-sub
:wallet/wallet-send-token-symbol
@ -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