-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfaucet.ts
54 lines (46 loc) · 1.77 KB
/
faucet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { task } from "hardhat/config";
import _ from "lodash";
import * as _path from "path";
import { FromArgType, PluginError } from "../helpers";
import "../type-extensions";
export const TASK_FAUCET = "faucet";
task(TASK_FAUCET, "Send some coins to other accounts")
.addOptionalParam("from", "Sender address or index", 0, FromArgType)
.addOptionalParam("to", "A comma-separated string of recipient addresses and indices. By default all signers", "")
.addOptionalParam("amount", "Amount of coins to send in ETH", "1")
.setAction(async (taskArgs) => {
const { from, to, amount } = taskArgs;
const sender = await hre.ethers.getSigner(from);
const value = hre.ethers.utils.parseEther(amount);
const recipients: string[] = [];
if (to == "") {
const allSigners = await hre.ethers.getSigners();
for (const signer of allSigners) {
recipients.push(await signer.getAddress());
}
} else {
for (const token of _.split(to, ",")) {
if (hre.ethers.utils.isAddress(token)) { // address
recipients.push(token);
} else if (/^\d+$/.test(token)) { // index
const signer = await hre.ethers.getSigner(_.toNumber(token) as any);
recipients.push(await signer.getAddress());
} else {
throw PluginError(`Not an address or index: ${token}`);
}
}
}
console.log(`Send from ${sender.address} to ${recipients.length} accounts ${amount} ETH each`);
const txs = [];
for (const recipient of recipients) {
const tx = await sender.sendTransaction({
to: recipient,
value: value
});
txs.push(tx.wait());
}
const rcs = await Promise.all(txs);
for (const rc of rcs) {
console.log(`to ${rc.to} txid ${rc.transactionHash}`);
}
});