Home » Articles » Tutorial – How To Code and Execute Smart Contracts On Ethereum’s Blockchain
Click Here To Hide Tor

Tutorial – How To Code and Execute Smart Contracts On Ethereum’s Blockchain

A smart contract represents an account holding object that runs on ethereum’s blockchain. It includes code functions and can interact with other smart contracts, record data, make decisions and send ether transactions to others. A smart contract is defined by its creator, yet its execution, and the services it offers, is determined by ethereum’s network. It will exist and remain executable so long as ethereum’s blockchain exists, and will only vanish if it was coded to “self destruct”.

So, what can one do with a smart contract? Well, you can literally do almost anything, yet for the purpose of this tutorial, we’ll design a simple smart contract; a classic “Hello World” contract, then you will be able to even build your very own token on Ethereum’s blockchain. Once you are able to create an ethereum based token, you will be able to raise money via crowdfunding, and formulate an entirely transparent organization that relies on democracy to give its citizens full control, will never drift away from its constitution and can never be censored, tampered with or shut down. All of this can be done via no more than 300 lines of code.

The Greeter – Your first smart contract:

The “Greeter” is a simple smart contract that will live on ethereum’s blockchain and will have the capability to engage in conversations with anyone interacting with it, as per its input. The “Greeter” is not a perfect “talker”; however, it is an excellent “listener”. Here is the code:

You can notice that the code includes two separate contracts’ “mortal” and “greeter”. This is due to the fact that Solidity, the smart contract programming language we are using, is characterized by “inheritance”. In other words, a smart contract can inherit features of another contract. This can be extremely useful in simplifying programming of smart contracts, as common contract traits needn’t be rewritten multiple times, and all smart contracts can be programmed in smaller, easily readable chunks of code. Accordingly, via declaration that the “greeter” is “mortal”, the former will inherit all of the characteristics of the “mortal” contract, while keeping greeter’s code simple and easily readable.

Via the inherited characteristic from “mortal”, the “greeter” contract can be killed by its owner only, in order to clean the blockchain and recover coins locked into the “greeter” contract when it is no longer needed. By default, smart contracts across ethereum’s blockchain are immortal and are not owned by anyone. In other words, once a smart contract is deployed, its author will have no special privileges over other users. This has to be considered thoroughly before deploying your smart contract.

Before you can deploy your smart contract on Ethereum’s network, you will need two essential things:

a. Compile the code using a compiler like Solc, or online compilers

b. The Application Binary Interface (ABI); a JavaScript object that determines how interaction with the smart contract should ensue.

Compiling Your Contract’s Code With The Solc Compiler:

1. First, you will have to run your “Geth” client. (Kindly, refer to our tutorial “How To Run a Full Ethereum Node” for full details on how to install Geth). The client will take a few minutes to synchronize with Ethereum’s network as shown on the below screenshot.

2. By default, geth comes with a built in Solidity compiler. To test it, run this command on your geth’s console:


If Solidity is installed, you will be prompted with this:


3. Before compiling your smart contract, you have to reformat your code by omitting line-breaks so that it would fit into a string variable. Online tools like Textfixer.com, will help you do this in a simple way; all you have to do is copy and paste your code into a box and press a button to remove all line-breaks.

4. After removing all line-breaks, compile your smart contract’s code by entering the following into your geth’s command line:

var greeterSource = ‘Enter here your contract’s code after removing line-breaks as shown in the previous step’

var greeterCompiled = web3.eth.compile.solidity(greeterSource)

5. Now, after compiling your code, to deploy it, enter the following into your geth’s command line. You can change the greeting message by editing the first line and entering the message you want. I used here “DeepDotWeb Loves You!”:

Using the Online Solidity Compiler:

Instead of using Solc, you can use the online solidity compiler:

1. Point your browser to the online Solidity compiler, and paste your contract’s code into the box on the left side of the page as shown on the below screenshot:

2. On the right side of the page, choose “Web3 Provider” from the dropdown menu labeled “Execution environment”. You will be prompted, to choose whether or not to connect to an ethereum node; press “ok” to continue.

3. Now, click on the “Settings” tab on the menu bar on the right side of the page. Then, from the dropdown menu, choose the compiler you want to use with your contract. I recommend using the “latest local version” option, as shown on the below screenshot. Press “Compile” to start compiling your code.

4. You will be prompted with text that represents your compiled code. Copy and paste this text into your geth’s command line to execute your smart contract.

Executing Your Smart Contract:

After entering your compiled code into geth’s command line, you will be prompted with a message like the following after around 30-40 seconds.

Contract mined! address: 0x1172dcb797acd1bf436166a316114206dc60aeff

To make sure that your smart contract has been successfully deployed, enter the following into geth’s command line:


If you are prompted with anything other than “0x”, then your smart contract is functioning properly, congratulations!

Note: You will have to unlock your account that is executing the smart contract, using the password you used during creation of your account, because you will have to pay for the gas needed to deploy the contract. To unlock your account, enter the following before executing your contract on geth’s command line:

personal.unlockAccount(web3.eth.accounts[0], “Enter your password here”)

As per the online Solidity Compiler, this smart contract will require approximately 180,000 ether gas to be deployed successfully. At the time of writing of this tutorial, one unit of ether gas costs 0.00000002 ethereum, so with ethereum price today at $163, the required amount of gas equals 180,000 x 0.00000002 x 163 = $0.58. Of course, this amount is relatively high, but take into account that ethereum price has increased by around 1000% during less than 6 months.

Running Your “Greeter” Contract:

To call your “greeter” contract, enter the following into your geth’s command line:


Given the fact that this contract induces no changes to the blockchain, it will be instantly executed without needing any ether gas. Executing our compiled smart contract will return the greeting we chose:

‘DeepDotWeb Loves You’

Deleting Your Smart Contract:

You are probably very excited to have your first ever smart contract live on Ethereum’s blockchain, yet this excitement will mostly wear off when you start writing and executing more complicated contracts. To keep the blockchain tidy, it is highly recommended to remove abandoned, unused smart contracts.

To delete your smart contract, an amount of ether has to be paid to implement changes on the blockchain, i.e. deleting your smart contract using the “self-destruct” function,. This will cost much less than a conventional transaction.

To delete your smart contract, enter the following into geth’s command line:


Note that every smart contract has to deploy its own self-destruct clause. In case of our created contract, only the account that created it can kill it. If you don’t add a “self-destruct” clause to your smart contract, it has the potential to eternally exist on the blockchain .

This was a simple tutorial to help you create your first smart contract on Ethereum’s blockchain. From there, you can work on more complex contracts. If you have any questions, feel free to add them to the comments’ section below and I will be glad to answer them promptly.

One comment

  1. I Love you too DeepDotWeb :::)

Leave a Reply

Your email address will not be published. Required fields are marked *


Captcha: *