<?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: Perelyn-sama</title>
    <description>The latest articles on DEV Community by Perelyn-sama (@perelynsama).</description>
    <link>https://dev.to/perelynsama</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%2F417083%2F1b2b718d-3bb8-4129-ac82-da3ce91f20ec.jpg</url>
      <title>DEV Community: Perelyn-sama</title>
      <link>https://dev.to/perelynsama</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/perelynsama"/>
    <language>en</language>
    <item>
      <title>NatSpec: The Right way to comment Ethereum smart contracts.</title>
      <dc:creator>Perelyn-sama</dc:creator>
      <pubDate>Mon, 20 Jun 2022 18:32:26 +0000</pubDate>
      <link>https://dev.to/perelynsama/natspec-the-right-way-to-comment-ethereum-smart-contracts-1b0c</link>
      <guid>https://dev.to/perelynsama/natspec-the-right-way-to-comment-ethereum-smart-contracts-1b0c</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;While writing any sort of program, it is recommended that the code should be kept clean and properly documented as the saying goes “clean code is good code”. Smart contracts are not exempted from this and, they are needed more in smart contracts, this is because of several reasons including the fact that the code of smart contracts are open-sourced making them accessible to everyone who would desire to read them. Since the code of your smart contract is open to the general public, properly commenting on your code is very important because it makes it easier for other developers and curious users to understand your code.&lt;/p&gt;

&lt;p&gt;Another more crucial reason for commenting on your smart contracts is to make it easier for auditing companies to audit your smart contract since they are provided with in-depth information about what each contract, library and function is expected to do. With this information, auditors can spend more time trying to spot vulnerabilities in your smart contract rather than trying to figure out the intent behind your code. &lt;/p&gt;

&lt;p&gt;By now you should see the importance of documenting your smart contracts and you’re properly wondering how you can get started and what principles you are to follow. Thankfully the solidity team has introduced a format that solidity smart contract developers can follow called the &lt;strong&gt;Ethereum Natural Language Specification Format (NatSpec)&lt;/strong&gt; and for the rest of this article we will be taking a deep dive into what &lt;strong&gt;NatSpec&lt;/strong&gt; is and how we can use it effectively while writing smart contracts.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is NatSpec?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;NatSpec&lt;/strong&gt; is a documentation format created by the solidity team which was inspired by &lt;a href="https://en.wikipedia.org/wiki/Doxygen" rel="noopener noreferrer"&gt;Doxygen&lt;/a&gt;. it helps you describe the intent behind the code of your solidity smart contracts which could be usefully for the author themselves and other people who might be interested in your code like other developers, users and auditors. &lt;strong&gt;NatSpec&lt;/strong&gt; does not only help describe the intent of your code, it also tells the readers how to interact with it too.&lt;/p&gt;

&lt;p&gt;If your solidity smart contract is commented with &lt;strong&gt;NatSpec&lt;/strong&gt; you could also use it to automatically generate documentation for your code and some tags like &lt;code&gt;@notice&lt;/code&gt; can be displayed in the frontend of your smart contract while users are calling particular functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  NatSpec Tags
&lt;/h2&gt;

&lt;p&gt;Tags are an essential part of &lt;strong&gt;NatSpec&lt;/strong&gt;, they are used to provide information on particular areas in your smart contract. Tags in NatSpec are as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;@title&lt;/strong&gt;: Should provide information on the title of a contract or interface.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@author&lt;/strong&gt;: Should provide information about the author of a contract, library or interface.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@notice&lt;/strong&gt;: Should provide information to the end-user about what a contract, library, interface, function, public state variable or event does.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@dev&lt;/strong&gt;: Should provide extra information about the contract, library, interface, function, state variable or event to a developer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/param"&gt;@param&lt;/a&gt;&lt;/strong&gt;: Should provide information about the parameters of functions or events followed by the parameter name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@return&lt;/strong&gt;: Should provide information about the return variables of a contract’s function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@inheritdoc:&lt;/strong&gt; Copies all missing tags from the base function (must be followed by the contract name)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/custom"&gt;@custom&lt;/a&gt;&lt;/strong&gt;:…: Custom tag, semantics is application-defined&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s a cheat sheet of the above tags from the solidity docs:  &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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1655747898073%2FBbds0wQ00.jfif%2520align%3D" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1655747898073%2FBbds0wQ00.jfif%2520align%3D" alt="FIW5QVIUUAMzLg2.jfif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  NatSpec Example
&lt;/h2&gt;

&lt;p&gt;In another one of my articles where I write about &lt;strong&gt;&lt;a href="https://book.getfoundry.sh/index.html" rel="noopener noreferrer"&gt;Foundry&lt;/a&gt;,&lt;/strong&gt; I employ the use of an ERC20 contract as a test subject to show readers how to use Foundry, admittedly the code for that contract was poorly commented so let’s fix that.&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.13;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

