use call-private-rpc in ethereum/call

- remove web3 parameter for ethereum/call
- remove first parameter for callback of ethereum/call which
was always nil

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2019-03-28 11:03:42 +01:00
parent 1bfa3cf178
commit 884a44b5df
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
16 changed files with 198 additions and 172 deletions

View File

@ -88,7 +88,7 @@
(re-frame/dispatch [:browser.callback/resolve-ens-multihash-error])))) (re-frame/dispatch [:browser.callback/resolve-ens-multihash-error]))))
(fx/defn resolve-url (fx/defn resolve-url
[{{:keys [web3 network] :as db} :db} {:keys [error? resolved-url]}] [{{:keys [network] :as db} :db} {:keys [error? resolved-url]}]
(when (not error?) (when (not error?)
(let [current-url (get-current-url (get-current-browser db)) (let [current-url (get-current-url (get-current-browser db))
host (http/url-host current-url)] host (http/url-host current-url)]
@ -96,22 +96,20 @@
(let [network (get-in db [:account/account :networks network]) (let [network (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network)] chain (ethereum/network->chain-keyword network)]
{:db (update db :browser/options assoc :resolving? true) {:db (update db :browser/options assoc :resolving? true)
:browser/resolve-ens-content {:web3 web3 :browser/resolve-ens-content {:registry (get ens/ens-registries
:registry (get ens/ens-registries
chain) chain)
:ens-name host :ens-name host
:cb resolve-ens-content-callback}}) :cb resolve-ens-content-callback}})
{:db (update db :browser/options assoc :url (or resolved-url current-url) :resolving? false)})))) {:db (update db :browser/options assoc :url (or resolved-url current-url) :resolving? false)}))))
(fx/defn resolve-ens-contenthash (fx/defn resolve-ens-contenthash
[{{:keys [web3 network] :as db} :db}] [{{:keys [network] :as db} :db}]
(let [current-url (get-current-url (get-current-browser db)) (let [current-url (get-current-url (get-current-browser db))
host (http/url-host current-url)] host (http/url-host current-url)]
(let [network (get-in db [:account/account :networks network]) (let [network (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network)] chain (ethereum/network->chain-keyword network)]
{:db (update db :browser/options assoc :resolving? true) {:db (update db :browser/options assoc :resolving? true)
:browser/resolve-ens-contenthash {:web3 web3 :browser/resolve-ens-contenthash {:registry (get ens/ens-registries
:registry (get ens/ens-registries
chain) chain)
:ens-name host :ens-name host
:cb resolve-ens-contenthash-callback}}))) :cb resolve-ens-contenthash-callback}})))
@ -361,13 +359,13 @@
(re-frame/reg-fx (re-frame/reg-fx
:browser/resolve-ens-content :browser/resolve-ens-content
(fn [{:keys [web3 registry ens-name cb]}] (fn [{:keys [registry ens-name cb]}]
(resolver/content web3 registry ens-name cb))) (resolver/content registry ens-name cb)))
(re-frame/reg-fx (re-frame/reg-fx
:browser/resolve-ens-contenthash :browser/resolve-ens-contenthash
(fn [{:keys [web3 registry ens-name cb]}] (fn [{:keys [registry ens-name cb]}]
(resolver/contenthash web3 registry ens-name cb))) (resolver/contenthash registry ens-name cb)))
(re-frame/reg-fx (re-frame/reg-fx
:browser/send-to-bridge :browser/send-to-bridge
@ -396,4 +394,3 @@
:browser/show-web-browser-selection :browser/show-web-browser-selection
(fn [link] (fn [link]
(list-selection/browse-in-web-browser link))) (list-selection/browse-in-web-browser link)))

View File

