8

I'm using Hardhat with the waffle plugin to test my smart contract.

To get Signers (ether.js) I'm using :

await ethers.getSigners();

This return an array with 20 signers. If I call again, I get the same 20 signers, but how do I get a new one ?

3 Answers 3

11

Using ethers.Wallet.createRandom() from François Guyot this is how I managed to get many Signers for my tests.

ctx is your contract instance and addr1 is one of the Signer you got by calling await ethers.getSigners();

            for( let i=0; i < nb; i++){
                // Get a new wallet
                wallet = ethers.Wallet.createRandom();
                // add the provider from Hardhat
                wallet =  wallet.connect(ethers.provider);
                // send ETH to the new wallet so it can perform a tx
                await addr1.sendTransaction({to: wallet.address, value: ethers.utils.parseEther("1")});
                
                await ctx.connect(wallet).CallYouContract())
            }
7

You could generate a bunch of random signers by doing this:

import { ethers } from 'ethers'

export const randomSigners = (amount: number): Signer[] => {
  const signers: Signer[] = []
  for (let i = 0; i < amount; i++) {
    signers.push(ethers.Wallet.createRandom())
  }
  return signers
}
1
  • 1
    Thank you François for your help. To make it work with Hardhat and Waffle I had to add a provider but this helped me to go in the right direction. Mar 10, 2022 at 15:51
1

If you're using hardhat, the default number of account (20) can be changed in the config:

hardhat.config.ts

networks: {
        hardhat: {
            accounts: {
                count: 60,
                // accountsBalance: 10000000000000000000000, // default value is 10000ETH in wei
            },
        },
    },

The getSigners function will retrieve all the accounts.

More parameters for the accounts are defined in the hardhat documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.