From c16b0860ec921a61c119857de27c6f29e301e4f6 Mon Sep 17 00:00:00 2001 From: andrey Date: Wed, 10 Nov 2021 15:32:01 +0100 Subject: [PATCH] custom network symbol Signed-off-by: andrey --- .env | 1 + src/status_im/ethereum/core.cljs | 12 +++++--- src/status_im/ethereum/tokens.cljs | 15 ++++++---- src/status_im/network/core.cljs | 8 +++-- src/status_im/network/core_test.cljs | 24 ++++++++++++++- src/status_im/notifications/local.cljs | 2 +- src/status_im/signing/core.cljs | 2 +- src/status_im/subs.cljs | 30 +++++++++++-------- .../ui/screens/chat/message/command.cljs | 3 +- .../screens/network/edit_network/views.cljs | 7 +++++ src/status_im/ui/screens/signing/views.cljs | 2 +- .../ui/screens/wallet/account/views.cljs | 7 +++-- src/status_im/wallet/core.cljs | 6 +--- src/status_im/wallet/prices.cljs | 2 +- translations/en.json | 1 + 15 files changed, 84 insertions(+), 38 deletions(-) diff --git a/.env b/.env index 1ceb03357e..85d1f28826 100644 --- a/.env +++ b/.env @@ -31,3 +31,4 @@ DATABASE_MANAGEMENT_ENABLED=1 METRICS_ENABLED=0 DELETE_MESSAGE_ENABLED=1 COLLECTIBLES_ENABLED=1 +COMMANDS_ENABLED=1 diff --git a/src/status_im/ethereum/core.cljs b/src/status_im/ethereum/core.cljs index bfe7d71d61..d95bf367e3 100644 --- a/src/status_im/ethereum/core.cljs +++ b/src/status_im/ethereum/core.cljs @@ -122,13 +122,17 @@ network->chain-keyword name)) -(defn chain-keyword +(defn get-current-network [{:networks/keys [current-network networks]}] - (network->chain-keyword (get networks current-network))) + (get networks current-network)) + +(defn chain-keyword + [db] + (network->chain-keyword (get-current-network db))) (defn chain-id - [{:networks/keys [current-network networks]}] - (network->chain-id (get networks current-network))) + [db] + (network->chain-id (get-current-network db))) (defn snt-symbol [db] (chain-keyword->snt-symbol (chain-keyword db))) diff --git a/src/status_im/ethereum/tokens.cljs b/src/status_im/ethereum/tokens.cljs index 0ec2150c11..e58d3727ca 100644 --- a/src/status_im/ethereum/tokens.cljs +++ b/src/status_im/ethereum/tokens.cljs @@ -1,13 +1,15 @@ (ns status-im.ethereum.tokens - (:require [clojure.string :as string]) + (:require [clojure.string :as string] + [status-im.ethereum.core :as ethereum]) (:require-macros [status-im.ethereum.macros :as ethereum.macros :refer [resolve-icons]])) (def default-native-currency (memoize - (fn [] + (fn [symbol] {:name "Native" :symbol :ETH + :symbol-display symbol :decimals 18 :icon {:source (js/require "../resources/images/tokens/default-token.png")}}))) @@ -43,8 +45,9 @@ (def native-currency-symbols (set (map #(-> % val :symbol) all-native-currencies))) -(defn native-currency [chain] - (-> (get all-native-currencies chain (default-native-currency)))) +(defn native-currency [{:keys [symbol] :as current-network}] + (let [chain (ethereum/network->chain-keyword current-network)] + (get all-native-currencies chain (default-native-currency symbol)))) (defn ethereum? [symbol] (native-currency-symbols symbol)) @@ -855,8 +858,8 @@ (defn address->token [all-tokens address] (get all-tokens (string/lower-case address))) -(defn asset-for [all-tokens chain symbol] - (let [native-coin (native-currency chain)] +(defn asset-for [all-tokens current-network symbol] + (let [native-coin (native-currency current-network)] (if (or (= (:symbol-display native-coin) symbol) (= (:symbol native-coin) symbol)) native-coin diff --git a/src/status_im/network/core.cljs b/src/status_im/network/core.cljs index 1255e05bdc..26eb821202 100644 --- a/src/status_im/network/core.cljs +++ b/src/status_im/network/core.cljs @@ -19,6 +19,7 @@ (def default-manage {:name {:value ""} :url {:value ""} + :symbol {:value ""} :chain {:value :mainnet}}) (defn validate-string [{:keys [value]}] @@ -38,6 +39,7 @@ (-> manage (update :url validate-url) (update :name validate-string) + (update :symbol validate-string) (update :chain validate-string) (update :network-id validate-network-id))) @@ -151,7 +153,7 @@ :on-success #(re-frame/dispatch [:navigate-back])}]})) (defn new-network - [random-id network-name upstream-url chain-type chain-id] + [random-id network-name symbol upstream-url chain-type chain-id] (let [data-dir (str "/ethereum/" (name chain-type) "_rpc") config {:NetworkId (or (when chain-id (int chain-id)) (ethereum/chain-keyword->chain-id chain-type)) @@ -160,6 +162,7 @@ :URL upstream-url}}] {:id random-id :name network-name + :symbol symbol :config config})) (fx/defn save @@ -169,10 +172,11 @@ random-id-generator :random-id-generator :as cofx}] (if (valid-manage? manage) ;; rename network-id from UI to chain-id - (let [{:keys [name url chain network-id]} manage + (let [{:keys [name url chain network-id symbol]} manage random-id (string/replace (random-id-generator) "-" "") network (new-network random-id (:value name) + (:value symbol) (:value url) (:value chain) (:value network-id)) diff --git a/src/status_im/network/core_test.cljs b/src/status_im/network/core_test.cljs index 1c4df48307..1c7f166ad8 100644 --- a/src/status_im/network/core_test.cljs +++ b/src/status_im/network/core_test.cljs @@ -34,11 +34,13 @@ (deftest new-network-test (let [actual (network.core/new-network "randomid" "network-name" + "network-symbol" "upstream-url" :mainnet nil)] (is (= {:id "randomid" :name "network-name" + :symbol "network-symbol" :config {:NetworkId 1 :DataDir "/ethereum/mainnet_rpc" :UpstreamConfig {:Enabled true @@ -48,11 +50,13 @@ (deftest new-network-id-test (let [actual (network.core/new-network "randomid" "network-name" + "network-symbol" "upstream-url" :mainnet "5777")] (is (= {:id "randomid" :name "network-name" + :symbol "network-symbol" :config {:NetworkId 5777 :DataDir "/ethereum/mainnet_rpc" :UpstreamConfig {:Enabled true @@ -63,21 +67,31 @@ (testing "a valid manage" (is (network.core/valid-manage? {:url {:value "http://valid.com"} :name {:value "valid"} + :symbol {:value "valid"} :chain {:value "valid"}}))) (testing "invalid url" (is (not (network.core/valid-manage? {:url {:value "invalid"} :name {:value "valid"} + :symbol {:value "valid"} :chain {:value "valid"}})))) (testing "invalid name" (is (not (network.core/valid-manage? {:url {:value "http://valid.com"} :name {:value ""} + :symbol {:value "valid"} :chain {:value "valid"}})))) (testing "invalid chain" (is (not (network.core/valid-manage? {:url {:value "http://valid.com"} :name {:value "valid"} - :chain {:value ""}}))))) + :symbol {:value "valid"} + :chain {:value ""}})))) + + (testing "invalid symbol" + (is (not (network.core/valid-manage? {:url {:value "http://valid.com"} + :name {:value "valid"} + :symbol {:value ""} + :chain {:value "valid"}}))))) (deftest set-input-test (testing "it updates and validate a field" @@ -85,6 +99,8 @@ :error false} :name {:value "" :error true} + :symbol {:value "symbol" + :error false} :chain {:value "mainnet" :error false} :network-id {:value nil @@ -93,6 +109,8 @@ :error true} :name {:value "" :error false} + :symbol {:value "symbol" + :error false} :chain {:value "mainnet" :error false}}}} :url "http://valid.com"))))) @@ -110,11 +128,13 @@ (let [fx (network.core/save {:random-id-generator (constantly "random-id") :db {:networks/manage {:url {:value "http://valid.com"} :chain {:value :mainnet} + :symbol {:value "symbol"} :name {:value "valid"}} :multiaccount {} :networks/networks {"random2" {:id "random2" :name "network-name" + :symbol "symbol" :config {:NetworkId 1 :DataDir "/ethereum/mainnet_rpc" :UpstreamConfig {:Enabled true @@ -147,11 +167,13 @@ :db {:networks/manage {:url {:value "http://valid.com"} :chain {:value :mainnet} :name {:value "valid"} + :symbol {:value "symbol"} :network-id {:value 5}} :multiaccount {} :networks/networks {"randomid" {:id "randomid" :name "network-name" + :symbol "symbol" :config {:NetworkId 3 :DataDir "/ethereum/mainnet_rpc" :UpstreamConfig {:Enabled true diff --git a/src/status_im/notifications/local.cljs b/src/status_im/notifications/local.cljs index 93fd260ab2..ee9f3d9c4a 100644 --- a/src/status_im/notifications/local.cljs +++ b/src/status_im/notifications/local.cljs @@ -66,7 +66,7 @@ (get-in tokens/all-tokens-normalized [(keyword chain) (clojure.string/lower-case contract)] default-erc20-token) - (tokens/native-currency (keyword chain))) + (tokens/native-currency network)) amount (money/wei->ether (decode/uint value)) to (or (:name toAccount) (utils/get-shortened-address to)) from (or (:name fromAccount) (utils/get-shortened-address from)) diff --git a/src/status_im/signing/core.cljs b/src/status_im/signing/core.cljs index 56a1c2472f..57f2871c48 100644 --- a/src/status_im/signing/core.cljs +++ b/src/status_im/signing/core.cljs @@ -197,7 +197,7 @@ :symbol :ETH :value value :amount (str eth-amount) - :token (tokens/asset-for (:wallet/all-tokens db) (ethereum/chain-keyword db) :ETH)} + :token (tokens/asset-for (:wallet/all-tokens db) (ethereum/get-current-network db) :ETH)} (not (nil? token)) token :else diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index ad7803ba3d..74f3c12cbd 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -509,6 +509,12 @@ (fn [chain-id] (= 1 chain-id))) +(re-frame/reg-sub + :ethereum-network? + :<- [:chain-id] + (fn [chain-id] + (< chain-id 6))) + (re-frame/reg-sub :network-name :<- [:current-network] @@ -654,9 +660,9 @@ (re-frame/reg-sub :ethereum/native-currency - :<- [:ethereum/chain-keyword] - (fn [chain-keyword] - (tokens/native-currency chain-keyword))) + :<- [:current-network] + (fn [network] + (tokens/native-currency network))) ;;MULTIACCOUNT ============================================================================================================== @@ -1794,12 +1800,12 @@ (re-frame/reg-sub :wallet/visible-assets - :<- [:ethereum/chain-keyword] + :<- [:current-network] :<- [:wallet/visible-tokens-symbols] :<- [:wallet/sorted-tokens] - (fn [[chain visible-tokens-symbols all-tokens-sorted]] + (fn [[network visible-tokens-symbols all-tokens-sorted]] (conj (filter #(contains? visible-tokens-symbols (:symbol %)) all-tokens-sorted) - (tokens/native-currency chain)))) + (tokens/native-currency network)))) (re-frame/reg-sub :wallet/visible-assets-with-amount @@ -2796,11 +2802,11 @@ :<- [:wallet] :<- [:offline?] :<- [:wallet/all-tokens] - :<- [:ethereum/chain-keyword] + :<- [:current-network] (fn [[{:keys [symbol from to amount-text] :as transaction} - wallet offline? all-tokens chain]] + wallet offline? all-tokens current-network]] (let [balance (get-in wallet [:accounts (:address from) :balance]) - {:keys [decimals] :as token} (tokens/asset-for all-tokens chain symbol) + {:keys [decimals] :as token} (tokens/asset-for all-tokens current-network symbol) {:keys [value error]} (wallet.db/parse-amount amount-text decimals) amount (money/formatted->internal value symbol decimals) {:keys [amount-error] :as transaction-new} @@ -2823,11 +2829,11 @@ :<- [:wallet] :<- [:offline?] :<- [:wallet/all-tokens] - :<- [:ethereum/chain-keyword] + :<- [:current-network] (fn [[{:keys [symbol from to amount-text] :as transaction} - wallet offline? all-tokens chain]] + wallet offline? all-tokens current-network]] (let [balance (get-in wallet [:accounts (:address from) :balance]) - {:keys [decimals] :as token} (tokens/asset-for all-tokens chain symbol) + {:keys [decimals] :as token} (tokens/asset-for all-tokens current-network symbol) {:keys [value error]} (wallet.db/parse-amount amount-text decimals) amount (money/formatted->internal value symbol decimals) {:keys [amount-error] :as transaction-new} diff --git a/src/status_im/ui/screens/chat/message/command.cljs b/src/status_im/ui/screens/chat/message/command.cljs index 2ec1ff4915..6f75eb8e99 100644 --- a/src/status_im/ui/screens/chat/message/command.cljs +++ b/src/status_im/ui/screens/chat/message/command.cljs @@ -149,12 +149,13 @@ (defn- command-transaction-info [contract value] - (let [{:keys [symbol icon decimals color] :as token} + (let [{:keys [symbol symbol-display icon decimals color] :as token} (if (seq contract) (get @(re-frame/subscribe [:wallet/all-tokens]) contract transactions/default-erc20-token) @(re-frame/subscribe [:ethereum/native-currency])) + symbol (or symbol-display symbol) amount (money/internal->formatted value symbol decimals) {:keys [code]} @(re-frame/subscribe [:wallet/currency]) diff --git a/src/status_im/ui/screens/network/edit_network/views.cljs b/src/status_im/ui/screens/network/edit_network/views.cljs index 6c488bbddb..77f7a0ceed 100644 --- a/src/status_im/ui/screens/network/edit_network/views.cljs +++ b/src/status_im/ui/screens/network/edit_network/views.cljs @@ -34,6 +34,13 @@ :default-value (get-in manage-network [:name :value]) :on-change-text #(re-frame/dispatch [::network/input-changed :name %]) :auto-focus true}]] + [react/view {:padding-vertical 8} + [quo/text-input + {:label (i18n/label :t/symbol) + :placeholder (i18n/label :t/specify-symbol) + :default-value (get-in manage-network [:symbol :value]) + :on-change-text #(re-frame/dispatch [::network/input-changed :symbol %]) + :auto-focus true}]] [react/view {:padding-vertical 8} [quo/text-input {:label (i18n/label :t/rpc-url) diff --git a/src/status_im/ui/screens/signing/views.cljs b/src/status_im/ui/screens/signing/views.cljs index f1042d7807..1b1ddf1eb4 100644 --- a/src/status_im/ui/screens/signing/views.cljs +++ b/src/status_im/ui/screens/signing/views.cljs @@ -410,7 +410,7 @@ [{:keys [from contact amount token cancel?] :as tx}] (views/letsubs [fee [:signing/fee] sign [:signing/sign] - chain [:ethereum/chain-keyword] + chain [:current-network] {:keys [amount-error gas-error gas-error-state]} [:signing/amount-errors (:address from)] keycard-multiaccount? [:keycard-multiaccount?] diff --git a/src/status_im/ui/screens/wallet/account/views.cljs b/src/status_im/ui/screens/wallet/account/views.cljs index d70d4ef132..d0deeae362 100644 --- a/src/status_im/ui/screens/wallet/account/views.cljs +++ b/src/status_im/ui/screens/wallet/account/views.cljs @@ -102,14 +102,15 @@ currency [:wallet/currency] opensea-enabled? [:opensea-enabled?] collectible-collection [:wallet/collectible-collection address] - mainnet? [:mainnet?]] + mainnet? [:mainnet?] + ethereum-network? [:ethereum-network?]] (let [{:keys [tab]} @state] [react/view {:flex 1} [react/view {:flex-direction :row :margin-bottom 8 :padding-horizontal 4} [tabs/tab-title state :assets (i18n/label :t/wallet-assets) (= tab :assets)] - (when mainnet? + (when ethereum-network? [tabs/tab-title state :nft (i18n/label :t/wallet-collectibles) (= tab :nft)]) - (when mainnet? + (when ethereum-network? [tabs/tab-title state :history (i18n/label :t/history) (= tab :history)])] (cond (= tab :assets) diff --git a/src/status_im/wallet/core.cljs b/src/status_im/wallet/core.cljs index ae1dec1b84..bcf54a3ef4 100644 --- a/src/status_im/wallet/core.cljs +++ b/src/status_im/wallet/core.cljs @@ -503,14 +503,10 @@ [{:keys [db]} chat-id {:keys [value contract] :as request-parameters}] (let [identity (:current-chat-id db) all-tokens (:wallet/all-tokens db) - current-network-string (:networks/current-network db) - all-networks (:networks/networks db) - current-network (get all-networks current-network-string) - chain (ethereum/network->chain-keyword current-network) {:keys [symbol decimals]} (if (seq contract) (get all-tokens contract) - (tokens/native-currency chain)) + (tokens/native-currency (ethereum/get-current-network db))) amount-text (str (money/internal->formatted value symbol decimals))] {:db (assoc db :wallet/prepare-transaction {:from (ethereum/get-default-account (:multiaccount/accounts db)) diff --git a/src/status_im/wallet/prices.cljs b/src/status_im/wallet/prices.cljs index 81f7e61dcf..495add421a 100644 --- a/src/status_im/wallet/prices.cljs +++ b/src/status_im/wallet/prices.cljs @@ -58,7 +58,7 @@ {:wallet/get-prices {:from (if mainnet? (conj tokens "ETH") - [(-> (tokens/native-currency chain) + [(-> (tokens/native-currency (ethereum/get-current-network db)) (wallet.utils/exchange-symbol))]) :to [(:code currency)] :mainnet? mainnet? diff --git a/translations/en.json b/translations/en.json index 5fac774c2b..29772d8c23 100644 --- a/translations/en.json +++ b/translations/en.json @@ -1157,6 +1157,7 @@ "soon": "Soon", "specify-address": "Specify address", "specify-name": "Specify a name", + "specify-symbol": "Specify a symbol", "specify-network-id": "Specify network id", "specify-rpc-url": "Specify a RPC URL", "start-chat": "Start chat",