调用合同方法并手动签名时出错。 SendTransaction的作品SendRawTransaction没有
美好的一天,调用合同方法并手动签名时出错。 SendTransaction的作品SendRawTransaction没有
我正在写一个节点API来暴露我的区块链上的方法(部署和测试松露)。我使用web3.js,ethereumjs-tx,ethereum,松露和坚实作为我的技术堆栈。
var txMethodData = masterKeyContract.myMethod.getData(myParams);
交易PARAMS是:
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
};
IM使用ethereumjs-TX
const EthereumTx = require('ethereumjs-tx');
与链接到我的mainAccount的私有密钥签名的交易
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);
}
});
而且我得到了错误天然气*价格+价值的资金不足。我从mainAccount(txParams的from:字段)发送此事务。所以我登录我的mainAccount余额
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区块的天然气限制是4,707,806。所以我应该有足够的。所以问题在于为什么我的资金不足。
我怀疑的唯一原因是from:field,这是我的mainAccount实际上并不是交易的付款人。
UPDATE: 问题可能是与签约,因为我只是
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');
实际上与我mainAccount。 private_key_inserted_here从“密文”字段中从与我的主帐户相关的密钥库中获取。我通过匹配密钥库的“地址”字段来检查与我的mainAccount相关的信息。
在此先感谢。
有些事情,我可以看到
- 这是
gas
,而不是不需要gasLimit
chainId
- 你真的应该管理你自己的
nonce
而是依靠节点送你正确的随机数。换句话说,不从节点查询,但保持周围
一个变量,如果你愿意,你可以看看简约的钱包签名类我写 https://gist.github.com/gaiazov/e65a3e36c67eaf46fe81982cd193316d
我会去看一下的,而不是气体气体限制的确是一个错误。感谢您将它指向我。 – andu