{"id":7230,"date":"2024-02-16T06:15:56","date_gmt":"2024-02-16T06:15:56","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7230"},"modified":"2024-02-16T06:15:58","modified_gmt":"2024-02-16T06:15:58","slug":"hardhat-enhancing-test-coverage-using-different-addresses-in-scripts","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/hardhat-enhancing-test-coverage-using-different-addresses-in-scripts\/","title":{"rendered":"Hardhat Enhancing Test Coverage: Using Different Addresses in Scripts"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/hardhat-enhancing-test-coverage-using-different-addresses-in-scripts\/#How_It_Works_in_Hardhat\" title=\"How It Works in Hardhat\">How It Works in Hardhat<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/hardhat-enhancing-test-coverage-using-different-addresses-in-scripts\/#Why_Its_Useful_in_Hardhat\" title=\"Why It&#8217;s Useful in Hardhat\">Why It&#8217;s Useful in Hardhat<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/hardhat-enhancing-test-coverage-using-different-addresses-in-scripts\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>When you&#8217;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 <a href=\"https:\/\/metaschool.so\/articles\/structure-of-a-smart-contract\/\" target=\"_blank\" rel=\"noreferrer noopener\">smart contracts<\/a> with a different address? Hardhat&#8217;s got your back with a super trick using the <code>connect<\/code> method from ethers.js.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-it-works-in-hardhat\"><span class=\"ez-toc-section\" id=\"How_It_Works_in_Hardhat\"><\/span>How It Works in Hardhat<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>However, if you want to execute transactions as if they were initiated by a different Ethereum address, you can use the <code>connect<\/code> method provided by ethers.js. This method creates a connected instance of the contract associated with the specified Ethereum address.<\/p>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong> <a href=\"https:\/\/metaschool.so\/courses\/learn-everything-about-nfts\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Learn everything about NFTs<\/strong><\/a><\/p>\n\n\n\n<p>Let&#8217;s say you want to test a transfer function between two accounts. Here&#8217;s how you can do it:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:16px\"><code>\/\/ your previous code\n\nit(\"Should transfer tokens between accounts\", async function() {\n    \/\/ First, transfer some tokens to addr1\n    await myToken.transfer(addr1.address, 20);\n    \/\/ Check if the tokens are received by addr1\n    expect(await myToken.balanceOf(addr1.address)).to.equal(20);\n\n    \/\/ Now, let's use addr1 to transfer tokens to addr2\n    await myToken.connect(addr1).transfer(addr2.address, 20);\n    \/\/ Check if the tokens are received by addr2\n    expect(await myToken.balanceOf(addr2.address)).to.equal(20);\n});<\/code><\/pre>\n\n\n\n<p>In the example provided, we have a test case where we&#8217;re transferring tokens between two accounts. Here&#8217;s what&#8217;s happening step by step:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>We start by transferring some tokens to <code>addr1<\/code> using the <code>transfer<\/code> function of the token contract (<code>myToken<\/code>).<\/li><li>After the transfer, we want to simulate a transfer from <code>addr1<\/code> to <code>addr2<\/code>. Instead of using the default address, we use the <code>connect<\/code> method to create a connected instance of <code>myToken<\/code> associated with <code>addr1<\/code>.<\/li><li>With this connected instance, when we call the <code>transfer<\/code> function again, it behaves as if it were initiated by <code>addr1<\/code>. This means the transfer operation to <code>addr2<\/code> is executed as if <code>addr1<\/code> initiated it.<\/li><li>Finally, we verify the results of the transfer by checking the balance of <code>addr2<\/code> to ensure the tokens were received successfully.<\/li><\/ol>\n\n\n\n<p>\ud83d\udd25\u00a0<strong>Check this article out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/articles\/build-full-stack-nft-minting-dapp\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Build a Full Stack NFT minting Dapp w\/ Hardhat, ethers.js, Next.js &amp; TailwindCSS<\/strong><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-it-s-useful-in-hardhat\"><span class=\"ez-toc-section\" id=\"Why_Its_Useful_in_Hardhat\"><\/span>Why It&#8217;s Useful in Hardhat<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This feature allows you to simulate transactions from different accounts in your tests and scripts. It&#8217;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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Testing your smart contracts with different addresses in Hardhat is now easy thanks to the <code>connect<\/code> method. Whether you&#8217;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!<\/p>\n\n\n\n<p><strong>Try it out and let us know how it went by tagging Metaschool on Social Media.<\/strong><\/p>\n\n\n\n<p><strong>Follow us on<\/strong>&nbsp;\u2013<\/p>\n\n\n\n<p>\ud83d\udd2eTwitter \u2013&nbsp;<a href=\"https:\/\/twitter.com\/0xmetaschool\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/twitter.com\/0xmetaschool<\/a><\/p>\n\n\n\n<p>\ud83d\udd17LinkedIn \u2013\u00a0<a href=\"https:\/\/www.linkedin.com\/company\/0xmetaschool\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.linkedin.com\/company\/0xmetaschool\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":16,"featured_media":7233,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[51,327,46,29],"class_list":["post-7230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-hardhat","tag-solidity","tag-web3"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=7230"}],"version-history":[{"count":3,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7230\/revisions"}],"predecessor-version":[{"id":7235,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7230\/revisions\/7235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/7233"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}