<?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: Swaraj Papadkar</title>
    <description>The latest articles on DEV Community by Swaraj Papadkar (@swaraj70).</description>
    <link>https://dev.to/swaraj70</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%2F362858%2F33356ef8-956c-4d12-8611-90379af52643.JPG</url>
      <title>DEV Community: Swaraj Papadkar</title>
      <link>https://dev.to/swaraj70</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/swaraj70"/>
    <language>en</language>
    <item>
      <title>Learning to Code Blockchain DApps from CryptoZombies 🧟‍♂️</title>
      <dc:creator>Swaraj Papadkar</dc:creator>
      <pubDate>Sun, 24 Oct 2021 06:22:54 +0000</pubDate>
      <link>https://dev.to/swaraj70/learning-to-code-blockchain-dapps-from-cryptozombies-3ff4</link>
      <guid>https://dev.to/swaraj70/learning-to-code-blockchain-dapps-from-cryptozombies-3ff4</guid>
      <description>&lt;p&gt;The popularity of Blockchain technology is increasing day by day and so are the reasons to learn it.&lt;/p&gt;

&lt;p&gt;This is the first article of the series &lt;a href="https://swaraj70.com/series/cryptozombies"&gt;Learning to Code Blockchain DApps from CryptoZombies&lt;/a&gt; in which I will be documenting my journey as I go through all the lessons present in the &lt;a href="https://cryptozombies.io/"&gt;CryptoZombies&lt;/a&gt; curriculum.&lt;/p&gt;

&lt;p&gt;Before getting into the lessons, Let's first understand what is CryptoZombies?&lt;/p&gt;

&lt;h2&gt;
  
  
  So, What is CryptoZombies?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://cryptozombies.io/"&gt;CryptoZombies&lt;/a&gt; is a free resource for aspiring blockchain dApp developers in which users get to learn all the important concepts of dApp development and the Solidity programming language by building their own NFT-style Zombie game.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CryptoZombies is an interactive school that teaches you all things technical about blockchains. Learn to make smart contracts in Solidity by making your own crypto-collectibles game.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why CryptoZombies?
&lt;/h2&gt;

&lt;p&gt;When learning something new I generally try to find resources which are well structured and which walks you through the concepts in a progressive way, so that I am clear with the basics before jumping into more advanced concepts and after browsing through CryptoZombies curriculum I found out it follows a similar structure.&lt;/p&gt;

&lt;p&gt;The best part about learning from CryptoZombies is that it's completely browser based and you don't have to install or configure anything.&lt;/p&gt;

&lt;p&gt;So, after this little introduction of CryptoZombies let's try to understand the concepts explained in Lesson 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 1 - Making the Zombie Factory
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Chapter 1: Lesson Overview
&lt;/h3&gt;

&lt;p&gt;In this lesson, we are supposed to build a &lt;code&gt;Zombie Factory&lt;/code&gt; to make a lot of zombies for our army and in order to do that we have to,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;maintain a database of all zombies in our army.&lt;/li&gt;
&lt;li&gt;make a function for creating new zombies.&lt;/li&gt;
&lt;li&gt;make sure each zombie has a random and unique appearance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N9cQEMcI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633782320758/XW7liU4rc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N9cQEMcI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633782320758/XW7liU4rc.png" alt="lesson1a.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But before we do that, We should first understand how zombie DNA works as the zombie's appearance is based on its &lt;code&gt;Zombie DNA&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Zombie DNA&lt;/code&gt; is just a simple 16-digit integer, like:&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;So, just like real DNA different parts of this number maps to different traits. The first 2 digits map to the zombie's head type, the second 2 digits to the zombie's eyes, etc. The zombie can have only 7 different types of heads and 8, 3 are the first two digits in the above Zombie DNA sample, to map that to the zombie's head type, we do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;83 % 7 + 1 = 7

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

&lt;/div&gt;



