• Technology
  • by Piotr Pawlak
  • 13/12/2017

Blockchain – Introduction to Smart Contracts

A smart contract is a computer protocol intended to facilitate, verify, or enforce the negotiation or performance of a contract. Let’s see how it works!

blockcian smart contracts

Smart Contracts – needed knowledge

Before we start creating example of Smart Contract I would like to introduction some terminology that you should known. What technologies and tools we need to create and deploy a Smart Contract to the Ethereum network. In this article I will show you how source code looks like, how to compile with Solidity compiler. Also how to create private network for testing.

You should know basic knowledge about:

  • Javascript
  • Linux and CLI
  • Solidity
  • Ethereum network

If you don’t known anything about Blockchain or Ethereum, to help you start you can start reading about them on our other articles:

The tools what we need

  • NodeJS
  • Solidity compiler (solcjs)
  • Ethereum client: geth

To write a Smart Contract we need editor. I prefer Web IDE called Remix. Remix gives us possibility to write, compile and deploy our Contract. It also support JavaScript Virtual Machine – so we don’t really need deploy our code to the network. It is good for testing, but does not reflect a full network (e.g. with time of block mining). In this article we will use private network. Remix have option to set Web3 Provider, so then we can use our network.

Ethereum have multiple clients. Mentioned above geth but also Parity or cpp_ethereum. Parity is good for general group of users because it have Web UI, where we can find your wallet, transactions list, transactions queues even console.

Private Network

To create private Ethereum network and blockchain we need Ethereum Client (called nodes). Ethereum network is Peer to Peer network. In minimal scenario we should have only one node. It can be installed on our private computer. When we have multiple developers in a project, every developer should install the client. Then they connect to one P2P network. In this scenario it would be good to have master client, for example installed on server. Also it would be good to have one global miner, who will be confirm (mining) transactions.

Configuration of private network

Every network have configuration of genesis block. Genesis block is the first block in blockchain, and it does not have hash of previous block. For Ethereum network there must exists genesis.json file.

In this file there is an option to define our networkId (chainId), information about some improvement (eips) with block number when change will be online on the network. It is mainly for main network. What else? Definition of starting difficulty and gas limit. You can also predefine addresses with starting balances.

Starting the network

As mentioned above we need some client. We will use geth, because we do not need the UI. Only CLI. Geth can be downloaded from official website: Geth & Tools.

To start network we should generate some node.key. It will be used to generate enode url. That url is used to connection P2P between nodes.

Next step is to generate some new Ethereum account in our client.

Of course our new generated account we can define in genesis.json.

Starting the client without initialize genesis will initialize main-net genesis block. To do define custom genesis block we should run:

As you can see, we using the same chainId number in networkid parameter, node.key file and genesis.json. After that we can start our client without init genesis.json fragment but we need to enable mine option: –mine –minerthreads 1 –etherbase <yours ethereum address>. And client will have P2P address based on our node.key. It will be looks like that:

Smart Contracts

Now, when we have our client up and running we can try create Hello world contract. To do such thing we need to have source code of contract and as mentioned above some compiler. Compiler will compile code to the byte-code and also it will generate Application Binary Interface (ABI). ABI will be needed to do any operation on the deployed Smart Contract. Example source code looks like this:

Compile – How-to

To compile the source code we need install compiler.

And generate binary code and ABI code.

Deployment script

Next step in deployment is to create javascript file with deployment script. In this script we must set ABI and binary data ( place).

We define text to Hello World. This variable will be executed as parameter of constructor. And showText() function should return this text.

Then this script will be directly loaded to geth console. When geth is running we can attach with command: geth attach. Then we just execute:

After couple seconds we will see information about submitted contract creation and if contract will be mined then some Ethereum address will be generated.

After deployment

When we successfully deployed our first contract, we can use ABI to load contract and execute some action. To do this we need to again attach to the client console and just run:

After this we can execute our showText() function.

Piotr Pawlak

Blockchain / DApps Developer

Games and code geek. He deals with the programming of Internet applications - both frontend and backend. Open to new technologies - including blockchains. For several months dealing with this technology professionally.

Subscribe to our weekly news feed

Get the latest info about Concise Software

Let's work together.

Are you ready to build the future together?