Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to create web3 instance from injected web3 #8

Merged
merged 1 commit into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ Some functions in `cljs-web3.core` don't really need a web3 instance, even thoug
#### Extra functions for a better life
These are a few extra functions, which you won't find in the Web3 API:
```clojure
;; Create web3 instance
;; Create web3 instance from injected web3 (Mist / Metamask)
(web3/web3)

;; Create web3 instance from HTTP provider
(web3/create-web3 "http://localhost:8545/")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think it would be nice if this actually had http in the name somehow but it's part of the API now so I don't know 🙂


;; Deploy new contract
Expand Down
10 changes: 8 additions & 2 deletions src/cljs_web3/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
(create-web3 \"http://localhost:8545/\"))`

The Web3 JavaScript object is provided on the browser window."
(:require [cljs-web3.utils :as u :refer [js-apply js-prototype-apply]]))

(:require [cljs-web3.utils :as u :refer [js-apply js-prototype-apply]]
[goog.object]))

(def version-api
"Returns a string representing the Ethereum js api version.
Expand Down Expand Up @@ -386,6 +386,12 @@
[web3]
(aget web3 "currentProvider"))

(defn web3
"Return the web3 instance injected via Mist or Metamask"
[]
(new (goog.object/getValueByKeys js/window "web3" "constructor")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used constructor access here as I felt it's a little more explicit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get nil when I call (web3/web3) but I get the proper instance when calling:

(new js/web3.constructor (web3-core/current-provider (goog.object/get js/window "web3")))

Do you have an idea why this could happen?
(I'm in a setup where MetaMask is unlocked etc.)

(current-provider
(goog.object/get js/window "web3"))))

;;; Providers

Expand Down