Ethereum Smart Contract Development for beginners

Chimezie Chuta
16 min readAug 14, 2019

--

We want to do something about the prevailing blockchain-developer-talent shortage in the ecosystem. We are launching “BlockchainDev1000” initiative. We want to train 1000 blockchain developers in Nigeria on different skillsets within the next 1–2 years. This article is the first step in achieving that.

What is Ethereum?

Bitcoin blockchain is primarily used for sending money between various parties on the blockchain without the need for a central authority such as a bank. A 19-year-old developer by name Vitalik Buterin wanted to apply the idea of decentralization (no central authority) to do more than just money transfer. He wanted to build applications that could run globally without any central authority in control. For example, if you are a Facebook user, Facebook as a company owns your data and they have the right to remove your account if they wish. In 10 years, Facebook could shut down in which case all your data could be lost.

Vitalik proposed to add a scripting language to Bitcoin to be able to build applications. However, after failing to gain agreement from the Bitcoin development team, in January 2014, he officially published the white paper proposing the development of a new platform with a more general scripting language. A team formed shortly and one of the developers, Dr. Gavin Wood soon released the Ethereum yellow paper, which covered the Ethereum Virtual Machine (EVM), the runtime environment that executes all of the smart contracts on the network (You will learn more about EVM and smart contracts as we progress). The development of the platform was funded through a crowdsale in July–August 2014, with the participants buying the Ethereum value token (ether) with bitcoin. They raised about 31,529 BTC ($18.4 million at the time).

Global Computer

Ethereum is a public, blockchain-based distributed computing platform. It can be thought of as one big computer made up of small computers around the world. You can write applications and run them on this global computer. The platform guarantees that your application will always run without any downtime, censorship, fraud or third-party interference. Apart from running applications, Ethereum blockchain can also transfer money between 2 parties without a central authority.

All these computers (also called nodes) are connected to one another and have a full copy of the code and data. When you deploy your code on to the Ethereum blockchain, the code is replicated across all the nodes in the network. When your application stores any data, even that data is replicated across all the nodes. There are thousands of nodes in the network and it is almost impossible for anyone to stop all the nodes. This ensures your application to be always accessible.

Webapp Architecture

One of the best ways to understand Ethereum is by comparing it with a traditional client/server architecture of the www. If you are a web developer, the diagram bellow should make sense (You can still follow along if you are not aware of the web architecture). It is a high-level client/server architecture of a simple web application.

A typical web application consists of server-side code which is usually written in a programming language like Java, C#, Ruby, Python etc. The frontend code is implemented using HTML/CSS/Javascript. This entire application is then hosted on a hosting provider like AWS, Microsoft Azure, Google Cloud Platform, Heroku or a VPS.

Users interact with the web application using a client such as a web browser, curl/wget (command line) or through an API. Note that there is one web application which is centralized and all the clients interact with this one application. When a client makes a request to the server, the server processes the request interacts with the database and/or cache, reads/writes/updates the database and returns a response to the client.

In the next section, we will see how this compares to the working of the Ethereum blockchain.

World Wide Web architecture.

Ethereum Architecture

If you notice the diagram on the bellow, every client (browser) communicates with its own instance of the application. There is no central server to which all clients connect to. This means, in an ideal decentralized world, every person who wants to interact with a dapp (Decentralized Application) will need a full copy of the blockchain running on their computer/phone etc. That means, before you can use an application, you have to download the entire blockchain and then start using the application.

We don’t live in an ideal world and it is unreasonable to expect everyone to run a blockchain server to use these apps. But the idea behind decentralization is to not rely on a single/centralized server. So, the community has come up with solutions (hosted blockchain servers, metamask etc.) where you don’t have to spend lot of your hard disk and RAM downloading and running a full copy of the blockchain but also not compromise on the decentralized aspect. We will evaluate those options in the future lessons.

