英文:
ETH sendSignedTransaction
问题
我尝试使用@ethereumjs/tx来签名交易。
我想从一个地址发送ETH到第二个地址。
....
const legacy = require('@ethereumjs/tx/dist/legacyTransaction');
const utils = require('web3-utils');
const { Personal } = require('web3-eth-personal');
....
// https://docs.chainstack.com/reference/ethereum-sendrawtransaction
const from = request.body.from;
const to = request.body.to;
const amount = '0.01';
const passphrase = request.body.passphrase;
const private_key = getPrivateKey(from, passphrase);
// 定义gas限制
const gasLimit = await new Web3Eth(local_provider).estimateGas({
from: from,
to: to,
});
// 获取发送者地址的交易计数
const nonce = await new Web3Eth(local_provider).getTransactionCount(from);
// 定义交易对象
const txData = {
from,
nonce: nonce,
to,
gasLimit: 21000,
maxPriorityFeePerGas: utils.toHex(utils.toWei("5", "gwei")),
maxFeePerGas: utils.toHex(utils.toWei("20", "gwei")),
value: utils.toHex(utils.toWei('0.01', 'ether')),
};
const tx = legacy.Transaction.fromTxData(txData);
const signedTx = tx.sign(private_key);
const serializedTx = signedTx.serialize();
const rawTransaction = '0x' + serializedTx.toString('hex');
try {
console.log(`正在发送交易...`);
const result = await new Web3Eth(provider).sendSignedTransaction(rawTransaction);
console.log(`交易哈希: ${result.transactionHash}`);
} catch (error) {
console.error(error);
}
错误信息:
原因: 'err: 燃料 * 价格 + 值的资金不足: 地址0x2f1030396172F3e3d308819833fBbA655A9785a3拥有19175306697701238,需要16737889130546396655769049807908504612912(提供的gas 21000)',
当我更改为以下内容时:
var tet = utils.toWei('0.01', 'ether');
const txData = {
from,
nonce: nonce,
to,
gasPrice: "0x" + parseInt(18000000000, 10).toString(16),
gasLimit: "0x" + parseInt(90000, 10).toString(16),
value: "0x" + parseInt(tet, 10).toString(16),
};
它完美运行!
我的愿望是不设置燃料费和价格。
我希望它能自动完成。
但是如果不注入燃料,它将无法工作。
英文:
I Try to use @ethereumjs/tx to sign Transaction.
I want to send ETH from one address to second address.
....
const legacy = require('@ethereumjs/tx/dist/legacyTransaction');
const utils = require('web3-utils');
const {Personal} = require('web3-eth-personal');
....
// https://docs.chainstack.com/reference/ethereum-sendrawtransaction
const from = request.body.from;
const to = request.body.to;
const amount = '0.01';
const passphrase = request.body.passphrase;
const private_key = getPrivateKey(from, passphrase);
// Define the gas limit
const gasLimit = await new Web3Eth(local_provider).estimateGas({
from: from,
to: to,
});
// Get the transaction count for the sender address
const nonce = await new Web3Eth(local_provider).getTransactionCount(from);
// Define the transaction object
const txData = {
from,
nonce: nonce,
to,
gasLimit: 21000,
maxPriorityFeePerGas: utils.toHex(utils.toWei("5", "gwei")),
maxFeePerGas: utils.toHex(utils.toWei("20", "gwei")),
value: utils.toHex(utils.toWei('0.01', 'ether')),
};
const tx = legacy.Transaction.fromTxData(txData);
const signedTx = tx.sign(private_key);
const serializedTx = signedTx.serialize();
const rawTransaction = '0x' + serializedTx.toString('hex');
try {
console.log(`Sending transaction...`);
const result = await new Web3Eth(provider).sendSignedTransaction(rawTransaction);
console.log(`Transaction hash: ${result.transactionHash}`);
} catch (error) {
console.error(error);
}
> reason: 'err: insufficient funds for gas * price + value: address 0x2f1030396172F3e3d308819833fBbA655A9785a3 have 19175306697701238 want 16737889130546396655769049807908504612912 (supplied gas 21000)',
when I change to:
var tet = utils.toWei('0.01', 'ether');
const txData = {
from,
nonce: nonce,
to,
gasPrice: "0x" + parseInt(18000000000, 10).toString(16),
gasLimit: "0x" + parseInt(90000, 10).toString(16),
value: "0x" + parseInt(tet, 10).toString(16),
};
It's works Perfect!
My wish is not to set the gas fees and prices.
I wish to make it auto.
but without inject the gas, it will not work.
答案1
得分: 1
当您提交交易而没有指定燃气价格时,交易可能不会被执行或可能会遇到显著的延迟。因为矿工会优先处理交易。具有更高燃气价格的交易更有可能被包含在下一个区块中,因为矿工可以赚更多的钱。
燃气价格不是固定的。如果以太坊网络非常繁忙,燃气价格可能会非常高。如果您希望燃气价格自动设置,那么当价格很高时,您将不知不觉地支付很多钱。
英文:
When you submit a transaction without specifying the gas price, the transaction may not get executed or may experience significant delays. Because miners prioritize the transactions. Transactions with higher gas prices are more likely to be included in the next block becuase miners will make more money.
gas price is not constant. If the Ethereum network is very busy, gas prices might go very high. If you want gas price to be auto, then when the price is high, you will be paying so much money, unwittingly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论