chore: add sub to get network info (#17650)

This commit is contained in:
Jamie Caprani 2023-10-27 12:54:58 +02:00 committed by GitHub
parent 954373105d
commit 06c31f3a12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 245 additions and 93 deletions

View File

@ -37,7 +37,7 @@
[k] [k]
(get tokens k)) (get tokens k))
(def networks (def ^:private networks
{:arbitrum (js/require "../resources/images/networks/Arbitrum.png") {:arbitrum (js/require "../resources/images/networks/Arbitrum.png")
:ethereum (js/require "../resources/images/networks/Ethereum.png") :ethereum (js/require "../resources/images/networks/Ethereum.png")
:gnosis (js/require "../resources/images/networks/Gnosis.png") :gnosis (js/require "../resources/images/networks/Gnosis.png")

View File

@ -358,3 +358,17 @@
(def ^:const ens-action-type-register 1) (def ^:const ens-action-type-register 1)
(def ^:const ens-action-type-set-pub-key 2) (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)

View File

@ -22,6 +22,7 @@
[status-im2.contexts.profile.rpc :as profile.rpc] [status-im2.contexts.profile.rpc :as profile.rpc]
[status-im2.contexts.push-notifications.events :as notifications] [status-im2.contexts.push-notifications.events :as notifications]
[status-im2.contexts.shell.activity-center.events :as activity-center] [status-im2.contexts.shell.activity-center.events :as activity-center]
[status-im2.contexts.wallet.events :as wallet]
[status-im2.navigation.events :as navigation] [status-im2.navigation.events :as navigation]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[utils.re-frame :as rf] [utils.re-frame :as rf]
@ -91,6 +92,7 @@
(activity-center/notifications-fetch-pending-contact-requests) (activity-center/notifications-fetch-pending-contact-requests)
(activity-center/update-seen-state) (activity-center/update-seen-state)
(activity-center/notifications-fetch-unread-count) (activity-center/notifications-fetch-unread-count)
(wallet/get-ethereum-chains)
(redirect-to-root)))) (redirect-to-root))))
;; login phase 2, we want to load and show chats faster so we split login into 2 phases ;; login phase 2, we want to load and show chats faster so we split login into 2 phases

View File

@ -2,6 +2,7 @@
(:require (:require
[quo.core :as quo] [quo.core :as quo]
[quo.foundations.colors :as colors] [quo.foundations.colors :as colors]
[quo.theme :as quo.theme]
[react-native.core :as rn] [react-native.core :as rn]
[status-im2.contexts.wallet.account.tabs.about.style :as style] [status-im2.contexts.wallet.account.tabs.about.style :as style]
[status-im2.contexts.wallet.common.temp :as temp] [status-im2.contexts.wallet.common.temp :as temp]
@ -9,20 +10,21 @@
[utils.re-frame :as rf])) [utils.re-frame :as rf]))
(defn description (defn description
[{:keys [address]}] [{:keys [address theme]}]
(let [networks-list (rf/sub [:wallet/network-details])]
[quo/text {:size :paragraph-2} [quo/text {:size :paragraph-2}
(map (fn [network] (map (fn [{:keys [chain-id short-name network-name]}]
^{:key (str network)} ^{:key (str chain-id short-name)}
[quo/text [quo/text
{:size :paragraph-2 {:size :paragraph-2
:weight :medium :weight :medium
:style {:color (colors/custom-color network)}} :style {:color (colors/resolve-color network-name theme)}}
(str (subs (name network) 0 3) (when (= network :arbitrum) "1") ":")]) (str short-name ":")])
temp/network-names) networks-list)
[quo/text [quo/text
{:size :paragraph-2 {:size :paragraph-2
:weight :monospace} :weight :monospace}
address]]) address]]))
(defn about-options (defn about-options
[] []
@ -49,12 +51,16 @@
:accessibility-label :share-address :accessibility-label :share-address
:label (i18n/label :t/share-address)}]]]) :label (i18n/label :t/share-address)}]]])
(defn view (defn- view-internal
[] [{:keys [theme]}]
[rn/view {:style style/about-tab} [rn/view {:style style/about-tab}
[quo/data-item [quo/data-item
(merge temp/data-item-state (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} :container-style {:margin-bottom 12}
:on-press #(rf/dispatch [:show-bottom-sheet {:content about-options}])})] :on-press #(rf/dispatch [:show-bottom-sheet {:content about-options}])})]
[quo/account-origin temp/account-origin-state]]) [quo/account-origin temp/account-origin-state]])
(def view (quo.theme/with-theme view-internal))

View File

@ -1,7 +1,6 @@
(ns status-im2.contexts.wallet.account.view (ns status-im2.contexts.wallet.account.view
(:require (:require
[quo.core :as quo] [quo.core :as quo]
[quo.foundations.resources :as quo.resources]
[react-native.core :as rn] [react-native.core :as rn]
[react-native.safe-area :as safe-area] [react-native.safe-area :as safe-area]
[reagent.core :as reagent] [reagent.core :as reagent]
@ -49,11 +48,6 @@
:padding-bottom 8} :padding-bottom 8}
:render-fn quo/settings-item}]]) :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 (def tabs-data
[{:id :assets :label (i18n/label :t/assets) :accessibility-label :assets-tab} [{:id :assets :label (i18n/label :t/assets) :accessibility-label :assets-tab}
{:id :collectibles :label (i18n/label :t/collectibles) :accessibility-label :collectibles-tab} {:id :collectibles :label (i18n/label :t/collectibles) :accessibility-label :collectibles-tab}
@ -67,6 +61,7 @@
(let [top (safe-area/get-top) (let [top (safe-area/get-top)
selected-tab (reagent/atom (:id (first tabs-data)))] selected-tab (reagent/atom (:id (first tabs-data)))]
(fn [] (fn []
(let [networks (rf/sub [:wallet/network-details])]
[rn/view [rn/view
{:style {:flex 1 {:style {:flex 1
:margin-top top}} :margin-top top}}
@ -75,7 +70,7 @@
:background :blur :background :blur
:icon-name :i/close :icon-name :i/close
:on-press #(rf/dispatch [:navigate-back]) :on-press #(rf/dispatch [:navigate-back])
:networks networks-list :networks networks
:networks-on-press #(js/alert "Pressed Networks") :networks-on-press #(js/alert "Pressed Networks")
:right-side :account-switcher :right-side :account-switcher
:account-switcher {:customization-color :purple :account-switcher {:customization-color :purple
@ -97,4 +92,4 @@
:data tabs-data :data tabs-data
:on-change #(reset! selected-tab %) :on-change #(reset! selected-tab %)
:scrollable? true}] :scrollable? true}]
[tabs/view {:selected-tab @selected-tab}]]))) [tabs/view {:selected-tab @selected-tab}]]))))