Now, what exactly is in the Ethereum blockchain? The blockchain has 2 main components:

  1. Database: Every transaction in the network is stored in the blockchain. When you deploy your application, it is considered as a transaction. If you have for example a Voting application that allows anyone to vote for candidates, a vote for a candidate would be considered a transaction. All these transactions are public and anyone can see this and verify. This data can never be tampered with. To make sure all the nodes in the network have the same copy of the data and to ensure no invalid data gets written to this database, Ethereum uses an algorithm called Proof of Work to secure the network. (http://ethereum.stackexchange.com/questions/14/what-proof-of-work-function-does-ethereum-use)
  2. Code: The database aspect of blockchain just stores the transactions. But where is all the logic to vote for a candidate, retrieve the total votes etc. In Ethereum world, you write the logic/application code (called contract) in a language called Solidity. You then use the solidity compiler to compile it to Ethereum Byte Code and then deploy that byte code to the blockchain (There are few other languages you could use to write contracts but solidity is by far the most popular and relatively easier option). So, not only does Ethereum blockchain store the transactions, it also stores and executes the contract code.

So basically, the blockchain stores your data, stores the code and also runs the code in the EVM (Ethereum Virtual Machine). You will learn more about ByteCode and EVM in the future sections.

What are smart contracts?

In the previous section, we discussed how we can write applications in the Solidity programming language and deploy it on to the Ethereum blockchain. In Ethereum, these applications are called Smart Contracts.

You must be wondering why these applications are called smart contracts. In general, contract is a written agreement between two or many parties that is intended to be enforced by law. If we take this written contract and translate it into code and deploy on the blockchain, we get digital contracts. But the beauty of this code on the blockchain is that it can enforce the agreement between parties and that is the reason they are called “smart contracts”.

How can this code enforce the agreement? If you remember, once a contract is deployed to the blockchain, it can neither be stopped nor modified. That is how the agreement is enforced. Let’s take a look at a simple example to understand this better.

Let’s say we want to build a Crowdsale application. There is a creator who wants to raise $10,000 to create a product and there are potential customers who are interested in this product and are willing to pre-pay for it. The agreement is, each customer will contribute anywhere between $10 — $10,000 and if the goal is met, the money is sent to the creator. If not, money is sent back to the contributors. This is an ideal use case to use smart contract to enforce the agreement between the creator and supporters. It is trivial to implement this logic in a centralized application as well. The advantages of implementing this in a smart contract are

  1. The money sent by all the contributors is in the smart contract. Neither the contributor nor the creator can take that money.
  2. The code can be implemented so that the money is sent to the creator as soon as the goal is reached.
  3. The code can be implemented so anyone can trigger a refund to all the customers if the goal is not reached within a certain time frame.
  4. This code cannot be tampered with once it is deployed on to the blockchain.

Ether & Denominations

Just like in the real world, each country has it’s own currency like USD, INR, RNB, GBP, EUR etc, each blockchain has it’s own currency. In the case of Ethereum blockchain, the native currency is called Ether. There are exchanges where you can convert Ether to any other fiat currency like USD or EUR. You can see the current price of Ether here

In the real world, currencies have various denominations. For example, a US Dollar is equal to 100 cents and it has various denominations such as pennies (1 cent), nickel (5 cents), dime (10 cents), quarter (25 cents). Depending on your country and currency, you probably have various denominations as well.

Ether also has various denominations as shown on the right. The only two you should really remember are Ether and Wei. Wei is the lowest denomination and this is the denomination you use in your smart contracts.

What is an Ethereum address?

To login to any website like Facebook, you usually use an email/username and password. Your username is your identity in Facebook and you use your username/password to authenticate with Facebook.

In Ethereum blockchain, address is your identity. An Ethereum address looks like this: 001d3f1ef827552ae1114027bd3ecf1f086ba0f9. An address has a corresponding private key. You can think of private key as a password that only you know. You need this pair of address+private key to interact with the blockchain. Below are few key things to memorize without which it is hard to understand blockchain and Ethereum:

  1. Ethereum address is public and you can share it with anyone in the world.
  2. The private key should never ever be shared with anyone.
  3. The address+private key is not stored in any database. Only you are in control of them.

In the future sections, you will learn how to manage these addresses and ways to use them.

How is an Ethereum address generated?

Below is a quick explanation of how this address ex: 001d3f1ef827552ae1114027bd3ecf1f086ba0f9 is generated. This assumes that you know how public key cryptography works. You can learn more about it here: https://en.wikipedia.org/wiki/Public-key_cryptography.

Generate a private key

k(private key) = f8f8a2f43c8376ccb0871305060d7b27b0554d2cc72bccf41b2705608452f315

Derive the public key from the private key

K(public key) = 6e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b83b5c38e5e2b0c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0

Calculate the hash of the public key by using keccak256 algorithm

Keccak256(K) = 2a5bc342ed616b5ba5732269001d3f1ef827552ae1114027bd3ecf1f086ba0f9001d3f1ef827552ae1114027bd3ecf1f086ba0f9

Take the last 20 bytes of the generated hash to get the Ethereum address

0x001d3f1ef827552ae1114027bd3ecf1f086ba0f9

This my article explained hash functions well.

What is an Ethereum account?

The combination of Ethereum address and it’s private key is referred to as an account. Not only that, an account in Ethereum can hold balance (Ether) and can send transactions. Ethereum has 2 types of accounts.

1. Externally owned accounts (EOA): This is the account we mentioned above which is a combination of public address and private key. You can use these accounts to

  1. Send and receive Ether to/from another account
  2. Send transactions to smart contracts

2. Contract accounts: These accounts don’t have a corresponding private key. These accounts are generated when you deploy your contract to the blockchain. You will see them referred to as just contracts in lot of places (instead of contract accounts). Some key features of a contract (accounts) are

  1. They can send and receive ether just like EOA
  2. They have code associated with them unlike EOA
  3. Transactions have to be triggered by an EOA or another contract

It will get a lot more clearer in the future chapters when we create and use these accounts.

What is an Ethereum wallet?

Ethereum Wallets are software plugins/libraries that are used to store and manage your Ethereum accounts. Wallets allow you to manage multiple accounts, provides the functionality to sign transactions, track balances and so on. Wallets can be broadly classified into 2 types:

  1. Non-Deterministic Wallet: This is a type of wallet that uses a random private key and generates a public key from it. You can generate as many public/private key pairs but there is no relation between each pair of keys.
  2. Deterministic Wallet: In this wallet, keys are derived from a single starting point known as a seed. The seed allows a user to easily back up and restore a wallet without needing any other information and can in some cases allow the creation of public addresses without the knowledge of the private key. Seeds are typically serialized into human-readable words in a Mnemonic phrase. If you have used a plugin like metamask, it would have asked you to save the 12 word seed when you first installed it. The Metamask section in the next chapter has a video demo of the deterministic wallet.

Gas, Gas Price and Gas Limit

Gas

We know that you can deploy contracts on the Ethereum blockchain and execute transactions on it. However, there is a cost associated with each interaction. You have to pay Ether to the miners in the network to execute a transaction on the blockchain.

Who decides how much Ether to pay for a transaction? This is where it gets interesting and also where lot of developers new to Ethereum get confused. The answer is, the yellow paper has specification on how many units of work a transaction has. For example, if your transaction is to simply add two numbers, that is 3 units of work. If it is multiplication, that would be 5 units of work and so on. This unit of work is called gas.

Gas Price

We now know how much gas each transaction will need. But we still don’t know how much Ether we have to pay the miners. That is determined by Gas Price and the interesting thing is, you can set whatever gas price you want. For example, you can say “I am willing to pay 3 Gwei per gas” for my transaction. If your transaction takes 100000 gas and you set the gas price to 3Gwei, you end up paying 300000 Gwei for your transaction.

The higher the gas price you set, the sooner your transaction gets mined. This is because most of the miners sort the transactions in the descending order of gas price and pick the transactions with higher gas price to include in the block. If you set the price low, your transaction will eventually get included in the block but you might have to wait for a bit.

Gas Limit

It is very difficult to know exactly how much gas your transaction will take. As a developer, you don’t want to blindly execute a transaction and realize your transaction took hundreds of dollars worth of Ether. To avoid this situation, you can specify a gas limit which indicates the maximum amount of gas you are willing to buy to execute your transaction.

Don’t confuse this with Block gas limit. Block gas limit is the maximum cap applied to each block in Ethereum. Currently, a block can only include transactions whose total sum of gas is less than 8 million. The reason you have a limit for each block is to prevent someone from just writing an infinite loop in which case the transaction wouldn’t complete it’s execution resulting in block not getting mined.

Byte Code

Smart contract code is usually written in a high-level programming language such as Solidity. This code gets compiled to something called the EVM bytecode which gets deployed to the Ethereum blockchain. This is very similar to a programming language like Java where the code gets converted to JVM Byte code. The Ethereum runtime environment only understands and can execute the bytecode.

One of the benefits of this design is that it gives developers the option to use other programming languages to implement smart contracts. Currently, there are a handful of languages like Vyper (similar to Python) that compiles down to the EVM bytecode. If you are a language designer, you can just refer to the Ethereum yellow paper and implement your own programming language for Ethereum!

What is EVM?

The Ethereum Virtual Machine (EVM) is a simple but powerful, Turing complete 256bit Virtual Machine that allows anyone to execute arbitrary EVM Byte Code. The EVM is part of the Ethereum Protocol and plays a crucial role in the consensus engine of the Ethereum system. It allows anyone to execute arbitrary code in a trust-less environment in which the outcome of an execution can be guaranteed and is fully deterministic.

When you install and start the geth, parity or any other client, the EVM is started and it starts syncing, validating and executing transactions.

Geth/Parity Clients

So far we have only talked about the theoretical concepts of Ethereum. The best way to understand is by installing and using these various libraries/software. Let’s see what tools are available to build applications on the Ethereum blockchain.

Geth

Geth is the official client software provided by the Ethereum Foundation. It is written in the Go programming language. When you start geth, it connects to other Ethereum clients (also called nodes) in the network and downloads a copy of the blockchain. It will constantly communicate with other nodes to keep it’s copy of the blockchain up to date. It also has the ability to mine blocks and add transactions to the blockchain, validate the transactions in the block and also execute the transactions. It also acts as a server by exposing APIs you can interact with through RPC.

It also comes with a javascript client (geth console) that can be used to connect to the blockchain.

Parity

Parity is another good implementation of the Ethereum protocol and is written in the Rust programming language. It is maintained by a company called Parity Inc.

Anyone can implement the client software and join the Ethereum network. There are clients written in C++ and Python and other languages. You can follow the specs in the yellow paper to implement your own client if you like!

A simple illustration of the Ethereum network with various clients connected to one another.

Web3JS/Truffle

Web3JS

This is a very popular javascript library that is used to interact with the Ethereum blockchain. You can use this javascript library in any frontend framework to build a user-facing decentralized application.

Another library which is worth looking into for interacting with the blockchain is Ethers.js

Truffle

Just like you have frameworks for web application development such as Ruby on Rails, Python/Django etc, Truffle is one of the most popular frameworks used to develop dapps. They abstract away lot of the complexities of compiling and deploying your contract on the blockchain. It also has an in-built testing framework you can use to test your contracts. You can find more details about the truffle framework here: http://truffleframework.com/. All our courses are based on the Truffle framework.

There is another alternative to Truffle called Embark which is worth exploring.

Ganache

Using a client like geth or parity for development can slow you down because it takes 15 seconds to execute each transaction. In order to get around this issue, developers usually use an in-memory blockchain called ganache for development purposes. You can either install a command-line version called ganache-cli or the GUI version. Another nice thing about ganache is that you get 10 test accounts loaded with 100 Ether for your testing.

Metamask

If you remember, we discussed about Wallets in the previous chapter. Metamask is one of the popular wallets everyone in the Ethereum community use. You can install it as a chrome plugin from their website. We created a video showing how to install metamask and also some of the concepts of deterministic wallets and seed phrases we discussed in the previous chapter.

Etherscan

Etherscan is a handy frontend application offered as a free service by the company called Etherscan to explore the Ethereum blockchain. If you interact with the public Ethereum blockchain, all the addresses, transactions, block details etc can be viewed on that website. You can see on the right, the visualization of the Ethereum network and how Etherscan fits in the network.

You can find links to Etherscan frontend for various public networks below:

Mainnet: https://etherscan.io/

Ropsten: https://ropsten.etherscan.io/

Rinkeby: https://rinkeby.etherscan.io/

Kovan: https://kovan.etherscan.io/

Remix

Remix is a browser IDE you can use to code your smart contracts. Not only can you use it to as an editor but it can be used to compile and deploy your contracts to various networks and interact with them directly from the IDE. It has many features to select various compiler versions, debug your contracts and so on. You can access the IDE here: https://remix.ethereum.org.

In our opinion, remix is good to quickly test out small contracts but we recommend using an editor like Vim, Atom, Visual Studio or Sublime for development and use truffle framework for compiling and deploying your contracts.

Closing Thoughts

I hope this was a useful introduction to all the important concepts in Ethereum and hopefully you have a much better understanding of the Ethereum blockchain platform. If you discover any new tools you think we should add to the list, any concepts you think we should explain, have any feedback or just say hello by emailing us at info@blockchainnigeria.group :)

Next Step

To proceed beyond this point, these prerequisites have to apply to you;

You must have had some kind of knowledge of the workings and concepts of Blockchain & Distributed Ledger Technology (DLT)

You should be familiar with any object-oriented programming language (Python, C++, PHP, Java, Rugby, C#)

You should be familiar with web technologies (HTML, CSS and Javascript)

You are familiar with CLI or Command-line Interface

Your laptop should be developer ready, running MAC, Linux or Windows.

--

--

Chimezie Chuta
Chimezie Chuta

Written by Chimezie Chuta

IT Professional & Drupal Web Developer. Trainer @ Lagos DrupalVersity. Founder, Blockspace.Digital, Coordinator, Blockchain Nigeria User Group, 3D Printing guy!

Responses (1)