Skip to content
Home ยป Answers ยป Hardhat Enhancing Test Coverage: Using Different Addresses in Scripts

Hardhat Enhancing Test Coverage: Using Different Addresses in Scripts

When you’re running tests and scripts in Hardhat, by default, all functions are called using the first address provided. But what if you want to mix things up a bit and test your smart contracts with a different address? Hardhat’s got your back with a super trick using the connect method from ethers.js.

How It Works in Hardhat

When you call a function on a contract instance in Solidity, it requires an Ethereum address to execute the transaction. By default, Hardhat uses the first address provided to execute these transactions during testing and scripting.

However, if you want to execute transactions as if they were initiated by a different Ethereum address, you can use the connect method provided by ethers.js. This method creates a connected instance of the contract associated with the specified Ethereum address.

๐Ÿ”ฅ Check this course out: Learn everything about NFTs

Let’s say you want to test a transfer function between two accounts. Here’s how you can do it:

// your previous code

it("Should transfer tokens between accounts", async function() {
    // First, transfer some tokens to addr1
    await myToken.transfer(addr1.address, 20);
    // Check if the tokens are received by addr1
    expect(await myToken.balanceOf(addr1.address)).to.equal(20);

    // Now, let's use addr1 to transfer tokens to addr2
    await myToken.connect(addr1).transfer(addr2.address, 20);
    // Check if the tokens are received by addr2
    expect(await myToken.balanceOf(addr2.address)).to.equal(20);
});

In the example provided, we have a test case where we’re transferring tokens between two accounts. Here’s what’s happening step by step:

  1. We start by transferring some tokens to addr1 using the transfer function of the token contract (myToken).
  2. After the transfer, we want to simulate a transfer from addr1 to addr2. Instead of using the default address, we use the connect method to create a connected instance of myToken associated with addr1.
  3. With this connected instance, when we call the transfer function again, it behaves as if it were initiated by addr1. This means the transfer operation to addr2 is executed as if addr1 initiated it.
  4. Finally, we verify the results of the transfer by checking the balance of addr2 to ensure the tokens were received successfully.

๐Ÿ”ฅย Check this article out:ย Build a Full Stack NFT minting Dapp w/ Hardhat, ethers.js, Next.js & TailwindCSS

Why It’s Useful in Hardhat

This feature allows you to simulate transactions from different accounts in your tests and scripts. It’s super handy for testing scenarios where multiple users interact with your smart contracts. By using different addresses, you can ensure your contracts behave as expected under various conditions.

Conclusion

Testing your smart contracts with different addresses in Hardhat is now easy thanks to the connect method. Whether you’re transferring tokens between accounts or simulating other interactions, this feature enables more comprehensive and realistic testing. So go ahead, experiment with different addresses, and make sure your contracts are rock solid!

Try it out and let us know how it went by tagging Metaschool on Social Media.

Follow us on โ€“

๐Ÿ”ฎTwitter โ€“ https://twitter.com/0xmetaschool

๐Ÿ”—LinkedIn โ€“ย https://www.linkedin.com/company/0xmetaschool/