Beginner’s Guide to EOS and Basic Smart Contracts

Abhishek Sharma
QuillHash
Published in
7 min readNov 13, 2018

--

ref —https://tinyurl.com/yby5wap7

1. What is Eosio ?

Eosio is a software that introduces a blockchain architecture and claims to be a decentralised operating system which supports Dapps, it has developed under an open-source MIT software license. Block.one has build the eosio software, it is a leading blockchain solution company that provide high performance blockchain solution.

The software provides accounts, authentication, databases, asynchronous communication and the scheduling of applications across multiple CPU cores and/or clusters.

2. How Eosio is different from other Blockchain (mainly Ethereum) ?

  1. Eosio is more scalable as it claims to manage millions of transaction per second, they are claiming this because they uses DPOS(delegated proof of stake) consensus mechanism.
  2. EOS provides parallel processing of smart contracts through horizontal scalability, they achieve horizontal scalability by adding more system to resource pool.
  3. They are planning to completely remove transaction fees, Eos works on an ownership model whereby users own and are entitled to use resources proportional to their stake, rather than having to pay for every transaction.

prior knowledge required:

C/C++ intermediate experience

Blockchain Conceptual knowledge

Linux or command line tool

Architecture of EOSIO

ref — https://developers.eos.io/eosio-home/docs/how-it-all-fits-together

All the terms in architecture diagram will be explained later on when it is required don’t get confused with the diagram.

Firstly you need to setup environment and have to install all the dependencies and contract development toolkit to create wallet and accounts on EOS blockchain.

3. Setup Environment

3.1) Download dependencies:

Docker

3.2) Setup a development directory

Command :

Mkdir EOStestCd EOStest

3.3) Get the Dockers image

The below statement will download an Ubuntu image which contains the compiled software.

docker pull eosio/eos:v1.4.2

3.4) Boot node and Wallet

Command :

docker run — name eosio \ — publish 8888:8888 \ — publish 127.0.0.1:5555:5555 \ — volume /home/abhi_21094/Desktop/EOStest:/home/abhi_21094/Desktop/EOStest \ — detach \eosio/eos:v1.4.2 \/bin/bash -c \“keosd — http-server-address=0.0.0.0:5555 & exec nodeos -e -p eosio — plugin eosio::producer_plugin — plugin eosio::chain_api_plugin — plugin eosio::history_plugin — plugin eosio::history_api_plugin — plugin eosio::http_plugin -d /mnt/dev/data — config-dir /mnt/dev/config — http-server-address=0.0.0.0:8888 — access-control-allow-origin=* — contracts-console — http-validate-host=false — filter-on=’*’”

These settings accomplish the following:

  1. Forward port 8888 and 5555 to host machine
  2. Alias a work volume on your local drive to the docker container.
  3. Run the Nodeos startup in bash. This command loads all the basic plugins, set the server address, enable CORS and add some contract debugging.
  4. Enable CORS with no restrictions (*)

3.5) Check the Installation

Command :

docker logs — tail 10 eosio

3.6) Check the Wallet

Open new tab in the terminal

Command :

docker exec -it eosio bashcleos — wallet-url http://127.0.0.1:5555 wallet list

As now keosd is running successfully, now you should know the following terms that we will use during installation and also while developing.

What is Cleos ?

(cli + eos = cleos) — command line interface to interact with the blockchain and to manage wallets.

What is Nodeos ?

nodeos (node + eos = nodeos) — the core EOSIO node daemon that can be configured with plugins to run a node.

What is Keosd ?

(key + eos = keosd) — component that securely stores EOSIO keys in wallets.

3.7) Check Nodeos endpoints

This will check that RPC API working correctly

curl http://localhost:7777/v1/chain/get_info

You can also check this in your Browser provide by chain_api_plugin

http://localhost:7777/v1/chain/get_info

3.8) Aliasing Cleos

We don’t need to enter in dockers container bash everytime we interact with keosd so solution for this we can alias it

alias cleos=’docker exec -it eosio /opt/eosio/bin/cleos — url http://127.0.0.1:7777 — wallet-url http://127.0.0.1:5555'

