[#18739] Fix ENS resolution (#19238)

* Resolve ENS on address-to-watch screen based on test-net settings

* Resolve ENS on `:wallet/search-ens` event based on test-net settings
This commit is contained in:
Ulises Manuel 2024-04-10 11:09:40 -06:00 committed by GitHub
parent ec53f17897
commit ce7912632c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 49 deletions

View File

@ -1,6 +1,6 @@
(ns status-im.contexts.wallet.accounts.add-account.address-to-watch.events (ns status-im.contexts.wallet.accounts.add-account.address-to-watch.events
(:require [clojure.string :as string] (:require [clojure.string :as string]
[status-im.constants :as constants] [status-im.contexts.wallet.common.utils :as utils]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[utils.re-frame :as rf])) [utils.re-frame :as rf]))
@ -33,9 +33,10 @@
(rf/reg-event-fx (rf/reg-event-fx
:wallet/get-address-details :wallet/get-address-details
(fn [{:keys [db]} [address-or-ens]] (fn [{db :db} [address-or-ens]]
(let [request-params [constants/ethereum-mainnet-chain-id address-or-ens] (let [ens? (string/includes? address-or-ens ".")
ens? (string/includes? address-or-ens ".")] chain-id (utils/network->chain-id db :mainnet)
request-params [chain-id address-or-ens]]
{:db (-> db {:db (-> db
(assoc-in [:wallet :ui :add-address-to-watch :activity-state] :scanning) (assoc-in [:wallet :ui :add-address-to-watch :activity-state] :scanning)
(assoc-in [:wallet :ui :add-address-to-watch :validated-address] nil)) (assoc-in [:wallet :ui :add-address-to-watch :validated-address] nil))

View File

@ -201,31 +201,36 @@
mainnet-chain-id)) mainnet-chain-id))
(defn network->chain-id (defn network->chain-id
[{:keys [network testnet-enabled? goerli-enabled?]}] ([db network]
(condp contains? (keyword network) (let [{:keys [test-networks-enabled? is-goerli-enabled?]} (:profile/profile db)]
#{constants/mainnet-network-name (keyword constants/mainnet-short-name)} (network->chain-id {:network network
(get-chain-id :testnet-enabled? test-networks-enabled?
{:mainnet-chain-id constants/ethereum-mainnet-chain-id :goerli-enabled? is-goerli-enabled?})))
:sepolia-chain-id constants/ethereum-sepolia-chain-id ([{:keys [network testnet-enabled? goerli-enabled?]}]
:goerli-chain-id constants/ethereum-goerli-chain-id (condp contains? (keyword network)
:testnet-enabled? testnet-enabled? #{constants/mainnet-network-name (keyword constants/mainnet-short-name)}
:goerli-enabled? goerli-enabled?}) (get-chain-id
{:mainnet-chain-id constants/ethereum-mainnet-chain-id
:sepolia-chain-id constants/ethereum-sepolia-chain-id
:goerli-chain-id constants/ethereum-goerli-chain-id
:testnet-enabled? testnet-enabled?
:goerli-enabled? goerli-enabled?})
#{constants/optimism-network-name (keyword constants/optimism-short-name)} #{constants/optimism-network-name (keyword constants/optimism-short-name)}
(get-chain-id (get-chain-id
{:mainnet-chain-id constants/optimism-mainnet-chain-id {:mainnet-chain-id constants/optimism-mainnet-chain-id
:sepolia-chain-id constants/optimism-sepolia-chain-id :sepolia-chain-id constants/optimism-sepolia-chain-id
:goerli-chain-id constants/optimism-goerli-chain-id :goerli-chain-id constants/optimism-goerli-chain-id
:testnet-enabled? testnet-enabled? :testnet-enabled? testnet-enabled?
:goerli-enabled? goerli-enabled?}) :goerli-enabled? goerli-enabled?})
#{constants/arbitrum-network-name (keyword constants/arbitrum-short-name)} #{constants/arbitrum-network-name (keyword constants/arbitrum-short-name)}
(get-chain-id (get-chain-id
{:mainnet-chain-id constants/arbitrum-mainnet-chain-id {:mainnet-chain-id constants/arbitrum-mainnet-chain-id
:sepolia-chain-id constants/arbitrum-sepolia-chain-id :sepolia-chain-id constants/arbitrum-sepolia-chain-id
:goerli-chain-id constants/arbitrum-goerli-chain-id :goerli-chain-id constants/arbitrum-goerli-chain-id
:testnet-enabled? testnet-enabled? :testnet-enabled? testnet-enabled?
:goerli-enabled? goerli-enabled?}))) :goerli-enabled? goerli-enabled?}))))
(defn get-standard-fiat-format (defn get-standard-fiat-format
[crypto-value currency-symbol fiat-value] [crypto-value currency-symbol fiat-value]

View File

@ -281,46 +281,50 @@
data)}] data)}]
{:db (assoc-in db [:wallet :networks] network-data)}))) {:db (assoc-in db [:wallet :networks] network-data)})))
(rf/reg-event-fx :wallet/find-ens (rf/reg-event-fx
(fn [{:keys [db]} [input contacts chain-id cb]] :wallet/find-ens
(fn [{:keys [db]} [input contacts on-error-fn]]
(let [result (if (empty? input) (let [result (if (empty? input)
[] []
(filter #(string/starts-with? (or (:ens-name %) "") input) contacts))] (filter #(string/starts-with? (or (:ens-name %) "") input) contacts))]
(if (and input (empty? result)) (if (and input (empty? result))
(rf/dispatch [:wallet/search-ens input chain-id cb ".stateofus.eth"]) (rf/dispatch [:wallet/search-ens input on-error-fn ".stateofus.eth"])
{:db (-> db {:db (-> db
(assoc-in [:wallet :ui :search-address :local-suggestions] (assoc-in [:wallet :ui :search-address :local-suggestions]
(map #(assoc % :type item-types/saved-address) result)) (map #(assoc % :type item-types/saved-address) result))
(assoc-in [:wallet :ui :search-address :valid-ens-or-address?] (assoc-in [:wallet :ui :search-address :valid-ens-or-address?]
(not-empty result)))})))) (not-empty result)))}))))
(rf/reg-event-fx :wallet/search-ens (rf/reg-event-fx
(fn [_ [input chain-id cb domain]] :wallet/search-ens
(let [ens (if (string/includes? input ".") input (str input domain))] (fn [{db :db} [input on-error-fn domain]]
(let [ens (if (string/includes? input ".")
input
(str input domain))
chain-id (utils/network->chain-id db :mainnet)]
{:fx [[:json-rpc/call {:fx [[:json-rpc/call
[{:method "ens_addressOf" [{:method "ens_addressOf"
:params [chain-id ens] :params [chain-id ens]
:on-success #(rf/dispatch [:wallet/set-ens-address % ens]) :on-success #(rf/dispatch [:wallet/set-ens-address % ens])
:on-error (fn [] :on-error (fn []
(if (= domain ".stateofus.eth") (if (= domain ".stateofus.eth")
(rf/dispatch [:wallet/search-ens input chain-id cb ".eth"]) (rf/dispatch [:wallet/search-ens input on-error-fn ".eth"])
(do (do
(rf/dispatch [:wallet/set-ens-address nil ens]) (rf/dispatch [:wallet/set-ens-address nil ens])
(cb))))}]]]}))) (on-error-fn))))}]]]})))
(rf/reg-event-fx :wallet/set-ens-address (rf/reg-event-fx
:wallet/set-ens-address
(fn [{:keys [db]} [result ens]] (fn [{:keys [db]} [result ens]]
{:db (let [suggestion (if result
(-> db [{:type item-types/address
(assoc-in [:wallet :ui :search-address :local-suggestions] :ens ens
(if result :address (eip55/address->checksum result)
[{:type item-types/address :networks [:ethereum :optimism]}]
:ens ens [])]
:address (eip55/address->checksum result) {:db (-> db
:networks [:ethereum :optimism]}] (assoc-in [:wallet :ui :search-address :local-suggestions] suggestion)
[])) (assoc-in [:wallet :ui :search-address :valid-ens-or-address?] (boolean result)))})))
(assoc-in [:wallet :ui :search-address :valid-ens-or-address?]
(boolean result)))}))
(rf/reg-event-fx :wallet/fetch-address-suggestions (rf/reg-event-fx :wallet/fetch-address-suggestions
(fn [{:keys [db]} [_address]] (fn [{:keys [db]} [_address]]

View File

@ -30,7 +30,6 @@
recipient (rf/sub [:wallet/wallet-send-recipient]) recipient (rf/sub [:wallet/wallet-send-recipient])
recipient-plain-address? (= send-address recipient) recipient-plain-address? (= send-address recipient)
valid-ens-or-address? (rf/sub [:wallet/valid-ens-or-address?]) valid-ens-or-address? (rf/sub [:wallet/valid-ens-or-address?])
chain-id (rf/sub [:chain-id])
contacts (rf/sub [:contacts/active])] contacts (rf/sub [:contacts/active])]
[quo/address-input [quo/address-input
{:on-focus #(reset! input-focused? true) {:on-focus #(reset! input-focused? true)
@ -58,7 +57,7 @@
; is loaded but not being shown to the user (deep in the ; is loaded but not being shown to the user (deep in the
; navigation stack) and avoid undesired behaviors ; navigation stack) and avoid undesired behaviors
(debounce/debounce-and-dispatch (debounce/debounce-and-dispatch
[:wallet/find-ens text contacts chain-id cb] [:wallet/find-ens text contacts cb]
300))) 300)))
:on-change-text (fn [text] :on-change-text (fn [text]
(when (empty? text) (when (empty? text)