Skip to content

Gas 估算与优化:前端 Gas 管理实践

Gas 机制解析:EIP-1559 的 base fee + priority fee ​

EIP-1559 重塑了以太坊的费用市场。每笔交易的费用由两部分组成:

  • base fee:由协议根据区块拥堵程度动态调整,被燃烧而非支付给验证者
  • priority fee(tip):用户自愿支付给验证者的小费,影响交易打包优先级

交易通过两个参数控制 Gas 价格:

  • maxFeePerGas:用户愿意支付的最高 Gas 价格(base fee + priority fee 的上限)
  • maxPriorityFeePerGas:用户愿意支付的 priority fee

实际支付:min(maxFeePerGas, baseFee + maxPriorityFeePerGas)

当 base fee 上涨时,如果 maxFeePerGas 足够高,交易仍能被打包,priority fee 会自动降低。这种机制使前端无需精确预测 Gas 价格,只需设置合理的上限。

前端 Gas 估算 API:eth_estimateGas, eth_feeHistory ​

以太坊提供了两个核心 API 用于 Gas 估算:

typescript
import { createPublicClient, http, type Address } from 'viem'
import { mainnet } from 'viem/chains'

const publicClient = createPublicClient({
  chain: mainnet,
  transport: http(),
})

// 1. eth_estimateGas — 估算交易所需的 Gas Limit
async function estimateGasLimit(
  from: Address,
  to: Address,
  data: `0x${string}` = '0x',
  value: bigint = 0n,
): Promise<bigint> {
  try {
    const gas = await publicClient.estimateGas({
      account: from,
      to,
      data,
      value,
    })
    // 增加 20% 缓冲,避免边缘情况
    return (gas * 120n) / 100n
  } catch (error) {
    // 估算失败通常意味着交易会 revert
    throw new GasEstimationError(
      'Gas estimation failed. Transaction may revert.',
      error,
    )
  }
}

// 2. eth_feeHistory — 获取历史 Gas 价格分布
async function getGasPriceDistribution(
  blockCount: number = 20,
  rewardPercentiles: number[] = [10, 50, 90],
) {
  const feeHistory = await publicClient.getFeeHistory({
    blockCount,
    rewardPercentiles,
  })

  const baseFees = feeHistory.baseFeePerGas
  const gasUsedRatios = feeHistory.gasUsedRatio

  // 计算最近区块的 priority fee 分布
  const rewards = feeHistory.reward || []
  const p10 = rewards.map((r) => r[0]).filter(Boolean)
  const p50 = rewards.map((r) => r[1]).filter(Boolean)
  const p90 = rewards.map((r) => r[2]).filter(Boolean)

  return {
    currentBaseFee: baseFees[baseFees.length - 1],
    nextBaseFee: baseFees[baseFees.length - 1], // 下一区块的预估 base fee
    avgPriorityFee: {
      low: p10.length ? p10.reduce((a, b) => a + b, 0n) / BigInt(p10.length) : 0n,
      medium: p50.length ? p50.reduce((a, b) => a + b, 0n) / BigInt(p50.length) : 0n,
      high: p90.length ? p90.reduce((a, b) => a + b, 0n) / BigInt(p90.length) : 0n,
    },
    congestionLevel: this.calculateCongestion(gasUsedRatio),
  }
}

maxFeePerGas 与 maxPriorityFeePerGas 的计算 ​

合理的 Gas 价格计算策略需要平衡确认速度和费用成本:

typescript
type SpeedPreference = 'slow' | 'standard' | 'fast' | 'instant'

interface GasPriceSuggestion {
  maxFeePerGas: bigint
  maxPriorityFeePerGas: bigint
  estimatedSeconds: number
  estimatedCost: (gasLimit: bigint) => bigint
}

