EIP-3198 · Final · Core · 2021-01-13
EIP-3198: BASEFEE opcode
Added the BASEFEE opcode (0x48), which returns the current block's EIP-1559 base fee so contracts can react to network fee levels.
What problem does it solve?
EIP-1559 gave every block a protocol-set base fee per gas. That number lives in the block header, but without a new opcode a contract had no way to read it.
Contracts that care about gas prices had to rely on guesses, admin-updated constants or tx.gasprice (which the caller controls). Typical use cases the spec gives:
- Keeper bounties: a contract that pays people to "poke" it can set the reward to
basefee + xorbasefee * (1 + x), so it always covers the cost of the call whatever market conditions are. - Gas price derivatives: futures or options on gas can settle against an on-chain, manipulation-resistant price.
- Fraud-proof systems: state channels and optimistic rollups can lengthen challenge periods automatically when base fee is high and posting a challenge is expensive.
How it works
One opcode, activated in the London upgrade together with EIP-1559:
| Opcode | Name | Inputs | Outputs | Gas |
|---|---|---|---|---|
0x48 |
BASEFEE |
0 | 1 | 2 (G_base) |
It pushes the base fee of the block currently being executed, in wei per gas, as a 32-byte word. The value is already known before execution starts (it is needed to validate transactions), so the opcode costs the same as other cheap environment reads like NUMBER or TIMESTAMP.
Example from the spec: if the base fee is 7 wei, bytecode 0x4800 (BASEFEE, STOP) leaves 7 on the stack and uses 2 gas.
Sample code
In Solidity (0.8.7 and later) the opcode is exposed as block.basefee:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract PokeBounty {
uint256 public constant POKE_GAS = 60_000;
uint256 public lastPoke;
receive() external payable {}
function poke() external {
require(block.timestamp >= lastPoke + 1 hours, "too soon");
lastPoke = block.timestamp;
// ... do the periodic work ...
// Reimburse at base fee plus a 20% margin, independent of the caller's tip.
uint256 reward = POKE_GAS * block.basefee * 12 / 10;
(bool ok, ) = msg.sender.call{value: reward}("");
require(ok, "pay failed");
}
function priorityFeePaid() external view returns (uint256) {
return tx.gasprice - block.basefee; // the tip per gas this tx actually pays
}
}
From off-chain code, the same value is in the block header (baseFeePerGas):
const block = await provider.getBlock("latest");
console.log(block!.baseFeePerGas); // bigint, wei per gas
Things to know
- Status: Final. Live on mainnet since the London upgrade (August 2021).
tx.gasprice - block.basefeegives the effective priority fee of the current transaction.- In
eth_callsimulations some nodes run with a base fee of 0 when no gas price is supplied, soblock.basefeemay read 0 there. Do not treat that as the real network value. - The base fee can move by up to 12.5 % per block, so a reward based on it is only a rough estimate of what the caller paid.
- Blob transactions have a separate blob base fee, exposed by the
BLOBBASEFEEopcode (block.blobbasefee), not by this one.