编写简单的智能合约

基本步骤

EOS的智能合约,是用CPP语言写的,通过eosiocpp工具,分别生成wasm编码(执行),和abi文件(描述)

然后通过cleos set contract工具加载到区块链,最后通过cleos push actoin执行

Hello.cpp

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

using namespace eosio;

class nhello : public eosio::contract {
  public:
      using contract::contract;

      /// @abi action
      void hi( account_name user ) {
         //限定输入的user必须是执行合约的用户
         require_auth( user );
         print( "Hello, ", name{user} );
      }
};

EOSIO_ABI( nhello, (hi) )

编译

eosiocpp -o neo.hello.wast neo.hello.cpp
eosiocpp -g neo.hello.abi neo.hello.cpp

加载及运行

#开启挖坑
nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin  eosio::account_history_api_plugin

#打开并解锁钱包
cleos wallet open
cleos wallet unlock
"PW5JAj9qw2tQsuVajLVXsiKUD7ULQnZ83qxpaB5YVFcs6qbhQ84J1"

cleos wallet open -n hospital2
cleos wallet unlock -n hospital2
"PW5JxnbBTTbTQKUHHNvYojU1xrcjxQduz2wU6gfMEgiZjwT5S6xJ6"

cleos wallet list

#创建运行合约的账号
cleos create account eosio hello1 EOS8K9wqRtD8cCuHvbdzcquEhYHnSaFvyQSWm8kVxYFHavraNeue2 EOS8K9wqRtD8cCuHvbdzcquEhYHnSaFvyQSWm8kVxYFHavraNeue2

#加载合约
cleos set contract hello1 neo.hello -p hello1

#运行合约
cleos push action hello1 hi '["hello1"]' -p hello1

最后更新于