Skip to main content

Use ethers.js

Send a regular transaction from one account to another with the ethers.js JavaScript library.

Prerequisites

info

Use MetaMask or similar to create an Ethereum account for testing.

Steps

1. Create a project directory

Create a new directory:

mkdir infura

cd into the directory:

cd infura

2. install the dependencies

npm install --save ethers

3. Create a .env file

Create a .env file in your project directory to store the project and Ethereum account details.

ETHEREUM_NETWORK = "sepolia"
INFURA_API_KEY = "<API-KEY>"
SIGNER_PRIVATE_KEY = "<PRIVATE-KEY>"

Ensure you replace the following values in the .env file:

If using a network other than Sepolia, ensure you update ETHEREUM_NETWORK with the network name.

danger

Never disclose your private key. Anyone with your private keys can steal any assets held in your account.

4. Create eip1559_tx.js file

In this example, we'll create a JavaScript file (eip1559_tx.js) in the project directory which configures and sends the transaction.

info

Replace to_account with the relevant details.

const { ethers } = require("ethers");

async function main() {
// Configuring the connection to an Ethereum node
const network = process.env.ETHEREUM_NETWORK;
const provider = new ethers.providers.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, provider);

// Creating and sending the transaction object
const tx = await signer.sendTransaction({
to: "<to_account>",
value: ethers.utils.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}`);
}

require("dotenv").config();
main();

5. Execute the transaction

Run the script:

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 like Sepolia Etherscan.

6. Fine tune the transaction details (optional)

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,
});