ethers.js icon indicating copy to clipboard operation
ethers.js copied to clipboard

Websocket Provider calling eth_chainId on instantiation

Open janschmutz opened this issue 3 years ago • 2 comments

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

janschmutz avatar Oct 22 '21 12:10 janschmutz

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 avatar Nov 01 '21 16:11 janschmutz

@janschmutz @ricmoo any information about how we can deal with this?

ChrisLahaye avatar Aug 24 '22 08:08 ChrisLahaye

Im having the same issue, maybe extending a different WebSocketProvider class from StaticJsonRpcProvider class?

alebusse avatar Oct 26 '22 14:10 alebusse

@ricmoo Im having the same issue

jinyang1994 avatar Apr 18 '23 02:04 jinyang1994

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

alko89 avatar Apr 24 '23 08:04 alko89

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.

ricmoo avatar Apr 24 '23 08:04 ricmoo

To be clear, when you say “latest version”, you mean v6?

Just saw. :)

ricmoo avatar Apr 24 '23 08:04 ricmoo

thanks @ricmoo trying something with a free Infura account and its a bit difficult getting throttled after a few tries :sweat_smile:

image

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 }

alko89 avatar Apr 24 '23 08:04 alko89

There is a built-in InfuraProvider which specifically suppresses calls to eth_chainId. ;)

But I'm looking into this. :)

ricmoo avatar Apr 24 '23 08:04 ricmoo