Node.js 调用合同方法并手动签名时出错。SendTransaction工作SendRawTransaction不工作';T
你好 我正在编写一个节点api来公开我的区块链上的方法(使用块菌部署和测试)。我使用web3.js、ethereumjstx、ethereum、truffle和solidity作为我的技术堆栈Node.js 调用合同方法并手动签名时出错。SendTransaction工作SendRawTransaction不工作';T,node.js,ethereum,solidity,web3js,web3,Node.js,Ethereum,Solidity,Web3js,Web3,你好 我正在编写一个节点api来公开我的区块链上的方法(使用块菌部署和测试)。我使用web3.js、ethereumjstx、ethereum、truffle和solidity作为我的技术堆栈 var txMethodData = masterKeyContract.myMethod.getData(myParams); 事务参数为: const txParams = { nonce: web3.toHex(web3.eth.getTransactionCount(web3.eth.
var txMethodData = masterKeyContract.myMethod.getData(myParams);
事务参数为:
const txParams = {
nonce: web3.toHex(web3.eth.getTransactionCount(web3.eth.coinbase)),
gasPrice: web3.toHex(web3.eth.gasPrice),
gasLimit: web3.toHex(2000000),
from: mainAccount,
value: '0x00',
to: targetContract.address,
data: txMethodData,
chainId: 3
};
我正在使用以太坊
const EthereumTx = require('ethereumjs-tx');
使用链接到我的主帐户的私钥签署交易
const tx = new EthereumTx(txParams);
tx.sign(privateKey);
const serializedTx = tx.serialize();
web3.eth.sendRawTransaction("0x" + serializedTx.toString('hex'), function (err1, resp1) {
if (err1) {
console.log(err1);
} else {
console.log(resp1);
}
});
web3.eth.getBalance(mainAccount, function (error, result) {
if (!error) {
console.log(web3.fromWei(result.toNumber(), "ether"));
} else {
console.error(error);
}
});
我得到了一个错误:汽油的资金不足*价格+价值。我正在从Main帐户发送此事务(txParams中的from:字段)。所以我把余额记在了我的主账户上
const tx = new EthereumTx(txParams);
tx.sign(privateKey);
const serializedTx = tx.serialize();
web3.eth.sendRawTransaction("0x" + serializedTx.toString('hex'), function (err1, resp1) {
if (err1) {
console.log(err1);
} else {
console.log(resp1);
}
});
web3.eth.getBalance(mainAccount, function (error, result) {
if (!error) {
console.log(web3.fromWei(result.toNumber(), "ether"));
} else {
console.error(error);
}
});
结果为252.12609391539726。因此,没有资金是不可能的。我甚至估计了web3.eth.estimateGas(txParams)交易,它给了我97899。当前ropstein区块的天然气限值为4707806。所以我应该吃够了。所以问题仍然是为什么我没有得到足够的资金
我怀疑的唯一原因是from:字段(我的主帐户)实际上不是交易的付款人
更新:
问题可能在于签名,因为我刚刚用
web3.eth.sendTransaction(txParams, function (err1, resp1) {
if (err1) {
console.log(err1);
} else {
console.log(resp1);
}
});
所以问题是为什么sendRawTransaction不起作用。这可能与我签署交易的方式有关吗
我检查了一下
const privateKey = Buffer.from('[private_key_inserted_here]', 'hex');
实际上与我的主帐户有关。此处插入的私钥取自“密文”字段中与我的主帐户相关的密钥库。我通过匹配密钥库的“地址”字段来检查它是否与我的主帐户相关
提前谢谢。我能看到一些东西
- 它是
而不是gas
gasLimit
- 不需要链ID
- 您应该自己管理
,而不是依赖节点向您发送正确的nonce。换句话说,不要从节点查询它,而是保留一个变量nonce
我会看一看,气体而不是气体限制确实是一个错误。谢谢你把它指给我看。