2

What is the difference between ethers.getDefaultProvider() and providers.EtherscanProvider() in ethersjs and when should you use one over the other?

2 Answers 2

2

From ethers docs,

ethers.getDefaultProvider() connects you to a node on the ethereum network. Use it when you want to interact directly with the blockchain.

new ethers.providers.EtherscanProvider() connects you to the Etherscan blockchain web service API. Use it if you only want data from the Etherscan web service API.

0

ethers.getDefaultProvider() is meant basically for prototyping and testing. When you connect to a specific provider with your own API key, you have better performance and access to communication metrics. (see docs)

These API keys are provided as a community resource by the backend services for low-traffic projects and for early prototyping.

Since these [default provider] API keys are shared by all users (that have not acquired their own API key), they are aggressively throttled which means retries occur more frequently and the responses are slower.

It is highly recommended that you sign up for a free API key from each service for their free tier, which (depending on the service) includes many advantages:

  • a much higher request rate and concurrent request limit
  • faster responses with fewer retries and timeouts
  • useful metric tracking for performance tuning and to analyze your customer behaviour
  • more advanced APIs, such as archive data or advanced log queries

For example, to connect to an Infura node on the Ropsten test network:

let provider = new ethers.providers.InfuraProvider("ropsten", infuraProjectID);

And for WebSockets:

let infuraRopstenWSProvider = new ethers.providers.InfuraProvider.getWebSocketProvider("ropsten", process.env.RPC_INFURA_PROJECTID);

You can also use a simper JRPC URL, however JRPC connections are read-only:

let JRPC_infuraRopstenProvider = new ethers.providers.JsonRpcProvider(process.env.RPC_INFURA_HTTP_ROPSTEN_URL);

Here is also a nice Ethers.js Cheat Sheet for other functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.