Use ethers.js
In this tutorial, you'll send a transaction of 0.001 ETH from one account to another using the ethers.js
JavaScript library.
Prerequisites
- Node.js
- An Ethereum account
Use MetaMask or similar to create an Ethereum account for testing.
Steps
1. Select your network and verify funds
- Sepolia
- Alternative network
To use the Sepolia testnet, ensure that your account has Sepolia ETH. You can use the Infura faucet to add more funds.
To use an alternative network, ensure that your account has testnet ETH for that network.
When using an alternative network, you'll update your .env
file in
Step 4 with the alternative network name.
2. Create a project directory
Create a new directory for your project using the command line:
mkdir infura
Change into the new directory:
cd infura
3. Install required packages
Install the ethers
and dotenv
packages in the project directory.
The dotenv
package allows you to use a .env
file to securely store private environment variables on your local machine.
Install the ethers
package:
npm install --save ethers
Install the dotenv
package:
npm install dotenv --save
4. Create a .env
file
Create a .env
file in your project directory to store the project and Ethereum account details:
- Syntax
- Example
ETHEREUM_NETWORK = "<NETWORK>"
INFURA_API_KEY = "<YOUR-API-KEY>"
SIGNER_PRIVATE_KEY = "<PRIVATE-KEY>"
ETHEREUM_NETWORK = "sepolia"
INFURA_API_KEY = "d23...x...6e"
SIGNER_PRIVATE_KEY = "0x561...x...61df"
Replace the following values in the .env
file:
<NETWORK>
withsepolia
or the alternative network you are using.<YOUR-API-KEY>
with your API key of the web3 project.<PRIVATE-KEY>
with the private key of your Ethereum account. A transaction must be signed with the sender's private key. Make sure that you prefix theSIGNER_PRIVATE_KEY
value with0x
. The private key you export from MetaMask isn't prefixed with0x
.
Never disclose your private key. Anyone with your private keys can steal the assets controlled by those keys.
5. Create an eip1559_tx.js
file
In the project directory, create an eip1559_tx.js
file, which configures and sends the transaction. For example:
To send test ETH to an account of your choice, update line 15 with your selected account.
const { ethers, parseUnits } = require("ethers");
async function main() {
// Configuring the connection to an Ethereum node
const network = process.env.ETHEREUM_NETWORK;
const provider = new ethers.InfuraProvider(
network,
process.env.INFURA_API_KEY
);
// Creating a signing account from a private key
const signer = new ethers.Wallet(process.env.SIGNER_PRIVATE_KEY).connect(provider);
// Creating and sending the transaction object
const tx = await signer.sendTransaction({
to: "0x618917c657e9F5b346c0141CB14F5D3CED65D449", // Replace with your selected account
value: parseUnits("0.001", "ether"),
});
console.log("Mining transaction...");
console.log(`https://${network}.etherscan.io/tx/${tx.hash}`);
// Waiting for the transaction to be mined
const receipt = await tx.wait();
// The transaction is now on chain!
console.log(`Mined in block ${receipt.blockNumber}`);
}
6. Execute the transaction
To execute the transaction, run:
node eip1559_tx.js
Example output:
Mining transaction...
https://sepolia.etherscan.io/tx/0x7c5c0061fbda9e01c1bb1269ffc7323107e2116d8f7327ee945aecc7c33d21c8
Mined in block 7587728
You can search for the transaction on a block explorer such as Sepolia Etherscan.
(Optional) Fine tune the transaction details
To change default values, update the signer.sendTransaction
method to include an estimateGas
result:
const limit = provider.estimateGas({
from: signer.address,
to: "<to_address_goes_here>",
value: ethers.utils.parseUnits("0.001", "ether"),
});
// Creating and sending the transaction object
const tx = await signer.sendTransaction({
to: "<to_address_goes_here>",
value: ethers.utils.parseUnits("0.001", "ether"),
gasLimit: limit,
nonce: signer.getTransactionCount(),
maxPriorityFeePerGas: ethers.utils.parseUnits("2", "gwei"),
chainId: 3,
});