&lt;p&gt;Which means this Zombie would have the 7th zombie head type (The Santa hat).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9ZPR_5Bb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633789360829/GbgcvJoVF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9ZPR_5Bb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633789360829/GbgcvJoVF.png" alt="headgene7.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we now have a better understanding of what &lt;code&gt;Zombie DNA&lt;/code&gt; is, let's see how we can build our own Zombie army.&lt;/p&gt;

&lt;h3&gt;
  
  
  Chapter 2: Contracts
&lt;/h3&gt;

&lt;p&gt;Solidity's code is encapsulated in &lt;strong&gt;contracts&lt;/strong&gt; and to start creating our Zombie army, we have to create a base &lt;code&gt;Contract&lt;/code&gt; called &lt;code&gt;ZombieFactory&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  What are Contracts?
&lt;/h4&gt;

&lt;p&gt;A contract is the fundamental building block of Ethereum applications — all variables and functions belong to a contract, and this will be the starting point of all your projects.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

}

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

&lt;/div&gt;



&lt;p&gt;In the above code, the first line &lt;code&gt;pragma solidity &amp;gt;=0.5.0 &amp;lt;0.6.0;&lt;/code&gt; is known as a &lt;code&gt;Version Pragma&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  What is a Version Pragma?
&lt;/h4&gt;

&lt;p&gt;All solidity source code starts with a "version pragma". It simply tells the source code is written for which solidity version. This is to prevent issues with future compiler versions potentially introducing changes that would break the code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our contract uses solidity version which is greater than or equal to 0.5.0 and less than 0.6.0. Therefore,&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 &amp;gt;=0.5.0 &amp;lt;0.6.0;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Chapter 3: State Variables and Integeres
&lt;/h3&gt;

&lt;p&gt;Now, as our Zombie DNA is going to be determined by a 16-digit number we will create a &lt;code&gt;State Variable&lt;/code&gt; named &lt;code&gt;dnaDigits&lt;/code&gt; and set it equal to &lt;code&gt;16&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  What are State Variables?
&lt;/h4&gt;

&lt;p&gt;Variables which are permanently stored in contract storage is known as State Variables. These variables are permanently written to the Ethereum blockchain. Think of it as storing values in a database.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We will be using the &lt;code&gt;uint&lt;/code&gt; data type while defining our state variable &lt;code&gt;dnaDigits&lt;/code&gt; which means its value must not be negative.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: &lt;code&gt;uint&lt;/code&gt; is a data type which stands for unsigned integer. There's also an &lt;code&gt;int&lt;/code&gt; data type for signed integers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Chapter 4: Math Operations
&lt;/h3&gt;

&lt;p&gt;To make sure our Zombie's DNA is only 16 characters we will make another &lt;code&gt;uint&lt;/code&gt; named &lt;code&gt;dnaModulus&lt;/code&gt; and set it equal to &lt;code&gt;10^dnaDigits&lt;/code&gt;, since &lt;code&gt;dnaDigits = 16&lt;/code&gt;. That way we can later use the modulus operator &lt;code&gt;%&lt;/code&gt; to shorten an integer to 16 digits.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Math in solidity is pretty straightforward and the operations are almost the same as in most programming languages.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our solidity code should now look something 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;pragma solidity &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

    // This will be stored permanently in the blockchain
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; // equal to 10^16

}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Chapter 5: Structs
&lt;/h3&gt;

&lt;p&gt;Just like us humans, our zombies will also have multiple properties like &lt;code&gt;name (a string)&lt;/code&gt;, &lt;code&gt;dna (a uint)&lt;/code&gt; and to club these properties into one single data type solidity provides us with a tool known as &lt;code&gt;structs&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  What are Structs?
&lt;/h4&gt;

&lt;p&gt;Structs allow us to create more complicated data types that have multiple properties.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As we are going to want to create some zombies for our app, we will create a &lt;code&gt;struct&lt;/code&gt; named &lt;code&gt;Zombie&lt;/code&gt; with two properties &lt;code&gt;name (a string)&lt;/code&gt; and &lt;code&gt;dna (a uint)&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;pragma solidity &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

    // This will be stored permanently in the blockchain
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; // equal to 10^16

    // Struct
    struct Zombie {
        string name;
        uint dna;
    }

}

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: String is a data type which can store a sequence of characters. Ex:&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string greeting = "Hello world!"

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