async function suggestGasPrice(
  publicClient: PublicClient,
  speed: SpeedPreference = 'standard',
): Promise<GasPriceSuggestion> {
  const [block, feeHistory] = await Promise.all([
    publicClient.getBlock(),
    publicClient.getFeeHistory({
      blockCount: 20,
      rewardPercentiles: [10, 25, 50, 75, 90],
    }),
  ])

  const baseFee = block.baseFeePerGas!
  const rewards = feeHistory.reward || []

  // 根据速度偏好选择 priority fee
  const percentileMap: Record<SpeedPreference, number> = {
    slow: 10,
    standard: 50,
    fast: 75,
    instant: 90,
  }

  const percentile = percentileMap[speed]
  const priorityFees = rewards.map((r) => r[percentileMap[speed]]).filter(Boolean)

  const avgPriorityFee = priorityFees.length > 0
    ? priorityFees.reduce((a, b) => a + b, 0n) / BigInt(priorityFees.length)
    : 1_000_000_000n // 默认 1 gwei

  // maxFeePerGas = baseFee * 2 + priorityFee(2 倍缓冲应对 base fee 波动)
  const maxFeePerGas = baseFee * 2n + avgPriorityFee
  const maxPriorityFeePerGas = avgPriorityFee

  // 估算确认时间
  const estimatedBlocks: Record<SpeedPreference, number> = {
    slow: 5,
    standard: 3,
    fast: 2,
    instant: 1,
  }
  const estimatedSeconds = estimatedBlocks[speed] * 12

  return {
    maxFeePerGas,
    maxPriorityFeePerGas,
    estimatedSeconds,
    estimatedCost: (gasLimit: bigint) => gasLimit * maxFeePerGas,
  }
}

估算失败的原因与处理 ​

Gas 估算失败是前端常见问题,常见原因和解决方案:

typescript
class GasEstimationErrorHandler {
  // 分类处理估算失败
  async handleEstimationError(
    error: unknown,
    publicClient: PublicClient,
    tx: { from: Address; to: Address; data: `0x${string}`; value?: bigint },
  ): Promise<GasEstimationResult> {
    // 原因 1:交易会 revert
    const revertReason = await this.tryGetRevertReason(publicClient, tx)
    if (revertReason) {
      return {
        success: false,
        reason: 'revert',
        message: revertReason,
        suggestion: '请检查合约条件和参数',
      }
    }

    // 原因 2:Out of Gas(Gas Limit 不够)
    const code = await this.estimateGasWithHighLimit(publicClient, tx)
    if (code) {
      return {
        success: false,
        reason: 'out_of_gas',
        message: '交易 Gas 不足',
        suggestion: '增加 Gas Limit',
      }
    }

    // 原因 3:Nonce 冲突
    const nonce = await publicClient.getTransactionCount({
      address: tx.from,
      blockTag: 'pending',
    })
    const pendingTxs = await this.getPendingTransactions(tx.from)
    if (pendingTxs.length > 0) {
      return {
        success: false,
        reason: 'nonce_conflict',
        message: '有待处理交易阻塞',
        suggestion: '等待待处理交易完成或加速',
      }
    }

    // 原因 4:RPC 节点问题
    return {
      success: false,
      reason: 'rpc_error',
      message: 'RPC 节点暂时不可用',
      suggestion: '切换 RPC 节点重试',
    }
  }

  private async tryGetRevertReason(
    publicClient: PublicClient,
    tx: { from: Address; to: Address; data: `0x${string}`; value?: bigint },
  ): Promise<string | null> {
    try {
      await publicClient.simulate({
        account: tx.from,
        to: tx.to,
        data: tx.data,
        value: tx.value,
      })
      return null
    } catch (error: any) {
      return error.shortMessage || error.message
    }
  }
}

Gas 优化策略:批量交易(multicall) ​

Multicall 是最有效的 Gas 优化手段之一。它将多个合约调用合并为一笔交易:

typescript
import { createPublicClient, http, encodeFunctionData, type Address } from 'viem'
import { mainnet } from 'viem/chains'

// Multicall 地址(Ethereum mainnet)
const MULTICALL3_ADDRESS = '0xcA11bde05977b363116702886EbE2E56Ab8d6F2E' as Address

class BatchTransactionBuilder {
  private publicClient: PublicClient

  constructor(rpcUrl: string) {
    this.publicClient = createPublicClient({
      chain: mainnet,
      transport: http(rpcUrl),
    })
  }

