From 44f1ca474bf0ab78eea679eef9d340c89bdd9845 Mon Sep 17 00:00:00 2001 From: Dmitry Novotochinov Date: Tue, 26 Jun 2018 13:07:39 +0300 Subject: [PATCH] Use correct chain name if custom network added Signed-off-by: Goran Jovic --- src/status_im/chat/events/commands.cljs | 15 +++++++-------- src/status_im/chat/events/shortcuts.cljs | 2 +- src/status_im/chat/models/message.cljs | 12 ++++++------ src/status_im/ui/screens/db.cljs | 2 ++ src/status_im/ui/screens/events.cljs | 5 ++++- src/status_im/ui/screens/subs.cljs | 2 +- src/status_im/ui/screens/wallet/send/events.cljs | 14 +++++++------- src/status_im/utils/ethereum/core.cljs | 13 +++++-------- 8 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/status_im/chat/events/commands.cljs b/src/status_im/chat/events/commands.cljs index 4570224af2..5ec79a3549 100644 --- a/src/status_im/chat/events/commands.cljs +++ b/src/status_im/chat/events/commands.cljs @@ -14,9 +14,8 @@ ;;TODO(goranjovic): currently we only allow tokens which are enabled in Manage assets here ;; because balances are only fetched for them. Revisit this decision with regard to battery/network consequences ;; if we were to update all balances. -(defn- allowed-assets [network account] - (let [chain (keyword (ethereum/network-names network)) - visible-token-symbols (get-in account [:settings :wallet :visible-tokens chain])] +(defn- allowed-assets [chain account] + (let [visible-token-symbols (get-in account [:settings :wallet :visible-tokens chain])] (->> (tokens/tokens-for chain) (filter #(not (:nft? %))) (filter #(contains? visible-token-symbols (:symbol %))) @@ -26,19 +25,19 @@ (defn- generate-context "Generates context for jail call" - [account current-account-id chat-id group-chat? to network] + [account current-account-id chat-id group-chat? to chain] (merge {:platform platform/os - :network (ethereum/network-names network) + :network chain :from current-account-id :to to - :allowed-assets (clj->js (allowed-assets network account)) + :allowed-assets (clj->js (allowed-assets chain account)) :chat {:chat-id chat-id :group-chat (boolean group-chat?)}} i18n/delimeters)) (defn request-command-message-data "Requests command message data from jail" - [{:contacts/keys [contacts] :account/keys [account] :keys [network] :as db} + [{:contacts/keys [contacts] :account/keys [account] :keys [chain] :as db} {{:keys [command command-scope-bitmask bot params type]} :content :keys [chat-id group-id] :as message} {:keys [data-type] :as opts}] @@ -51,7 +50,7 @@ to (get-in contacts [chat-id :address]) address (get-in db [:account/account :address]) jail-params {:parameters params - :context (generate-context account address chat-id (models.message/group-message? message) to network)}] + :context (generate-context account address chat-id (models.message/group-message? message) to chain)}] {:db db :call-jail [{:jail-id bot :path path diff --git a/src/status_im/chat/events/shortcuts.cljs b/src/status_im/chat/events/shortcuts.cljs index 4d5d9ceb89..c49be4cc9f 100644 --- a/src/status_im/chat/events/shortcuts.cljs +++ b/src/status_im/chat/events/shortcuts.cljs @@ -15,7 +15,7 @@ :from-chat? true))) (defn send-shortcut-fx [{:account/keys [account] :as db} contact params] - (let [chain (keyword (ethereum/network-names (:network db))) + (let [chain (keyword (:chain db)) symbol (-> params :asset keyword) {:keys [decimals]} (tokens/asset-for chain symbol)] (merge {:db (-> db diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index 6aa059ce99..66d5e94fec 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -386,7 +386,7 @@ :keys [prefill prefillBotDb] :as request} {:keys [params command handler-data content-type]} - network + chain currency prices tx-hash] @@ -405,7 +405,7 @@ :prefill-bot-db prefillBotDb} {:params (cond-> params (= (:name command) constants/command-send) - (assoc :network (ethereum/network-names network) + (assoc :network chain :fiat-amount (money/fiat-amount-value (:amount params) (-> params :asset keyword) currency @@ -444,7 +444,7 @@ :dispatch [:update-transactions]}])))) (defn send-command - [{{:keys [current-public-key chats network prices] :as db} :db :keys [now] :as cofx} params] + [{{:keys [current-public-key chats chain prices] :as db} :db :keys [now] :as cofx} params] (let [{{:keys [handler-data to-message command] :as content} :command chat-id :chat-id} params ;; We send commands to deleted chats as well, i.e. signed later transactions chat (or (get chats chat-id) {:chat-id chat-id}) @@ -454,7 +454,7 @@ currency (-> (currency-settings/get-user-currency db) name string/upper-case keyword)] (handlers-macro/merge-fx cofx (upsert-and-send (prepare-command-message current-public-key chat now request content - network currency prices tx-hash)) + chain currency prices tx-hash)) (console-events/console-respond-command-messages command handler-data) (requests-events/request-answered chat-id to-message) (update-transactions command-name tx-hash {:with-delay? false})))) @@ -470,7 +470,7 @@ (merge {:dispatch-n dn} (dissoc fx :dispatch-n) (dissoc command :dispatch-n))))) (defn invoke-command-handlers - [{{:contacts/keys [contacts] :keys [network] :as db} :db} + [{{:contacts/keys [contacts] :keys [chain] :as db} :db} {{:keys [command params id]} :command :keys [chat-id address] :as orig-params}] @@ -484,7 +484,7 @@ :context (cond-> {:from address :to to :current-account (get db :account/account) - :network (ethereum/network-names network) + :network chain :message-id id} (:async-handler command) (assoc :orig-params orig-params))}] diff --git a/src/status_im/ui/screens/db.cljs b/src/status_im/ui/screens/db.cljs index 485cd915db..1ca2bf574f 100644 --- a/src/status_im/ui/screens/db.cljs +++ b/src/status_im/ui/screens/db.cljs @@ -131,6 +131,7 @@ ;;;;NETWORK (spec/def ::network (spec/nilable string?)) +(spec/def ::chain (spec/nilable string?)) (spec/def ::peers-count (spec/nilable integer?)) (spec/def ::peers-summary (spec/nilable vector?)) (spec/def :inbox/fetching? (spec/nilable boolean?)) @@ -225,6 +226,7 @@ ::sync-state ::sync-data ::network + ::chain :navigation/view-id :navigation/navigation-stack :navigation/prev-tab-view-id diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index f277805040..ff3ae92e85 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -337,7 +337,9 @@ status-module-initialized? status-node-started? device-UUID] :or [network (get app-db :network)]} [_ address]] (let [console-contact (get contacts constants/console-chat-id) - current-account (accounts address)] + current-account (accounts address) + account-network-id (get current-account :network network) + account-network (get-in current-account [:networks account-network-id])] (cond-> (assoc app-db :access-scope->commands-responses access-scope->commands-responses :current-public-key (:public-key current-account) @@ -350,6 +352,7 @@ :account/account current-account :network-status network-status :network network + :chain (ethereum/network->chain-name account-network) :peers-summary peers-summary :peers-count peers-count :device-UUID device-UUID) diff --git a/src/status_im/ui/screens/subs.cljs b/src/status_im/ui/screens/subs.cljs index 827f0772e6..9c22454c63 100644 --- a/src/status_im/ui/screens/subs.cljs +++ b/src/status_im/ui/screens/subs.cljs @@ -39,7 +39,7 @@ (fn [current-account] (get (:networks current-account) (:network current-account)))) -(reg-sub :network-name (comp ethereum/network-names :network)) +(reg-sub :network-name :chain) (reg-sub :sync-state :sync-state) (reg-sub :network-status :network-status) diff --git a/src/status_im/ui/screens/wallet/send/events.cljs b/src/status_im/ui/screens/wallet/send/events.cljs index a0354938bd..98d12e5323 100644 --- a/src/status_im/ui/screens/wallet/send/events.cljs +++ b/src/status_im/ui/screens/wallet/send/events.cljs @@ -51,8 +51,8 @@ (clj->js {:from from :to to :value value :gas gas :gasPrice gas-price}) #())) -(defn- send-tokens [{:keys [web3 from to value gas gas-price symbol network]}] - (let [contract (:address (tokens/symbol->token (keyword (ethereum/network-names network)) symbol))] +(defn- send-tokens [{:keys [web3 from to value gas gas-price symbol chain]}] + (let [contract (:address (tokens/symbol->token (keyword chain) symbol))] (erc20/transfer web3 contract from to value {:gas gas :gasPrice gas-price} #()))) (re-frame/reg-fx @@ -277,8 +277,8 @@ (dissoc :message-id :id))) (defn prepare-unconfirmed-status-transaction [db now hash transaction] - (let [network (:network db) - token (tokens/symbol->token (keyword (ethereum/network-names network)) (:symbol transaction))] + (let [chain (:chain db) + token (tokens/symbol->token (keyword chain) (:symbol transaction))] (-> transaction (assoc :confirmations "0" :timestamp (str now) @@ -342,9 +342,8 @@ (handlers/register-handler-fx :wallet/sign-transaction - (fn [{{:keys [web3] :as db} :db} [_ later?]] + (fn [{{:keys [web3 chain] :as db} :db} [_ later?]] (let [db' (assoc-in db [:wallet :send-transaction :wrong-password?] false) - network (:network db) {:keys [amount id password to symbol method gas gas-price]} (get-in db [:wallet :send-transaction])] (if id {::accept-transaction {:id id @@ -363,7 +362,8 @@ :gas-price gas-price :symbol symbol :method method - :network network}})))) + :chain chain}})))) + (handlers/register-handler-fx :wallet/sign-message-modal (fn [{db :db} _] diff --git a/src/status_im/utils/ethereum/core.cljs b/src/status_im/utils/ethereum/core.cljs index e39fc0da3b..204f3bc60e 100644 --- a/src/status_im/utils/ethereum/core.cljs +++ b/src/status_im/utils/ethereum/core.cljs @@ -12,14 +12,6 @@ :testnet {:id 3 :name "Ropsten"} :rinkeby {:id 4 :name "Rinkeby"}}) -(def network-names - {"mainnet" "mainnet" - "mainnet_rpc" "mainnet" - "testnet" "testnet" - "testnet_rpc" "testnet" - "rinkeby" "rinkeby" - "rinkeby_rpc" "rinkeby"}) - (defn chain-id->chain-keyword [i] (some #(when (= i (:id (val %))) (key %)) chains)) @@ -72,6 +64,11 @@ (defn network->chain-keyword [network] (chain-id->chain-keyword (network->chain-id network))) +(defn network->chain-name [network] + (-> network + network->chain-keyword + name)) + (defn sha3 [s] (.sha3 dependencies/Web3.prototype (str s)))