我同时通过联系人发送一些交易。但是块执行其中一个,因为两个事务具有相同的随机数(我从 getTransactionCount 获得)。我该如何解决?
每次创建交易时,我都会使用此代码:
var token = web3.eth.contract(config.contract.abi).at(config.contract.address);
var payloadData = token.transfer.getData(address, amount);
gasPrice = web3.eth.gasPrice;
gasPriceHex = web3.toHex(gasPrice);
gasLimitHex = web3.toHex(gaslimit);
console.log('Current gasPrice: ' + gasPrice + ' OR ' + gasPriceHex);
nonce = web3.eth.getTransactionCount(config.account.address);
nonceHex = web3.toHex(nonce);
console.log('nonce (transaction count on fromAccount): ' + nonce + '(' + nonceHex + ')');
var rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
to: config.contract.address,
from: config.account.address,
value: '0x00',
data: payloadData
};
var tx = new Tx(rawTx);
tx.sign(Buffer.from(config.account.privateKey, 'hex'));
var serializedTx = tx.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
if (err) {
console.log(err);
console.log(hash);
}
});