当前位置: 首页 > 文档资料 > EOS 开发者指南 >

EOS 3.0 HelloWorld 教程

优质
小牛编辑
126浏览
2023-12-01

1.智能合约文件的编写

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

//用eosio命名空间
using namespace eosio;

//所有的智能合约都继承自contract类
class hello : public eosio::contract {

  public:
      using contract::contract;

      /// @abi action
      void hi( account_name user ) {
         print( "Hello, ", name{user} );
      }

};
EOSIO_ABI( hello, (hi) )

2.合约文件的生成

很多人运行eosiocpp的时候提示:

not found ~/eos/build/bin/eosio-abigen

下面是生成文件的正确方法:

programs/eosio-abigen 文件夹下编译过得 eosio-abigen 文件放在 ~/eos/build/bin/ 文件夹下.

.wast生成:

eosiocpp -o hello.wast hello.cpp

生成 .wast 文件.

'.abi'生成:

eosiocpp -g hello.abi hello.cpp

生成 .abi 文件.文件内容如下:

{
  "____comment": "This file was generated by eosio-abigen. DO NOT EDIT - 2018-04-02T13:49:42",
  "types": [],
  "structs": [{
      "name": "hi",
      "base": "",
      "fields": [{
          "name": "user",
          "type": "account_name"
        }
      ]
    }
  ],
  "actions": [{
      "name": "hi",
      "type": "hi"
    }
  ],
  "tables": []
}

3.部署合约

lome@lome:~/eos/build/tools$ cleos set contract lome hello -p lome
Reading WAST...
Assembling WASM...
Publishing contract...
executed transaction: 245c6ef78373689de9036a4a0a1b3740f9ac15e059245c79d2e0a8807ba74907  1668 bytes  10000 cycles
#         eosio <= eosio::setcode               {"account":"lome","vmtype":0,"vmversion":0,"code":"0061736d01000000018b011460027f7e0060057e7e7e7e7f0...
#         eosio <= eosio::setabi                {"account":"lome","abi":{"types":[],"structs":[{"name":"hi","base":"","fields":[{"name":"user","type...

4.执行合约

lome@lome:~/eos/build/tools$ cleos push action lome hi '{"user": "lome"}' -p lome2
executed transaction: 6807d42b37660347a4f33704f1c8082be8517634c3c21ea76550b31c44465e73  244 bytes  1000 cycles
#          lome <= lome::hi                     {"user":"lome"}
>> Hello, lome