The ultimate guide to EOS Smart Contracts — Zero to Hero EOS tutorial

web3abhi
CryptoDigest
Published in
4 min readMar 13, 2019

This tutorial will guide you through the basic fundamental concepts and essentials of EOSIO blockchain based smart contracts.

EOSIO — A NEW DAWN

After finishing this tutorial, you will —

  • Understand the key concepts of EOSIO contracts clearly
  • Be able to develop and test smart contracts.

This tutorial assumes you have basic knowledge of smart contracts and some experience with solidity development and a good understanding of C++.

This tutorial takes a more practical implementation based approach than theory-based, hence it will be good if you implement the concepts alongside the tutorial to get throughout knowledge.

Fundamental Concepts —

The smart contract basically consists of actions and type definitions. Actions are functions and behaviors and type definitions consist of data and structures.

Transactions —

Contracts and accounts communicate in the form of actions. These actions can be sent individually, or in combined form, to be executed as a whole.

A transaction is basically a collection of one or more actions.

A transaction has the following properties —

The transaction’s actions is an array which contains multiple actions and their properties.

Communication model —

Smart contracts communicate with each other through actions and data sharing. There are 2 types of communications that happen with EOS transactions, Inline and Deferred.

Inline communication executes within the same transaction as it is invoked, or it gets unwind. Inline actions operate with the same scopes and authorities of the original transaction and give no notification.

Deferred communication, on the other hand, gets scheduled to run, at best, at a later time, at the producer’s discretion and act as action notification.

Starting with the “Hello world” contracts —

Let's start with a basic hello world smart contract —

As in C++, we will first include essential libraries from eosiolib, namely eosio and print in this case. Check EOSIOLib GitHub repo for learning more about the library.

After declaring eosio namespace, we will start with inheriting our contract class from eosio:: contract.

Ideally, it should be public to make it universally accessible, as we can set permissions on actions level.

Now we will define an action and overwrite it from the base class and define the logic of that action. In this case, we will define the action “hi” and it's logic as “print(“Hello World”)”.

Token Smart contract —

To build an ERC20 equivalent token on EOS, first, we will create the header file eosio_token.hpp and will define the header logic there.

This file is basically the interface of all essential functions we need to implement in our smart contracts.

Here, after importing all the essential header files and defining namespaces, we will create a class token, inherited from the contract class.

Now let’s start with defining the private data used in the contract-

Here we are defining 2 structs eosio tables, one for keeping track of account balances, and another one for the currency stats with fields supply, max supply, and the name of the issuer.

We will use Multi index table structure to store the data for those 2 tables. Multi-Index Tables are a way to cache state and/or data in RAM for fast access. Multi-index tables support to create, read, update and delete (CRUD) operation.

Now we will define 2 private functions to add and subtract balance from EOS accounts of users.

Now let's jump to the definition of public functions the token contract will have —

Here we will define the 2 types of commonly used functions — getters and setters. In EOS’s case, the setter functions are essentially the actions and hence flagged as such by [[eosio::action]].

The functions are defined as such —

  1. create — it creates a token with the given symbol name and max supply. The issuer will be the one with the authority to call issue and or perform other actions such as freezing, recalling, and whitelisting of owners.
  2. issue — this functions issues tokens to an account along with an attached memo.
  3. transfer — this functions subtract the balance of sender account and add it to receiver account.
  4. get_supply — this getter function will return the total supply of the token.
  5. get_balance — this public function will return the balance of any given account.

Also, we will implement the get_supply and get_balance functions in this file itself as described above.

  1. Get_supply

2. Get_balance —

Now that we have defined our interface, let’s start with developing out token cpp file.

Let’ s implement the functions one by one in eosio.token.cpp file

  1. Create —

2. Issue —

3. Transfer —

Now finally dispatch all these actions by using EOSIO_DISPATCH function.

The final eosio.token.cpp file will be —

This is the basic implementation of the token contract and it can be further enhanced by adding functions like an open and close contract etc.

Now that you have a thorough understanding of smart contracts on EOS, go ahead and head to official docs and start building your next big dapp.

You can check out the whole code of this article here.

Explore more —

You can head over to

  1. EOS smart contract docs and tutorial
  2. The code repo of EOS example contracts
  3. EOS for Beginners: A Guide to Getting Started

and build the next big dapp out there with EOS.

Also, follow me here for more interesting blockchain know-hows.

Get in Touch —

I can be personally reached directly here or twitter and would love to work with you on something interesting. 😀😀

Give some 👏 if you find this useful. Bye 🖐

--

--