Skip to content

Commit

Permalink
some refactor on utils and function and variable naming
Browse files Browse the repository at this point in the history
  • Loading branch information
briansztamfater committed Apr 9, 2024
1 parent 63fd2a1 commit 13460f4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
14 changes: 0 additions & 14 deletions src/status_im/contexts/wallet/common/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,3 @@
{}
tokens)
(update-vals #(prettify-balance currency-symbol %))))

(defn resolve-receiver-networks
[{:keys [prefix testnet-enabled? goerli-enabled?]}]
(let [prefix (if (string/blank? prefix)
constants/default-multichain-address-prefix
prefix)
prefix-seq (string/split prefix #":")]
(->> prefix-seq
(remove string/blank?)
(mapv
#(network->chain-id
{:network %
:testnet-enabled? testnet-enabled?
:goerli-enabled? goerli-enabled?})))))
18 changes: 18 additions & 0 deletions src/status_im/contexts/wallet/common/utils/networks.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns status-im.contexts.wallet.common.utils.networks
(:require [clojure.string :as string]
[status-im.constants :as constants]
[status-im.contexts.wallet.common.utils :as utils]))

(defn resolve-receiver-networks
[{:keys [prefix testnet-enabled? goerli-enabled?]}]
(let [prefix (if (string/blank? prefix)
constants/default-multichain-address-prefix
prefix)
prefix-seq (string/split prefix #":")]
(->> prefix-seq
(remove string/blank?)
(mapv
#(utils/network->chain-id
{:network %
:testnet-enabled? testnet-enabled?
:goerli-enabled? goerli-enabled?})))))
3 changes: 2 additions & 1 deletion src/status_im/contexts/wallet/send/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[native-module.core :as native-module]
[status-im.constants :as constants]
[status-im.contexts.wallet.common.utils :as utils]
[status-im.contexts.wallet.common.utils.networks :as network-utils]
[status-im.contexts.wallet.send.utils :as send-utils]
[taoensso.timbre :as log]
[utils.address :as address]
Expand Down Expand Up @@ -85,7 +86,7 @@
(let [[prefix to-address] (utils/split-prefix-and-address address)
testnet-enabled? (get-in db [:profile/profile :test-networks-enabled?])
goerli-enabled? (get-in db [:profile/profile :is-goerli-enabled?])
selected-networks (utils/resolve-receiver-networks
selected-networks (network-utils/resolve-receiver-networks
{:prefix prefix
:testnet-enabled? testnet-enabled?
:goerli-enabled? goerli-enabled?})]
Expand Down
46 changes: 26 additions & 20 deletions src/status_im/contexts/wallet/send/routes/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[utils.re-frame :as rf]
[utils.vector :as vector-utils]))

(def ^:private network-priority
(def ^:private network-priority-score
{:ethereum 1
:optimism 2
:arbitrum 3})
Expand All @@ -32,28 +32,34 @@
:checked? (some #(= % chain-id) @network-preferences)
:on-change on-change}})

(defn- find-insertion-index
[data chain-id loading-suggested-routes?]
(let [network (utils/id->network chain-id)
new-item-priority (network-priority network)]
(or (->> data
(keep-indexed (fn [idx item]
(let [item-network (utils/id->network (if loading-suggested-routes?
item
(get-in item [:from :chain-id])))]
(when (> (network-priority item-network) new-item-priority)
(defn- find-network-link-insertion-index
[network-links chain-id loading-suggested-routes?]
(let [network (utils/id->network chain-id)
inserted-network-link-priority-score (network-priority-score network)]
(or (->> network-links
(keep-indexed (fn [idx network-link]
(let [network-link (utils/id->network (if loading-suggested-routes?
network-link
(get-in network-link
[:from :chain-id])))]
(when (> (network-priority-score network-link)
inserted-network-link-priority-score)
idx))))
first)
(count data))))
(count network-links))))

(defn- add-disabled-networks
[data disabled-from-networks loading-suggested-routes?]
(let [sorted-networks (sort-by (comp network-priority utils/id->network) disabled-from-networks)]
(reduce (fn [acc-data chain-id]
(let [index (find-insertion-index acc-data chain-id loading-suggested-routes?)
item {:status :disabled :chain-id chain-id :network (utils/id->network chain-id)}]
(vector-utils/insert-element-at acc-data item index)))
data
[network-links disabled-from-networks loading-suggested-routes?]
(let [sorted-networks (sort-by (comp network-priority-score utils/id->network) disabled-from-networks)]
(reduce (fn [acc-network-links chain-id]
(let [index (find-network-link-insertion-index acc-network-links
chain-id
loading-suggested-routes?)
disabled-network-link {:status :disabled
:chain-id chain-id
:network (utils/id->network chain-id)}]
(vector-utils/insert-element-at acc-network-links disabled-network-link index)))
network-links
sorted-networks)))

(defn networks-drawer
Expand Down Expand Up @@ -151,7 +157,7 @@
[item index _
{:keys [from-values-by-chain to-values-by-chain theme fetch-routes on-press-from-network
on-press-to-network token-symbol loading-suggested-routes?]}]
(let [first-item? (= index 0)
(let [first-item? (zero? index)
disabled-network? (= (:status item) :disabled)
from-chain-id (get-in item [:from :chain-id])
to-chain-id (get-in item [:to :chain-id])
Expand Down

0 comments on commit 13460f4

Please sign in to comment.