[#21305] fix: sort assets by value and collectibles by name (#21908)

* [#21305] fix: sort assets by value and collectibles by name

* [#21305] fix: use seq to check name is nil or empty

* [#21305] fix: normalize text to sort properly
This commit is contained in:
Mohsen 2025-01-20 19:41:40 +03:00 committed by GitHub
parent 70a5ed255f
commit ef7d977299
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 98 additions and 34 deletions

View File

@ -1,5 +1,6 @@
(ns status-im.contexts.wallet.collectible.utils
(:require [status-im.config :as config]
(:require [clojure.string :as string]
[status-im.config :as config]
[status-im.constants :as constants]
[status-im.contexts.wallet.common.utils.networks :as network-utils]
[taoensso.timbre :as log]
@ -79,3 +80,12 @@
acc)))
{})
vals))
(defn sort-collectibles-by-name
[collectibles]
(sort-by (fn [collectible]
(let [name (-> collectible :collectible-data :name)
normalized-name (some-> name
string/lower-case)]
[(if (seq normalized-name) 0 1) normalized-name]))
collectibles))

View File

@ -44,3 +44,18 @@
:token-id token-id
:test-networks-enabled? true})
"https://testnets.opensea.io/assets/optimism-sepolia/0xC/0xT"))))
(deftest sort-collectibles-by-name-test
(testing "Sorts collectibles by name, moving nil or empty names to the end"
(let [collectibles [{:collectible-data {:name "Alpha"}}
{:collectible-data {:name nil}}
{:collectible-data {:name "Beta"}}
{:collectible-data {:name ""}}
{:collectible-data {:name "Zeta"}}]
sorted-collectibles (utils/sort-collectibles-by-name collectibles)
expected [{:collectible-data {:name "Alpha"}}
{:collectible-data {:name "Beta"}}
{:collectible-data {:name "Zeta"}}
{:collectible-data {:name nil}}
{:collectible-data {:name ""}}]]
(is (= sorted-collectibles expected)))))

View File