&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Chapter 6: Arrays
&lt;/h3&gt;

&lt;p&gt;In order to store an army of zombies we will need a collection of zombies and in solidity, &lt;code&gt;arrays&lt;/code&gt; are used to create a collection of something.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  What are Arrays?
&lt;/h4&gt;

&lt;p&gt;Arrays are used to store a collection of something. In solidity, there are two types of arrays - &lt;code&gt;Fixed arrays&lt;/code&gt; and &lt;code&gt;Dynamic arrays&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Array with a fixed length of 2 uint elements:
uint[2] fixedArray;

// A dynamic Array - has no fixed size, can keep growing:
uint[] dynamicArray;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We can also declare an array as &lt;code&gt;public&lt;/code&gt; which enables other contracts to read from this array, but can not write to this array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Person struct
struct Person {
    uint age;
    string name;
}

// Dynamic Public Array named people which can store a lot of persons.
Person[] public people;

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

&lt;/div&gt;



&lt;p&gt;So, we will now create a public array of &lt;code&gt;Zombie struct&lt;/code&gt;, and name it &lt;code&gt;zombies&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;pragma solidity &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

    // This will be stored permanently in the blockchain
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; // equal to 10^16

    // Zombie struct
    struct Zombie {
        string name;
        uint dna;
    }

    // zombies array
    Zombie[] public zombies;

}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Chapter 7: Function Declarations
&lt;/h3&gt;

&lt;p&gt;Now, we will create a &lt;code&gt;public function&lt;/code&gt; named &lt;code&gt;createZombie&lt;/code&gt; for creating more zombies. It will take two parameters: &lt;code&gt;_name (a string)&lt;/code&gt;, and &lt;code&gt;_dna (a uint)&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;pragma solidity &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

    // This will be stored permanently in the blockchain
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; // equal to 10^16

    // Zombie struct
    struct Zombie {
        string name;
        uint dna;
    }

    // zombies array
    Zombie[] public zombies;

    // createZombie public function
    function createZombie(string memory _name, uint _dna) public {

    }

}

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: It's convention (but not required) to start function parameter variable names with an underscore (_) in order to differentiate them from global variables.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For now the body of the function is empty. Note that we're specifying the function visibility as &lt;code&gt;public&lt;/code&gt;. We're also providing instructions about where the _name variable should be stored- &lt;code&gt;in memory&lt;/code&gt;. This is required for all reference types such as arrays, structs, mappings, and strings.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are two ways in which you can pass an argument to a Solidity function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By value, which means that the Solidity compiler creates a new copy of the parameter's value and passes it to your function. This allows your function to modify the value without worrying that the value of the initial parameter gets changed.&lt;/li&gt;
&lt;li&gt;By reference, which means that your function is called with a reference to the original variable. Thus, if your function changes the value of the variable it receives, the value of the original variable gets changed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Chapter 8: Working With Structs and Arrays
&lt;/h3&gt;

&lt;p&gt;In the last chapter, we created an empty function named &lt;code&gt;createZombie&lt;/code&gt; with two parameters: &lt;code&gt;_name (a string)&lt;/code&gt;, and &lt;code&gt;_dna (a uint)&lt;/code&gt;. So, in this chapter we will try to fill the function so it can actually create a &lt;code&gt;zombie&lt;/code&gt; whose name and dna will come from the function arguments and add it to the &lt;code&gt;zombies&lt;/code&gt; array.&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 &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

    // This will be stored permanently in the blockchain
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; // equal to 10^16

    // Zombie struct
    struct Zombie {
        string name;
        uint dna;
    }

    // zombies array
    Zombie[] public zombies;

    // createZombie public function
    function createZombie(string memory _name, uint _dna) public {
        // Create a new zombie and push it at the end of zombies array
        zombies.push(Zombie(_name, _dna));
    }

}

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: array.push() adds something to the end of the array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Chapter 9: Private / Public Functions
&lt;/h3&gt;

