<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Duarte Roso</title>
    <description>The latest articles on DEV Community by Duarte Roso (@duarteroso).</description>
    <link>https://dev.to/duarteroso</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F923409%2F3d80e8e0-f993-4287-856b-cdc48d94c7ac.jpeg</url>
      <title>DEV Community: Duarte Roso</title>
      <link>https://dev.to/duarteroso</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/duarteroso"/>
    <language>en</language>
    <item>
      <title>My first Solidity Smart Contract</title>
      <dc:creator>Duarte Roso</dc:creator>
      <pubDate>Sat, 25 Mar 2023 18:22:51 +0000</pubDate>
      <link>https://dev.to/duarteroso/my-first-solidity-smart-contract-379e</link>
      <guid>https://dev.to/duarteroso/my-first-solidity-smart-contract-379e</guid>
      <description>&lt;h1&gt;
  
  
  My first Solidity Smart Contract
&lt;/h1&gt;

&lt;p&gt;After &lt;a href="https://dev.to/duarteroso/introduction-to-evm-smart-contract-development-1bdo"&gt;setting up our environment&lt;/a&gt; we are now ready to write our first smart contract using Solidity.&lt;/p&gt;

&lt;p&gt;In this article we will be writing a basic smart contract, some tests as well as a deployment script to push it into the blockchain. As a bonus, we will configure Hardhat to give us cost estimations from our code. Exciting!&lt;/p&gt;




&lt;h2&gt;
  
  
  Index
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Project Setup&lt;/li&gt;
&lt;li&gt;Smart Contract: Person&lt;/li&gt;
&lt;li&gt;Testing the contract&lt;/li&gt;
&lt;li&gt;Gas reporter&lt;/li&gt;
&lt;li&gt;Deploying the Contract&lt;/li&gt;
&lt;li&gt;Storage Layout&lt;/li&gt;
&lt;li&gt;Smart Contract: Better Person&lt;/li&gt;
&lt;li&gt;Better?&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;If you followed the previous &lt;a href="https://dev.to/duarteroso/introduction-to-evm-smart-contract-development-1bdo"&gt;article&lt;/a&gt; you know how to create a new project using hardhat. Go ahead and create it; you can delete the contents of &lt;code&gt;contracts&lt;/code&gt;, &lt;code&gt;scripts&lt;/code&gt; as well as &lt;code&gt;test&lt;/code&gt;, we will be creating new files for each category.&lt;/p&gt;

&lt;h2&gt;
  
  
  Smart Contract: Person
&lt;/h2&gt;

&lt;p&gt;Imagine for a moment we want to represent a "person" as a smart contract. For simplicity sake a person will be limited to their birth date. Create a contract and call it &lt;code&gt;PersonV1.sol&lt;/code&gt; (there will be V2 later).&lt;/p&gt;

