在以太坊交易中,maxFeePerGas 是所愿意支付的最高 gas 费用,而 maxPriorityFeePerGas 则是用于赞助优先 gas 的费用。在使用 Alchemy API 时,当将 maxFeePerGas 设定为一个小于 maxPriorityFeePerGas 的数值时,会出现上述错误。因此,需要确保 maxFeePerGas 大于或等于 maxPriorityFeePerGas。
示例代码:
// 设置 gas fee 相关参数 let maxPriorityFeePerGas = 5; let maxFeePerGas = 10;
// 检查参数是否合法 if (maxFeePerGas < maxPriorityFeePerGas) { console.log("Error: maxFeePerGas cannot be less than maxPriorityFeePerGas"); return; }
// 使用 Alchemy API 发起交易 web3.eth.sendTransaction({ from: '0x123456...', to: '0xabcdef...', value: web3.utils.toWei('1', 'ether'), gas: 21000, maxFeePerGas: web3.utils.toWei(maxFeePerGas.toString(), 'gwei'), maxPriorityFeePerGas: web3.utils.toWei(maxPriorityFeePerGas.toString(), 'gwei') }) .then((receipt) => { console.log(receipt); }) .catch((error) => { console.log(error); });