Skip to content

Commit

Permalink
Wallet - Initialize event, refactor networks data and bug fix (#18374)
Browse files Browse the repository at this point in the history
This commit:

- Introduces new event ":wallet/initialize" to group events that need to be called on login
- Refactors wallet networks to use the nested structure in re-frame
- Fixes a bug when updating the testnet network preferences of an account the production network preferences update

---------

Signed-off-by: Mohamed Javid <19339952+smohamedjavid@users.noreply.github.com>
  • Loading branch information
smohamedjavid authored Jan 10, 2024
1 parent eb53bbe commit 5a707c0
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 42 deletions.
12 changes: 5 additions & 7 deletions src/status_im/contexts/profile/login/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@
(assoc :chats/loading? true
:networks/current-network current-network
:networks/networks (merge networks config/default-networks-by-id)
:profile/profile (merge profile-overview settings))
(assoc-in [:wallet :ui :tokens-loading?] true))
:fx [[:dispatch [:wallet/get-ethereum-chains]]
[:dispatch [:universal-links/generate-profile-url]]
[:dispatch [:community/fetch]]]}
:profile/profile (merge profile-overview settings)))
:fx [[:dispatch [:universal-links/generate-profile-url]]
[:dispatch [:community/fetch]]
[:dispatch [:wallet/initialize]]]}
(notifications/load-preferences)
(data-store.chats/fetch-chats-preview
{:on-success
Expand Down Expand Up @@ -125,8 +124,7 @@
:on-error #(log/error
"failed to start messenger")}]
:check-eip1559-activation {:network-id network-id}
:effects.profile/enable-local-notifications nil
:dispatch-n [[:wallet/get-accounts]]}
:effects.profile/enable-local-notifications nil}
(not (:universal-links/handling db))
(assoc :effects.chat/open-last-chat (get-in db [:profile/profile :key-uid]))
notifications-enabled?
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/contexts/wallet/data_store.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
(update-vals #(cske/transform-keys csk/->kebab-case %))
(update-vals #(mapv rpc->balances-per-chain %))))

(defn <-rpc
(defn rpc->network
[network]
(-> network
(set/rename-keys
Expand Down
25 changes: 15 additions & 10 deletions src/status_im/contexts/wallet/edit_account/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
(defn- show-save-account-toast
[updated-key]
(let [message (case updated-key
: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
: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
:test-preferred-chain-ids) :t/edit-wallet-network-preferences-updated-message
nil)]
(rf/dispatch [:toasts/upsert
{:id :edit-account
Expand Down Expand Up @@ -50,11 +51,15 @@
:new-value @edited-account-name}))]
(fn []
(let [{:keys [name emoji address color watch-only?]
: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))]
:as account} (rf/sub [:wallet/current-viewing-account])
network-details (rf/sub [:wallet/network-preference-details])
test-networks-enabled? (rf/sub [:profile/test-networks-enabled?])
network-preferences-key (if test-networks-enabled?
:test-preferred-chain-ids
:prod-preferred-chain-ids)
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 @@ -95,7 +100,7 @@
(rf/dispatch [:hide-bottom-sheet])
(save-account
{:account account
:updated-key :prod-preferred-chain-ids
:updated-key network-preferences-key
:new-value chain-ids}))
:watch-only? watch-only?}])}]))
:container-style style/data-item}]]))))
10 changes: 7 additions & 3 deletions src/status_im/contexts/wallet/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@
(let [network-data
{:test (map #(->> %
:Test
data-store/<-rpc)
data-store/rpc->network)
data)
:prod (map #(->> %
:Prod
data-store/<-rpc)
data-store/rpc->network)
data)}]
{:db (assoc db :wallet/networks network-data)})))
{:db (assoc-in db [:wallet :networks] network-data)})))

(rf/reg-event-fx :wallet/find-ens
(fn [{:keys [db]} [input contacts chain-id cb]]
Expand Down Expand Up @@ -327,3 +327,7 @@
(fn [_ [explorer-link address]]
{:fx [[:dispatch [:hide-bottom-sheet]]
[:dispatch [:browser.ui/open-url (str explorer-link "/" address)]]]}))

(rf/reg-event-fx :wallet/initialize
(fn []
{:fx [[:dispatch-n [[:wallet/get-ethereum-chains] [:wallet/get-accounts]]]]}))
1 change: 0 additions & 1 deletion src/status_im/subs/profile.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
(fn [profile]
(:test-networks-enabled? profile)))


