我想使用 web3 签署 bep20 令牌,但我收到此错误
gas资金不足*价格+价值:地址____有4960958883943891想要69411784877494906285795002945479676105(提供gas 21000)
const Tx = require('ethereumjs-tx').Transaction
const { Web3 } = require('web3');
const newLocal = 'https://bsc-dataseed1.binance.org:443'
const web3 = new Web3(new Web3.providers.HttpProvider(newLocal));
const txObject = {
nonce:web3.utils.toHex(Number(txCount)),
to:addressTo,
value:web3.utils.toHex(web3.utils.toWei(balancetoSendToEther,'ether')),
gasLimit:web3.utils.toHex(21000),
gasPrice:web3.utils.toHex(web3.utils.toWei(gasPriceToEther,'gwei')),
maxPriorityFeePerGas: web3.utils.toWei("3", "gwei"),
maxFeePerGas: web3.utils.toWei("3", "gwei"),
common: {
customChain: {
name: 'custom-chain',
chainId: 56,
networkId: 56
}
}
}
// Sign the transaction
const tx = new Tx(txObject)
tx.sign(_private_key)
const serializedTransaction = tx.serialize()
const raw = '0x'+ serializedTransaction.toString('hex')
为什么gasprice那么高=> 69411784877494906285795002945479676105
我最终通过将web3版本更改为1.7.5来降低gasprice。这是代码
async function send_funds(balancetoSend) {
const signedTx = await web3.eth.accounts.signTransaction({
to: addressTo,
value: '1000000000000000',
gas: 21000,
common: {
customChain: {
name: 'custom-chain',
chainId: 56,
networkId: 56
}
} }, private_key);
web3.eth.sendSignedTransaction(signedTx.rawTransaction, function (error, hash) {
if (!error) {
console.log("🎉 The hash of your transaction is: ", hash);
} else {
console.log("❗Something went wrong while submitting your transaction:",
error)
} }); }