@ -10,15 +10,11 @@
token-name :name
total-balance :total-balance
disabled? :bridge-disabled?
fiat-value :fiat-value
:as token}
_ _
{:keys [currency currency-symbol on-token-press preselected-token-symbol prices-per-token]}]
(let [fiat-value (utils/calculate-token-fiat-value
{:currency currency
:balance total-balance
:token token
:prices-per-token prices-per-token})
crypto-formatted (utils/get-standard-crypto-format token total-balance prices-per-token)
{:keys [currency-symbol on-token-press preselected-token-symbol prices-per-token]}]
(let [crypto-formatted (utils/get-standard-crypto-format token total-balance prices-per-token)
fiat-formatted (utils/fiat-formatted-for-ui currency-symbol fiat-value)]
[quo/token-network
{:token token-symbol
@ -38,13 +34,11 @@
(let [filtered-tokens (rf/sub [:wallet/current-viewing-account-tokens-filtered
{:query search-text
:chain-ids chain-ids}])
currency (rf/sub [:profile/currency])
currency-symbol (rf/sub [:profile/currency-symbol])
prices-per-token (rf/sub [:wallet/prices-per-token])]
[gesture/flat-list
{:data filtered-tokens
:render-data {:currency currency
:currency-symbol currency-symbol
:render-data {:currency-symbol currency-symbol
:on-token-press on-token-press
:preselected-token-symbol preselected-token-symbol
:prices-per-token prices-per-token}

View File

@ -409,13 +409,15 @@
(defn sort-tokens
[tokens]
(let [priority #(get constants/token-sort-priority (:symbol %) ##Inf)]
(sort-by (juxt (comp - :balance) priority) tokens)))
(sort-by (comp - :balance) tokens))
(defn sort-tokens-by-fiat-value
[tokens]
(sort-by (comp - :fiat-value) tokens))
(defn sort-tokens-by-name
[tokens]
(let [priority #(get constants/token-sort-priority (:symbol %) ##Inf)]
(sort-by (juxt :symbol priority) tokens)))
(sort-by :symbol tokens))
(defn token-with-balance
([token networks]

View File

@ -345,3 +345,33 @@
expected []]
(is (= (utils/get-accounts-with-token-balance accounts token)
expected)))))
(deftest sort-tokens-test
(testing "Sort tokens by balance in descending order"
(let [tokens [{:symbol "BTC" :balance 2}
{:symbol "ETH" :balance 5}
{:symbol "DAI" :balance 10}]
expected [{:symbol "DAI" :balance 10}
{:symbol "ETH" :balance 5}
{:symbol "BTC" :balance 2}]]
(is (= (utils/sort-tokens tokens) expected)))))
(deftest sort-tokens-by-fiat-value-test
(testing "Sort tokens by fiat value in descending order"
(let [tokens [{:symbol "BTC" :fiat-value 50000}
{:symbol "ETH" :fiat-value 15000}
{:symbol "DAI" :fiat-value 1000}]
expected [{:symbol "BTC" :fiat-value 50000}
{:symbol "ETH" :fiat-value 15000}
{:symbol "DAI" :fiat-value 1000}]]
(is (= (utils/sort-tokens-by-fiat-value tokens) expected)))))
(deftest sort-tokens-by-name-test
(testing "Sort tokens by symbol in ascending order"
(let [tokens [{:symbol "ETH"}
{:symbol "DAI"}
{:symbol "BTC"}]
expected [{:symbol "BTC"}
{:symbol "DAI"}
{:symbol "ETH"}]]
(is (= (utils/sort-tokens-by-name tokens) expected)))))

View File

@ -2,6 +2,7 @@
(:require
[clojure.string :as string]
[re-frame.core :as re-frame]
[status-im.contexts.wallet.collectible.utils :as collectible-utils]
[utils.collection]))
(defn- filter-collectibles-in-chains
@ -46,7 +47,10 @@
:wallet/current-viewing-account-collectibles
:<- [:wallet/current-viewing-account]
(fn [current-account]
(-> current-account :collectibles add-collectibles-preview-url)))
(-> current-account
:collectibles
add-collectibles-preview-url
collectible-utils/sort-collectibles-by-name)))
(re-frame/reg-sub
:wallet/current-viewing-account-collectibles-in-selected-networks
@ -71,7 +75,8 @@
(apply interleave)
(remove nil?)
(utils.collection/distinct-by :id)
(add-collectibles-preview-url)))))
(add-collectibles-preview-url)
(collectible-utils/sort-collectibles-by-name)))))
(re-frame/reg-sub
:wallet/owned-collectibles-list-in-selected-networks

View File

@ -505,30 +505,38 @@
:<- [:wallet/current-viewing-account-or-default]
:<- [:wallet/network-details]
:<- [:wallet/wallet-send]
(fn [[account networks send-data] [_ {:keys [query chain-ids hide-token-fn]}]]
:<- [:profile/currency]
:<- [:wallet/prices-per-token]
(fn [[account networks send-data currency price-per-token] [_ {:keys [query chain-ids hide-token-fn]}]]
(let [tx-type (:tx-type send-data)
tokens (->> (:tokens account)
(map
(fn [token]
(assoc token
: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)))
:supported-networks (network-utils/network-list token networks)
:available-balance (utils/calculate-total-token-balance token)
:total-balance (utils/calculate-total-token-balance
token
chain-ids))))
(let [total-balance (utils/calculate-total-token-balance
token
chain-ids)]
(assoc token
: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)))
:supported-networks (network-utils/network-list token networks)
:available-balance (utils/calculate-total-token-balance token)
:total-balance total-balance
:fiat-value (utils/calculate-token-fiat-value
{:currency currency
:balance total-balance
:token token
:prices-per-token price-per-token})))))
(filter (fn [{:keys [networks]}]
(pos? (count networks))))
(remove #(when hide-token-fn (hide-token-fn constants/swap-tokens-my %))))
sorted-tokens (utils/sort-tokens tokens)]
sorted-tokens (utils/sort-tokens-by-fiat-value tokens)]
(if query
(let [query-string (string/lower-case query)]
(filter #(or (string/starts-with? (string/lower-case (:name %)) query-string)