Sub topic 2
Fees and gas: paying for a transaction
This chapter assumes you can read basic JavaScript, know what an address is, and have a testnet wallet with test funds. Code uses ethers v6.
Fees and gas
Every transaction pays a fee to the network's validators. The fee equals the gas used multiplied by the price per unit of gas. A plain native-coin transfer uses 21,000 gas on Ethereum-style networks. Token transfers use more, because they run contract code.
Use estimateGas before you send, and multiply by the maximum fee per gas to get a ceiling:
import { JsonRpcProvider, Wallet, parseEther, formatEther } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Wallet(process.env.PRIVATE_KEY, provider);
const tx = { to: "0x0000000000000000000000000000000000000001", value: parseEther("0.01") };
const gas = await wallet.estimateGas(tx);
const feeData = await provider.getFeeData();
const price = feeData.maxFeePerGas ?? feeData.gasPrice;
console.log("Maximum fee:", formatEther(gas * price));Nonces and transaction ordering
A nonce is a counter for each account. Each outgoing transaction must use the next nonce in sequence. The network processes transactions from one account in nonce order, so a transaction with nonce 7 waits until nonce 6 is processed.
Ask for the pending count when you send several transactions quickly:
const nonce = await provider.getTransactionCount(wallet.address, "pending");The life of a transaction
Sending a native coin step by step
- Check that the network and the RPC URL match the test network you intend to use.
- Confirm the sender balance covers the amount plus the maximum fee.
- Sign and broadcast the transaction.
- Save the returned hash and wait for inclusion.
- Check the receipt status. A status of 1 means success.
Sending tokens
A token transfer calls the token contract's transfer function. The amount uses the token's own decimals, not 18 by default. The sender pays the fee in the network's native coin, so the wallet needs native funds even when it sends only tokens.
import { JsonRpcProvider, Wallet, Contract, parseUnits } from "ethers";
const abi = ["function decimals() view returns (uint8)", "function transfer(address to, uint256 amount) returns (bool)"];
const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Wallet(process.env.PRIVATE_KEY, provider);
const token = new Contract(process.env.TOKEN_ADDRESS, abi, wallet);
const amount = parseUnits("5.0", await token.decimals());
const tx = await token.transfer("0x0000000000000000000000000000000000000001", amount);
await tx.wait();Exercise
A wallet has 0.001 native coin and wants to send 0.0005 native coin. Estimated gas is 21,000 and the maximum fee per gas is 40 gwei. Can the transfer go through, and why?
Show answer
The fee is 21,000 × 40 gwei = 840,000 gwei, which is 0.00084 native coin. Adding the 0.0005 amount gives 0.00134, more than the 0.001 balance. The transfer fails, so the wallet needs at least 0.00134 native coin.