&lt;p&gt;In solidity, functions are &lt;code&gt;public&lt;/code&gt; by default which means anyone or any contract can call our contract's function and execute its code this can make our contract vulnerable to attacks.&lt;/p&gt;

&lt;p&gt;Our contract's &lt;code&gt;createZombie&lt;/code&gt; function is currently &lt;code&gt;public&lt;/code&gt; and anyone could call it and create a new Zombie in our contract which is not desirable. So, we will modify our &lt;code&gt;createZombie&lt;/code&gt; function and make it private. This will only allow other functions within our contract to call &lt;code&gt;createZombie&lt;/code&gt; function and add new zombies to the &lt;code&gt;zombies&lt;/code&gt; array.&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 &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

    // This will be stored permanently in the blockchain
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; // equal to 10^16

    // Zombie struct
    struct Zombie {
        string name;
        uint dna;
    }

    // zombies array
    Zombie[] public zombies;

    // createZombie PRIVATE function
    function _createZombie(string memory _name, uint _dna) private {
        // Create a new zombie and push it at the end of zombies array
        zombies.push(Zombie(_name, _dna));
    }

}

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As with function parameters, it's convention to start private function names with an underscore (_).&lt;/li&gt;
&lt;li&gt;It is a good practice to mark your functions as private by default, and then only make those functions public which you want to expose to the world.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Chapter 10: More on Functions
&lt;/h3&gt;

&lt;p&gt;In this chapter, we have to create a &lt;code&gt;private view function&lt;/code&gt; called &lt;code&gt;_generateRandomDna&lt;/code&gt; which takes a single &lt;code&gt;string&lt;/code&gt; parameter &lt;code&gt;_str&lt;/code&gt; and it should return a &lt;code&gt;uint&lt;/code&gt;. We will fill the function body in the next chapter.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;return&lt;/code&gt; value in solidity functions is a value which gets returned when the function is called. In our case, a &lt;code&gt;uint&lt;/code&gt; will be returned.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;view&lt;/code&gt; is a function modifier for a function that only views the data present in our smart and does not modify the data.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Solidity also contains pure functions, which means the function does not access any data in the app and doesn't even read from the state of the app. Its return value depends only on its function parameters.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

    // This will be stored permanently in the blockchain
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; // equal to 10^16

    // Zombie struct
    struct Zombie {
        string name;
        uint dna;
    }

    // zombies array
    Zombie[] public zombies;

    // createZombie PRIVATE function
    function _createZombie(string memory _name, uint _dna) private {
        // Create a new zombie and push it at the end of zombies array
        zombies.push(Zombie(_name, _dna));
    }

    // create private view function _generateRandomDna
    function _generateRandomDna(string memory _str) private view returns (uint) {

    }

}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Chapter 11: Keccak256 and Typecasting
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Keccak256&lt;/code&gt; is a hash function in Ethereum which is a version of &lt;a href="https://en.wikipedia.org/wiki/SHA-3"&gt;&lt;code&gt;SHA3&lt;/code&gt;&lt;/a&gt;. A hash function basically converts an input into a random 256-bit hexadecimal number and even if there is just a slight change in the input it will cause a large change in the hash. We will use it for pseudo-random number generation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;keccak256&lt;/code&gt; expects a single parameter of type bytes. This means that we have to "pack" any parameters before calling &lt;code&gt;keccak256&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Typecasting&lt;/code&gt; is a process to convert between data types. For Eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uint8 a = 5;
uint b = 6;
// throws an error because a * b returns a uint, not uint8:
uint8 c = a * b;
// we have to typecast b as a uint8 to make it work:
uint8 c = a * uint8(b);

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

&lt;/div&gt;



