From 298f000fcb4bd98b4180f196fcd80c0508696983 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 18 Mar 2019 16:41:25 +0200 Subject: [PATCH] Disable Etherscan and Cryptocompare requests in chaos mode --- src/status_im/accounts/core.cljs | 7 +++ .../realm/schemas/base/account.cljs | 1 - src/status_im/events.cljs | 5 ++ src/status_im/models/transactions.cljs | 62 ++++++++++++------- src/status_im/models/wallet.cljs | 6 +- .../ui/screens/profile/user/views.cljs | 7 ++- src/status_im/ui/screens/wallet/events.cljs | 5 +- src/status_im/utils/prices.cljs | 13 ++-- translations/en.json | 1 + 9 files changed, 72 insertions(+), 35 deletions(-) diff --git a/src/status_im/accounts/core.cljs b/src/status_im/accounts/core.cljs index 4694c5ee5c..884309f2e6 100644 --- a/src/status_im/accounts/core.cljs +++ b/src/status_im/accounts/core.cljs @@ -62,6 +62,13 @@ (accounts.update/account-update {:dev-mode? dev-mode?} {}))) +(fx/defn switch-chaos-mode [{:keys [db] :as cofx} chaos-mode?] + (let [settings (get-in db [:account/account :settings])] + (accounts.update/update-settings + cofx + (assoc settings :chaos-mode? chaos-mode?) + {}))) + (fx/defn enable-notifications [cofx desktop-notifications?] (accounts.update/account-update cofx {:desktop-notifications? desktop-notifications?} diff --git a/src/status_im/data_store/realm/schemas/base/account.cljs b/src/status_im/data_store/realm/schemas/base/account.cljs index 29c1564d32..6881568086 100644 --- a/src/status_im/data_store/realm/schemas/base/account.cljs +++ b/src/status_im/data_store/realm/schemas/base/account.cljs @@ -238,4 +238,3 @@ (def v21 (update v20 :properties merge {:syncing-on-mobile-network? {:type :bool :default false} :remember-syncing-choice? {:type :bool :default false}})) - diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 6c65a1e108..c9dfbd509f 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -170,6 +170,11 @@ (fn [cofx [_ dev-mode?]] (accounts/switch-dev-mode cofx dev-mode?))) +(handlers/register-handler-fx + :accounts.ui/chaos-mode-switched + (fn [cofx [_ dev-mode?]] + (accounts/switch-chaos-mode cofx dev-mode?))) + (handlers/register-handler-fx :accounts.ui/notifications-enabled (fn [cofx [_ desktop-notifications?]] diff --git a/src/status_im/models/transactions.cljs b/src/status_im/models/transactions.cljs index 9965e76a90..ce15a1086e 100644 --- a/src/status_im/models/transactions.cljs +++ b/src/status_im/models/transactions.cljs @@ -12,7 +12,8 @@ [taoensso.timbre :as log] [status-im.utils.fx :as fx] [re-frame.core :as re-frame] - [re-frame.db]) + [re-frame.db] + [status-im.utils.config :as config]) (:require-macros [cljs.core.async.macros :refer [go-loop go]])) @@ -167,7 +168,7 @@ (when (etherscan-supported? chain) (let [network-subdomain (when-let [subdomain (network->subdomain chain)] (str subdomain "."))] - (str "https://" network-subdomain "etherscan.io/tx/" hash))))) + (str "https://" network-subdomain "etherscan.io/tx/" hash))))) (def etherscan-api-key "DMSI4UAAKUBVGCDMVP3H2STAMSAUV7BYFI") @@ -177,12 +178,17 @@ (:mainnet) "api" (:rinkeby) "api-rinkeby")) -(defn- get-transaction-url [chain account] - {:pre [(keyword? chain) (string? account)] - :post [(string? %)]} - (let [network-subdomain (get-api-network-subdomain chain)] - (str "https://" network-subdomain ".etherscan.io/api?module=account&action=txlist&address=0x" - account "&startblock=0&endblock=99999999&sort=desc&apikey=" etherscan-api-key "&q=json"))) +(defn- get-transaction-url + ([chain account] (get-transaction-url chain account false)) + ([chain account chaos-mode?] + {:pre [(keyword? chain) (string? account)] + :post [(string? %)]} + (let [network-subdomain (get-api-network-subdomain chain)] + (if chaos-mode? + "http://httpstat.us/500" + (str "https://" network-subdomain + ".etherscan.io/api?module=account&action=txlist&address=0x" + account "&startblock=0&endblock=99999999&sort=desc&apikey=" etherscan-api-key "&q=json"))))) (defn- format-transaction [account {:keys [value timeStamp blockNumber hash from to @@ -217,21 +223,26 @@ (map (juxt :hash identity))) result)))) -(defn- etherscan-transactions [chain account on-success on-error] - (if (etherscan-supported? chain) - (let [url (get-transaction-url chain account)] - (log/debug "HTTP GET" url) - (http/get url - #(on-success (format-transactions-response % account)) - on-error)) - (log/info "Etherscan not supported for " chain))) +(defn- etherscan-transactions + ([chain account on-success on-error] + (etherscan-transactions chain account on-success on-error false)) + ([chain account on-success on-error chaos-mode?] + (if (etherscan-supported? chain) + (let [url (get-transaction-url chain account chaos-mode?)] + (log/debug "HTTP GET" url) + (http/get url + #(on-success (format-transactions-response % account)) + on-error)) + (log/info "Etherscan not supported for " chain)))) -(defn- get-transactions [{:keys [web3 chain chain-tokens account-address success-fn error-fn]}] +(defn- get-transactions [{:keys [web3 chain chain-tokens account-address + success-fn error-fn chaos-mode?]}] (log/debug "Syncing transactions data..") (etherscan-transactions chain account-address success-fn - error-fn) + error-fn + chaos-mode?) (doseq [direction [:inbound :outbound]] (get-token-transactions web3 chain-tokens @@ -319,7 +330,7 @@ (defonce polling-executor (atom nil)) -(defn transactions-query-helper [web3 all-tokens account-address chain done-fn] +(defn transactions-query-helper [web3 all-tokens account-address chain done-fn chaos-mode?] (get-transactions {:account-address account-address :chain chain @@ -337,18 +348,20 @@ (done-fn)) :error-fn (fn [http-error] (log/debug "Unable to get transactions: " http-error) - (done-fn))})) + (done-fn)) + :chaos-mode? chaos-mode?})) (defn- sync-now! [{:keys [network-status :account/account :wallet/all-tokens app-state network web3] :as opts}] (when @polling-executor (let [chain (ethereum/network->chain-keyword (get-in account [:networks network])) - account-address (:address account)] + account-address (:address account) + chaos-mode? (get-in account [:settings :chaos-mode?])] (when (and (not= network-status :offline) (= app-state "active") (not= :custom chain)) (async-util/async-periodic-run! @polling-executor - (partial transactions-query-helper web3 all-tokens account-address chain)))))) + #(transactions-query-helper web3 all-tokens account-address chain % chaos-mode?)))))) ;; this function handles background syncing of transactions (defn- background-sync [web3 account-address done-fn] @@ -362,11 +375,12 @@ (done-fn) (let [chat-transaction-ids (chat-map->transaction-ids network chats) transaction-map (:transactions wallet) - transaction-ids (set (keys transaction-map))] + transaction-ids (set (keys transaction-map)) + chaos-mode? (get-in account [:settings :chaos-mode?])] (if-not (or (have-unconfirmed-transactions? (vals transaction-map)) (not-empty (set/difference chat-transaction-ids transaction-ids))) (done-fn) - (transactions-query-helper web3 all-tokens account-address chain done-fn)))))) + (transactions-query-helper web3 all-tokens account-address chain done-fn chaos-mode?)))))) (defn- start-sync! [{:keys [:account/account network web3] :as options}] (let [account-address (:address account)] diff --git a/src/status_im/models/wallet.cljs b/src/status_im/models/wallet.cljs index eaae310f47..842f758539 100644 --- a/src/status_im/models/wallet.cljs +++ b/src/status_im/models/wallet.cljs @@ -197,7 +197,8 @@ :tokens (get tokens/all-default-tokens chain)}})))) (fx/defn update-wallet - [{{:keys [web3 network network-status] {:keys [address settings]} :account/account :as db} :db}] + [{{:keys [web3 network network-status] + {:keys [address settings]} :account/account :as db} :db}] (let [all-tokens (:wallet/all-tokens db) network (get-in db [:account/account :networks network]) chain (ethereum/network->chain-keyword network) @@ -225,7 +226,8 @@ :to [(:code currency)] :mainnet? mainnet? :success-event :update-prices-success - :error-event :update-prices-fail} + :error-event :update-prices-fail + :chaos-mode? (:chaos-mode? settings)} :db (-> db (clear-error-message :prices-update) (clear-error-message :balance-update) diff --git a/src/status_im/ui/screens/profile/user/views.cljs b/src/status_im/ui/screens/profile/user/views.cljs index 783f1fc0cc..da1f9cfb82 100644 --- a/src/status_im/ui/screens/profile/user/views.cljs +++ b/src/status_im/ui/screens/profile/user/views.cljs @@ -224,7 +224,12 @@ [profile.components/settings-switch-item {:label-kw :t/dev-mode :value dev-mode? - :action-fn #(re-frame/dispatch [:accounts.ui/dev-mode-switched %])}]]) + :action-fn #(re-frame/dispatch [:accounts.ui/dev-mode-switched %])}] + [profile.components/settings-item-separator] + [profile.components/settings-switch-item + {:label-kw :t/chaos-mode + :value (:chaos-mode? settings) + :action-fn #(re-frame/dispatch [:accounts.ui/chaos-mode-switched %])}]]) (defview advanced [params on-show] (letsubs [advanced? [:get :my-profile/advanced?]] diff --git a/src/status_im/ui/screens/wallet/events.cljs b/src/status_im/ui/screens/wallet/events.cljs index ae81e23743..1e4ecfb892 100644 --- a/src/status_im/ui/screens/wallet/events.cljs +++ b/src/status_im/ui/screens/wallet/events.cljs @@ -65,12 +65,13 @@ ;; TODO(oskarth): At some point we want to get list of relevant assets to get prices for (re-frame/reg-fx :get-prices - (fn [{:keys [from to mainnet? success-event error-event]}] + (fn [{:keys [from to mainnet? success-event error-event chaos-mode?]}] (prices/get-prices from to mainnet? #(re-frame/dispatch [success-event %]) - #(re-frame/dispatch [error-event %])))) + #(re-frame/dispatch [error-event %]) + chaos-mode?))) (re-frame/reg-fx :update-gas-price diff --git a/src/status_im/utils/prices.cljs b/src/status_im/utils/prices.cljs index 9532bac9bd..480cfcb0f2 100644 --- a/src/status_im/utils/prices.cljs +++ b/src/status_im/utils/prices.cljs @@ -1,6 +1,7 @@ (ns status-im.utils.prices (:require [status-im.utils.http :as http] - [status-im.utils.types :as types])) + [status-im.utils.types :as types] + [status-im.utils.config :as config])) ;; Responsible for interacting with Cryptocompare API to get current prices for ;; currencies and tokens. @@ -16,8 +17,10 @@ (defn- ->url-param-syms [syms] ((comp (partial clojure.string/join ",") (partial map name)) syms)) -(defn- gen-price-url [fsyms tsyms] - (str api-url "/pricemultifull?fsyms=" (->url-param-syms fsyms) "&tsyms=" (->url-param-syms tsyms) "&" status-identifier)) +(defn- gen-price-url [fsyms tsyms chaos-mode?] + (if chaos-mode? + "http://httpstat.us/500" + (str api-url "/pricemultifull?fsyms=" (->url-param-syms fsyms) "&tsyms=" (->url-param-syms tsyms) "&" status-identifier))) (defn- format-price-resp [resp mainnet?] ;;NOTE(this check is to allow value conversion for sidechains with native currencies listed on cryptocompare @@ -36,8 +39,8 @@ :price (:PRICE entry) :last-day (:OPEN24HOUR entry)}}))})))) -(defn get-prices [from to mainnet? on-success on-error] +(defn get-prices [from to mainnet? on-success on-error chaos-mode?] (http/get - (gen-price-url from to) + (gen-price-url from to chaos-mode?) (fn [resp] (on-success (format-price-resp resp mainnet?))) on-error)) diff --git a/translations/en.json b/translations/en.json index 3baf07f93b..98673d9957 100644 --- a/translations/en.json +++ b/translations/en.json @@ -595,6 +595,7 @@ "at": "at", "off": "Off", "dev-mode": "Development mode", + "chaos-mode": "Chaos mode", "intro-text-description": "A community-powered platform to securely chat and transact on the Ethereum blockchain", "currency-display-name-kwd": "Kuwaiti Dinar", "invalid-number": "Invalid number",