以太坊 Web3.js:内在气体太低



过去几天我一直在尝试在以太坊测试网 Rinkeby 上发送交易,无论我增加多少 gas 都会不断收到此错误。

"未处理的剔除错误:返回的错误:本征气体过低">

我发送的数据是:

"0x7b22416e7377657273223a5b7b225175657374696f6e223a2231222c22416e73776572223a2234227d2c7b225175657374696f6e223a2232222c22416e73776572223a2234227d2c7b225175657374696f6e223a2233222c22416e73776572223a2234227d2c7b225175657374696f6e223a2234222c22416e73776572223a2234227d2c7b225175657374696f6e223a2235222c22416e73776572223a2234227d2c7b225175657374696f6e223a2236222c22416e73776572223a2234227d5d7d"

转换为十六进制后。

我在下面添加了我的代码。

var number = web3.eth.getTransactionCount(address).then(function(count) {
console.log("Count " + count);
var privateKey = new EthJS.Buffer.Buffer(privateKey, 'hex');
console.log(web3.utils.toHex(finalAnswers));
var rawTx = {
nonce: web3.utils.toHex(count),
to: '0xF1aA87F7058e5ABE561cCe8A466eE1CC17d69639',
value: 0,
data: web3.utils.toHex(finalAnswers),
gas: 50000,
gasPrice: web3.utils.toWei('300', 'gwei')
};
var tx = new EthJS.Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', console.log);
});

您收到此错误的原因是gasPrice高于gasLimit。您需要通过设置更高的gasLimit来分配更多的气体。

正如@AdamKipris评论中建议的那样,在构建交易时,您应该使用gasLimit:而不是gas:

就我而言,某些方法必须手动下注设置气体限制,因此我切换到交易的默认气体限制。在煎饼交换参考中,它是 20,000 个单位 https://github.com/pancakeswap/pancake-frontend/blob/master/src/config/index.ts#L31 不过,您可以根据gas调整道具

代码如下所示:

const txReceipt = await callWithGasPrice(
wethContract,
"deposit",
undefined,
{
value: `0x${inputAmount.raw.toString(16)}`,
gasPrice,
gasLimit: DEFAULT_GAS_LIMIT,
}
);

最新更新