EOS Smart Contracts Guide: Getting Started

Learn something more about Blockchain technology. Get started with EOS Smart Contracts including a practical case.

by Ernesto Messina, Software Engineer

EOS.IO is the first-to-market, enterprise-ready, third generation Blockchain. It aims to become a decentralized operating system supporting industrial-scale applications, eliminating transaction fees and being able to conduct millions of transactions per second.

EOS.IO takes smart contracts to the next level, operating as a smart contract platform. In this article we will learn its basics.

Running our Private Node

In order to deploy our first “Hello world!” smart contract we are going to need at least one EOS node running. We will communicate with this node via CLI and provide the smart contract to be deployed. Please note that this requires to have EOS installed. You can see how to do that here.

Start Your Node

You can start your node using this single command:

This will show an output as the following:

This is a notifying message, each half a second duration, showing that a new block has been produced and confirming that the node is up and running.

Cleos and Wallet Creation

Cleos command is our friend. This CLI application allows us to communicate with our node, create wallets, ask for balance, transfer money, and so much more.

In order set a contract to an account, we need to have the required permissions. Since in this example we are going to be using the eosio account (the EOS’s root account), we already have these permissions. But we do need to sign the transaction, and to do this, we need eosio’s private key.

To use this private key we need to ensure we have it in our wallet, and for this, my friends… we need a wallet.

The above command creates our default wallet, the standard output then showing its password. Save it! You will need this password in order to unlock the wallet.

This command creates our default wallet, showing the password via standard output.

Importing Keys

You can think of a wallet as a keyring, it keeps all of our keys organized in one place. So, this is why we create it, to keep our eosio’s private key to sign our transactions validating that we have the required permissions. Now, we import the eosio’s private key.

The one above is the public domain key. If you have changed the eosio’s public key, replace it with the corresponding private key.

Hello World!

Having the required key in our wallet, we can now focus on our smart contract code. For the sake of simplicity, we will write a simple Hello World smart contract.

Let’s create a hello.cppfile with the following content:

Code analysis

Is the header for EOS’s smart contracts.

Declares hello class as a eosiio::contract derived.

Declares hi() function as part of the contract’s ABI.

Prints Hello World! on calling hi.

It is the ABI declaration of our smart contract, so it must include all the actions (class methods) that we want to potentially get called on it. It is the json to binary (and reverse) bridge. This macro also calls apply() on the contract.

Compiling

The next step is to compile the contract. For this we will use the EOS’s eosiocpp script with the following parameters:

This will compile our C++ code into WebAssembly, resulting on a hello.wast file.

And then:

Which will create a hello.abi file with the ABI declaration of our contract.

Both files are required to deploy the Hello Word! smart contract.

Deploying The Smart Contract

Finally, the last step is to deploy our smart contract to the blockchain. The command to do this has the following format:

So we have:

Verifying

We can verify that our smart contract is responding to us by calling on this:

Here we send an action to eosio, the user we have previously deployed the contract with, calling the action hi without parameters. Then we sign on with our private key (-p eosio), used earlier in one of our previous steps.

Summary

Creating a smart contract for EOS is easy. You must have a wallet and have imported the user-key signing the transaction. Then just write the smart contract, compile and deploy it to the blockchain.

Also, if you are looking for a partner that can start developing your any kind of software project Devlane is available for you. We specialize in many categories & technologies and blockchain is one of these. We have special knowledge in Ethereum & EOS.

You can have all your development made from scratch or we can lend you some IT resources to augment your in-house team.

If you wish to know more about us, visit our full webpage here or you can contact us directly here.