# ETH 交易费用组成

By [0xFinian](https://paragraph.com/@finian) · 2023-02-28

---

EIP-1559 前
----------

    fees = <used gas> * <gas price>
    

交易费用等于交易实际使用的 gas 数量，乘于 gas 价格（用户指定），这些钱全部被矿工拿走。 实际使用的 gas 数量不会多于 gas limit，如果所需 gas 数量超过 gas limit，则交易失败（但钱照收）。

EIP-1559 后
----------

    fees = <used gas> * (<base fee> + <priority fee>)
    

交易费用等于交易实际使用的 gas 数量，乘于 gas 价格，gas 价格由两部分组成：基础费用和优先费用（矿工小费）。基础费用由网络决定，上下浮动，看上一个区块的负载，在 -12.5% ～ 12.5% 之间浮动（空载则下调 12.5%，一半负载则不变，满载则上浮 12.5%），这部分费用直接销毁（通缩）。小费直接给矿工，当然给的越多打包越快。

用户可以设置 max base fee（或叫 max fee）和 max priority fee（或叫 priority fee）。

为什么要设置 max base fee？因为 base fee 是浮动的，有可能交易发送后，base fee 连续暴涨（每次涨幅不超过 12.5%），这时候如果之前设置的 max base fee 比较大，能够覆盖此时的 base fee，则更有可能被打包（当然还要看给的小费多少）。很容易计算得出，如果 max base fee 设为 base fee 的两倍，则在未来 6 个区块中，即使负载暴涨，也能覆盖此时的 base fee。

那矿工能拿多少呢？不会超过你设置的 priority fee。用 max base fee（max fee）减去实际 base fee，剩下的就给矿工，但最多不超过 priority fee。

举个例子，某次交易，假设被打包时 base fee 是 20，max base fee 设置为 20.5，max priority fee 设置为 1，则矿工拿走小费 0.5。那如果 max base fee 和 base fee 相同，矿工岂不是拿不到小费？这种情况，矿工应该会等到 base fee 小于 max base fee 时再打包吧，或者优先打包其他能赚到小费的。（测试了一下，在小狐狸设置小费为 0，交易一直 pending 没发出去，最终经过 3 小时成功打包，也就是没有小费矿工也会选择打包）

所以打价格战时，激进一点的做法是，查看当前 gas 情况（比如用插件 Blocknative Gas Fee Estimator），按照「Max price in pending block」的数值，设置 max base fee 和 max priority fee，比如 Max price in pending block 的数值为 50，则设置 max base fee 和 max priority fee 都为 50，则极大概率能将交易在当前区块送出去，当然这时你的花费就是 50 了（base fee 以外的全给矿工了）

参考
--

> [https://metamask.io/1559/](https://metamask.io/1559/) [https://www.blocknative.com/blog/eip-1559-fees](https://www.blocknative.com/blog/eip-1559-fees)

---

*Originally published on [0xFinian](https://paragraph.com/@finian/eth)*
