{"id":2251,"date":"2022-01-25T18:11:09","date_gmt":"2022-01-25T18:11:09","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=2251"},"modified":"2022-01-25T18:11:09","modified_gmt":"2022-01-25T18:11:09","slug":"build-a-real-world-dapp-with-react-solidity-and-web3-js","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=2251","title":{"rendered":"Build a Real-World dApp With React, Solidity, and Web3.js"},"content":{"rendered":"\n<p id=\"2f83\">The world of online technology is transitioning fast towards&nbsp;<a href=\"https:\/\/www.freecodecamp.org\/news\/what-is-web3\/\" rel=\"noreferrer noopener\" target=\"_blank\">web 3.0<\/a>. It seems like people are done with centralized systems with their digital privacy being maligned on daily basis by huge organizations. Therefore, they want a solution and web 3.0 seems to be the answer for now.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p id=\"b008\">This blog post isn\u2019t meant to cover the know-hows of blockchain and decentralized systems. Instead, this is for those who would like to build online solutions for users, customers, and clients to guarantee them better privacy and security for their data.<\/p>\n\n\n\n<p id=\"8e2d\">Having clarified that, in this article I am going to show you the ABC of how to make a decentralized application from scratch and set up your development environment. Below are some of the topics we are going to cover.<\/p>\n\n\n\n<ol><li>Tools<\/li><li>Setup Tools<\/li><li>Write Code<\/li><\/ol>\n\n\n\n<h1 id=\"5e52\">A Look at the Tools<\/h1>\n\n\n\n<p id=\"b388\">Below are some of the tools I am going to use in this blog post.<\/p>\n\n\n\n<p id=\"3b13\">a.&nbsp;<a href=\"https:\/\/trufflesuite.com\/\" rel=\"noreferrer noopener\" target=\"_blank\">Truffle Framework<\/a><\/p>\n\n\n\n<p id=\"f025\">b.&nbsp;<a href=\"https:\/\/trufflesuite.com\/ganache\/\" rel=\"noreferrer noopener\" target=\"_blank\">Ganache<\/a><\/p>\n\n\n\n<p id=\"0ca8\">c.&nbsp;<a href=\"https:\/\/docs.soliditylang.org\/en\/v0.8.10\/\" rel=\"noreferrer noopener\" target=\"_blank\">Solidity 0.8.10<\/a><\/p>\n\n\n\n<p id=\"01b0\">d.&nbsp;<a href=\"https:\/\/metamask.io\/\" rel=\"noreferrer noopener\" target=\"_blank\">Metamask<\/a><\/p>\n\n\n\n<p id=\"98a0\">e.&nbsp;<a href=\"https:\/\/reactjs.org\/versions\/\" rel=\"noreferrer noopener\" target=\"_blank\">React 17.0.2<\/a><\/p>\n\n\n\n<p id=\"f09a\">f.&nbsp;<a href=\"https:\/\/web3js.readthedocs.io\/en\/v1.5.2\/\" rel=\"noreferrer noopener\" target=\"_blank\">Web3.js 1.6.1<\/a><\/p>\n\n\n\n<p id=\"fd85\">In order to get going, I would like to briefly touch a little bit about the above-mentioned tools.<\/p>\n\n\n\n<p id=\"77d9\">The first one, Truffle Framework offers a set of tools for developing Ethereum smart contracts. It offers tools such as smart contract management, deployment and migration, network management, development console etc.<\/p>\n\n\n\n<p id=\"e6a4\"><a href=\"https:\/\/trufflesuite.com\/ganache\/\" rel=\"noreferrer noopener\" target=\"_blank\">Ganache<\/a>&nbsp;is a personal blockchain, which is a local development blockchain that can be used to mimic the behavior of a public blockchain.<\/p>\n\n\n\n<p id=\"0023\">Solidity is an object-oriented, high-level language for implementing smart contracts. To know more about Solidity please click&nbsp;<a href=\"https:\/\/docs.soliditylang.org\/en\/v0.8.10\/\" rel=\"noreferrer noopener\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p id=\"3fd1\">Most browsers currently do not allow us to connect to the blockchain network for that reason, I would use the Metamask<strong>&nbsp;<\/strong>chrome extension which will allow us to connect our chrome browser to the blockchain network.<\/p>\n\n\n\n<p id=\"7f4c\">For UI or Front end development, we are going to use React library which is one of the most widely used JavaScript libraries among the front-end communities.<\/p>\n\n\n\n<p id=\"1d2f\"><a href=\"https:\/\/web3js.readthedocs.io\/en\/v1.5.2\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>Web3.js<\/strong><\/a><strong>&nbsp;<\/strong>is a JavaScript library that allows us to communicate with the Ethereum blockchain. It turns our React application into blockchain enabled application.<\/p>\n\n\n\n<h1 id=\"4ed9\">Setting Up Tools<\/h1>\n\n\n\n<p id=\"d565\">Now that I have briefly explained the tools set that are going to be used in this blog post, it&#8217;s time to get into it and set the tools up.<\/p>\n\n\n\n<p id=\"5f5c\">First and foremost, I would like you to download&nbsp;<a href=\"http:\/\/truffleframework.com\/\" rel=\"noreferrer noopener\" target=\"_blank\">Truffle Framework<\/a>&nbsp;and install it with the below command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">npm install -g truffle<\/pre>\n\n\n\n<p id=\"95f0\">Next, go ahead and download and install&nbsp;<a href=\"http:\/\/truffleframework.com\/ganache\" rel=\"noreferrer noopener\" target=\"_blank\">Ganache<\/a>. Once you do that and open it, you will get the below screen:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/0*McDHgmnGAxEiLT8h.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"92b4\">Next, we need Metamask.<\/p>\n\n\n\n<p id=\"a998\">Go ahead and add&nbsp;<a href=\"https:\/\/chrome.google.com\/webstore\/detail\/metamask\/nkbihfbeogaeaoehlefnkodbefgpgknn?hl=en\" rel=\"noreferrer noopener\" target=\"_blank\">Metamask<\/a>&nbsp;extension to your google chrome and get to the screen when you get something like below. In order to set up Metamask please follow this&nbsp;<a href=\"https:\/\/asifwaquar.com\/connect-metamask-to-localhost\/\" rel=\"noreferrer noopener\" target=\"_blank\">blog post<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/338\/0*5UEMK8rVYAKtOJI2.png\" alt=\"\"\/><\/figure>\n\n\n\n<h1 id=\"ed62\">Writing Code<\/h1>\n\n\n\n<p id=\"f2a2\">Now that the tools are set up, let\u2019s proceed to the next step which is writing smart contracts. To do that, open a terminal and create a folder in your projects folder with the below command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir blockchain<\/pre>\n\n\n\n<p id=\"9540\">Now make a folder inside the blockchain folder with the below command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd blockchainmkdir contractscd contracts<\/pre>\n\n\n\n<p id=\"17cb\">Now run the below command to create a truffle project which will allow us to develop smart contracts:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">truffle init<\/pre>\n\n\n\n<p id=\"ff1b\">With the above command, you should get an output like below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/278\/0*iUj5zINEUEEUdvwO.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"2e43\">Now open your truffle project in your favorite text editor. I am going to use Sublime Text. You should see the following folder and files structure:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/339\/0*UZ3FdXhe72Zq4kwV.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"8a56\">Inside&nbsp;<code>contracts<\/code>&nbsp;folder, we are going to write our smart contracts.<\/p>\n\n\n\n<p id=\"fb66\">Within the&nbsp;<code>migrations<\/code>&nbsp;folder, we are going to migrate our newly created smart contracts.<\/p>\n\n\n\n<p id=\"03fe\">Inside&nbsp;<code>test<\/code>&nbsp;folder, we usually write tests to test our smart contract however, it is beyond the scope of this blog post therefore, we are not going to get into that. I would highly recommend writing tests for your smart contracts before deploying them to public blockchain nodes.<\/p>\n\n\n\n<p id=\"be08\"><code>truffle-config.js<\/code>&nbsp;file contains all the configuration of or truffle project.<\/p>\n\n\n\n<p id=\"6710\">Now let\u2019s write our smart contract. Create a new file and name it&nbsp;<code>contacts.sol<\/code>&nbsp;inside&nbsp;<code>contracts<\/code>&nbsp;folder. Now open that file and write the below code inside it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pragma solidity &gt;=0.4.22 &lt;0.9.0;<\/pre>\n\n\n\n<p id=\"ac25\">This should always be the first line in your smart contract file. With this, we are specifying the version of solidity.<\/p>\n\n\n\n<p id=\"6572\">Now let\u2019s make our first smart contract with code:https:\/\/betterprogramming.pub\/media\/178d8ebcc0947c9682f2a06b99413e3f<\/p>\n\n\n\n<p id=\"788d\">We can write a smart contract using the contract keyword and followed by the name of the contract which in this case&nbsp;<code>Contacts<\/code>.<\/p>\n\n\n\n<p id=\"016f\">Now to track the number of contacts inside our smart contract, we will create a state variable.https:\/\/betterprogramming.pub\/media\/b206192e479846390dd51e183770c0af<\/p>\n\n\n\n<p id=\"9923\">This is a special variable and any data we write into this variable will be stored in blockchain storage. We are using a special modifier keyword i.e.&nbsp;<code>public<\/code>&nbsp;to have access to this variable outside of the smart contract. Then we are assigning 0 to this variable.<\/p>\n\n\n\n<p id=\"abb2\">Now that we have a state variable and it has a value in it. Let\u2019s go into the front end part and let\u2019s try to access this public state variable first. With this approach, we will establish communication between React application and our smart contract.<\/p>\n\n\n\n<p id=\"6da2\">Run the below commands inside the&nbsp;<code>blockchain<\/code>&nbsp;folder to create a react application.https:\/\/betterprogramming.pub\/media\/e36cce3e851c3ee7866ce88750b16438<\/p>\n\n\n\n<p id=\"2f3a\">As you can see we also added&nbsp;<code>web3.js<\/code>&nbsp;as a dependency for this project to make our react application interact with our smart contract.<\/p>\n\n\n\n<p id=\"e0f2\">Now open that contacts folder in your favorite editor. I am using Sublime Text.<\/p>\n\n\n\n<p id=\"179b\">We are not going to set up the folder structure and implement complex architecture because that is out of the scope of this blog post.<\/p>\n\n\n\n<p id=\"8666\">We\u2019ll write all our code in&nbsp;<code>App.js<\/code>&nbsp;file. Therefore, go ahead and open the file in your editor.<\/p>\n\n\n\n<p id=\"67af\">Write below code inside&nbsp;<code>App.js<\/code>&nbsp;file:https:\/\/betterprogramming.pub\/media\/ca74af9630de27cefcad5987c1d58b42<\/p>\n\n\n\n<p id=\"d011\">We are importing a couple of hooks from React and Web3 from&nbsp;<code>Web3.js<\/code>. Now let\u2019s create a state variable like below:https:\/\/betterprogramming.pub\/media\/9f9975709f64f8268ddf357e8b192b94<\/p>\n\n\n\n<p id=\"1241\">Now inside&nbsp;<code>useEffect<\/code>&nbsp;hook we will with our smart contract like below:https:\/\/betterprogramming.pub\/media\/88fc1e1f94c8583dc328ae91ae01a087<\/p>\n\n\n\n<p id=\"ff1c\">Now in order to run this client-side successfully, we need to take a few steps in the backend. Let\u2019s get back to&nbsp;<code>contracts<\/code>&nbsp;folder. First, open&nbsp;<code>truffle-config.js<\/code>&nbsp;file and add the below properties:https:\/\/betterprogramming.pub\/media\/e848488e3ba30448e425298e47bc39d6<\/p>\n\n\n\n<p id=\"43e1\">Now inside&nbsp;<code>migrations<\/code>&nbsp;folder, create a new file and name it&nbsp;<code>2_deploy_contracts.js<\/code>&nbsp;and paste the below code:https:\/\/betterprogramming.pub\/media\/f1c03a20c3f1c2b8505528b4deeba750<\/p>\n\n\n\n<p id=\"59ed\">Now in the terminal, type the below code to migrate your contract:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">truffle migrate<\/pre>\n\n\n\n<p id=\"d117\">You should see an output something like below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/0*QnvqpCSVkodn6rA1.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"8b6d\">Now go back to the front end folder and run the below command to run the application:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yarn start<\/pre>\n\n\n\n<p id=\"ffc1\">This will open your React application in your browser and it will trigger Metamask to interact with your blockchain network. You should see a screen like below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/348\/0*70qm1YW04p_vBvBp.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"f4ec\">In the above screen, click next and you should be presented with the below screen.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/346\/0*V3UUyOQSlViXXp6e.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"404a\">Now click connect. Then you should see the output on your browser with your account number like below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/587\/0*qDP8R6PuHvyDtI5r.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"3c91\">Now that we have successfully interacted with our smart contract and retrieved account id. Let\u2019s proceed with making a new function inside our smart contract to get a list of contacts and send it over to the front end and render them on the view.<\/p>\n\n\n\n<p id=\"d848\">Let go back to&nbsp;<code>contracts<\/code>&nbsp;folder and open&nbsp;<code>Contracts.sol<\/code>&nbsp;file and add the below function.https:\/\/betterprogramming.pub\/media\/dc3c48df76e0021868986517890a33ef<\/p>\n\n\n\n<p id=\"65b4\">Now migrate this contract again as we made changes to this because smart contracts are immutable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">truffle migrate<\/pre>\n\n\n\n<p id=\"f8ca\">Now let\u2019s get back to the front end i.e.&nbsp;<code>contacts<\/code>&nbsp;folder. Create a new file&nbsp;<code>config.js<\/code>&nbsp;inside&nbsp;<code>src\/<\/code>&nbsp;folder and paste the below code in that.https:\/\/betterprogramming.pub\/media\/37eef2092ec439b9c9988c0eec8f7e14<\/p>\n\n\n\n<p id=\"882a\">Now import both smart contract address and ABI into&nbsp;<code>App.js<\/code>&nbsp;file like below and also update load function with below code:https:\/\/betterprogramming.pub\/media\/72c95fc41668ed217d3629d8d7037045<\/p>\n\n\n\n<p id=\"b042\">The above code will get all of the contacts from our smart contract and set them in&nbsp;<code>contacts<\/code>&nbsp;state variable. The code is very well commented on and explained.<\/p>\n\n\n\n<p id=\"3e10\">Now let\u2019s render all the contacts in ul like below.https:\/\/betterprogramming.pub\/media\/5a6f29d4f52d2e9b6da64285be7fda92<\/p>\n\n\n\n<p id=\"838f\">As you can find the updated code inside return \u2014 when you run this react app now in browser, you should be able to see something like below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/596\/0*v4WLiEj4ibiBNVc7.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"ddd5\">If that is what you see then congratulations, you have just built your first dApp with the smart contract and web app that interacts with your smart contract on the blockchain.<\/p>\n\n\n\n<p id=\"efd2\">Now that you know how to retrieve information from the smart contract, go ahead and improve this further by adding new contacts, updating them and deleting them with full CRUD operations.<\/p>\n\n\n\n<p id=\"8541\">That&#8217;s it from this article. The entire project can be found in the below GitHub Repository:<a href=\"https:\/\/github.com\/zafar-saleem\/blockchain-tutorial\" rel=\"noreferrer noopener\" target=\"_blank\">GitHub \u2014 zafar-saleem\/blockchain-tutorialNote: This is a sample project for my blog on medium which can be found here. Clone this repository cd\u2026github.com<\/a><\/p>\n\n\n\n<p id=\"de56\">Below are my social media profiles if you would like to get in contact with me.<\/p>\n\n\n\n<p id=\"c5cc\"><a href=\"https:\/\/www.linkedin.com\/in\/zeesaleem\/\" rel=\"noreferrer noopener\" target=\"_blank\">Linkedin<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/github.com\/zafar-saleem\" rel=\"noreferrer noopener\" target=\"_blank\">Github<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/gitlab.com\/zsaleem\" rel=\"noreferrer noopener\" target=\"_blank\">Gitlab<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/zafar-saleem.github.io\/\" rel=\"noreferrer noopener\" target=\"_blank\">Website<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The world of online technology is transitioning fast towards&nbsp;web 3.0. It seems like people are done with centralized systems with their digital privacy being maligned on daily basis by huge organizations. Therefore, they want a solution and web 3.0 seems to be the answer for now.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[178],"tags":[65,176],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2251"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2251"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2251\/revisions"}],"predecessor-version":[{"id":2252,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2251\/revisions\/2252"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}