Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 910
fullyallocated Avatar asked Nov 15 '25 03:11

fullyallocated


1 Answers

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
like image 127
Jaymon Avatar answered Nov 17 '25 20:11

Jaymon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!