2

I can make providers using ether-hardhat, alchemy, and metamask. What is the difference between these?

1 Answer 1

2

The main difference between then is the infrastructure they use to connect to the Ethereum network - they also offer different features. Think of them as different services that will allow you to interact with the Ethereum Blockchain in different ways.

You can also read the official ethers.js Docs about providers: https://docs.ethers.org/v5/api/providers/

A Provider is an abstraction of a connection to the Ethereum network, providing a concise, consistent interface to standard Ethereum node functionality. The ethers.js library provides several options which should cover the vast majority of use-cases, but also includes the necessary functions and classes for sub-classing if a more custom configuration is necessary.

You may classify them in Node and API Providers.

API Providers

API providers like InfuraProvider, EtherscanProvider, Alchemyprovider allow developers to utilize their infrastructure to interact with Ethereum. Some of the features you can get is real-time data, access to Nodes without running your own, and generally speaking devtools for building dApps.

AlchemyProvider

const provider = new ethers.providers.AlchemyProvider('goerli', 'your-alchemy-api-key');

InfuraProvider

const provider = new ethers.providers.InfuraProvider('goerli', 'your-infura-api-key');

EtherscanProvider

const provider = new ethers.providers.EtherscanProvider('goerli', 'your-etherscan-api-key');

Node Providers

Node providers on the other hand like JsonRpcProvider, HardhatProvider, and Web3Provider basically allow you to run your own nodes and interact with the network directly.

JsonRpcProvider connects to a Ethereum node via JSON-RPC

const provider = new ethers.providers.JsonRpcProvider('http://localhost:8545');

Hardhat using provider object from hardhat runtime or with Web3Provider

const hre = require('hardhat');
const provider = hre.ethers.provider;

// or with Web3Provider ..

const hre = require('hardhat');
const provider = new ethers.providers.Web3Provider(hre.network.provider);

Web3Provider with Metamask

const provider = new ethers.providers.Web3Provider(window.ethereum);

So the choice which provider you choose will depend on your specific needs - if you don't want to run you own node, an API provider like AlchemyProvider will probably the most convenient way. If you want more control, you might look at JsonRpcProvider for example.

3

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.