我正在尝试用一个用js编写的程序向Ethereum发送大量事务。我使用NodeJS,web3和infura ropsten。问题是:如果我立即发送交易,其中大部分都会消失。试图解决这个问题,我发送间隔的交易。它工作,但非常缓慢。我花了大约一个小时只发送100笔交易。有没有解决方案让它更快更好地工作?我考虑在前一个开始挂起后发送交易,但我不知道我该怎么做。函数sendRaw仅在一段时间后获取事务编号。此代码读取文件,获取地址,数量和可选数据,并使用智能合约的方法来转移令牌。 这是代码:
function sendRaw(rawTx) {
var privateKey = new Buffer(key, 'hex');
var transaction = new tx(rawTx);
transaction.sign(privateKey);
var serializedTx = transaction.serialize().toString('hex');
web3.eth.sendRawTransaction(
'0x' + serializedTx, function(err, result) {
if(err) {
console.log(err);
} else {
console.log(result);
Ntrans=result;
}
});
}
var nonce = web3.eth.getTransactionCount(address);
var gasPrice = web3.eth.gasPrice;
var gasLimit = 90000;
var fs = require("fs");
var buf1 = new Buffer(1024);
var buf2 = new Buffer(1024);
var buf3 = new Buffer(1024);
var i = 0;
var j = 0;
var k = 0;
var fd = fs.openSync('/home/kate/Desktop/file.txt', 'r+');
function recurs()
{
if(k==5) return -1;
j = 0;
do
{
fs.readSync(fd, buf1, j, 1, i);
i++;
j++;
}
while(buf1[j-1] != 32);
AddressC = String(buf1.slice(0, j-1))
console.log(AddressC);
j = 0;
do
{
fs.readSync(fd, buf2, j, 1, i)
i++;
j++;
}
while(buf2[j-1]!=32);
ValueT = Number(buf2.slice(0, j-1))
console.log(ValueT);
j = 0;
do
{
fs.readSync(fd, buf3, j, 1, i);
i++;
j++;
}
while(buf3[j-1]!=10);
TxC = String(buf3.slice(0, j-1));
txOptions =
{
nonce: web3.toHex(nonce),
gasLimit: web3.toHex(gasLimit),
gasPrice: web3.toHex(gasPrice),
to: contractAddress
}
console.log(TxC);
console.log(txOptions);
rawTx = txutils.functionTx(interface, 'foreignBuy', [AddressC,
ValueT, TxC], txOptions);
sendRaw(rawTx);
k++;
nonce++;
/* while(web3.eth.getTransactionReceipt(Ntrans)=="null")
{
} */
}
setTimeout(function(){
recurs();
}, 5000);
}
recurs();