fix(wallet): asset sorting (#20659)

* fix(wallet): asset sorting (#20659)
This commit is contained in:
Omar Basem 2024-07-10 14:49:24 +04:00 committed by GitHub
parent 3521421098
commit 6c352397ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 79 additions and 27 deletions

View File

@ -177,7 +177,7 @@
"This function returns token values in the props of token-value (quo) component"
[{:keys [token color currency currency-symbol]}]
(let [balance (calculate-total-token-balance token)
fiat-value (calculate-token-fiat-value
fiat-unformatted-value (calculate-token-fiat-value
{:currency currency
:balance balance
:token token})
@ -191,7 +191,7 @@
crypto-value (get-standard-crypto-format token balance)
fiat-value (get-standard-fiat-format crypto-value
currency-symbol
fiat-value)]
fiat-unformatted-value)]
{:token (:symbol token)
:token-name (:name token)
:state :default
@ -201,10 +201,11 @@
(neg? change-pct-24hour) :negative
:else :empty)
:customization-color color
:values {:crypto-value crypto-value
:fiat-value fiat-value
:fiat-change formatted-token-price
:percentage-change percentage-change}}))
:values {:crypto-value crypto-value
:fiat-value fiat-value
:fiat-unformatted-value fiat-unformatted-value
:fiat-change formatted-token-price
:percentage-change percentage-change}}))
(defn get-multichain-address
[networks address]
@ -298,3 +299,18 @@
(utils.string/contains-emoji? s) :emoji
(existing-account-names s) :existing-name
(utils.string/contains-special-character? s) :special-character))
(defn calculate-and-sort-tokens
[{:keys [tokens color currency currency-symbol]}]
(let [calculate-token (fn [token]
(calculate-token-value {:token token
:color color
:currency currency
:currency-symbol currency-symbol}))
calculated-tokens (map calculate-token tokens)
token-priority {"SNT" 1 "STT" 1 "ETH" 2 "DAI" 3}]
(sort-by (fn [token]
(let [fiat-value (get-in token [:values :fiat-unformatted-value])
priority (get token-priority (:token token) 999)]
[(- fiat-value) priority]))
calculated-tokens)))

View File

@ -115,3 +115,39 @@
(is (= (utils/prettify-percentage-change 1.113454) "1.11"))
(is (= (utils/prettify-percentage-change -0.35) "0.35"))
(is (= (utils/prettify-percentage-change -0.78234) "0.78"))))
(deftest calculate-and-sort-tokens-test
(testing "calculate-and-sort-tokens function"
(let [mock-color "blue"
mock-currency "USD"
mock-currency-symbol "$"]
(with-redefs [utils/calculate-token-value
(fn [{:keys [token]}]
(case (:symbol token)
"ETH" {:token "ETH" :values {:fiat-unformatted-value 5}}
"DAI" {:token "DAI" :values {:fiat-unformatted-value 10}}
"SNT" {:token "SNT" :values {:fiat-unformatted-value 1}}))]
(testing "Standard case with different fiat-unformatted-values"
(let [mock-tokens [{:symbol "ETH"
:name "Ethereum"
:balances-per-chain {:mock-chain 5}
:decimals 18}
{:symbol "DAI"
:name "Dai"
:balances-per-chain {:mock-chain 10}
:decimals 18}
{:symbol "SNT"
:name "Status Network Token"
:balances-per-chain {:mock-chain 1}
:decimals 18}]
mock-input {:tokens mock-tokens
:color mock-color
:currency mock-currency
:currency-symbol mock-currency-symbol}
sorted-tokens (map :token (utils/calculate-and-sort-tokens mock-input))
expected-order ["DAI" "ETH" "SNT"]]
(is (= expected-order sorted-tokens))))))))

View File

@ -489,19 +489,6 @@
(fn [ui]
(get-in ui [:account-page :active-tab])))
(rf/reg-sub
:wallet/current-viewing-account-token-values
:<- [:wallet/current-viewing-account]
:<- [:wallet/current-viewing-account-tokens-in-selected-networks]
:<- [:profile/currency]
:<- [:profile/currency-symbol]
(fn [[{:keys [color]} tokens currency currency-symbol]]
(mapv #(utils/calculate-token-value {:token %
:color color
:currency currency
:currency-symbol currency-symbol})
tokens)))
(rf/reg-sub
:wallet/aggregated-tokens
:<- [:wallet/accounts-without-watched-accounts]
@ -515,6 +502,18 @@
(fn [[aggregated-tokens chain-ids]]
(utils/filter-tokens-in-chains aggregated-tokens chain-ids)))
(rf/reg-sub
:wallet/current-viewing-account-token-values
:<- [:wallet/current-viewing-account]
:<- [:wallet/current-viewing-account-tokens-in-selected-networks]
:<- [:profile/currency]
:<- [:profile/currency-symbol]
(fn [[{:keys [color]} tokens currency currency-symbol]]
(utils/calculate-and-sort-tokens {:tokens tokens
:color color
:currency currency
:currency-symbol currency-symbol})))
(rf/reg-sub
:wallet/aggregated-token-values-and-balance
:<- [:wallet/aggregated-tokens-in-selected-networks]
@ -522,16 +521,17 @@
:<- [:profile/currency]
:<- [:profile/currency-symbol]
(fn [[aggregated-tokens color currency currency-symbol]]
(let [balance (utils/calculate-balance-from-tokens {:currency currency
:tokens aggregated-tokens})
formatted-balance (utils/prettify-balance currency-symbol balance)]
(let [balance (utils/calculate-balance-from-tokens {:currency currency
:tokens aggregated-tokens})
formatted-balance (utils/prettify-balance currency-symbol balance)
sorted-token-values (utils/calculate-and-sort-tokens {:tokens aggregated-tokens
:color color
:currency currency
:currency-symbol currency-symbol})]
{:balance balance
:formatted-balance formatted-balance
:tokens (mapv #(utils/calculate-token-value {:token %
:color color
:currency currency
:currency-symbol currency-symbol})
aggregated-tokens)})))
:tokens sorted-token-values})))
(rf/reg-sub
:wallet/network-preference-details