Smart contracts - what they are and how to create them | Concise Software

Smart contracts – what are they and how to create them?

Smart contracts are digital contracts functioning in the form of self-service applications. Sounds abstract? Therefore, we will try to familiarize ourselves with this subject and show how to create a smart contract on our own.

Smart contracts are nothing more than intelligent contracts, expressed in the form of applications. They have introduced to the general circulation thanks to Ethereum, but it is worth mentioning that Bitcoin already made it possible to carry out the first simple transactions, which allowed the transfer of data between the users of this cryptocurrency.

Bitcoin, due to its limitations, unfortunately, does not allow to create more advanced solutions of this type. It is here that the second most popular cryptocurrency enters with relief, offering much more than coins. Ethereum is an entire development platform, which structure gives the possibility to create decentralized, distributed applications, as well as much more advanced smart contracts than in the case of Bitcoin.

What is a smart contract and how does it work?

Smart contracts are a kind of applications, which, after meeting strict conditions, allow you to execute the code using EVM (Ethereum Virtual Machine) and return the expected results. To better illustrate this, let’s use a simple example:

Smart contracts are based on a simple principle: “if X happens, do Y”. Applying this principle to the situation we may face in everyday life, let’s assume:

You buy an apartment. Using a smart contract, when paying for an apartment, you automatically make an appropriate entry changing the ownership of the property in the “virtual land and mortgage register” (i.e. it is recorded in a specific transaction on the register stored in the blockchain), and at the same time you get a code of access to the apartment and without dealing with formalities in any offices, you can move in. Simple as that 😉

So, as you can see (in a considerable simplification), smart contracts are applications that can have the same power of action as traditional legal contracts. However, they have the huge advantage that they are verified much faster, thanks to which we save a lot of time, effort and formalities, and above all – we skip the whole process of bureaucracy.

Smart contracts work like ordinary contracts. The first step leading to their creation is to establish specific terms of such an agreement between the parties (“if X happens, perform Y”). Then the contract terms are written down – not in the traditional form, but in the blockchain application. The detailed method of contract execution will be described in the further part of this article.

Where can I use smart contracts?

The times when we will be able to take full advantage of smart contracts are certainly still to come. So far, they are entering our reality relatively slowly, mainly due to the need to adapt or modify many legal norms or systems. Moreover, smart contracts in the public consciousness are still more associated with the topic of “suspicious cryptocurrencies” than with the FinTech sector. Nevertheless, it is easy to find a lot of places where smart contracts will work perfectly.

Looking for Blockchain Development Services?

Find out how can we help you!

click the button below

The current giant of the Swiss banking sector UBS (together with Barclays Credit Suisse, HBC, SIX and Thomson Reuters) has leaned over one of the smart contracts applications. This is the Madrec (Massive Autonomous Distributed Reconciliation) platform, which (in a considerable simplification) aims to make it easier for banks to analyze the vast range of counterparty data and potential credit risk.

Smart contracts can of course also be used in many other areas, for example:

  • the fight against counterfeiting and trade in stolen digital goods (Alibaba)
  • tax purposes (VATcoin)
  • healthcare (Roche Group, MediLedger Project)
  • payments in e-commerce (maybe not necessarily in Bitcoins… ;))
  • automation of orders (if customer X chooses car Y and pays Z, then change the owner of car Y to X, save in BC and send info to the Communication Office(?))
  • real estate sector (example of real estate purchase given above)

How to create a smart contract?

So far, our guest articles on Antyweb have been created for people who are not developers and are looking for simple explanations of popular topics related to cryptocurrencies and blockchain. We tried to dress the discussed topics in non-technical language and easily assimilable for people who do not work with code on a daily basis. However, this article is slightly different from our assumptions, because using the opportunity to develop the topic of smart contracts, we also want to show you how to create such a smart contract in a simple way. So, don’t be surprised by the fragment of code that you will find below 🙂 Let’s get to work!

In this part of the text you will find out:

  • How does a smart contract look like?
  • How to upload smart contract to the network
  • How to execute the uploaded application code in order to get the expected result

Smart Contracts – what knowledge will you need at the beginning?

Before we start creating smart contracts, let’s define what knowledge is required for this type of fun.

If you want to go deeper into the world of cryptocurrencies and smart contracts it would be good for you to know:

  • Javascript
  • Linux i Command Line Interface
  • Programming language: Solidity
  • The way the Ethereum network works

Tools that you will need:

  • Solidity compiler (solcjs)
  • Client Ethereum: geth
  • Text editor

Remix

As for the editor, the best option will be to use a web-based IDE called Remix. Remix is a tool that allows you to write smart contracts using a browser. It also allows you to compile them and upload them to the network. Remix also supports the JavaScript virtual machine, which allows you to simulate running and uploading a contract to the network. Then you can test all functions.

This is a good solution for people who want to test smart contracts and learn how to create them, but remember about one thing! Remix does not reflect the full network (or the time it takes to dig a block).

Ethereum client

Ethereum has several clients, including Geth. The main alternatives to Geth are Parity, and the client Cpp Ethereum. For end users Parity may turn out to be better (and above all more convenient to use), mainly because it provides a web interface that we can easily run in our browser.

Private network

In order to run our first private network, we need one or more clients who will confirm our blocks. The Ethereum network is a Peer to Peer (P2P) network, so it is good to start with a minimum of 3 instances (or more).

