Wallet - Initialize event, refactor networks data and bug fix (#18374)

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>
This commit is contained in:
Mohamed Javid 2024-01-10 18:47:24 +05:30 committed by GitHub
parent eb53bbe4ec
commit 5a707c0dca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 51 additions and 42 deletions

View File

@ -90,11 +90,10 @@
(assoc :chats/loading? true (assoc :chats/loading? true
:networks/current-network current-network :networks/current-network current-network
:networks/networks (merge networks config/default-networks-by-id) :networks/networks (merge networks config/default-networks-by-id)
:profile/profile (merge profile-overview settings)) :profile/profile (merge profile-overview settings)))
(assoc-in [:wallet :ui :tokens-loading?] true)) :fx [[:dispatch [:universal-links/generate-profile-url]]
:fx [[:dispatch [:wallet/get-ethereum-chains]] [:dispatch [:community/fetch]]
[:dispatch [:universal-links/generate-profile-url]] [:dispatch [:wallet/initialize]]]}
[:dispatch [:community/fetch]]]}
(notifications/load-preferences) (notifications/load-preferences)
(data-store.chats/fetch-chats-preview (data-store.chats/fetch-chats-preview
{:on-success {:on-success
@ -125,8 +124,7 @@
:on-error #(log/error :on-error #(log/error
"failed to start messenger")}] "failed to start messenger")}]
:check-eip1559-activation {:network-id network-id} :check-eip1559-activation {:network-id network-id}
:effects.profile/enable-local-notifications nil :effects.profile/enable-local-notifications nil}
:dispatch-n [[:wallet/get-accounts]]}
(not (:universal-links/handling db)) (not (:universal-links/handling db))
(assoc :effects.chat/open-last-chat (get-in db [:profile/profile :key-uid])) (assoc :effects.chat/open-last-chat (get-in db [:profile/profile :key-uid]))
notifications-enabled? notifications-enabled?

View File

@ -64,7 +64,7 @@
(update-vals #(cske/transform-keys csk/->kebab-case %)) (update-vals #(cske/transform-keys csk/->kebab-case %))
(update-vals #(mapv rpc->balances-per-chain %)))) (update-vals #(mapv rpc->balances-per-chain %))))
(defn <-rpc (defn rpc->network
[network] [network]
(-> network (-> network
(set/rename-keys (set/rename-keys

View File

@ -16,7 +16,8 @@
:name :t/edit-wallet-account-name-updated-message :name :t/edit-wallet-account-name-updated-message
:color :t/edit-wallet-account-colour-updated-message :color :t/edit-wallet-account-colour-updated-message
:emoji :t/edit-wallet-account-emoji-updated-message :emoji :t/edit-wallet-account-emoji-updated-message
:prod-preferred-chain-ids :t/edit-wallet-network-preferences-updated-message (:prod-preferred-chain-ids
:test-preferred-chain-ids) :t/edit-wallet-network-preferences-updated-message
nil)] nil)]
(rf/dispatch [:toasts/upsert (rf/dispatch [:toasts/upsert
{:id :edit-account {:id :edit-account
@ -52,6 +53,10 @@
(let [{:keys [name emoji address color watch-only?] (let [{:keys [name emoji address color watch-only?]
:as account} (rf/sub [:wallet/current-viewing-account]) :as account} (rf/sub [:wallet/current-viewing-account])
network-details (rf/sub [:wallet/network-preference-details]) 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) account-name (or @edited-account-name name)
button-disabled? (or (nil? @edited-account-name) button-disabled? (or (nil? @edited-account-name)
(= name @edited-account-name))] (= name @edited-account-name))]
@ -95,7 +100,7 @@
(rf/dispatch [:hide-bottom-sheet]) (rf/dispatch [:hide-bottom-sheet])
(save-account (save-account
{:account account {:account account
:updated-key :prod-preferred-chain-ids :updated-key network-preferences-key
:new-value chain-ids})) :new-value chain-ids}))
:watch-only? watch-only?}])}])) :watch-only? watch-only?}])}]))
:container-style style/data-item}]])))) :container-style style/data-item}]]))))

View File

@ -195,13 +195,13 @@
(let [network-data (let [network-data
{:test (map #(->> % {:test (map #(->> %
:Test :Test
data-store/<-rpc) data-store/rpc->network)
data) data)
:prod (map #(->> % :prod (map #(->> %
:Prod :Prod
data-store/<-rpc) data-store/rpc->network)
data)}] data)}]
{:db (assoc db :wallet/networks network-data)}))) {:db (assoc-in db [:wallet :networks] network-data)})))
(rf/reg-event-fx :wallet/find-ens (rf/reg-event-fx :wallet/find-ens
(fn [{:keys [db]} [input contacts chain-id cb]] (fn [{:keys [db]} [input contacts chain-id cb]]
@ -327,3 +327,7 @@
(fn [_ [explorer-link address]] (fn [_ [explorer-link address]]
{:fx [[:dispatch [:hide-bottom-sheet]] {:fx [[:dispatch [:hide-bottom-sheet]]
[:dispatch [:browser.ui/open-url (str explorer-link "/" address)]]]})) [: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]]]]}))

View File

@ -106,7 +106,6 @@
(fn [profile] (fn [profile]
(:test-networks-enabled? profile))) (:test-networks-enabled? profile)))
(re-frame/reg-sub (re-frame/reg-sub
:multiaccount/contact :multiaccount/contact
:<- [:profile/profile] :<- [:profile/profile]

View File

@ -154,7 +154,6 @@
(reg-root-key-sub :wallet :wallet) (reg-root-key-sub :wallet :wallet)
(reg-root-key-sub :wallet/scanned-address :wallet/scanned-address) (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/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/local-suggestions :wallet/local-suggestions)
(reg-root-key-sub :wallet/valid-ens-or-address? :wallet/valid-ens-or-address?) (reg-root-key-sub :wallet/valid-ens-or-address? :wallet/valid-ens-or-address?)

View File

@ -4,10 +4,16 @@
[status-im.constants :as constants])) [status-im.constants :as constants]))
(re-frame/reg-sub (re-frame/reg-sub
:wallet/filtered-networks-by-mode :wallet/networks
:<- [:wallet]
:-> :networks)
(re-frame/reg-sub
:wallet/networks-by-mode
:<- [:wallet/networks] :<- [:wallet/networks]
(fn [networks [_ test?]] :<- [:profile/test-networks-enabled?]
(get networks (if test? :test :prod)))) (fn [[networks test-networks-enabled?]]
(get networks (if test-networks-enabled? :test :prod))))
(def mainnet-network-details (def mainnet-network-details
{:source (resources/get-network constants/mainnet-network-name) {:source (resources/get-network constants/mainnet-network-name)
@ -32,7 +38,7 @@
(re-frame/reg-sub (re-frame/reg-sub
:wallet/network-details :wallet/network-details
:<- [:wallet/filtered-networks-by-mode false] :<- [:wallet/networks-by-mode]
(fn [networks] (fn [networks]
(->> networks (->> networks
(keep (keep

View File

@ -37,7 +37,7 @@
(h/deftest-sub :wallet/network-details (h/deftest-sub :wallet/network-details
[sub-name] [sub-name]
(testing "returns data with prod" (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 (is (= [{:network-name :ethereum
:short-name "eth" :short-name "eth"
:chain-id 1 :chain-id 1

View File

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

View File

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