View File

@ -9,10 +9,6 @@
[utils.i18n :as i18n] [utils.i18n :as i18n]
[utils.re-frame :as rf])) [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 (defn wallet-temporary-navigation
[] []
@ -28,7 +24,8 @@
[quo/button {:on-press #(rf/dispatch [:navigate-to :wallet-saved-addresses])} [quo/button {:on-press #(rf/dispatch [:navigate-to :wallet-saved-addresses])}
"Saved Addresses"]]) "Saved Addresses"]])
(def wallet-overview-state (defn wallet-overview-state
[networks]
{:state :default {:state :default
:time-frame :none :time-frame :none
:metrics :none :metrics :none
@ -111,8 +108,6 @@
:account :default :account :default
:customization-color :blue}) :customization-color :blue})
(def network-names [:ethereum :optimism :arbitrum])
(def address "0x39cf6E0Ba4C4530735616e1Ee7ff5FbCB726fBd4") (def address "0x39cf6E0Ba4C4530735616e1Ee7ff5FbCB726fBd4")
(def data-item-state (def data-item-state

View File

@ -1,6 +1,7 @@
(ns status-im2.contexts.wallet.events (ns status-im2.contexts.wallet.events
(:require (:require
[native-module.core :as native-module] [native-module.core :as native-module]
[status-im2.data-store.wallet :as data-store]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[utils.re-frame :as rf] [utils.re-frame :as rf]
[utils.security.core :as security])) [utils.security.core :as security]))
@ -51,3 +52,26 @@
(rf/dispatch [:wallet/add-account password account-details (rf/dispatch [:wallet/add-account password account-details
(first derived-adress-details)]))] (first derived-adress-details)]))]
{:fx [[:dispatch [:wallet/create-derived-addresses password account-details on-success]]]}))) {: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)})))

View File

@ -51,14 +51,16 @@
(let [top (safe-area/get-top) (let [top (safe-area/get-top)
selected-tab (reagent/atom (:id (first tabs-data)))] selected-tab (reagent/atom (:id (first tabs-data)))]
(fn [] (fn []
(let [networks (rf/sub [:wallet/network-details])]
[rn/view [rn/view
{:style {:margin-top top {:style {:margin-top top
:flex 1}} :flex 1}}
[common.top-nav/view] [common.top-nav/view]
[rn/view {:style style/overview-container} [rn/view {:style style/overview-container}
[quo/wallet-overview temp/wallet-overview-state]] [quo/wallet-overview (temp/wallet-overview-state networks)]]
[rn/pressable [rn/pressable
{:on-long-press #(rf/dispatch [:show-bottom-sheet {:content temp/wallet-temporary-navigation}])} {:on-long-press #(rf/dispatch [:show-bottom-sheet
{:content temp/wallet-temporary-navigation}])}
[quo/wallet-graph {:time-frame :empty}]] [quo/wallet-graph {:time-frame :empty}]]
[rn/view {:style style/accounts-container} [rn/view {:style style/accounts-container}
[rn/flat-list [rn/flat-list
@ -80,4 +82,4 @@
:key :assets-list :key :assets-list
:content-container-style {:padding-horizontal 8}}] :content-container-style {:padding-horizontal 8}}]
:collectibles [collectibles/view] :collectibles [collectibles/view]
[activity/view])]))) [activity/view])]))))

View File

@ -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})))

View File

@ -10,7 +10,8 @@
status-im2.subs.onboarding status-im2.subs.onboarding
status-im2.subs.pairing status-im2.subs.pairing
status-im2.subs.profile status-im2.subs.profile
status-im2.subs.shell)) status-im2.subs.shell
status-im2.subs.wallet.networks))
(defn reg-root-key-sub (defn reg-root-key-sub
[sub-name db-key] [sub-name db-key]
@ -147,6 +148,7 @@
;;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)
;;debug ;;debug
(when js/goog.DEBUG (when js/goog.DEBUG

View File

@ -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)))

View File

@ -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]))))))