Advertisement
AD

Main navigation

Error message

  • Warning: Undefined array key 1 in Drupal\cryptocompare\TwigExtension\RemoveSpace::getcard() (line 3111 of modules/custom/cryptocompare/src/TwigExtension/RemoveSpace.php).
    Drupal\cryptocompare\TwigExtension\RemoveSpace::getcard('
                        Contents
                        <ul class="article__contents-list"><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h237">How does Ethereum work?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h389">How does a smart contract work?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h418">How to build a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h52">How to make a token on Ethereum?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h679">How to launch a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h784">What can you do with a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h841">Summary</a></li></ul>
        Advertisement
        
            
        
        
            
                    
                
                                                
    
            
            
                    
                
                                                
    
            
        
    In over 10 years of its development, blockchain has significantly expanded from a straightforward system of transferring value via coins to the basis for Web 3.0 and decentralized applications. This happened mainly due to an outbreak of new projects after Ethereum introduced its ERC20 standard and smart contracts.
    
    Smart contracts were widely used in <a href="https://u.today/ico-market-is-dead-only-118-mln-raised-in-q1-wsj-report">fundraising for ICOs</a> to ensure transparency for participants. This made them famous, but that wasn’t the only implementation. There are successful cases for smart contracts in creating distributed autonomous organizations, managing rights and conducting governance. The need for them is so apparent that there even are attempts to create smart contracts for Bitcoin.
    
    If you want to make it in blockchain engineering, you will have to know how smart contracts work and how to build them. Fortunately, Ethereum provides everything necessary for it, including its own programming language – Solidity. This language was specifically created for working with Ethereum Virtual Machine. It’s pretty easy to understand, but there are some distinctive features that you will have to keep in mind. Today we will dive deep into the world of smart contracts and code some of them. Let’s roll!
    
    <a rel="nofollow" target="_blank" href="#-programming-how-many-programming-languages-do-you-need-for-blockchain">card</a>
    
    How does Ethereum work?
    
    First and foremost, we need to understand the basics of Ethereum’s blockchain and how it functions. This will help us in understanding the difference between coding centralized apps and <a rel="nofollow" target="_blank" href="#">decentralized ones</a> (which smart contracts basically are).
    
    In its core, Ethereum has a virtual machine. You probably encountered them earlier when launching old games in an emulator or running some OS-specific software on the non-native OS. In simple terms, a virtual machine is a computer inside a computer, which uses the hardware of the original machine. In the case of Ethereum, the EVM uses resources of computers connected to the network (nodes). Like any other VM, Ethereum’s one has its own RAM and ROM, which are used by programs running on top of it. The only significant exception is that the EVM’s ROM memory is blockchain, so once something gets there, it cannot be removed.
    
    Why is this important? Creating smart contracts is an incredible responsibility because in case there is a vulnerability, users’ money can be stolen or lost. While you can amend or completely replace the code on a centralized server, you can’t do so on a decentralized VM. In addition, every action performed with a smart contract requires paying fees to the network. Why? Because the EVM has to process a given task, and it can only do it by utilizing some nodes’ processing power. <a rel="nofollow" target="_blank" href="#">The fees are called Gas</a> and are paid in Gwei, which are similar to Bitcoin’s Satoshi.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    How does a smart contract work?
    
    If you have ever used Ethereum, then you generated a wallet in the network at least once. Your wallet is basically an account that is represented by an address. A smart contract is an account too, but it differs from a regular account.
    
    Once you create a wallet, you can connect it to any of the available networks, and the only thing that will change is your balance. This is because user addresses are not internal to any network – they exist in parallel. On the other hand, an instance of a <a rel="nofollow" target="_blank" href="#">smart contract</a> is included in a given blockchain and can’t be viewed in another chain. To be able to launch a contract on the Main Network after testing it in, let’s say Rinkeby, its creator will need to deploy a new instance of the contract. This will require compiling the source code once again. A contract creator can make as many instances of the code as they wish, as long as they have enough ETH to pay Gas for interacting with the network.
    
    Just like a centralized application, a smart contract has a dedicated place for storing its resources and a placeholder for code (picture it as a web page that you download from the internet, it will have a .html file and some folders with pictures and other stuff). There is also an indication of how much ETH a contract has on its balance; this is crucial for various use cases like fundraising, wills, etc. The full schematic is presented in the picture below.
    Image by U.Today
    How to build a smart contract?
    
    Now that we know the principles of how <a href="https://u.today/ethereum">Ethereum</a> functions and how smart contracts run within it, we can create one by ourselves. First, we will need to set up the proper environment. You can go two routes, either writing the code in browser with the Remix IDE provided by Ethereum Foundation or downloading a coding app of your choice along with some packages for it. For the purpose of this tutorial, we will show you both Remix and Atom (a universal coding app for Mac &amp; Windows).
    
    The first smart contract that we are going to make will take the names of different car models and their horsepower. What we want to see is two input and two output fields for the values we have. Since we are going to change the inputs, we need to set variables of two types: a string for the names and an integer for the horsepower.
    
    Before our contract, we will put a line that specifies the language version, as this will help the compiler to adapt the code properly. Compiling the code is necessary because once we get to the deployment stage, the code will be adapted to the machine code that the EVM will be able to read and process. In addition, an application binary interface will be created that could be used further in creating a full-fledged <a rel="nofollow" target="_blank" href="#">decentralized application</a>. You can always check the latest version of Solidity on its official website. To specify it, type ‘pragma solidity’ and the version after the ‘^’ sign.
    
    <a href="https://u.today/no-dice-ethereum-loses-dapps-race-to-eos-and-tron">card</a>
    
    To set borders for our contract, let’s give it a name followed by a parenthesis. First, we will declare two variables: ‘model’ and ‘HP’. The first one will be a string that represents a car model and the second one will be an integer that represents a given car model’s horsepower. There are two things to mention about variables in terms of Solidity. The former is that we should declare whether or not third parties should be able to see them by putting ‘public’ or ‘private’ next to them. The latter is that the integers in Solidity can be signed or unsigned. An unsigned integer can only be possible and is written as ‘uint’. A signed integer can be both positive &amp; negative and is written as ‘int’.
    
    After we have our variables specified, it’s time to write some functions that our smart contract will perform. Firstly, we need to make placeholders for our model and HP. We will use a couple of set public functions and attach our variables to them. Secondly, we need to outline a couple of get public functions that will return an input. Take a look at how our smart contract looks.
    Image by U.Today
    How to make a token on Ethereum?
    
    Many people were attracted to the blockchain industry because of the <a rel="nofollow" target="_blank" href="#">outrageous gains on the crypto market at the end of 2017</a>. This was mainly the outcome of the advent of the new ERC20 token standard that a huge number of startups used to launch their tokens and raise money for development. Despite the decline in the hype, some companies are still launching their tokens on Ethereum and are selling to contributors, proving that the demand is still there. If you have always dreamt of building a startup and launching your token, this part will be very interesting.
    
    Creating a token means using the ERC20 standard, which can be quickly googled. Don’t forget to set the version of Solidity before the standard itself. There will be a set of functions and a couple of events that are supposed to log actions into the Ethereum’s ledger. Among the functions, you will find the set of necessary ones: one for defining the total supply, one for checking the balance of a given wallet, and one for transferring tokens between wallets. As Ethereum allows controlling wallets from other wallets, you will also find functions that define a target wallet and allow balance for transfer and approve a transaction. Save this into a separate .sol file.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    It’s time to outline our own token. Start with specifications of the version of Solidity. Then import the interface and attach it to a new smart contract via ‘is’. Going further we will:
    
    <ul>
    	<li dir="ltr">
    	First, outline the symbol, the name and the number of decimals in variables. The number of decimals lets the user transfer and store parts of a token, the most common number of decimals is 18. All of these shall be visible to users, so we make them public.
    	</li>
    	<li dir="ltr">
    	Second, we will define the total supply. In the case of this tutorial, it will be fixed and equal to one billion.
    	</li>
    	<li dir="ltr">
    	Third, let’s make mappings for checking balances on wallets and allowances in case of any third party will control an address.
    	</li>
    	<li dir="ltr">
    	Fourth, we will set a function that will be executed only once upon the contract’s deployment (constructor) that will give our address all the tokens we will create.
    	</li>
    	<li dir="ltr">
    	Fifth, we need to create a number of functions that constrain what we have in the interface. The total supply will be taken from the variable we declared before. The balances will be checked according to the mapping. The transfer will only be allowed if a user has some ETH and they are sending less or equal to what than they actually have. Controlling another wallet will be done according to the previously defined mapping and check for allowed amount. Check the code we got below.
    	</li>
    </ul>
    Image by U.Today
    How to launch a smart contract?
    
    You now know how to code simple smart contracts on Solidity. Let’s try to understand how they can be launched on the Ethereum network. First, we need to compile the code. For this purpose, you can either use Remix’s integrated compiler or do it on your machine with the help of Truffle framework. Let’s go the easy route first.
    
    Before we begin it’s necessary that you create an Ethereum account if you haven’t so far. For this, we recommend using Metamask extension which can be installed on top of Chrome, Opera, Firefox and Brave browsers. <a rel="nofollow" target="_blank" href="#">Once you create a wallet</a>, make sure that you save the seed phrase because it will be the only way to access a wallet from other machines or in case you have to reinstall the extension. The advantage of Metamask is that you can switch between various networks. This will come in handy as we are going to use a test network for our experiments. Open the extension and choose Ropsten network. Then, go to the faucet and request some ETH, remember that every operation with the EVM costs Wei.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    Open remix.ethereum.org. You will see a code for a ballot – close the tab with it. Remix has a dedicated browser for .sol files and an integrated compiler. At the top of the browser, there is a cross icon that allows creating new files. Let’s create a file and copy our first smart contract with vehicles and their horsepower. On the right part of the screen, you will see the compiler with a few tabs. In the first tab select a compiler version that corresponds with the version of Solidity you specified and initiate compilation process. If there are no errors, you will be presented with a ‘success’ message. Select the second tab and you will see a section where you should specify an appropriate environment (in our case Web3) and check that your address is correct (it should come down from Metamask). If everything is correct, press ‘deploy’ and you will get a pop-up from Metamask with a transaction. Confirm it and voila, your smart contract is deployed to the network.
    
    Now to something more interesting. If you used a text editor on your computer to write the code, you can deploy it locally by using Node.js. First, download it from the official website or install via a Command Line (Windows) or Terminal (OSX). In our case, we will use OSX, but the process is similar.
    Image by U.Today
    Second, install the Solidity framework Truffle via ‘npm install’.
    Image by U.Today
    Third, make a new folder and create a repo via ‘init’ command.
    Image by U.Today
    Fourth, install HDWalletProvider into this repo via Node.js too.
    Image by U.Today
    Fifth, copy files with the ERC20 interface and your token contract to the ‘contracts’ folder in your repo. Sixth, create the following JavaScript file in your ‘Migrations’ folder within the repo.
    Image by U.Today
    Seventh, you need to modify your Truffle config file. However, there is one extra step required prior to doing so. You will input your wallet’s mnemonic (which you saved earlier) and an API for Ropsten testnet. To get the latter, go to Infura and register a project. On the project’s page, you will see its ID. Use it as an API.
    Image by U.Today
    Finally, you are ready to launch your smart contract. Here’s what the command and the process look like. Should there be any issues, you will be presented with them instead.
    Image by U.Today
    What can you do with a smart contract?
    
    Good work, you have the first two smart contracts deployed. Now what? For starters, you need to verify and publish them. Let’s find them first in any of Ethereum’s block explorers. We will be using <a rel="nofollow" target="_blank" href="https://etherscan.io/">Etherscan</a>.
    
    The quickest way to access your contracts is through the transactions from your wallet. Access Metamask, press three dots, and it will let you view your account in Etherscan. Your address now has at least one transaction for contract creation. Accessing a transaction, you will find a destination address, which is essentially a contract’s address. Once you end up on the contract’s page, you will see a few tabs. Go to the ‘Code’ tab and proceed to the verification page. Here input all the relevant information and press the button to start the verification process.
    Image by U.Today
    After you verify the first smart contract that we created, you can now interact with it. Go to the ‘Write contract’ tab and you will see the placeholders that we specified. Let’s connect Etherscan to Metamask and input Toyota Soarer and 280 HP. Refresh the page and you will see the data in the ‘Read’ section.
    Image by U.Today
    Summary
    
    To sum up, building smart contracts is a <a rel="nofollow" target="_blank" href="#-certification-how-to-get-certified-in-blockchain-technology">necessary skill for every blockchain engineer</a> out there. The first platform to introduce smart contracts was Ethereum; the Foundation also presented the community with a specially written language called Solidity. Despite the novelty of the concept and the language itself, building smart contracts has nothing exceptional to it. People who have previous experience with object-oriented programming languages, especially JavaScript, will feel very comfortable getting around.
    
    <a rel="nofollow" target="_blank" href="#-developer-salaries-top-job-offers-of-blockchain-companies">card</a>
    
    Smart contracts are just programs, but they have distinctive features that every developer should keep in mind. For instance, because smart contracts run on top of a decentralized VM, their data is getting immutably <a rel="nofollow" target="_blank" href="#">recorded into the blockchain</a>. Hence, there is no room for mistakes, and testing a smart contract is crucial before it gets deployed to the Main Network. Another characteristic of a smart contract is that every action is done through transactions, which require the EVM’s processing power. As a consequence, deploying smart contracts and interacting with them costs money.
    
    Today we explained the principles of functioning for Ethereum Main and test networks, showed you various tools for creating smart contracts, and outlined the process of making two types of contracts. We also demonstrated different ways of launching smart contracts on the network and further interaction with them. We believe that with this knowledge you will be ready to start out as a blockchain engineer and build exceptional smart contracts and decentralized apps based on them. Enjoy!
    ') (Line: 1133)
    Drupal\cryptocompare\TwigExtension\RemoveSpace->formatbody(Array) (Line: 54)
    __TwigTemplate_75845256f703f5319a38e035b4af7dd9->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 379)
    Twig\Template->render(Array, Array) (Line: 40)
    Twig\TemplateWrapper->render(Array) (Line: 53)
    twig_render_template('themes/cryptod/templates/field--body.html.twig', Array) (Line: 372)
    Drupal\Core\Theme\ThemeManager->render('field', Array) (Line: 436)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 474)
    Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 1002)
    __TwigTemplate_625426e732c5f7a66fde6d628d98a6b2->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 62)
    __TwigTemplate_e934e56c1e459c359b150360c7169113->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 379)
    Twig\Template->render(Array, Array) (Line: 40)
    Twig\TemplateWrapper->render(Array) (Line: 53)
    twig_render_template('themes/cryptod/templates/node.html.twig', Array) (Line: 372)
    Drupal\Core\Theme\ThemeManager->render('node', Array) (Line: 436)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 238)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 583)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 239)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 187)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 191)
    Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 128)
    Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 82)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 18)
    
  • Warning: Undefined array key 1 in Drupal\cryptocompare\TwigExtension\RemoveSpace::getcard() (line 3114 of modules/custom/cryptocompare/src/TwigExtension/RemoveSpace.php).
    Drupal\cryptocompare\TwigExtension\RemoveSpace::getcard('
                        Contents
                        <ul class="article__contents-list"><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h237">How does Ethereum work?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h389">How does a smart contract work?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h418">How to build a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h52">How to make a token on Ethereum?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h679">How to launch a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h784">What can you do with a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h841">Summary</a></li></ul>
        Advertisement
        
            
        
        
            
                    
                
                                                
    
            
            
                    
                
                                                
    
            
        
    In over 10 years of its development, blockchain has significantly expanded from a straightforward system of transferring value via coins to the basis for Web 3.0 and decentralized applications. This happened mainly due to an outbreak of new projects after Ethereum introduced its ERC20 standard and smart contracts.
    
    Smart contracts were widely used in <a href="https://u.today/ico-market-is-dead-only-118-mln-raised-in-q1-wsj-report">fundraising for ICOs</a> to ensure transparency for participants. This made them famous, but that wasn’t the only implementation. There are successful cases for smart contracts in creating distributed autonomous organizations, managing rights and conducting governance. The need for them is so apparent that there even are attempts to create smart contracts for Bitcoin.
    
    If you want to make it in blockchain engineering, you will have to know how smart contracts work and how to build them. Fortunately, Ethereum provides everything necessary for it, including its own programming language – Solidity. This language was specifically created for working with Ethereum Virtual Machine. It’s pretty easy to understand, but there are some distinctive features that you will have to keep in mind. Today we will dive deep into the world of smart contracts and code some of them. Let’s roll!
    
    <a rel="nofollow" target="_blank" href="#-programming-how-many-programming-languages-do-you-need-for-blockchain">card</a>
    
    How does Ethereum work?
    
    First and foremost, we need to understand the basics of Ethereum’s blockchain and how it functions. This will help us in understanding the difference between coding centralized apps and <a rel="nofollow" target="_blank" href="#">decentralized ones</a> (which smart contracts basically are).
    
    In its core, Ethereum has a virtual machine. You probably encountered them earlier when launching old games in an emulator or running some OS-specific software on the non-native OS. In simple terms, a virtual machine is a computer inside a computer, which uses the hardware of the original machine. In the case of Ethereum, the EVM uses resources of computers connected to the network (nodes). Like any other VM, Ethereum’s one has its own RAM and ROM, which are used by programs running on top of it. The only significant exception is that the EVM’s ROM memory is blockchain, so once something gets there, it cannot be removed.
    
    Why is this important? Creating smart contracts is an incredible responsibility because in case there is a vulnerability, users’ money can be stolen or lost. While you can amend or completely replace the code on a centralized server, you can’t do so on a decentralized VM. In addition, every action performed with a smart contract requires paying fees to the network. Why? Because the EVM has to process a given task, and it can only do it by utilizing some nodes’ processing power. <a rel="nofollow" target="_blank" href="#">The fees are called Gas</a> and are paid in Gwei, which are similar to Bitcoin’s Satoshi.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    How does a smart contract work?
    
    If you have ever used Ethereum, then you generated a wallet in the network at least once. Your wallet is basically an account that is represented by an address. A smart contract is an account too, but it differs from a regular account.
    
    Once you create a wallet, you can connect it to any of the available networks, and the only thing that will change is your balance. This is because user addresses are not internal to any network – they exist in parallel. On the other hand, an instance of a <a rel="nofollow" target="_blank" href="#">smart contract</a> is included in a given blockchain and can’t be viewed in another chain. To be able to launch a contract on the Main Network after testing it in, let’s say Rinkeby, its creator will need to deploy a new instance of the contract. This will require compiling the source code once again. A contract creator can make as many instances of the code as they wish, as long as they have enough ETH to pay Gas for interacting with the network.
    
    Just like a centralized application, a smart contract has a dedicated place for storing its resources and a placeholder for code (picture it as a web page that you download from the internet, it will have a .html file and some folders with pictures and other stuff). There is also an indication of how much ETH a contract has on its balance; this is crucial for various use cases like fundraising, wills, etc. The full schematic is presented in the picture below.
    Image by U.Today
    How to build a smart contract?
    
    Now that we know the principles of how <a href="https://u.today/ethereum">Ethereum</a> functions and how smart contracts run within it, we can create one by ourselves. First, we will need to set up the proper environment. You can go two routes, either writing the code in browser with the Remix IDE provided by Ethereum Foundation or downloading a coding app of your choice along with some packages for it. For the purpose of this tutorial, we will show you both Remix and Atom (a universal coding app for Mac &amp; Windows).
    
    The first smart contract that we are going to make will take the names of different car models and their horsepower. What we want to see is two input and two output fields for the values we have. Since we are going to change the inputs, we need to set variables of two types: a string for the names and an integer for the horsepower.
    
    Before our contract, we will put a line that specifies the language version, as this will help the compiler to adapt the code properly. Compiling the code is necessary because once we get to the deployment stage, the code will be adapted to the machine code that the EVM will be able to read and process. In addition, an application binary interface will be created that could be used further in creating a full-fledged <a rel="nofollow" target="_blank" href="#">decentralized application</a>. You can always check the latest version of Solidity on its official website. To specify it, type ‘pragma solidity’ and the version after the ‘^’ sign.
    
    <a href="https://u.today/no-dice-ethereum-loses-dapps-race-to-eos-and-tron">card</a>
    
    To set borders for our contract, let’s give it a name followed by a parenthesis. First, we will declare two variables: ‘model’ and ‘HP’. The first one will be a string that represents a car model and the second one will be an integer that represents a given car model’s horsepower. There are two things to mention about variables in terms of Solidity. The former is that we should declare whether or not third parties should be able to see them by putting ‘public’ or ‘private’ next to them. The latter is that the integers in Solidity can be signed or unsigned. An unsigned integer can only be possible and is written as ‘uint’. A signed integer can be both positive &amp; negative and is written as ‘int’.
    
    After we have our variables specified, it’s time to write some functions that our smart contract will perform. Firstly, we need to make placeholders for our model and HP. We will use a couple of set public functions and attach our variables to them. Secondly, we need to outline a couple of get public functions that will return an input. Take a look at how our smart contract looks.
    Image by U.Today
    How to make a token on Ethereum?
    
    Many people were attracted to the blockchain industry because of the <a rel="nofollow" target="_blank" href="#">outrageous gains on the crypto market at the end of 2017</a>. This was mainly the outcome of the advent of the new ERC20 token standard that a huge number of startups used to launch their tokens and raise money for development. Despite the decline in the hype, some companies are still launching their tokens on Ethereum and are selling to contributors, proving that the demand is still there. If you have always dreamt of building a startup and launching your token, this part will be very interesting.
    
    Creating a token means using the ERC20 standard, which can be quickly googled. Don’t forget to set the version of Solidity before the standard itself. There will be a set of functions and a couple of events that are supposed to log actions into the Ethereum’s ledger. Among the functions, you will find the set of necessary ones: one for defining the total supply, one for checking the balance of a given wallet, and one for transferring tokens between wallets. As Ethereum allows controlling wallets from other wallets, you will also find functions that define a target wallet and allow balance for transfer and approve a transaction. Save this into a separate .sol file.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    It’s time to outline our own token. Start with specifications of the version of Solidity. Then import the interface and attach it to a new smart contract via ‘is’. Going further we will:
    
    <ul>
    	<li dir="ltr">
    	First, outline the symbol, the name and the number of decimals in variables. The number of decimals lets the user transfer and store parts of a token, the most common number of decimals is 18. All of these shall be visible to users, so we make them public.
    	</li>
    	<li dir="ltr">
    	Second, we will define the total supply. In the case of this tutorial, it will be fixed and equal to one billion.
    	</li>
    	<li dir="ltr">
    	Third, let’s make mappings for checking balances on wallets and allowances in case of any third party will control an address.
    	</li>
    	<li dir="ltr">
    	Fourth, we will set a function that will be executed only once upon the contract’s deployment (constructor) that will give our address all the tokens we will create.
    	</li>
    	<li dir="ltr">
    	Fifth, we need to create a number of functions that constrain what we have in the interface. The total supply will be taken from the variable we declared before. The balances will be checked according to the mapping. The transfer will only be allowed if a user has some ETH and they are sending less or equal to what than they actually have. Controlling another wallet will be done according to the previously defined mapping and check for allowed amount. Check the code we got below.
    	</li>
    </ul>
    Image by U.Today
    How to launch a smart contract?
    
    You now know how to code simple smart contracts on Solidity. Let’s try to understand how they can be launched on the Ethereum network. First, we need to compile the code. For this purpose, you can either use Remix’s integrated compiler or do it on your machine with the help of Truffle framework. Let’s go the easy route first.
    
    Before we begin it’s necessary that you create an Ethereum account if you haven’t so far. For this, we recommend using Metamask extension which can be installed on top of Chrome, Opera, Firefox and Brave browsers. <a rel="nofollow" target="_blank" href="#">Once you create a wallet</a>, make sure that you save the seed phrase because it will be the only way to access a wallet from other machines or in case you have to reinstall the extension. The advantage of Metamask is that you can switch between various networks. This will come in handy as we are going to use a test network for our experiments. Open the extension and choose Ropsten network. Then, go to the faucet and request some ETH, remember that every operation with the EVM costs Wei.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    Open remix.ethereum.org. You will see a code for a ballot – close the tab with it. Remix has a dedicated browser for .sol files and an integrated compiler. At the top of the browser, there is a cross icon that allows creating new files. Let’s create a file and copy our first smart contract with vehicles and their horsepower. On the right part of the screen, you will see the compiler with a few tabs. In the first tab select a compiler version that corresponds with the version of Solidity you specified and initiate compilation process. If there are no errors, you will be presented with a ‘success’ message. Select the second tab and you will see a section where you should specify an appropriate environment (in our case Web3) and check that your address is correct (it should come down from Metamask). If everything is correct, press ‘deploy’ and you will get a pop-up from Metamask with a transaction. Confirm it and voila, your smart contract is deployed to the network.
    
    Now to something more interesting. If you used a text editor on your computer to write the code, you can deploy it locally by using Node.js. First, download it from the official website or install via a Command Line (Windows) or Terminal (OSX). In our case, we will use OSX, but the process is similar.
    Image by U.Today
    Second, install the Solidity framework Truffle via ‘npm install’.
    Image by U.Today
    Third, make a new folder and create a repo via ‘init’ command.
    Image by U.Today
    Fourth, install HDWalletProvider into this repo via Node.js too.
    Image by U.Today
    Fifth, copy files with the ERC20 interface and your token contract to the ‘contracts’ folder in your repo. Sixth, create the following JavaScript file in your ‘Migrations’ folder within the repo.
    Image by U.Today
    Seventh, you need to modify your Truffle config file. However, there is one extra step required prior to doing so. You will input your wallet’s mnemonic (which you saved earlier) and an API for Ropsten testnet. To get the latter, go to Infura and register a project. On the project’s page, you will see its ID. Use it as an API.
    Image by U.Today
    Finally, you are ready to launch your smart contract. Here’s what the command and the process look like. Should there be any issues, you will be presented with them instead.
    Image by U.Today
    What can you do with a smart contract?
    
    Good work, you have the first two smart contracts deployed. Now what? For starters, you need to verify and publish them. Let’s find them first in any of Ethereum’s block explorers. We will be using <a rel="nofollow" target="_blank" href="https://etherscan.io/">Etherscan</a>.
    
    The quickest way to access your contracts is through the transactions from your wallet. Access Metamask, press three dots, and it will let you view your account in Etherscan. Your address now has at least one transaction for contract creation. Accessing a transaction, you will find a destination address, which is essentially a contract’s address. Once you end up on the contract’s page, you will see a few tabs. Go to the ‘Code’ tab and proceed to the verification page. Here input all the relevant information and press the button to start the verification process.
    Image by U.Today
    After you verify the first smart contract that we created, you can now interact with it. Go to the ‘Write contract’ tab and you will see the placeholders that we specified. Let’s connect Etherscan to Metamask and input Toyota Soarer and 280 HP. Refresh the page and you will see the data in the ‘Read’ section.
    Image by U.Today
    Summary
    
    To sum up, building smart contracts is a <a rel="nofollow" target="_blank" href="#-certification-how-to-get-certified-in-blockchain-technology">necessary skill for every blockchain engineer</a> out there. The first platform to introduce smart contracts was Ethereum; the Foundation also presented the community with a specially written language called Solidity. Despite the novelty of the concept and the language itself, building smart contracts has nothing exceptional to it. People who have previous experience with object-oriented programming languages, especially JavaScript, will feel very comfortable getting around.
    
    <a rel="nofollow" target="_blank" href="#-developer-salaries-top-job-offers-of-blockchain-companies">card</a>
    
    Smart contracts are just programs, but they have distinctive features that every developer should keep in mind. For instance, because smart contracts run on top of a decentralized VM, their data is getting immutably <a rel="nofollow" target="_blank" href="#">recorded into the blockchain</a>. Hence, there is no room for mistakes, and testing a smart contract is crucial before it gets deployed to the Main Network. Another characteristic of a smart contract is that every action is done through transactions, which require the EVM’s processing power. As a consequence, deploying smart contracts and interacting with them costs money.
    
    Today we explained the principles of functioning for Ethereum Main and test networks, showed you various tools for creating smart contracts, and outlined the process of making two types of contracts. We also demonstrated different ways of launching smart contracts on the network and further interaction with them. We believe that with this knowledge you will be ready to start out as a blockchain engineer and build exceptional smart contracts and decentralized apps based on them. Enjoy!
    ') (Line: 1133)
    Drupal\cryptocompare\TwigExtension\RemoveSpace->formatbody(Array) (Line: 54)
    __TwigTemplate_75845256f703f5319a38e035b4af7dd9->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 379)
    Twig\Template->render(Array, Array) (Line: 40)
    Twig\TemplateWrapper->render(Array) (Line: 53)
    twig_render_template('themes/cryptod/templates/field--body.html.twig', Array) (Line: 372)
    Drupal\Core\Theme\ThemeManager->render('field', Array) (Line: 436)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 474)
    Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 1002)
    __TwigTemplate_625426e732c5f7a66fde6d628d98a6b2->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 62)
    __TwigTemplate_e934e56c1e459c359b150360c7169113->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 379)
    Twig\Template->render(Array, Array) (Line: 40)
    Twig\TemplateWrapper->render(Array) (Line: 53)
    twig_render_template('themes/cryptod/templates/node.html.twig', Array) (Line: 372)
    Drupal\Core\Theme\ThemeManager->render('node', Array) (Line: 436)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 238)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 583)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 239)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 187)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 191)
    Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 128)
    Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 82)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 18)
    
  • Warning: Undefined array key 1 in Drupal\cryptocompare\TwigExtension\RemoveSpace::getcard() (line 3111 of modules/custom/cryptocompare/src/TwigExtension/RemoveSpace.php).
    Drupal\cryptocompare\TwigExtension\RemoveSpace::getcard('
                        Contents
                        <ul class="article__contents-list"><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h237">How does Ethereum work?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h389">How does a smart contract work?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h418">How to build a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h52">How to make a token on Ethereum?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h679">How to launch a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h784">What can you do with a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h841">Summary</a></li></ul>
        Advertisement
        
            
        
        
            
                    
                
                                                
    
            
            
                    
                
                                                
    
            
        
    In over 10 years of its development, blockchain has significantly expanded from a straightforward system of transferring value via coins to the basis for Web 3.0 and decentralized applications. This happened mainly due to an outbreak of new projects after Ethereum introduced its ERC20 standard and smart contracts.
    
    Smart contracts were widely used in <a href="https://u.today/ico-market-is-dead-only-118-mln-raised-in-q1-wsj-report">fundraising for ICOs</a> to ensure transparency for participants. This made them famous, but that wasn’t the only implementation. There are successful cases for smart contracts in creating distributed autonomous organizations, managing rights and conducting governance. The need for them is so apparent that there even are attempts to create smart contracts for Bitcoin.
    
    If you want to make it in blockchain engineering, you will have to know how smart contracts work and how to build them. Fortunately, Ethereum provides everything necessary for it, including its own programming language – Solidity. This language was specifically created for working with Ethereum Virtual Machine. It’s pretty easy to understand, but there are some distinctive features that you will have to keep in mind. Today we will dive deep into the world of smart contracts and code some of them. Let’s roll!
    
    <a rel="nofollow" target="_blank" href="#-programming-how-many-programming-languages-do-you-need-for-blockchain">card</a>
    
    How does Ethereum work?
    
    First and foremost, we need to understand the basics of Ethereum’s blockchain and how it functions. This will help us in understanding the difference between coding centralized apps and <a rel="nofollow" target="_blank" href="#">decentralized ones</a> (which smart contracts basically are).
    
    In its core, Ethereum has a virtual machine. You probably encountered them earlier when launching old games in an emulator or running some OS-specific software on the non-native OS. In simple terms, a virtual machine is a computer inside a computer, which uses the hardware of the original machine. In the case of Ethereum, the EVM uses resources of computers connected to the network (nodes). Like any other VM, Ethereum’s one has its own RAM and ROM, which are used by programs running on top of it. The only significant exception is that the EVM’s ROM memory is blockchain, so once something gets there, it cannot be removed.
    
    Why is this important? Creating smart contracts is an incredible responsibility because in case there is a vulnerability, users’ money can be stolen or lost. While you can amend or completely replace the code on a centralized server, you can’t do so on a decentralized VM. In addition, every action performed with a smart contract requires paying fees to the network. Why? Because the EVM has to process a given task, and it can only do it by utilizing some nodes’ processing power. <a rel="nofollow" target="_blank" href="#">The fees are called Gas</a> and are paid in Gwei, which are similar to Bitcoin’s Satoshi.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    How does a smart contract work?
    
    If you have ever used Ethereum, then you generated a wallet in the network at least once. Your wallet is basically an account that is represented by an address. A smart contract is an account too, but it differs from a regular account.
    
    Once you create a wallet, you can connect it to any of the available networks, and the only thing that will change is your balance. This is because user addresses are not internal to any network – they exist in parallel. On the other hand, an instance of a <a rel="nofollow" target="_blank" href="#">smart contract</a> is included in a given blockchain and can’t be viewed in another chain. To be able to launch a contract on the Main Network after testing it in, let’s say Rinkeby, its creator will need to deploy a new instance of the contract. This will require compiling the source code once again. A contract creator can make as many instances of the code as they wish, as long as they have enough ETH to pay Gas for interacting with the network.
    
    Just like a centralized application, a smart contract has a dedicated place for storing its resources and a placeholder for code (picture it as a web page that you download from the internet, it will have a .html file and some folders with pictures and other stuff). There is also an indication of how much ETH a contract has on its balance; this is crucial for various use cases like fundraising, wills, etc. The full schematic is presented in the picture below.
    Image by U.Today
    How to build a smart contract?
    
    Now that we know the principles of how <a href="https://u.today/ethereum">Ethereum</a> functions and how smart contracts run within it, we can create one by ourselves. First, we will need to set up the proper environment. You can go two routes, either writing the code in browser with the Remix IDE provided by Ethereum Foundation or downloading a coding app of your choice along with some packages for it. For the purpose of this tutorial, we will show you both Remix and Atom (a universal coding app for Mac &amp; Windows).
    
    The first smart contract that we are going to make will take the names of different car models and their horsepower. What we want to see is two input and two output fields for the values we have. Since we are going to change the inputs, we need to set variables of two types: a string for the names and an integer for the horsepower.
    
    Before our contract, we will put a line that specifies the language version, as this will help the compiler to adapt the code properly. Compiling the code is necessary because once we get to the deployment stage, the code will be adapted to the machine code that the EVM will be able to read and process. In addition, an application binary interface will be created that could be used further in creating a full-fledged <a rel="nofollow" target="_blank" href="#">decentralized application</a>. You can always check the latest version of Solidity on its official website. To specify it, type ‘pragma solidity’ and the version after the ‘^’ sign.
    
    <a href="https://u.today/no-dice-ethereum-loses-dapps-race-to-eos-and-tron">card</a>
    
    To set borders for our contract, let’s give it a name followed by a parenthesis. First, we will declare two variables: ‘model’ and ‘HP’. The first one will be a string that represents a car model and the second one will be an integer that represents a given car model’s horsepower. There are two things to mention about variables in terms of Solidity. The former is that we should declare whether or not third parties should be able to see them by putting ‘public’ or ‘private’ next to them. The latter is that the integers in Solidity can be signed or unsigned. An unsigned integer can only be possible and is written as ‘uint’. A signed integer can be both positive &amp; negative and is written as ‘int’.
    
    After we have our variables specified, it’s time to write some functions that our smart contract will perform. Firstly, we need to make placeholders for our model and HP. We will use a couple of set public functions and attach our variables to them. Secondly, we need to outline a couple of get public functions that will return an input. Take a look at how our smart contract looks.
    Image by U.Today
    How to make a token on Ethereum?
    
    Many people were attracted to the blockchain industry because of the <a rel="nofollow" target="_blank" href="#">outrageous gains on the crypto market at the end of 2017</a>. This was mainly the outcome of the advent of the new ERC20 token standard that a huge number of startups used to launch their tokens and raise money for development. Despite the decline in the hype, some companies are still launching their tokens on Ethereum and are selling to contributors, proving that the demand is still there. If you have always dreamt of building a startup and launching your token, this part will be very interesting.
    
    Creating a token means using the ERC20 standard, which can be quickly googled. Don’t forget to set the version of Solidity before the standard itself. There will be a set of functions and a couple of events that are supposed to log actions into the Ethereum’s ledger. Among the functions, you will find the set of necessary ones: one for defining the total supply, one for checking the balance of a given wallet, and one for transferring tokens between wallets. As Ethereum allows controlling wallets from other wallets, you will also find functions that define a target wallet and allow balance for transfer and approve a transaction. Save this into a separate .sol file.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    It’s time to outline our own token. Start with specifications of the version of Solidity. Then import the interface and attach it to a new smart contract via ‘is’. Going further we will:
    
    <ul>
    	<li dir="ltr">
    	First, outline the symbol, the name and the number of decimals in variables. The number of decimals lets the user transfer and store parts of a token, the most common number of decimals is 18. All of these shall be visible to users, so we make them public.
    	</li>
    	<li dir="ltr">
    	Second, we will define the total supply. In the case of this tutorial, it will be fixed and equal to one billion.
    	</li>
    	<li dir="ltr">
    	Third, let’s make mappings for checking balances on wallets and allowances in case of any third party will control an address.
    	</li>
    	<li dir="ltr">
    	Fourth, we will set a function that will be executed only once upon the contract’s deployment (constructor) that will give our address all the tokens we will create.
    	</li>
    	<li dir="ltr">
    	Fifth, we need to create a number of functions that constrain what we have in the interface. The total supply will be taken from the variable we declared before. The balances will be checked according to the mapping. The transfer will only be allowed if a user has some ETH and they are sending less or equal to what than they actually have. Controlling another wallet will be done according to the previously defined mapping and check for allowed amount. Check the code we got below.
    	</li>
    </ul>
    Image by U.Today
    How to launch a smart contract?
    
    You now know how to code simple smart contracts on Solidity. Let’s try to understand how they can be launched on the Ethereum network. First, we need to compile the code. For this purpose, you can either use Remix’s integrated compiler or do it on your machine with the help of Truffle framework. Let’s go the easy route first.
    
    Before we begin it’s necessary that you create an Ethereum account if you haven’t so far. For this, we recommend using Metamask extension which can be installed on top of Chrome, Opera, Firefox and Brave browsers. <a rel="nofollow" target="_blank" href="#">Once you create a wallet</a>, make sure that you save the seed phrase because it will be the only way to access a wallet from other machines or in case you have to reinstall the extension. The advantage of Metamask is that you can switch between various networks. This will come in handy as we are going to use a test network for our experiments. Open the extension and choose Ropsten network. Then, go to the faucet and request some ETH, remember that every operation with the EVM costs Wei.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    Open remix.ethereum.org. You will see a code for a ballot – close the tab with it. Remix has a dedicated browser for .sol files and an integrated compiler. At the top of the browser, there is a cross icon that allows creating new files. Let’s create a file and copy our first smart contract with vehicles and their horsepower. On the right part of the screen, you will see the compiler with a few tabs. In the first tab select a compiler version that corresponds with the version of Solidity you specified and initiate compilation process. If there are no errors, you will be presented with a ‘success’ message. Select the second tab and you will see a section where you should specify an appropriate environment (in our case Web3) and check that your address is correct (it should come down from Metamask). If everything is correct, press ‘deploy’ and you will get a pop-up from Metamask with a transaction. Confirm it and voila, your smart contract is deployed to the network.
    
    Now to something more interesting. If you used a text editor on your computer to write the code, you can deploy it locally by using Node.js. First, download it from the official website or install via a Command Line (Windows) or Terminal (OSX). In our case, we will use OSX, but the process is similar.
    Image by U.Today
    Second, install the Solidity framework Truffle via ‘npm install’.
    Image by U.Today
    Third, make a new folder and create a repo via ‘init’ command.
    Image by U.Today
    Fourth, install HDWalletProvider into this repo via Node.js too.
    Image by U.Today
    Fifth, copy files with the ERC20 interface and your token contract to the ‘contracts’ folder in your repo. Sixth, create the following JavaScript file in your ‘Migrations’ folder within the repo.
    Image by U.Today
    Seventh, you need to modify your Truffle config file. However, there is one extra step required prior to doing so. You will input your wallet’s mnemonic (which you saved earlier) and an API for Ropsten testnet. To get the latter, go to Infura and register a project. On the project’s page, you will see its ID. Use it as an API.
    Image by U.Today
    Finally, you are ready to launch your smart contract. Here’s what the command and the process look like. Should there be any issues, you will be presented with them instead.
    Image by U.Today
    What can you do with a smart contract?
    
    Good work, you have the first two smart contracts deployed. Now what? For starters, you need to verify and publish them. Let’s find them first in any of Ethereum’s block explorers. We will be using <a rel="nofollow" target="_blank" href="https://etherscan.io/">Etherscan</a>.
    
    The quickest way to access your contracts is through the transactions from your wallet. Access Metamask, press three dots, and it will let you view your account in Etherscan. Your address now has at least one transaction for contract creation. Accessing a transaction, you will find a destination address, which is essentially a contract’s address. Once you end up on the contract’s page, you will see a few tabs. Go to the ‘Code’ tab and proceed to the verification page. Here input all the relevant information and press the button to start the verification process.
    Image by U.Today
    After you verify the first smart contract that we created, you can now interact with it. Go to the ‘Write contract’ tab and you will see the placeholders that we specified. Let’s connect Etherscan to Metamask and input Toyota Soarer and 280 HP. Refresh the page and you will see the data in the ‘Read’ section.
    Image by U.Today
    Summary
    
    To sum up, building smart contracts is a <a rel="nofollow" target="_blank" href="#-certification-how-to-get-certified-in-blockchain-technology">necessary skill for every blockchain engineer</a> out there. The first platform to introduce smart contracts was Ethereum; the Foundation also presented the community with a specially written language called Solidity. Despite the novelty of the concept and the language itself, building smart contracts has nothing exceptional to it. People who have previous experience with object-oriented programming languages, especially JavaScript, will feel very comfortable getting around.
    
    <a rel="nofollow" target="_blank" href="#-developer-salaries-top-job-offers-of-blockchain-companies">card</a>
    
    Smart contracts are just programs, but they have distinctive features that every developer should keep in mind. For instance, because smart contracts run on top of a decentralized VM, their data is getting immutably <a rel="nofollow" target="_blank" href="#">recorded into the blockchain</a>. Hence, there is no room for mistakes, and testing a smart contract is crucial before it gets deployed to the Main Network. Another characteristic of a smart contract is that every action is done through transactions, which require the EVM’s processing power. As a consequence, deploying smart contracts and interacting with them costs money.
    
    Today we explained the principles of functioning for Ethereum Main and test networks, showed you various tools for creating smart contracts, and outlined the process of making two types of contracts. We also demonstrated different ways of launching smart contracts on the network and further interaction with them. We believe that with this knowledge you will be ready to start out as a blockchain engineer and build exceptional smart contracts and decentralized apps based on them. Enjoy!
    ') (Line: 1133)
    Drupal\cryptocompare\TwigExtension\RemoveSpace->formatbody(Array) (Line: 54)
    __TwigTemplate_75845256f703f5319a38e035b4af7dd9->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 379)
    Twig\Template->render(Array, Array) (Line: 40)
    Twig\TemplateWrapper->render(Array) (Line: 53)
    twig_render_template('themes/cryptod/templates/field--body.html.twig', Array) (Line: 372)
    Drupal\Core\Theme\ThemeManager->render('field', Array) (Line: 436)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 474)
    Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 1002)
    __TwigTemplate_625426e732c5f7a66fde6d628d98a6b2->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 62)
    __TwigTemplate_e934e56c1e459c359b150360c7169113->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 379)
    Twig\Template->render(Array, Array) (Line: 40)
    Twig\TemplateWrapper->render(Array) (Line: 53)
    twig_render_template('themes/cryptod/templates/node.html.twig', Array) (Line: 372)
    Drupal\Core\Theme\ThemeManager->render('node', Array) (Line: 436)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 238)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 583)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 239)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 187)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 191)
    Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 128)
    Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 82)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 18)
    
  • Warning: Undefined array key 1 in Drupal\cryptocompare\TwigExtension\RemoveSpace::getcard() (line 3114 of modules/custom/cryptocompare/src/TwigExtension/RemoveSpace.php).
    Drupal\cryptocompare\TwigExtension\RemoveSpace::getcard('
                        Contents
                        <ul class="article__contents-list"><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h237">How does Ethereum work?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h389">How does a smart contract work?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h418">How to build a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h52">How to make a token on Ethereum?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h679">How to launch a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h784">What can you do with a smart contract?</a></li><li class="article__contents-item"><a class="article__contents-link link-pseudo" href="#h841">Summary</a></li></ul>
        Advertisement
        
            
        
        
            
                    
                
                                                
    
            
            
                    
                
                                                
    
            
        
    In over 10 years of its development, blockchain has significantly expanded from a straightforward system of transferring value via coins to the basis for Web 3.0 and decentralized applications. This happened mainly due to an outbreak of new projects after Ethereum introduced its ERC20 standard and smart contracts.
    
    Smart contracts were widely used in <a href="https://u.today/ico-market-is-dead-only-118-mln-raised-in-q1-wsj-report">fundraising for ICOs</a> to ensure transparency for participants. This made them famous, but that wasn’t the only implementation. There are successful cases for smart contracts in creating distributed autonomous organizations, managing rights and conducting governance. The need for them is so apparent that there even are attempts to create smart contracts for Bitcoin.
    
    If you want to make it in blockchain engineering, you will have to know how smart contracts work and how to build them. Fortunately, Ethereum provides everything necessary for it, including its own programming language – Solidity. This language was specifically created for working with Ethereum Virtual Machine. It’s pretty easy to understand, but there are some distinctive features that you will have to keep in mind. Today we will dive deep into the world of smart contracts and code some of them. Let’s roll!
    
    <a rel="nofollow" target="_blank" href="#-programming-how-many-programming-languages-do-you-need-for-blockchain">card</a>
    
    How does Ethereum work?
    
    First and foremost, we need to understand the basics of Ethereum’s blockchain and how it functions. This will help us in understanding the difference between coding centralized apps and <a rel="nofollow" target="_blank" href="#">decentralized ones</a> (which smart contracts basically are).
    
    In its core, Ethereum has a virtual machine. You probably encountered them earlier when launching old games in an emulator or running some OS-specific software on the non-native OS. In simple terms, a virtual machine is a computer inside a computer, which uses the hardware of the original machine. In the case of Ethereum, the EVM uses resources of computers connected to the network (nodes). Like any other VM, Ethereum’s one has its own RAM and ROM, which are used by programs running on top of it. The only significant exception is that the EVM’s ROM memory is blockchain, so once something gets there, it cannot be removed.
    
    Why is this important? Creating smart contracts is an incredible responsibility because in case there is a vulnerability, users’ money can be stolen or lost. While you can amend or completely replace the code on a centralized server, you can’t do so on a decentralized VM. In addition, every action performed with a smart contract requires paying fees to the network. Why? Because the EVM has to process a given task, and it can only do it by utilizing some nodes’ processing power. <a rel="nofollow" target="_blank" href="#">The fees are called Gas</a> and are paid in Gwei, which are similar to Bitcoin’s Satoshi.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    How does a smart contract work?
    
    If you have ever used Ethereum, then you generated a wallet in the network at least once. Your wallet is basically an account that is represented by an address. A smart contract is an account too, but it differs from a regular account.
    
    Once you create a wallet, you can connect it to any of the available networks, and the only thing that will change is your balance. This is because user addresses are not internal to any network – they exist in parallel. On the other hand, an instance of a <a rel="nofollow" target="_blank" href="#">smart contract</a> is included in a given blockchain and can’t be viewed in another chain. To be able to launch a contract on the Main Network after testing it in, let’s say Rinkeby, its creator will need to deploy a new instance of the contract. This will require compiling the source code once again. A contract creator can make as many instances of the code as they wish, as long as they have enough ETH to pay Gas for interacting with the network.
    
    Just like a centralized application, a smart contract has a dedicated place for storing its resources and a placeholder for code (picture it as a web page that you download from the internet, it will have a .html file and some folders with pictures and other stuff). There is also an indication of how much ETH a contract has on its balance; this is crucial for various use cases like fundraising, wills, etc. The full schematic is presented in the picture below.
    Image by U.Today
    How to build a smart contract?
    
    Now that we know the principles of how <a href="https://u.today/ethereum">Ethereum</a> functions and how smart contracts run within it, we can create one by ourselves. First, we will need to set up the proper environment. You can go two routes, either writing the code in browser with the Remix IDE provided by Ethereum Foundation or downloading a coding app of your choice along with some packages for it. For the purpose of this tutorial, we will show you both Remix and Atom (a universal coding app for Mac &amp; Windows).
    
    The first smart contract that we are going to make will take the names of different car models and their horsepower. What we want to see is two input and two output fields for the values we have. Since we are going to change the inputs, we need to set variables of two types: a string for the names and an integer for the horsepower.
    
    Before our contract, we will put a line that specifies the language version, as this will help the compiler to adapt the code properly. Compiling the code is necessary because once we get to the deployment stage, the code will be adapted to the machine code that the EVM will be able to read and process. In addition, an application binary interface will be created that could be used further in creating a full-fledged <a rel="nofollow" target="_blank" href="#">decentralized application</a>. You can always check the latest version of Solidity on its official website. To specify it, type ‘pragma solidity’ and the version after the ‘^’ sign.
    
    <a href="https://u.today/no-dice-ethereum-loses-dapps-race-to-eos-and-tron">card</a>
    
    To set borders for our contract, let’s give it a name followed by a parenthesis. First, we will declare two variables: ‘model’ and ‘HP’. The first one will be a string that represents a car model and the second one will be an integer that represents a given car model’s horsepower. There are two things to mention about variables in terms of Solidity. The former is that we should declare whether or not third parties should be able to see them by putting ‘public’ or ‘private’ next to them. The latter is that the integers in Solidity can be signed or unsigned. An unsigned integer can only be possible and is written as ‘uint’. A signed integer can be both positive &amp; negative and is written as ‘int’.
    
    After we have our variables specified, it’s time to write some functions that our smart contract will perform. Firstly, we need to make placeholders for our model and HP. We will use a couple of set public functions and attach our variables to them. Secondly, we need to outline a couple of get public functions that will return an input. Take a look at how our smart contract looks.
    Image by U.Today
    How to make a token on Ethereum?
    
    Many people were attracted to the blockchain industry because of the <a rel="nofollow" target="_blank" href="#">outrageous gains on the crypto market at the end of 2017</a>. This was mainly the outcome of the advent of the new ERC20 token standard that a huge number of startups used to launch their tokens and raise money for development. Despite the decline in the hype, some companies are still launching their tokens on Ethereum and are selling to contributors, proving that the demand is still there. If you have always dreamt of building a startup and launching your token, this part will be very interesting.
    
    Creating a token means using the ERC20 standard, which can be quickly googled. Don’t forget to set the version of Solidity before the standard itself. There will be a set of functions and a couple of events that are supposed to log actions into the Ethereum’s ledger. Among the functions, you will find the set of necessary ones: one for defining the total supply, one for checking the balance of a given wallet, and one for transferring tokens between wallets. As Ethereum allows controlling wallets from other wallets, you will also find functions that define a target wallet and allow balance for transfer and approve a transaction. Save this into a separate .sol file.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    It’s time to outline our own token. Start with specifications of the version of Solidity. Then import the interface and attach it to a new smart contract via ‘is’. Going further we will:
    
    <ul>
    	<li dir="ltr">
    	First, outline the symbol, the name and the number of decimals in variables. The number of decimals lets the user transfer and store parts of a token, the most common number of decimals is 18. All of these shall be visible to users, so we make them public.
    	</li>
    	<li dir="ltr">
    	Second, we will define the total supply. In the case of this tutorial, it will be fixed and equal to one billion.
    	</li>
    	<li dir="ltr">
    	Third, let’s make mappings for checking balances on wallets and allowances in case of any third party will control an address.
    	</li>
    	<li dir="ltr">
    	Fourth, we will set a function that will be executed only once upon the contract’s deployment (constructor) that will give our address all the tokens we will create.
    	</li>
    	<li dir="ltr">
    	Fifth, we need to create a number of functions that constrain what we have in the interface. The total supply will be taken from the variable we declared before. The balances will be checked according to the mapping. The transfer will only be allowed if a user has some ETH and they are sending less or equal to what than they actually have. Controlling another wallet will be done according to the previously defined mapping and check for allowed amount. Check the code we got below.
    	</li>
    </ul>
    Image by U.Today
    How to launch a smart contract?
    
    You now know how to code simple smart contracts on Solidity. Let’s try to understand how they can be launched on the Ethereum network. First, we need to compile the code. For this purpose, you can either use Remix’s integrated compiler or do it on your machine with the help of Truffle framework. Let’s go the easy route first.
    
    Before we begin it’s necessary that you create an Ethereum account if you haven’t so far. For this, we recommend using Metamask extension which can be installed on top of Chrome, Opera, Firefox and Brave browsers. <a rel="nofollow" target="_blank" href="#">Once you create a wallet</a>, make sure that you save the seed phrase because it will be the only way to access a wallet from other machines or in case you have to reinstall the extension. The advantage of Metamask is that you can switch between various networks. This will come in handy as we are going to use a test network for our experiments. Open the extension and choose Ropsten network. Then, go to the faucet and request some ETH, remember that every operation with the EVM costs Wei.
    
    <a rel="nofollow" target="_blank" href="#">card</a>
    
    Open remix.ethereum.org. You will see a code for a ballot – close the tab with it. Remix has a dedicated browser for .sol files and an integrated compiler. At the top of the browser, there is a cross icon that allows creating new files. Let’s create a file and copy our first smart contract with vehicles and their horsepower. On the right part of the screen, you will see the compiler with a few tabs. In the first tab select a compiler version that corresponds with the version of Solidity you specified and initiate compilation process. If there are no errors, you will be presented with a ‘success’ message. Select the second tab and you will see a section where you should specify an appropriate environment (in our case Web3) and check that your address is correct (it should come down from Metamask). If everything is correct, press ‘deploy’ and you will get a pop-up from Metamask with a transaction. Confirm it and voila, your smart contract is deployed to the network.
    
    Now to something more interesting. If you used a text editor on your computer to write the code, you can deploy it locally by using Node.js. First, download it from the official website or install via a Command Line (Windows) or Terminal (OSX). In our case, we will use OSX, but the process is similar.
    Image by U.Today
    Second, install the Solidity framework Truffle via ‘npm install’.
    Image by U.Today
    Third, make a new folder and create a repo via ‘init’ command.
    Image by U.Today
    Fourth, install HDWalletProvider into this repo via Node.js too.
    Image by U.Today
    Fifth, copy files with the ERC20 interface and your token contract to the ‘contracts’ folder in your repo. Sixth, create the following JavaScript file in your ‘Migrations’ folder within the repo.
    Image by U.Today
    Seventh, you need to modify your Truffle config file. However, there is one extra step required prior to doing so. You will input your wallet’s mnemonic (which you saved earlier) and an API for Ropsten testnet. To get the latter, go to Infura and register a project. On the project’s page, you will see its ID. Use it as an API.
    Image by U.Today
    Finally, you are ready to launch your smart contract. Here’s what the command and the process look like. Should there be any issues, you will be presented with them instead.
    Image by U.Today
    What can you do with a smart contract?
    
    Good work, you have the first two smart contracts deployed. Now what? For starters, you need to verify and publish them. Let’s find them first in any of Ethereum’s block explorers. We will be using <a rel="nofollow" target="_blank" href="https://etherscan.io/">Etherscan</a>.
    
    The quickest way to access your contracts is through the transactions from your wallet. Access Metamask, press three dots, and it will let you view your account in Etherscan. Your address now has at least one transaction for contract creation. Accessing a transaction, you will find a destination address, which is essentially a contract’s address. Once you end up on the contract’s page, you will see a few tabs. Go to the ‘Code’ tab and proceed to the verification page. Here input all the relevant information and press the button to start the verification process.
    Image by U.Today
    After you verify the first smart contract that we created, you can now interact with it. Go to the ‘Write contract’ tab and you will see the placeholders that we specified. Let’s connect Etherscan to Metamask and input Toyota Soarer and 280 HP. Refresh the page and you will see the data in the ‘Read’ section.
    Image by U.Today
    Summary
    
    To sum up, building smart contracts is a <a rel="nofollow" target="_blank" href="#-certification-how-to-get-certified-in-blockchain-technology">necessary skill for every blockchain engineer</a> out there. The first platform to introduce smart contracts was Ethereum; the Foundation also presented the community with a specially written language called Solidity. Despite the novelty of the concept and the language itself, building smart contracts has nothing exceptional to it. People who have previous experience with object-oriented programming languages, especially JavaScript, will feel very comfortable getting around.
    
    <a rel="nofollow" target="_blank" href="#-developer-salaries-top-job-offers-of-blockchain-companies">card</a>
    
    Smart contracts are just programs, but they have distinctive features that every developer should keep in mind. For instance, because smart contracts run on top of a decentralized VM, their data is getting immutably <a rel="nofollow" target="_blank" href="#">recorded into the blockchain</a>. Hence, there is no room for mistakes, and testing a smart contract is crucial before it gets deployed to the Main Network. Another characteristic of a smart contract is that every action is done through transactions, which require the EVM’s processing power. As a consequence, deploying smart contracts and interacting with them costs money.
    
    Today we explained the principles of functioning for Ethereum Main and test networks, showed you various tools for creating smart contracts, and outlined the process of making two types of contracts. We also demonstrated different ways of launching smart contracts on the network and further interaction with them. We believe that with this knowledge you will be ready to start out as a blockchain engineer and build exceptional smart contracts and decentralized apps based on them. Enjoy!
    ') (Line: 1133)
    Drupal\cryptocompare\TwigExtension\RemoveSpace->formatbody(Array) (Line: 54)
    __TwigTemplate_75845256f703f5319a38e035b4af7dd9->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 379)
    Twig\Template->render(Array, Array) (Line: 40)
    Twig\TemplateWrapper->render(Array) (Line: 53)
    twig_render_template('themes/cryptod/templates/field--body.html.twig', Array) (Line: 372)
    Drupal\Core\Theme\ThemeManager->render('field', Array) (Line: 436)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 474)
    Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 1002)
    __TwigTemplate_625426e732c5f7a66fde6d628d98a6b2->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 62)
    __TwigTemplate_e934e56c1e459c359b150360c7169113->doDisplay(Array, Array) (Line: 394)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
    Twig\Template->display(Array) (Line: 379)
    Twig\Template->render(Array, Array) (Line: 40)
    Twig\TemplateWrapper->render(Array) (Line: 53)
    twig_render_template('themes/cryptod/templates/node.html.twig', Array) (Line: 372)
    Drupal\Core\Theme\ThemeManager->render('node', Array) (Line: 436)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 238)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 583)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 239)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 187)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 191)
    Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 128)
    Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 82)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 18)
    