/// @title An ERC20 contract named MyToken
/// @author Perelyn
/// @notice Serves as a fungible token
/// @dev Inherits the OpenZepplin ERC20 implentation
contract MyToken is ERC20 {

    /// @notice Deployer of the smart contract
    /// @dev Only the owner can call the mint and burn functions
    /// @return owner the address of this smart contract's deployer
    address public owner;

    /// @notice Deploys the smart contract and creates 1000 tokens
    /// @dev Assigns `msg.sender` to the owner state variable
    constructor() ERC20("My Token", "MTKN") {
        owner = msg.sender;
        _mint(msg.sender, 1000 * 10**decimals());
    }

    /// @notice Creates `MyToken` tokens and increases the total supply
    /// @dev Function can only be called by the contract's deployer
    /// @param account The address of the account that will receive the newly created tokens
    /// @param amount The amount of tokens `account` will receive
    function mint(address account, uint256 amount) public {
        require(msg.sender == owner, "Only Owner can mint");
        _mint(account, amount);
    }

    /// @notice Destorys `MyToken` tokens and decreases the total supply
    /// @dev Function can only be called by the contract's deployer
    /// @param account The address of the account that will receive the newly created tokens
    /// @param amount The amount of tokens `account` will receive
    function burn(address account, uint256 amount) public {
        require(msg.sender == owner, "Only Owner can burn");
        _burn(account, amount);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks good, doesn’t it? For extra context, in a situation when &lt;code&gt;@notice&lt;/code&gt; is not available &lt;code&gt;///&lt;/code&gt; could serve as a substitute for it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Generating Documentation
&lt;/h2&gt;

&lt;p&gt;Remember I said we could generate documentation from &lt;strong&gt;NatSpec&lt;/strong&gt; comments for our smart contracts, It's time we put that to action.&lt;/p&gt;

&lt;p&gt;The first thing you need to do to be able to generate documentation is to have the solidity compiler &lt;code&gt;solc&lt;/code&gt; on your machine. Follow this &lt;a href="https://docs.soliditylang.org/en/v0.8.13/installing-solidity.html" rel="noopener noreferrer"&gt;link&lt;/a&gt; for guides on how you can install &lt;code&gt;solc&lt;/code&gt; on your machine. &lt;/p&gt;

&lt;p&gt;Run this command to be sure you have &lt;code&gt;solc&lt;/code&gt; on your machine:&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;# on Linux &lt;/span&gt;
solc &lt;span class="nt"&gt;--version&lt;/span&gt;

&lt;span class="c"&gt;# on windows &lt;/span&gt;
solcjs &lt;span class="nt"&gt;-V&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that you have &lt;code&gt;solc&lt;/code&gt; on your machine let’s get started. Remember that &lt;code&gt;@notice&lt;/code&gt; is supposed to be shown to users and &lt;code&gt;@dev&lt;/code&gt; provides information for other developers, this division gives us the ability to create two types of documentation. We can generate documentation for users which would only include information from the &lt;code&gt;@notice&lt;/code&gt; comments and another for developers which would only include information from the &lt;code&gt;@dev&lt;/code&gt; comments. &lt;/p&gt;

&lt;p&gt;The following command can generate user documentation of a 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;solc &lt;span class="nt"&gt;--userdoc&lt;/span&gt; &lt;span class="nt"&gt;--pretty-json&lt;/span&gt; &amp;lt;contract_file&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following command can generate developer documentation of a 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;solc &lt;span class="nt"&gt;--devdoc&lt;/span&gt; &lt;span class="nt"&gt;--pretty-json&lt;/span&gt; &amp;lt;contract_file&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yeah, it's that easy.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Note: The &lt;code&gt;—pretty-json&lt;/code&gt; tag is not needed to generate the documentation but to format the output in the terminal.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For a better illustration let’s try testing out these commands. First, you have to create a directory called &lt;strong&gt;NatSpec&lt;/strong&gt; and then create a file inside it called &lt;strong&gt;EtherWallet.sol&lt;/strong&gt; inside this file, paste in the code below:&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 &amp;gt;=0.4.0 &amp;lt;0.9.0;

contract EtherWallet {
    address public owner;

    constructor(){
        owner = msg.sender;
    }

    receive() external payable {}

    function withdraw(uint _amount) external {
        require(msg.sender == owner, "Caller is not owner");
        payable(msg.sender).transfer(_amount);
    }

    function getBalance( ) external view returns (uint) {
        return address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the code does not have comments, I did this on purpose so you can try out commenting it yourself as a way of practising what you have learned so far. All this contract does is try to act like a wallet for storing Ether and also give users the ability to withdraw their &lt;strong&gt;Ether&lt;/strong&gt; and check the balance of the contract. If you do not want to go through this exercise it's okay, the commented version will be at the bottom of the article.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Note: The Author of the above code is &lt;a href="https://twitter.com/ProgrammerSmart" rel="noopener noreferrer"&gt;Smart contract Programmer&lt;/a&gt; and I got the code from his &lt;a href="https://solidity-by-example.org/app/ether-wallet" rel="noopener noreferrer"&gt;solidity by example&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once you are done with that, open up your terminal and 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;solc &lt;span class="nt"&gt;--userdoc&lt;/span&gt; &lt;span class="nt"&gt;--pretty-json&lt;/span&gt; EtherWallet.sol
&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1655747941993%2FdcPPlNg2z.png%2520align%3D" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1655747941993%2FdcPPlNg2z.png%2520align%3D" alt="natspec-user-doc"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;=======&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;EtherWallet.sol:EtherWallet&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=======&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;User&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Documentation&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"kind"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"methods"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"constructor"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"notice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Deploys the contract"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"getBalance()"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"notice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Get the balance of the contract"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"owner()"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"notice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Get the address of owner of the contract"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"withdraw(uint256)"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"notice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Withdraw Ether from the contract"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"notice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"A Smart contract for storing Ether"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember when using the &lt;code&gt;—userdoc&lt;/code&gt; command, the documentation provided will only display the &lt;code&gt;@notice&lt;/code&gt; comments and ignore the &lt;code&gt;@dev&lt;/code&gt; ones. &lt;/p&gt;

&lt;p&gt;Now let's try generating the developer documentation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;solc &lt;span class="nt"&gt;--devdoc&lt;/span&gt; &lt;span class="nt"&gt;--pretty-json&lt;/span&gt; EtherWallet.sol
&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1655748013976%2Ftnrmady5F.png%2520align%3D" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1655748013976%2Ftnrmady5F.png%2520align%3D" alt="natspec-dev-doc"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s a clearer version incase the screenshot was not good enough:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;=======&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;EtherWallet.sol:EtherWallet&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=======&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;Developer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Documentation&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"smart contract programmer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"details"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"No deposit function, Ether has to be sent directly"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"kind"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"methods"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"constructor"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"details"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sets `msg.sender` as the owner of the contract"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"getBalance()"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"details"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Uses address(this) to get the address of the contract and .balance to get the balance of the address"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"returns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"_0"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"balance the amount of Ether in the contract"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"withdraw(uint256)"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"details"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Function can only be called by the deployer of the contract"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"params"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"_amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"the amount"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"stateVariables"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"owner"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"details"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Owner is set at deployment"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"return"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"owner the deployer of the Ether wallet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"returns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"_0"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"owner the deployer of the Ether wallet"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Ether Wallet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the developer documentation contains the &lt;code&gt;@dev&lt;/code&gt;, &lt;code&gt;@param&lt;/code&gt; and &lt;code&gt;@returns&lt;/code&gt; but excludes &lt;code&gt;@notice&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;Blockchain technology is a very interesting innovation. It has opened ways for humans to interact and work together with each other without worry and fear. You do not have to worry about trusting people because the system itself is trustless. To maintain this amazing feat, we developers have to play our part in contributing to a truly decentralized future and properly documenting the code of our smart contracts for others to quickly understand whether they might be users, developers or auditors. This might seem insignificant but trust me, it goes a long way. &lt;/p&gt;

&lt;p&gt;Till next time, Fren^-^.&lt;/p&gt;

&lt;p&gt;Here’s the commented version of &lt;strong&gt;EtherWallet&lt;/strong&gt; I promised.&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 &amp;gt;=0.4.0 &amp;lt;0.9.0;

/// @title Ether Wallet
/// @author smart contract programmer
/// @notice A Smart contract for storing Ether
/// @dev No deposit function, Ether has to be sent directly
contract EtherWallet {
    /// @notice Get the address of owner of the contract
    /// @dev Owner is set at deployment
    /// @return owner the deployer of the Ether wallet
    address public owner;

    /// @notice Deploys the contract
    /// @dev sets `msg.sender` as the owner of the contract
    constructor() {
        owner = msg.sender;
    }

    /// @notice Receive Ether
    /// @dev alternative would be a deposit function
    receive() external payable {}

    /// @notice Withdraw Ether from the contract
    /// @dev Function can only be called by the deployer of the contract
    /// @param _amount the amount
    function withdraw(uint256 _amount) external {
        require(msg.sender == owner, "Caller is not owner");
        payable(msg.sender).transfer(_amount);
    }

    /// @notice Get the balance of the contract
    /// @dev Uses address(this) to get the address of the contract and .balance to get the balance of the address
    /// @return balance the amount of Ether in the contract
    function getBalance() external view returns (uint256) {
        return address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ethereum</category>
      <category>blockchain</category>
      <category>solidity</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Demystifying Foundry</title>
      <dc:creator>Perelyn-sama</dc:creator>
      <pubDate>Mon, 20 Jun 2022 01:15:07 +0000</pubDate>
      <link>https://dev.to/perelynsama/demystifying-foundry-5ao5</link>
      <guid>https://dev.to/perelynsama/demystifying-foundry-5ao5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you're like me, you probably like trying out different technologies just for the fun of it and maybe you've been hearing everyone talk about Foundry and you don't know what it is or have the time to research about it or even go through the &lt;a href="https://book.getfoundry.sh/index.html" rel="noopener noreferrer"&gt;foundry book&lt;/a&gt;.  Well, if you fit into this category, this article is for you because I'll be talking about what foundry is, why I think you should use it and how you can use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Foundry?
&lt;/h2&gt;

&lt;p&gt;According to the foundry book:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Foundry is a smart contract development toolchain.&lt;br&gt;
Foundry manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command line.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might be thinking isn't that what hardhat and truffle already do? Yeah, you're right but Foundry does it faster and personally, I think the best thing about Foundry is the tests. Usually, when working with hardhat or truffle, during smart contracts development you need to write tests for your smart contracts and they're usually in JavaScript. Some may enjoy this but not everyone enjoys switching between languages as they work and let's not forget the fact that not everyone knows JavaScript. &lt;/p&gt;

&lt;p&gt;Foundry takes a different approach with writing tests for smart contracts, tests are written in solidity and developers are given the ability to manipulate their test EVM environment to an unbelievable length and are also introduced to various types of tests, not just plain old unit tests. Foundry introduces smart contract developers to other types of testing such as Fuzzing, differential testing and other goodies that the Foundry team are still working on behind the scenes. &lt;/p&gt;

&lt;p&gt;At this point, you might be thinking “yeah, it has fancy tests so what?”, whether you thought of that or not I would like to stress the importance of testing smart contracts. At this early stage of blockchain technology in general, we are faced with various issues and one of the most annoying ones is security exploits on smart contracts which leads to the loss of millions of dollars in various protocols. These exploits push the reality of mass adoption of blockchain technologies further and further away and as blockchain developers, we do not want that because definitely when the ecosystem grows, so do we and that's why it is important for you to have a tool like Foundry in your arsenal to help you test your smart contracts beyond what other existing tools provide.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;So now that you understand the usefulness of Foundry, it's time you add it to your development toolkit. &lt;/p&gt;

&lt;h3&gt;
  
  
  On Linux and macOS
&lt;/h3&gt;

&lt;p&gt;If you use Linux or macOS, you can get Foundry by the following the steps:&lt;br&gt;
&lt;strong&gt;Install the latest release by using foundry up&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the easiest option for Linux and macOS users.&lt;br&gt;
Open your terminal and type in 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;curl &lt;span class="nt"&gt;-L&lt;/span&gt; https://foundry.paradigm.xyz | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will download &lt;code&gt;foundryup&lt;/code&gt;. Then install Foundry by running:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;If everything goes well, you will now have three binaries at your disposal: forge, cast and anvil.&lt;/p&gt;

&lt;h3&gt;
  
  
  On Windows, build from source
&lt;/h3&gt;

&lt;p&gt;If you use Windows, you need to build from source to get Foundry.&lt;/p&gt;

&lt;p&gt;Download and run &lt;code&gt;rustup-init&lt;/code&gt; from &lt;a href="https://rustup.rs/" rel="noopener noreferrer"&gt;rustup.rs&lt;/a&gt;. It will start the installation in a console.&lt;/p&gt;

&lt;p&gt;After this, run the following to build Foundry from source:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
cargo &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--git&lt;/span&gt; https://github.com/foundry-rs/foundry foundry-cli anvil &lt;span class="nt"&gt;--bins&lt;/span&gt; &lt;span class="nt"&gt;--locked&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To update from source, run the same command again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;Now that we have Foundry installed on our machine, let’s do something with it and I think building, testing, deploying and verifying an ERC20 contract with Foundry would not hurt. &lt;/p&gt;

&lt;p&gt;We will start by creating a Foundry project, first, go into the directory you want to create your project in then run the following commands:&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;# creates foundry project&lt;/span&gt;
forge init hey-foundry

&lt;span class="c"&gt;# navigates into the project&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;hey-foundry 

&lt;span class="c"&gt;# shows you the content of the project&lt;/span&gt;
tree &lt;span class="nt"&gt;-L&lt;/span&gt; 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These commands should create a new Foundry project called &lt;code&gt;hey-foundry&lt;/code&gt; , enter the directory of the project then show you the contents of your project. &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%2Fzcgg4j6yaodeuxtkyzhr.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%2Fzcgg4j6yaodeuxtkyzhr.png" alt="foundry-project-structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our newly generated Foundry project, we have three directories and one &lt;code&gt;toml&lt;/code&gt; file which are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;src&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;test&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;script&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;lib&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;foundry.toml&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let’s talk briefly about what each of them is for, the &lt;code&gt;src&lt;/code&gt; directory is where our smart contracts are kept, the &lt;code&gt;test&lt;/code&gt; directory is where we write our tests, the &lt;code&gt;script&lt;/code&gt; directory is where we store how solidity scripts, the &lt;code&gt;lib&lt;/code&gt; directory is similar to a &lt;code&gt;node_modules&lt;/code&gt; directory and it contains dependencies we install and finally we have the &lt;code&gt;foundry.toml&lt;/code&gt; file, which we use to configure the behaviour of our foundry project.&lt;/p&gt;

&lt;p&gt;💡 Note: the &lt;code&gt;src&lt;/code&gt;, &lt;code&gt;test&lt;/code&gt;and &lt;code&gt;script&lt;/code&gt; directories both have one file each in them by default called &lt;code&gt;Contract.sol&lt;/code&gt;, &lt;code&gt;Contract.t.sol&lt;/code&gt;  and &lt;code&gt;Contract.s.sol&lt;/code&gt; respectively. The &lt;code&gt;lib&lt;/code&gt; directory has a folder in it too called &lt;code&gt;forge-std&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, you should open the &lt;code&gt;hey-foundry&lt;/code&gt; project in your preferred code editor.&lt;/p&gt;

&lt;p&gt;Instead of writing our own ERC20 contract from scratch, let’s use the OpenZeppelin implementation and what’s a better way to use their contracts than installing their contracts into our project, so we can import them for our usage. Usually, with other toolchains, we would need to use the &lt;strong&gt;Node Package Manager&lt;/strong&gt;(NPM) to install the OpenZeppelin contracts but with Foundry, we have the privilege to use something that is faster and also has less baggage. To install the OpenZeppelin contracts into our project we would need to 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;forge &lt;span class="nb"&gt;install &lt;/span&gt;OpenZeppelin/openzeppelin-contracts 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you check the &lt;code&gt;lib&lt;/code&gt; directory you should you should see the &lt;code&gt;openzeppelin-contracts&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Now open the &lt;code&gt;src&lt;/code&gt;  directory and delete the &lt;code&gt;Contract.sol&lt;/code&gt;  file and create a new file called &lt;code&gt;MyToken.sol&lt;/code&gt;, the contents of this file should look like this:&lt;/p&gt;

&lt;p&gt;Now open the &lt;code&gt;src&lt;/code&gt; directory and rename the &lt;code&gt;Contract.sol&lt;/code&gt; file as &lt;code&gt;MyToken.sol&lt;/code&gt; then copy and paste the code below into it.&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.13;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
    address public owner;

    constructor() ERC20("My Token", "MTKN") {
        owner = msg.sender;
        _mint(msg.sender, 1000 * 10**decimals());
    }

    function mint(address account, uint256 amount) public {
        require(msg.sender == owner, "Only Owner can mint");
        _mint(account, amount);
    }

    function burn(address account, uint256 amount) public {
        require(msg.sender == owner, "Only Owner can burn");
        _burn(account, amount);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your code editor you should be getting an error on line 4 &lt;code&gt;import "@openzeppelin/contracts/token/ERC20/ERC20.sol";&lt;/code&gt; , this is because your code editor cannot find the path to the file we want to import. We can fix this by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;forge remappings &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; remappings.txt 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should create a remappings.txt file which contains the path to all the dependencies that we have installed in our project and we can use this file to direct other files that want to import our dependencies on where they can get it. The contents of this file should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openzeppelin-contracts/=lib/openzeppelin-contracts/
forge-std/=lib/forge-std/src/
ds-test/=lib/forge-std/lib/ds-test/src/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And remember our &lt;code&gt;MyToken.sol&lt;/code&gt; file is trying to import the OpenZeppelin ERC20 contract from &lt;code&gt;@openzeppelin&lt;/code&gt; which would work if we had installed the dependency with &lt;strong&gt;NPM&lt;/strong&gt; but since we installed it with &lt;code&gt;forge&lt;/code&gt; we have to do a little tweaking. Don’t worry it’s not that complex all we have to do is change the line that has &lt;code&gt;openzeppelin-contracts/=lib/openzeppelin-contracts/&lt;/code&gt; in the &lt;code&gt;remappings.txt&lt;/code&gt; file to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@openzeppelin/=lib/openzeppelin-contracts/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you followed the steps correctly, you should notice that the error has been resolved. This is not the only way to solve this error but for simplicity's sake, we’ll just go with this for now.&lt;/p&gt;

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

&lt;p&gt;Now that we are done writing our smart contract, it is ideal we test it. So as we did before, rename the &lt;code&gt;Contract.t.sol&lt;/code&gt; file as &lt;code&gt;MyToken.t.sol&lt;/code&gt;. Once you’re done copy and paste the following code into the file.&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: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "../src/MyToken.sol";

contract MyTokenTest is Test {
    // Target contract
    MyToken myToken;

    // Actors
    address owner;
    address ZERO_ADDRESS = address(0);
    address spender = address(1);
    address user = address(2);

    // Test params
    string public name = "My Token";
    string public symbol = "MTKN";

    uint256 public decimals = 18;
    uint256 amount = 1000 * 1e18;
    uint256 public initialSupply = 1000 * 1e18;

    event Transfer(address indexed from, address indexed to, uint256 value);

    //  =====   Set up  =====

    function setUp() public {
        owner = address(this);
        myToken = new MyToken();
    }

    //  =====   Initial state   =====

    /**
     *  @dev Tests the relevant initial state of the contract.
     *
     *  - Token name is 'My Token'
     *  - Token symbol is 'MTKN'
     *  - Token initail supply is '1000000000000000000000'
     *  - Token decimals is '18'
     */
    function testinitialState() public {
        // assert if the corrent name was used
        assertEq(myToken.name(), name);

        // assert if the correct symbol was used
        assertEq(myToken.symbol(), symbol);

        // assert if the correct initial supply was set
        assertEq(myToken.totalSupply(), initialSupply);

        // assert if the correct decimal was set
        assertEq(myToken.decimals(), decimals);
    }

    //  =====   Functionality tests   =====

    /// @dev Test `mint`

    // Only Owner should be able to mint
    function testFailUnauthorizedMinter() public {
        vm.prank(user);
        myToken.mint(user, amount);
    }

    // Should not be able to mint to the zero address
    function testFailMintToZeroAddress() public {
        vm.prank(owner);
        myToken.mint(ZERO_ADDRESS, amount);
    }

    // Should increase total supply
    function testIncreseTotalSupply() public {
        uint256 expectedSupply = initialSupply + amount;
        vm.prank(owner);
        myToken.mint(owner, amount);
        assertEq(myToken.totalSupply(), expectedSupply);
    }

    // Should increase recipient balance
    function testIncreaseRecipientBalance() public {
        vm.prank(owner);
        myToken.mint(user, amount);
        assertEq(myToken.balanceOf(user), amount);
    }

    // Should emit Transfer event
    function testEmitTransferEventForMint() public {
        vm.expectEmit(true, true, false, true);
        emit Transfer(ZERO_ADDRESS, user, amount);
        vm.prank(owner);
        myToken.mint(user, amount);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don’t worry if some of it seems weird to you now, by the end of the tutorial you should have a good understanding of what is happening in the code above but for now just know we’re writing unit tests for our &lt;code&gt;MyToken.sol&lt;/code&gt; contract.&lt;/p&gt;

&lt;p&gt;We’ll now go through the code together line by line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "forge-std/Test.sol";
import "../src/MyToken.sol";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Over here, we import two files, the contract we want to test - &lt;code&gt;MyToken.sol&lt;/code&gt; and a contract containing utilities we’ll need while testing our contract - &lt;code&gt;Test.sol&lt;/code&gt; from the forge standard library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract MyTokenTest is Test {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create a contract named &lt;code&gt;MyTokenTest&lt;/code&gt; and inherit the &lt;code&gt;Test&lt;/code&gt; contract from &lt;code&gt;"forge-std/Test.sol"&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // Target contract
    MyToken myToken;

    // Actors
    address owner;
    address ZERO_ADDRESS = address(0);
    address spender = address(1);
    address user = address(2);

    // Test params
    string public name = "My Token";
    string public symbol = "MTKN";

    uint256 public decimals = 18;
    uint256 amount = 1000 * 1e18;
    uint256 public initialSupply = 1000 * 1e18;

    event Transfer(address indexed from, address indexed to, uint256 value);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of these guys as the variables we need to make our tests work.&lt;br&gt;
&lt;/p&gt;

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

    function setUp() public {
        owner = address(this);
        myToken = new MyToken();
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an important function that is used when writing Foundry tests, as the name implies we are setting up our test. We usually do things like deploying our contracts in it and that’s what we’re doing here, we’re also assigning an address to the &lt;code&gt;owner&lt;/code&gt; state variable. The address of the &lt;code&gt;MyTokenTest&lt;/code&gt; is set as the owner because it is the address that deploys it.&lt;br&gt;
&lt;/p&gt;

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

    /**
     *  @dev Tests the relevant initial state of the contract.
     *
     *  - Token name is 'My Token'
     *  - Token symbol is 'MTKN'
     *  - Token initail supply is '1000000000000000000000'
     *  - Token decimals is '18'
     */
    function testinitialState() public {
        // assert if the corrent name was used
        assertEq(myToken.name(), name);

        // assert if the correct symbol was used
        assertEq(myToken.symbol(), symbol);

        // assert if the correct initial supply was set
        assertEq(myToken.totalSupply(), initialSupply);

        // assert if the correct decimal was set
        assertEq(myToken.decimals(), decimals);
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Over here we are testing for the initial state of the &lt;code&gt;MyToken&lt;/code&gt; contract we just deployed in the &lt;code&gt;setUp&lt;/code&gt; function. Since we assigned the deployed contract to the identifier &lt;code&gt;myToken&lt;/code&gt;, we can invoke functions in it by writing &lt;code&gt;myToken.&amp;lt;function_name&amp;gt;()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;ERC20 tokens usually have a name, symbol, initial supply and decimals, so here we’re trying to check if the correct ones were set. We do this by using the &lt;code&gt;assertEq&lt;/code&gt; function, which checks if the two parameters passed into it are equal.&lt;/p&gt;

&lt;p&gt;💡 Note: We get the &lt;code&gt;assertEq&lt;/code&gt; function from the &lt;code&gt;Test.sol&lt;/code&gt; file we imported earlier.&lt;/p&gt;



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

    /// @dev Test `mint`

    // Only Owner should be able to mint
    function testFailUnauthorizedMinter() public {
        vm.prank(user);
        myToken.mint(user, amount);
    }

    // Should not be able to mint to the zero address
    function testFailMintToZeroAddress() public {
        vm.prank(owner);
        myToken.mint(ZERO_ADDRESS, amount);
    }

    // Should increase total supply
    function testIncreseTotalSupply() public {
        uint256 expectedSupply = initialSupply + amount;
        vm.prank(owner);
        myToken.mint(owner, amount);
        assertEq(myToken.totalSupply(), expectedSupply);
    }

    // Should increase recipient balance
    function testIncreaseRecipientBalance() public {
        vm.prank(owner);
        myToken.mint(user, amount);
        assertEq(myToken.balanceOf(user), amount);
    }

    // Should emit Transfer event
    function testEmitTransferEventForMint() public {
        vm.expectEmit(true, true, false, true);
        emit Transfer(ZERO_ADDRESS, user, amount);
        vm.prank(owner);
        myToken.mint(user, amount);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this part of the code, we test the &lt;code&gt;mint&lt;/code&gt; function for different cases in a series of functions. The comments I added at the top of each function should provide enough information about what they do, although the lines where I used &lt;code&gt;vm.prank()&lt;/code&gt; and &lt;code&gt;vm.expectEmit()&lt;/code&gt; may seem alien to you. &lt;/p&gt;

&lt;p&gt;These two functions are examples of what are known as cheat codes in Foundry, we use them to manipulate the test EVM environment to procure real-life situations, and there are cheat codes to manipulate &lt;code&gt;block.timestamp&lt;/code&gt; , &lt;code&gt;block.number&lt;/code&gt;, the nonce and so &lt;a href="https://book.getfoundry.sh/cheatcodes/index.html" rel="noopener noreferrer"&gt;much more&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;So what are &lt;code&gt;vm.prank()&lt;/code&gt; and &lt;code&gt;vm.expectEmit()&lt;/code&gt; used for? &lt;code&gt;vm.prank()&lt;/code&gt; is used to change the &lt;code&gt;msg.sender&lt;/code&gt; of the next call and you pass the address you want the next call to use as &lt;code&gt;msg.sender&lt;/code&gt; into &lt;code&gt;vm.prank&lt;/code&gt; as a parameter while &lt;code&gt;vm.expectEmit&lt;/code&gt; on the other hand, helps us check if the next event emitted in a function is what we expect.&lt;/p&gt;

&lt;p&gt;💡 For more information on Foundry cheat codes, check out this &lt;a href="https://book.getfoundry.sh/cheatcodes/index.html" rel="noopener noreferrer"&gt;link&lt;/a&gt;. For more information on &lt;code&gt;vm.prank()&lt;/code&gt; and &lt;code&gt;vm.expectEmit()&lt;/code&gt; check out the following links respectively, &lt;a href="https://book.getfoundry.sh/cheatcodes/prank.html" rel="noopener noreferrer"&gt;vm.prank()&lt;/a&gt; and &lt;a href="https://book.getfoundry.sh/cheatcodes/expect-emit.html" rel="noopener noreferrer"&gt;vm.expectEmit()&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that you understand the code, let’s run it. We run tests in Foundry with 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;forge &lt;span class="nb"&gt;test&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running &lt;code&gt;forge test&lt;/code&gt; , you should see this in your terminal:&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%2Fnce1u13awwinn7pxthw3.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%2Fnce1u13awwinn7pxthw3.png" alt="foundry-test"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And with that, we’re done testing our contract. Congrats for making it this far, here have some coffee ☕.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying and Verifying
&lt;/h2&gt;

&lt;p&gt;Prior to now, we would run extensive commands in our terminal to deploy our contracts but recently the Foundry team introduced something phenomenal and that’s solidity scripting. If you have experience using hardhat you should be familiar with using scripts to deploy smart contracts, that’s practically the same thing we’re doing here but instead of writing our scripts in JavaScript, we write them in solidity instead. &lt;/p&gt;

&lt;p&gt;Now, that we have the theoretical part out of the way let’s get coding. First of all, we would need to rename the &lt;code&gt;Contract.s.sol&lt;/code&gt; file that’s in the &lt;code&gt;script&lt;/code&gt; directory to &lt;code&gt;MyToken.s.sol&lt;/code&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: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Script.sol";
import "../src/MyToken.sol";

contract MyTokenScript is Script {
    function run() external {
        vm.startBroadcast();
        MyToken myToken = new MyToken();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our script is pretty similar to a normal solidity smart contract so it easy to understand. Basically, it is a contract that has one function called &lt;code&gt;run&lt;/code&gt; and that’s where we deploy our contract, the only thing that is alien here is &lt;code&gt;vm.startBroadcast&lt;/code&gt;. All &lt;code&gt;vm.startBroadcast&lt;/code&gt; does is &lt;em&gt;create transactions that can later be signed and sent onchain.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Now that we have written our script, it is time we run this code so we can deploy our contract to the Rinkeby testnet but before we do that we have to create a &lt;code&gt;.env&lt;/code&gt; file storing our Rinkeby RPC URL, private key and Etherscan key . The &lt;code&gt;.env&lt;/code&gt; file should have the following variables provided:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RINKEBY_RPC_URL=
PRIVATE_KEY=
ETHERSCAN_KEY=
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we run the following commands :&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;# To give our shell access to our environment variables&lt;/span&gt;
&lt;span class="nb"&gt;source&lt;/span&gt; .env

&lt;span class="c"&gt;# To deploy and verify our contract&lt;/span&gt;
forge script script/MyToken.s.sol:MyScript &lt;span class="nt"&gt;--rpc-url&lt;/span&gt; &lt;span class="nv"&gt;$RINKEBY_RPC_URL&lt;/span&gt;  &lt;span class="nt"&gt;--private-key&lt;/span&gt; &lt;span class="nv"&gt;$PRIVATE_KEY&lt;/span&gt; &lt;span class="nt"&gt;--broadcast&lt;/span&gt; &lt;span class="nt"&gt;--verify&lt;/span&gt; &lt;span class="nt"&gt;--etherscan-api-key&lt;/span&gt; &lt;span class="nv"&gt;$ETHERSCAN_KEY&lt;/span&gt; &lt;span class="nt"&gt;-vvvv&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%2F5t53nwwbtx2tprc0ouwu.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%2F5t53nwwbtx2tprc0ouwu.png" alt="contract-deployed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well done! You’ve just deployed the &lt;code&gt;MyToken&lt;/code&gt; contract to the Rinkeby network and that’s not all you did, you also verified the contract on Etherscan all with one command.&lt;/p&gt;

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

&lt;p&gt;Although Foundry is a new tool and some developers might be sceptical to use it because of how comfortable they have become with previously existing tools if you’re one of them I would ask you to give Foundry a shot because Foundry is a tool that is beyond its time and is essential to the general advancement of the Smart contract development community. &lt;/p&gt;

&lt;p&gt;In this tutorial, we only covered the surface of Foundry. To learn more about Foundry you could try going through the &lt;a href="https://book.getfoundry.sh/index.html" rel="noopener noreferrer"&gt;Foundry book&lt;/a&gt; and use Foundry more. Thanks for reading till next time Fren^-^.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>ethereum</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Getting the frequency of an element in an array in JavaScript</title>
      <dc:creator>Perelyn-sama</dc:creator>
      <pubDate>Sun, 06 Dec 2020 12:22:34 +0000</pubDate>
      <link>https://dev.to/perelynsama/getting-the-frequency-of-an-element-in-an-array-in-javascript-lgf</link>
      <guid>https://dev.to/perelynsama/getting-the-frequency-of-an-element-in-an-array-in-javascript-lgf</guid>
      <description>&lt;p&gt;First of all, what is frequency?&lt;br&gt;
According to Wikipedia, it's" the rate at which something occurs over a particular period of time or in a given sample."&lt;br&gt;
There are multiple ways to get the frequency of an element in an array. In this article, we'll focus on one of those ways by using some of the higher-order functions in Javascript.&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's begin
&lt;/h2&gt;

&lt;p&gt;For instance, if we have an array of letters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;letters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&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;To get the frequency of each element, we will first need to create an empty &lt;code&gt;object&lt;/code&gt; named &lt;code&gt;count&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we'll utilize one those higher order functions we were talking about:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;letters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;]&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="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;++&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="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;]&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;What the above code does is simply check, if each element of &lt;code&gt;letters&lt;/code&gt; is present in &lt;code&gt;count&lt;/code&gt;. Initially &lt;code&gt;count&lt;/code&gt; is empty and does not contain any of &lt;code&gt;letters&lt;/code&gt; elements, so when the ternary operator runs on an element of &lt;code&gt;letters&lt;/code&gt; to check if its present in &lt;code&gt;count&lt;/code&gt; for the first time, it's going to work with the falsy condition which is to assign &lt;code&gt;letters&lt;/code&gt; elements to 1 in the &lt;code&gt;count&lt;/code&gt; object.&lt;br&gt;
So the count object will then become:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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="c1"&gt;// {"a" : 1, "b" : 1, "c" : 1};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Afterward, when the ternary operator checks if &lt;code&gt;count&lt;/code&gt; has an element of &lt;code&gt;letters&lt;/code&gt; that has already been checked, the ternary operator will then run the truthy condition which is to increment the value of the elements in &lt;code&gt;letters&lt;/code&gt; if they are already present in &lt;code&gt;count&lt;/code&gt;, so &lt;code&gt;count&lt;/code&gt; will now become:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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="c1"&gt;// {"a" : 3, "b" : , "c" : 2};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And just like that ladies and gentlemen, we've been able to get the frequency of each element of &lt;code&gt;letters&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;Here's the complete code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;letters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="nx"&gt;letters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;]&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="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;++&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="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;]&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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="c1"&gt;// {"a" : 1, "b" : 1, "c" : 1}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;P.S: This code can be used for both strings and numbers.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>The Programmer's path</title>
      <dc:creator>Perelyn-sama</dc:creator>
      <pubDate>Wed, 25 Nov 2020 18:46:14 +0000</pubDate>
      <link>https://dev.to/perelynsama/the-programmer-s-path-54j9</link>
      <guid>https://dev.to/perelynsama/the-programmer-s-path-54j9</guid>
      <description>&lt;p&gt;The path of becoming a programmer is easier than most newbies would think, Yes the road is long, tedious, and too often tests our patience and confidence, it still is a profession that anyone with enough zeal and passion can overcome. Most times all we need to do is just start the race than thinking about all the wrong things that could go wrong. I'm not a stranger to anxiety, I know how it feels to not know what to do in the right situation that's why I've come up with these tips to help you on your journey.&lt;/p&gt;

&lt;h2&gt;
  
  
  Join Freecodecamp
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Raf4SFiC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l9xpzv2b9lbwy6eg6lan.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Raf4SFiC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l9xpzv2b9lbwy6eg6lan.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
A lot of the experienced developers, we know today started with FCC(Freecodecamp) because of its rich content and a magical way of presenting its content to users. Its beginner-friendly, free, and has a large community that is tolerant towards newbies and their questions. I would advise every beginner to start at FCC because its curriculum really simplifies coding concepts and it also provides a wide range of topics like HTML, CSS, JavaScript, Frameworks and so much more. Here's their website  &lt;a href="https://www.freecodecamp.org/"&gt;Freecodecamp&lt;/a&gt; check it out, I'm sure you will love it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get a code editor
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yl-i7ahp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/anr03kzg17ffnjxfqb3s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yl-i7ahp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/anr03kzg17ffnjxfqb3s.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
The most essential magic sauce to every programmer's growth is practice! And you can't practice coding without a code editor. So now what code editor should you use? I would suggest Visual Studio Code, it's a code editor that was made by Mircosoft and in my opinion, it provides the most beginner-friendly environment any code newbie could ask for. Here's the link to download their software &lt;a href="https://code.visualstudio.com/"&gt;Vs Code&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Join a dev community!
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uyld9tbQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uz8a7id5af0kip9msran.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uyld9tbQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uz8a7id5af0kip9msran.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Being around people who are interested in the same thing you are is a blessing that everyone should enjoy. When you are in a community you learn things that will help boost your programming skills and even when you become a pro, your community can help you get job offers! One of my favorite dev communities is the twitter community, there you can get expos from senior developers and a limitless supply of resources that will surely help your growth. Now you would think how do I join the Twitter community well that's very easy all you need to do is follow senior developers' accounts, along with tech topics, and most importantly interact and ask questions. A common practice in the twitter developers community is the 100 days of code challenge, in this challenge you have to code at least an hour every day and post what you learned each day along with the tag #100DaysOfCode. Here's a link to expand your knowledge on the challenge &lt;a href="https://www.100daysofcode.com/"&gt;link&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SUdCKtJJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t8m219ugrrav9w5kf9fz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SUdCKtJJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t8m219ugrrav9w5kf9fz.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Many times on this path, you'll want to quit and just give up but just keep on and don't feel that you're weak, everyone gets this feeling and has to learn to move past it individually. Another thing to remember is to always be consistent, try to learn something new every single day and you'll find your skill improving rapidly! Good luck.&lt;/p&gt;

</description>
      <category>startup</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>vscode</category>
    </item>
  </channel>
</rss>