(re-frame/reg-sub
:multiaccount/contact
:<- [:profile/profile]
Expand Down
1 change: 0 additions & 1 deletion src/status_im/subs/root.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@
(reg-root-key-sub :wallet :wallet)
(reg-root-key-sub :wallet/scanned-address :wallet/scanned-address)
(reg-root-key-sub :wallet/create-account :wallet/create-account)
(reg-root-key-sub :wallet/networks :wallet/networks)
(reg-root-key-sub :wallet/local-suggestions :wallet/local-suggestions)
(reg-root-key-sub :wallet/valid-ens-or-address? :wallet/valid-ens-or-address?)

Expand Down
14 changes: 10 additions & 4 deletions src/status_im/subs/wallet/networks.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
[status-im.constants :as constants]))

(re-frame/reg-sub
:wallet/filtered-networks-by-mode
:wallet/networks
:<- [:wallet]
:-> :networks)

(re-frame/reg-sub
:wallet/networks-by-mode
:<- [:wallet/networks]
(fn [networks [_ test?]]
(get networks (if test? :test :prod))))
:<- [:profile/test-networks-enabled?]
(fn [[networks test-networks-enabled?]]
(get networks (if test-networks-enabled? :test :prod))))

(def mainnet-network-details
{:source (resources/get-network constants/mainnet-network-name)
Expand All @@ -32,7 +38,7 @@

(re-frame/reg-sub
:wallet/network-details
:<- [:wallet/filtered-networks-by-mode false]
:<- [:wallet/networks-by-mode]
(fn [networks]
(->> networks
(keep
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/subs/wallet/networks_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
(h/deftest-sub :wallet/network-details
[sub-name]
(testing "returns data with prod"
(swap! rf-db/app-db assoc :wallet/networks network-data)
(swap! rf-db/app-db assoc-in [:wallet :networks] network-data)
(is (= [{:network-name :ethereum
:short-name "eth"
:chain-id 1
Expand Down
16 changes: 7 additions & 9 deletions src/status_im/subs/wallet/wallet.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,13 @@
:wallet/accounts
:<- [:wallet]
:<- [: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)))))
:<- [:profile/test-networks-enabled?]
(fn [[wallet network-details test-networks-enabled?]]
(->> wallet
:accounts
vals
(map #(assoc-network-preferences-names network-details % test-networks-enabled?))
(sort-by :position))))

(rf/reg-sub
:wallet/addresses
Expand Down
10 changes: 5 additions & 5 deletions src/status_im/subs/wallet/wallet_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
(swap! rf-db/app-db
#(-> %
(assoc-in [:wallet :accounts] accounts)
(assoc :wallet/networks network-data)))
(assoc-in [:wallet :networks] network-data)))
(is
(=
(list {:path "m/44'/60'/0'/0/0"
Expand Down Expand Up @@ -252,7 +252,7 @@
#(-> %
(assoc-in [:wallet :accounts] accounts)
(assoc-in [:wallet :current-viewing-account-address] "0x1")
(assoc :wallet/networks network-data)))
(assoc-in [:wallet :networks] network-data)))

(let [result (rf/sub [sub-name])]
(is
Expand Down Expand Up @@ -310,7 +310,7 @@
#(-> %
(assoc-in [:wallet :accounts] accounts)
(assoc-in [:wallet :current-viewing-account-address] "0x2")
(assoc :wallet/networks network-data)))
(assoc-in [:wallet :networks] network-data)))
(is
(= (list
{:path "m/44'/60'/0'/0/0"
Expand Down Expand Up @@ -365,7 +365,7 @@
(swap! rf-db/app-db
#(-> %
(assoc-in [:wallet :accounts] accounts)
(assoc :wallet/networks network-data)))
(assoc-in [:wallet :networks] network-data)))
(is
(= (list
{:path "m/44'/60'/0'/0/0"
Expand Down Expand Up @@ -421,7 +421,7 @@
#(-> %
(assoc-in [:wallet :accounts] accounts)
(assoc-in [:wallet :current-viewing-account-address] "0x1")
(assoc :wallet/networks network-data)))
(assoc-in [:wallet :networks] network-data)))
(is
(match? [{:short-name "eth"
:network-name :ethereum
Expand Down

0 comments on commit 5a707c0

Please sign in to comment.