Blockchain Programming: Solidity. Programming the Smart Contracts

Advertisement
Mon, 3/06/2019 - 13:12
Blockchain Programming: Solidity. Programming the Smart Contracts
Cover image via U.Today
Read U.TODAY on
Google News
Contents
Advertisement

In over 10 years of its development, blockchain has significantly expanded from a straightforward system of transferring value via coins to the basis for Web 3.0 and decentralized applications. This happened mainly due to an outbreak of new projects after Ethereum introduced its ERC20 standard and smart contracts.

Smart contracts were widely used in fundraising for ICOs to ensure transparency for participants. This made them famous, but that wasn’t the only implementation. There are successful cases for smart contracts in creating distributed autonomous organizations, managing rights and conducting governance. The need for them is so apparent that there even are attempts to create smart contracts for Bitcoin.

If you want to make it in blockchain engineering, you will have to know how smart contracts work and how to build them. Fortunately, Ethereum provides everything necessary for it, including its own programming language – Solidity. This language was specifically created for working with Ethereum Virtual Machine. It’s pretty easy to understand, but there are some distinctive features that you will have to keep in mind. Today we will dive deep into the world of smart contracts and code some of them. Let’s roll!

card

How does Ethereum work?

First and foremost, we need to understand the basics of Ethereum’s blockchain and how it functions. This will help us in understanding the difference between coding centralized apps and decentralized ones (which smart contracts basically are).

In its core, Ethereum has a virtual machine. You probably encountered them earlier when launching old games in an emulator or running some OS-specific software on the non-native OS. In simple terms, a virtual machine is a computer inside a computer, which uses the hardware of the original machine. In the case of Ethereum, the EVM uses resources of computers connected to the network (nodes). Like any other VM, Ethereum’s one has its own RAM and ROM, which are used by programs running on top of it. The only significant exception is that the EVM’s ROM memory is blockchain, so once something gets there, it cannot be removed.

Why is this important? Creating smart contracts is an incredible responsibility because in case there is a vulnerability, users’ money can be stolen or lost. While you can amend or completely replace the code on a centralized server, you can’t do so on a decentralized VM. In addition, every action performed with a smart contract requires paying fees to the network. Why? Because the EVM has to process a given task, and it can only do it by utilizing some nodes’ processing power. The fees are called Gas and are paid in Gwei, which are similar to Bitcoin’s Satoshi.

card

How does a smart contract work?

If you have ever used Ethereum, then you generated a wallet in the network at least once. Your wallet is basically an account that is represented by an address. A smart contract is an account too, but it differs from a regular account.

Once you create a wallet, you can connect it to any of the available networks, and the only thing that will change is your balance. This is because user addresses are not internal to any network – they exist in parallel. On the other hand, an instance of a smart contract is included in a given blockchain and can’t be viewed in another chain. To be able to launch a contract on the Main Network after testing it in, let’s say Rinkeby, its creator will need to deploy a new instance of the contract. This will require compiling the source code once again. A contract creator can make as many instances of the code as they wish, as long as they have enough ETH to pay Gas for interacting with the network.

