You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Web3.js 4.0.3禁用MetaMask窗口中的站点建议手续费?

解决Web3.js 4.0.3中禁用MetaMask站点建议手续费的问题

在Web3.js 4.x版本中,EIP-1559交易参数的处理逻辑和1.x版本存在差异,直接传递null给maxPriorityFeePerGas和maxFeePerGas不再能实现禁用站点建议手续费的效果。以下是两种可行的解决办法:

方案1:使用Legacy交易模式(指定gasPrice)

如果不需要EIP-1559的动态手续费机制,可以直接指定gasPrice参数,同时不设置maxPriorityFeePerGas和maxFeePerGas。这种情况下MetaMask会使用你指定的gasPrice,不会显示站点建议的手续费选项:

const web3 = new Web3(window.ethereum);
const contract = new web3.eth.Contract(erc20ABI, contractAddress);
contract.methods
  .transfer(destinationAdrs, (depositAmt * 1000000).toString())
  .send({ 
    from: sourceAdrs, 
    gas: "21000", 
    gasPrice: await web3.eth.getGasPrice() // 也可手动指定固定gasPrice值
  })
  .then(function (receipt) {
    console.log(receipt);
  }).catch(function (error) {
    console.log(error);
  });

方案2:不指定EIP-1559参数(交由MetaMask完全处理手续费)

若想保留EIP-1559模式但禁用站点手续费建议,只需移除maxPriorityFeePerGas和maxFeePerGas的设置(或设置为undefined)。此时Web3.js不会自动估算手续费,而是完全交由MetaMask处理,MetaMask将不再显示站点建议的手续费:

const web3 = new Web3(window.ethereum);
const contract = new web3.eth.Contract(erc20ABI, contractAddress);
contract.methods
  .transfer(destinationAdrs, (depositAmt * 1000000).toString())
  .send({ 
    from: sourceAdrs, 
    gas: "21000"
    // 移除maxPriorityFeePerGas和maxFeePerGas参数
  })
  .then(function (receipt) {
    console.log(receipt);
  }).catch(function (error) {
    console.log(error);
  });

关键说明

Web3.js 4.x中,若传递null给EIP-1559相关参数,库会自动触发手续费估算逻辑,这就是站点建议手续费重新出现的原因。另外要确保gas值设置合理,避免因gas不足导致交易失败。

内容的提问来源于stack exchange,提问作者Abhilash Reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 14:15:14