Skip to content

Commit 27eab38

Browse files
committed
Hardhat task to ensure accounts ether balances
We add a hardhat task that can be used to ensure addresses are funded with a minimum value of ether. This will be used in test environments setup to ensure when a client starts the operator has enough funds to run. Sample command: ``` npx hardhat ensure-eth-balance "0.5 ether" 0x6299496199d99941193Fdd2d717ef585F431eA05 0x93df7c54c41A9D7FB17C1E8039d387a2A924708c ```
1 parent bfcc9bd commit 27eab38

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { task, types } from "hardhat/config"
2+
3+
import { parseValue, TASK_SEND_ETH } from "./send-eth"
4+
5+
export const TASK_ENSURE_ETH_BALANCE = "ensure-eth-balance"
6+
7+
task(
8+
TASK_ENSURE_ETH_BALANCE,
9+
"Ensure addresses hold a minimum ether balance, top-up if needed"
10+
)
11+
.addOptionalParam(
12+
"from",
13+
"Address to send value from",
14+
undefined,
15+
types.string
16+
)
17+
.addPositionalParam(
18+
"balance",
19+
'Minimum ether balance the addresses are expected to hold, e.g. "0.5 ether", "100 gwei"',
20+
undefined,
21+
types.string
22+
)
23+
.addVariadicPositionalParam(
24+
"addresses",
25+
"Addresses for which balance should be validated",
26+
undefined,
27+
types.string
28+
)
29+
.setAction(async (args, hre) => {
30+
const { ethers } = hre
31+
32+
// FIXME: `validate` will fail for badly checksummed addresses
33+
// see: https://github.com/ethers-io/ethers.js/discussions/3261
34+
const addresses: Set<string> = new Set(
35+
Array.from(args.addresses).map(hre.helpers.address.validate)
36+
)
37+
38+
for (let address of addresses) {
39+
const expectedBalance = parseValue(args.balance, hre)
40+
const currentBalance = await ethers.provider.getBalance(address)
41+
42+
console.log(
43+
`current balance of ${address} is ${ethers.utils.formatEther(
44+
currentBalance
45+
)} ether`
46+
)
47+
48+
if (currentBalance.lt(expectedBalance)) {
49+
const topUpAmount = expectedBalance.sub(currentBalance)
50+
51+
await hre.run(TASK_SEND_ETH, {
52+
from: args.from,
53+
value: topUpAmount.toString(),
54+
to: address,
55+
})
56+
}
57+
}
58+
})

solidity/random-beacon/tasks/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
require("./unlock-eth-accounts")
33
require("./initialize")
44
require("./genesis")
5+
require("./send-eth")
6+
require("./ensure-eth-balance")
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { BigNumber, Signer } from "ethers"
2+
import { task, types } from "hardhat/config"
3+
import { HardhatRuntimeEnvironment } from "hardhat/types"
4+
5+
import type { TransactionResponse } from "@ethersproject/abstract-provider"
6+
7+
export const TASK_SEND_ETH = "send-eth"
8+
9+
task(TASK_SEND_ETH, "Send ether to an address")
10+
.addOptionalParam(
11+
"from",
12+
"Address to send value from",
13+
undefined,
14+
types.string
15+
)
16+
.addParam(
17+
"value",
18+
'Value to transfer with unit, e.g. "0.5 ether", "100 gwei"',
19+
undefined,
20+
types.string
21+
)
22+
.addParam("to", "Transfer receiver address", undefined, types.string)
23+
.setAction(async (args, hre) => {
24+
const from: Signer = args.from
25+
? await hre.ethers.getSigner(args.from)
26+
: (await hre.ethers.getSigners())[0]
27+
28+
const value: BigNumber = parseValue(args.value, hre)
29+
30+
// FIXME: `validate` will fail for badly checksummed addresses
31+
// see: https://github.com/ethers-io/ethers.js/discussions/3261
32+
const to = hre.helpers.address.validate(args.to)
33+
34+
const tx: TransactionResponse = await from.sendTransaction({
35+
to: to,
36+
value: value,
37+
})
38+
39+
console.log(
40+
`sending ${value} wei from ${await from.getAddress()} to ${to} in tx ${
41+
tx.hash
42+
}...`
43+
)
44+
45+
await tx.wait()
46+
})
47+
48+
export function parseValue(
49+
value: string,
50+
hre: HardhatRuntimeEnvironment
51+
): BigNumber {
52+
const parsed = String(value).trim().split(" ")
53+
54+
if (parsed.length == 0 || parsed.length > 2) {
55+
throw new Error(`invalid value: ${value}`)
56+
}
57+
58+
return hre.ethers.utils.parseUnits(parsed[0], parsed[1] || "wei")
59+
}

solidity/random-beacon/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
],
1111
"compilerOptions": {
1212
"esModuleInterop": true,
13-
"noImplicitAny": true
13+
"noImplicitAny": true,
14+
"downlevelIteration": true
1415
}
1516
}

0 commit comments

Comments
 (0)