  // 批量读取(aggregate3)
  async batchRead(
    calls: { target: Address; callData: `0x${string}` }[],
  ): Promise<`0x${string}`[]> {
    const results = await this.publicClient.readContract({
      address: MULTICALL3_ADDRESS,
      abi: multicall3Abi,
      functionName: 'aggregate3',
      args: [calls.map((c) => ({ target: c.target, callData: c.callData, allowFailure: false }))],
    })

    return results.map((r) => r.returnData)
  }

  // 批量写入(将多个操作合并为一笔交易)
  buildBatchWrite(
    walletClient: WalletClient,
    calls: { target: Address; callData: `0x${string}`; value?: bigint }[],
  ) {
    // 方式一:使用 Multicall3 的 aggregate3Value(支持发送 ETH)
    const multicallData = encodeFunctionData({
      abi: multicall3Abi,
      functionName: 'aggregate3Value',
      args: [
        calls.map((c) => ({
          target: c.target,
          callData: c.callData,
          value: c.value || 0n,
          allowFailure: false,
        })),
      ],
    })

    const totalValue = calls.reduce((sum, c) => sum + (c.value || 0n), 0n)

    return {
      to: MULTICALL3_ADDRESS,
      data: multicallData,
      value: totalValue,
    }
  }

  // 实际使用:一次性 approve + swap + stake
  async buildDeFiCombo(
    walletClient: WalletClient,
    params: {
      tokenIn: Address
      tokenOut: Address
      amountIn: bigint
      minAmountOut: bigint
      router: Address
      staking: Address
    },
  ) {
    const calls = [
      // 1. Approve Router
      {
        target: params.tokenIn,
        callData: encodeFunctionData({
          abi: erc20Abi,
          functionName: 'approve',
          args: [params.router, params.amountIn],
        }),
      },
      // 2. Swap
      {
        target: params.router,
        callData: encodeFunctionData({
          abi: routerAbi,
          functionName: 'swapExactTokensForTokens',
          args: [params.amountIn, params.minAmountOut, [params.tokenIn, params.tokenOut], MULTICALL3_ADDRESS],
        }),
      },
      // 3. Approve Staking
      {
        target: params.tokenOut,
        callData: encodeFunctionData({
          abi: erc20Abi,
          functionName: 'approve',
          args: [params.staking, params.minAmountOut],
        }),
      },
      // 4. Stake
      {
        target: params.staking,
        callData: encodeFunctionData({
          abi: stakingAbi,
          functionName: 'stake',
          args: [params.minAmountOut],
        }),
      },
    ]

    const batchTx = this.buildBatchWrite(walletClient, calls)

    // 估算 Gas
    const gasLimit = await this.publicClient.estimateGas({
      account: walletClient.account!.address,
      to: batchTx.to!,
      data: batchTx.data,
      value: batchTx.value,
    })

    return {
      ...batchTx,
      gasLimit: (gasLimit * 120n) / 100n,
    }
  }
}

Gas 节省量化:4 笔独立交易约需 4 × 21000(基础)+ 4 × 合约执行 Gas。通过 Multicall 合并,只需 1 × 21000 + 4 × 合约执行 Gas,节省约 63000 Gas。

合约层面的 Gas 优化对前端的影响 ​

合约优化直接影响前端的 Gas 估算和用户体验:

  • 存储优化:使用 uint128 而非 uint256 可以打包多个变量到一个 slot
  • 批量操作:合约提供的批量函数比前端 multicall 更高效
  • 事件 vs 存储:只读数据用事件而非存储可以节省大量 Gas
  • 代理模式:代理合约的 delegatecall 比 call 多消耗约 2000 Gas

前端应感知合约层面的优化。例如,如果合约提供了 batchTransfer 函数,前端应优先使用而非通过 multicall 调用多次 transfer。

完整的 Gas 估算与管理模块 ​

typescript
class GasManager {
  private publicClient: PublicClient
  private priceCache: { price: GasPriceSuggestion; timestamp: number } | null = null
  private cacheTTL = 10000 // 10 秒缓存

  constructor(publicClient: PublicClient) {
    this.publicClient = publicClient
  }

