Code looks like:
const eth = require("ethers")
const provider = new eth.providers.JsonRpcProvider(process.env.ETH_RPC_URL)
console.log(provider)
const chainId = (await provider.getNetwork()).chainId
And it outputs:
JsonRpcProvider {
chainId: 1,
ensAddress: '0x314159265dD8dbb310642f98f50C066173C1259b',
name: 'homestead',
_events: {},
_emitted: { block: -1 },
resetEventsBlock: [Function],
url:
'https://eth-rinkeby.alchemyapi.io/jsonrpc/abc123' }
(node:401) UnhandledPromiseRejectionWarning: TypeError: provider.getNetwork is not a function
at [stdin]:7:35
at [stdin]:51:5
at Script.runInThisContext (vm.js:96:20)
at Object.runInThisContext (vm.js:303:38)
at Object.<anonymous> ([stdin]-wrapper:6:22)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at evalScript (internal/bootstrap/node.js:582:27)
at Socket.<anonymous> (internal/bootstrap/node.js:325:15)
at Socket.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
The wrong chainId lead me to issues #108 and #149 but neither issue provided an answer to what's going on here.
If I manually specify the chainId:
const provider = new eth.providers.JsonRpcProvider(process.env.ETH_RPC_URL, eth.providers.networks.rinkeby)
Then it'll print a provider with the correct chainId, I can send transactions, but I still don't get the getNetwork function or a provider.connection property.
Seems like the provider is taking a sec to automatically detect the network but await on the provider init doesn't make a difference.
This code is running in a container via docker exec btw but within the container I can curl this $ETH_RPC_URL and get the correct network version so doesn't seem like a networking problem unless it's just because the network is slow.
If I run the same code from a local node process things seem to work just fine 馃え
Using ethers 4.0.26
Looks like this works to get a provider and verify it's url but I still can't figure out a reliable way to get the chainId from the provider, not it's mission critical.
if (process.env.ETH_NETWORK_ID === "1") {
provider = new eth.providers.JsonRpcProvider(process.env.ETH_RPC_URL, eth.providers.networks.homestead)
} else if (process.env.ETH_NETWORK_ID === "4") {
provider = new eth.providers.JsonRpcProvider(process.env.ETH_RPC_URL, eth.providers.networks.rinkeby)
} else {
provider = new eth.providers.JsonRpcProvider(process.env.ETH_RPC_URL)
}
const url = (provider.connection || provider).url
This seems like it is a problem with alchemyapi.io. I do not have an account, I've signed up but am far down the list.
Can you use one of the other Ethereum providers, for now? Perhaps INFURA?
My guess is that alchemyapi does not support the version API, so it cannot detect the chain. You can pass in the chainId by itself (as a number) to simplify your above method; when a number is given, it will lookup the well-known networks. i.e.
let url = process.env.ETH_RPC_URL;
let networkId = parseInt(process.env.ETH_NETWORK_ID);
const provider = new ethers.providers.JsonRpcProvider(url, networkId);
That will at least hopefully simplify you code.
Engineer from Alchemy here. Would love to help get to the bottom of this. AlchemyAPI definitely supports net_version, and running this same snippet, I get the correct chain id:
const eth = require("ethers")
const provider = new eth.providers.JsonRpcProvider('https://eth-rinkeby.alchemyapi.io/jsonrpc/<KEY>')
console.log(provider)
JsonRpcProvider {
ready:
Promise {
{ name: 'rinkeby',
chainId: 4,
ensAddress: '0xe7410170f87102DF0055eB195163A03B7F2Bff4A',
_defaultProvider: [Function] },
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
members: [] } },
_lastBlockNumber: -2,
_balances: {},
_events: [],
_pollingInterval: 4000,
_emitted: { block: -2 },
_fastQueryDate: 0,
connection: { url: 'https://eth-rinkeby.alchemyapi.io/jsonrpc/<KEY>' },
_network:
{ name: 'rinkeby',
chainId: 4,
ensAddress: '0xe7410170f87102DF0055eB195163A03B7F2Bff4A',
_defaultProvider: [Function] } }
@bohendo I'll message you to try to figure this out.
@ricmoo I can follow up with you and try my best to make an exception and get you bumped up the waiting list since supporting ethers.js is high on our priority list!
@dtran320 Awesome! Thanks! (request will be under [email protected])
Ohhhh boy think I found a problem. I have ethers version 4.0.26 in my package.json and in my local node_modules but running eth.version inside the code itself shows 3.0.27, looks like my docker container is out of date 馃槄
Ah! Yes, that would explain it. V3 did not have getNetwork(), it had network instead, which was synchronous, so auto-detection wasn鈥檛 possible. :)
Alright, updated and am definitely using v4.026 now and things work just as we'd expect them to.
Great, ethers & alchemy are both in great shape! No problems on your end, just a bit of user error 馃お
Thanks for the help though!
Hey @ricmoo, make sense support Alchemy on the Default/Fallback provider by default?
Best
Most helpful comment
Alright, updated and am definitely using v4.026 now and things work just as we'd expect them to.
Great, ethers & alchemy are both in great shape! No problems on your end, just a bit of user error 馃お
Thanks for the help though!