&lt;p&gt;We start off by the smart contract license. A full list of licenses can be found &lt;a href="https://spdx.org/licenses/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. We will go with MIT because it is the second easiest to understand after &lt;a href="https://en.wikipedia.org/wiki/WTFPL" rel="noopener noreferrer"&gt;WTFPL&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Right after, we need to select the solidity version we want our contract to be compiled with. We will go with latest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.17;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A contract is defined by the keyword &lt;code&gt;contract&lt;/code&gt; followed by a meaningful name. If you are familiar with object-oriented programming languages, it is similar to defining a class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract PersonV1 {
    uint256 public year;
    uint256 public month;
    uint256 public day;

    constructor(
        uint256 _year,
        uint256 _month,
        uint256 _day
    ) {
        year = _year;
        month = _month;
        day = _day;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our person is defined by the year, the month and the day she was born. Obviously, this is a rather over simplified example as nothing other than the address of this contract makes this person unique in the eyes of the law. Moving on...&lt;/p&gt;

&lt;p&gt;The birth date of this person is set at the constructor level, meaning that once set it cannot be changed; we will be adding a method exactly for this purpose to show the cost of altering data on the blockchain. We willingly ignore bad input for the sake of keeping it simple.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract PersonV1 {
    // ...

    function setAge(
        uint256 _year,
        uint256 _month,
        uint256 _day
    ) public {
        year = _year;
        month = _month;
        day = _day;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this we are able to change our smart contract and measure the cost of such operations. Just like a regular program each operation has a cost, whereas on a CPU the cost is payed with time and energy, on the blockchain the cost is payed with money. We will later try to optimize our contract and understand how we can better write smart contracts to minimize the monetary cost.&lt;/p&gt;

&lt;p&gt;You can now compile your contract to make sure the syntax is correct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat compile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing the contract
&lt;/h2&gt;

&lt;p&gt;Testing a smart contract is the single most important task a smart contract developer will be doing. A flawed contract is a contract that is open for attacks, potentially putting at risk the Ether you have placed into it.&lt;/p&gt;

&lt;p&gt;Thankfully our smart contract does not receive any ETH, we don't have to worry about breaches. Regardless, it is a good practice to test every single functionality of your contract; with basic cases as well a edge cases to make sure it can handle all situations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;expect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chai&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BigNumberish&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ethers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hardhat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PersonV1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BigNumberish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1987&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BigNumberish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BigNumberish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;deployPerson&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getContractFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PersonV1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deploy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Deployment&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should set the right age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;deployPerson&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="c1"&gt;//&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;year&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c1"&gt;//&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;month&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c1"&gt;//&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;day&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should update the age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;deployPerson&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="c1"&gt;//&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;month&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c1"&gt;//&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;year&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c1"&gt;//&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;month&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;month&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c1"&gt;//&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;day&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our first test (defined by the first &lt;code&gt;it&lt;/code&gt; function), we make sure that the values passed when constructing our smart contract are correctly saved. On our second test we call the age setter method and verify the new values have been saved into the blockchain.&lt;/p&gt;

&lt;p&gt;As you can see, any interaction with the contract requires an &lt;code&gt;await&lt;/code&gt; keyword. This is because interacting with the blockchain is not an immediate operation and therefore needs to wait for the value before proceeding.&lt;/p&gt;

&lt;p&gt;Run the tests to make sure everything gets the green light.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Gas reporter
&lt;/h2&gt;

&lt;p&gt;Hardhat has a feature that, after setting it up and connecting all the pieces, allows our tests to report gas usage based on data taken from &lt;a href="https://coinmarketcap.com/api/" rel="noopener noreferrer"&gt;CoinMarketCap&lt;/a&gt;. It remains an estimate and the actual price will vary depending on a number of factors, but it is useful enough to start optimizing our smart contracts.&lt;/p&gt;

&lt;p&gt;The gas reporter package does not come with the default project setup. Run the following command to add it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pnpm add &lt;span class="nt"&gt;-D&lt;/span&gt; hardhat-gas-reporter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in our &lt;code&gt;hardhat.config.ts&lt;/code&gt; we can import the package to expose new configuration options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hardhat-gas-reporter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding this import will add a new &lt;code&gt;gasReporter&lt;/code&gt; property to &lt;code&gt;HardhatUserConfig&lt;/code&gt;. This new property has multiple entries, the ones we need right now are used to set the currency and to set the API key to connect to CoinMarketCap.&lt;br&gt;
To get an API key, head to &lt;a href="https://coinmarketcap.com/api/" rel="noopener noreferrer"&gt;CoinMarketCap&lt;/a&gt;, after creating an account and you will find your key in the &lt;code&gt;Overview&lt;/code&gt; page. They offer multiple plans, from free to enterprise, that come with more or less limitations. For our needs the free tier is more than enough.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HardhatUserConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//...&lt;/span&gt;
  &lt;span class="na"&gt;gasReporter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;USD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;coinmarketcap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are all set up! Running our tests will from now on include a summary of our gas usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pnpm hardhat &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl953qnqjlo2b9szgx7x4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl953qnqjlo2b9szgx7x4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As it is right now, it would cost us roughly $12 to add a new person to the blockchain... ouch! Another $1.50 to change their birth data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying the Contract
&lt;/h2&gt;

&lt;p&gt;Create a &lt;code&gt;deploy.ts&lt;/code&gt; file under &lt;code&gt;scripts&lt;/code&gt;. This will be our deploy script that we will use to actually deploy our smart contract into the local blockchain. It is in this stage that we can pass custom values to the constructor of our smart contract or apply any other logic you may require.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hardhat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printMemoryLayout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;### Memory Layout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;slot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStorageAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Slot\t#&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;\t&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;deployPersonV1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PersonV1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getContractFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PersonV1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;PersonV1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deploy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1987&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deployed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PersonV1 deployed!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;printMemoryLayout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;deployPersonV1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exitCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;deployPersonV1&lt;/code&gt; function we start off by getting the &lt;code&gt;PersonV1&lt;/code&gt; contract factory and using it to deploy a new instance of the smart contract by passing it three inputs as expect by &lt;code&gt;PersonV1&lt;/code&gt;'s constructor. Notice that, again, all methods that interact with the blockchain require an &lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We also have a &lt;code&gt;printMemoryLayout&lt;/code&gt; function that we call after deploying the &lt;code&gt;PersonV1&lt;/code&gt; contract. This method will be useful to check our contract storage layout.&lt;/p&gt;

&lt;p&gt;Time to publish our first smart contract!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat run scripts/deploy.ts &lt;span class="nt"&gt;--network&lt;/span&gt; localhost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Storage Layout
&lt;/h2&gt;

&lt;p&gt;In the EVM, the maximum size a type can have is 256 bits. It stores values in &lt;code&gt;slots&lt;/code&gt; which also happen to have a size of 256 bits. This means that in a &lt;code&gt;slot&lt;/code&gt; you can fit data with a combined size of at most 256 bits. For example, in a &lt;code&gt;slot&lt;/code&gt; you can fit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 &lt;code&gt;int256&lt;/code&gt;/&lt;code&gt;uint256&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;2 &lt;code&gt;int128&lt;/code&gt;/&lt;code&gt;uint128&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;4 &lt;code&gt;int64&lt;/code&gt;/&lt;code&gt;uint64&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;li&gt;32 &lt;code&gt;int8&lt;/code&gt;/&lt;code&gt;uint&lt;/code&gt;8&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or it can be combined: 1 &lt;code&gt;int128&lt;/code&gt;/&lt;code&gt;uint128&lt;/code&gt; and 2 &lt;code&gt;int64&lt;/code&gt;/&lt;code&gt;uint64&lt;/code&gt; because &lt;code&gt;1x128 (bits) + 2x64 (bits) = 256 (bits)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When compiling your smart contract, the compiler will attempt to pack your data into the most optimized way in order to use the least slots possible.&lt;/p&gt;

&lt;p&gt;After deploying the contract, the following layout was printed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;### Memory Layout&lt;/span&gt;
Slot    &lt;span class="c"&gt;#0  0x00000000000000000000000000000000000000000000000000000000000007c3&lt;/span&gt;
Slot    &lt;span class="c"&gt;#1  0x0000000000000000000000000000000000000000000000000000000000000004&lt;/span&gt;
Slot    &lt;span class="c"&gt;#2  0x0000000000000000000000000000000000000000000000000000000000000011&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These values might not seem relevant until you look at its hexadecimal values. Remember that our contract had only three values:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;uint256 public year&lt;/code&gt; initialized to &lt;code&gt;1987&lt;/code&gt; or &lt;code&gt;0x07C3&lt;/code&gt; which can be seen at the end of Slot #0&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;uint256 public month&lt;/code&gt; initialized to &lt;code&gt;4&lt;/code&gt; or &lt;code&gt;0x04&lt;/code&gt; which can be seen at the end of Slot #1&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;uint256 public day&lt;/code&gt; initialized to &lt;code&gt;17&lt;/code&gt; or &lt;code&gt;0x11&lt;/code&gt; which can be seen at the end of Slot #2&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A 256 bits long number seems a bit of a waste for values that will never be able to make full use of its capacity. Remember that the solidity compiler will try to pack the memory as best as possible but sometimes the developer needs to give it a hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Smart Contract: Better Person
&lt;/h2&gt;

&lt;p&gt;In our first contract, all our data was of type &lt;code&gt;uint256&lt;/code&gt; which can have a maximum value of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;115792089237316195423570985008687907853269984665640564039457584007913129639935
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Personally, I cannot pronounce that number...&lt;/p&gt;

&lt;p&gt;More importantly no month has that many days, no year has that many months and the universe will not have that many years (or will it?).&lt;/p&gt;

&lt;p&gt;Therefore we can definitely save a lot of space by using appropriate types that fit our usage, namely &lt;code&gt;uint8&lt;/code&gt; (whose maximum value is 255) for the month and day and an uint16 (whose maximum value is 65535) for the year.&lt;/p&gt;

&lt;p&gt;Create a new file under &lt;code&gt;contracts&lt;/code&gt; and name it &lt;code&gt;PersonV2.sol&lt;/code&gt;. This will be a duplicate of &lt;code&gt;PersonV1&lt;/code&gt; but instead of using 3 &lt;code&gt;uint256&lt;/code&gt; we will use smaller sized types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract PersonV2 {
    uint16 public year;
    uint8 public month;
    uint8 public day;

    constructor(
        uint16 _year,
        uint8 _month,
        uint8 _day
    ) {
        year = _year;
        month = _month;
        day = _day;
    }

    function setAge(
        uint16 _year,
        uint8 _month,
        uint8 _day
    ) public {
        year = _year;
        month = _month;
        day = _day;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the same tests as for &lt;code&gt;PersonV1&lt;/code&gt; as nothing was changed. Duplicate &lt;code&gt;test/PersonV1.ts&lt;/code&gt; an name it &lt;code&gt;PersonV2.ts&lt;/code&gt;. Make sure you replace any reference of &lt;code&gt;PersonV1&lt;/code&gt; with &lt;code&gt;PersonV2&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Finally let's also duplicate the deploy code for our &lt;code&gt;PersonV2&lt;/code&gt; contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;deployPersonV2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PersonV2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getContractFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PersonV2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;PersonV2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deploy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1987&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deployed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PersonV2 deployed!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;printMemoryLayout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//...&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;deployPersonV2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Better?
&lt;/h2&gt;

&lt;p&gt;After you deploy both smart contracts, you will see a different storage layout for each:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;PersonV1 deployed!
&lt;span class="c"&gt;### Memory Layout&lt;/span&gt;
Slot    &lt;span class="c"&gt;#0  0x00000000000000000000000000000000000000000000000000000000000007c3&lt;/span&gt;
Slot    &lt;span class="c"&gt;#1  0x0000000000000000000000000000000000000000000000000000000000000004&lt;/span&gt;
Slot    &lt;span class="c"&gt;#2  0x0000000000000000000000000000000000000000000000000000000000000011&lt;/span&gt;
PersonV2 deployed!
&lt;span class="c"&gt;### Memory Layout&lt;/span&gt;
Slot    &lt;span class="c"&gt;#0  0x00000000000000000000000000000000000000000000000000000000110407c3&lt;/span&gt;
Slot    &lt;span class="c"&gt;#1  0x0000000000000000000000000000000000000000000000000000000000000000&lt;/span&gt;
Slot    &lt;span class="c"&gt;#2  0x0000000000000000000000000000000000000000000000000000000000000000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For &lt;code&gt;PersonV2&lt;/code&gt;, slot #1 and slot #2 are zero-ed out although both contract contain three member. Why would that be?&lt;/p&gt;

&lt;p&gt;Unlike previously, this time the solidity compiler was able to do its magic after a bit of our help. You see, the compiler will attempt to pack storage memory into as few slots as possible. Whereas before each of our data was using 256 bits, now we are only using a fraction of that! &lt;/p&gt;

&lt;p&gt;You can still see that our data hasn't changed. Slot #0 of &lt;code&gt;PersonV2&lt;/code&gt; has the value of &lt;code&gt;0x110407c3&lt;/code&gt;, the slots aggregation of &lt;code&gt;PersonV1&lt;/code&gt;. &lt;br&gt;
If we decompose this value:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Day now fits in 8 bits, i.e. 2 bytes = &lt;code&gt;0x11&lt;/code&gt; (17)&lt;/li&gt;
&lt;li&gt;Month also fits in 8 bits, i.e. 2 bytes = &lt;code&gt;0x04&lt;/code&gt; (4)&lt;/li&gt;
&lt;li&gt;Year fits in 16 bits, i.e. 4 bytes = &lt;code&gt;0x07c3&lt;/code&gt; (1987)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We have effectively reduced our storage footprint by a lot! Does that help cost wise? &lt;/p&gt;

&lt;p&gt;Each time we alter the blockchain, a cost has to be paid. This time we are deploying a contract that will use less storage space on the blockchain. Comparing the cost of deployment for each version of our contract speaks for itself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PersonV1 = $12.60&lt;/li&gt;
&lt;li&gt;PersonV2 = $9.34 (25% less!)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Same goes for the method that alters the state of the blockchain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PersonV1 = $1.52&lt;/li&gt;
&lt;li&gt;PersonV2 = $1.14 (25% less!)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz2iax3pn590i2mwd0737.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz2iax3pn590i2mwd0737.png" alt="PersonV1-vs-PersonV2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Creating a smart contract, creating tests to verify all its functionality and creating a deploy script is the foundation of what it means to be a smart contract developer.&lt;/p&gt;

&lt;p&gt;On top of that, thanks to some basic profiling tools and common sense, the developer is able make a smart contract more cost effective.&lt;/p&gt;

&lt;p&gt;In this article we attempted to cover all of these points to hopefully give you a good starting point on becoming a smart contract developer. The most challending part will be to audit your contracts to avoid any exploits and potentially loosing money.&lt;/p&gt;

&lt;p&gt;Remember: only practice makes perfect and stay curious!&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>typescript</category>
      <category>hardhat</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to EVM Smart Contract development</title>
      <dc:creator>Duarte Roso</dc:creator>
      <pubDate>Thu, 26 Jan 2023 18:52:12 +0000</pubDate>
      <link>https://dev.to/duarteroso/introduction-to-evm-smart-contract-development-1bdo</link>
      <guid>https://dev.to/duarteroso/introduction-to-evm-smart-contract-development-1bdo</guid>
      <description>&lt;h1&gt;
  
  
  Introduction to EVM SmartContract development
&lt;/h1&gt;

&lt;p&gt;In this introduction, we will go through the basics of Hardhat, its most useful commands and a basic Hardhat workflow.&lt;/p&gt;




&lt;h2&gt;
  
  
  Index
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Prerequisite&lt;/li&gt;
&lt;li&gt;
Hardhat

&lt;ol&gt;
&lt;li&gt;Intro&lt;/li&gt;
&lt;li&gt;Initial Setup&lt;/li&gt;
&lt;li&gt;
Important Commands

&lt;ol&gt;
&lt;li&gt;Create local network&lt;/li&gt;
&lt;li&gt;Compile smart contracts&lt;/li&gt;
&lt;li&gt;Test smart contracts&lt;/li&gt;
&lt;li&gt;Deploy smart contracts&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;/li&gt;

&lt;li&gt;

Solidity

&lt;ol&gt;
&lt;li&gt;About&lt;/li&gt;
&lt;li&gt;Version&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;

Vyper

&lt;ol&gt;
&lt;li&gt;Solidity or Vyper&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Blockchain communication&lt;/li&gt;

&lt;li&gt;First Run&lt;/li&gt;

&lt;li&gt;Conclusion&lt;/li&gt;

&lt;/ol&gt;




&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;p&gt;This articles starts with a few assumptions, assumptions needed to keep the scope of the article to a minimum. If you are not familiar with the following topics or you need to refresh your knowledge, I advice to take the time to read about them before continuing.&lt;/p&gt;

&lt;p&gt;We assume that you are familiar with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Overall concepts and terminology of the &lt;a href="https://ethereum.org/en/what-is-ethereum/" rel="noopener noreferrer"&gt;blockchain&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Comfortable using the terminal and cli tools&lt;/li&gt;
&lt;li&gt;Familiarity with development tools like VSCode, &lt;a href="https://www.typescriptlang.org/docs/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt;, &lt;a href="https://www.tutorialspoint.com/solidity/index.htm" rel="noopener noreferrer"&gt;Solidity&lt;/a&gt;, &lt;a href="https://pnpm.io/installation" rel="noopener noreferrer"&gt;npm&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Hardhat
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;Hardhat is a development environment for developing on a blockchain; in our case we will focus on the Ethereum network. It provides a set of tools that makes developing smart contracts easier:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TypeScript support&lt;/li&gt;
&lt;li&gt;Connect to a local network or test networks&lt;/li&gt;
&lt;li&gt;Run tests for your smart contracts&lt;/li&gt;
&lt;li&gt;Estimate costs for deploying &amp;amp; calling your smart contracts&lt;/li&gt;
&lt;li&gt;Inspect the memory slot layout of your contracts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;just to name a few. It also provides an abstraction to communicate with a wallet, be it MetaMask or a hardware wallet. This way we don't need to worry about the technicalities of our wallet, we can talk directly with our smart contracts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initial Setup
&lt;/h3&gt;

&lt;p&gt;Hardhat is available as a NPM package and can easily be installed using your favorite package manager.&lt;/p&gt;

&lt;p&gt;Start by initializing a new project in an empty folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;intro
&lt;span class="nb"&gt;cd &lt;/span&gt;intro/
pnpm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will setup your project by creating a &lt;code&gt;package.json&lt;/code&gt; file. To add Hardhat run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pnpm add &lt;span class="nt"&gt;-D&lt;/span&gt; hardhat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the command completes we can use &lt;code&gt;npx&lt;/code&gt; to create our first Hardhat project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fewaray1t3m4vmugtg62u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fewaray1t3m4vmugtg62u.png" alt="Hardhat project layout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will be creating a TypeScript project, simply because having the types will help us understand the data we are handling as well as save us some painful headaches. Therefore select &lt;code&gt;Create a TypeScript project&lt;/code&gt; and follow the instructions.&lt;/p&gt;

&lt;p&gt;Your project structure should look like the following&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgnawmeoh7ologgjusvmj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgnawmeoh7ologgjusvmj.png" alt="hardhat project structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The folders that matter to us in this introduction are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;contracts&lt;/code&gt; - where we will place our smart contracts&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;scripts&lt;/code&gt; - where we will place our deploy scripts (and others)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tests&lt;/code&gt; - where we will place our tests for our smart contracts&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Important commands
&lt;/h3&gt;

&lt;p&gt;With our project ready to be used, let us take some time to learn a few Hardhat commands.&lt;/p&gt;
&lt;h4&gt;
  
  
  Create local network
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Use this command to create a local network. You are not limited by it, any network can be configured (test or mainnet)&lt;/p&gt;

&lt;p&gt;Your local node will be setup with 10 accounts each owning 10000ETH. Hardhat will always generate the same accounts conveniently saving us from changing the account addresses in our scripts each time we launch a local node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE: These private addresses are for testing locally and locally only! Any ETH sent to those addresses on any network (mainnet or test) will be lost!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The process does not return, therefore my advice is to run it on a side terminal. You can even share a process across multiple projects you might be working on.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;node&lt;/code&gt; process will print information about any transaction occurring on our local blockchain, useful to check contract addresses, gas spending, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE: The local network will not keep state in between runs, that is, each time this command is run will give you a fresh new blockchain.&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Compile smart contracts
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat compile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Other commands might trigger a compilation if hardhat sees any change in our smart contracts' code. Still, it is a good practice to run the compilation independently.&lt;/p&gt;

&lt;p&gt;Upon a successful compilation, hardhat will generate nicely crafted types to use with your TypeScript code, allowing us to make use of the beautiful type system.&lt;/p&gt;
&lt;h4&gt;
  
  
  Test smart contracts
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This command will run any test found under the &lt;code&gt;tests/&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Once we configure our project, it will even be able to give us some cost estimation from deploying contracts and from calling our contract's functions.&lt;/p&gt;
&lt;h4&gt;
  
  
  Deploy smart contracts
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat run &amp;lt;script&amp;gt; &lt;span class="nt"&gt;--network&lt;/span&gt; &amp;lt;ethereum_network&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We will be using this command to deploy our smart contracts into a specified network (mostly our local network).&lt;/p&gt;


&lt;h2&gt;
  
  
  Solidity
&lt;/h2&gt;
&lt;h3&gt;
  
  
  About
&lt;/h3&gt;

&lt;p&gt;Solidity is the programming language used to create smart contracts on the Ethereum blockchain or any blockchain compatible with the Ethereum Virtual Machine, EVM for short.&lt;/p&gt;
&lt;h3&gt;
  
  
  Version
&lt;/h3&gt;

&lt;p&gt;Being a programming language, it needs a compiler to convert the code into bytecode that can be understood by the EVM. Luckily, Hardhat already provides us with a compiler so no further setup is required.&lt;/p&gt;

&lt;p&gt;You can find the current used version of the Solidity compiler in &lt;code&gt;hardhat.config.ts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flbhj4ah9abui48vezvk2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flbhj4ah9abui48vezvk2.png" alt="hardhat solidity version"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you wish to use a different version, simply update it in there and voila!&lt;/p&gt;


&lt;h2&gt;
  
  
  Vyper
&lt;/h2&gt;

&lt;p&gt;Vyper is another programming language used to create smart contracts on any EVM compatible blockchain. Unlike Solidity, Vyper is based on the python syntax. &lt;/p&gt;

&lt;p&gt;Hardhat does not add support for Vyper on a fresh installation. Therefore we need to explicitly add it to our dev dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pnpm add &lt;span class="nt"&gt;-D&lt;/span&gt; @nomiclabs/hardhat-vyper
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now need to tell hardhat to compile our Vyper contracts. Head to &lt;code&gt;hardhat.config.ts&lt;/code&gt; to add a vyper import&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@nomiclabs/hardhat-vyper&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will extend the &lt;code&gt;HardhatUserConfig&lt;/code&gt; to include an optional Vyper parameter to our config&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HardhatUserConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// your versions may differ from mine&lt;/span&gt;
  &lt;span class="na"&gt;solidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.8.17&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;vyper&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.3.7&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solidity or Vyper
&lt;/h3&gt;

&lt;p&gt;Should we use one over the other? Why do we need more than one language to develop our smart contracts?&lt;/p&gt;

&lt;p&gt;The answer to these questions will depend on multiple factors. If two programming languages offer the same set of functionality then it becomes a matter of personal choice. In our case there is a clear difference, one which is very important to know and understand how to use.&lt;/p&gt;

&lt;p&gt;Vyper is a simpler languages restricting the logic of our smart contract. By being more strict in its features it is often cheaper to run a Vyper contract than a twin Solidity contract due to its predictability:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Recursions are forbidden &lt;/li&gt;
&lt;li&gt;No Inheritance&lt;/li&gt;
&lt;li&gt;No dynamic data structures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By controlling these aspects of a program, the compiler can produce more optimized code that will need less processing power to execute. Remember: less processing == less gas used (i.e. cheaper).&lt;/p&gt;

&lt;p&gt;Because interacting with blockchains has a cost, a developer should be able to use different tools to perform the task in an optimized manner. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you need complex logic: use &lt;code&gt;Solidity&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For simple transaction based contracts: use &lt;code&gt;Vyper&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The blockchain world is still at an early age. The blockchains we know today will change, they might fade away and leave space to new, more modern, more advanced blockchains.&lt;br&gt;
Programming languages will appear, based on or adapted from languages we already know and love.&lt;/p&gt;

&lt;p&gt;What is important today might become irrelevant tomorrow. Be adaptable and curious!&lt;/p&gt;


&lt;h2&gt;
  
  
  Blockchain communication
&lt;/h2&gt;

&lt;p&gt;We are still missing an important piece to make this all work. We need a way to interact with the blockchain from our code.&lt;/p&gt;

&lt;p&gt;The two most popular libraries at the time of writing are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Web3.js&lt;/li&gt;
&lt;li&gt;ethers.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hardhat provides a plugin for both, but decides to use &lt;code&gt;ethers.js&lt;/code&gt; by default. Both these libraries will provide similar functionality and you can read the differences in &lt;a href="https://medium.com/l4-media/announcing-ethers-js-a-web3-alternative-6f134fdd06f3" rel="noopener noreferrer"&gt;this article&lt;/a&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  First run
&lt;/h2&gt;

&lt;p&gt;At this point, nothing is blocking us from experimenting with what the default project provides us so far. Let’s try it!&lt;/p&gt;

&lt;p&gt;First of all, head to the project folder. The following steps will be your usual workflow in compiling, testing and deploying.&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 1
&lt;/h4&gt;

&lt;p&gt;Start the local node if it isn't already running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1txpoqom1hhith9bukox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1txpoqom1hhith9bukox.png" alt="hardhat node"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2
&lt;/h4&gt;

&lt;p&gt;Compile your smart contracts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat compile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzre03wlictwdjxrcdl88.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzre03wlictwdjxrcdl88.png" alt="hardhat compile"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3
&lt;/h4&gt;

&lt;p&gt;Test your smart contracts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazirhyjzei0jrfxmxzdh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazirhyjzei0jrfxmxzdh.png" alt="hardhat test"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 4
&lt;/h4&gt;

&lt;p&gt;Deploy your smart contracts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx hardhat run scripts/deploy.ts &lt;span class="nt"&gt;--network&lt;/span&gt; localhost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fielv5vz72cl9rshlmgc8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fielv5vz72cl9rshlmgc8.png" alt="hardhat run"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 5
&lt;/h4&gt;

&lt;p&gt;Check your smart contract was added to the blockchain. Check in the terminal running the local node process for the output.&lt;/p&gt;

&lt;p&gt;You should see something similar to the following:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8eng45yk3tghm5ri18l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8eng45yk3tghm5ri18l.png" alt="hardhat transaction"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means a first block was mined (Block #1), it used 326016 GAS and our contract can be found at the (local) address &lt;code&gt;0x5fbdb2315678afecb367f032d93f642f64180aa3&lt;/code&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We have covered the very basics of smart contract development.&lt;/p&gt;

&lt;p&gt;We are able to create a Hardhat project, we learned about the essential commands that Hardhat provides and we understand the flow of developing smart contracts using Hardhat.&lt;/p&gt;

&lt;p&gt;In the next article, we will write our first smart contract, write our first test and our first deployment script. We will also configure our hardhat project to get estimations of the cost of each of our operation on the blockchain.&lt;/p&gt;

&lt;p&gt;Hope to see you there!&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>hardhat</category>
      <category>vyper</category>
      <category>blockchain</category>
    </item>
  </channel>
</rss>
