Skip to content

Commit

Permalink
Account networks sub
Browse files Browse the repository at this point in the history
Check chains

Save network-preferences

Improvements

Fix linter issues

Clean up

Updates

Update sub test

Update tests

Updates

Store as set

Fix minor issues

Update naming

Revive `get-account-by-address`
  • Loading branch information
Rende11 committed Nov 16, 2023
1 parent 2e0643f commit 74674c0
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

(defn view
[]
(let [{:keys [name color emoji address]} (rf/sub [:wallet/current-viewing-account])]
(let [{:keys [name color emoji address]} (rf/sub [:wallet/current-viewing-account])
network-preference-details (rf/sub [:wallet/network-preference-details])]
[:<>
[quo/drawer-top
{:title name
:type :account
:networks [{:network-name :ethereum :short-name "eth"}
{:network-name :optimism :short-name "opt"}
{:network-name :arbitrum :short-name "arb1"}]
:networks network-preference-details
:description address
:account-avatar-emoji emoji
:customization-color color}]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,94 @@
(ns status-im2.contexts.wallet.common.sheets.network-preferences.view
(:require [quo.core :as quo]
(:require [clojure.string :as string]
[quo.core :as quo]
[quo.foundations.colors :as colors]
[quo.foundations.resources :as resources]
[quo.theme :as quo.theme]
[reagent.core :as reagent]
[status-im2.contexts.wallet.common.sheets.network-preferences.style :as style]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))

(defn- mainnet
[account-color]
[{:title "Mainnet"
:image :icon-avatar
:image-props {:icon (resources/get-network :ethereum)
:size :size-20}
:action :selector
:action-props {:type :checkbox
:customization-color account-color}}])

(defn- networks-list
[account-color]
[{:title "Optimism"
:image :icon-avatar
:image-props {:icon (resources/get-network :optimism)
:size :size-20}
:action :selector
:action-props {:type :checkbox
:customization-color account-color}}
{:title "Arbitrum"
:image :icon-avatar
:image-props {:icon (resources/get-network :arbitrum)
:size :size-20}
:action :selector
:action-props {:type :checkbox
:customization-color account-color}}])
(defn- make-network-item
[{:keys [network-name] :as _network}
{:keys [color on-change network-preferences] :as _options}]
{:title (string/capitalize (name network-name))
:image :icon-avatar
:image-props {:icon (resources/get-network network-name)
:size :size-20}
:action :selector
:action-props {:type :checkbox
:customization-color color
:checked? (contains? network-preferences network-name)
:on-change on-change}})

(defn- view-internal
[{:keys [on-save theme]}]
(let [{:keys [color address]} (rf/sub [:wallet/current-viewing-account])]
[:<>
[quo/drawer-top
{:title (i18n/label :t/network-preferences)
:description (i18n/label :t/network-preferences-desc)}]
[quo/data-item
{:status :default
:size :default
:description :default
:label :none
:blur? false
:card? true
:title (i18n/label :t/address)
:subtitle address
:container-style (merge style/data-item
{:background-color (colors/theme-colors colors/neutral-2_5
colors/neutral-90
theme)})}]
[quo/category
{:list-type :settings
:data (mainnet color)}]
[quo/category
{:list-type :settings
:label (i18n/label :t/layer-2)
:data (networks-list color)}]
[quo/bottom-actions
{:button-one-label (i18n/label :t/update)
:button-one-props {:disabled? true
:on-press on-save
:customization-color color}}]]))
[]
(let [{:keys [color address
network-preferences-names]} (rf/sub [:wallet/current-viewing-account])
network-preferences-names-state (reagent/atom network-preferences-names)
toggle-network (fn [network-name]
(if (contains? @network-preferences-names-state
network-name)
(swap! network-preferences-names-state disj
network-name)
(swap! network-preferences-names-state conj
network-name)))]
(fn [{:keys [on-save theme]}]
(let [network-details (rf/sub [:wallet/network-details])
mainnet (first network-details)
layer-2-networks (rest network-details)
current-networks (filter (fn [network]
(contains? @network-preferences-names-state
(:network-name network)))
network-details)]
[:<>
[quo/drawer-top
{:title (i18n/label :t/network-preferences)
:description (i18n/label :t/network-preferences-desc)}]
[quo/data-item
{:status :default
:size :default
:description :default
:label :none
:blur? false
:card? true
:title (i18n/label :t/address)
:custom-subtitle (fn []
[quo/address-text
{:networks current-networks
:address address
:format :long}])
:container-style (merge style/data-item
{:background-color (colors/theme-colors colors/neutral-2_5
colors/neutral-90
theme)})}]

[quo/category
{:list-type :settings
:data [(make-network-item mainnet
{:color color
:network-preferences @network-preferences-names-state
:on-change #(toggle-network (:network-name
mainnet))})]}]
[quo/category
{:list-type :settings
:label (i18n/label :t/layer-2)
:data (mapv (fn [network]
(make-network-item network
{:color color
:network-preferences @network-preferences-names-state
:on-change #(toggle-network (:network-name
network))}))
layer-2-networks)}]
[quo/bottom-actions
{:button-one-label (i18n/label :t/update)
:button-one-props {:disabled? (= @network-preferences-names-state
network-preferences-names)
:on-press (fn []
(let [chain-ids (map :chain-id current-networks)]
(on-save chain-ids)))
:customization-color color}}]]))))

