EOS搬运工之智能合约的编写、编译、部署和执行

编写、编译、部署和执行EOS智能合约

EOS智能合约的目的就是定义一个数据集,并定义与数据集相关联的一系列动作,使得数据在链上持久保存。其中的数据集便是EOS中的表,动作对应action。合约在EOS中的准确定义,可以参考官方文档:https://developers.eos.io/eosio-cpp/docs/introduction

先看下源码

farm.hpp

#pragma once

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

using std::string;
using eosio::contract;

class farm : public contract {
    public:
          explicit farm( action_name self )
          :contract(self) {}

          // 插入一条记录
          void insert( uint64_t id, const string& name, const string& create_time, const string& location );
          // 更新一条记录
          void update( uint64_t id, const string& name, const string& create_time, const string& location );
          // 删除一条记录
          void del( const uint64_t id );
    private:
          // @abi table tfarm
          struct myfarm {
            uint64_t  id;
            string    name;
            string    create_time;
            string    location;

            uint64_t primary_key()const {return id;}

            EOSLIB_SERIALIZE( myfarm, (id)(name)(create_time)(location) )
          };

          // 这里的表名“tfarm”,必须和上面@abi申明的表名相同,否则执行cleos get table时将查不到数据
          typedef eosio::multi_index<N(tfarm), myfarm> farms;
};

farm.cpp

#include "farm.hpp"

void farm::insert( uint64_t id, const string& name, const string& create_time, const string& location )
{
        // 实例化表
        farms afarm(_self, _self); // 第一个参数是表所在合约的账户,第二个参数是表记录所在的域(scope),参数类型都是uint64_t

        // 判断插入的记录是新的
        auto itr = afarm.find(id);
        eosio_assert(itr == afarm.end(), "the farm already exists");

        // 添加数据
        // 上链存储需要付费,第一个参数是付费的账户,类型是uint64_t
        afarm.emplace(_self, [&](auto& newfarm){
            newfarm.id = id;
            newfarm.name = name;
            newfarm.create_time = create_time;
            newfarm.location = location;
        });
}

void farm::update( uint64_t id, const string& name, const string& create_time, const string& location )
{
        farms afarm(_self, _self);

        auto itr = afarm.find(id);
        // 更新的记录必须存在
        eosio_assert(itr != afarm.end(), "the farm not found");

        // 修改链上的记录需要付费,第2个参数是付费账户,类型是uint64_t
        afarm.modify(itr, _self, [&](auto& newfarm){
            newfarm.create_time = create_time;
            newfarm.name = name;
            newfarm.location = location;
        });
}
void farm::del( const uint64_t id )
{
        farms afarm(_self, _self);

        auto itr = afarm.find(id);
        eosio_assert(itr != afarm.end(), "the farm not found");

        //删除
        afarm.erase(itr);
}

EOSIO_ABI( farm, (insert)(update)(del) )

编译、部署和执行

$ eosiocpp -o farm.wast farm.cpp
$ eosiocpp -g farm.abi farm.cpp
$ cleos create account eosio farm.code EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
$ cleos set contract farm.code ../farm -p farm.code@active
$ cleos push action farm.code insert '[123, "fashion-farm", "2018-08-15", "hangzhouxihu"]' -p farm.code@active
$ cleos get table farm.code farm.code tfarm; #查询表数据

自己开发智能合约中的一些坑

  1. 如果有多个cpp文件,需要在引用cpp文件里#include被引用的cpp,hpp里#include对应的hpp头文件,否则在部署合约的时候,会报函数未定义(unresolve)的错误,即使在eosiocpp命令行明确条件被引用的cpp文件也不行。虽然,编译wast和abi文件都能成功。

猜你喜欢

转载自blog.csdn.net/ccyhummer/article/details/81705318