Search code examples
soliditysmartcontractshardhat

Hardhat smart contract deployment script using multiple wallets


In hardhat config file I am able to provide multiple wallets via an array in the account field. How do I access the 2nd signer? I would like to have a single run deploy script to deploy two contracts each with a different signer.

I know I can just split the deployment into two each with their own config, but my use case is a 12 contract codebase with intricate dependencies between them and I'd like to deploy all of them in one go.

I'm using this approach to deploy: https://hardhat.org/hardhat-runner/docs/guides/deploying

Would be great to have a similar example for the second signer.


Solution

  • Consider the example deploy script from the hardhat documentation:

    const Lock = await ethers.getContractFactory("Lock");
    const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
    
    await lock.deployed();
    

    Now I will deploy the same Lock contract using 2 signers:

    Step 1: Get the signers using hardhat ethers.

    const signers = await ethers.getSigners();
    

    This gives an array of signers.

    Step 2: Deploy using the first signer:

    const lock1 = await Lock.connect(signers[0]).deploy(unlockTime, { value: lockedAmount });
    
    await lock1.deployed();
    

    Step 3: Deploy using the second signer:

    const lock2 = await Lock.connect(signers[1]).deploy(unlockTime, { value: lockedAmount });
    
    await lock2.deployed();
    

    Another way is to do this entirely using ethers.js. I have an example of this on my GitHub: https://github.com/codeTIT4N/axelar-two-way-nft-example/blob/main/scripts/deploy.js