custom network symbol

Signed-off-by: andrey <motor4ik@gmail.com>
This commit is contained in:
andrey 2021-11-10 15:32:01 +01:00
parent f448f014f6
commit c16b0860ec
No known key found for this signature in database
GPG Key ID: 89B67245FD2F0272
15 changed files with 84 additions and 38 deletions

1
.env
View File

@ -31,3 +31,4 @@ DATABASE_MANAGEMENT_ENABLED=1
METRICS_ENABLED=0
DELETE_MESSAGE_ENABLED=1
COLLECTIBLES_ENABLED=1
COMMANDS_ENABLED=1

View File

@ -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)))

View File

@ -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

View File

@ -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))

View File

@ -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

View File

@ -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))

View File

@ -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

View File

@ -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}

View File

@ -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])

View File

@ -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)

View File

@ -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?]

View File

@ -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)

View File

@ -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))

View File

@ -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?

View File

@ -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",