  // 获取 Gas 价格(带缓存)
  async getGasPrice(speed: SpeedPreference = 'standard'): Promise<GasPriceSuggestion> {
    if (this.priceCache && Date.now() - this.priceCache.timestamp < this.cacheTTL) {
      return this.priceCache.price
    }

    const price = await suggestGasPrice(this.publicClient, speed)
    this.priceCache = { price, timestamp: Date.now() }
    return price
  }

  // 完整的交易 Gas 估算
  async estimateTransaction(tx: {
    from: Address
    to: Address
    data?: `0x${string}`
    value?: bigint
  }, speed: SpeedPreference = 'standard'): Promise<TransactionGasEstimate> {
    const [gasLimit, gasPrice] = await Promise.all([
      this.estimateGasWithFallback(tx),
      this.getGasPrice(speed),
    ])

    const gasCost = gasLimit * gasPrice.maxFeePerGas
    const gasCostEth = formatEther(gasCost)
    const gasCostUsd = await this.convertToUsd(gasCost)

    return {
      gasLimit,
      maxFeePerGas: gasPrice.maxFeePerGas,
      maxPriorityFeePerGas: gasPrice.maxPriorityFeePerGas,
      estimatedCostWei: gasCost,
      estimatedCostEth: gasCostEth,
      estimatedCostUsd: gasCostUsd,
      estimatedSeconds: gasPrice.estimatedSeconds,
    }
  }

  // 带回退的 Gas 估算
  private async estimateGasWithFallback(tx: {
    from: Address
    to: Address
    data?: `0x${string}`
    value?: bigint
  }): Promise<bigint> {
    try {
      const gas = await this.publicClient.estimateGas(tx)
      return (gas * 120n) / 100n // 20% 缓冲
    } catch {
      // 回退到固定 Gas Limit
      return this.getFallbackGasLimit(tx.data)
    }
  }

  // 根据调用数据大小估算 Gas
  private getFallbackGasLimit(data?: `0x${string}`): bigint {
    if (!data || data === '0x') {
      return 21000n // 简单转账
    }
    // 合约调用基础 + 数据 Gas
    const dataSize = (data.length - 2) / 2
    return 30000n + BigInt(dataSize * 16)
  }

  private async convertToUsd(wei: bigint): Promise<number> {
    // 从 Chainlink 获取 ETH/USD 价格
    const ethUsd = await this.getEthUsdPrice()
    return Number(formatEther(wei)) * ethUsd
  }

  private async getEthUsdPrice(): Promise<number> {
    const price = await this.publicClient.readContract({
      address: '0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419', // Chainlink ETH/USD
      abi: chainlinkAbi,
      functionName: 'latestAnswer',
    })
    return Number(price) / 1e8
  }
}

interface TransactionGasEstimate {
  gasLimit: bigint
  maxFeePerGas: bigint
  maxPriorityFeePerGas: bigint
  estimatedCostWei: bigint
  estimatedCostEth: string
  estimatedCostUsd: number
  estimatedSeconds: number
}

Gas 预测 UI:交易前展示预估费用 ​

良好的 Gas UI 应在用户签名前展示完整费用信息:

tsx
function GasEstimationPanel({ estimate, onChange }: {
  estimate: TransactionGasEstimate
  onChange: (speed: SpeedPreference) => void
}) {
  const [speed, setSpeed] = useState<SpeedPreference>('standard')

  return (
    <div className="gas-panel">
      <div className="gas-speed-selector">
        {(['slow', 'standard', 'fast', 'instant'] as const).map((s) => (
          <button
            key={s}
            className={speed === s ? 'active' : ''}
            onClick={() => { setSpeed(s); onChange(s) }}
          >
            <span className="label">{SPEED_LABELS[s]}</span>
            <span className="time">~{estimate.estimatedSeconds}s</span>
          </button>
        ))}
      </div>

      <div className="gas-breakdown">
        <div className="row">
          <span>Gas Limit</span>
          <span>{Number(estimate.gasLimit).toLocaleString()}</span>
        </div>
        <div className="row">
          <span>Base Fee</span>
          <span>{formatGwei(estimate.maxFeePerGas - estimate.maxPriorityFeePerGas)} Gwei</span>
        </div>
        <div className="row">
          <span>Priority Fee</span>
          <span>{formatGwei(estimate.maxPriorityFeePerGas)} Gwei</span>
        </div>
        <div className="row total">
          <span>预估总费用</span>
          <span>
            {estimate.estimatedCostEth} ETH
            <span className="usd">(~${estimate.estimatedCostUsd.toFixed(2)})</span>
          </span>
        </div>
      </div>
    </div>
  )
}