&lt;p&gt;Now, ln our &lt;code&gt;_generateRandomDna&lt;/code&gt; function we have to call the &lt;code&gt;keccak256&lt;/code&gt; hash function by passing &lt;code&gt;abi.encodePacked(_str)&lt;/code&gt; as its parameter to generate a pseudo-random hexadecimal, after that typecast it as a &lt;code&gt;uint&lt;/code&gt;, and finally store the result in a &lt;code&gt;uint&lt;/code&gt; called &lt;code&gt;rand&lt;/code&gt;. Also we want our DNA to be only 16 digits long so we will use the &lt;code&gt;dnaModulus&lt;/code&gt; variable that we created earlier in the lesson and in the end return the result.&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 &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

    // This will be stored permanently in the blockchain
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; // equal to 10^16

    // Zombie struct
    struct Zombie {
        string name;
        uint dna;
    }

    // zombies array
    Zombie[] public zombies;

    // createZombie PRIVATE function
    function _createZombie(string memory _name, uint _dna) private {
        // Create a new zombie and push it at the end of zombies array
        zombies.push(Zombie(_name, _dna));
    }

    // create private view function _generateRandomDna
    function _generateRandomDna(string memory _str) private view returns (uint) {
        // Keccak256 and Typecasting
        uint rand = uint(keccak256(abi.encodePacked(_str)));
        return rand % dnaModulus;
    }

}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Chapter 12: Putting It Together
&lt;/h3&gt;

&lt;p&gt;To complete our random Zombie generator we will create a &lt;code&gt;public&lt;/code&gt; function named &lt;code&gt;createRandomZombie&lt;/code&gt; that takes one parameter named &lt;code&gt;_name&lt;/code&gt; (a string), then inside this function we will call &lt;code&gt;_generateRandomDna&lt;/code&gt; by passing &lt;code&gt;_name&lt;/code&gt; as its parameter and store it in a &lt;code&gt;uint&lt;/code&gt; named &lt;code&gt;randDna&lt;/code&gt;. Next, we will call the &lt;code&gt;_createZombie&lt;/code&gt; function and pass &lt;code&gt;_name&lt;/code&gt; and &lt;code&gt;randDna&lt;/code&gt; as its parameters for creating a new zombie.&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 &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

    // This will be stored permanently in the blockchain
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; // equal to 10^16

    // Zombie struct
    struct Zombie {
        string name;
        uint dna;
    }

    // zombies array
    Zombie[] public zombies;

    // createZombie PRIVATE function
    function _createZombie(string memory _name, uint _dna) private {
        // Create a new zombie and push it at the end of zombies array
        zombies.push(Zombie(_name, _dna));
    }

    // create private view function _generateRandomDna
    function _generateRandomDna(string memory _str) private view returns (uint) {
        // Keccak256 and Typecasting
        uint rand = uint(keccak256(abi.encodePacked(_str)));
        return rand % dnaModulus;
    }

    // create public function createRandomZombie
    function createRandomZombie(string memory _name) public {
        uint randDna = _generateRandomDna(_name);
        _createZombie(_name, randDna);
    }

}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Chapter 13: Events
&lt;/h3&gt;

&lt;p&gt;We want an event to let our front-end know every time a new zombie was created, so the app can display it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Events are a way for a contract to communicate that something happened on the blockchain to our app front-end, which can be 'listening' for certain events and take action when they happen.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, to do that we will declare an event called &lt;code&gt;NewZombie&lt;/code&gt; and pass &lt;code&gt;zombieId&lt;/code&gt; (a uint), &lt;code&gt;name&lt;/code&gt; (a string), &lt;code&gt;dna&lt;/code&gt; (a uint) as its parameters and then modify the &lt;code&gt;_createZombie&lt;/code&gt; function to fire the &lt;code&gt;NewZombie&lt;/code&gt; event after adding the new Zombie to our &lt;code&gt;zombies&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;We are also going to need the zombie's id and &lt;code&gt;array.push()&lt;/code&gt; returns a &lt;code&gt;uint&lt;/code&gt; of the new length of the array - and since the first item in an array has index 0, &lt;code&gt;array.push() - 1&lt;/code&gt; will be the index of the zombie we just added. So, we will store the result of &lt;code&gt;zombies.push() - 1&lt;/code&gt; in a &lt;code&gt;uint&lt;/code&gt; called &lt;code&gt;id&lt;/code&gt;, and then use this in the NewZombie event.&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 &amp;gt;=0.5.0 &amp;lt;0.6.0;

