Skip to content

Commit

Permalink
custom network symbol
Browse files Browse the repository at this point in the history
Signed-off-by: andrey <motor4ik@gmail.com>
  • Loading branch information
flexsurfer committed Nov 16, 2021
1 parent f448f01 commit c16b086
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 38 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ DATABASE_MANAGEMENT_ENABLED=1
METRICS_ENABLED=0
DELETE_MESSAGE_ENABLED=1
COLLECTIBLES_ENABLED=1
COMMANDS_ENABLED=1
12 changes: 8 additions & 4 deletions src/status_im/ethereum/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
15 changes: 9 additions & 6 deletions src/status_im/ethereum/tokens.cljs
Original file line number Diff line number Diff line change
@@ -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")}})))

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/status_im/network/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
(def default-manage
{:name {:value ""}
:url {:value ""}
:symbol {:value ""}
:chain {:value :mainnet}})

(defn validate-string [{:keys [value]}]
Expand All @@ -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)))

Expand Down Expand Up @@ -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))
Expand All @@ -160,6 +162,7 @@
:URL upstream-url}}]
{:id random-id
:name network-name
:symbol symbol
:config config}))

(fx/defn save
Expand All @@ -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))
Expand Down
24 changes: 23 additions & 1 deletion src/status_im/network/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -63,28 +67,40 @@
(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"
(is (= {:db {:networks/manage {:url {:value "http://valid.com"
:error false}
:name {:value ""
:error true}
:symbol {:value "symbol"
:error false}
:chain {:value "mainnet"
:error false}
:network-id {:value nil
Expand All @@ -93,6 +109,8 @@
:error true}
:name {:value ""
:error false}
:symbol {:value "symbol"
:error false}
:chain {:value "mainnet"
:error false}}}}
:url "http://valid.com")))))
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/notifications/local.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/signing/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 18 additions & 12 deletions src/status_im/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 ==============================================================================================================

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down
3 changes: 2 additions & 1 deletion src/status_im/ui/screens/chat/message/command.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
7 changes: 7 additions & 0 deletions src/status_im/ui/screens/network/edit_network/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/ui/screens/signing/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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?]
Expand Down
7 changes: 4 additions & 3 deletions src/status_im/ui/screens/wallet/account/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions src/status_im/wallet/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/wallet/prices.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit c16b086

Please sign in to comment.