Blockchain | EOS | Smart contract smart contract introduction 1-Hello World!

1. Create a new folder helloWorld under your contract directory.

cd /Users/yourUserName/Documents/EOS/contracts
mkdir helloWorld
cd helloWorld

2. Create a C ++ file in the newly created folder.

touch helloWorld.cpp

3. Write the following in it. 

#include <eosio/eosio.hpp>  //导入eosio头文件

using namespace eosio;      //使用命名空间,可以使代码更加简洁,因为eos中所有类跟函数都在eosio命名空间下;

class [[eosio::contract]] helloWorld : public contract { //合约需继承自eosio::contract(因为使用了命名空间,此处可以简写为public contract);
							//同时使用[[eosio::contract]]来指定编译器生成主调度程序并用来生成ABI。
  public:
      using eosio::contract::contract;  //继承eosio::contract中的构造函数,可省略eosio::;

      [[eosio::action]]    //用eosio::action来修饰该方法为一个动作;
      void hello( name user ) {   //name是eosio中的一种常用数据类型;
		  print( "Hello World", user );
		}
};

Note: #warning "<eosiolib / eosio.hpp> is deprecated use <eosio / eosio.hpp>", the above warning is written in the eoiso.hpp source file.

4. Compile the file. The contract needs to be compiled into the wasm format before it can be deployed on the blockchain network. 

eosio-cpp helloWorld.cpp -o helloWorld.wasm

    The following warning will appear, no need to heed.

Warning, empty ricardian clause file
Warning, empty ricardian clause file
Warning, action <hello> does not have a ricardian contract

    After the compilation is successful, there will be two more files.abi and .wasm in the same directory of .cpp. .abi is called application binary interface, which is a json-based interface description document. .wasm is a binary file, a format of Web Assembly, which has another readable text format wast, the two are equivalent.

 5. Create an account called hello, and then deploy the above contract to the account.

cleos create account eosio hello EOS6GwYrUANGhouVRR97W8ukiCPX49Z74toqMTTxGH8mViYiPuuPi -p eosio@active

     It means that the account eosio creates a class account hello and uses the public key EOS6G ... uPi to give it active permissions. If you use cleos get account hello, you will find that the owner permission public key is also set to the same public key as the active permission

At the same time create an account bob. For simplicity, the same public key is used here.

cleos create account eosio bob EOS6GwYrUANGhouVRR97W8ukiCPX49Z74toqMTTxGH8mViYiPuuPi -p eosio@active

6. Deploy the helloWorld contract to the hello account.

cleos set contract hello /Users/yourUserName/Documents/EOS/contracts/helloWorld -p hello@active

    Command line explanation: set contract accountName contractDirect. That is deployed to the hello user, the contract address is /...../helloWorld. The permission is from the hello user to the active permission.

    The output is as follows, because I have already run it, so the abi shown here is the same as the existing one. 

Reading WASM from /Users/yourUserName/Documents/EOS/contracts/helloWorld/helloWorld.wasm...
Skipping set abi because the new abi is the same as the existing abi
Publishing contract...
executed transaction: b73420d8256676389aaeffbcd8f6e0d30d76d3cdaebae8ada5988844f3098cde  640 bytes  358 us
#         eosio <= eosio::setcode               {"account":"hello","vmtype":0,"vmversion":0,"code":"0061736d0100000001370b6000017f60027f7f0060037f7f...
warn  2020-04-22T01:24:38.839 thread-0  main.cpp:506                  print_result     warning: transaction executed locally, but may not be confirmed by the network yet

7. Execute the helloWorld contract.

cleos push action hello hi '["bob"]' -p bob@active

     The results are as follows.

executed transaction: 8857adbe3a2671c98f39ec7dc212313163092eea09f096f4c3c14bbc64e396ae  104 bytes  154 us
#         hello <= hello::hello                 {"user":"bob"}
>> Hello Worldbob
warn  2020-04-22T01:27:29.754 thread-0  main.cpp:506                  print_result     warning: transaction executed locally, but may not be confirmed by the network yet

 

In addition, a line of user verification is added below, that is, the input name parameter must be consistent with the recipient, otherwise the contract cannot be executed.

#include <eosio/eosio.hpp>  //导入eosio头文件

using namespace eosio;      //使用命名空间,可以使代码更加简洁,因为eos中所有类跟函数都在eosio命名空间下;

class [[eosio::contract]] helloWorld : public contract { //合约需继承自eosio::contract(因为使用了命名空间,此处可以简写为public contract);
							//同时使用[[eosio::contract]]来指定编译器生成主调度程序并用来生成ABI。
  public:
      using eosio::contract::contract;  //继承eosio::contract中的构造函数,可省略eosio::;

      [[eosio::action]]    //用eosio::action来修饰该方法为一个动作;
      void hello( name user ) {   //name是eosio中的一种常用数据类型;
		  require_auth( user );
		  print( "Hello World", name{user} );
		}
};

 

Note: This article refers to the official development document and adds appropriate comments.

Published 4 original articles · Like1 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/weixin_40462761/article/details/105696618