[blockchain-036] 用c++编写一个智能合约

琴俊良
2023-12-01

1.参考文献

https://github.com/EOSIO/eos/wiki/Tutorial-Hello-World-Contract

注意:编译eos之后,要"cd build; sudo make install",这样后文的编译才不会出现问题,否则会缺失很多头文件

2.启动单节点eos测试网络

./nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::account_history_api_plugin

3.编写部署智能合约

  3.1 创建目录hello, cd hello
  3.2 创建hellp.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) )

    3.3 编译hello.wast文件

eosiocpp -o hello.wast hello.cpp

3.4  编译hello.abc文件

eosiocpp -g hello.abi hello.cpp

3.5 创建帐号      

cleos create account eosio hello.code EOS6x6SZaYXZiWE1xebmCUKy5afPAueQiKPmXRpGBF9s5HyQtgXox EOS6x6SZaYXZiWE1xebmCUKy5afPAueQiKPmXRpGBF9s5HyQtgXox

3.6 部署智能合约

cleos set contract hello.code ../hello -p hello.code
3.7 运行智能合同
     cleos push action hello.code hi '["user"]' -p user
  运行之后,会输出
      #    hello.code <= hello.code::hi               {"user":"user"}
      >> Hello, user
 类似资料: