Skip to main content

Preparing a transaction using ethers.js

In this tutorial, we will learn how to prepare a transaction using ethers.js. we will use Testnet to send the transaction and you can get fund from ETDFaucet.

Importing the library

First, we need to import the ethers library.

import { ethers } from "ethers";

Creating a provider

We need to create a provider to connect to the network. We will use the

const provider = new ethers.providers.JsonRpcProvider(
"https://rpc.debugchain.net"
);

Creating a signer

We need to create a signer to sign the transaction. Replace YOUR_PRIVATE_KEY with your private key.

// we will get wallet from private key
const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider);

Prepare a transaction

We can prepare a transaction by using the signer.signTransaction method. Replace RECEIVER_ADDRESS with your address.

const receiver = "RECEIVER_ADDRESS";

// get total transaction count from sender as nonce
const nonce = await provider.getTransactionCount(wallet.address);

const signedTransaction = await signer.signTransaction({
to: receiver,
value: ethers.utils.parseEther("0.01"),
nonce: nonce,
gasPrice: await provider.getGasPrice(),
gasLimit: 21000,
chainId: 8348, // this is the chainId of Testnet
});

Send the transaction

We can send the transaction by using the provider.sendTransaction method.

const tx = await provider.sendTransaction(signedTransaction);

Check the transaction

You can check the transaction status by going to https://faucet.debugchain.net/tx/TRANSACTION_HASH. Replace your TRANSACTION_HASH with the transaction hash.

Full code

const provider = new ethers.providers.JsonRpcProvider(
"https://rpc.debugchain.net"
);

// we will get wallet from private key
const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider);

// put your receiver here
const receiver = "RECEIVER_ADDRESS";

// get total transaction count from sender as nonce
const nonce = await provider.getTransactionCount(wallet.address);

// sign transaction
const signedTransaction = await wallet.signTransaction({
to: receiver,
value: ethers.utils.parseEther("0.01"),
nonce: nonce,
gasPrice: await provider.getGasPrice(),
gasLimit: 21000,
chainId: 8348,
});

console.log("Signed transaction", signedTransaction);

const txId = await provider.sendTransaction(signedTransaction);
console.log("Transaction id", txId);

CodeSandbox

Before using the sandbox to send the transaction, make sure you set up the secret in sandbox by adding PK in the sandbox settings. Name should be pk and value should be your private key.

sandbox

You can check the transaction sent in the above sandbox by going to the ETDFaucet.