Just like a centralized application, a smart contract has a dedicated place for storing its resources and a placeholder for code (picture it as a web page that you download from the internet, it will have a .html file and some folders with pictures and other stuff). There is also an indication of how much ETH a contract has on its balance; this is crucial for various use cases like fundraising, wills, etc. The full schematic is presented in the picture below.

Ethereum EVM & Wallet schematic
Image by U.Today

How to build a smart contract?

Now that we know the principles of how Ethereum functions and how smart contracts run within it, we can create one by ourselves. First, we will need to set up the proper environment. You can go two routes, either writing the code in browser with the Remix IDE provided by Ethereum Foundation or downloading a coding app of your choice along with some packages for it. For the purpose of this tutorial, we will show you both Remix and Atom (a universal coding app for Mac & Windows).

The first smart contract that we are going to make will take the names of different car models and their horsepower. What we want to see is two input and two output fields for the values we have. Since we are going to change the inputs, we need to set variables of two types: a string for the names and an integer for the horsepower.

Before our contract, we will put a line that specifies the language version, as this will help the compiler to adapt the code properly. Compiling the code is necessary because once we get to the deployment stage, the code will be adapted to the machine code that the EVM will be able to read and process. In addition, an application binary interface will be created that could be used further in creating a full-fledged decentralized application. You can always check the latest version of Solidity on its official website. To specify it, type ‘pragma solidity’ and the version after the ‘^’ sign.