contract ZombieFactory {

    // declare our NewZombie event
    event NewZombie(uint zombieId, string name, uint name);

    // This will be stored permanently in the blockchain
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; // equal to 10^16

    // Zombie struct
    struct Zombie {
        string name;
        uint dna;
    }

    // zombies array
    Zombie[] public zombies;

    // createZombie PRIVATE function
    function _createZombie(string memory _name, uint _dna) private {
        // Create a new zombie, 
        // push it at the end of zombies array and 
        // get the id of the zombie we just added.
        uint id = zombies.push(Zombie(_name, _dna)) - 1;
        // fire NewZombie event
        emit NewZombie(id, _name, _dna);
    }

    // create private view function _generateRandomDna
    function _generateRandomDna(string memory _str) private view returns (uint) {
        // Keccak256 and Typecasting
        uint rand = uint(keccak256(abi.encodePacked(_str)));
        return rand % dnaModulus;
    }

    // create public function createRandomZombie
    function createRandomZombie(string memory _name) public {
        uint randDna = _generateRandomDna(_name);
        _createZombie(_name, randDna);
    }

}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Chapter 14: Web3.js
&lt;/h3&gt;

&lt;p&gt;We have completed our &lt;code&gt;solidity&lt;/code&gt; contract. Now we need to write a javascript frontend that interacts with the contract.&lt;/p&gt;

&lt;p&gt;Ethereum has a Javascript library called &lt;code&gt;Web3.js&lt;/code&gt;. In later lessons we will go over in depth how to deploy a contract and set up &lt;code&gt;Web3.js&lt;/code&gt;. For now CryptoZombies have provided us with some sample code that will help us understand how &lt;code&gt;Web3.js&lt;/code&gt; would interact with our deployed contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Here's how we would access our contract:
var abi = /* abi generated by the compiler */
var ZombieFactoryContract = web3.eth.contract(abi)
var contractAddress = /* our contract address on Ethereum after deploying */
var ZombieFactory = ZombieFactoryContract.at(contractAddress)
// `ZombieFactory` has access to our contract's public functions and events

// some sort of event listener to take the text input:
$("#ourButton").click(function(e) {
  var name = $("#nameInput").val()
  // Call our contract's `createRandomZombie` function:
  ZombieFactory.createRandomZombie(name)
})

// Listen for the `NewZombie` event, and update the UI
var event = ZombieFactory.NewZombie(function(error, result) {
  if (error) return
  generateZombie(result.zombieId, result.name, result.dna)
})

// take the Zombie dna, and update our image
function generateZombie(id, name, dna) {
  let dnaStr = String(dna)
  // pad DNA with leading zeroes if it's less than 16 characters
  while (dnaStr.length &amp;lt; 16)
    dnaStr = "0" + dnaStr

  let zombieDetails = {
    // first 2 digits make up the head. We have 7 possible heads, so % 7
    // to get a number 0 - 6, then add 1 to make it 1 - 7. Then we have 7
    // image files named "head1.png" through "head7.png" we load based on
    // this number:
    headChoice: dnaStr.substring(0, 2) % 7 + 1,
    // 2nd 2 digits make up the eyes, 11 variations:
    eyeChoice: dnaStr.substring(2, 4) % 11 + 1,
    // 6 variations of shirts:
    shirtChoice: dnaStr.substring(4, 6) % 6 + 1,
    // last 6 digits control color. Updated using CSS filter: hue-rotate
    // which has 360 degrees:
    skinColorChoice: parseInt(dnaStr.substring(6, 8) / 100 * 360),
    eyeColorChoice: parseInt(dnaStr.substring(8, 10) / 100 * 360),
    clothesColorChoice: parseInt(dnaStr.substring(10, 12) / 100 * 360),
    zombieName: name,
    zombieDescription: "A Level 1 CryptoZombie",
  }
  return zombieDetails
}

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

&lt;/div&gt;



