'How to generate arbitrary wallet seeded with eth in hardhat tests using ethers.js?

I'm currently trying to run a test in hardhat/waffle that requires hundreds of unique wallets to call a contract, using new ethers.Wallet.createRandom(). However, none of these wallets are supplied with eth, so I can't call/send transactions with them.

What would be the simplest/most effective way to supply an arbitrary amount of randomly generated wallets with eth? Thanks!



Solution 1:[1]

I think you'll probably want to use the hardhat network method hardhat_setBalance, the docs use an example like this:

await network.provider.send("hardhat_setBalance", [
  "<ACCOUNT ADDRESS>",
  "0x1000", # 4096 wei
]);

I'm not developing in javascript though, but I've been doing something similar in python using web3.py and eth-account with code like this:

from web3 import Web3
from eth_account import Account


chain = Web3(HTTPProvider("http://127.0.0.1:8545"))

acct = Account.create('<RANDOM VALUE>')
address = Web3.toChecksumAddress(acct.address)
print(chain.eth.get_balance(address)) # 0 wei

# add a balance of eth tokens to the address
chain.provider.make_request("hardhat_setBalance", [address, "0x1000"])

print(chain.eth.get_balance(address)) # 4096 wei

Solution 2:[2]

saw this answer, and i think it solves the problem:

https://github.com/ethers-io/ethers.js/issues/686

const wallet = ethers.Wallet.createRandom().connect(provider);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jaymon
Solution 2 Ricardo Martins