一个EOS延迟交易的示例

合约基本功能编写完成,主要功能是为每个玩家存储雇佣的npc(劳工),并且记录npc的薪水,定时支付给npc工资。

由于最终规则还没有定,所以未对程序进行优化。

 

Labornpc.hpp源码

class [[eosio::contract("labornpc")]] labornpc : public contract {

   public:

      using contract::contract;

      //对外接口,玩家雇佣NPC

      [[eosio::action]]

      void addnpc( name   player, const std::vector<name>   &npcid,

                   asset  wage);

      //对外接口,玩家解雇NPC

      [[eosio::action]]

      void delnpc( name   player,const std::vector<name>   &npcid);

      //对外接口,玩家开业

      [[eosio::action]]

      void create( name   user, asset  balance );

   

   private:

    

      name              playuser;

      asset             balance;

      time_point_sec    opentime;

      time_point_sec    pay_wage_time;

 

//定义一个持久化的表结构,记录NPC雇员名称以及雇员的薪水

      struct [[eosio::table]] currency_npc {

         asset             wage;

         time_point_sec    payedtime;

         name              npcid;

         time_point_sec    joindate;

         uint64_t primary_key()const { return npcid.value; }

      };

//持久化

      typedef eosio::multi_index< "npcinfo"_n, currency_npc > npcinfos;

 

      //内部函数,支付工资

      void paywage( );

      //内部函数,设置下次定时支付工资

      void paycontiue(  );

};

程序的关键部分

//支付给NPC雇员工资

void labornpc::paywage( )

{

    print( "Labor::paywage\n");

    //访问NPC雇员信息表

     npcinfos npcitable( _self,_code.value  );

//根据NPC雇员信息,支付薪水

     for(auto& item : npcitable) {

      print( "Labor::paywage npcid:", name{ item.npcid}, "\n");

       action(

        permission_level{_self,"active"_n},

        "xcenter"_n, "transfer"_n,//部署token合约账户名,以后需要修改。

        std::make_tuple(_self, item.npcid, item.wage, _self )

        ).send();

    }

    //预定下次延时交易

    paycontiue( );

}

 

void labornpc::paycontiue( )

{

   //设置下次延时交易

    eosio::transaction txn{};

    txn.actions.emplace_back(

      eosio::permission_level(_self, "active"_n),

      _self,

     "paywage"_n, //action名称

      std::make_tuple());//tuple

//设置延时交易的时间,单位为秒,这里为了测试方便,修改为1秒

    txn.delay_sec = 1;

    //(sender_id, payer, replace_existed)

    txn.send(_self.value, _self, false);

 

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值