ref: using changes in :wallet/tokens-filtered

This commit is contained in:
Cristian Lungu 2024-10-25 14:37:53 +03:00
parent 42a2b86723
commit f248265182
No known key found for this signature in database
GPG Key ID: 00D675EDE1B264D9
3 changed files with 89 additions and 44 deletions

View File

@ -57,24 +57,36 @@
(defn normalize-market-values (defn normalize-market-values
[market-values] [market-values]
(reduce (fn [acc [token-symbol data]] (reduce-kv (fn [acc token-symbol data]
(assoc acc (assoc acc
token-symbol token-symbol
{:market-cap (:MKTCAP data) {:market-cap (:MKTCAP data)
:high-day (:HIGHDAY data) :high-day (:HIGHDAY data)
:low-day (:LOWDAY data) :low-day (:LOWDAY data)
:change-pct-hour (:CHANGEPCTHOUR data) :change-pct-hour (:CHANGEPCTHOUR data)
:change-pct-day (:CHANGEPCTDAY data) :change-pct-day (:CHANGEPCTDAY data)
:change-pct-24h (:CHANGEPCT24HOUR data) :change-pct-24h (:CHANGEPCT24HOUR data)
:change-24h (:CHANGE24HOUR data)})) :change-24h (:CHANGE24HOUR data)}))
{} {}
market-values)) market-values))
(defn normalize-token-details (defn normalize-token-details
[token-details] [token-details]
(reduce (fn [acc [token-symbol data]] (reduce-kv (fn [acc token-symbol data]
(->> data (->> data
(cske/transform-keys transforms/->kebab-case-keyword) (cske/transform-keys transforms/->kebab-case-keyword)
(assoc acc token-symbol))) (assoc acc token-symbol)))
{} {}
token-details)) token-details))
(defn normalize-prices
[token-prices]
(reduce-kv
(fn [acc k v]
(->> v
(map (fn [[currency price]]
[currency {:price price}]))
(into {})
(assoc acc k)))
{}
token-prices))

View File

@ -25,7 +25,8 @@
(defn fetch-prices (defn fetch-prices
[symbols currencies] [symbols currencies]
(-> (rpc-events/call-async "wallet_fetchPrices" true symbols currencies) (-> (rpc-events/call-async "wallet_fetchPrices" true symbols currencies)
(promesa/then transforms/js->clj))) (promesa/then transforms/js->clj)
(promesa/then tokens-data/normalize-prices)))
(defn fetch-additional-token-data (defn fetch-additional-token-data
[symbols currency] [symbols currency]

View File

@ -474,33 +474,65 @@
sorted-tokens)) sorted-tokens))
sorted-tokens)))) sorted-tokens))))
;; NOTE: ideally we just pass the symbols list and individual components get the details themselves
(rf/reg-sub
:wallet.swap/popular-token-symbols
:<- [:wallet/tokens]
(fn [{:keys [symbol-lists tokens-by-symbol]} [_ chain-id query]]
(when symbol-lists
(let [tokens (->> (get symbol-lists chain-id)
(mapv (fn [token-symbol]
{:type constants/swap-tokens-popular
:name (get-in tokens-by-symbol [chain-id (keyword token-symbol) :name])
:symbol token-symbol})))]
(if query
(let [query-string (string/lower-case query)]
(filter #(or (string/starts-with? (string/lower-case (:name %)) query-string)
(string/starts-with? (string/lower-case (str (:symbol %))) query-string))
tokens))
tokens)))))
(rf/reg-sub
:wallet.swap/get-popular-token-data
:<- [:wallet/tokens]
(fn [{:keys [tokens-by-symbol market-values-by-symbol prices-by-symbol details-by-symbol]}
[_ chain-id token-symbol]]
(let [token-symbol (keyword token-symbol)]
(-> tokens-by-symbol
(get-in [chain-id token-symbol])
(assoc :market-values (get market-values-by-symbol token-symbol)
:details (get details-by-symbol token-symbol)
:market-values-per-currency (get prices-by-symbol token-symbol))))))
(-> (rf/subscribe [:wallet.swap/popular-token-symbols 1 "ETH"])
deref)
;; NOTE: even with this structure, we don't have to filter out the chain-ids, as they are already
;; split by chain-ids
(rf/reg-sub
:wallet/all-tokens-by-chain-id
:<- [:wallet/tokens]
(fn [{:keys [symbol-lists tokens-by-symbol market-values-by-symbol prices-by-symbol details-by-symbol]}
[_ chain-id]]
(->> (get symbol-lists chain-id)
(map (fn [token-symbol]
(-> tokens-by-symbol
(get-in [chain-id (keyword token-symbol)])
(assoc :market-values (get market-values-by-symbol token-symbol)
:details (get details-by-symbol token-symbol)
:market-values-per-currency (get prices-by-symbol token-symbol))))))))
(rf/reg-sub (rf/reg-sub
:wallet/tokens-filtered :wallet/tokens-filtered
:<- [:wallet/tokens] (fn [[_ {:keys [chain-ids]}]]
:<- [:profile/currency] (rf/subscribe [:wallet/all-tokens-by-chain-id (first chain-ids)]))
(fn [[{:keys [by-symbol market-values-per-token details-per-token prices-per-token]} currency] (fn [tokens [_ {:keys [query]}]]
[_ {:keys [query chain-ids hide-token-fn]}]] (if query
(let [tokens (->> by-symbol (let [query-string (string/lower-case query)]
(map (fn [token] (filter #(or (string/starts-with? (string/lower-case (:name %)) query-string)
(let [token-symbol (keyword (:symbol token)) (string/starts-with? (string/lower-case (:symbol %)) query-string))
price (get-in prices-per-token [token-symbol currency])] tokens))
(-> token tokens)))
(assoc :market-values
(get market-values-per-token token-symbol))
(assoc :details (get details-per-token token-symbol))
(assoc-in [:market-values-per-currency currency :price]
price)))))
(filter (fn [{:keys [chain-id]}]
(some #{chain-id} chain-ids)))
(remove #(when hide-token-fn
(hide-token-fn constants/swap-tokens-popular %))))
sorted-tokens (utils/sort-tokens-by-name tokens)]
(if query
(let [query-string (string/lower-case query)]
(filter #(or (string/starts-with? (string/lower-case (:name %)) query-string)
(string/starts-with? (string/lower-case (:symbol %)) query-string))
sorted-tokens))
sorted-tokens))))
(rf/reg-sub (rf/reg-sub
:wallet/token-by-symbol :wallet/token-by-symbol