Disable Etherscan and Cryptocompare requests in chaos mode

This commit is contained in:
Roman Volosovskyi 2019-03-18 16:41:25 +02:00
parent 3c5502075a
commit 298f000fcb
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
9 changed files with 72 additions and 35 deletions

View File

@ -62,6 +62,13 @@
(accounts.update/account-update {:dev-mode? dev-mode?} (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?] (fx/defn enable-notifications [cofx desktop-notifications?]
(accounts.update/account-update cofx (accounts.update/account-update cofx
{:desktop-notifications? desktop-notifications?} {:desktop-notifications? desktop-notifications?}

View File

@ -238,4 +238,3 @@
(def v21 (update v20 :properties merge (def v21 (update v20 :properties merge
{:syncing-on-mobile-network? {:type :bool :default false} {:syncing-on-mobile-network? {:type :bool :default false}
:remember-syncing-choice? {:type :bool :default false}})) :remember-syncing-choice? {:type :bool :default false}}))

View File

@ -170,6 +170,11 @@
(fn [cofx [_ dev-mode?]] (fn [cofx [_ dev-mode?]]
(accounts/switch-dev-mode 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 (handlers/register-handler-fx
:accounts.ui/notifications-enabled :accounts.ui/notifications-enabled
(fn [cofx [_ desktop-notifications?]] (fn [cofx [_ desktop-notifications?]]

View File

@ -12,7 +12,8 @@
[taoensso.timbre :as log] [taoensso.timbre :as log]
[status-im.utils.fx :as fx] [status-im.utils.fx :as fx]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[re-frame.db]) [re-frame.db]
[status-im.utils.config :as config])
(:require-macros (:require-macros
[cljs.core.async.macros :refer [go-loop go]])) [cljs.core.async.macros :refer [go-loop go]]))
@ -177,12 +178,17 @@
(:mainnet) "api" (:mainnet) "api"
(:rinkeby) "api-rinkeby")) (:rinkeby) "api-rinkeby"))
(defn- get-transaction-url [chain account] (defn- get-transaction-url
([chain account] (get-transaction-url chain account false))
([chain account chaos-mode?]
{:pre [(keyword? chain) (string? account)] {:pre [(keyword? chain) (string? account)]
:post [(string? %)]} :post [(string? %)]}
(let [network-subdomain (get-api-network-subdomain chain)] (let [network-subdomain (get-api-network-subdomain chain)]
(str "https://" network-subdomain ".etherscan.io/api?module=account&action=txlist&address=0x" (if chaos-mode?
account "&startblock=0&endblock=99999999&sort=desc&apikey=" etherscan-api-key "&q=json"))) "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 (defn- format-transaction [account
{:keys [value timeStamp blockNumber hash from to {:keys [value timeStamp blockNumber hash from to
@ -217,21 +223,26 @@
(map (juxt :hash identity))) (map (juxt :hash identity)))
result)))) result))))
(defn- etherscan-transactions [chain account on-success on-error] (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) (if (etherscan-supported? chain)
(let [url (get-transaction-url chain account)] (let [url (get-transaction-url chain account chaos-mode?)]
(log/debug "HTTP GET" url) (log/debug "HTTP GET" url)
(http/get url (http/get url
#(on-success (format-transactions-response % account)) #(on-success (format-transactions-response % account))
on-error)) on-error))
(log/info "Etherscan not supported for " chain))) (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..") (log/debug "Syncing transactions data..")
(etherscan-transactions chain (etherscan-transactions chain
account-address account-address
success-fn success-fn
error-fn) error-fn
chaos-mode?)
(doseq [direction [:inbound :outbound]] (doseq [direction [:inbound :outbound]]
(get-token-transactions web3 (get-token-transactions web3
chain-tokens chain-tokens
@ -319,7 +330,7 @@
(defonce polling-executor (atom nil)) (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 (get-transactions
{:account-address account-address {:account-address account-address
:chain chain :chain chain
@ -337,18 +348,20 @@
(done-fn)) (done-fn))
:error-fn (fn [http-error] :error-fn (fn [http-error]
(log/debug "Unable to get transactions: " 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}] (defn- sync-now! [{:keys [network-status :account/account :wallet/all-tokens app-state network web3] :as opts}]
(when @polling-executor (when @polling-executor
(let [chain (ethereum/network->chain-keyword (get-in account [:networks network])) (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) (when (and (not= network-status :offline)
(= app-state "active") (= app-state "active")
(not= :custom chain)) (not= :custom chain))
(async-util/async-periodic-run! (async-util/async-periodic-run!
@polling-executor @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 ;; this function handles background syncing of transactions
(defn- background-sync [web3 account-address done-fn] (defn- background-sync [web3 account-address done-fn]
@ -362,11 +375,12 @@
(done-fn) (done-fn)
(let [chat-transaction-ids (chat-map->transaction-ids network chats) (let [chat-transaction-ids (chat-map->transaction-ids network chats)
transaction-map (:transactions wallet) 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)) (if-not (or (have-unconfirmed-transactions? (vals transaction-map))
(not-empty (set/difference chat-transaction-ids transaction-ids))) (not-empty (set/difference chat-transaction-ids transaction-ids)))
(done-fn) (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}] (defn- start-sync! [{:keys [:account/account network web3] :as options}]
(let [account-address (:address account)] (let [account-address (:address account)]

View File

@ -197,7 +197,8 @@
:tokens (get tokens/all-default-tokens chain)}})))) :tokens (get tokens/all-default-tokens chain)}}))))
(fx/defn update-wallet (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) (let [all-tokens (:wallet/all-tokens db)
network (get-in db [:account/account :networks network]) network (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network) chain (ethereum/network->chain-keyword network)
@ -225,7 +226,8 @@
:to [(:code currency)] :to [(:code currency)]
:mainnet? mainnet? :mainnet? mainnet?
:success-event :update-prices-success :success-event :update-prices-success
:error-event :update-prices-fail} :error-event :update-prices-fail
:chaos-mode? (:chaos-mode? settings)}
:db (-> db :db (-> db
(clear-error-message :prices-update) (clear-error-message :prices-update)
(clear-error-message :balance-update) (clear-error-message :balance-update)

View File

@ -224,7 +224,12 @@
[profile.components/settings-switch-item [profile.components/settings-switch-item
{:label-kw :t/dev-mode {:label-kw :t/dev-mode
:value 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] (defview advanced [params on-show]
(letsubs [advanced? [:get :my-profile/advanced?]] (letsubs [advanced? [:get :my-profile/advanced?]]

View File

@ -65,12 +65,13 @@
;; TODO(oskarth): At some point we want to get list of relevant assets to get prices for ;; TODO(oskarth): At some point we want to get list of relevant assets to get prices for
(re-frame/reg-fx (re-frame/reg-fx
:get-prices :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 (prices/get-prices from
to to
mainnet? mainnet?
#(re-frame/dispatch [success-event %]) #(re-frame/dispatch [success-event %])
#(re-frame/dispatch [error-event %])))) #(re-frame/dispatch [error-event %])
chaos-mode?)))
(re-frame/reg-fx (re-frame/reg-fx
:update-gas-price :update-gas-price

View File

@ -1,6 +1,7 @@
(ns status-im.utils.prices (ns status-im.utils.prices
(:require [status-im.utils.http :as http] (: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 ;; Responsible for interacting with Cryptocompare API to get current prices for
;; currencies and tokens. ;; currencies and tokens.
@ -16,8 +17,10 @@
(defn- ->url-param-syms [syms] (defn- ->url-param-syms [syms]
((comp (partial clojure.string/join ",") (partial map name)) syms)) ((comp (partial clojure.string/join ",") (partial map name)) syms))
(defn- gen-price-url [fsyms tsyms] (defn- gen-price-url [fsyms tsyms chaos-mode?]
(str api-url "/pricemultifull?fsyms=" (->url-param-syms fsyms) "&tsyms=" (->url-param-syms tsyms) "&" status-identifier)) (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?] (defn- format-price-resp [resp mainnet?]
;;NOTE(this check is to allow value conversion for sidechains with native currencies listed on cryptocompare ;;NOTE(this check is to allow value conversion for sidechains with native currencies listed on cryptocompare
@ -36,8 +39,8 @@
:price (:PRICE entry) :price (:PRICE entry)
:last-day (:OPEN24HOUR 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 (http/get
(gen-price-url from to) (gen-price-url from to chaos-mode?)
(fn [resp] (on-success (format-price-resp resp mainnet?))) (fn [resp] (on-success (format-price-resp resp mainnet?)))
on-error)) on-error))

View File

@ -595,6 +595,7 @@
"at": "at", "at": "at",
"off": "Off", "off": "Off",
"dev-mode": "Development mode", "dev-mode": "Development mode",
"chaos-mode": "Chaos mode",
"intro-text-description": "A community-powered platform to securely chat and transact on the Ethereum blockchain", "intro-text-description": "A community-powered platform to securely chat and transact on the Ethereum blockchain",
"currency-display-name-kwd": "Kuwaiti Dinar", "currency-display-name-kwd": "Kuwaiti Dinar",
"invalid-number": "Invalid number", "invalid-number": "Invalid number",