Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #18 from district0x/subscriptions-fixes
Browse files Browse the repository at this point in the history
Add/fix functions to support event and block subscriptions
  • Loading branch information
madis authored Jul 12, 2022
2 parents d04857c + 6564f59 commit 1c16395
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/cljs_web3_next/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
([uri] (ws-provider uri {}))
([uri opts] (websocket-provider uri opts)))

(defn support-subscriptions? [provider]
(some? (aget provider "currentProvider" "on")))

(defn connection-url [provider]
(oget provider "currentProvider" "connection" "_url"))

Expand Down
17 changes: 10 additions & 7 deletions src/cljs_web3_next/eth.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
(defn get-transaction-receipt [provider tx-hash & [callback]]
(oapply+ provider "eth.getTransactionReceipt" (remove nil? [tx-hash callback])))

(defn accounts [provider]
(js-invoke (oget provider "eth") "getAccounts"))
(defn accounts [provider & [callback]]
(oapply+ provider "eth.getAccounts" (remove nil? [callback])))

;; recheck conflicts with updated fn
;; (defn get-balance [provider address]
Expand Down Expand Up @@ -43,11 +43,14 @@
(defn subscribe-events [contract-instance event opts & [callback]]
(oapply+ (oget contract-instance "events")
(web3-helpers/camel-case (name event)) ; https://web3js.readthedocs.io/en/v1.7.1/web3-eth-contract.html#contract-events
[contract-instance (web3-helpers/cljkk->js opts) callback]))
[(web3-helpers/cljkk->js opts) callback]))

(defn subscribe-logs [provider opts & [callback]]
(js-invoke (aget provider "eth") "subscribe" "logs" (web3-helpers/cljkk->js opts) callback))

(defn subscribe-blocks [provider & [callback]]
(js-invoke (aget provider "eth") "subscribe" "newBlockHeaders" callback))

(defn decode-log [provider abi data topics]
(ocall+ provider "eth.abi.decodeLog" (clj->js abi) data (clj->js topics)))

Expand Down Expand Up @@ -296,7 +299,7 @@
set with set-default-block.
callback-fn - callback with two parameters, error and result
Returns a BigNumber instance of the current balance for the given address in
Returns a String of the current balance for the given address in
wei.
Example:
Expand All @@ -305,7 +308,7 @@
\"latest\"
(fn [err res] (when-not err (println res))))`
nil
user> #object[e 1729597111000000000]"
user> \"1729597111000000000\""
[provider & [address default-block :as args]]
(oapply+ provider "eth" "getBalance" args))

Expand Down Expand Up @@ -688,8 +691,8 @@
(fn [err res] (when-not err (println res))))
nil
user> 0x..."
[web3 & [transaction-object :as args]]
(oapply+ (eth web3) "sendTransaction" args))
[web3 transaction-object & [callback]]
(oapply+ (eth web3) "sendTransaction" (remove nil? [(clj->js transaction-object) callback])))


(defn send-raw-transaction!
Expand Down

0 comments on commit 1c16395

Please sign in to comment.