一笔交易怎么确定需要多少 Gas?

简单
第 1 / 3 题
上一题下一题
参考答案模拟面试

标准答案:

  • 使用 eth_estimateGas 或框架提供的 contract.estimateGas.method(args) 进行估算。
  • 节点会在本地模拟执行交易,返回一个预估值。
  • 实际执行时仍可能因为链上状态不同而略有差异。
▾代码块
JavaScript
1
const gasEstimate = await contract.estimateGas.transfer(to, amount);