Skip to content

Instantly share code, notes, and snippets.

@AyushyaChitransh
Last active July 23, 2018 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AyushyaChitransh/dca2cfc85755b20e373aa18b1648f9e1 to your computer and use it in GitHub Desktop.
Save AyushyaChitransh/dca2cfc85755b20e373aa18b1648f9e1 to your computer and use it in GitHub Desktop.
Send many transactions rapidly from an account
var Tx = require('ethereumjs-tx');
var Web3 = require('web3');
var util = require('ethereumjs-util');
var stripHexPrefix= require('strip-hex-prefix');
// connect to node
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
function getTransactionReceiptMined(txHash, interval,callback) {
const self = web3.eth;
const transactionReceiptAsync = function(resolve, reject) {
self.getTransactionReceipt(txHash, (error, receipt) => {
if (error) {
reject(error);
} else if (receipt == null) {
setTimeout(
() => transactionReceiptAsync(resolve, reject),
interval ? interval : 500);
} else {
if(stripHexPrefix(receipt.status) === '1'){
return callback({
receipt: receipt,
txStatus: 1
});
}
else{
return callback({
txStatus: 0
})
}
}
});
};
if (Array.isArray(txHash)) {
return Promise.all(txHash.map(
oneTxHash => self.getTransactionReceiptMined(oneTxHash, interval)));
} else if (typeof txHash === "string") {
return new Promise(transactionReceiptAsync);
} else {
throw new Error("Invalid Type: " + txHash);
}
};
const addressFrom = 'address'
var key = 'secret_key';
var privateKey = new Buffer(key, 'hex');
const addressTo = 'address'
var count = web3.eth.getTransactionCount(addressFrom);
console.log(count);
var j = 1;
for(let i = 0; i < 500; i++){
var rawTx = {
nonce: web3.toHex(count+i),
gasLimit: web3.toHex(51334),
gasPrice: web3.toHex(10),
to: addressTo,
from: addressFrom,
value: 0x1,
data: 0x0
}
console.log("nonce: ",count+i)
var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
// console.log(serializedTx);
// console.log(`Attempting to send signed tx: ${serializedTx.toString('hex')}\n------------------------`);
// var receipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));
// console.log(`Receipt info: \n${JSON.stringify(receipt, null, '\t')}\n------------------------`);
// try{
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
// console.log("hash:",hash)
if (!err){
getTransactionReceiptMined(hash,10,function(txHash){
if(txHash.txStatus === 0){
console.log("exception thrown by contract",j)
j++;
}
if(txHash.txStatus === 1){
console.log('Done: ',j);
j++;
}
})
}
else{
console.log("error",err)
}
/*var receipt=web3.eth.getTransactionReceipt(trHash);
if(receipt ! = 1){
var receipt=web3.eth.getTransactionReceipt(trHash);
}
else{
console.log("Transaction mined");
}*/
});
// }
// catch(exception e){
// console.log(e)
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment