I can make providers using ether-hardhat, alchemy, and metamask. What is the difference between these?
1 Answer
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.
-
look at this smart contract code : gist.github.com/ameeetgaikwad/4fff65794f688e44b6ae812e5c4e833c now look at this deploy.js :gist.github.com/ameeetgaikwad/1d4b0e09e542a57ba150934f59c96902 I didn't use any provider how am I able to call
abc
function ?– amitMar 5 at 18:15 -
its probably better to create a new separate question and post your code there plus further describe your problem, as in the comment section here Mar 5 at 18:27
-
1You are right, here is the new question: ethereum.stackexchange.com/questions/146925/…– amitMar 6 at 3:13