Sub topic 6
Fees and gas: paying for a transaction
Assume you understand what a blockchain and an account are. This chapter focuses on account-based networks such as Ethereum. Basic JavaScript is needed only for the optional code example.
What gas pays for
Gas measures the computational work required by a transaction. A simple native-coin transfer uses a small, predictable amount of gas. A token transfer usually uses more because it calls a smart contract. More complex contract calls can use much more.
The network charges for each gas unit used. The basic fee calculation is:
transaction fee = gas used × price per gas unit
Gas is not the asset you send. On Ethereum, the fee is paid in ETH. On another network, it is paid in that network's native coin. Sending a token does not normally pay the fee in that token.
A transaction has a gas limit. This is the maximum gas the sender allows the transaction to consume. If the transaction needs more gas than this limit, it fails. Any unused gas from a successful transaction is not charged.
EIP-1559 fee fields
On Ethereum-style networks that use EIP-1559, a transaction commonly has these fields:
| Field | Meaning |
|---|---|
| Gas limit | Maximum number of gas units the transaction may use |
| Base fee | Network-determined fee per gas unit; it is burned |
| Priority fee | Tip offered to the block producer |
| Max fee | Highest total fee per gas unit the sender permits |
The effective price is generally the base fee plus the priority fee, but it cannot exceed the max fee. A useful upper-bound estimate is:
gas limit × max fee
The wallet may suggest these values automatically. A higher priority fee can help a transaction get included sooner when demand is high. A higher max fee protects the transaction from being rejected if the base fee rises before inclusion. Setting both values too low can leave the transaction pending.
import { JsonRpcProvider, formatEther, parseEther } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const from = process.env.FROM_ADDRESS;
const to = "0x0000000000000000000000000000000000000001";
const feeData = await provider.getFeeData();
const gasLimit = await provider.estimateGas({
from,
to,
value: parseEther("0.01"),
});
const maxFee = feeData.maxFeePerGas ?? feeData.gasPrice;
if (maxFee === null) {
throw new Error("The provider returned no fee quote");
}
const worstCaseFee = gasLimit * maxFee;
console.log("Gas limit:", gasLimit.toString());
console.log("Max fee per gas:", maxFee.toString());
console.log("Worst-case fee in native coin:", formatEther(worstCaseFee));The estimate is a quote, not a guarantee. Network conditions can change between estimation and inclusion. A transaction can also use a different amount of gas if its contract state changes.
Reading a fee estimate
A wallet usually shows the amount being sent and the network fee separately. Check both before approving. Make sure the account has enough native coin for the fee in addition to the amount being sent.
Gas estimation can fail when the transaction would revert. Common causes include an insufficient token balance, an incorrect contract address, a missing allowance, or invalid contract arguments. Increasing the gas limit does not fix a transaction whose logic will revert.
Fee decisions and common errors
Use a current wallet quote for ordinary transfers. Avoid copying a gas price from an old transaction. Verify that the wallet is connected to the intended network, because fee units and native coins differ across networks.
A very high fee may confirm quickly but costs more. A very low fee may remain pending. Canceling or replacing a pending transaction usually requires another transaction with the same nonce and a sufficiently higher fee.
Exercise
You want to send 5 USDC on an Ethereum-compatible network. Your wallet shows 5 USDC and a fee of 0.0008 ETH. What must the account hold before you send?
Show answer
It must hold at least 5 USDC and at least 0.0008 ETH, plus a small safety margin if the fee estimate may change.