Skip to content

Instantly share code, notes, and snippets.

@Rami-Majdoub
Created September 13, 2022 12:39
Show Gist options
  • Select an option

  • Save Rami-Majdoub/061a561ef7dbe740dd16d8e4c3b06725 to your computer and use it in GitHub Desktop.

Select an option

Save Rami-Majdoub/061a561ef7dbe740dd16d8e4c3b06725 to your computer and use it in GitHub Desktop.
send multiple transaction at once and wait for them to be mined

run script

npx hardhat run scripts/hardhat-send-multiple-txs.ts --network goerli

result

Sending funds
Tx sent: nonce: 11 | hash: 0xcc25e7c4ea0aec0726f28ae0bbdfd438cbcb9a78b4571610219171b500533ea8
Tx sent: nonce: 12 | hash: 0x5851f016b17772c479df1adb98406a1b3bb7a8f44f6418aec9b5d7c0895217ff
Tx confirmed: hash: 0x5851f016b17772c479df1adb98406a1b3bb7a8f44f6418aec9b5d7c0895217ff
Tx confirmed: hash: 0xcc25e7c4ea0aec0726f28ae0bbdfd438cbcb9a78b4571610219171b500533ea8
import { TransactionResponse } from "@ethersproject/providers";
import { Signer } from "ethers";
import { ethers } from "hardhat";
/*
const nonce0 = await signer.getTransactionCount();
await Promise.all(argsArray.map(
async (args: Type, index: number) => {
// TODO: send transaction here with nonce = nonce0 + index
}
));
*/
async function sendFunds(signer: Signer, receivers: string[]) {
console.log("Sending funds");
const nonce0 = await signer.getTransactionCount();
// send multiple transactions
await Promise.all(receivers.map(
async (receiver: string, index: number) => {
// create transaction
// const tx: TransactionResponse = await contract.method(args, {nonce: nonce0 + index});
const tx: TransactionResponse = await signer.sendTransaction({
to: receiver,
value: ethers.utils.parseEther("0.1"),
nonce: nonce0 + index,
});
console.log(`Tx sent: nonce: ${tx.nonce} | hash: ${tx.hash}`);
// wait for confirmation
const minedTx = await tx.wait()
console.log(`Tx confirmed: hash: ${minedTx.transactionHash}`);
}
));
}
async function main() {
const signers = await ethers.getSigners()
// the 2nd account will send the 1st and the 3rd accounts 0.1 ETH
await sendFunds(signers[1], [
signers[0].address,
signers[2].address
]);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment