Avoid eth_chainId call when creating a WebSocketProvider #1818
-
Hey there, I'm developing a wallet application as a chrome extension and noticed that a big chunk of my infura usage boils down to In my case it is impossible because we always use mainnet, so I'm wondering if it is possible to disable this feature while using the WebSocketProvider (which obviously helps me save resources elsewhere e.g. eth_blockNumber) |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments
-
The WebSocketProvider should be safe to mimic the StaticJsonRpcProvider logic to not call chainId. I’ll investigate this tomorrow. :) |
Beta Was this translation helpful? Give feedback.
-
Taking a quick look at the code, it looks like it should cache the chainId. I'll investigate more tomorrow, but quick check on what version of ethers are you using? |
Beta Was this translation helpful? Give feedback.
-
yes, thanks it is npm version 5.0.32 |
Beta Was this translation helpful? Give feedback.
-
Can you try upgrading to 5.4.1 and checking this wasn't fixed by a change since March? I don't recall making any change that would affect this, but just to double-check. :) |
Beta Was this translation helpful? Give feedback.
-
Hi @ricmoo so I figured out in the end where all these eth_chainId requests come from with the websocket provider I think. Every time a new websocket provider instance is created (E.g. onclose handler is called or similar.) there is a single eth_chainId request conducted internally. In my case this happens quite often thus the number of requests. Is this necessary internally or is there a way to perhaps disable this feature? |
Beta Was this translation helpful? Give feedback.
-
This is necessary, but if you know the // Replace the current provider, but re-use the network lookup
websocket = new WebSocket(websocket.connection.url, websocket.getNetwork()); Does that make sense? |
Beta Was this translation helpful? Give feedback.
-
totally, thanks that solved the issue for me! |
Beta Was this translation helpful? Give feedback.
-
Moving to discussion in case someone else has a similar question in the future. :) |
Beta Was this translation helpful? Give feedback.
-
Ahh @ricmoo looks like I was a little early, the infura requests just came in with some time delay.
If I understand your code example correctly above you suggest just replacing the underlying websocket and not creating a new provider instance?
this is not possible because the property is marked readonly here I think. Did I misunderstand you ? |
Beta Was this translation helpful? Give feedback.
-
This would only work for JsonRpcProvider not WebSocketProvider, that one always calls detectNetwork(). |
Beta Was this translation helpful? Give feedback.
This is necessary, but if you know the
chainId
you can pass it in as the second parameter to the WebSocketProvider connector, or even passing in the.getNetwork()
promise should work, like: