ethers.js
ethers.js copied to clipboard
Websocket Provider calling eth_chainId on instantiation
Hi there,
this is a recurring issue that has kept me from using the websocket provider. Everytime a new websocket instance is created, a call to eth_chainid is registered, even though I'm passing the network / chainId as the second argument in the constructor. Why is this call to the provider necessary?
Since I replace the websocket provider when an error occurs (e.g. connection timeout and so on) this accounts for an incredible amount of my infura resources being used up.
provider = new ethers.providers.WebSocketProvider(infuraUrl, 1)
I'm using the latest version of ethers.js
HI @ricmoo, did you have a chance to check this out? Looking into the code quickly it seems that the detectNetwork method is being called here no matter if I provide a network on instantiation of the provider or not.
Was the method meant to be referenced here instead of called?
defineReadOnly(this, "_detectNetwork", super.detectNetwork());
websocket-provider.ts line 76
@janschmutz @ricmoo any information about how we can deal with this?
Im having the same issue, maybe extending a different WebSocketProvider class from StaticJsonRpcProvider class?
@ricmoo Im having the same issue
Having this issue as well with both JSON or WS Providers, setting the network doesn't seem to help, not sure if its relevant but it seems here in the code the #network
is being set to null
https://github.com/ethers-io/ethers.js/blob/main/src.ts/providers/provider-jsonrpc.ts#L453
using ethers 6.3.0
Sorry. Just saw this. If you are passing in the chain, it certainly shouldn’t be calling eth_chainId. I’ll look into this shortly.
To be clear, when you say “latest version”, you mean v6?
Just saw. :)
thanks @ricmoo trying something with a free Infura account and its a bit difficult getting throttled after a few tries :sweat_smile:
I've tried initiating the library with network id and name as well, without help.
this.provider = new JsonRpcProvider(
`https://mainnet.infura.io/v3/${infuraApiKey}`,
1,
);
this.provider = new JsonRpcProvider(
`https://mainnet.infura.io/v3/${infuraApiKey}`,
'mainnet',
);
This seems to be a recurring issue, found #901, #1797, #3562. It would be great if this could be simply disabled by passing an option to the constructor, something like { detectNetwork: false }
There is a built-in InfuraProvider
which specifically suppresses calls to eth_chainId
. ;)
But I'm looking into this. :)