Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ethereum - sign and send raw transaction in batches using infura api
var ethers = require('ethers');
var apiKey = '#yourAPIKey#'; //github is a good source for this :p
var url = `https://goerli.infura.io/v3/${apiKey}`;
var rpc = new ethers.providers.JsonRpcProvider(url);
var privateKey = "#ethPrivateKey#";
var wallet = new ethers.Wallet(privateKey);
console.log("Address: " + wallet.address);
async function main(){
let transactions = [];
let tx = {
to: "0x60C063d3f3B744e2d153FcbE66A068B09109cf1b", //null for contract creation
value: ethers.utils.parseEther("0.0001"),
gasLimit: 21000, //72000 for contract creation
//gasPrice: "0x07f9acf02",
//gasPrice: 151,//12*10**6,
nonce: 0+1, // next account nonce (check with etherscan)
chainId: 5, //goerli
data: "0xbaadf00d",
gasPrice: ethers.utils.parseEther("0.0000001"),
//type:2,
//maxFeePerGas: 9,
//maxPriorityFeePerGas: 9,
}
let start = tx.nonce
let num = 1 //
console.log("--start--")
for(let i = start; i<start+num; i++){
tx.nonce = i
transactions.push(await wallet.signTransaction(tx))
console.log(transactions[transactions.length -1])
}
let ii = 0;
for(let t of transactions){
rpc.sendTransaction(t)
ii+=1
console.log(ii)
}
console.log("--end--")
/* another way of sending all transactions. doing this twice to make sure all tx go through :p */
await Promise.allSettled(transactions.map( tsx => rpc.sendTransaction(tsx)))
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment