From 7d6b2ac87a4dfd869cebf1be01d252304f17b959 Mon Sep 17 00:00:00 2001 From: Mohamed Javid <19339952+smohamedjavid@users.noreply.github.com> Date: Wed, 6 Mar 2024 20:59:52 +0530 Subject: [PATCH] [Feature] Wallet - Handle Blockchain Status (#18850) This commit adds the feature to handle blockchain status signals from the status-go and shows a toast if the provider for any of the blockchains is down. Signed-off-by: Mohamed Javid <19339952+smohamedjavid@users.noreply.github.com> --- src/status_im/constants.cljs | 3 ++ src/status_im/contexts/wallet/data_store.cljs | 3 +- src/status_im/contexts/wallet/events.cljs | 41 ++++++++++++++++++- src/status_im/contexts/wallet/signals.cljs | 26 +++++------- src/utils/money.cljs | 6 ++- translations/en.json | 3 +- 6 files changed, 61 insertions(+), 21 deletions(-) diff --git a/src/status_im/constants.cljs b/src/status_im/constants.cljs index 36fafde387..dd2b52b432 100644 --- a/src/status_im/constants.cljs +++ b/src/status_im/constants.cljs @@ -425,6 +425,9 @@ (def ^:const optimism-goerli-chain-id 420) (def ^:const optimism-sepolia-chain-id 11155420) +(def ^:const mainnet-chain-ids + #{ethereum-mainnet-chain-id arbitrum-mainnet-chain-id optimism-mainnet-chain-id}) + (def ^:const mainnet-short-name "eth") (def ^:const optimism-short-name "opt") (def ^:const arbitrum-short-name "arb1") diff --git a/src/status_im/contexts/wallet/data_store.cljs b/src/status_im/contexts/wallet/data_store.cljs index 8421e680ca..974c8c2d02 100644 --- a/src/status_im/contexts/wallet/data_store.cljs +++ b/src/status_im/contexts/wallet/data_store.cljs @@ -60,8 +60,7 @@ :color :colorId}) (update :prodPreferredChainIds chain-ids-set->string) (update :testPreferredChainIds chain-ids-set->string) - (dissoc :watch-only?) - (dissoc :default-account?))) + (dissoc :watch-only? :default-account? :tokens :collectibles))) (defn- rpc->balances-per-chain [token] diff --git a/src/status_im/contexts/wallet/events.cljs b/src/status_im/contexts/wallet/events.cljs index 1d3e8ad7e0..23fb7668e7 100644 --- a/src/status_im/contexts/wallet/events.cljs +++ b/src/status_im/contexts/wallet/events.cljs @@ -3,6 +3,8 @@ [clojure.string :as string] [react-native.background-timer :as background-timer] [react-native.platform :as platform] + [status-im.constants :as constants] + [status-im.contexts.wallet.common.utils :as utils] [status-im.contexts.wallet.data-store :as data-store] [status-im.contexts.wallet.events.collectibles] [status-im.contexts.wallet.item-types :as item-types] @@ -12,7 +14,8 @@ [utils.ethereum.eip.eip55 :as eip55] [utils.i18n :as i18n] [utils.number] - [utils.re-frame :as rf])) + [utils.re-frame :as rf] + [utils.transforms :as transforms])) (rf/reg-event-fx :wallet/show-account-created-toast (fn [{:keys [db]} [address]] @@ -426,7 +429,6 @@ (rf/reg-event-fx :wallet/store-secret-phrase store-secret-phrase) - (defn new-keypair-created [{:keys [db]} [{:keys [new-keypair]}]] {:db (assoc-in db [:wallet :ui :create-account :new-keypair] new-keypair) @@ -448,3 +450,38 @@ {:db (update-in db [:wallet :ui :create-account] dissoc :new-keypair)}) (rf/reg-event-fx :wallet/clear-new-keypair clear-new-keypair) + +(rf/reg-event-fx + :wallet/blockchain-status-changed + (fn [{:keys [db]} [{:keys [message]}]] + (let [chains (-> (transforms/json->clj message) + (update-keys (comp utils.number/parse-int name))) + down-chains (-> (select-keys chains + (for [[k v] chains :when (= v "down")] k)) + keys) + test-networks-enabled? (get-in db [:profile/profile :test-networks-enabled?]) + disabled-chain-id? (fn [chain-id] + (let [mainnet-chain? (contains? constants/mainnet-chain-ids chain-id)] + (if test-networks-enabled? + mainnet-chain? + (not mainnet-chain?)))) + chains-filtered-by-mode (remove disabled-chain-id? down-chains) + chains-down? (seq chains-filtered-by-mode) + chain-names (when chains-down? + (->> (map #(-> (utils/id->network %) + name + string/capitalize) + chains-filtered-by-mode) + distinct + (string/join ", ")))] + (when (seq down-chains) + (log/info "[wallet] Chain(s) down: " down-chains) + (log/info "[wallet] Test network enabled: " (boolean test-networks-enabled?))) + {:db (assoc-in db [:wallet :statuses :blockchains] chains) + :fx (when chains-down? + [[:dispatch + [:toasts/upsert + {:id :chains-down + :type :negative + :text (i18n/label :t/provider-is-down {:chains chain-names}) + :duration 10000}]]])}))) diff --git a/src/status_im/contexts/wallet/signals.cljs b/src/status_im/contexts/wallet/signals.cljs index 877dbfee4d..335478c97c 100644 --- a/src/status_im/contexts/wallet/signals.cljs +++ b/src/status_im/contexts/wallet/signals.cljs @@ -2,12 +2,13 @@ (:require [oops.core :as oops] [taoensso.timbre :as log] - [utils.re-frame :as rf])) + [utils.re-frame :as rf] + [utils.transforms :as transforms])) (rf/reg-event-fx :wallet/pending-transaction-status-changed-received (fn [{:keys [db]} [{:keys [message]}]] - (let [details (js->clj (js/JSON.parse message) :keywordize-keys true) + (let [details (transforms/json->clj message) tx-hash (:hash details)] {:db (update-in db [:wallet :transactions tx-hash] assoc :status :confirmed :blocks 1)}))) @@ -25,22 +26,17 @@ "pending-transaction-status-changed" {:fx [[:dispatch [:wallet/pending-transaction-status-changed-received - (js->clj event-js - :keywordize-keys - true)]]]} + (transforms/js->clj event-js)]]]} "wallet-owned-collectibles-filtering-done" {:fx [[:dispatch [:wallet/owned-collectibles-filtering-done - (js->clj event-js - :keywordize-keys - true)]]]} + (transforms/js->clj event-js)]]]} "wallet-get-collectibles-details-done" {:fx [[:dispatch [:wallet/get-collectible-details-done - (js->clj event-js - :keywordize-keys - true)]]]} + (transforms/js->clj event-js)]]]} "wallet-tick-reload" {:fx [[:dispatch [:wallet/reload]]]} + "wallet-blockchain-status-changed" {:fx [[:dispatch + [:wallet/blockchain-status-changed + (transforms/js->clj event-js)]]]} (log/debug ::unknown-wallet-event - :type type - :event (js->clj event-js - :keywordize-keys - true)))))) + :type event-type + :event (transforms/js->clj event-js)))))) diff --git a/src/utils/money.cljs b/src/utils/money.cljs index c1b622dc1b..a977d395b8 100644 --- a/src/utils/money.cljs +++ b/src/utils/money.cljs @@ -213,10 +213,14 @@ "Add with defaults, this version is able to receive `nil` and takes them as 0." (fnil add* (bignumber 0) (bignumber 0))) -(defn mul +(defn- mul* [bn1 bn2] (.mul ^js bn1 bn2)) +(def mul + "Multiply with defaults, this version is able to receive `nil` and takes them as 0." + (fnil mul* (bignumber 0) (bignumber 0))) + (defn mul-and-round [bn1 bn2] (.round (.mul ^js bn1 bn2) 0)) diff --git a/translations/en.json b/translations/en.json index a3a8b8a452..26d205f3c9 100644 --- a/translations/en.json +++ b/translations/en.json @@ -2514,5 +2514,6 @@ "keypair-name-input-placeholder": "Collectibles account, Old vault....", "goerli-testnet-toggle-confirmation": "Are you sure you want to toggle Goerli? This will log you out and you will have to login again.", "bridged-to": "Bridged to {{network}}", - "slide-to-bridge": "Slide to bridge" + "slide-to-bridge": "Slide to bridge", + "provider-is-down": "The provider for the following chain(s) is down: {{chains}}" }