---
eip: 3198
label: EIP-3198
title: BASEFEE opcode
status: Final
type: Standards Track
category: Core
created: 2021-01-13
requires: 1559
official: https://eips.ethereum.org/EIPS/eip-3198
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3198.md
canonical: https://eips.huatuo.xyz/zh/eip-3198/
lang: zh
tldr: 新增 BASEFEE 操作码（0x48），返回当前区块的 EIP-1559 基础费用，让合约可以根据网络费用水平作出反应。
tags: [evm, opcode, gas, fees, london]
---

# EIP-3198: BASEFEE opcode

> Adds an opcode that gives the EVM access to the block's base fee.

**TL;DR:** 新增 BASEFEE 操作码（0x48），返回当前区块的 EIP-1559 基础费用，让合约可以根据网络费用水平作出反应。

## 它解决什么问题？

[EIP-1559](/eip-1559/) 为每个区块设定了由协议决定的**每单位 gas 基础费用**。这个数字存放在区块头中，但没有新的操作码，合约就无法读取它。

关心 gas 价格的合约只能依靠猜测、由管理员更新的常量，或 `tx.gasprice`（由调用者控制）。规范给出的典型用例：

- **Keeper 赏金**：为"戳一下"合约而向人付费的合约，可以把奖励设为 `basefee + x` 或 `basefee * (1 + x)`，这样无论市场状况如何，都能始终覆盖调用的成本。
- **gas 价格衍生品**：关于 gas 的期货或期权可以对照链上难以操纵的价格进行结算。
- **欺诈证明系统**：状态通道和 optimistic rollup 可以在基础费用较高、提交质疑成本昂贵时，自动延长质疑期。

## 它如何运作

一个操作码，在伦敦升级中与 EIP-1559 一同激活：

| 操作码 | 名称 | 输入 | 输出 | Gas |
| --- | --- | --- | --- | --- |
| `0x48` | `BASEFEE` | 0 | 1 | 2（`G_base`） |

它把当前正在执行的区块的基础费用（单位为每 gas 多少 wei）作为 32 字节的字压入栈。该值在执行开始前就已知（验证交易时需要它），因此该操作码的成本与 `NUMBER` 或 `TIMESTAMP` 等其他廉价的环境读取相同。

规范中的示例：若基础费用为 7 wei，字节码 `0x4800`（`BASEFEE`、`STOP`）会在栈上留下 `7`，并消耗 2 gas。

## 示例代码

在 Solidity（0.8.7 及以后）中，该操作码以 `block.basefee` 的形式提供：

```solidity
// 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
    }
}
```

在链下代码中，同样的值位于区块头（`baseFeePerGas`）：

```ts
const block = await provider.getBlock("latest");
console.log(block!.baseFeePerGas); // bigint, wei per gas
```

## 需要注意

- 状态：**Final**。自伦敦升级（2021 年 8 月）起已在主网上线。
- `tx.gasprice - block.basefee` 可得到当前交易的实际优先费。
- 在 `eth_call` 模拟中，如果没有提供 gas 价格，一些节点会以 0 作为基础费用运行，因此那里的 `block.basefee` 可能读出 0。不要把它当作网络的真实值。
- 基础费用每个区块最多可变动 12.5%，因此基于它的奖励只是对调用者所付费用的粗略估计。
- Blob 交易有单独的 blob 基础费用，由 `BLOBBASEFEE` 操作码（`block.blobbasefee`）提供，而不是这个操作码。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-3198