To set borders for our contract, let’s give it a name followed by a parenthesis. First, we will declare two variables: ‘model’ and ‘HP’. The first one will be a string that represents a car model and the second one will be an integer that represents a given car model’s horsepower. There are two things to mention about variables in terms of Solidity. The former is that we should declare whether or not third parties should be able to see them by putting ‘public’ or ‘private’ next to them. The latter is that the integers in Solidity can be signed or unsigned. An unsigned integer can only be possible and is written as ‘uint’. A signed integer can be both positive & negative and is written as ‘int’.

After we have our variables specified, it’s time to write some functions that our smart contract will perform. Firstly, we need to make placeholders for our model and HP. We will use a couple of set public functions and attach our variables to them. Secondly, we need to outline a couple of get public functions that will return an input. Take a look at how our smart contract looks.

Vehicle model & horsepower smart contract
Image by U.Today

How to make a token on Ethereum?

Many people were attracted to the blockchain industry because of the outrageous gains on the crypto market at the end of 2017. This was mainly the outcome of the advent of the new ERC20 token standard that a huge number of startups used to launch their tokens and raise money for development. Despite the decline in the hype, some companies are still launching their tokens on Ethereum and are selling to contributors, proving that the demand is still there. If you have always dreamt of building a startup and launching your token, this part will be very interesting.