(def view (quo.theme/with-theme view-internal))
5 changes: 5 additions & 0 deletions src/status_im2/contexts/wallet/common/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
(defn get-derivation-path
[number-of-accounts]
(str constants/path-wallet-root "/" number-of-accounts))

(defn format-derivation-path
[path]
(string/replace path "/" " / "))
Expand All @@ -23,6 +24,10 @@
(let [path (get-derivation-path number-of-accounts)]
(format-derivation-path path)))

(defn get-account-by-address
[accounts address]
(some #(when (= (:address %) address) %) accounts))

(defn- calculate-raw-balance
[raw-balance decimals]
(if-let [n (utils.number/parse-int raw-balance nil)]
Expand Down
29 changes: 17 additions & 12 deletions src/status_im2/contexts/wallet/edit_account/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
:name :t/edit-wallet-account-name-updated-message
:color :t/edit-wallet-account-colour-updated-message
:emoji :t/edit-wallet-account-emoji-updated-message
:prod-preferred-chain-ids :t/edit-wallet-network-preferences-updated-message
nil)]
(rf/dispatch [:toasts/upsert
{:id :edit-account
Expand Down Expand Up @@ -55,10 +56,11 @@
:new-value @edited-account-name
:theme theme}))]
(fn []
(let [{:keys [name emoji address color]
:as account} (rf/sub [:wallet/current-viewing-account])
account-name (or @edited-account-name name)
button-disabled? (or (nil? @edited-account-name) (= name @edited-account-name))]
(let [{:keys [name emoji address color] :as account} (rf/sub [:wallet/current-viewing-account])
network-details (rf/sub [:wallet/network-preference-details])
account-name (or @edited-account-name name)
button-disabled? (or (nil? @edited-account-name)
(= name @edited-account-name))]
[create-or-edit-account/view
{:page-nav-right-side [{:icon-name :i/delete
:on-press #(js/alert "Delete account: to be implemented")}]
Expand Down Expand Up @@ -86,18 +88,21 @@
:right-icon :i/advanced
:card? true
:title (i18n/label :t/address)
:custom-subtitle (fn []
[quo/address-text
{:networks [{:network-name :ethereum :short-name "eth"}
{:network-name :optimism :short-name "opt"}
{:network-name :arbitrum :short-name "arb1"}]
:address address
:format :long}])
:custom-subtitle (fn [] [quo/address-text
{:networks network-details
:address address
:format :long}])
:on-press (fn []
(rf/dispatch [:show-bottom-sheet
{:content (fn []
[network-preferences/view
{:on-save #(js/alert "calling on save")}])}]))
{:on-save (fn [chain-ids]
(rf/dispatch [:hide-bottom-sheet])
(save-account
{:account account
:updated-key :prod-preferred-chain-ids
:new-value chain-ids
:theme theme}))}])}]))
:container-style style/data-item}]]))))

(def view (quo.theme/with-theme view-internal))
16 changes: 9 additions & 7 deletions src/status_im2/subs/wallet/networks.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
:wallet/network-details
:<- [:wallet/filtered-networks-by-mode false]
(fn [networks]
(keep
(fn [{:keys [chain-id related-chain-id test?]}]
(let [network-details (get network-list (if test? related-chain-id chain-id))]
(assoc network-details
:chain-id chain-id
:related-chain-id related-chain-id)))
networks)))
(->> networks
(keep
(fn [{:keys [chain-id related-chain-id layer test?]}]
(let [network-details (get network-list (if test? related-chain-id chain-id))]
(assoc network-details
:chain-id chain-id
:related-chain-id related-chain-id
:layer layer))))
(sort-by (juxt :layer :short-name)))))
27 changes: 18 additions & 9 deletions src/status_im2/subs/wallet/networks_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,43 @@
{:test [{:test? true
:short-name "eth"
:network-name :ethereum
:related-chain-id 1}
:related-chain-id 1
:layer 1}
{:test? true
:short-name "arb1"
:related-chain-id 42161}
:related-chain-id 42161
:layer 2}
{:test? true
:short-name "opt"
:related-chain-id 10}]
:related-chain-id 10
:layer 2}]
:prod [{:test? false
:short-name "eth"
:chain-id 1}
:chain-id 1
:layer 1}
{:test? false
:short-name "arb1"
:chain-id 42161}
:chain-id 42161
:layer 2}
{:test? false
:short-name "opt"
:chain-id 10}]})
:chain-id 10
:layer 2}]})

