{"id":2709,"date":"2018-12-17T23:18:54","date_gmt":"2018-12-17T17:48:54","guid":{"rendered":"http:\/\/navveenbalani.dev\/?p=2709"},"modified":"2019-12-18T17:32:32","modified_gmt":"2019-12-18T12:02:32","slug":"creating-and-deploying-ethereum-smart-contract","status":"publish","type":"post","link":"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/","title":{"rendered":"Creating and Deploying Ethereum Smart Contract"},"content":{"rendered":"\n<p>In this section, we would implement and deploy the smart contract for\nour use case.<\/p>\n\n\n\n<p>The crowdsourcing contract is in ethereum\/contract folder. Open the <em>CrowdSourceContract.sol<\/em> file. We will go\nover the important code snippets of our contract. Our contract is written in\nSolidity programming language. There are three main components to look at:<\/p>\n\n\n\n<p><em>CrowdFunding<\/em> &#8211; This is the\ncontract class.<\/p>\n\n\n\n<p><em>FundingProject<\/em> \u2013 A custom type\nthat hold project details as part of the contract.<\/p>\n\n\n\n<p><em>Funder<\/em> \u2013 A custom type\nthat holds funder details as part of the contract.<\/p>\n\n\n\n<p>Let\u2019s walkthrough the code:<\/p>\n\n\n\n<p>We first specify the Solidity version required to compile our\ncontract.<\/p>\n\n\n\n<p>pragma solidity ^0.4.11;<\/p>\n\n\n\n<p>We then create the <em>CrowdFunding<\/em>\ncontract using the <em>contract<\/em> keyword<\/p>\n\n\n\n<p>contract CrowdFunding {<\/p>\n\n\n\n<p>The <em>CrowdFunding<\/em> contract contains the <em>FundingProject<\/em> and <em>Funder<\/em> custom types with required attributes. We then create instances of the custom types.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Funding project details\n    struct FundingProject {\n        \n        string name;\n        string email;\n        string website;\n        uint minimumfunds;\n        uint amountraised;\n        address owner;\n        string status;\n      \n    }\n    \n    \/\/Funder who funds project.\n    struct Funder {\n        string name;\n        address fundedby;\n        uint amount;\n    }\n\n\/\/Multiple funders can fund project\n    Funder[] public funders;\n    \n    \/\/Instance\n    FundingProject public fundingproject\n<\/code><\/pre>\n\n\n\n<p>Next, we define methods\/operations that would be exposed by our\ncontract.<\/p>\n\n\n\n<p>The <em>CrowdFunding()<\/em> method shown below is the constructor of our contract which would initialize the project details. It expects parameters that are used to initialize the <em>FundingProject<\/em> type. We will show you how to pass these parameters once we instantiate the contract through our wallet interface in the next section.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function CrowdFunding (        \n        string _name,\n        string _email,\n        string _website,\n        uint _minimumfunds,\n        address _owner        \n        )\n    {\n       \t\n \/\/convert to ether\nuint minimumfunds = _minimumfunds * 1 ether;\nuint amountraised = 0;\nfundingproject = \nFundingProject(_name,_email,_website,\nminimumfunds,amountraised,_owner,\n\"Funding Started\");\n       \n    }\n<\/code><\/pre>\n\n\n\n<p>Next, we initialize the <em>Funder<\/em>\ntype and assign it to the <em>funders<\/em>\narray as part of the <em>fundProject()<\/em>\nmethod. The attribute <em>amountraised<\/em> of\nthe <em>FundingProject<\/em> type will be\nupdated to reflect the funder\u2019s amount. It means the funder has provided\nfunding of that amount value. If the attribute <em>amountraised<\/em> is greater than <em>minimumfunds<\/em>\ni.e. if the project meets the funding target then the amount is sent to the\nbeneficiary account named <em>ProjectAI<\/em>.\nThis is done using the <em>send() <\/em>method\nof the <em>FundingProject.owner<\/em>\nattribute. Following shows the code snippet&nbsp;\n&#8211;<\/p>\n\n\n\n<p>fundingproject.owner.send(fundingproject.amountraised)<\/p>\n\n\n\n<p>The important thing to note is that the amount reaches the ProjectAI\naccount only when minimum funding amount is raised. Until that duration, the\namount transferred from Funder\u2019s account is put on hold by the contract.<\/p>\n\n\n\n<p>The below <em>fundProject<\/em>() method uses the payable modifier to receive ethers from the application (i.e., from Funders account who is funding the project)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function fundProject( string name) public payable {\n\nif (stringsEqual(fundingproject.status ,\"Funding Completed\")) revert();\n\tfunders.push(Funder({\n\t\t\t\tname: name,\n\t\t\t\tfundedby: msg.sender,\n\t\t\t\tamount: msg.value\n\t\t\t   }) \n\t\t\t);\n\t fundingproject.amountraised =\t    \n                fundingproject.amountraised + msg.value ;\n\nif (fundingproject.amountraised >= \n               fundingproject.minimumfunds) {\n\t\t\t\t                 \n              if(!fundingproject.owner.send(fundingproject.amountraised )) revert();\t\t\t\t \n               fundingproject.status = \"Funding Completed\";\n\t} \n\telse {\t\t   \t\t\t\n                         fundingproject.status = \"In Progress\";\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>Next, we define the<em>\nstopFundRaising() <\/em>method. The said method returns all the money to the\nrespective Funders (if the funding target is not met) by iterating through the\nfunders array and calling the <em>send()<\/em>\nmethod of <em>Funder.fundedBy<\/em> attribute for\neach funder object in the array. Following shows the code snippet&nbsp; &#8211;<\/p>\n\n\n\n<p>funders[p].fundedby.send(funders[p].amount)<\/p>\n\n\n\n<p>The <em>stopFundRaising()<\/em> method\nuses the <em>payable<\/em> modifier to send\nethers from the contract to respective funders account.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function  stopFundRaising() public payable {\n    \t if (stringsEqual(fundingproject.status ,\"Funding Completed\")) revert();\n    \t fundingproject.status = \"Funding Stopped\";\n    \t \/\/return money to all funders\n    \t for (uint p = 0; p &lt; funders.length; p++) {\n    \t \tif(!funders[p].fundedby.send(funders[p].amount)) throw;\n    \t }\n    \t fundingproject.amountraised = 0;\n    }\n\n<\/code><\/pre>\n\n\n\n<p>Next, we define the <em>getProjectStatus()<\/em> constant method, that displays the status of the project at any interval.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getProjectStatus() public constant\treturns(string) {\n    \t\treturn (fundingproject.status);\n}\n\n<\/code><\/pre>\n\n\n\n<p>We now have our <em>CrowdFunding<\/em>\ncontract ready, the next step will be to deploy the same. You can copy the\ncontent of the CrowdSourceContract.sol file and navigate back to Ethereum\nWallet application.&nbsp; Click on <em>Contracts<\/em> from the top menu.&nbsp; On contracts window, navigate to <em>Deploy New Contract<\/em>. Select <em>Main Account<\/em> as the <em>From<\/em> option. <\/p>\n\n\n\n<p>Paste the copied content inside the <em>Solidity Contract Source code<\/em> window. The resulting contract will\nbe converted to bytecode once it is successfully compiled. If there were an\nerror in the contract, you would see a message \u201ccould not compile source code.\u201d<\/p>\n\n\n\n<p>Once the contract is compiled successfully, select the contract<em> Crowd Funding<\/em> from <em>Select Contract to Deploy<\/em> dropbox as shown in the figure below and\nenter the following details:<\/p>\n\n\n\n<ul><li>Enter name as ProjectAI<\/li><li>Enter any email address and website<\/li><li>For minimum funds, enter 1000. This is the\nminimum amount to be raised for this crowdfunding project.<\/li><li>Enter the address of account ProjectAI (if you\nrecall, we had copied and saved this earlier)<\/li><li>Click <em>Deploy<\/em><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"333\" height=\"259\" src=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-54.png\" alt=\"\" class=\"wp-image-2710\" srcset=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-54.png 333w, https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-54-300x233.png 300w\" sizes=\"(max-width: 333px) 100vw, 333px\" \/><figcaption><br><br><\/figcaption><\/figure>\n\n\n\n<p>On the <em>Create contract<\/em>\nwindow, you would see the transaction fee and gas price for deploying the\ncontract as shown in the figure below. Enter the password of the <em>Main Account<\/em> and click <em>Send Transaction.<\/em> <\/p>\n\n\n\n<p>If you go to <em>Wallets<\/em> tab and scroll down to the <em>Latest Transactions <\/em>section, you would see \u201cCrowdsourcing Contract\u201d being committed. Once all the confirmations are done, click on the transaction entry. You would see the transaction details and public address of the contract in the <em>To<\/em> column as shown in the figure below. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"216\" height=\"192\" src=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-55.png\" alt=\"\" class=\"wp-image-2711\"\/><\/figure>\n\n\n\n<p>Click on the <em>Crowd Funding<\/em> public address link, and you would see the contract details. You can verify the contract details entered earlier. You will also see the status of the project as <em>Funding Started<\/em> as shown in the figure below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"216\" height=\"192\" src=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-56.png\" alt=\"\" class=\"wp-image-2712\"\/><\/figure>\n\n\n\n<p>Now, in the <em>Select function<\/em>\ndrop down, select <em>Fund Project<\/em> method\nand enter name as \u201cFund from Funder 1\u201d. In the <em>Execute from<\/em> option select Funder 1 and specify 700 as ether amount\nas shown in the figure below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"216\" height=\"192\" src=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-57.png\" alt=\"\" class=\"wp-image-2713\"\/><figcaption><br><\/figcaption><\/figure>\n\n\n\n<p>Click Execute. On the Execute Contract window, specify the password\nfor <em>Funder1<\/em> and click <em>Send Transaction<\/em>. You should see the\namount raised as 700000000000000000000 and status of the project changed as \u201cIn\nprogress.\u201d The 700000000000000000000 value is in wei units (1 Ether =\n1000000000000000000 wei), the Wallet displays currency in wei units. You can\nverify the transaction details in the <em>Latest\nTransaction<\/em> view, and you should also see 700 ethers and transaction fee\ndeducted from the <em>Funder1<\/em> balance. <\/p>\n\n\n\n<p>Next, go back to the contract (either by clicking on contract link\nfrom the transaction view or by selecting <em>Contracts<\/em>\nfrom the top menu and clicking on the specific contract address from the custom\ncontract list). <\/p>\n\n\n\n<p>Now, select <em>Fund Project<\/em> method from<em> Select function<\/em> dropdown and this time, select <em>Funder2<\/em> and transfer 350 ethers as shown below. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"216\" height=\"192\" src=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-58.png\" alt=\"\" class=\"wp-image-2714\"\/><figcaption><br><\/figcaption><\/figure>\n\n\n\n<p>Click <em>Execute<\/em> and send the transaction.&nbsp; Once the transaction is committed, you will see the status of the project changed to Funding Completed. You should also see 350 ethers and transaction fee deducted from the <em>Funder2<\/em> balance. The <em>ProjectAI<\/em> account would be credited with 1050 ether amount.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"192\" height=\"168\" src=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-60.png\" alt=\"\" class=\"wp-image-2716\"\/><\/figure>\n\n\n\n<p>Next, we would execute the Stop Funding use case, where the amount funded by Funders would be transferred back to the Funders wallet if the funding target is not met.<\/p>\n\n\n\n<p>For this use case, we would follow similar steps as described earlier\nwith the addition of executing the Stop Funding function. Let\u2019s call this as\nUse Case 2 (we would reference this later). To implement \u2018Stop Funding\u2019 use\ncase, carry out the following steps :<\/p>\n\n\n\n<ol><li>Create a new contract as per earlier      guidelines and copy the content of CrowdFunding.sol in the new contract<\/li><li>Select <em>Crowd Funding<\/em> contract and provide the necessary details. Provide      minimum funds as 500 and execute the contract.<\/li><li>Verify the project status as      \u2018Funding Started\u2019.<\/li><li>Select the <em>Fund Project<\/em> method and transfer 200 ethers from <em>Funder1 account<\/em>. Execute the      transaction.<\/li><li>Verify the minimum funding amount      value is 200 for this project after the transaction is executed. <\/li><li>Now, execute the <em>Stop Fund Raising<\/em> method, select      the <em>execute<\/em> from as <em>Main Account (or Funder1 Account)<\/em>.      Click <em>Execute<\/em>.<\/li><li>The  status of the project would be <em>Funding      Stopped,<\/em> and you will see the 200 Ether transferred back to <em>Funder1 Account<\/em>. You can verify the  balance of the Funder1<em> Account<\/em>.<\/li><\/ol>\n\n\n\n<p>In the<a href=\"https:\/\/navveenbalani.dev\/index.php\/articles\/integrating-smart-contract-with-web-app\/\"> next article<\/a>, we will look at how to invoke the smart contract from a web application<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this section, we would implement and deploy the smart contract for our use case. The crowdsourcing contract is in ethereum\/contract folder. Open the CrowdSourceContract.sol file. We will go over the important code snippets of our contract. Our contract is written in Solidity programming language. There are three main components to look at: CrowdFunding &#8211; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,174],"tags":[286],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.0.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating and Deploying Ethereum Smart Contract - Current and Future Technology Trends by Navveen Balani<\/title>\n<meta name=\"description\" content=\"Creating and Deploying Ethereum Smart Contract - Articles\" \/>\n<link rel=\"canonical\" href=\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating and Deploying Ethereum Smart Contract - Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"og:description\" content=\"Creating and Deploying Ethereum Smart Contract - Articles\" \/>\n<meta property=\"og:url\" content=\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/\" \/>\n<meta property=\"og:site_name\" content=\"Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-17T17:48:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-18T12:02:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-54.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"7 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/navveenbalani.dev\/#website\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Current and Future Technology Trends by Navveen Balani\",\"description\":\"Current and Future Technology Trends by Navveen Balani\",\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/navveenbalani.dev\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-54.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/#webpage\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/\",\"name\":\"Creating and Deploying Ethereum Smart Contract - Current and Future Technology Trends by Navveen Balani\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/#primaryimage\"},\"datePublished\":\"2018-12-17T17:48:54+00:00\",\"dateModified\":\"2019-12-18T12:02:32+00:00\",\"description\":\"Creating and Deploying Ethereum Smart Contract - Articles\",\"breadcrumb\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/\",\"name\":\"Creating and Deploying Ethereum Smart Contract\"}}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/#webpage\"},\"author\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"headline\":\"Creating and Deploying Ethereum Smart Contract\",\"datePublished\":\"2018-12-17T17:48:54+00:00\",\"dateModified\":\"2019-12-18T12:02:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/#webpage\"},\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"image\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-and-deploying-ethereum-smart-contract\/#primaryimage\"},\"keywords\":\"blockchain-guide\",\"articleSection\":\"Articles,Blockchain\",\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\",\"name\":\"Navveen\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/07\/navveen_balani.jpeg\",\"width\":200,\"height\":200,\"caption\":\"Navveen\"},\"logo\":{\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\"},\"sameAs\":[\"http:\/\/naveenbalani.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/2709"}],"collection":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/comments?post=2709"}],"version-history":[{"count":3,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/2709\/revisions"}],"predecessor-version":[{"id":2775,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/2709\/revisions\/2775"}],"wp:attachment":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/media?parent=2709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/categories?post=2709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/tags?post=2709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}