discover token from status-go

Signed-off-by: andrey <motor4ik@gmail.com>
This commit is contained in:
andrey 2022-05-12 11:19:35 +02:00
parent c3e599721f
commit 2d6872976e
No known key found for this signature in database
GPG Key ID: 89B67245FD2F0272
9 changed files with 30 additions and 229 deletions

1
.env
View File

@ -2,7 +2,6 @@ CACHED_WEBVIEWS_ENABLED=1
DEBUG_WEBVIEW=1 DEBUG_WEBVIEW=1
DEFAULT_NETWORK=mainnet_rpc DEFAULT_NETWORK=mainnet_rpc
DEV_BUILD=1 DEV_BUILD=1
ERC20_CONTRACT_WARNINGS=0
ETHEREUM_DEV_CLUSTER=1 ETHEREUM_DEV_CLUSTER=1
EXTENSIONS=0 EXTENSIONS=0
FLEET=eth.prod FLEET=eth.prod

View File

@ -1,6 +1,5 @@
DEBUG_WEBVIEW=1 DEBUG_WEBVIEW=1
DEFAULT_NETWORK=testnet_rpc DEFAULT_NETWORK=testnet_rpc
ERC20_CONTRACT_WARNINGS=1
ETHEREUM_DEV_CLUSTER=1 ETHEREUM_DEV_CLUSTER=1
EXTENSIONS=0 EXTENSIONS=0
FLEET=eth.staging FLEET=eth.staging

View File

@ -1,7 +1,6 @@
CACHED_WEBVIEWS_ENABLED=1 CACHED_WEBVIEWS_ENABLED=1
DEBUG_WEBVIEW=1 DEBUG_WEBVIEW=1
DEFAULT_NETWORK=testnet_rpc DEFAULT_NETWORK=testnet_rpc
ERC20_CONTRACT_WARNINGS=0
ETHEREUM_DEV_CLUSTER=1 ETHEREUM_DEV_CLUSTER=1
EXTENSIONS=0 EXTENSIONS=0
FLEET=eth.prod FLEET=eth.prod

View File

@ -1,6 +1,5 @@
DEBUG_WEBVIEW=1 DEBUG_WEBVIEW=1
DEFAULT_NETWORK=mainnet_rpc DEFAULT_NETWORK=mainnet_rpc
ERC20_CONTRACT_WARNINGS=0
ETHEREUM_DEV_CLUSTER=1 ETHEREUM_DEV_CLUSTER=1
EXTENSIONS=0 EXTENSIONS=0
FLEET=eth.prod FLEET=eth.prod

View File

@ -1,6 +1,5 @@
DEBUG_WEBVIEW=0 DEBUG_WEBVIEW=0
DEFAULT_NETWORK=mainnet_rpc DEFAULT_NETWORK=mainnet_rpc
ERC20_CONTRACT_WARNINGS=0
ETHEREUM_DEV_CLUSTER=0 ETHEREUM_DEV_CLUSTER=0
EXTENSIONS=0 EXTENSIONS=0
FLEET=eth.prod FLEET=eth.prod

View File

