From 96916269926ea6909cd32a4990989968e72b7707 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 20 Oct 2020 17:13:42 +0300 Subject: [PATCH] [#11189] Backward compatible ENS registrar --- src/status_im/ens/core.cljs | 38 ++++++++++++++----------- src/status_im/ethereum/stateofus.cljs | 22 ++++++++++++-- src/status_im/subs.cljs | 15 ++-------- src/status_im/ui/screens/ens/views.cljs | 4 +-- 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/status_im/ens/core.cljs b/src/status_im/ens/core.cljs index 39db5902d6..c3fdda634a 100644 --- a/src/status_im/ens/core.cljs +++ b/src/status_im/ens/core.cljs @@ -69,8 +69,11 @@ (re-frame/reg-fx ::get-expiration-time - (fn [[registrar label-hash cb]] - (stateofus/get-expiration-time registrar label-hash cb))) + (fn [[chain label-hash cb]] + (stateofus/get-registrar + chain + (fn [registrar] + (stateofus/get-expiration-time registrar label-hash cb))))) (fx/defn set-state {:events [::name-resolved]} @@ -166,19 +169,21 @@ address (ethereum/default-address db) chain (ethereum/chain-keyword db) chain-id (ethereum/chain-id db) - contract (get stateofus/registrars chain) amount (registration-cost chain-id) {:keys [x y]} (ethereum/coordinates public-key)] - (signing/eth-transaction-call - cofx - {:contract (contracts/get-address db :status/snt) - :method "approveAndCall(address,uint256,bytes)" - :params [contract - (money/unit->token amount 18) - (abi-spec/encode "register(bytes32,address,bytes32,bytes32)" - [(ethereum/sha3 username) address x y])] - :on-result [:update-ens-tx-state-and-redirect :submitted username custom-domain?] - :on-error [::on-registration-failure]}))) + (stateofus/get-registrar + chain + (fn [contract] + (signing/eth-transaction-call + cofx + {:contract (contracts/get-address db :status/snt) + :method "approveAndCall(address,uint256,bytes)" + :params [contract + (money/unit->token amount 18) + (abi-spec/encode "register(bytes32,address,bytes32,bytes32)" + [(ethereum/sha3 username) address x y])] + :on-result [:update-ens-tx-state-and-redirect :submitted username custom-domain?] + :on-error [::on-registration-failure]}))))) (defn- valid-custom-domain? [username] (and (ens/is-valid-eth-name? username) @@ -280,11 +285,10 @@ {:events [::navigate-to-name]} [{:keys [db] :as cofx} username] (let [chain (ethereum/chain-keyword db) - registry (get ens/ens-registries chain) - registrar (get stateofus/registrars chain)] + registry (get ens/ens-registries chain)] (fx/merge cofx {::get-expiration-time - [registrar + [chain (-> username stateofus/username ethereum/sha3) @@ -317,4 +321,4 @@ (when (= name preferred-name) (multiaccounts.update/multiaccount-update :preferred-name (first new-names) {})) - (navigation/navigate-back)))) \ No newline at end of file + (navigation/navigate-back)))) diff --git a/src/status_im/ethereum/stateofus.cljs b/src/status_im/ethereum/stateofus.cljs index 9febfc1df5..2412909b9d 100644 --- a/src/status_im/ethereum/stateofus.cljs +++ b/src/status_im/ethereum/stateofus.cljs @@ -1,6 +1,7 @@ (ns status-im.ethereum.stateofus (:require [clojure.string :as string] - [status-im.ethereum.json-rpc :as json-rpc])) + [status-im.ethereum.json-rpc :as json-rpc] + [status-im.ethereum.ens :as ens])) (def domain "stateofus.eth") @@ -24,12 +25,29 @@ (when (and name (string/ends-with? name domain)) (first (string/split name ".")))) -(def registrars +(def old-registrars {:mainnet "0xDB5ac1a559b02E12F29fC0eC0e37Be8E046DEF49" ;;NOTE: can be enabled for testing builds ;;:testnet "0x11d9F481effd20D76cEE832559bd9Aca25405841" }) +(def registrars-cache (atom {})) + +(defn get-registrar [chain callback] + (if-let [contract (get @registrars-cache chain)] + (callback contract) + (let [registry (get ens/ens-registries chain)] + (ens/get-owner + registry + domain + (fn [addr] + (let [addr (or addr (get old-registrars chain))] + (swap! registrars-cache assoc chain addr) + (callback addr))))))) + +(defn get-cached-registrar [chain] + (get @registrars-cache chain)) + (defn lower-case? [s] (when s (= s (string/lower-case s)))) diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index 819dc0b4d3..53833ad4b4 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -10,7 +10,6 @@ [status-im.contact.db :as contact.db] [status-im.ens.core :as ens] [status-im.ethereum.core :as ethereum] - [status-im.ethereum.stateofus :as stateofus] [status-im.ethereum.tokens :as tokens] [status-im.ethereum.transactions.core :as transactions] [status-im.fleet.core :as fleet] @@ -2077,14 +2076,6 @@ (string/ends-with? screen-snt-amount "."))))))))) ;;ENS ================================================================================================================== - -(re-frame/reg-sub - :ens.stateofus/registrar - :<- [:current-network] - (fn [network] - (let [chain (ethereum/network->chain-keyword network)] - (get stateofus/registrars chain)))) - (re-frame/reg-sub :multiaccount/usernames :<- [:multiaccount] @@ -2116,18 +2107,18 @@ (re-frame/reg-sub :ens/checkout-screen :<- [:ens/registration] - :<- [:ens.stateofus/registrar] + :<- [:current-network] :<- [:multiaccount/default-account] :<- [:multiaccount/public-key] :<- [:chain-id] :<- [:balance-default] (fn [[{:keys [custom-domain? username]} - registrar default-account public-key chain-id balance]] + network default-account public-key chain-id balance]] {:address (ethereum/normalized-hex (:address default-account)) :username username :public-key public-key :custom-domain? custom-domain? - :contract registrar + :network network :amount-label (ens-amount-label chain-id) :sufficient-funds? (money/sufficient-funds? (money/formatted->internal (money/bignumber 10) :SNT 18) diff --git a/src/status_im/ui/screens/ens/views.cljs b/src/status_im/ui/screens/ens/views.cljs index 7f7be3d3a4..b754ed6044 100644 --- a/src/status_im/ui/screens/ens/views.cljs +++ b/src/status_im/ui/screens/ens/views.cljs @@ -279,7 +279,7 @@ (let [checked? (reagent/atom false)] (fn [] (let [{:keys [username address custom-domain? public-key - contract amount-label sufficient-funds?]} + network amount-label sufficient-funds?]} @(re-frame/subscribe [:ens/checkout-screen])] [react/keyboard-avoiding-view {:flex 1} [toolbar] @@ -306,7 +306,7 @@ :typography :main-medium}} (domain-label custom-domain?)] [react/view {:flex 1 :min-width 24}]]] - [registration checked? contract address public-key]] + [registration checked? (stateofus/get-cached-registrar network) address public-key]] [toolbar/toolbar {:show-border? true :size :large