Configuration of the private network

Blockchain network in our case has a definition of rules for the start block – also called the genesis block. For the Ethereum network such a definition is stored in the genesis.json file (in the case of the geth client). This file looks more or less like the one shown below:

definition stored in the genesis.json file

This file defines mainly the network number. The main net has number 1. Then the block contains information about changes in the protocol and the number of the block in which the change takes place. Additionally, it contains the initial difficulty of the network* and the allocation of startup balances for the given addresses.

What is network difficulty? Let’s recall that digging a block in the Ethereum network takes on average 15 seconds. The difficulty of the network ensures that this average time is maintained. Initially, we can set a low value, because, during the operation and activity of the “miners”, the difficulty will be adjusted to the speed at which the device will be activated – so that the block is excavated on average every 15 seconds set.

How to start a private network?

As mentioned above, you will need a client to run the network. In our case, it will be geth. This client can be downloaded directly from the website: Geth & Tools

To start our network, we need to generate a client ID in the P2P network. For this purpose we use the bootnode tool:

./bootnode –genkey node.key

We then generate our wallet, which we will use to carry out the transaction.

./geth –datadir=./ethereum account new

Of course, we can first generate an account and assign the appropriate amount of money allocated to the address at the start. If we don’t do this, we will have to start the digging process in order to get the money (coming from the prize for finding a block).

The next step will be the initialization of the network

./geth –datadir=./ethereum –networkid 2456 –nodekey node.key init genesis.json

and its activation (without the init genesis.json ending)

./geth –datadir=./ethereum –networkid 2456 –nodekey node.key –mine –minerthreads 1

Smart Contracts – code

When our private network is ready, the next step will be to create a Smart Contract. The simplest example is the contract that will return “Hello World” to us. To perform this operation, we have to write a contract and compile it.

After compilation, we upload it to the network. It creates a binary code and application description (ABI – Application Binary Interface). ABI is required to perform operations on our Smart Contract. The sample contract code will be like this one:

sample contract code

Compilation of the code – how to do it?

To compile, we need to install the compiler:

npm install -g solc

Then generate binary code and binary interface (ABI):

solcjs Hello.sol –bin
solcjs Hello.sol –abi

Uploading smart contracts to the network

The next step is to upload our compiled contract to the network. To do this we need a small JavaScript code, in which we define our binary interface and binary code (in place with 0x prefix).

Deploy.js:

In the beginning, we define our text, which will be immediately saved in the network when sending the contract to the network. The showText() function should return the above-defined text Hello World.

The above script must be executed with the help of the geth console. To do this, we need to connect to the client by executing:

./geth attach –datadir=./ethereum.

Then run the following command in the customer console:

loadScript(“deploy.js”)

Looking for Blockchain Development Services?

Find out how can we help you!

click the button below

Attention: If you have created an account with a password, you have to unlock it earlier – using the command: personal.unlockAccount(eth.coinbase, ‘12345678’, 0) – where 12345678 is an example of a password. The last parameter means that we want to unblock our account for an indefinite period of time. Of course, you can enter a different value there 🙂

After a while, the client should display a message that our contract has been dug up and give us its address:

Contract mined! address: 0x1fbc3e8b42f9b9ea7ca3df8057f47c2d58226e00

and the number of the transaction in which the contract was dug out:

transactionHash:
0x9726dc4ffece040e1e9eeeea77b43cc419139c722ea7ebabf4e991b817549384

Execution of the code

After successfully sending the code to the network, we will be able to execute it. To do this, we need to specify the address in the client console and provide the ABI of our contract.

var hello = eth.contract([{“constant”:true,”inputs”:[],”name”:”showText”,”outputs”:[{“name”:””,”type”:”string”}],”payable”:false,”stateMutability”:”view”,”type”:”function”},{“inputs”:[{“name”:”text”,”type”:”string”}],”payable”:false,”stateMutability”:”nonpayable”,”type”:
“constructor”}]).at(“0x1fbc3e8b42f9b9ea7ca3df8057f47c2d58226e00”)

In the next step, we make a reference to our function, which returns to us our expected result.

hello.showText()
“Hello World”

The code that we used in today’s article can also be found on our GitHub.

Smart Contracts at the very beginning of an adventure can seem complicated. A programmer who wants to start programming contracts will need to understand a different approach – an approach in which each operation (in this case a transaction) is not performed immediately. Reading data from the contract is quick, but modifying the network requires waiting for the block to be executed and receiving several confirmations. In addition, it is necessary to understand to a greater or lesser degree the way the Ethereum network itself operates. Having this knowledge, the rest becomes much simpler.

You may like to read:

Concise Software among the best blockchain developers in Poland!
Blockchain in healthcare – here are 5 essential use cases
10 Use Cases of Blockchain in Banking
How are we all going to use blockchains in the near future?
Blockchain – a starter guide to the world of technology
Introduction to a smart contract on blockchain
Cryptocurrencies and “mining” – how does the digging process work?
Everything you need to know about Ethereum
Open banking – a new dimension of financial services
P2P payments – what are peer-to-peer payment apps used for?
What is an Initial Exchange Offering (IEO) and how to participate in it?
IEO vs STO – key differences you should know
What is blockchain development? A complete guide!

Contact Us