From 06c31f3a12e831a0ad33effd8437d019dc4a1adc Mon Sep 17 00:00:00 2001 From: Jamie Caprani Date: Fri, 27 Oct 2023 12:54:58 +0200 Subject: [PATCH] chore: add sub to get network info (#17650) --- src/quo/foundations/resources.cljs | 2 +- src/status_im2/constants.cljs | 14 ++++ .../contexts/profile/login/events.cljs | 2 + .../wallet/account/tabs/about/view.cljs | 40 ++++++----- .../contexts/wallet/account/view.cljs | 69 +++++++++---------- .../contexts/wallet/common/temp.cljs | 9 +-- src/status_im2/contexts/wallet/events.cljs | 24 +++++++ src/status_im2/contexts/wallet/home/view.cljs | 62 +++++++++-------- src/status_im2/data_store/wallet.cljs | 25 +++++++ src/status_im2/subs/root.cljs | 4 +- src/status_im2/subs/wallet/networks.cljs | 43 ++++++++++++ src/status_im2/subs/wallet/networks_test.cljs | 44 ++++++++++++ 12 files changed, 245 insertions(+), 93 deletions(-) create mode 100644 src/status_im2/data_store/wallet.cljs create mode 100644 src/status_im2/subs/wallet/networks.cljs create mode 100644 src/status_im2/subs/wallet/networks_test.cljs diff --git a/src/quo/foundations/resources.cljs b/src/quo/foundations/resources.cljs index df4b5bfa39..5c98a3c7e8 100644 --- a/src/quo/foundations/resources.cljs +++ b/src/quo/foundations/resources.cljs @@ -37,7 +37,7 @@ [k] (get tokens k)) -(def networks +(def ^:private networks {:arbitrum (js/require "../resources/images/networks/Arbitrum.png") :ethereum (js/require "../resources/images/networks/Ethereum.png") :gnosis (js/require "../resources/images/networks/Gnosis.png") diff --git a/src/status_im2/constants.cljs b/src/status_im2/constants.cljs index 7f3f57f78b..bfa08d2e0d 100644 --- a/src/status_im2/constants.cljs +++ b/src/status_im2/constants.cljs @@ -358,3 +358,17 @@ (def ^:const ens-action-type-register 1) (def ^:const ens-action-type-set-pub-key 2) + +;; wallet +(def ^:const mainnet-chain-id 1) +(def ^:const optimism-chain-id 10) +(def ^:const arbitrum-chain-id 42161) +(def ^:const goerli-chain-id 5) + +(def ^:const mainnet-short-name "eth") +(def ^:const optimism-short-name "opt") +(def ^:const arbitrum-short-name "arb1") + +(def ^:const mainnet-network-name :ethereum) +(def ^:const optimism-network-name :optimism) +(def ^:const arbitrum-network-name :arbitrum) diff --git a/src/status_im2/contexts/profile/login/events.cljs b/src/status_im2/contexts/profile/login/events.cljs index 7a7c7c59c7..d65470eb71 100644 --- a/src/status_im2/contexts/profile/login/events.cljs +++ b/src/status_im2/contexts/profile/login/events.cljs @@ -22,6 +22,7 @@ [status-im2.contexts.profile.rpc :as profile.rpc] [status-im2.contexts.push-notifications.events :as notifications] [status-im2.contexts.shell.activity-center.events :as activity-center] + [status-im2.contexts.wallet.events :as wallet] [status-im2.navigation.events :as navigation] [taoensso.timbre :as log] [utils.re-frame :as rf] @@ -91,6 +92,7 @@ (activity-center/notifications-fetch-pending-contact-requests) (activity-center/update-seen-state) (activity-center/notifications-fetch-unread-count) + (wallet/get-ethereum-chains) (redirect-to-root)))) ;; login phase 2, we want to load and show chats faster so we split login into 2 phases diff --git a/src/status_im2/contexts/wallet/account/tabs/about/view.cljs b/src/status_im2/contexts/wallet/account/tabs/about/view.cljs index 961142a45e..4a07c43ce8 100644 --- a/src/status_im2/contexts/wallet/account/tabs/about/view.cljs +++ b/src/status_im2/contexts/wallet/account/tabs/about/view.cljs @@ -2,6 +2,7 @@ (:require [quo.core :as quo] [quo.foundations.colors :as colors] + [quo.theme :as quo.theme] [react-native.core :as rn] [status-im2.contexts.wallet.account.tabs.about.style :as style] [status-im2.contexts.wallet.common.temp :as temp] @@ -9,20 +10,21 @@ [utils.re-frame :as rf])) (defn description - [{:keys [address]}] - [quo/text {:size :paragraph-2} - (map (fn [network] - ^{:key (str network)} - [quo/text - {:size :paragraph-2 - :weight :medium - :style {:color (colors/custom-color network)}} - (str (subs (name network) 0 3) (when (= network :arbitrum) "1") ":")]) - temp/network-names) - [quo/text - {:size :paragraph-2 - :weight :monospace} - address]]) + [{:keys [address theme]}] + (let [networks-list (rf/sub [:wallet/network-details])] + [quo/text {:size :paragraph-2} + (map (fn [{:keys [chain-id short-name network-name]}] + ^{:key (str chain-id short-name)} + [quo/text + {:size :paragraph-2 + :weight :medium + :style {:color (colors/resolve-color network-name theme)}} + (str short-name ":")]) + networks-list) + [quo/text + {:size :paragraph-2 + :weight :monospace} + address]])) (defn about-options [] @@ -49,12 +51,16 @@ :accessibility-label :share-address :label (i18n/label :t/share-address)}]]]) -(defn view - [] +(defn- view-internal + [{:keys [theme]}] [rn/view {:style style/about-tab} [quo/data-item (merge temp/data-item-state - {:custom-subtitle (fn [] [description {:address temp/address}]) + {:custom-subtitle (fn [] [description + {:theme theme + :address temp/address}]) :container-style {:margin-bottom 12} :on-press #(rf/dispatch [:show-bottom-sheet {:content about-options}])})] [quo/account-origin temp/account-origin-state]]) + +(def view (quo.theme/with-theme view-internal)) diff --git a/src/status_im2/contexts/wallet/account/view.cljs b/src/status_im2/contexts/wallet/account/view.cljs index 0bbda4ea4c..a45bc9492f 100644 --- a/src/status_im2/contexts/wallet/account/view.cljs +++ b/src/status_im2/contexts/wallet/account/view.cljs @@ -1,7 +1,6 @@ (ns status-im2.contexts.wallet.account.view (:require [quo.core :as quo] - [quo.foundations.resources :as quo.resources] [react-native.core :as rn] [react-native.safe-area :as safe-area] [reagent.core :as reagent] @@ -49,11 +48,6 @@ :padding-bottom 8} :render-fn quo/settings-item}]]) -(def ^:private networks-list - [{:source (quo.resources/get-network :ethereum)} - {:source (quo.resources/get-network :optimism)} - {:source (quo.resources/get-network :arbitrum)}]) - (def tabs-data [{:id :assets :label (i18n/label :t/assets) :accessibility-label :assets-tab} {:id :collectibles :label (i18n/label :t/collectibles) :accessibility-label :collectibles-tab} @@ -67,34 +61,35 @@ (let [top (safe-area/get-top) selected-tab (reagent/atom (:id (first tabs-data)))] (fn [] - [rn/view - {:style {:flex 1 - :margin-top top}} - [quo/page-nav - {:type :wallet-networks - :background :blur - :icon-name :i/close - :on-press #(rf/dispatch [:navigate-back]) - :networks networks-list - :networks-on-press #(js/alert "Pressed Networks") - :right-side :account-switcher - :account-switcher {:customization-color :purple - :on-press #(rf/dispatch [:show-bottom-sheet - {:content account-options - :gradient-cover? true - :customization-color :purple}]) - :emoji "🍑"}}] - [quo/account-overview temp/account-overview-state] - [quo/wallet-graph {:time-frame :empty}] - [quo/wallet-ctas - {:send-action #(rf/dispatch [:open-modal :wallet-select-address]) - :buy-action #(rf/dispatch [:show-bottom-sheet - {:content buy-drawer}])}] - [quo/tabs - {:style style/tabs - :size 32 - :default-active @selected-tab - :data tabs-data - :on-change #(reset! selected-tab %) - :scrollable? true}] - [tabs/view {:selected-tab @selected-tab}]]))) + (let [networks (rf/sub [:wallet/network-details])] + [rn/view + {:style {:flex 1 + :margin-top top}} + [quo/page-nav + {:type :wallet-networks + :background :blur + :icon-name :i/close + :on-press #(rf/dispatch [:navigate-back]) + :networks networks + :networks-on-press #(js/alert "Pressed Networks") + :right-side :account-switcher + :account-switcher {:customization-color :purple + :on-press #(rf/dispatch [:show-bottom-sheet + {:content account-options + :gradient-cover? true + :customization-color :purple}]) + :emoji "🍑"}}] + [quo/account-overview temp/account-overview-state] + [quo/wallet-graph {:time-frame :empty}] + [quo/wallet-ctas + {:send-action #(rf/dispatch [:open-modal :wallet-select-address]) + :buy-action #(rf/dispatch [:show-bottom-sheet + {:content buy-drawer}])}] + [quo/tabs + {:style style/tabs + :size 32 + :default-active @selected-tab + :data tabs-data + :on-change #(reset! selected-tab %) + :scrollable? true}] + [tabs/view {:selected-tab @selected-tab}]])))) diff --git a/src/status_im2/contexts/wallet/common/temp.cljs b/src/status_im2/contexts/wallet/common/temp.cljs index 05e1825b7f..2e26bd3756 100644 --- a/src/status_im2/contexts/wallet/common/temp.cljs +++ b/src/status_im2/contexts/wallet/common/temp.cljs @@ -9,10 +9,6 @@ [utils.i18n :as i18n] [utils.re-frame :as rf])) -(def networks - [{:source (quo.resources/get-network :ethereum)} - {:source (quo.resources/get-network :optimism)} - {:source (quo.resources/get-network :arbitrum)}]) (defn wallet-temporary-navigation [] @@ -28,7 +24,8 @@ [quo/button {:on-press #(rf/dispatch [:navigate-to :wallet-saved-addresses])} "Saved Addresses"]]) -(def wallet-overview-state +(defn wallet-overview-state + [networks] {:state :default :time-frame :none :metrics :none @@ -111,8 +108,6 @@ :account :default :customization-color :blue}) -(def network-names [:ethereum :optimism :arbitrum]) - (def address "0x39cf6E0Ba4C4530735616e1Ee7ff5FbCB726fBd4") (def data-item-state diff --git a/src/status_im2/contexts/wallet/events.cljs b/src/status_im2/contexts/wallet/events.cljs index d2ea57c7bf..a73a6086d6 100644 --- a/src/status_im2/contexts/wallet/events.cljs +++ b/src/status_im2/contexts/wallet/events.cljs @@ -1,6 +1,7 @@ (ns status-im2.contexts.wallet.events (:require [native-module.core :as native-module] + [status-im2.data-store.wallet :as data-store] [taoensso.timbre :as log] [utils.re-frame :as rf] [utils.security.core :as security])) @@ -51,3 +52,26 @@ (rf/dispatch [:wallet/add-account password account-details (first derived-adress-details)]))] {:fx [[:dispatch [:wallet/create-derived-addresses password account-details on-success]]]}))) + +(rf/defn get-ethereum-chains + {:events [:wallet/get-ethereum-chains]} + [{:keys [db]}] + {:fx [[:json-rpc/call + [{:method "wallet_getEthereumChains" + :params [] + :on-success [:wallet/get-ethereum-chains-success] + :on-error #(log/info "failed to get networks " %)}]]]}) + +(rf/reg-event-fx + :wallet/get-ethereum-chains-success + (fn [{:keys [db]} [data]] + (let [network-data + {:test (map #(->> % + :Test + data-store/<-rpc) + data) + :prod (map #(->> % + :Prod + data-store/<-rpc) + data)}] + {:db (assoc db :wallet/networks network-data)}))) diff --git a/src/status_im2/contexts/wallet/home/view.cljs b/src/status_im2/contexts/wallet/home/view.cljs index 4bb7705b1a..f9e4bbcb1d 100644 --- a/src/status_im2/contexts/wallet/home/view.cljs +++ b/src/status_im2/contexts/wallet/home/view.cljs @@ -51,33 +51,35 @@ (let [top (safe-area/get-top) selected-tab (reagent/atom (:id (first tabs-data)))] (fn [] - [rn/view - {:style {:margin-top top - :flex 1}} - [common.top-nav/view] - [rn/view {:style style/overview-container} - [quo/wallet-overview temp/wallet-overview-state]] - [rn/pressable - {:on-long-press #(rf/dispatch [:show-bottom-sheet {:content temp/wallet-temporary-navigation}])} - [quo/wallet-graph {:time-frame :empty}]] - [rn/view {:style style/accounts-container} - [rn/flat-list - {:style style/accounts-list - :data account-cards - :horizontal true - :separator [rn/view {:style {:width 12}}] - :render-fn quo/account-card}]] - [quo/tabs - {:style style/tabs - :size 32 - :default-active @selected-tab - :data tabs-data - :on-change #(reset! selected-tab %)}] - (case @selected-tab - :assets [rn/flat-list - {:render-fn token-value/view - :data temp/tokens - :key :assets-list - :content-container-style {:padding-horizontal 8}}] - :collectibles [collectibles/view] - [activity/view])]))) + (let [networks (rf/sub [:wallet/network-details])] + [rn/view + {:style {:margin-top top + :flex 1}} + [common.top-nav/view] + [rn/view {:style style/overview-container} + [quo/wallet-overview (temp/wallet-overview-state networks)]] + [rn/pressable + {:on-long-press #(rf/dispatch [:show-bottom-sheet + {:content temp/wallet-temporary-navigation}])} + [quo/wallet-graph {:time-frame :empty}]] + [rn/view {:style style/accounts-container} + [rn/flat-list + {:style style/accounts-list + :data account-cards + :horizontal true + :separator [rn/view {:style {:width 12}}] + :render-fn quo/account-card}]] + [quo/tabs + {:style style/tabs + :size 32 + :default-active @selected-tab + :data tabs-data + :on-change #(reset! selected-tab %)}] + (case @selected-tab + :assets [rn/flat-list + {:render-fn token-value/view + :data temp/tokens + :key :assets-list + :content-container-style {:padding-horizontal 8}}] + :collectibles [collectibles/view] + [activity/view])])))) diff --git a/src/status_im2/data_store/wallet.cljs b/src/status_im2/data_store/wallet.cljs new file mode 100644 index 0000000000..c5952fa344 --- /dev/null +++ b/src/status_im2/data_store/wallet.cljs @@ -0,0 +1,25 @@ +(ns status-im2.data-store.wallet + (:require + clojure.set)) + +(defn <-rpc + [network] + (-> network + (clojure.set/rename-keys + {:Prod :prod + :Test :test + :isTest :test? + :tokenOverrides :token-overrides + :rpcUrl :rpc-url + :chainColor :chain-color + :chainName :chain-name + :nativeCurrencyDecimals :native-currency-decimals + :relatedChainId :related-chain-id + :shortName :short-name + :chainId :chain-id + :originalFallbackURL :original-fallback-url + :originalRpcUrl :original-rpc-url + :fallbackURL :fallback-url + :blockExplorerUrl :block-explorer-url + :nativeCurrencySymbol :native-currency-symbol + :nativeCurrencyName :native-currency-symbol}))) diff --git a/src/status_im2/subs/root.cljs b/src/status_im2/subs/root.cljs index 66f314383a..b0bf402a54 100644 --- a/src/status_im2/subs/root.cljs +++ b/src/status_im2/subs/root.cljs @@ -10,7 +10,8 @@ status-im2.subs.onboarding status-im2.subs.pairing status-im2.subs.profile - status-im2.subs.shell)) + status-im2.subs.shell + status-im2.subs.wallet.networks)) (defn reg-root-key-sub [sub-name db-key] @@ -147,6 +148,7 @@ ;;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) ;;debug (when js/goog.DEBUG diff --git a/src/status_im2/subs/wallet/networks.cljs b/src/status_im2/subs/wallet/networks.cljs new file mode 100644 index 0000000000..afed703867 --- /dev/null +++ b/src/status_im2/subs/wallet/networks.cljs @@ -0,0 +1,43 @@ +(ns status-im2.subs.wallet.networks + (:require [quo.foundations.resources :as resources] + [re-frame.core :as re-frame] + [status-im2.constants :as constants])) + +(re-frame/reg-sub + :wallet/filtered-networks-by-mode + :<- [:wallet/networks] + (fn [networks [_ test?]] + (get networks (if test? :test :prod)))) + +(def mainnet-network-details + {:source (resources/get-network constants/mainnet-network-name) + :short-name constants/mainnet-short-name + :network-name constants/mainnet-network-name}) + +(def arbitrum-network-details + {:source (resources/get-network constants/arbitrum-network-name) + :short-name constants/arbitrum-short-name + :network-name constants/arbitrum-network-name}) + +(def optimism-network-details + {:source (resources/get-network constants/optimism-network-name) + :short-name constants/optimism-short-name + :network-name constants/optimism-network-name}) + +(def network-list + {constants/mainnet-chain-id mainnet-network-details + constants/goerli-chain-id mainnet-network-details + constants/arbitrum-chain-id arbitrum-network-details + constants/optimism-chain-id optimism-network-details}) + +(re-frame/reg-sub + :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))) diff --git a/src/status_im2/subs/wallet/networks_test.cljs b/src/status_im2/subs/wallet/networks_test.cljs new file mode 100644 index 0000000000..6050896751 --- /dev/null +++ b/src/status_im2/subs/wallet/networks_test.cljs @@ -0,0 +1,44 @@ +(ns status-im2.subs.wallet.networks-test + (:require + [cljs.test :refer [is testing]] + [re-frame.db :as rf-db] + status-im2.subs.root + status-im2.subs.wallet.networks + [test-helpers.unit :as h] + [utils.re-frame :as rf])) + +(def network-data + {:test [{:test? true + :short-name "eth" + :network-name :ethereum + :related-chain-id 1} + {:test? true + :short-name "arb1" + :related-chain-id 42161} + {:test? true + :short-name "opt" + :related-chain-id 10}] + :prod [{:test? false + :short-name "eth" + :chain-id 1} + {:test? false + :short-name "arb1" + :chain-id 42161} + {:test? false + :short-name "opt" + :chain-id 10}]}) + +(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} + {:network-name :arbitrum + :short-name "arb1" + :chain-id 42161} + {:network-name :optimism + :short-name "opt" + :chain-id 10}] + (map #(dissoc % :source :related-chain-id) (rf/sub [sub-name]))))))