Write Your First EOS Blockchain based Smart Contract in a Pre-configured Development Environment
1.0 Introduction
Setting up a development environment to write smart contract itself needs lots of effort. In this article, I will let you know, how to write EOS blockchain based smart contract using pre-configured development environment powered by gitpod.io and Docker. We will write a smart contract to display the message “hello, star”.
2.0 EOS Blockchain
EOS blockchain uses Delegated Proof of Stack (DPOS) algorithm for consensus which gives it power to perform transaction very fast. To know more about eos, visit the website https://eossupport.io. C++ is the programming language of EOS blockchanin. If you are new to c++ or want to brush up your c++ knowledge, follow the link https://www.w3schools.com/CPP/default.asp.
3.0 Setting up Your Development Environment
To setup your development environment, you have to arrange so many things, correct hardware, compatible operating system, flawless installation, create development wallet, create development account and many more steps. All these steps are done for you in the development environment offered by gitpod.io. Development environment can be accessed by clicking on the link given below:
It will ask you to enter your github user name & password. On success, you will get your own dashboard in pre-configured development environment. Your EOS development environment looks like figure given below.
It is ready with development account eosio having public key “EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV”.
It comes with a default smart contract, leave it as it is, create separate directory for your own smart contract. In the next section, we will write the smart contract to display message, “hello, star”.
1.0 Write the Contract
Do following steps to write your first smart contract.
Step 1 : Open a new terminal
Use the terminal drop-down menu to open a new terminal.
Step 2 : Create a directory for your smart contract
Create a directory called hello with the command given below:
mkdir hello
Step 3 : Go to new director with the command given below:
cd hello
Step 4 : Create a new file called hello.cpp with the command given below:
touch hello.cpp
You can check in the explorer a file called hello.cpp is present under the hello directory. Click on the file to open it in the editor.
Step 5 : Write the smart contract code
a) Add the following line at the top of the file to import eosio base library:
#include <eosio/eosio.hpp>
b) Add the following line to create the contract class:
Recommended by LinkedIn
class [[eosio::contract]] hello : public eosio::contract {
//write your code here
};
c) Add public specifier:
public
using eosio::contract::contract;
Your complete first smart contract is as given below:
#include <eosio/eosio.hpp>
class [[eosio::contract]] hello : public eosio::contract {
public:
using eosio::contract::contract;
[[eosio::action]] void hi( eosio::name user ) {
print( "Hello, ", user);
}
};
Step 6 : Compile and deploy
a) Use the command given below to compile your program:
eosio-cpp -abigen -o hello.wasm hello.cpp
Check that two files called hello.abi and hello.wasm are created under hello directory.
b) Use the following commands to Install the contract:
cleos create account eosio hello EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
cleos set code hello hello.wasm
cleos set abi hello hello.abi
Step 7 : Creating users and using the contract
a) Create a user called star using the command given below:
cleos create account eosio star EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
b) Finally, display the message:
cleos push action hello hi '["star"]' -p star@active
5.0 Conclusion
In this article, we have learned how to write eos blockchain based smart contract using pre-configured, web based development environment offered by gitpod. In our next article, we will learn, how to add authorization to our smart contract.
Great Work!