&lt;p&gt;What this javascript code then does is take the values generated in &lt;code&gt;zombieDetails&lt;/code&gt; above, and use some browser-based javascript magic to swap out the images and apply CSS filters. For Eg:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cfoj3azD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1635054122395/nURnt964M.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cfoj3azD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1635054122395/nURnt964M.png" alt="swaraj70.png"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;We have now successfully completed Lesson 1 of CryptoZombies. In order to complete Lesson 1 of CryptoZombies we have learnt -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to write Ethereum smart contracts.&lt;/li&gt;
&lt;li&gt;Basics of Ethereum's smart contract programming language - Solidity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are interested in learning how to create your own blockchain dapps on Ethereum like me for FREE, &lt;a href="https://cryptozombies.io/"&gt;CryptoZombies&lt;/a&gt; could be a good place to start.&lt;/p&gt;

&lt;p&gt;Thank you for reading this article. I am learning more about web 3.0 and blockchain development, if you're interested in being a part of this learning journey feel free to follow me here or on &lt;a href="https://twitter.com/swaraj70"&gt;twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Let's understand the difference between Web 1, Web 2 and Web 3.</title>
      <dc:creator>Swaraj Papadkar</dc:creator>
      <pubDate>Sat, 02 Oct 2021 10:40:12 +0000</pubDate>
      <link>https://dev.to/swaraj70/let-s-understand-the-difference-between-web-1-web-2-and-web-3-5a82</link>
      <guid>https://dev.to/swaraj70/let-s-understand-the-difference-between-web-1-web-2-and-web-3-5a82</guid>
      <description>&lt;p&gt;Have you ever thought about how the web has evolved from what it was before 10 - 20 years ago VS the web that we are experiencing today?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Web is a commonly used term for World Wide Web which is a collection of webpages found on a network of computers known as the internet. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this article, we will try to understand the history of the web, how the web has evolved and where is it going next. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/xT5LMtHmmaTZgm9ELC/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/xT5LMtHmmaTZgm9ELC/giphy.gif" alt="History lesson"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Web 1.0
&lt;/h2&gt;

&lt;p&gt;It all started in the year 1993 - 1994 when the web became available for the general everyday use. The people who used web during this era were mostly consumers. The content creators were comparatively fewer, typically developers or researchers who built websites to share information in mainly text or image format. This was the very first iteration of the web which lasted approximately from 1991 to 2004, and is now commonly known as Web 1.0.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/dV3GXudtLAbTi/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/dV3GXudtLAbTi/giphy.gif" alt="Old Computer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The content on websites during this era was mostly &lt;code&gt;static&lt;/code&gt; or in simple terms &lt;code&gt;read-only&lt;/code&gt;, and websites didn't have much interactivity at all.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Web 1.0 is also known as the Static Web. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Web 2.0
&lt;/h2&gt;

&lt;p&gt;In the year 2004 it all changed as Web 2.0 was introduced. Web 2.0 refers to the version of the internet most of us know today. With this version of the web, interactivity between users and websites have increased, web pages have become dynamic i.e. it can serve different content for different users, and with the introduction of social media users don't have to be a developer to participate in the process of content creation.&lt;/p&gt;

&lt;p&gt;Think of it like this, users who were only able to view images on websites are now able to post and share their lives with the world where millions of people can see it, interact with it, and comment on it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/RK4evFEy79PFqFJ7Ks/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/RK4evFEy79PFqFJ7Ks/giphy.gif" alt="Social Media"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Web 2.0 is great and we enjoy using it on a daily basis (YouTube, Twitter, Facebook, etc). But, there are few flaws in it.&lt;/p&gt;

&lt;p&gt;All the websites built using Web 2.0 are centralized which means behind every website or app there is a central official institution which has the full power over the content that we post, share and comment on. &lt;/p&gt;

&lt;p&gt;This means although the content is created by many and consumed by many, it is owned only by a few&lt;br&gt;
and in order to gain profits from this information collected from the users, these institutions can exploit this information for personalized advertisements or can even sell personal data of users.&lt;/p&gt;

&lt;p&gt;Another major flaw with applications using Web 2.0 is with security and privacy. As big amount of user data is owned by a single entity, it can become a prime target for hacking and data breaches.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Web 2.0 is also known as Social Web.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To solve this shortcomings Web 3.0 came into existence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web 3.0
&lt;/h2&gt;

&lt;p&gt;Web 3.0 introduces decentralization which roughly means that the content created by users will be separated and stored on a large number of computers which are not owned by one company and instead each user can own a small amount of data so that it is fully transparent and no official institution can have full control over it.&lt;/p&gt;

&lt;p&gt;Because of this, even though the websites or applications are owned by a certain company, the content created by users will always belong to the users and users will be able to own their data.&lt;/p&gt;

&lt;p&gt;There are different ways for achieving decentralization like decentralized networks of many peer to peer nodes (servers) and blockchain.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Web 3.0 is also known as Decentralized Web.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Blockchain is the most well-known solution and &lt;a href="https://bitcoin.org/en/"&gt;Bitcoin&lt;/a&gt; and &lt;a href="https://ethereum.org/en/"&gt;Ethereum&lt;/a&gt; are the two major blockchains in existence.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/k4n9RZ6c9Gc3eOvBKc/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/k4n9RZ6c9Gc3eOvBKc/giphy.gif" alt="Bitcoin Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we hear about blockchain, generally cryptocurrency comes to our mind. This is because it plays a very important role as it is used to provide financial incentives, known as &lt;strong&gt;Tokens&lt;/strong&gt; for anyone who participates in creating, governing, contributing to, improving one of the applications or even for being early adapters of the applications.&lt;/p&gt;

&lt;p&gt;Tokens are easy to transfer and can be sent directly from the provider to the user without any need of other third party services like Stripe or Paypal. Users can also make a living by participating in the protocol in various ways, in both technical and non-technical levels.&lt;/p&gt;

&lt;p&gt;The famous NFTs are also tokens but like paintings they vary in their values and are unique. If you're interested in getting into it you can check out &lt;a href="https://opensea.io/"&gt;Opensea&lt;/a&gt; for more information.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This token economy is exactly what the web was created for in the first place. It's direct. People reward people for providing a service or participating in a better world. There is no malicious data tracking or manipulation. No exploitation, no incessant ads, no algorithms designed to be addictive. No reliance on huge profit-driven companies. Just people interacting with people. That's web3. - @&lt;a href="https://twitter.com/CatMcGeeCode"&gt;Cat McGee&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;Most applications that we are using today are based on Web 2.0, but applications built using Web 3.0 have slowly started picking up the pace and are beginning to enter huge industries like &lt;a href="https://audius.co/"&gt;music&lt;/a&gt;, &lt;a href="https://livepeer.org/"&gt;video streaming&lt;/a&gt;, and &lt;a href="https://axieinfinity.com/"&gt;gaming&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you are interested in getting involved and exploring blockchain applications as well, the only thing you will require is a crypto wallet like &lt;a href="https://metamask.io/"&gt;Metamask&lt;/a&gt; and you are good to go.&lt;/p&gt;

&lt;p&gt;This is my very first article and I hope I was able to provide you with some valuable information through this article. Thanks for reading. I am learning more about web 3.0 and web development, if you're interested in being a part of this learning journey feel free to follow me here or on &lt;a href="https://twitter.com/swaraj70"&gt;twitter&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  References and Further Readings
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.freecodecamp.org/news/what-is-web3/"&gt;What is Web3?&lt;/a&gt; by @&lt;a href="https://twitter.com/dabit3"&gt;Nader Dabit&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://blog.mcgee.cat/why-web3-matters"&gt;Why Web3 matters&lt;/a&gt; by @&lt;a href="https://twitter.com/CatMcGeeCode"&gt;Cat McGee&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://twitter.com/i_pesovski/status/1442773748185276417"&gt;A twitter thread on history of web&lt;/a&gt; by &lt;a href="https://twitter.com/i_pesovski"&gt;Ivica Pesovski&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.oreilly.com/library/view/what-is-the/9781492072973/ch01.html"&gt;What is token economy?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ethereum.org/en/developers/docs/web2-vs-web3/"&gt;Web2 VS Web3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
