Smart contract on Ethereum Blockchain

Antoine Boxho
Takeo Engineering
Published in
7 min readDec 11, 2017
Smart contract on the blockchain

Bitcoin, Blockchain, Smart Contracts, DLT… Lately, we have been overwhelmed with these buzzwords. Do these announce the emergence of Web 3.0? I don’t know for sure, but it might be the case.

What is Web 3.0? You can think of it as the decentralization of the Web we currently know where most services are centralized by central entities like Amazon, Google, PayPal…

Comparison between the Web 2.0 stack and the Web 3.0 stack

Thanks to technologies like blockchain and peer-to-peer communication protocol, we are now capable of building these things called “decentralized applications” or Dapps.

We call it decentralized because all services like scalable computation, file storage, monetization, and payments are done in a decentralized fashion. I am not going into details about the whole Web 3.0 stack because this article focuses on smart contracts that you embed inside your Dapp.

Part 1

What are Smart Contracts?

Smart contracts consist of computer programs, to which certain conditions have been attached and can be used by everyone on the blockchain. They can self-execute and self-enforce if the conditions are met. Applications are limitless in terms of smart contracts.

In the Web 2.0 environment, you would deploy such code on a central service like Heroku for compiling the code. Here it’s different. You push the smart contract on a blockchain network. Once the contract is deployed, every miner has this piece of code and this code is immutable.

In this article, I show you how to deploy your smart contract on the Ethereum blockchain.

What is Ethereum?

It is a platform to easily build decentralized applications using blockchain technology. We say of Ethereum that it is a Turing-complete blockchain because you have access to fundamental building blocks like loops and all sorts of application-level constructs to develop an application on top of the Ethereum blockchain.

Ethereum blockchain

Since these scripts need to be executed on every node present in the network, there must be a mechanism to limit the computation used by each contract. Every operation that is executed on the EVM can sometimes be really expensive in terms of computation. The introduction of a payment scheme allows for a limitation of computation usage by smart contracts.

To deploy such a smart contract on the Ethereum Virtual Machine, it requires this thing called “gas”, which is internal to Ethereum and is used to dynamically compute the price of a transaction.

Part 2

Setup of Basic Blockchain Environment

For the sake of simplicity, we are going to deploy the contract locally on our device. So, the first thing you will do is downloading Node.js because we are going to work with libraries that are compatible with Node.

Go to this website to download Node: https://nodejs.org/en/download/

Once Node is installed, you should have npm (node package manager) installed on your computer, which is a package manager for JavaScript packages.

Then, with the npm tool, inside your terminal, you will install the package called ethereumjs-testrpc, which is an in-memory blockchain that we are going to use for testing out the smart contracts without having to download the actual blockchain.

npm install -g ethereumjs-testrpc

We can use testrpc to deploy our smart contract on a simulated blockchain network. Then with a simple user interface, anyone can interact with this contract locally.

We can run testrpc in a new terminal and leave it running while we develop. Each time we run testrpc, it will generate 10 new addresses with simulated test funds for us to use.

testrpc

The following image should appear on your screen. You should see a bunch of available wallets you can use to simulate a blockchain locally on your device.

Screenshot of testrpc

Notice that all the accounts are created randomly every time a new instance is launched, so it is not necessary to store these addresses somewhere on your device because they won’t be the same when starting a new Testrpc session.

Setup of Truffle Environment

The next step is to install an environment that will facilitate the deployment of the contract on either the simulated or real blockchain network.

We are using the truffle, which is a development framework that facilitates the creation of smart contracts. It provides us with an environment for compiling, deploying and testing the smart contract.

Truffle is also a javascript package, so you can simply install it globally like that:

npm install -g truffle

Let’s now create the actual project.

mkdir transactions-dapp
cd transactions-dapp
truffle init

I won’t go into details on what each directory is doing. For more information about the Truffle framework, visit the following link: http://truffleframework.com/docs/

You should have 3 directories (contract, migrations, and test) and 1 file (truffle.js) in your project. The one we are interested in for now is the truffle.js file, which is the configuration file of your project. Inside this file you can copy-paste the following chunk of code:

Config file of truffle project

It instructs to the compiler all the configuration it should apply to the project.

At this stage, you should have a blank truffle project up and running. To test everything is working as intended, we should first compile the project and then deploy it to the local instance of the Ethereum blockchain that is running in the background.

truffle compile
truffle migrate

If no errors occur, then we are good to go.

Creating a Simple Coin Transfer Contract

Now that the environment is set up, we can begin with the implementation of the smart contract that we will then deploy on our local blockchain.

To get started with the creation of a contract, we simply run truffle create contract YourContract that will create a solidity file.

The smart contract looks like this:

Smart contract taking care of transactions

This contract here is written in Solidity language, the official language used by Ethereum developers to implement smart contracts. The Solidity language is a contract-programming language.

For more information about this language, visit the following URL: https://solidity.readthedocs.io/en/develop/

You have two structs, one for the clients that get added to the contract state and one for the transactions taking place between the clients.

In a contract you have 2 kinds of functions:

  • Read-only functions that don’t perform any state changes, they only read state, perform computation and return values (getBalance, getClients, getTransactions and clientExists)
  • Then you have transactional functions, these functions perform a state change in the contract or move funds (addClientToBank and transfer)

Once the contract has been added to the project, inside the migrations directory, we have to add a new file that you can name whatever you want, but it has to be prefixed with an increment of the highest prefix in the directory.

In other words, if the only file is 1_initial_migration.js, the file you should create should begin with 2_some_file_name.js:

cd /migrations
touch 2_transactions_migration.js

Then, inside this newly created file you can copy the following snippet of code:

It tells truffle to deploy the smart contract you want to deploy on the blockchain. Now we are ready to deploy the whole project and interact with the deployed contract.

We need to compile and migrate the whole project once again:

truffle compile
truffle migrate --reset

This will create “artifacts”, but these are JSON files. You can find these files in the “build” directory and there you will find all your artifacts files.

Interacting with the Contract through Nodejs

Once the contract is deployed, you can interact with the contract you deployed through the truffle console. You can send messages to the contract via function calls and read the state of it that is public.

truffle console

First, you need to access the contract that was deployed:

var contract = Transactions.deployed()

Then, you can create a variable that contains all the ten available accounts that are provided by the local blockchain:

var accounts = web3.eth.accounts

Web3 is a thin client that we will use to access the contract from the local blockchain. This library provides us with the eth object that is specifically used for Ethereum blockchain interactions.

Now, you can interact with the functions that are present inside the contract:

contract.then(function(instance) {return instance.getClients()})
contract.then(function(instance) {return instance.getBalance.call(accounts[0])}).then(function(balance) {return balance.toNumber()})

But, this is not the best way to interact with it. Let’s build some basic UI

Creating the UI

I won’t go over all the user interface I have to build to create interaction with the contract since it is not the point of this article. I’ve built a simple UI using the ReactJS framework from Facebook.

You can download the whole project on my GitHub account: https://github.com/C00kieMonsta/Transactions-dapp

Drop me a message if something is not clear!

--

--

Antoine Boxho
Takeo Engineering

Engineer | Computer science 🖥 | Blockchain & other stuff…