@ -94,37 +94,25 @@
(common/set-on-card-connected :keycard/sign-typed-data) (common/set-on-card-connected :keycard/sign-typed-data)
{:db (assoc-in db [:signing/sign :keycard-step] :signing)})))) {:db (assoc-in db [:signing/sign :keycard-step] :signing)}))))
(fx/defn fetch-currency-symbol-on-success (fx/defn fetch-currency-token-on-success
{:events [:keycard/fetch-currency-symbol-on-success]} {:events [:keycard/fetch-currency-token-on-success]}
[{:keys [db] :as cofx} currency] [{:keys [db]} {:keys [decimals symbol]}]
{:db (assoc-in db [:signing/sign :formatted-data :message :formatted-currency] currency)}) {:db (-> db
(assoc-in [:signing/sign :formatted-data :message :formatted-currency] symbol)
(fx/defn fetch-currency-decimals-on-success (update-in [:signing/sign :formatted-data :message]
{:events [:keycard/fetch-currency-decimals-on-success]}
[{:keys [db] :as cofx} decimals]
{:db (update-in db [:signing/sign :formatted-data :message]
#(assoc % :formatted-amount (.dividedBy ^js (money/bignumber (:amount %)) #(assoc % :formatted-amount (.dividedBy ^js (money/bignumber (:amount %))
(money/bignumber (money/from-decimal decimals)))))}) (money/bignumber (money/from-decimal decimals))))))})
(fx/defn store-hash-and-sign-typed (fx/defn store-hash-and-sign-typed
{:events [:keycard/store-hash-and-sign-typed]} {:events [:keycard/store-hash-and-sign-typed]}
[{:keys [db] :as cofx} result] [{:keys [db] :as cofx} result]
(let [{:keys [result error]} (types/json->clj result) (let [{:keys [result]} (types/json->clj result)
message (get-in db [:signing/sign :formatted-data :message]) message (get-in db [:signing/sign :formatted-data :message])
currency-contract (:currency message)] currency-contract (:currency message)]
(when currency-contract (when currency-contract
(json-rpc/eth-call {:contract currency-contract {::json-rpc/call [{:method "wallet_discoverToken"
:method "decimals()" :params [(ethereum/chain-id db) currency-contract]
:outputs ["uint8"] :on-success #(re-frame/dispatch [:keycard/fetch-currency-token-on-success %])}]})
:on-success (fn [[decimals]]
(re-frame/dispatch [:keycard/fetch-currency-decimals-on-success decimals]))})
(json-rpc/eth-call {:contract currency-contract
:method "symbol()"
:outputs ["string"]
:on-success (fn [[currency]]
(re-frame/dispatch [:keycard/fetch-currency-symbol-on-success currency]))}))
(fx/merge cofx (fx/merge cofx
{:db (assoc-in db [:keycard :hash] result)} {:db (assoc-in db [:keycard :hash] result)}
sign-typed-data))) sign-typed-data)))

View File

@ -30,7 +30,6 @@
(def cached-webviews-enabled? (enabled? (get-config :CACHED_WEBVIEWS_ENABLED 0))) (def cached-webviews-enabled? (enabled? (get-config :CACHED_WEBVIEWS_ENABLED 0)))
(def snoopy-enabled? (enabled? (get-config :SNOOPY 0))) (def snoopy-enabled? (enabled? (get-config :SNOOPY 0)))
(def dev-build? (enabled? (get-config :DEV_BUILD 0))) (def dev-build? (enabled? (get-config :DEV_BUILD 0)))
(def erc20-contract-warnings-enabled? (enabled? (get-config :ERC20_CONTRACT_WARNINGS)))
(def tr-to-talk-enabled? (enabled? (get-config :TRIBUTE_TO_TALK 0))) (def tr-to-talk-enabled? (enabled? (get-config :TRIBUTE_TO_TALK 0)))
(def max-message-delivery-attempts (js/parseInt (get-config :MAX_MESSAGE_DELIVERY_ATTEMPTS "6"))) (def max-message-delivery-attempts (js/parseInt (get-config :MAX_MESSAGE_DELIVERY_ATTEMPTS "6")))
(def max-images-batch (js/parseInt (get-config :MAX_IMAGES_BATCH "1"))) (def max-images-batch (js/parseInt (get-config :MAX_IMAGES_BATCH "1")))

View File

@ -87,82 +87,11 @@
[cofx hash address] [cofx hash address]
(navigation/navigate-to-cofx cofx :wallet-transaction-details {:hash hash :address address})) (navigation/navigate-to-cofx cofx :wallet-transaction-details {:hash hash :address address}))
(defn- validate-token-name!
[{:keys [address symbol name]}]
(json-rpc/eth-call
{:contract address
:method "name()"
:outputs ["string"]
:on-success
(fn [[contract-name]]
(when (and (seq contract-name)
(not= name contract-name))
(let [message (i18n/label :t/token-auto-validate-name-error
{:symbol symbol
:expected name
:actual contract-name
:address address})]
(log/warn message)
(utils.utils/show-popup (i18n/label :t/warning) message))))}))
(defn- validate-token-symbol!
[{:keys [address symbol]}]
(when-not (or (= symbol :DCN) (= symbol :SUPRR)) ;; ignore this symbol because it has weird symbol
(json-rpc/eth-call
{:contract address
:method "symbol()"
:outputs ["string"]
:on-success
(fn [[contract-symbol]]
;;NOTE(goranjovic): skipping check if field not set in contract
(when (and (seq contract-symbol)
(not= (clojure.core/name symbol) contract-symbol))
(let [message (i18n/label :t/token-auto-validate-symbol-error
{:symbol symbol
:expected (clojure.core/name symbol)
:actual contract-symbol
:address address})]
(log/warn message)
(utils.utils/show-popup (i18n/label :t/warning) message))))})))
(defn- validate-token-decimals!
[{:keys [address symbol decimals nft?]}]
(when-not nft?
(json-rpc/eth-call
{:contract address
:method "decimals()"
:outputs ["uint256"]
:on-success
(fn [[contract-decimals]]
(when (and (not (nil? contract-decimals))
(not= decimals contract-decimals))
(let [message (i18n/label :t/token-auto-validate-decimals-error
{:symbol symbol
:expected decimals
:actual contract-decimals
:address address})]
(log/warn message)
(utils.utils/show-popup (i18n/label :t/warning) message))))})))
(defn dups [seq] (defn dups [seq]
(for [[id freq] (frequencies seq) (for [[id freq] (frequencies seq)
:when (> freq 1)] :when (> freq 1)]
id)) id))
(re-frame/reg-fx
:wallet/validate-tokens
(fn [[tokens all-default-tokens]]
(let [symb-dups (dups (map :symbol all-default-tokens))
addr-dups (dups (map :address all-default-tokens))]
(when (seq symb-dups)
(utils.utils/show-popup (i18n/label :t/warning) (str "Duplicated tokens symbols" symb-dups)))
(when (seq addr-dups)
(utils.utils/show-popup (i18n/label :t/warning) (str "Duplicated tokens addresses" addr-dups)))
(doseq [token (vals tokens)]
(validate-token-decimals! token)
(validate-token-symbol! token)
(validate-token-name! token)))))
(defn- clean-up-results (defn- clean-up-results
"remove empty balances "remove empty balances
if there is no visible assets, returns all positive balances if there is no visible assets, returns all positive balances
@ -277,10 +206,7 @@
(let [default-tokens (utils.core/index-by :address tokens) (let [default-tokens (utils.core/index-by :address tokens)
;;we want to override custom-tokens by default ;;we want to override custom-tokens by default
all-tokens (merge (rpc->token custom-tokens) default-tokens)] all-tokens (merge (rpc->token custom-tokens) default-tokens)]
(merge {:db (assoc db :wallet/all-tokens all-tokens)}))
{:db (assoc db :wallet/all-tokens all-tokens)}
(when config/erc20-contract-warnings-enabled?
{:wallet/validate-tokens [default-tokens tokens]}))))
(fx/defn initialize-favourites (fx/defn initialize-favourites
[{:keys [db]} favourites] [{:keys [db]} favourites]

View File

@ -7,101 +7,21 @@
[quo.design-system.colors :as colors] [quo.design-system.colors :as colors]
[status-im.ui.components.react :as react] [status-im.ui.components.react :as react]
[status-im.utils.fx :as fx] [status-im.utils.fx :as fx]
[status-im.utils.money :as money]
[status-im.wallet.core :as wallet] [status-im.wallet.core :as wallet]
[status-im.navigation :as navigation] [status-im.navigation :as navigation]
[status-im.wallet.prices :as prices])) [status-im.wallet.prices :as prices]))
(re-frame/reg-fx
:wallet.custom-token/get-decimals
(fn [contract]
(json-rpc/eth-call
{:contract contract
:method "decimals()"
:outputs ["uint256"]
:on-error #(re-frame/dispatch [:wallet.custom-token/not-supported])
:on-success
(fn [[contract-decimals]]
(re-frame/dispatch [:wallet.custom-token/decimals-result
contract-decimals]))})))
(re-frame/reg-fx
:wallet.custom-token/get-symbol
(fn [contract]
(json-rpc/eth-call
{:contract contract
:method "symbol()"
:outputs ["string"]
:on-error #(re-frame/dispatch [:wallet.custom-token/not-supported])
:on-success
(fn [[contract-symbol]]
(re-frame/dispatch [:wallet.custom-token/symbol-result
contract
contract-symbol]))})))
(re-frame/reg-fx
:wallet.custom-token/get-balance
(fn [[contract wallet-address]]
(json-rpc/eth-call
{:contract contract
:method "balanceOf(address)"
:params [wallet-address]
:outputs ["uint256"]
:on-success
(fn [[balance]]
(re-frame/dispatch [:wallet.custom-token/balance-result
contract
(money/bignumber balance)]))})))
(re-frame/reg-fx
:wallet.custom-token/get-name
(fn [contract]
(json-rpc/eth-call
{:contract contract
:method "name()"
:outputs ["string"]
:on-error #(re-frame/dispatch [:wallet.custom-token/not-supported])
:on-success
(fn [[contract-name]]
(re-frame/dispatch [:wallet.custom-token/name-result
contract
contract-name]))})))
(re-frame/reg-fx
:wallet.custom-token/get-total-supply
(fn [contract]
(json-rpc/eth-call
{:contract contract
:method "totalSupply()"
:outputs ["uint256"]
:on-success
(fn [[contract-total-supply]]
(re-frame/dispatch [:wallet.custom-token/total-supply-result
contract
(money/bignumber contract-total-supply)]))})))
(re-frame/reg-fx (re-frame/reg-fx
:wallet.custom-token/contract-address-paste :wallet.custom-token/contract-address-paste
(fn [] (fn []
(react/get-from-clipboard (react/get-from-clipboard
#(re-frame/dispatch [:wallet.custom-token/contract-address-is-pasted #(re-frame/dispatch [:wallet.custom-token/contract-address-is-pasted (string/trim %)]))))
(string/trim %)]))))
(defn field-exists? (defn field-exists?
[{:wallet/keys [all-tokens]} field-key field-value] [{:wallet/keys [all-tokens]} field-key field-value]
(some #(= field-value (get % field-key)) (some #(= field-value (get % field-key))
(vals all-tokens))) (vals all-tokens)))
(fx/defn total-supply-result
{:events [:wallet.custom-token/total-supply-result]}
[{:keys [db]} contract total-supply]
(if (money/valid? total-supply)
{:wallet.custom-token/get-name contract}
{:db (update db
:wallet/custom-token-screen
merge {:in-progress? nil
:error (i18n/label :t/wrong-contract)})}))
(defn token-in-list? (defn token-in-list?
[{:wallet/keys [all-tokens]} contract] [{:wallet/keys [all-tokens]} contract]
(not (nil? (get all-tokens (string/lower-case contract))))) (not (nil? (get all-tokens (string/lower-case contract)))))
@ -117,40 +37,27 @@
{:db (assoc db {:db (assoc db
:wallet/custom-token-screen :wallet/custom-token-screen
{:contract contract :in-progress? true}) {:contract contract :in-progress? true})
:wallet.custom-token/get-total-supply contract}) ::json-rpc/call [{:method "wallet_discoverToken"
:params [(ethereum/chain-id db) contract]
:on-success #(re-frame/dispatch [:wallet.custom-token/token-discover-result %])
:on-error #(re-frame/dispatch [:wallet.custom-token/not-supported])}]})
{:db (assoc db {:db (assoc db
:wallet/custom-token-screen :wallet/custom-token-screen
{:contract contract {:contract contract
:error (i18n/label :t/wrong-address)})})) :error (i18n/label :t/wrong-address)})}))
(fx/defn decimals-result (fx/defn token-discover-result
{:events [:wallet.custom-token/decimals-result]} {:events [:wallet.custom-token/token-discover-result]}
[{:keys [db]} result] [{:keys [db]} {:keys [name symbol decimals]}]
(let [symbol-exists? (field-exists? db :symbol (keyword symbol))]
{:db (update db {:db (update db
:wallet/custom-token-screen
merge
{:decimals (str result)
:in-progress? nil})})
(fx/defn symbol-result
{:events [:wallet.custom-token/symbol-result]}
[{:keys [db]} contract token-symbol]
(let [symbol-exists? (field-exists? db :symbol (keyword token-symbol))]
{:db
(update db
:wallet/custom-token-screen merge :wallet/custom-token-screen merge
{:symbol token-symbol {:name name
:symbol symbol
:error-symbol (when symbol-exists? :error-symbol (when symbol-exists?
(i18n/label :t/you-already-have-an-asset {:value token-symbol}))}) (i18n/label :t/you-already-have-an-asset {:value symbol}))
:wallet.custom-token/get-decimals :decimals (str decimals)
contract})) :in-progress? nil})}))
(fx/defn name-result
{:events [:wallet.custom-token/name-result]}
[{:keys [db]} contract token-name]
{:db
(update db :wallet/custom-token-screen merge {:name token-name})
:wallet.custom-token/get-symbol contract})
(fx/defn not-supported (fx/defn not-supported
{:events [:wallet.custom-token/not-supported]} {:events [:wallet.custom-token/not-supported]}
@ -158,20 +65,6 @@
{:db (assoc-in db [:wallet/custom-token-screen :in-progress?] nil) {:db (assoc-in db [:wallet/custom-token-screen :in-progress?] nil)
:utils/show-popup {:content (i18n/label :t/contract-isnt-supported)}}) :utils/show-popup {:content (i18n/label :t/contract-isnt-supported)}})
(fx/defn balance-result
{:events [:wallet.custom-token/balance-result]}
[{:keys [db]} contract balance]
(if (money/valid? balance)
{:db (assoc-in db
[:wallet/custom-token-screen :balance]
(str balance))
:wallet.custom-token/get-name contract}
{:db (update db
:wallet/custom-token-screen
merge
{:in-progress? nil
:error (i18n/label :t/wrong-contract)})}))
(fx/defn add-custom-token (fx/defn add-custom-token
{:events [:wallet.custom-token.ui/add-pressed]} {:events [:wallet.custom-token.ui/add-pressed]}
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]