Execute this command in your terminal or add this to your .bashrc file , you can find .bashrc file at home it may be a hidden file , use shortcut cntrl + h to show hidden files.

4. Contract Development Toolkit(Install CDT)

CDT for short, is a collection of tools related to contract compilation, use CDT primarily for compiling contracts and generating ABIs.

Install Command :

Wget https://github.com/eosio/eosio.cdt/releases/download/v1.3.2/eosio.cdt-1.3.2.x86_64-0.x86_64 .rpmsudo yum install ./eosio.cdt-1.3.2.x86_64–0.x86_64.rpm

Copy this command and execute in your terminal you will get the following result

Till now you have successfully setup the environment and installed Contract development toolkit. Now you will going setup wallet and account that really very interesting part of EOS

blockchain .

5. Create Development Wallet

5.1) create a wallet

To create a wallet you need to enter in a docker container bash by using following command

Command :

sudo docker exec -it eosio bash

After that enter wallet creation command

cleos wallet create — to-console

— to — console is used because we are using only for development purpose not production for production use — to — file as it will return your wallet private.

Wallets are closed by default

Command :

Cleos wallet openCleos wallet list

If you will get the following result then your wallet is created and opened successfully.

By default your cleos wallet is locked you need to unlock it by using command : cleos wallet unlock , you can see the difference between locked and unlocked wallet when you execute a command cleos wallet list it will return default for now without asterisk when it is locked, after

executing unlock command you can see the asterisk sign which show now wallet is unlocked.

Now we have to import key in wallet

cleos wallet create_key

Result : Created new private key with a public key of: “EOS8PEJ…GDJQY5Y”

By executing this command you will get the public key as shown below copy that key.

5.2) Import Development Key

cleos wallet import

Now enter eosio development key provided below:

5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

The default wallet is now unlocked and a key has been loaded. You are ready to proceed.

Use case : Deploy , Issue and transfer Tokens

We are implementing token contract to issue token and transfer to any account/user.

Source Code is available at EOSIO’S github:

Command :

git clone https://github.com/EOSIO/eosio.contracts — branch v1.4.0 — single-branch

Make sure you are in the eosio contract directory:

cd eosio.contracts/eosio.token

6) Create Account

An account is a collection of authorizations, stored on the blockchain, and used to identify a sender/recipient.

cleos create account eosio eosio.token EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV

6.1) Compile Contract

eosio-cpp -I include -o eosio.token.wasm src/eosio.token.cpp — abigen

6.2) Deploy Contract

cleos set contract eosio.token /home/abhi_21094/Desktop/EOStest/eosio.contracts/eosio.token — abi eosio.token.abi -p eosio.token@active

6.3) Create Token

cleos push action eosio.token create ‘[ “eosio”, “1000000000.0000 SYS”]’ -p eosio.token@active

6.4) Issue Token

cleos push action eosio.token create ‘{“issuer”:”eosio”, “maximum_supply”:”1000000000.0000 SYS”}’ -p eosio.token@active

6.5) Transfer Tokens from one account to another account

cleos push action eosio.token transfer ‘[ “alice”, “bob”, “25.0000 SYS”, “m” ]’ -p alice@active

6.6) Return a Transaction in JSON

cleos push action eosio.token issue ‘[“alice”, “100.0000 SYS”, “memo”]’ -p eosio@active -d -j

6.6) To check balance of any account using account name

cleos get currency balance eosio.token bob SYS25.00 SYScleos get currency balance eosio.token alice SYS75.00 SYS

Well Done You have successfully Set Up Environment, Created Wallet , Accounts, compiled and Deployed Smart Contract of Token on EOSIO Blockchain.

We’re available for EOS smart contract development and security auditing work.You can fill this form https://quillhash.typeform.com/to/KQ5Hhm to get in touch.

To be up to date with our work, Join Our Community :-

Website | Medium| Telegram | Twitter | Facebook

--

--

Abhishek Sharma
QuillHash

Smart Contract Developer and Auditor, DeFi, DEX