EVM 兼容链 Gas 费用估算方法
March 16th, 2023

以太坊和其他 EVM 兼容链的 Gas 费用计算遵循下面这个公式。 

GasFee = gasPrice * gasCount * tokenPrice

 当然在 EIP-1559 升级之后,公式中的 gasPrice 增加了一项矿工的小费,也就是我们在提交交易时设置的 Max Priority 值,正常情况这个值很小。在估算的时候我们可以暂时忽略。

gasCount 依赖于合约的复杂程度和存储的数据长度,比如大多数的 NFT(ERC721)合约铸造一个 NFT 的 gasCount 在 [150,000, 210,000] 之间,部署好的合约,这个值基本不会变。

gasPrice 随网络负载情况变化,网络拥堵程度不同,差值可能非常大。以 Polygon 链为例,极端情况能达到20000Gwei 以上,虽然极少发生。常见的浮动范围在 [30, 2,500] Gwei 之间,一般熊市 gasPrice 多在 50Gwei 附近,牛市多在 500Gwei 附近,这里的 gasPrice 是估算,更准确数据需要读取链上大量历史交易数据进行分析。这里的一个 Gwei 是 10^9 wei。换算成 Native token后,对以太坊来说就是 10^-9 eth,对 Polygon 来说就是 10^-9 matic。

tokenPrice 是公链的 Native token,以太坊为 eth,Polygon 链为 matic,价格依赖于牛熊,变化范围也很大,根据这个公式就可以大概计算出消耗的 Gas 费用了。

 

Subscribe to MrBWG
Receive the latest updates directly to your inbox.
Nft graphic
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from MrBWG

Skeleton

Skeleton

Skeleton