Skip to content

Instantly share code, notes, and snippets.

@justinschuldt
Created March 19, 2022 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinschuldt/21895bc5d7bf668de2fbec065dc4e9de to your computer and use it in GitHub Desktop.
Save justinschuldt/21895bc5d7bf668de2fbec065dc4e9de to your computer and use it in GitHub Desktop.
import { ethers } from "ethers";
const rpc = 'http://localhost:8545'
const senderAddress = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"
const senderPrivateKey = "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d" // ganche default
const addresses = [
"0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d",
"0xd03ea8624C8C5987235048901fB614fDcA89b117",
"0x95cED938F7991cd0dFcb48F0a06a40FA1aF46EBC",
"0x3E5e9111Ae8eB78Fe1CC3bb8915d5D461F3Ef9A9",
"0x28a8746e75304c0780E011BEd21C72cD78cd535E"]
const amount = "2"
function funder(
provider: ethers.providers.JsonRpcProvider,
signer: ethers.Wallet,
senderAddress: string,
amount: string,
) {
return async function sendToAddress(recipientAddress: string) {
const currentGasPrice = await provider.getGasPrice()
const gas_price = ethers.utils.hexlify(currentGasPrice)
const tx = {
from: senderAddress,
to: recipientAddress,
value: ethers.utils.parseEther(amount),
nonce: await provider.getTransactionCount(senderAddress, "latest"),
gasLimit: ethers.utils.hexlify(1000000),
gasPrice: gas_price,
}
try {
const transaction = await signer.sendTransaction(tx)
const { hash } = transaction
return hash
} catch (e) {
console.error('failed sending: ', e)
throw e
}
}
}
// use a generator to send transaction serially,
// since they are all coming from same wallet, need nonces to increment.
async function* fundAccounts(sender: (addr: string) => Promise<ethers.providers.TransactionResponse['hash']>,
recipients: Array<string>) {
for (const recipient of recipients) {
yield await sender(recipient);
}
};
async function main(
rpcUrl: string,
senderAddress: string,
senderPrivKey: string,
recipientAddresses: Array<string>,
amountToSend: string
) {
let provider = new ethers.providers.JsonRpcProvider(rpcUrl)
let signer = new ethers.Wallet(senderPrivKey, provider)
const sendTo = funder(provider, signer, senderAddress, amountToSend)
try {
for await (const hash of fundAccounts(sendTo, recipientAddresses)) {
console.log(`sent: http://localhost:3302/tx/${hash}`)
}
} catch (err) {
console.log("failed. error:", err)
}
console.log("success!")
}
main(rpc, senderAddress, senderPrivateKey, addresses, amount)
{
"version": "0.2.0",
"configurations": [
{
"name": "ethers-fund-accounts.ts",
"type": "node",
"request": "launch",
"args": [
"${workspaceFolder}/ethers-fund-accounts.ts",
],
"runtimeArgs": [
"--nolazy",
"-r",
"${workspaceRoot}/node_modules/ts-node/register"
],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector",
},
]
}
{
"name": "ethers-fund-accounts",
"dependencies": {
"ethers": "5.4.4",
},
"scripts": {
"fund-accounts": "ts-node ethers-fund-accounts.ts"
},
"devDependencies": {
"ts-node": "^10.7.0",
"typescript": "^4.3.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment