<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Raj  on Medium]]></title>
        <description><![CDATA[Stories by Raj  on Medium]]></description>
        <link>https://medium.com/@raj.8?source=rss-3f6ba79c8cdc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GV1za7gyb5nGEzFN</url>
            <title>Stories by Raj  on Medium</title>
            <link>https://medium.com/@raj.8?source=rss-3f6ba79c8cdc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 02:35:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raj.8/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Blockchain Concepts: Build a Voting DAPP using Truffle, React, Ganache-CLI, Solidity (Part 2)]]></title>
            <link>https://medium.com/@raj.8/blockchain-concepts-build-a-voting-dapp-using-truffle-react-ganache-cli-solidity-part-2-ba72776c1565?source=rss-3f6ba79c8cdc------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba72776c1565</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[dapps]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Raj ]]></dc:creator>
            <pubDate>Mon, 15 Jun 2020 16:53:50 GMT</pubDate>
            <atom:updated>2020-06-15T16:53:50.981Z</atom:updated>
            <content:encoded><![CDATA[<p>So, the story continues… If you haven’t read Part 1. You can read from <a href="https://link.medium.com/UQPSOsIpg7"><strong>here</strong></a><strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*Ia3F7IGs0r9RGv4vFmh7dw.png" /></figure><p>We had already written smart contract, setup web3 (actually it was already done for us by truffle) and we made our Home component.</p><p>The backend part is done and it is time for frontend part. We will be making different components and stylise them using css. How to make your DAPP look attractive is your work. I will tell you how to interact with smart contract. So, without wasting time, lets get going.</p><h3>Components</h3><p>So, before we more forward, we should have clear idea what we want to make. So, this would be the structure -</p><blockquote><strong>‘/’ — ‘Home.js’ </strong>-Main web page we will land to</blockquote><blockquote><strong>‘/AddCandidate’ — ‘AddCandidate.js’</strong> -Page to add candidate, only accessible by admin</blockquote><blockquote><strong>‘/CandidateDetails’ — ‘CandidateDetails.js’</strong> -View all candidate details</blockquote><blockquote><strong>‘/RequestVoter’ — ‘RequestVoter.js’ </strong>-Apply for voter by details</blockquote><blockquote><strong>‘/VerifyVoter’ — ‘VerifyVoter.js’</strong> -verify voter on the basis of details, only accessible by admin</blockquote><blockquote><strong>‘/Vote’ — ‘Vote.js’</strong> -page where you can vote to candidate of your own constituency</blockquote><blockquote><strong>‘/Result’ — ‘Result.js’</strong> -page where you can see results after election is over, only accessible by admin</blockquote><blockquote><strong>‘/Admin’ — ‘Admin.js’</strong> -Admin portal page where you can start or end election</blockquote><blockquote><strong>‘</strong><a href="https://github.com/maasoomraj/Microsoft_2k19/blob/master/client/src/components/Navigation.js"><strong>Navigation.js</strong></a><strong>’</strong> -Navigation component for a voter</blockquote><blockquote><strong>‘</strong><a href="https://github.com/maasoomraj/Microsoft_2k19/blob/master/client/src/components/NavigationAdmin.js"><strong>NavigationAdmin.js</strong></a><strong>’ </strong>-Navigation component for admin</blockquote><p>Before making these components, we will makes <strong><em>routes</em></strong> for these in <strong>/src/index.js</strong> , so goto<strong> /src/index.js </strong>and make routes as -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*MPvYYgHO8Y7T3xPD9uqwvQ.png" /></figure><p>We have not made these components, but mentioned in <strong>index.js</strong>, so move to <strong>/src/components </strong>and let’s make these components.</p><h3>Navigation.js</h3><p>So, first we will make navigation component. It will be header in all end-points. So code this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*3udVDjhccxtzE6KYTyziaQ.png" /></figure><p>In navigation, which is accessible by voter, we don’t want some admin stuff to be shown, so we will mention them in NavigationAdmin.js</p><h3>NavigationAdmin.js</h3><p>This navigation will be accessible if user is admin. It will have Admin, VerifyVoter etc. Code as -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*zJvHQJ6LoQk1-97C7JTjVw.png" /></figure><p>Now, we will use either Navigation or NavigationAdmin depending on the address is owner or not.</p><h3>Home.js</h3><p>We have already done alot in Home in <a href="https://link.medium.com/UQPSOsIpg7"><strong>part-1.</strong></a><strong> </strong>If you haven’t read it then you won’t get things clearly hereafter.</p><p>Let’s import the components as -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*hqfsVS3-99guGTVwPLoh2A.png" /></figure><p>Now, we have imported them, check if <strong>isOwner</strong> or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*XEiDhvxOlJggb1hhxLWh_Q.png" /></figure><p>I have made my own css, to make it a bit attractive (css part is totally yours).</p><p>If you have not started the server you can run as -</p><pre>cd client<br>npm run start<br>(ganache-cli should be running in background)</pre><p>Now, you should see something like this in your browser if you are not <strong>admin.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EwARfCWPEfP-C8fORibFjA.png" /></figure><p>While if you are <strong>admin</strong>, you will see something like this -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QEHs81ScjuHCz--xAJ2NWQ.png" /></figure><p>You can clearly see the difference between admin homepage and voter’s homepage. So, let’s make other components.</p><h3>AddCandidate.js</h3><p>So, let’s make AddCandidate component. First of all import necessary files and modules.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*7-ncCNidq9lnnya9deAmFw.png" /></figure><p>We will use <strong>FormGroup</strong> to take <strong>input</strong> from user.</p><p>Next, we will make class <strong>AddCandidate</strong> and in <strong>constructor</strong>, set the initial <strong><em>state</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*DzNPXs-bMREGmWrOIFa4Xg.png" /></figure><p>We will take <strong>input</strong> for name, party, manifesto, constituency by user.</p><p>Next, we will set up our web3 instance and account details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*dZ77bx6lJiaAH5VvR0cFnA.png" /></figure><p>You can get to know well, if you read <a href="https://link.medium.com/UQPSOsIpg7"><strong>part 1</strong></a> of this series. Next, we will handle the <strong>error</strong> using <strong>catch</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*0mi2GNA8bUDWvRl5WZEwDw.png" /></figure><p>Now, we made our <strong>componentDidMount()</strong>, so we can come to our main render function.</p><p>In <strong>render</strong> function we will check if <strong>web3</strong> is loaded properly or not, if not -It will return <strong>Error Page.</strong></p><p>Next, we know that <strong>AddCandidate</strong> can be accessed only by <strong>admin</strong>, so we will check if <strong>isOwner</strong> is true or not, if false -It will return <strong>Error Page</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*9RcxcAdma6P_fJSW_2ZJyA.png" /></figure><p>You can see I’ve added navigation, so that the page doesn&#39;t terminate and user can browse to other pages.</p><p>Next, come to main <strong>return</strong> function where we want to <strong>input</strong> data from user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*cGvbClKhQfrKEfIKTo5CRQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*Ed0AkAN72yUBPVcOQIWMqw.png" /></figure><p>First you have <strong>div</strong> with “Add Candidate” text, then you have <strong>navigation</strong> bar, then you have four <strong>inputs</strong> and then you have a <strong>button</strong> which handles submission.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*WddVMSgitRFKYGTbYUlkfQ.png" /></figure><p>Above is Event Handler for inputs, which updates the state.</p><p>Now, comes the main part of this page, which is when you click <strong>Button</strong> it should take all data from <strong>states</strong> and pass it as arguments to <strong>addCandidate </strong>function in smart contract.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*IUdED-uiGihZQyCcEUqitA.png" /></figure><p>Let’s understand this code. When <strong>addCandidate</strong> function is invoked, it sends a <strong>transaction</strong> from your <strong>account</strong> with some <strong>gas</strong> against <strong><em>instance-methods-&lt;name of function&gt;</em></strong><em>.</em><strong><em> </em></strong>We reload the page at last so that all the states come back to initial state.</p><p>Now, we should be able to <strong>AddCandidate</strong> from admin’s address.</p><p>So, run the server and go to ‘/<strong>AddCandidate’ </strong>and you should see something like this (I have filled it with dummy data and sent the transaction from admin address)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tEGC8__VK70dWUmuiUsEVA.png" /></figure><p>When I hit the Add button, you should see a <strong>transaction</strong> in your <strong>ganache-cli</strong> terminal like this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/955/1*r7yw2219QYjNWx5Z5kEggw.png" /></figure><p>If you got this, means you have done everything correctly for now. But this is very inconvenient method where every time you need to check your <strong>ganache-cli</strong> if the <strong>transaction</strong> was complete or not. So, let’s make this simple using <strong>Metamask</strong> — chrome extension.</p><p>Search for <strong>Metamask</strong> in <strong><em>chrome web store</em></strong> and install it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3UVVejls7VJGe9E74mq8dA.png" /></figure><p>After you install it, a fox icon should appear in your browser. <strong>Click</strong> on it and import choose <strong>No, I already have a seed phrase.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*4HqckesbqqgPqn6SWmbtRw.png" /></figure><p>Now, go to <strong>ganache-cli</strong> terminal and scroll up where you started <strong>ganache</strong> and copy the <strong>mnemonic phrase.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hwRd1GEwCYnrVq5ZxpohVw.png" /></figure><p>Paste this phrase in <strong>metamask </strong>and choose a <strong>password. </strong>You should have your metamask working now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*FoaIhATi_RInc8DRlLJGPA.png" /></figure><p>Now, open <strong>metamask</strong> and go to <strong>Custom RPC</strong> in settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/1*lLBBjQdNEgikivfc2Kek7A.png" /></figure><p>In <strong>Custom RPC</strong>, add following details and save.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*atpZF3_ElZL4WGGvBGtLHw.png" /></figure><p>Now, choose “ganache-cli” as your network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/362/1*bGV0KuUxdHisQelesuVRvw.png" /></figure><p>Now, <strong><em>run the server </em></strong>again and try to add one more dummy candidate.</p><p>You should see a connection request from <strong>metamask</strong> as -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*zmNJYPnXx389dy8FvwQ9cQ.png" /></figure><p>Select <strong>connect</strong> and now you should be able to transact from <strong>metamask</strong>.</p><p>As far as now, you have made 4 components where you have Navigation, NavigationAdmin , Home and AddCandidate.</p><p>In the upcoming story, we will make the rest components.</p><p><strong><em>And if you are reading this line,</em></strong> <em>means you read all and I want to thank you for bearing with me.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba72776c1565" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Blockchain Voting DAPP using Truffle, React, Ganache-cli, Solidity]]></title>
            <link>https://medium.com/@raj.8/blockchain-voting-dapp-using-truffle-react-ganache-cli-solidity-b92b973c7a36?source=rss-3f6ba79c8cdc------2</link>
            <guid isPermaLink="false">https://medium.com/p/b92b973c7a36</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[truffle-boxes]]></category>
            <category><![CDATA[solidity-tutorial]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Raj ]]></dc:creator>
            <pubDate>Fri, 12 Jun 2020 18:02:05 GMT</pubDate>
            <atom:updated>2020-06-15T16:55:06.111Z</atom:updated>
            <content:encoded><![CDATA[<h3>Blockchain Concepts: Build a Voting DAPP using Truffle, React, Ganache-CLI, Solidity (Part 1)</h3><p>This “blockchain Decentralised App” thing has been common nowadays and if you are reading this then you must want to make one decentralised app of yours.</p><p>Blockchain is interesting stuff, but also complex. If you are new to <strong><em>What is blockchain? </em></strong>then this is not the right place to start with. First, get to know the blockchain technology.</p><p>You can refer given below links to know the basics of blockchain and why do we need it? It’s my collection and sure there might be other good videos too, but I learned the basics from them.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FSSo_EIwHSd4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSSo_EIwHSd4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FSSo_EIwHSd4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2932a409612280b76a35f72196523626/href">https://medium.com/media/2932a409612280b76a35f72196523626/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F3xGLc-zz9cA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D3xGLc-zz9cA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F3xGLc-zz9cA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f8c935b8a19d25314ea20d60479e097/href">https://medium.com/media/3f8c935b8a19d25314ea20d60479e097/href</a></iframe><p>So, if you are at this position means you know the basics of blockchain and what are the concepts behind it.</p><p>So, get ready as we are now going to start making a decentralized app. For example, I will be showing how to make a Voting App <strong><em>(say VoteChain)</em></strong></p><p>Prerequisite -</p><pre>Node v12.17.0 or higher<br>Npm v6.14.4 or higher</pre><p>First of all you need to install <strong><em>truffle </em></strong>and<strong> <em>ganache-cli</em></strong>.<strong> </strong>Go to terminal and type-</p><pre>npm install -g truffle<br>npm install -g ganache-cli</pre><p>Now in a separate terminal open ganache-cli and never end it as it will act as our network for this project.</p><pre>ganache-cli</pre><p>It should run without any errors and console <strong>Listening on 127.0.0.1:8545</strong>. If everything is working fine move ahead.</p><p>We will use <strong><em>truffle react box. </em></strong>You can visit<strong><em> </em></strong><a href="https://www.trufflesuite.com/">https://www.trufflesuite.com/</a> for more boxes and how to use them.</p><figure><img alt="https://www.trufflesuite.com/boxes/react" src="https://cdn-images-1.medium.com/max/1024/1*DM_jovLUlzGeOC-647Uz0A.png" /></figure><p>In terminal -</p><pre>truffle unbox react<br>truffle develop<br>compile<br>migrate</pre><p>In new terminal (remember not to terminate the previous terminal) -</p><pre>cd client<br>npm run start</pre><p>If everything is correct, your web-browser will automatically open up like this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BNU8w-JkDn27rxqbci6WrQ.png" /></figure><p><strong>Congrats</strong>, you have reached …mmm….I guess 20% of this DAPP thing.</p><p>Now let’s move ahead and look at what truffle make for us. In your code editor open your working folder. You can use any editor but personally, I love VS Code, so I’ll use it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/222/1*9bfJa_hp0gFca_EMLZ5QIg.png" /></figure><p>Let’s know what are these folders and what do they intend to contain</p><blockquote><strong>client/</strong>: React App files for Frontend</blockquote><blockquote><strong>client/public/</strong>: contain static web files</blockquote><blockquote><strong>client/src/</strong>: source files for react app</blockquote><blockquote><strong>contracts/</strong>: store original codes of the smart contract.</blockquote><blockquote><strong>migrations/</strong>: instructions for deploying the smart contract(s) in the “contracts” folder.</blockquote><blockquote><strong>test/</strong>: tests for your smart contracts</blockquote><blockquote><strong>truffle-config.js</strong>: configuration file.</blockquote><blockquote><strong>truffle.js</strong>: configuration document for windows user. (not shown in mac)</blockquote><h3>Smart Contracts</h3><p>Before writing our first smart contract, we need to make some changes in truffle-config.js file. The default solidity compiler version is 0.5.16 but we will change it to 0.4.17 for our convenience. In editor go to truffle-config.js and <strong>select all and delete </strong>and paste -</p><pre>const path = require(&quot;path&quot;);</pre><pre>module.exports = {</pre><pre>// See &lt;http://truffleframework.com/docs/advanced/configuration&gt;</pre><pre>// to customize your Truffle configuration!</pre><pre>   contracts_build_directory: path.join(__dirname, &quot;client/src/contracts&quot;),</pre><pre>networks: {</pre><pre>   development: {</pre><pre>      network_id: &quot;*&quot;,</pre><pre>      host: &#39;localhost&#39;,</pre><pre>      port: 8545,</pre><pre>      gas: 6721975,</pre><pre>      gasPrice: 20000000000</pre><pre>   }</pre><pre>},</pre><pre>compilers: {</pre><pre>   solc: {</pre><pre>       version: &quot;0.4.17&quot;,</pre><pre>       optimizer: {</pre><pre>          enabled: true,</pre><pre>          runs: 200</pre><pre>       }</pre><pre>   }<br>}</pre><pre>};</pre><p>Your truffle-config.js should look like this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pyfp0eDyUks8YPhKpNKV9g.png" /></figure><p>Then go to contracts/Migrations.sol and <strong>change</strong></p><ol><li>pragma solidity &gt;=0.4.21 &lt;0.7.0; <strong>To </strong>pragma solidity ^0.4.17;</li></ol><p>2. constructor() public { owner = msg.sender; } <strong>To </strong>function Migrations() public { owner = msg.sender; }</p><p>Now, your Migrations.sol should look like -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KV6CPeMvD6R3bN0s8LCfGA.png" /></figure><p>In contracts we have a file <strong>SimpleStorage.sol </strong>we do not need this file as we will write our own contract so just <strong>delete </strong>this file.</p><p>In contracts folder make a file named <strong>MasoomContract.sol</strong> (Masoom is my name and not a keyword…Lol !) It’s just a filename so you can name it anything. Then write some code in it as -</p><pre>pragma solidity ^0.4.17;</pre><pre>contract MasoomContract {</pre><pre>address public owner;</pre><pre>uint candidateCount;</pre><pre>uint voterCount;</pre><pre>bool start;</pre><pre>bool end;</pre><pre>// Constructor</pre><pre>function MasoomContract() public {</pre><pre>   owner = msg.sender;</pre><pre>   candidateCount = 0;</pre><pre>   voterCount = 0;</pre><pre>   start = false;</pre><pre>   end = false;</pre><pre>}</pre><pre>function getOwner() public view returns(address) {</pre><pre>   return owner;</pre><pre>}</pre><pre>// Only Admin can access</pre><pre>modifier onlyAdmin() {</pre><pre>   require(msg.sender == owner);</pre><pre>   _;</pre><pre>}</pre><pre>struct Candidate{</pre><pre>   string name;</pre><pre>   string party;</pre><pre>   string manifesto;</pre><pre>   uint voteCount;</pre><pre>   uint constituency;</pre><pre>   uint candidateId;</pre><pre>}</pre><pre>mapping(uint =&gt; Candidate) public candidateDetails;<br></pre><pre>// Only admin can add candidate</pre><pre>function addCandidate(string _name, string _party, string _manifesto, uint _constituency) public onlyAdmin {</pre><pre>   Candidate memory newCandidate = Candidate({</pre><pre>     name : _name,</pre><pre>     party : _party,</pre><pre>     manifesto : _manifesto,</pre><pre>     voteCount : 0,</pre><pre>     constituency : _constituency,</pre><pre>     candidateId : candidateCount</pre><pre>   });</pre><pre>   candidateDetails[candidateCount] = newCandidate;</pre><pre>   candidateCount += 1;</pre><pre>}</pre><pre>// get total number of candidates</pre><pre>function getCandidateNumber() public view returns (uint) {</pre><pre>   return candidateCount;</pre><pre>}</pre><pre>struct Voter{</pre><pre>   address voterAddress;</pre><pre>   string name;</pre><pre>   string aadhar;</pre><pre>   uint constituency;</pre><pre>   bool hasVoted;</pre><pre>   bool isVerified;</pre><pre>}</pre><pre>address[] public voters;</pre><pre>mapping(address =&gt; Voter) public voterDetails;</pre><pre>// request to be added as voter</pre><pre>function requestVoter(string _name, string _aadhar, uint _constituency) public {</pre><pre>   Voter memory newVoter = Voter({</pre><pre>     voterAddress : msg.sender,</pre><pre>     name : _name,</pre><pre>     aadhar : _aadhar,</pre><pre>     constituency : _constituency,</pre><pre>     hasVoted : false,</pre><pre>     isVerified : false</pre><pre>   });</pre><pre>   voterDetails[msg.sender] = newVoter;</pre><pre>   voters.push(msg.sender);</pre><pre>   voterCount += 1;</pre><pre>}</pre><pre>// get total number of voters</pre><pre>function getVoterCount() public view returns (uint) {</pre><pre>   return voterCount;</pre><pre>}</pre><pre>function verifyVoter(address _address) public onlyAdmin {</pre><pre>   voterDetails[_address].isVerified = true;</pre><pre>}</pre><pre>function vote(uint candidateId) public{</pre><pre>   require(voterDetails[msg.sender].hasVoted == false);</pre><pre>   require(voterDetails[msg.sender].isVerified == true);</pre><pre>   require(start == true);</pre><pre>   require(end == false);</pre><pre>   candidateDetails[candidateId].voteCount += 1;</pre><pre>   voterDetails[msg.sender].hasVoted = true;</pre><pre>}</pre><pre>function startElection() public onlyAdmin {</pre><pre>   start = true;</pre><pre>   end = false;</pre><pre>}</pre><pre>function endElection() public onlyAdmin {</pre><pre>   end = true;</pre><pre>   start = false;</pre><pre>}</pre><pre>function getStart() public view returns (bool) {</pre><pre>   return start;</pre><pre>}</pre><pre>function getEnd() public view returns (bool) {</pre><pre>   return end;</pre><pre>}<br></pre><pre>}</pre><p>It was a very long contract… !! So make sure you copy it correctly.</p><p>Let’s discuss what is in this contract.</p><p>The very first line says that the solidity compiler version must be 0.4.17 or higher. Then we have our MasoomContract which has</p><blockquote><strong>owner </strong>-Address of the account which was used to deploy contract</blockquote><blockquote><strong>candidateCount, voterCount -</strong>The number of candidates and voters</blockquote><blockquote><strong>start, end </strong>-Bool values to state if election has started or not</blockquote><blockquote><strong>function MasoomContract()</strong> -Constructor to set default values to false or 0.</blockquote><blockquote><strong>function getOwner()</strong> -Function to return address of owner</blockquote><blockquote><strong>modifier onlyAdmin()</strong> -Modifier to allow only owner/admin to access</blockquote><blockquote><strong>Candidate -</strong>Struct for<strong> </strong>properties of candidate</blockquote><blockquote><strong>candidateDetails</strong> -mapping of uint to Candidate</blockquote><blockquote><strong>function addCandidate()</strong> -Function to add candidate, only by admin</blockquote><blockquote><strong>function getCandidateNumber()</strong> -Function to return number of candidates</blockquote><blockquote><strong>Voter </strong>-Struct for properties of voter</blockquote><blockquote><strong>voters</strong> -Array of address to store address of voters</blockquote><blockquote><strong>voterDetails</strong> -mapping of address to Voter</blockquote><blockquote><strong>function requestVoter()</strong> -Function to add as voter but will be verified by owner</blockquote><blockquote><strong>function getVoterCount()</strong> -Function to return number of voters</blockquote><blockquote><strong>function verifyVoter()</strong> -Function to verify voter, only owner can verify</blockquote><blockquote><strong>function vote()</strong> -Function to vote a candidate, once you are verified as voter</blockquote><blockquote><strong>function startElection()</strong> -Function to start election, only owner can access</blockquote><blockquote><strong>function endElection()</strong> -Function to end election, only owner can access</blockquote><blockquote><strong>function getStart()</strong> -Function to get bool value start</blockquote><blockquote><strong>function getEnd()</strong> -Function to get bool value end</blockquote><p>This was all about smart contract and its functions. Go to <strong>migrations</strong> and <strong>2_deploy_contracts.js </strong>and change it as -</p><pre>var MasoomContract = artifacts.require(&quot;./MasoomContract.sol&quot;);</pre><pre>module.exports = function(deployer) {</pre><pre>deployer.deploy(MasoomContract);</pre><pre>};</pre><p>Your contract is ready and ready for migrations. Go to terminal and run-</p><pre>truffle compile --all<br>truffle migrate --reset</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*9-Idj98djb5dnid_jGZPlA.png" /></figure><p>Everything should run fine and something like above screen should appear. (Make sure you have ganache-cli already running on other terminal)</p><p>What happened after migrations ? If this is your question then well..go to <strong>client/src/contracts </strong>you can see <strong><em>MasoomContract.json</em></strong> file, this file is generated after migration. We also see <strong><em>SimpleStorage.json</em></strong>, this is the json file generated when we migrated SimpleStorage.sol which we later deleted. There is no use of <strong><em>SimpleStorage.json</em></strong><em> </em>file, so you can just delete it.</p><p>In <strong><em>MasoomContract.json </em></strong>you can find <strong>abi , bytecode, metadata, source </strong>and etc. We use this file to interact in the front end.</p><h3>Frontend Development</h3><p>Till now we have deployed our smart contract and now use it in our frontend. The frontend part can be handled from the <strong>/client</strong> folder. <strong>/client </strong>has<strong> /public </strong>for static files<strong> </strong>and<strong> /src </strong>for source files. We have <strong>package.json</strong> for scripts and dependencies.</p><p>We need to install some dependencies. Instead of using npm install &lt;module_name&gt;, we will be adding dependencies in package.json and npm install to install all at once.</p><p>In package.json, remove all dependencies and write-</p><pre>“bootstrap”: “3.4.1”,<br>“history”: “4.10.1”,<br>“next”: “9.4.4”,<br>“react”: “16.11.0”,<br>“react-bootstrap”: “1.0.1”,<br>“react-bootstrap-table”: “4.3.1”,<br>“react-dom”: “16.11.0”,<br>“react-router-dom”: “5.2.0”,<br>“react-scripts”: “3.4.1”,<br>“web3”: “1.2.2”</pre><p>Now your package.json should look like -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mzgNJ0SLs0FIP8lcwVMPfQ.png" /></figure><p>Now, go to terminal in <strong>/client</strong> folder and type</p><pre>npm install</pre><p>This might take some time as we will be installing all dependencies. (Just ignore the warnings)</p><p>Next, go to /public/index.html .This is the main html page. Here we are going to change our title which is by default <strong>React App. </strong>Replace -&lt;title&gt;React App&lt;/title&gt; to &lt;title&gt;VoteChain&lt;/title&gt;</p><p>Now lets make different React Components. Go to <strong>/src</strong> and make a new folder <strong>components. </strong>Move <strong><em>App.js </em></strong>in<strong> /src </strong>to<strong> /src/components. </strong>Just to keep all components together.</p><p>There are many cases where <strong><em>webpack dependency</em></strong> <strong><em>error</em></strong> occurs. For housekeeping purpose, in <strong>/client</strong> folder make a <strong>.env </strong>file and write SKIP_PREFLIGHT_CHECK=true and save it.</p><h3>App.js -&gt; Home.js</h3><p>Now let’s format our <strong>App.js</strong> in <strong>/client/src/components</strong> . First of all <strong><em>rename</em></strong> <strong>App.js </strong>to <strong>Home.js, </strong>it is better to understand that it would be our home page.</p><p>First import MasoomContract from “../contracts/MasoomContract.json”;</p><p>In Home.js, change class App to class Home and also export default App to export default Home.</p><p>Now change the state like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*ct--2PLN99MRfecWVM9t5Q.png" /></figure><p>We have initialised the state, now lets move to componentDidMount.</p><p>We will add -</p><pre>// FOR REFRESHING PAGE ONLY ONCE -</pre><pre>if(!window.location.hash){</pre><pre>window.location = window.location + &#39;#loaded&#39;;</pre><pre>window.location.reload();</pre><pre>}</pre><p>this to refresh our page for housekeeping purpose, so that web3 and instance is loaded every time. Then in our <strong>try</strong> we will set our <strong>instance, account and web3.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*uQUx_ac3MoWCJnuW92rv8A.png" /></figure><p>Then we will set our state <strong>MasoomInstance</strong> to <strong>instance</strong>, <strong>account</strong> to <strong>accounts</strong> and <strong>web3</strong> to <strong>web3.</strong></p><p>Then we will call our <strong>getOwner() </strong>function from contract and check if it is owner or not.</p><pre>const owner = await this.state.MasoomInstance.methods.getOwner().call();</pre><p>This is how we can <strong>call</strong> any function from our smart contract. We will then check if the account address is equal to the owner’s address or not. If yes, we will set <strong>owner : true</strong> else <strong>owner : false</strong></p><p>Then we will also call <strong>getStart()</strong> and <strong>getEnd() </strong>function as-</p><pre>let start=await this.state.MasoomInstance.methods.getStart().call();<br>let end=await this.state.MasoomInstance.methods.getEnd().call();</pre><p>This will tell if the election has started or not. We will also set this in our state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*reUTh2nk_WGkgHGFdUbPpA.png" /></figure><p>We don’t need the runSample function so we can just delete it.</p><p>In the render section, we will delete everything and write our own logic. Let’s start with the basic first-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*rpNNsfJtNFeJRjAJKUkb7A.png" /></figure><p>Save the file for now, and in <strong>/client/src </strong>folder make a file named <strong>history.js</strong></p><p>and write following-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*qLp9l5D0-geYQNq4Oy8DGw.png" /></figure><p>Then goto <strong>/client/src/index.js </strong>and make changes-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*KLipcPAUcplGNFGdfNcraQ.png" /></figure><p>Now you are ready, to at least run without error. Go to terminal, in <strong>/client </strong>folder — npm run start</p><p>If everything goes fine, you should be able to see a screen like this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Pb-RN4LnEq7a5O7e0egfA.png" /></figure><p>Now, let’s check our address and print it on web. Go to /components/Home.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*15J4URC6Tyamp6JBscg4Bw.png" /></figure><p>We will be printing our address and checking if we are the owner or not.</p><p>Now, run npm run start and you should see-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m01QoifuMjmAhLjLvrIb7A.png" /></figure><p>Huraaayy !! You are the owner. So, accounts[0] was used to deploy contract so only it should be the owner. So if we use another address, we should get <strong>No you are not the owner. </strong>Let’s try this, in <strong>Home.js</strong> goto</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/755/1*Nk-nAq9Nb9VR6jmyMxOCgw.png" /></figure><p>and change the account to accounts[1] like this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*iqeRlGU5sYErb4g3GCVjiw.png" /></figure><p>Save and npm run start . Now you should see something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fD0QYQXg6rxkK-O3P__93A.png" /></figure><p>So, yes you have now working frontend, but we need to implement many other things. Stay connected as we will learn many new concepts in the next series. You are in a process to build something like this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*likNejH395btupmtJxbfsQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AzUJVD2VrxR6IXE-kAYmhA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1RxpgBf96CcI-UFvshRHcg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hPTYYHoJZ69DyLSdG7FK6w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CR4T2KttqA1SH5dTq7xwFw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NCC8z2QRmLZ1wF1F58MHtw.png" /></figure><p>Will be making series for it. Stay connected.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b92b973c7a36" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>