Search code examples
ethereumsolidityethers.jshardhat

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

  • Hardhat allows to seed a custom amount of prefunded addresses using the accounts.count config option of the built-in hardhat network.

    Example:

    module.exports = {
        networks: {
            hardhat: {
                accounts: {
                    count: 1000
                }
            }
        }
    }
    

    Docs: https://hardhat.org/hardhat-network/reference/#config


    The new ethers.Wallet.createRandom() function only creates an account from a random private key in the context of the JS application, but it doesn't communicate with the provider (Hardhat in your case).