L2 Gas 估算的差异 ​

L2 的 Gas 机制与 L1 有显著差异,前端必须适配:

typescript
interface L2GasEstimator {
  chainId: number
  estimateGas: (tx: TransactionRequest) => Promise<{
    gasLimit: bigint
    l1DataGas: bigint   // L1 数据发布成本(L2 特有)
    l2ExecutionGas: bigint
    totalCost: bigint
  }>
}

// Optimism Gas 估算
class OptimismGasEstimator implements L2GasEstimator {
  chainId = 10

  async estimateGas(tx: TransactionRequest) {
    const l2Gas = await this.publicClient.estimateGas(tx)

    // Optimism 的 L1 数据成本
    const rlptx = encodeRlp(tx)
    const l1DataGas = await this.publicClient.request({
      method: 'eth_getL1GasUsed',
      params: [{ data: rlptx }],
    })

    const l1GasCost = BigInt(l1DataGas) * await this.getL1BaseFee()
    const l2GasCost = l2Gas * await this.getL2BaseFee()

    return {
      gasLimit: l2Gas,
      l1DataGas: BigInt(l1DataGas),
      l2ExecutionGas: l2Gas,
      totalCost: l1GasCost + l2GasCost,
    }
  }
}

// Arbitrum Gas 估算
class ArbitrumGasEstimator implements L2GasEstimator {
  chainId = 42161

  async estimateGas(tx: TransactionRequest) {
    // Arbitrum 通过 ArbOS 报告详细的 Gas 分解
    const result = await this.publicClient.request({
      method: 'eth_estimateGas',
      params: [tx],
    })

    // Arbitrum 的 gas price 包含 L1 和 L2 部分
    const gasPrice = await this.publicClient.getGasPrice()

    return {
      gasLimit: BigInt(result),
      l1DataGas: 0n, // Arbitrum 内部处理
      l2ExecutionGas: BigInt(result),
      totalCost: BigInt(result) * gasPrice,
    }
  }
}

最佳实践:Gas 费用监控与告警 ​

typescript
class GasMonitor {
  private threshold: bigint
  private interval: NodeJS.Timeout | null = null
  private callbacks: Set<(alert: GasAlert) => void> = new Set()

  constructor(private publicClient: PublicClient, thresholdGwei: number) {
    this.threshold = BigInt(thresholdGwei) * 1_000_000_000n
  }

  start(intervalMs: number = 60000) {
    this.interval = setInterval(async () => {
      const price = await this.publicClient.getGasPrice()
      if (price < this.threshold) {
        this.callbacks.forEach((cb) => cb({
          currentPrice: price,
          threshold: this.threshold,
          suggestion: 'Gas 价格低于阈值,适合发送交易',
        }))
      }
    }, intervalMs)
  }

  onLowGas(cb: (alert: GasAlert) => void) {
    this.callbacks.add(cb)
  }

  stop() {
    if (this.interval) clearInterval(this.interval)
  }
}

小结 ​

Gas 管理是 Web3 前端的核心体验环节。EIP-1559 的双费用模型让 Gas 估算更可预测,但前端仍需正确计算 maxFeePerGas 和 maxPriorityFeePerGas。Gas 估算失败的处理需要分类——revert、out of gas、nonce 冲突各有不同策略。Multicall 是最有效的 Gas 优化手段,能显著降低批量操作的成本。L2 的 Gas 机制与 L1 差异显著——Optimism 有 L1 数据成本,Arbitrum 有内部 Gas 折算,前端必须链感知地适配。Gas 预测 UI 应在签名前展示完整费用分解,让用户有明确的预期。Gas 监控和告警系统可以帮助用户在低 Gas 时段发送交易,优化交易成本。

MIT Licensed