[Xu Xiaodi] Develop the first EOS smart contract

With a little understanding of the EOS system, you will know that EOS's smart contracts are based on WebAssembly (WASM) technology, which achieves a good balance between performance and cross-platform compatibility. By compiling the original code into bytecode, it makes The code can be executed in the WASM virtual machine (or interpreter) on various platforms. Backed by tech giants such as Apple and Google, it has been hailed as the next generation of front-end internet technology. The current WebAssembly technology supports the C/C++ language, and has developed a JavaScript interface, which is supported by almost all mainstream browsers such as Chrome, Edge, Safari, and Firefox.

Because of the use of WebAssembly, the current EOS smart contract only supports C/C++ language, and a simple smart contract consists of 3 files: .hpp file, .cpp file, and .abi file. Among them, hpp is a C++ header file, which is generally used to define a class and its member variables and member functions. cpp is a C++ file used to implement the member functions declared in hpp and implement the business logic of the smart contract. The abi (Application Binary Interface) file is a binary interface file, the file format is similar to JSON, which is used to define the data interface between the smart contract and the external interaction of the EOS system.

If the smart contract is very simple with only one cpp file, you can omit the hpp file and define the classes and members in the cpp file. The abi file should be generated by the database space and external interface required by the C++ program, but EOS has developed an abi automatic generation tool, which can automatically generate the abi file according to the smart contract code, reducing the development workload. So the simplest smart contract only needs to implement the cpp file.

Hello Smart Contract

When the general operating system gets started, the convention is to write a Hello World program, which is to actively output a sentence. But we are different. What we write is a smart contract. The smart contract emphasizes interaction. It is called Action in EOS. Action means what other people can do to the contract. All smart contract code is a response to Action, which is passive. . Here is the first Hello smart contract:

hello.cpp:

#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, ", name{user} );
     }
};

EOSIO_ABI( hello, (hi) )

We define a class in the code: hello, this class name has nothing to do with the account name of the contract, there is only one simple method in the class:

void hi( account_name user ) {
        print( "Hello, ", name{user} );
     }

This is what is called in the EOS smart contract. We define an Action called hi. The parameter is another account name. The function body prints a sentence and responds to hello. That is to say, other accounts can call the hi Action of this contract, and the hello contract will print a hello in response. Action

The last line of code:

EOSIO_ABI( hello, (hi) )

EOSIO_ABI It is a macro that exposes a specific method of a specific class to the system and becomes an Action that other accounts can call.

Compile the smart contract

We use the eosiocpp tool to compile the written hello.cpp into a bytecode file (.wast):

$ eosiocpp -o hello.wast hello.cpp

Then use the eosiocpp tool to automatically generate the abi file:

$ eosiocpp -g hello.abi hello.cpp
Generated hello.abi

Take a look at the content of the generated abi file:

{
 "____comment": "This file was generated by eosio-abigen. DO NOT EDIT - 2018-04-16T13:37:55",
 "types": [],
 "structs": [{
     "name": "hi",
     "base": "",
     "fields": [{
         "name": "user",
         "type": "account_name"
       }
     ]
   }
 ],
 "actions": [{
     "name": "hi",
     "type": "hi",
     "ricardian_contract": "# CONTRACT FOR hello::hi## ACTION NAME: hi\n
     ### Parameters### Parameters\nInput paramters:Input paramters:\n
     \n
     * `user` (string to include in the output)* `user` (string to include in the output)\n
     \n
     Implied parameters: Implied parameters: \n
     \n
     * `account_name` (name of the party invoking and signing the contract)* `account_name` (name of the party invoking and signing the contract)\n
     \n
     ### Intent### Intent\n
     INTENT. The intention of the author and the invoker of this contract is to print output. It shall have no other effect.INTENT. The intention of the author and the invoker of this contract is to print output. It shall have no other effect.\n
     \n
     ### Term### Term\n
     TERM. This Contract expires at the conclusion of code execution.TERM. This Contract expires at the conclusion of code execution.\n"
   }
 ],
 "tables": [],
 "ricardian_clauses": [

  ...
  ...
  ...

 ]
}

We omit  , that is, the Ricardian clause part (Ricardian contracts refer to contracts that can be read by humans and machines, and EOS only recently added them to smart contracts).  We see that this has been declared in the abi file , and  the Ricardian contract of this is explained, which probably means that the input of this contract is a string of characters (user), and the intention of this contract is to print out and have no other effect. ricardian_clauses hi Action Action

Upload smart contract

Before uploading a smart contract, we need to create an account for the smart contract. There is a one-to-one correspondence between accounts and smart contracts in EOS. Create an account using the cleos command-line tool for EOS:

$ cleos create account eosio hello.code EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4 EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4

In the command, it hello.code is the account name of the smart contract. The account name of the EOS system must be within 12 characters. The last two public keys are the public keys that have the authority to create an account in the local test network (corresponding to the eosio account in the local test network).

Then you can upload the smart contract:

$ cleos set contract hello.code ../hello -p hello.code

Use smart contracts

We use  the  account to call : user hello.code hi Action

$ cleos push action hello.code hi '["user"]' -p user

hello.code It means to execute the hello.code contract, and hi it means to execute the hi Action in the contract. It '["user"]' is based on the incoming parameter written by abi, and the  parameter indicates the permission of which account to use. -p

The following is the system response:

executed transaction: 4c10c1426c16b1656e802f3302677594731b380b18a44851d38e8b5275072857  244 bytes  1000 cycles
#    hello.code <= hello.code::hi               {"user":"user"}
>> Hello, user

 It indicates that the action of the contract is executed  , and the system output is , the smart contract successfully responded to the Action. hello.code hi Hello, user

Yuanfangyuan Blockchain brings together a large number of famous blockchain teachers and adopts a tutor duty system to solve technical difficulties for students in real time. Please pay attention to Yuanfangyuan Blockchain Knowledge Planet and Mentors. (For training inquiries, please contact the captain on 13826054890 with the same WeChat mobile number)

 

The author, Xiaodi, focuses on EOS technology research and blockchain smart contract development. He is the mentor of Yuanfangyuan Blockchain. For more articles and videos of Mr. Xiaodi, please pay attention to the official account of Yuanfangyuan Chain Circle.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324808010&siteId=291194637