Sub topic 6
Fees and gas: paying for a transaction
This chapter assumes you know what an account, an address and a native coin are, and that you have seen a wallet ask you to confirm a transaction. It uses Ethereum-style networks (EVM chains) as the working model. Bitcoin-style fees are covered briefly at the end.
Why fees exist
Every transaction asks the network's validators to do work: check a signature, update balances, run contract code, and store the result. Validators are a limited resource, and block space is limited too. A fee does two jobs. It pays for that work, and it makes spamming the network expensive.
You always pay the fee in the network's native coin, even when you are sending a token. Sending USDC on Ethereum costs ETH. Sending a token on Polygon costs POL. If your account holds the token but no native coin, the transfer cannot be sent.
Gas: the unit of work
Gas is the unit that measures computation and storage on an EVM chain. Each operation has a fixed gas cost. Fees are then priced per unit of gas, so the work and the price are kept separate.
Some typical amounts:
| Action | Approximate gas used |
|---|---|
| Plain native-coin transfer | 21,000 |
| ERC-20 token transfer | 45,000 to 65,000 |
| Token swap on a decentralized exchange | 120,000 to 250,000 |
| Deploying a contract | Hundreds of thousands to millions |
A plain transfer always uses exactly 21,000 gas. A token transfer runs contract code that updates two balances in storage, so it costs more. The cost is higher still when the recipient has never held the token, because writing a new storage slot is expensive.
The fee formula
The total fee is the gas used multiplied by the price per unit of gas:
fee = gas used × price per gasPrice per gas is quoted in gwei. One gwei is one billionth of the native coin (10^-9 ETH). Wallets show gwei because whole-coin prices per gas would be tiny decimals.
Example: a plain transfer (21,000 gas) at 20 gwei costs 21,000 × 20 = 420,000 gwei, which is 0.00042 ETH.
EIP-1559: base fee and priority fee
Most major EVM chains use the fee market introduced by EIP-1559. The price per gas has two parts.
- Base fee: set by the protocol for each block. It rises when the previous block was more than half full and falls when it was less than half full. It is burned, meaning removed from supply, and no validator receives it.
- Priority fee (tip): an extra amount per gas that goes to the validator who includes your transaction. A higher tip makes your transaction more attractive when blocks are crowded.
You also set a max fee per gas, the highest total price per gas you will accept. The transaction pays the actual base fee plus your tip, capped by the max fee. If the base fee is lower than you allowed for, you get the difference back.
effective price = min(max fee, base fee + priority fee)Suppose you set max fee 50 gwei and tip 2 gwei, and the block's base fee is 30 gwei. You pay 32 gwei per gas. The unused 18 gwei of headroom is never charged. If the base fee jumps to 60 gwei before inclusion, your transaction does not qualify and waits.
Gas limit versus gas used
The gas limit is the most gas you allow the transaction to consume. Gas used is what it actually consumed. You pay for gas used, not for the limit. Unused gas is refunded.
The limit still matters:
- If the limit is too low, execution runs out of gas and stops. The transaction fails, state changes are reverted, and you still pay for all the gas consumed up to that point.
- If the limit is much higher than needed, you are charged only for what was used, but the wallet must be able to cover limit × max fee at submission. A balance that looks sufficient may be rejected.
Wallets and libraries ask the node to simulate the transaction (eth_estimateGas) and use that result, often with a safety margin. Simulation can be wrong if on-chain state changes between your estimate and inclusion.
Reading and estimating fees in code
The following ethers v6 script reads the network's current fee data and estimates the gas for a transfer. It sends nothing.
import { JsonRpcProvider, Wallet, parseEther, formatUnits, formatEther } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Wallet(process.env.PRIVATE_KEY, provider);
const feeData = await provider.getFeeData();
console.log("Max fee (gwei):", formatUnits(feeData.maxFeePerGas, "gwei"));
console.log("Tip (gwei):", formatUnits(feeData.maxPriorityFeePerGas, "gwei"));
const gasEstimate = await provider.estimateGas({
from: wallet.address,
to: "0x0000000000000000000000000000000000000001",
value: parseEther("0.01"),
});
console.log("Gas estimate:", gasEstimate.toString());
const worstCase = gasEstimate * feeData.maxFeePerGas;
console.log("Worst-case fee:", formatEther(worstCase));The worst-case value is the most the transaction can cost at those settings. The real cost is usually lower because the actual base fee is typically below the max fee.
If you send with wallet.sendTransaction without fee fields, ethers fills in these values for you. Set them yourself when you need control, for example to speed up a transaction.
What makes fees rise and fall
Fees follow demand for block space. When many users want inclusion at once, such as during a popular token launch, the base fee climbs block by block. When demand drops, it falls. On Ethereum the base fee can change by at most 12.5% from one block to the next, so a spike builds over several blocks and does not appear instantly.
Practical consequences:
- The same transfer can cost ten times more at a busy hour than at a quiet one.
- Fees on layer-2 networks (rollups) are usually much lower, because many transactions share the cost of posting data to the base layer. They still have a gas model, and they also include a charge for that data posting.
- Fees depend on gas used, not on the value sent. Sending 0.01 or 1,000 of a coin costs the same fee in a plain transfer.
Choosing a fee level
Most wallets offer low, medium and high presets. These mostly change the tip and max fee.
- For a non-urgent payment, choose a low or default setting and accept a wait.
- For a time-sensitive action, raise the tip. Raising the max fee alone does not help, because the validator is paid the tip, not the max fee.
- Never set the limit by hand below the estimate unless you understand why.
A very low tip does not make a transaction invalid. It makes it unattractive, so it may sit unconfirmed while the base fee rises past your max fee. That leads to stuck transactions, covered in a later chapter.
Failed transactions still cost gas
A transaction that is included in a block and then reverts has still consumed gas, and the fee is not refunded. Common causes are a contract rule that rejected the call, an insufficient token balance for a token transfer, and a limit that was too low. Before sending to a contract, a simulation (estimateGas or a dry call) often reveals the revert for free, because it fails before anything is broadcast.
A transaction that was never included, for example because it was replaced or dropped, costs nothing.
Other networks
Not every chain works this way.
- Bitcoin has no gas. The fee depends on the transaction's size in virtual bytes, and you choose a rate in satoshis per virtual byte. A transaction with many inputs is larger and pays more, regardless of the amount sent.
- Some EVM chains still use a single legacy
gasPriceinstead of base fee plus tip. The formula is the same: gas used × price. - Other networks use their own units, for example lamports on Solana, but they still charge in the native coin.
Always check which asset the wallet will deduct. Fees on a given network are paid in that network's native coin, so you need a small balance of it before you can move anything.
Common mistakes
- Holding tokens but no native coin, then being unable to send.
- Sending the entire native balance, leaving nothing for the fee. Wallets usually offer a "max" button that subtracts the fee for you. Hand-typed full amounts fail.
- Treating the estimate as a promise. The estimate is a forecast and the final fee depends on the block that includes you.
- Comparing fees in gwei across different chains. A gwei on one chain is worth a different amount of money than a gwei on another.
Exercise
A plain native transfer uses 21,000 gas. You set max fee 40 gwei and tip 3 gwei, and the block's base fee is 25 gwei. What is the effective price per gas, and what is the total fee in gwei? How much of it is burned?
Show answer
Effective price = min(40, 25 + 3) = 28 gwei. Total fee = 21,000 × 28 = 588,000 gwei (0.000588 ETH). The burned part is the base fee: 21,000 × 25 = 525,000 gwei. The validator receives the tip: 21,000 × 3 = 63,000 gwei.
Exercise
Your account holds 100 of a token and 0 of the network's native coin. You try to send 10 tokens and the wallet refuses. Why, and what do you do?
Show answer
Gas must be paid in the native coin, not in the token. The account has none, so the transaction cannot cover its fee. Send a small amount of the native coin to the account first, then repeat the token transfer.
Exercise
You raise only the max fee per gas, from 40 to 100 gwei, and leave the tip at 1 gwei. Your transaction is still slow to confirm during a busy period. Why?
Show answer
Validators are paid the tip, not the max fee. The max fee only sets your ceiling. With a tip of 1 gwei, validators have little reason to choose your transaction over others that pay more. Raise the priority fee to speed it up.