Documentation
Search…
2 Creating a Simple Contract
When creating a contract you may note that most of the actions are typical. The differences lay in the implementation of the functions performed by the contract, which are implemented directly in the body of the contract. In this section instructions are given for creating a contract whose main function is to issue a greeting that comes in the form of «Hello, user».

2.1 Create a directory for contracts

Create a directory CONTRACTS_DIR, download the Contract Development Toolkit components necessary for compiling contracts in it.
1
cd CONTRACTS_DIR
2
git clone --recursive https://github.com/cyberway/cyberway.cdt --branch <branch name> --single-branch
3
cd cyberway.cdt
4
./build.sh
5
sudo ./install.sh
Copied!

2.2 Create the hello.cpp file

1
cd CONTRACTS_DIR
2
mkdir hello
3
cd hello
4
touch hello.cpp
Copied!
Put «Hello, user» text message into hello.cpp file.
1
#include <eosiolib/eosio.hpp>
2
3
using namespace eosio;
4
5
class [[eosio::contract("hello")]] hello : public contract {
6
public:
7
using contract::contract;
8
9
[[eosio::action]]
10
void hi( name user ) {
11
print( "Hello, ", user);
12
}
13
};
14
15
EOSIO_DISPATCH( hello, (hi))
Copied!
This action receives a parameter named «user» and displays a «Hello, user» message as a result. EOSIO_DISPATCH acts as a macro-operation to handle this action.

2.3 Compile hello.cpp

1
eosio-cpp -o hello.wasm hello.cpp --abigen
Copied!

2.4 Set (unfold) contract

During the installation of the contract an account of this contract is created, as well as a public key of the account.
1
cleos wallet keys
2
cleos create account cyber hello <public key> -p [email protected]
Copied!
A guide for creating a wallet as well as creating a development key can be found on the CyberWay website.

2.5 Set the absolute path to the created contract

Specify the absolute path <contracts dir path> to the contracts’ directory in the following command:
1
cleos set contract hello CONTRACTS_DIR/hello -p [email protected]
Copied!

2.6 Check whether contract is running properly

To verify the operation of the contract you can call an action using user’s name, for example, try sending a greeting to the user «Bob» using the following command:
1
cleos push action hello hi '["bob"]' -p [email protected]
Copied!
The operation is considered as successful if the following information is displayed on the monitor:
1
executed transaction: ...
2
# hello.code <= hello.code::hi {"user":"bob"}
3
>> Hello, bob
Copied!
To expand the functions of the contract it is necessary to expand the logic of the hello.cpp file. It’s the logic of the file file_name.cpp that determines the functionality of the contract being created.
Last modified 2yr ago