Skip to content

Commit

Permalink
fix and move networks and balance to a sub
Browse files Browse the repository at this point in the history
  • Loading branch information
briansztamfater committed Nov 15, 2023
1 parent 6e3102d commit 8be716c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 61 deletions.
7 changes: 7 additions & 0 deletions src/status_im2/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,13 @@
(def ^:const mainnet-network-name :ethereum)
(def ^:const optimism-network-name :optimism)
(def ^:const arbitrum-network-name :arbitrum)
(def ^:const goerli-network-name :goerli)

(def ^:const networks-map
{mainnet-chain-id mainnet-network-name
optimism-chain-id optimism-network-name
arbitrum-chain-id arbitrum-network-name
goerli-chain-id goerli-network-name})

(def ^:const chain-id-separator ":")

Expand Down
19 changes: 5 additions & 14 deletions src/status_im2/contexts/wallet/common/utils.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns status-im2.contexts.wallet.common.utils
(:require [clojure.string :as string]
[quo.foundations.resources :as quo.resources]
[status-im2.constants :as constants]
[utils.number]))

Expand Down Expand Up @@ -30,7 +29,7 @@
(/ n (Math/pow 10 (utils.number/parse-int decimals)))
0))

(defn- total-token-value-in-all-chains
(defn total-token-value-in-all-chains
[{:keys [balances-per-chain decimals]}]
(->> balances-per-chain
(vals)
Expand All @@ -46,15 +45,7 @@
(reduce +)))

(defn network-names
[token]
(let [balances-per-chain (:balancesPerChain token)]
(mapv (fn [chain-id-keyword]
(let [chain-id-str (name chain-id-keyword)
chain-id (js/parseInt chain-id-str)]
(case chain-id
10 {:source (quo.resources/get-network :optimism)}
42161 {:source (quo.resources/get-network :arbitrum)}
5 {:source (quo.resources/get-network :ethereum)}
1 {:source (quo.resources/get-network :ethereum)}
:unknown))) ; Default case if the chain-id is not recognized
(keys balances-per-chain))))
[{:keys [balances-per-chain]} networks]
(mapv (fn [chain-id]
(first (filter #(= (:chain-id %) chain-id) networks)))
(keys balances-per-chain)))
27 changes: 0 additions & 27 deletions src/status_im2/contexts/wallet/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
[quo.foundations.colors :as colors]
[react-native.background-timer :as background-timer]
[status-im2.common.data-store.wallet :as data-store]
[status-im2.contexts.wallet.common.utils :as wallet-utils]
[status-im2.contexts.wallet.temp :as temp]
[taoensso.timbre :as log]
[utils.ethereum.chain :as chain]
Expand Down Expand Up @@ -125,32 +124,6 @@
(update-in [:wallet :accounts] add-tokens tokens)
(assoc-in [:wallet :ui :tokens-loading?] false))})))

(rf/reg-event-fx :wallet/get-wallet-token-success
(fn [{:keys [db]} [tokens]]
(let [tokens
(into
{}
(map (fn [[k v]]
[k
(map (fn [token]
(let [total-balance (reduce +
(map #(js/parseFloat (:balance %))
(vals (:balancesPerChain token))))
currency :usd
price-fiat-currency (get-in token
[:marketValuesPerCurrency currency
:price])
balance-fiat (* total-balance price-fiat-currency)]
(assoc token
:networks (wallet-utils/network-names token)
:total-balance total-balance
:total-balance-fiat balance-fiat)))
v)])
tokens))]
{:db (assoc db
:wallet/tokens tokens
:wallet/tokens-loading? false)})))

(rf/defn scan-address-success
{:events [:wallet/scan-address-success]}
[{:keys [db]} address]
Expand Down
20 changes: 9 additions & 11 deletions src/status_im2/contexts/wallet/send/select_asset/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
:on-press on-press}])))

(defn- asset-list
[account-address search-text]
(let [filtered-tokens (rf/sub [:wallet/tokens-filtered account-address search-text])]
[search-text]
(let [filtered-tokens (rf/sub [:wallet/tokens-filtered search-text])]
[rn/flat-list
{:data filtered-tokens
:style {:flex 1}
Expand All @@ -43,9 +43,9 @@
:render-fn asset-component}]))

(defn- tab-view
[account search-text selected-tab]
[search-text selected-tab]
(case selected-tab
:tab/assets [asset-list account search-text]
:tab/assets [asset-list search-text]
:tab/collectibles [quo/empty-state
{:title (i18n/label :t/no-collectibles)
:description (i18n/label :t/no-collectibles-description)
Expand All @@ -66,12 +66,10 @@

(defn- f-view-internal
[]
(let [{:keys [address]} (rf/sub [:wallet/current-viewing-account])
selected-tab (reagent/atom (:id (first tabs-data)))
search-text (reagent/atom "")
account-address (string/lower-case address)
on-close #(rf/dispatch [:navigate-back-within-stack
:wallet-select-asset])]
(let [selected-tab (reagent/atom (:id (first tabs-data)))
search-text (reagent/atom "")
on-close #(rf/dispatch [:navigate-back-within-stack
:wallet-select-asset])]
(fn []
[rn/safe-area-view {:style {:flex 1}}
[rn/scroll-view
Expand Down Expand Up @@ -101,7 +99,7 @@
:data tabs-data
:on-change #(reset! selected-tab %)}]
[search-input search-text]
[tab-view account-address @search-text @selected-tab]]])))
[tab-view @search-text @selected-tab]]])))

(defn- view-internal
[]
Expand Down
27 changes: 18 additions & 9 deletions src/status_im2/subs/wallet/wallet.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,22 @@

(rf/reg-sub
:wallet/tokens-filtered
:<- [:wallet/tokens]
(fn [tokens [_ account-address query]]
(let [account-tokens (get tokens (keyword account-address))
sorted-tokens (sort-by :name compare account-tokens)
filtered-tokens (filter #(or (string/starts-with? (string/lower-case (:name %))
(string/lower-case query))
(string/starts-with? (string/lower-case (:symbol %))
(string/lower-case query)))
sorted-tokens)]
:<- [:wallet/current-viewing-account]
:<- [:wallet/network-details]
(fn [[account networks] [_ query]]
(let [tokens (map (fn [token]
(assoc token
:networks (utils/network-names token networks)
:total-balance (utils/total-token-value-in-all-chains token)
:total-balance-fiat (utils/calculate-balance token)))
(:tokens account))

sorted-tokens
(sort-by :name compare tokens)
filtered-tokens
(filter #(or (string/starts-with? (string/lower-case (:name %))
(string/lower-case query))
(string/starts-with? (string/lower-case (:symbol %))
(string/lower-case query)))
sorted-tokens)]
filtered-tokens)))

0 comments on commit 8be716c

Please sign in to comment.