Set up EOS Smart Contract (Hello World!)
We are about to introduce an example that produces ‘Hello World’ with EOS Smart Contract.
General environment set-up would be done through the post: EOS Development Environment set-up and Run, and this article references the official EOS Git example.
Running nodeos
For verifying Hello World log, run it with — contracts-console option.
nodeos --contracts-console
Generating Smart Contract
Let’s try to make a folder called hello, in a different terminal, through accessing eos directory, besides the terminal for nodeos. The commands are below:
$ cd eos
$ cd mkdir hello
$ cd hello
Then, please create hello.cpp file and copy the below example and paste it. Hello contract inherits contract that EOS.IO provides, and has hi function that prints out an username with “Hello, ” attached in front of it.
Compile Code
- For the first command, let’s compile the code with WebAssembly. When compiling it, there might be a warning printed out (but we can ignore it)
- For the second command, let’s create abi.
# Compile webaassembly
eosiocpp -o hello.wast hello.cpp# Creating abi
eosiocpp -g hello.abi hello.cpp
Account Creation and Contract Release
- Create an account called hello.code. We will further use this account for releasing hello contract.
- Release contract with ‘cleos set contract ${account} ${path} -p ${permmition}’ command.
$ cleos create account eosio hello.code EOS8QMGRoRPZ4uf3w8WACcrg3wKzLtXpCk5Gpia6pdFzSuftLigWT EOS8QMGRoRPZ4uf3w8WACcrg3wKzLtXpCk5Gpia6pdFzSuftLigWTexecuted transaction: e6847fc85c7733dd70a9ff27c2cad98ea0b50fb6c80c2b0c7ea1bf64f9917916 200 bytes 225 us# eosio <= eosio::newaccount {"creator":"eosio","name":"hello.code","owner":{"threshold":1,"keys":[{"key":"EOS8QMGRoRPZ4uf3w8WACc...$ cleos set contract hello.code ../hello -p hello.codeReading WAST/WASM from ../hello/hello.wasm...Using already assembled WASM...Publishing contract...executed transaction: 7e1b070382188677e70cf4b87e8fbe02c072f10063983ffc1d8259b127d8fea7 1800 bytes 723 us# eosio <= eosio::setcode {"account":"hello.code","vmtype":0,"vmversion":0,"code":"0061736d01000000013b0c60027f7e006000017e600...# eosio <= eosio::setabi {"account":"hello.code","abi":"0e656f73696f3a3a6162692f312e30000102686900010475736572046e616d6501000...
Calling Function
Let’s call hi function of the hello class. It can be done with the below command:
cleos push action ${contract_name} ${function} ${[argument]} -p ${permission}
In the example of below, the user account called hi function in the hello.code contract.
$ cleos push action hello.code hi '["user"]' -p userexecuted transaction: d7932d1ee61ab6b0fed1f9e20d4a2e2607b029763aeaf1daea4ed718d2885797 104 bytes 500 us# hello.code <= hello.code::hi {"user":"user"}
Result
Below output has been added in the block where the transaction is executed in nodeos terminal. Followed by “Hello, “ is the name of the user who called hi function.
2703777ms thread-0 apply_context.cpp:28 print_debug ][(hello.code,hi)->hello.code]: CONSOLE OUTPUT BEGIN =====================Hello, user[(hello.code,hi)->hello.code]: CONSOLE OUTPUT END =====================
Extra) Granting an authority to run through require
- Replace to the hi function that contains require_auth like following.
Result
- An error (as below) occurs when we tried to call it with an unauthorized account.
- With an authorized account, it outputs normally without an error.
# Authorized$ cleos push action hello.code hi '["tester"]' -p userError 3090004: missing required authority# Unauthorized$ cleos push action hello.code hi '["tester"]' -p testerexecuted transaction: 16a34c27c7d162dc3940358197306df619911fb930cbddd6d208125a770886f4 104 bytes 243 us# hello.code <= hello.code::hi {"user":"tester"}
- Log can be printed as below, in nodeos’s block
2525788ms thread-0 http_plugin.cpp:405 handle_exception ] FC Exception encountered while processing chain.push_transaction2525788ms thread-0 http_plugin.cpp:406 handle_exception ] Exception Details: 3090004 missing_auth_exception: missing required authoritymissing authority of tester {"account":"tester"}thread-0 apply_context.cpp:132 require_authorization {"_pending_console_output.str()":""}thread-0 apply_context.cpp:62 exec_one