Use Ethers.js InfuraProvider
or Web3Provider
In this tutorial, you'll create a simple React app to show the differences between using the
Ethers.js library's InfuraProvider
and Web3Provider
methods to send a transaction.
The key difference is that with Web3Provider
, you can load the private key from a web3 wallet
(for example, MetaMask), while InfuraProvider
needs a wallet created locally with a stored private key.
This tutorial uses the Sepolia testnet.
Prerequisites
- Install MetaMask and create an Ethereum account for testing purposes.
- Load Sepolia ETH into your wallet from the MetaMask faucet.
- Node.js and npm installed.
You can run the following to confirm that Node and npm are installed:
node -v && npm -v
This tutorial was tested using ethers v5.7.2 and Node.js v16.17.0.
Steps
1. Create the React app
In the terminal, run the following command to create an app called my-app
:
npx create-react-app my-app
Change into the project directory:
cd my-app
2. Install ethers.js
Install ethers.js
in the project directory.
npm install --save ethers