Creating a token means using the ERC20 standard, which can be quickly googled. Don’t forget to set the version of Solidity before the standard itself. There will be a set of functions and a couple of events that are supposed to log actions into the Ethereum’s ledger. Among the functions, you will find the set of necessary ones: one for defining the total supply, one for checking the balance of a given wallet, and one for transferring tokens between wallets. As Ethereum allows controlling wallets from other wallets, you will also find functions that define a target wallet and allow balance for transfer and approve a transaction. Save this into a separate .sol file.

card

It’s time to outline our own token. Start with specifications of the version of Solidity. Then import the interface and attach it to a new smart contract via ‘is’. Going further we will:

  • First, outline the symbol, the name and the number of decimals in variables. The number of decimals lets the user transfer and store parts of a token, the most common number of decimals is 18. All of these shall be visible to users, so we make them public.

  • Second, we will define the total supply. In the case of this tutorial, it will be fixed and equal to one billion.

  • Third, let’s make mappings for checking balances on wallets and allowances in case of any third party will control an address.

  • Fourth, we will set a function that will be executed only once upon the contract’s deployment (constructor) that will give our address all the tokens we will create.

  • Fifth, we need to create a number of functions that constrain what we have in the interface. The total supply will be taken from the variable we declared before. The balances will be checked according to the mapping. The transfer will only be allowed if a user has some ETH and they are sending less or equal to what than they actually have. Controlling another wallet will be done according to the previously defined mapping and check for allowed amount. Check the code we got below.