@ -36,11 +36,12 @@
(first address) (first address)
address)] address)]
(if (ens/is-valid-eth-name? first-address) (if (ens/is-valid-eth-name? first-address)
(let [{:keys [web3 network]} db (let [{:keys [network]} db
network-info (get-in db [:account/account :networks network]) network-info (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network-info) chain (ethereum/network->chain-keyword network-info)
registry (get ens/ens-registries chain)] registry (get ens/ens-registries chain)]
(ens/get-addr web3 registry first-address #(f db (assoc arguments address-keyword %)))) (ens/get-addr registry first-address
#(f db (assoc arguments address-keyword %))))
(f db arguments)))) (f db arguments))))
(defn prepare-extension-transaction [params contacts on-success on-failure] (defn prepare-extension-transaction [params contacts on-success on-failure]
@ -375,11 +376,12 @@
:extensions/ethereum-resolve-ens :extensions/ethereum-resolve-ens
(fn [{db :db} [_ _ {:keys [name on-success on-failure]}]] (fn [{db :db} [_ _ {:keys [name on-success on-failure]}]]
(if (ens/is-valid-eth-name? name) (if (ens/is-valid-eth-name? name)
(let [{:keys [web3 network]} db (let [{:keys [network]} db
network-info (get-in db [:account/account :networks network]) network-info (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network-info) chain (ethereum/network->chain-keyword network-info)
registry (get ens/ens-registries chain)] registry (get ens/ens-registries chain)]
(ens/get-addr web3 registry name #(on-success {:value %}))) (ens/get-addr registry name
#(on-success {:value %})))
(when on-failure (when on-failure
(on-failure {:value (str "'" name "' is not a valid name")}))))) (on-failure {:value (str "'" name "' is not a valid name")})))))

View File

@ -42,13 +42,14 @@
(when (and (#{constants/swarm-proto-code constants/ipfs-proto-code} proto-code) hash) (when (and (#{constants/swarm-proto-code constants/ipfs-proto-code} proto-code) hash)
(re-frame/dispatch [:stickers/load-pack proto-code hash id price open?]))))) (re-frame/dispatch [:stickers/load-pack proto-code hash id price open?])))))
(fx/defn open-sticker-pack [{{:keys [web3 network] :stickers/keys [packs packs-installed] :as db} :db :as cofx} id] (fx/defn open-sticker-pack
[{{:keys [network] :stickers/keys [packs packs-installed] :as db} :db :as cofx} id]
(when id (when id
(let [pack (or (get packs-installed id) (get packs id)) (let [pack (or (get packs-installed id) (get packs id))
network (get-in db [:account/account :networks network])] network (get-in db [:account/account :networks network])]
(if pack (if pack
(navigation/navigate-to-cofx cofx :stickers-pack-modal pack) (navigation/navigate-to-cofx cofx :stickers-pack-modal pack)
{:stickers/pack-data-fx [web3 network id true]})))) {:stickers/pack-data-fx [network id true]}))))
(fx/defn load-pack [cofx proto-code hash id price open?] (fx/defn load-pack [cofx proto-code hash id price open?]
{:http-get {:url (str (if (= constants/swarm-proto-code proto-code) {:http-get {:url (str (if (= constants/swarm-proto-code proto-code)
@ -59,11 +60,11 @@
[:stickers/load-sticker-pack-success o id price open?]) [:stickers/load-sticker-pack-success o id price open?])
:failure-event-creator (constantly nil)}}) :failure-event-creator (constantly nil)}})
(fx/defn load-packs [{{:keys [web3 network] :as db} :db}] (fx/defn load-packs [{{:keys [network] :as db} :db}]
(let [network (get-in db [:account/account :networks network]) (let [network (get-in db [:account/account :networks network])
address (ethereum/normalized-address (get-in db [:account/account :address]))] address (ethereum/normalized-address (get-in db [:account/account :address]))]
{:stickers/owned-packs-fx [web3 network address] {:stickers/owned-packs-fx [network address]
:stickers/load-packs-fx [web3 network]})) :stickers/load-packs-fx [network]}))
(defn prepare-transaction [id tx on-result] (defn prepare-transaction [id tx on-result]
(merge {:id id (merge {:id id
@ -86,37 +87,40 @@
(prepare-transaction "approve" tx-object [:stickers/pending-pack pack-id]) (prepare-transaction "approve" tx-object [:stickers/pending-pack pack-id])
tx-object))) tx-object)))
(fx/defn pending-pack [{{:keys [web3 network] :as db} :db :as cofx} id] (fx/defn pending-pack
[{{:keys [network] :as db} :db :as cofx} id]
(let [network (get-in db [:account/account :networks network]) (let [network (get-in db [:account/account :networks network])
address (ethereum/normalized-address (get-in db [:account/account :address]))] address (ethereum/normalized-address (get-in db [:account/account :address]))]
(fx/merge cofx (fx/merge cofx
{:db (update db :stickers/packs-pendning conj id) {:db (update db :stickers/packs-pendning conj id)
:stickers/owned-packs-fx [web3 network address]} :stickers/owned-packs-fx [network address]}
(navigation/navigate-to-clean :wallet-transaction-sent-modal {}) (navigation/navigate-to-clean :wallet-transaction-sent-modal {})
#(when (zero? (count (:stickers/packs-pendning db))) #(when (zero? (count (:stickers/packs-pendning db)))
{:stickers/set-pending-timout-fx nil})))) {:stickers/set-pending-timout-fx nil}))))
(fx/defn pending-timeout [{{:keys [web3 network] :stickers/keys [packs-pendning packs-owned] :as db} :db}] (fx/defn pending-timeout
[{{:keys [network] :stickers/keys [packs-pendning packs-owned] :as db} :db}]
(let [packs-diff (clojure.set/difference packs-pendning packs-owned) (let [packs-diff (clojure.set/difference packs-pendning packs-owned)
network (get-in db [:account/account :networks network]) network (get-in db [:account/account :networks network])
address (ethereum/normalized-address (get-in db [:account/account :address]))] address (ethereum/normalized-address (get-in db [:account/account :address]))]
(merge {:db (assoc db :stickers/packs-pendning packs-diff)} (merge {:db (assoc db :stickers/packs-pendning packs-diff)}
(when-not (zero? (count packs-diff)) (when-not (zero? (count packs-diff))
{:stickers/owned-packs-fx [web3 network address] {:stickers/owned-packs-fx [network address]
:stickers/set-pending-timout-fx nil})))) :stickers/set-pending-timout-fx nil}))))
(fx/defn pack-owned [{db :db} id] (fx/defn pack-owned [{db :db} id]
{:db (update db :stickers/packs-owned conj id)}) {:db (update db :stickers/packs-owned conj id)})
(fx/defn get-owned-pack [{{:keys [web3 network] :as db} :db}] (fx/defn get-owned-pack
[{{:keys [network] :as db} :db}]
(let [address (ethereum/normalized-address (get-in db [:account/account :address]))] (let [address (ethereum/normalized-address (get-in db [:account/account :address]))]
{:stickers/owned-packs-fx [web3 network address]})) {:stickers/owned-packs-fx [network address]}))
(re-frame/reg-fx (re-frame/reg-fx
:stickers/pack-data-fx :stickers/pack-data-fx
(fn [[web3 network id open?]] (fn [[network id open?]]
(when-let [contract (get ethereum.stickers/contracts (ethereum/network->chain-keyword network))] (when-let [contract (get ethereum.stickers/contracts (ethereum/network->chain-keyword network))]
(ethereum.stickers/pack-data web3 contract id (pack-data-callback id open?))))) (ethereum.stickers/pack-data contract id (pack-data-callback id open?)))))
(re-frame/reg-fx (re-frame/reg-fx
:stickers/set-pending-timout-fx :stickers/set-pending-timout-fx
@ -125,19 +129,19 @@
(re-frame/reg-fx (re-frame/reg-fx
:stickers/load-packs-fx :stickers/load-packs-fx
(fn [[web3 network]] (fn [[network]]
(when-let [contract (get ethereum.stickers/contracts (ethereum/network->chain-keyword network))] (when-let [contract (get ethereum.stickers/contracts (ethereum/network->chain-keyword network))]
(ethereum.stickers/pack-count web3 contract (ethereum.stickers/pack-count contract
(fn [count] (fn [count]
(dotimes [n count] (dotimes [n count]
(ethereum.stickers/pack-data web3 contract n (pack-data-callback n false)))))))) (ethereum.stickers/pack-data contract n (pack-data-callback n false))))))))
(re-frame/reg-fx (re-frame/reg-fx
:stickers/owned-packs-fx :stickers/owned-packs-fx
(fn [[web3 network address]] (fn [[network address]]
(when-let [contract (get ethereum.stickers/contracts (ethereum/network->chain-keyword network))] (when-let [contract (get ethereum.stickers/contracts (ethereum/network->chain-keyword network))]
(ethereum.stickers/owned-tokens web3 contract address (ethereum.stickers/owned-tokens contract address
(fn [tokens] (fn [tokens]
(doseq [n tokens] (doseq [n tokens]
(ethereum.stickers/token-pack-id web3 contract n (ethereum.stickers/token-pack-id contract n
#(re-frame/dispatch [:stickers/pack-owned %])))))))) #(re-frame/dispatch [:stickers/pack-owned %]))))))))

View File

@ -9,12 +9,12 @@
(re-frame/reg-fx (re-frame/reg-fx
:resolve-public-key :resolve-public-key
(fn [{:keys [web3 registry ens-name cb]}] (fn [{:keys [registry ens-name cb]}]
(resolver/pubkey web3 registry ens-name cb))) (resolver/pubkey registry ens-name cb)))
(handlers/register-handler-fx (handlers/register-handler-fx
:new-chat/set-new-identity :new-chat/set-new-identity
(fn [{{:keys [web3 network network-status] :as db} :db} [_ new-identity]] (fn [{{:keys [network network-status] :as db} :db} [_ new-identity]]
(let [is-public-key? (and (string? new-identity) (let [is-public-key? (and (string? new-identity)
(string/starts-with? new-identity "0x"))] (string/starts-with? new-identity "0x"))]
(merge {:db (assoc db (merge {:db (assoc db
@ -23,8 +23,7 @@
(when-not is-public-key? (when-not is-public-key?
(let [network (get-in db [:account/account :networks network]) (let [network (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network)] chain (ethereum/network->chain-keyword network)]
{:resolve-public-key {:web3 web3 {:resolve-public-key {:registry (get ens/ens-registries chain)
:registry (get ens/ens-registries chain)
:ens-name (if (ens/is-valid-eth-name? new-identity) :ens-name (if (ens/is-valid-eth-name? new-identity)
new-identity new-identity
(str new-identity ".stateofus.eth")) (str new-identity ".stateofus.eth"))

View File

@ -69,8 +69,8 @@
(re-frame/reg-fx (re-frame/reg-fx
:resolve-address :resolve-address
(fn [{:keys [web3 registry ens-name cb]}] (fn [{:keys [registry ens-name cb]}]
(ens/get-addr web3 registry ens-name cb))) (ens/get-addr registry ens-name cb)))
(handlers/register-handler-fx (handlers/register-handler-fx
:wallet.send/set-recipient :wallet.send/set-recipient

View File

@ -18,7 +18,7 @@
ids)})) ids)}))
;; TODO(andrey) Each HTTP call will return up to 100 kitties. Maybe we need to implement some kind of paging later ;; TODO(andrey) Each HTTP call will return up to 100 kitties. Maybe we need to implement some kind of paging later
(defmethod collectibles/load-collectibles-fx ck [_ _ _ items-number address _] (defmethod collectibles/load-collectibles-fx ck [_ _ items-number address _]
{:http-get {:url (str "https://api.cryptokitties.co/kitties?offset=0&limit=" {:http-get {:url (str "https://api.cryptokitties.co/kitties?offset=0&limit="
items-number items-number
"&owner_wallet_address=" "&owner_wallet_address="

View File

@ -12,10 +12,10 @@
(defmethod load-collectible-fx :default [_ _ _] nil) (defmethod load-collectible-fx :default [_ _ _] nil)
(defmulti load-collectibles-fx (fn [_ _ symbol _ _] symbol)) (defmulti load-collectibles-fx (fn [_ symbol _ _] symbol))
(defmethod load-collectibles-fx :default [web3 all-tokens symbol items-number address chain-id] (defmethod load-collectibles-fx :default [all-tokens symbol items-number address chain-id]
{:load-collectibles-fx [web3 all-tokens symbol items-number address chain-id]}) {:load-collectibles-fx [all-tokens symbol items-number address chain-id]})
(handlers/register-handler-fx (handlers/register-handler-fx
:show-collectibles-list :show-collectibles-list
@ -25,22 +25,22 @@
items-number (money/to-number amount) items-number (money/to-number amount)
loaded-items-number (count (get-in db [:collectibles symbol]))] loaded-items-number (count (get-in db [:collectibles symbol]))]
(merge (when (not= items-number loaded-items-number) (merge (when (not= items-number loaded-items-number)
(load-collectibles-fx (:web3 db) all-tokens symbol items-number address chain-id)) (load-collectibles-fx all-tokens symbol items-number address chain-id))
{:dispatch [:navigate-to :collectibles-list collectible]})))) {:dispatch [:navigate-to :collectibles-list collectible]}))))
(defn load-token [web3 i items-number contract address symbol] (defn load-token [i items-number contract address symbol]
(when (< i items-number) (when (< i items-number)
(erc721/token-of-owner-by-index web3 contract address i (erc721/token-of-owner-by-index contract address i
(fn [v1 v2] (fn [response]
(load-token web3 (inc i) items-number contract address symbol) (load-token (inc i) items-number contract address symbol)
(re-frame/dispatch [:load-collectible symbol (.toNumber v2)]))))) (re-frame/dispatch [:load-collectible symbol (.toNumber response)])))))
(re-frame/reg-fx (re-frame/reg-fx
:load-collectibles-fx :load-collectibles-fx
(fn [[web3 all-tokens symbol items-number address chain-id]] (fn [[all-tokens symbol items-number address chain-id]]
(let [chain (ethereum/chain-id->chain-keyword chain-id) (let [chain (ethereum/chain-id->chain-keyword chain-id)
contract (:address (tokens/symbol->token all-tokens chain symbol))] contract (:address (tokens/symbol->token all-tokens chain symbol))]
(load-token web3 0 items-number contract address symbol)))) (load-token 0 items-number contract address symbol))))
(handlers/register-handler-fx (handlers/register-handler-fx
:load-collectible :load-collectible

View File

@ -14,18 +14,19 @@
(defmethod collectibles/load-collectible-fx kudos [{db :db} symbol id] (defmethod collectibles/load-collectible-fx kudos [{db :db} symbol id]
(let [chain-id (get-in constants/default-networks [(:network db) :config :NetworkId]) (let [chain-id (get-in constants/default-networks [(:network db) :config :NetworkId])
all-tokens (:wallet/all-tokens db)] all-tokens (:wallet/all-tokens db)]
{:erc721-token-uri [(:web3 db) all-tokens symbol id chain-id]})) {:erc721-token-uri [all-tokens symbol id chain-id]}))
(re-frame/reg-fx (re-frame/reg-fx
:erc721-token-uri :erc721-token-uri
(fn [[web3 all-tokens symbol tokenId chain-id]] (fn [[all-tokens symbol tokenId chain-id]]
(let [chain (ethereum/chain-id->chain-keyword chain-id) (let [chain (ethereum/chain-id->chain-keyword chain-id)
contract (:address (tokens/symbol->token all-tokens chain symbol))] contract (:address (tokens/symbol->token all-tokens chain symbol))]
(erc721/token-uri web3 contract tokenId (erc721/token-uri contract
tokenId
#(re-frame/dispatch [:token-uri-success #(re-frame/dispatch [:token-uri-success
tokenId tokenId
(when %2 (when %
(subs %2 (.indexOf %2 "http")))]))))) ;; extra chars in rinkeby (subs % (.indexOf % "http")))]))))) ;; extra chars in rinkeby
(handlers/register-handler-fx (handlers/register-handler-fx
:token-uri-success :token-uri-success

View File

@ -29,7 +29,7 @@
imageUri imageUri
}}}}")) }}}}"))
(defmethod collectibles/load-collectibles-fx superrare [_ _ _ _ address _] (defmethod collectibles/load-collectibles-fx superrare [_ _ _ address _]
{:http-post {:url graphql-url {:http-post {:url graphql-url
:data (types/clj->json {:query (graphql-query (ethereum/naked-address address))}) :data (types/clj->json {:query (graphql-query (ethereum/naked-address address))})
:opts {:headers {"Content-Type" "application/json"}} :opts {:headers {"Content-Type" "application/json"}}

View File

@ -1,6 +1,7 @@
(ns status-im.utils.ethereum.core (ns status-im.utils.ethereum.core
(:require [clojure.string :as string] (:require [clojure.string :as string]
[status-im.js-dependencies :as dependencies] [status-im.js-dependencies :as dependencies]
[status-im.native-module.core :as status]
[status-im.utils.ethereum.tokens :as tokens] [status-im.utils.ethereum.tokens :as tokens]
[status-im.utils.money :as money] [status-im.utils.money :as money]
[taoensso.timbre :as log])) [taoensso.timbre :as log]))
@ -124,8 +125,16 @@
(defn- sig->method-id [signature] (defn- sig->method-id [signature]
(apply str (take 10 (sha3 signature)))) (apply str (take 10 (sha3 signature))))
(defn call [web3 params cb] (defn call [params callback]
(.call (.-eth web3) (clj->js params) cb)) (status/call-private-rpc
(.stringify js/JSON (clj->js {:jsonprc "2.0"
:id 1
:method "eth_call"
:params [params "latest"]}))
(fn [response]
(if (= "" response)
(log/warn :web3-response-error)
(callback (get (js->clj (.parse js/JSON response)) "result"))))))
(defn call-params [contract method-sig & params] (defn call-params [contract method-sig & params]
(let [data (apply format-call-params (sig->method-id method-sig) params)] (let [data (apply format-call-params (sig->method-id method-sig) params)]

View File

@ -5,18 +5,20 @@
(def supports-interface-hash "0x01ffc9a7") (def supports-interface-hash "0x01ffc9a7")
(def marker-hash "0xffffffff") (def marker-hash "0xffffffff")
(defn supports-interface? [web3 contract hash cb] (defn supports-interface? [contract hash cb]
(ethereum/call web3 (ethereum/call (ethereum/call-params contract "supportsInterface(bytes4)" hash)
(ethereum/call-params contract "supportsInterface(bytes4)" hash) #(cb %)))
#(cb %1 %2)))
(defn supports? (defn supports?
"Calls cb with true if `supportsInterface` is supported by this contract. "Calls cb with true if `supportsInterface` is supported by this contract.
See EIP for details." See EIP for details."
[web3 contract cb] [web3 contract cb]
(supports-interface? web3 contract supports-interface-hash (supports-interface?
#(if (true? (ethereum/hex->boolean %2)) contract
(supports-interface? web3 contract marker-hash supports-interface-hash
(fn [o oo] #(if (true? (ethereum/hex->boolean %))
(cb o (false? (ethereum/hex->boolean oo))))) (supports-interface? contract
(cb %1 false)))) marker-hash
(fn [response]
(cb (false? (ethereum/hex->boolean response)))))
(cb false))))

View File

@ -18,8 +18,10 @@
(def default-address "0x0000000000000000000000000000000000000000") (def default-address "0x0000000000000000000000000000000000000000")
(def default-key "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") (def default-key "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
(defn namehash [s] (defn namehash
(ethereum/normalized-address (if (string/blank? s) [s]
(ethereum/normalized-address
(if (string/blank? s)
default-namehash default-namehash
(let [[label remainder] (-> s (let [[label remainder] (-> s
string/lower-case string/lower-case
@ -30,86 +32,100 @@
;; Registry contract ;; Registry contract
(defn resolver [web3 registry ens-name cb] (defn resolver
(ethereum/call web3 [registry ens-name cb]
(ethereum/call-params registry (ethereum/call (ethereum/call-params registry
"resolver(bytes32)" "resolver(bytes32)"
(namehash ens-name)) (namehash ens-name))
(fn [_ address] (let [address (ethereum/hex->address address)] (fn [address]
(let [address (ethereum/hex->address address)]
(cb (if (and address (not= address default-address)) address "")))))) (cb (if (and address (not= address default-address)) address ""))))))
(defn owner [web3 registry ens-name cb] (defn owner
(ethereum/call web3 [registry ens-name cb]
(ethereum/call-params registry (ethereum/call (ethereum/call-params registry
"owner(bytes32)" "owner(bytes32)"
(namehash ens-name)) (namehash ens-name))
(fn [_ address] (cb address)))) (fn [address]
(cb address))))
(defn ttl [web3 registry ens-name cb] (defn ttl
(ethereum/call web3 [registry ens-name cb]
(ethereum/call-params registry (ethereum/call (ethereum/call-params registry
"ttl(bytes32)" "ttl(bytes32)"
(namehash ens-name)) (namehash ens-name))
(fn [_ ttl] (cb (ethereum/hex->int ttl))))) (fn [ttl]
(cb (ethereum/hex->int ttl)))))
;; Resolver contract ;; Resolver contract
;; Resolver must implement EIP65 (supportsInterface). When interacting with an unknown resolver it's safer to rely on it. ;; Resolver must implement EIP65 (supportsInterface). When interacting with an unknown resolver it's safer to rely on it.
(def addr-hash "0x3b3b57de") (def addr-hash "0x3b3b57de")
(defn addr [web3 resolver ens-name cb] (defn addr
(ethereum/call web3 [resolver ens-name cb]
(ethereum/call-params resolver "addr(bytes32)" (namehash ens-name)) (ethereum/call (ethereum/call-params resolver "addr(bytes32)" (namehash ens-name))
(fn [_ address] (cb (ethereum/hex->address address))))) (fn [address]
(cb (ethereum/hex->address address)))))
(def name-hash "0x691f3431") (def name-hash "0x691f3431")
;; Defined by https://eips.ethereum.org/EIPS/eip-181 ;; Defined by https://eips.ethereum.org/EIPS/eip-181
(defn name [web3 resolver ens-name cb] (defn name
(ethereum/call web3 [resolver ens-name cb]
(ethereum/call-params resolver (ethereum/call (ethereum/call-params resolver
"name(bytes32)" "name(bytes32)"
(namehash ens-name)) (namehash ens-name))
(fn [_ address] (cb (ethereum/hex->address address))))) (fn [address]
(cb (ethereum/hex->address address)))))
(defn contenthash [web3 resolver ens-name cb] (defn contenthash
(ethereum/call web3 [resolver ens-name cb]
(ethereum/call-params resolver "contenthash(bytes32)" (namehash ens-name)) (ethereum/call (ethereum/call-params resolver
(fn [_ hash] (cb hash)))) "contenthash(bytes32)"
(namehash ens-name))
(fn [hash]
(cb hash))))
(defn content [web3 resolver ens-name cb] (defn content
(ethereum/call web3 [resolver ens-name cb]
(ethereum/call-params resolver "content(bytes32)" (namehash ens-name)) (ethereum/call (ethereum/call-params resolver
(fn [_ hash] (cb hash)))) "content(bytes32)"
(namehash ens-name))
(fn [hash]
(cb hash))))
(def ABI-hash "0x2203ab56") (def ABI-hash "0x2203ab56")
(def pubkey-hash "0xc8690233") (def pubkey-hash "0xc8690233")
(defn add-uncompressed-public-key-prefix [key] (defn add-uncompressed-public-key-prefix
[key]
(when (and key (when (and key
(not= "0x" key) (not= "0x" key)
(not= default-key key)) (not= default-key key))
(str "0x04" (subs key 2)))) (str "0x04" (subs key 2))))
(defn is-valid-eth-name? [ens-name] (defn is-valid-eth-name?
[ens-name]
(and ens-name (and ens-name
(string? ens-name) (string? ens-name)
(string/ends-with? ens-name ".eth"))) (string/ends-with? ens-name ".eth")))
(defn pubkey [web3 resolver ens-name cb] (defn pubkey
(ethereum/call web3 [resolver ens-name cb]
(ethereum/call-params resolver (ethereum/call (ethereum/call-params resolver
"pubkey(bytes32)" "pubkey(bytes32)"
(namehash ens-name)) (namehash ens-name))
(fn [_ key] (when-let [public-key (add-uncompressed-public-key-prefix key)] (fn [key]
(when-let [public-key (add-uncompressed-public-key-prefix key)]
(cb public-key))))) (cb public-key)))))
(defn get-addr [web3 registry ens-name cb] (defn get-addr
[registry ens-name cb]
{:pre [(is-valid-eth-name? ens-name)]} {:pre [(is-valid-eth-name? ens-name)]}
(resolver web3 (resolver registry
registry
ens-name ens-name
#(addr web3 % ens-name cb))) #(addr % ens-name cb)))
;; TODO ABI, pubkey ;; TODO ABI, pubkey

View File

@ -127,16 +127,23 @@
on-completed)) on-completed))
(defn transfer-from [web3 contract from-address to-address value cb] (defn transfer-from [web3 contract from-address to-address value cb]
(ethereum/call web3 (ethereum/call (ethereum/call-params contract
(ethereum/call-params contract "transferFrom(address,address,uint256)" (ethereum/normalized-address from-address) (ethereum/normalized-address to-address) (ethereum/int->hex value)) "transferFrom(address,address,uint256)"
#(cb %1 (ethereum/hex->boolean %2)))) (ethereum/normalized-address from-address)
(ethereum/normalized-address to-address)
(ethereum/int->hex value))
#(cb (ethereum/hex->boolean %))))
(defn approve [web3 contract address value cb] (defn approve [web3 contract address value cb]
(ethereum/call web3 (ethereum/call (ethereum/call-params contract
(ethereum/call-params contract "approve(address,uint256)" (ethereum/normalized-address address) (ethereum/int->hex value)) "approve(address,uint256)"
#(cb %1 (ethereum/hex->boolean %2)))) (ethereum/normalized-address address)
(ethereum/int->hex value))
#(cb (ethereum/hex->boolean %))))
(defn allowance [web3 contract owner-address spender-address cb] (defn allowance [web3 contract owner-address spender-address cb]
(ethereum/call web3 (ethereum/call (ethereum/call-params contract
(ethereum/call-params contract "allowance(address,address)" (ethereum/normalized-address owner-address) (ethereum/normalized-address spender-address)) "allowance(address,address)"
#(cb %1 (ethereum/hex->bignumber %2)))) (ethereum/normalized-address owner-address)
(ethereum/normalized-address spender-address))
#(cb (ethereum/hex->bignumber %))))

View File

@ -4,20 +4,17 @@
" "
(:require [status-im.utils.ethereum.core :as ethereum])) (:require [status-im.utils.ethereum.core :as ethereum]))
(defn token-of-owner-by-index [web3 contract address index cb] (defn token-of-owner-by-index [contract address index cb]
(ethereum/call web3 (ethereum/call (ethereum/call-params
(ethereum/call-params
contract contract
"tokenOfOwnerByIndex(address,uint256)" "tokenOfOwnerByIndex(address,uint256)"
(ethereum/normalized-address address) (ethereum/normalized-address address)
(ethereum/int->hex index)) (ethereum/int->hex index))
#(cb %1 (ethereum/hex->bignumber %2)))) #(cb (ethereum/hex->bignumber %))))
(defn token-uri [web3 contract tokenId cb] (defn token-uri [contract tokenId cb]
(ethereum/call web3 (ethereum/call (ethereum/call-params
(ethereum/call-params
contract contract
"tokenURI(uint256)" "tokenURI(uint256)"
(ethereum/int->hex tokenId)) (ethereum/int->hex tokenId))
(fn [v1 v2] #(cb (ethereum/hex->string %))))
(cb v1 (ethereum/hex->string v2)))))

View File

@ -4,28 +4,24 @@
(def default-hash "0x0000000000000000000000000000000000000000000000000000000000000000") (def default-hash "0x0000000000000000000000000000000000000000000000000000000000000000")
(defn contenthash [web3 registry ens-name cb] (defn contenthash [registry ens-name cb]
(ens/resolver web3 (ens/resolver registry
registry
ens-name ens-name
#(ens/contenthash web3 % ens-name cb))) #(ens/contenthash % ens-name cb)))
(defn content [web3 registry ens-name cb] (defn content [registry ens-name cb]
(ens/resolver web3 (ens/resolver registry
registry
ens-name ens-name
#(ens/content web3 % ens-name cb))) #(ens/content % ens-name cb)))
(defn name [web3 registry ens-name cb] (defn name [registry ens-name cb]
(ens/resolver web3 (ens/resolver registry
registry
ens-name ens-name
#(ens/name web3 % ens-name cb))) #(ens/name % ens-name cb)))
(defn pubkey (defn pubkey
[web3 registry ens-name cb] [registry ens-name cb]
{:pre [(ens/is-valid-eth-name? ens-name)]} {:pre [(ens/is-valid-eth-name? ens-name)]}
(ens/resolver web3 (ens/resolver registry
registry
ens-name ens-name
#(ens/pubkey web3 % ens-name cb))) #(ens/pubkey % ens-name cb)))

View File

@ -9,30 +9,26 @@
(defn pack-count (defn pack-count
"Returns number of packs rigestered in the contract" "Returns number of packs rigestered in the contract"
[web3 contract cb] [contract cb]
(ethereum/call web3 (ethereum/call (ethereum/call-params contract "packCount()")
(ethereum/call-params contract "packCount()") (fn [count] (cb (ethereum/hex->int count)))))
(fn [_ count] (cb (ethereum/hex->int count)))))
(defn pack-data (defn pack-data
"Returns vector of pack data parameters by pack id: [category owner mintable timestamp price contenthash]" "Returns vector of pack data parameters by pack id: [category owner mintable timestamp price contenthash]"
[web3 contract pack-id cb] [contract pack-id cb]
(ethereum/call web3 (ethereum/call (ethereum/call-params contract "getPackData(uint256)" (ethereum/int->hex pack-id))
(ethereum/call-params contract "getPackData(uint256)" (ethereum/int->hex pack-id)) (fn [data]
(fn [_ data]
(cb (abi-spec/decode data ["bytes4[]" "address" "bool" "uint256" "uint256" "bytes"]))))) (cb (abi-spec/decode data ["bytes4[]" "address" "bool" "uint256" "uint256" "bytes"])))))
(defn owned-tokens (defn owned-tokens
"Returns vector of owned tokens ids in the contract by address" "Returns vector of owned tokens ids in the contract by address"
[web3 contract address cb] [contract address cb]
(ethereum/call web3 (ethereum/call (ethereum/call-params contract "tokensOwnedBy(address)" (ethereum/normalized-address address))
(ethereum/call-params contract "tokensOwnedBy(address)" (ethereum/normalized-address address)) (fn [data]
(fn [_ data]
(cb (first (abi-spec/decode data ["uint256[]"])))))) (cb (first (abi-spec/decode data ["uint256[]"]))))))
(defn token-pack-id (defn token-pack-id
"Returns pack id in the contract by token id" "Returns pack id in the contract by token id"
[web3 contract token cb] [contract token cb]
(ethereum/call web3 (ethereum/call (ethereum/call-params contract "tokenPackId(uint256)" (ethereum/int->hex token))
(ethereum/call-params contract "tokenPackId(uint256)" (ethereum/int->hex token)) (fn [data] (cb (ethereum/hex->int data)))))
(fn [_ data] (cb (ethereum/hex->int data)))))