(h/deftest-sub :wallet/network-details
[sub-name]
(testing "returns data with prod"
(swap! rf-db/app-db assoc :wallet/networks network-data)
(is (= [{:network-name :ethereum
:short-name "eth"
:chain-id 1}
:chain-id 1
:layer 1}
{:network-name :arbitrum
:short-name "arb1"
:chain-id 42161}
:chain-id 42161
:layer 2}
{:network-name :optimism
:short-name "opt"
:chain-id 10}]
:chain-id 10
:layer 2}]
(map #(dissoc % :source :related-chain-id) (rf/sub [sub-name]))))))
58 changes: 49 additions & 9 deletions src/status_im2/subs/wallet/wallet.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
[status-im2.contexts.wallet.common.utils :as utils]
[utils.number]))

(defn- filter-networks
[chain-ids network-details]
(filter (fn [{:keys [chain-id]}]
(contains? chain-ids chain-id))
network-details))

(defn- assoc-network-preferences-names
[network-details account testnet?]
(let [{:keys [prod-preferred-chain-ids
test-preferred-chain-ids]} account
current-chain-ids (if testnet?
test-preferred-chain-ids
prod-preferred-chain-ids)
network-preferences-names (->> network-details
(filter-networks current-chain-ids)
(map :network-name)
(set))]
(assoc account :network-preferences-names network-preferences-names)))

(rf/reg-sub
:wallet/ui
:<- [:wallet]
Expand All @@ -13,13 +32,25 @@
:<- [:wallet/ui]
:-> :tokens-loading?)


(rf/reg-sub
:wallet/current-viewing-account-address
:<- [:wallet]
:-> :current-viewing-account-address)

(rf/reg-sub
:wallet/accounts
:<- [:wallet]
:-> #(->> %
:accounts
vals
(sort-by :position)))
:<- [:wallet/network-details]
(fn [[wallet network-details]]
;; TODO(@rende11): `testnet?` value would be relevant after this implementation,
;; https://github.com/status-im/status-mobile/issues/17826
(let [testnet? false]
(->> wallet
:accounts
vals
(map #(assoc-network-preferences-names network-details % testnet?))
(sort-by :position)))))

(rf/reg-sub
:wallet/balances
Expand All @@ -45,9 +76,18 @@

(rf/reg-sub
:wallet/current-viewing-account
:<- [:wallet]
:<- [:wallet/accounts]
:<- [:wallet/current-viewing-account-address]
:<- [:wallet/balances]
(fn [[{:keys [current-viewing-account-address] :as wallet} balances]]
(-> wallet
(get-in [:accounts current-viewing-account-address])
(assoc :balance (get balances current-viewing-account-address)))))
(fn [[accounts current-viewing-account-address balances]]
(let [current-viewing-account (utils/get-account-by-address accounts current-viewing-account-address)]
(-> current-viewing-account
(assoc :balance (get balances current-viewing-account-address))))))

(rf/reg-sub
:wallet/network-preference-details
:<- [:wallet/current-viewing-account]
:<- [:wallet/network-details]
(fn [[current-viewing-account network-details]]
(let [network-preferences-names (:network-preferences-names current-viewing-account)]
(filter #(contains? network-preferences-names (:network-name %)) network-details))))
Loading

0 comments on commit 74674c0

Please sign in to comment.