UTD ERC20 token
Image by U.Today

How to launch a smart contract?

You now know how to code simple smart contracts on Solidity. Let’s try to understand how they can be launched on the Ethereum network. First, we need to compile the code. For this purpose, you can either use Remix’s integrated compiler or do it on your machine with the help of Truffle framework. Let’s go the easy route first.

Before we begin it’s necessary that you create an Ethereum account if you haven’t so far. For this, we recommend using Metamask extension which can be installed on top of Chrome, Opera, Firefox and Brave browsers. Once you create a wallet, make sure that you save the seed phrase because it will be the only way to access a wallet from other machines or in case you have to reinstall the extension. The advantage of Metamask is that you can switch between various networks. This will come in handy as we are going to use a test network for our experiments. Open the extension and choose Ropsten network. Then, go to the faucet and request some ETH, remember that every operation with the EVM costs Wei.

card

Open remix.ethereum.org. You will see a code for a ballot – close the tab with it. Remix has a dedicated browser for .sol files and an integrated compiler. At the top of the browser, there is a cross icon that allows creating new files. Let’s create a file and copy our first smart contract with vehicles and their horsepower. On the right part of the screen, you will see the compiler with a few tabs. In the first tab select a compiler version that corresponds with the version of Solidity you specified and initiate compilation process. If there are no errors, you will be presented with a ‘success’ message. Select the second tab and you will see a section where you should specify an appropriate environment (in our case Web3) and check that your address is correct (it should come down from Metamask). If everything is correct, press ‘deploy’ and you will get a pop-up from Metamask with a transaction. Confirm it and voila, your smart contract is deployed to the network.

