Printing 'Hello World' with EOS Smart Contract

ITAMITAM Posts: 1 Brand New

Simply put, the development of smart contracts uses llvm and abigen with the objective of producing an abi file. In order to accomplish this objective, eos offers a utility called eosiocpp. In this post, we will be going over how to use this utility to produce a hello world smart contract.

The content of this post was made through CentOS.

Smart Contract Write-up

First, write a ahello.cpp file.

#mkdir /home/centos/sc/ahello
#cd /home/centos/sc/ahello
#vim ahello.cpp

Insert the following into the ahello.cpp file

----- ahello.cpp file -----------
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;
class hello : public eosio::contract {
  public:
      using contract::contract;
/// @abi action 
      void hi( account_name user ) {
         print( "Hello, World", name{user} );
      }
};
EOSIO_ABI( hello, (hi) )
------ end of ahello.cpp file ---------------

Next, compile and create a wast (web assembly) file and an abi file.

#/root/eos/build/program/tools/eosiocpp -o ahello.wast ahello.cpp
#/root/eos/build/program/tools/eosiocpp -g ahello.abi ahello.cpp

Smart Contract Sample Upload and Envoke

Creation of an account for testing the smart contract

First, creation a wallet.

#./cleos wallet create -n scuser
Creating wallet: scuser
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5JzRwAUN-----------------------------nAuCRWvHx4XnMPmGf9Kz "

Create a private key and a public key.

#./cleos create key
Private key: 5KZzUHNFNvf------------------------------vuF5z7d29uAUbsdn
Public key: EOS63ndkvF-----------------------9ZVcByP9nfZcwMLzbMpn

Save the key in the wallet.

#./cleos wallet import -n scuser 5KZzUHNFNvf---------------------d29uAUbsdn

Create an additional wallet for testing.

#./cleos create key
Private key: 5JbriTGYsnrpNDvL------------------LgniHVgyTnS5ommxo
Public key: EOS8XZoG2248Gu42-------------ps7JoW8tdHQwCsV

Save the additional key in the wallet as well.

#./cleos wallet import -n scuser 5JbriTGYsnrpND----------HVgyTnS5ommxo

Next, create a user.

#./cleos create account eosio scuser EOS63ndkvF---------cByP9nfZcwMLzbMpn EOS8XZo-------wJnieps7JoW8tdHQwCsV

Now upload the smart contract.

#./cleos set contract scuser /home/centos/sc/ahello
Reading WAST/WASM from /home/centos/sc/ahello/ahello.wasm...
Using already assembled WASM...
Publishing contract...
executed transaction: 053a4883d9c191c2754656544dd045da17bd869250af13a00284a613eed3d23b  1792 bytes  601 us
#         eosio <= eosio::setcode               {"account":"scuser","vmtype":0,"vmversion":0,"code":"0061736d01000000013b0c60027f7e006000017e60027e7...
#         eosio <= eosio::setabi                {"account":"scuser","abi":{"types":[],"structs":[{"name":"hi","base":"","fields":[{"name":"user","ty...
warning: transaction executed locally, but may not be confirmed by the network yet

Bring forward the uploaded smart contract, add hi action data (parameter) and execute.

./cleos push action scuser hi '["user1"]' -p scuser
executed transaction: 9ed2894aef0f476687ad893ed16594588cc7a813c524d4b8497ba9f50793b151  104 bytes  330 us
#        scuser <= scuser::hi                   {"user":"user1"}
>> Hello, World user1
warning: transaction executed locally, but may not be confirmed by the network yet

Something similar to the captured image below will be recorded in the blockchain.

In this case, it was recorded in the 27,995th block.

Code Analysis

The sample code above is the basic template of an EOS smart contract. We will now breakdown and analyze the code written above.

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
  • include the header file for eos smart contract.
using namespace eosio;
  • Assign eos namespace as a basis.
  • Because eos namespace is the basis, data types such as account_name can be directly used.
class hello : public eosio::contract {
  • Include contract class into eos namespace to create hello class.
public:
using contract::contract;
/// @abi action
  • This shows the executed function of what actually happens in the blockchain when an action is specified.
      void hi( account_name user ) {
         print( "Hello, World", name{user} );
      }
};
EOSIO_ABI( hello, (hi) )
  • EOSIO_ABI is a macro that contains a previous version of the apply () function.

In this post, we went over how to print Hellow World into a smart contract using EOS.

Telegram: http://t.me/itamnetwork
Medium: https://medium.com/itam

Sign In or Register to comment.