{"id":910,"date":"2017-04-17T08:52:49","date_gmt":"2017-04-17T08:52:49","guid":{"rendered":"http:\/\/www.talkcrypto.org\/blog\/?p=910"},"modified":"2017-04-22T07:46:07","modified_gmt":"2017-04-22T07:46:07","slug":"an-ethereum-hello-world-smart-contract-for-beginners-part-1","status":"publish","type":"post","link":"https:\/\/www.talkcrypto.org\/blog\/2017\/04\/17\/an-ethereum-hello-world-smart-contract-for-beginners-part-1\/","title":{"rendered":"An Ethereum Hello World Smart Contract for Beginners part 1"},"content":{"rendered":"<p>Venturing into the world of building smart contracts on Ethereum can very daunting at first. Where do you start?<\/p>\n<p><a href=\"https:\/\/www.ethereum.org\/greeter\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.ethereum.org\/greeter<\/a> is not a great place to start because it doesn&#8217;t give any context or start with the basics. Once you&#8217;ve built your first smart contract, the greeter makes a lot more sense.<\/p>\n<p><a href=\"https:\/\/medium.com\/@ConsenSys\/a-101-noob-intro-to-programming-smart-contracts-on-ethereum-695d15c1dab4\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/medium.com\/@ConsenSys\/a-101-noob-intro-to-programming-smart-contracts-on-ethereum-695d15c1dab4<\/a> is pretty good. What I wanted was more simple and basic though.<\/p>\n<p>I continued searching and plucked bits and pieces from all over the place and this is what I&#8217;ve come up with.<\/p>\n<h4>A super simple smart contract on Ethereum<\/h4>\n<p>In this article, I&#8217;m going to demonstrate how to build a very simple smart contract. This smart contract allows the user to deposit tokens and allows the user to query for the balance. Two simple functions.<\/p>\n<h4>Step 1: The code<\/h4>\n<p>The comments should explain what the code does.<\/p>\n<pre>\/\/ A simple bank smart contract where the user\r\n\/\/ can deposit and query funds.\r\n\/\/ by @seandotau 16 April 2017\r\n\r\n\/\/ This needs to be the first line.\r\npragma solidity ^0.4.0;\r\n\r\n\/\/ give your contract a name\r\ncontract aBank {\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 \/\/ this declares a key\/value array called balanceAccount.\r\n\u00a0\u00a0\u00a0 \/\/ the key is address and the value is an unsigned integer\r\n\u00a0\u00a0\u00a0 mapping (address =&gt; uint) balanceAccount;\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 \/\/ declare a deposit function that takes an input called amount\r\n\u00a0\u00a0\u00a0 function deposit(uint amount) public {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ add amount to the balance of the sender\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 balanceAccount[msg.sender] += amount;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 \/\/ a getBalance function that accepts no inputs but returns\r\n\u00a0\u00a0\u00a0 \/\/ the amount in the balanceAccount array\r\n\u00a0\u00a0\u00a0 function getBalance() returns (uint balance){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return balanceAccount[msg.sender];\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>You can use your favourite editor or Remix (<a href=\"https:\/\/ethereum.github.io\/browser-solidity\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/ethereum.github.io\/browser-solidity<\/a>) which is an Ethereum web based IDE which is what I&#8217;ll be using and recommend you do to. It helps the learning process. The code is also available at <a href=\"https:\/\/github.com\/seandotau\/SuperSimpleSmartContract\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/seandotau\/SuperSimpleSmartContract<\/a><\/p>\n<p>Copy the code into Remix and ensure Auto Compile is ticked. This is what you should see.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-911 size-full\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.01.50-PM.png\" alt=\"\" width=\"1411\" height=\"762\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.01.50-PM.png 1411w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.01.50-PM-300x162.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.01.50-PM-768x415.png 768w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.01.50-PM-1024x553.png 1024w\" sizes=\"(max-width: 1411px) 100vw, 1411px\" \/><\/p>\n<p>What next? This is the tricky part.<\/p>\n<h4>Step 2: The red pill or the blue pill?<\/h4>\n<p>In order to see this contract in action you have to make a decision. Do you want to deploy this on testnet or on testrpc?<\/p>\n<p>Testnet is good but a pain in the butt. It&#8217;s a real blockchain but you have to download the entire testnet blockchain and sometimes it doesn&#8217;t even work and you have a just wait until it does. I know, it sounds weird but you&#8217;ll understand when you choose this option.<\/p>\n<p>testrpc is fast but it is all command line. It&#8217;s not a real blockchain but it allows you to talk to the smart contract via an API. That is super neat because you can overlay a webpage on top.<\/p>\n<p>Let me go through both examples so you know what I&#8217;m talking about.<\/p>\n<h4>Testrpc<\/h4>\n<p>The first step is to install testrpc. Follow the instructions <a href=\"https:\/\/github.com\/ethereumjs\/testrpc\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. You need to have node.js v6.9.1 or greater and a whole bunch of other dependencies based on your system. This is part of the reason why it takes so long. The environment needs to be prepped.<\/p>\n<p>Type which testrpc to see if you have it already. If you do, you should see this:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-medium wp-image-913\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.15.32-PM-300x31.png\" alt=\"\" width=\"300\" height=\"31\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.15.32-PM-300x31.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.15.32-PM.png 308w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Once installed, when you run it by typing testrpc, you should see this:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-912 size-full\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.14.28-PM.png\" alt=\"\" width=\"659\" height=\"533\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.14.28-PM.png 659w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.14.28-PM-300x243.png 300w\" sizes=\"(max-width: 659px) 100vw, 659px\" \/><\/p>\n<p>Testrpc can be thought of as an in memory blockchain. Testrpc creates 10 test accounts to play with automatically. It also comes preloaded with 100 fake ethers.<\/p>\n<p>What you have to do now is open up another terminal to connect to this sandbox environment.<\/p>\n<p>Here, you need to have node installed. Typing &#8220;which node&#8221; will tell you if you have it or not.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-914 size-full\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.20.19-PM.png\" alt=\"\" width=\"296\" height=\"33\" \/><\/p>\n<p><a href=\"https:\/\/coolestguidesontheplanet.com\/installing-node-js-on-macos\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/coolestguidesontheplanet.com\/installing-node-js-on-macos\/<\/a> is a neat guide for mac users.<\/p>\n<p>Then run the following command to connect to testrpc<br \/>\n<code><br \/>\n&gt; Web3 = require('web3')<br \/>\n&gt; web3 = new Web3(new Web3.providers.HttpProvider(\"http:\/\/localhost:8545\"));<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-915 size-full\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.24.50-PM.png\" alt=\"\" width=\"564\" height=\"189\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.24.50-PM.png 564w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-9.24.50-PM-300x101.png 300w\" sizes=\"(max-width: 564px) 100vw, 564px\" \/><\/p>\n<p>Once it is connected, you can run this command to test<\/p>\n<pre>&gt; web3.eth.accounts<\/pre>\n<p>If you get a list of test accounts, you are good to go. In fact, if you look closely, these test accounts should match exactly to those in the testrpc terminal.<\/p>\n<p>If you get this error:<\/p>\n<pre>&gt; Error: Invalid JSON RPC response: undefined<\/pre>\n<p>it might mean that testrpc is not running. Check your other terminal screen.<\/p>\n<p>Now you have to compile the contract.<\/p>\n<h4>Step 3: Compile your smart contract with solidity.<\/h4>\n<p>Take the smart contract code from above and save it to a file. Or download <a href=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/aBank.txt\">this<\/a> if you&#8217;re lazy. Don&#8217;t forget to rename it to aBank.sol<\/p>\n<p>Now comes the fun part. Run these commands from within node in your second terminal window.<\/p>\n<p><code>&gt; code = fs.readFileSync('aBank.sol').toString()<br \/>\n&gt; contract = web3.eth.compile.solidity(code)<\/code><\/p>\n<p>The trick is to place the file in a location where node can find it otherwise you&#8217;ll get<\/p>\n<p><code>&gt; Error: ENOENT: no such file or directory, open 'aBank.sol'<\/code><\/p>\n<p>If you place it in your home location, it should compile fine. ie \/Users\/&lt;your_username&gt; for Mac users.<\/p>\n<p>The second command requires that you have solidity installed. Take another side track to install this.<\/p>\n<p>Go to <a href=\"https:\/\/github.com\/ethereum\/go-ethereum\/wiki\/Contract-Tutorial\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/ethereum\/go-ethereum\/wiki\/Contract-Tutorial<\/a> and search for Install SolC on Mac OSX (or whatever your OS is). On Mac it&#8217;s basically:<\/p>\n<pre><code>&gt; brew tap ethereum\/ethereum\r\n&gt; brew install solidity\r\n&gt; which solc\r\n<\/code><\/pre>\n<p>Note that it took about 30 minutes to install so go get some fresh air while it installs. It will look like nothing is happening but have faith.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-917 size-full\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-09-at-5.47.30-PM.png\" alt=\"\" width=\"705\" height=\"233\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-09-at-5.47.30-PM.png 705w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-09-at-5.47.30-PM-300x99.png 300w\" sizes=\"(max-width: 705px) 100vw, 705px\" \/><\/p>\n<p>Here it took 23 minutes 44 seconds to build one part of it. I&#8217;m grateful it didn&#8217;t take 90 minutes like this guy <a href=\"https:\/\/github.com\/ethereum\/solidity\/issues\/1199\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/ethereum\/solidity\/issues\/1199<\/a><\/p>\n<p>Now that you&#8217;ve got solidity installed, and half your day has gone, it&#8217;s time to compile the code.<\/p>\n<p><code>&gt; contract = web3.eth.compile.solidity(code)<\/code><\/p>\n<p>Time to deploy the contract.<\/p>\n<h4>Step 4: Deploy it already<code><\/code><br \/>\n<code><\/code><\/h4>\n<p>Run the following:<\/p>\n<p><code> &gt; aBankContract = web3.eth.contract(contract.info.abiDefinition)<br \/>\n&gt; deployedContract = aBankContract.new({data: contract.code, from: web3.eth.accounts[0], gas: 4700000})<br \/>\n&gt; contractInstance = aBankContract.at(deployedContract.address)<\/code><\/p>\n<p>The first command creates a contract object. The second deploys it with 3 fields. data, from and gas. The third grabs the contract address (which is a hash) and stores it in the contractInstance variable. We&#8217;ll use this soon.<\/p>\n<h4>Step 5: Talk to me smart contract<\/h4>\n<p>To get the balance of the bank type:<br \/>\n<code><br \/>\n&gt; contractInstance.getBalance.call().toString()<\/code><\/p>\n<p>To make a deposit:<br \/>\n<code><br \/>\n&gt; contractInstance.deposit('10', {from: web3.eth.accounts[0]})<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-918 size-medium\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-10.07.54-PM-300x57.png\" alt=\"\" width=\"300\" height=\"57\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-10.07.54-PM-300x57.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-10.07.54-PM-768x145.png 768w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-10-at-10.07.54-PM.png 982w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>And that is all there is to it folks!<\/p>\n<p>I did say I&#8217;d show you how to use testnet instead of testrpc. I&#8217;ll do that in my next post.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Venturing into the world of building smart contracts on Ethereum<\/p>\n","protected":false},"author":1,"featured_media":941,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-910","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ethereum"],"featured_image_urls":{"full":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/ehw.jpg",300,192,false],"thumbnail":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/ehw-150x150.jpg",150,150,true],"medium":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/ehw-300x192.jpg",300,192,true],"medium_large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/ehw.jpg",300,192,false],"large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/ehw.jpg",300,192,false],"1536x1536":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/ehw.jpg",300,192,false],"2048x2048":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/ehw.jpg",300,192,false],"chromenews-featured":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/ehw.jpg",300,192,false],"chromenews-large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/ehw.jpg",300,192,false],"chromenews-medium":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2017\/04\/ehw.jpg",300,192,false]},"author_info":{"info":["seandotau"]},"category_info":"<a href=\"https:\/\/www.talkcrypto.org\/blog\/category\/ethereum\/\" rel=\"category tag\">Ethereum<\/a>","tag_info":"Ethereum","comment_count":"0","_links":{"self":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/comments?post=910"}],"version-history":[{"count":9,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/910\/revisions"}],"predecessor-version":[{"id":970,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/910\/revisions\/970"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/media\/941"}],"wp:attachment":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/media?parent=910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/categories?post=910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/tags?post=910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}