Web3 Provider using Infura
Closed this issue · 5 comments
These works fine to init the subspace object.
const web3 = new Web3("ws://127.0.0.1:8545")
const subspace = new Subspace(web3.currentProvider)
// OR
const subspace = new Subspace(window.web3.currentProvider)
But when I try to use Infura, I run into issues.
const web3 = new Web3(
`http://kovan.infura.io/v3/${process.env.INFURA_ENDPOINT_KEY}`
)
const subspace = new Subspace(web3.currentProvider)
Gives Error: Invalid JSON RPC response: "" (web3.min.js:1)
const web3 = new Web3(
new Web3.providers.HttpProvider(
`https://kovan.infura.io/v3/${process.env.INFURA_ENDPOINT_KEY}`
)
)
const subspace = new Subspace(web3.currentProvider)
Gives: ReferenceError: sub is not defined (commons.js line 2112 > eval:228:11)
I also tried using Ethers.js
const infuraProvider = new ethers.providers.InfuraProvider(
"kovan",
process.env.INFURA_ENDPOINT_KEY
)
const subspace = new Subspace(infuraProvider)
Gives Error: invalid json request (ethers.min.js:1)
I'm using React. I yarn link
the subspace package to hopefully see more messages, but it didn't help. My code is here if you want to see.
I see that in your first code snippet, you forgot to use https
, so that might have been an issue.
But otherwise, I think you need to use the WS endpoint, since Subspace uses contract events. @richard-ramos might be able to confirm
Yeah. Currently Subspace requires Websockets to subscribe to contract events. You can try changing the infura URL to wss://kovan.infura.io/ws/v3/${process.env.INFURA_ENDPOINT_KEY}
Thanks guys! @richard-ramos your snippet worked!
Support for HttpProviders has been added in Subspace 1.2, so now it should work with http URLs and other providers.
Awesome thanks!