Now to something more interesting. If you used a text editor on your computer to write the code, you can deploy it locally by using Node.js. First, download it from the official website or install via a Command Line (Windows) or Terminal (OSX). In our case, we will use OSX, but the process is similar.

Installing Node.js.
Image by U.Today

Second, install the Solidity framework Truffle via ‘npm install’.

Installing Truffle
Image by U.Today

Third, make a new folder and create a repo via ‘init’ command.

Creating a Truffle repo
Image by U.Today

Fourth, install HDWalletProvider into this repo via Node.js too.

Installing HDWalletProvider
Image by U.Today

Fifth, copy files with the ERC20 interface and your token contract to the ‘contracts’ folder in your repo. Sixth, create the following JavaScript file in your ‘Migrations’ folder within the repo.

JS deployment file
Image by U.Today

Seventh, you need to modify your Truffle config file. However, there is one extra step required prior to doing so. You will input your wallet’s mnemonic (which you saved earlier) and an API for Ropsten testnet. To get the latter, go to Infura and register a project. On the project’s page, you will see its ID. Use it as an API.

Truffle config
Image by U.Today

Finally, you are ready to launch your smart contract. Here’s what the command and the process look like. Should there be any issues, you will be presented with them instead.

Smart contract being deployed locally
Image by U.Today

What can you do with a smart contract?

Good work, you have the first two smart contracts deployed. Now what? For starters, you need to verify and publish them. Let’s find them first in any of Ethereum’s block explorers. We will be using Etherscan.

The quickest way to access your contracts is through the transactions from your wallet. Access Metamask, press three dots, and it will let you view your account in Etherscan. Your address now has at least one transaction for contract creation. Accessing a transaction, you will find a destination address, which is essentially a contract’s address. Once you end up on the contract’s page, you will see a few tabs. Go to the ‘Code’ tab and proceed to the verification page. Here input all the relevant information and press the button to start the verification process.

Code verification
Image by U.Today

After you verify the first smart contract that we created, you can now interact with it. Go to the ‘Write contract’ tab and you will see the placeholders that we specified. Let’s connect Etherscan to Metamask and input Toyota Soarer and 280 HP. Refresh the page and you will see the data in the ‘Read’ section.

Smart contract works
Image by U.Today

Summary

To sum up, building smart contracts is a necessary skill for every blockchain engineer out there. The first platform to introduce smart contracts was Ethereum; the Foundation also presented the community with a specially written language called Solidity. Despite the novelty of the concept and the language itself, building smart contracts has nothing exceptional to it. People who have previous experience with object-oriented programming languages, especially JavaScript, will feel very comfortable getting around.

card

Smart contracts are just programs, but they have distinctive features that every developer should keep in mind. For instance, because smart contracts run on top of a decentralized VM, their data is getting immutably recorded into the blockchain. Hence, there is no room for mistakes, and testing a smart contract is crucial before it gets deployed to the Main Network. Another characteristic of a smart contract is that every action is done through transactions, which require the EVM’s processing power. As a consequence, deploying smart contracts and interacting with them costs money.

Today we explained the principles of functioning for Ethereum Main and test networks, showed you various tools for creating smart contracts, and outlined the process of making two types of contracts. We also demonstrated different ways of launching smart contracts on the network and further interaction with them. We believe that with this knowledge you will be ready to start out as a blockchain engineer and build exceptional smart contracts and decentralized apps based on them. Enjoy!

Advertisement
TopCryptoNewsinYourMailbox
TopCryptoNewsinYourMailbox
Advertisement

Latest Press Releases

Our social media
There's a lot to see there, too

Popular articles

Advertisement
AD