EOS Smart Contracts

Attic Lab
Attic Lab
Published in
3 min readAug 28, 2018

For versions up to 1.3.0!

Smart contracts for EOSIO are written in C++ programming language. For experiments with smart contracts, we need an installed EOSIO software that comes with standard C ++ libraries (located in ../eos/contracts/libc++ directory).

It is recommended that you test and debug your smart contract in Local Single-Node or Multi-Node Testnet before implementing it.

How to launch Local Multi-Node Testnet

After installing EOSIO, go to ../../eos/contracts directory, where you can find examples of various smart contracts. In this directory, we will create a new directory for the new smart contracts.

By using eosiocpp -n new_contract (new_contract the name of the created folder) command, you can generate templates for the header file new_contract.hpp, which should contain variables, constants, declarations of the functions of your smart contract, and a template for the source file new_contract.cpp, where you can find function’s implementation or definition.

Note: If the eosiocpp command is not available (eosiocpp: command not found), make sure that the following actions are performed

cd ../../eos/build

sudo make install

Subsequently, the script should display where the binary files were copied (by default it is /usr/local/eosio/bin) and if this path is not already in the $ PATH variable, you should add it:

export PATH=/usr/local/eosio/bin:$PATH

Compile

We recommend eosiocpp tool to compile smart contracts. This tool is given to you after the EOSIO installation.

A smart contract must be compiled into the WebAssembly (WASM) format, which allows you to run the high-performance code. WASM is the only format that EOSIO blockchain accepts. After you have a .cpp file ready, we recommend that you compile it into the text version of WASM (.wast) using the command:

eosiocpp -o new_contract/new_contract.wast new_contract/new_contract.cpp

An .abi file (Application Binary Interface) is also created, which is required by external systems to perform smart contract operations.

Deploy

To check/test a smart contract, you will need to start your EOS Local Single-Node or Multi-Node Testnet with the — contracts-console option or indicate the contracts-console = true parameter in the config.ini file.

Create an account for a smart contract with the following command

cleos system newaccount — stake-net “0.1 EOS” — stake-cpu “0.1 EOS” — buy-ram-kbytes 8 existing_account contract_account owner_public_key active_public_key

In Testnet, we can create an account using the eosio account, instead of the existing_account. Default eosio account keys are:

EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV

5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

Beforehand, you should import them into the wallet

cleos wallet create -n wallet_name

cleos wallet unlock -n wallet_name — password your_wallet_password

cleos wallet import -n wallet_name — private-key your_private_key

Deploy the smart contract on an account

cleos set contract contract_account ../../contracts/new_contract

If you see [Error 3080001: Account using more than allotted RAM usage], you should buy more RAM to keep the smart contract information by using the system buyram command. For example:

cleos system buyram eosio contract_account “1 EOS”

More about RAM in a different post

To call contract_function of the smart contract some_account sends some_data to the contract_account contract.

cleos push action contract_account contract_function ‘[ “some_data” ]’ -p some_account@active

Note: If we want the called contact function to authenticate the user, we need to add require_auth (user) (details in C / C ++ Function References) to the .cpp file where contract_function is implemented.

Update

In order to update the contract, you should compile the .wast file again and generate .abi, after these steps, run the command cleos set contract contract_account ../../contracts/new_contract again and the account’s contract will be updated. After the update, if the new version of the contract uses a different amount of RAM, the amount of memory used will automatically change to the required one (if less, some of the used memory will become free)

Note:

eosiocpp multi-tool will be removed in v1.3.0

contract development libraries (contracts/eosiolib, contracts/libc++, and contracts/musl ) will be removed in v1.3.0, some contracts examples will be removed too

new tools eosio.wasmsdk repository

Follow us!

Website: https://atticlab.net/eos/

Twitter: https://twitter.com/atticlab_it

Facebook: https://www.facebook.com/atticlab/

Reddit: https://www.reddit.com/r/atticlabeosb/

Steemit: https://steemit.com/eos/@attic-lab

Medium: https://medium.com/eosatticlab

Golos: https://golos.io/@atticlab

Telegram Chat: https://t.me/atticlabeosb

Telegram channel: https://t.me/eos_atticlab

--

--