eosio.token.hpp解析

本文深入解析EOS智能合约的实现机制,包括关键的接口库、资产定义、账户操作及资产交易功能。介绍了如何创建、发行和转移资产,以及资产持有者和资产状态的数据结构。
#pragma once


#include <eosiolib/eosio.hpp>    //大部分智能合约所要用到的接口库,包含一些基础文件、定义文件
#include <eosiolib/asset.hpp>   //定义了资产(数和量的资产  数:多少,  量:种类(ETH,BTC等))

#include <string>

namespace eosiosystem {
   class system_contract;
}

namespace eosio {

   using std::string;
    //定义类,该类继承自contract类
   class token : public contract {
    //注意:智能合约的名字与类没有关系,智能合约在eos系统你上传的时候规定的这个智能合约属于哪个账户,这个智能合约就叫那个名字
      public:
         token( account_name self ):contract(self){}//构造函数


        /*
        *函数名: create
        *作  用: 创建资产
        *说  明: 申明一个action(动作) ,让别人(账户)在我们的智能合约中创建一个资产。把资产数据保存到我们的智能合约的账户中。
        *         别人就不需要为发一个资产再开发一个智能合约了,只需要在我们这个智能合约账户里面新建一个资产。
        *参数说明:
        *issuer                 发币人     
        *maximum_supply         发币的最大量  
        *issuer_can_freeze      发币人是否冻结
        *issuer_can_recall      发币人是否可以召回
        *issuer_can_whitelist   发币人是否可以设置白名单, 有的币是发给特定的人。*/
         void create( account_name issuer,
                      asset        maximum_supply,
                      uint8_t      issuer_can_freeze,
                      uint8_t      issuer_can_recall,
                      uint8_t     issuer_can_whitelist);
        
        /*
        *函数名: issue
        *作  用: 发布资产
        *参数说明:
        *to           发给谁     
        *quantity     发送数量
        *memo         发行代币时的备注信息
       */
         void issue( account_name to, asset quantity, string memo );
         
       /*
        *函数名: transfer
        *作  用: 交易资产
        *参数说明:
        *from         谁转出来 
        *to           转给谁
        *quantity     数量  
        *memo         转账时的备注信息
        */
         void transfer( account_name from,
                        account_name to,
                        asset        quantity,
                        string       memo );

        /* 内联函数声明 */
         inline asset get_supply( symbol_name sym )const;   //获取某种类型的资产总量
         
         inline asset get_balance( account_name owner, symbol_name sym )const;;//获取某用户的某种资产的总量

      private:

      /*说明:定义资产持有人资料,结构体存放每一个拥有这个合约里资产的人(账户)的资料(账户名,是不是被冻结了,是不是在白名单里,
              如果要存在数据库里还需要一个主键primary_key)
      * 参数说明:
	     balance       资产余额
         froze         是否被冻结     默认否
         whitelist     是否在白名单   默认是
         primary_key   主键
      */
         struct account {
            asset    balance;
            bool frozen = false;
            bool whitelist =true;
            uint64_t primary_key()const { return balance.symbol.name(); }
         };


      /*
       *说明:资产的资料
       *参数说明: 
            supply   供应量(供应了多少)
            max_supply   最大供应量
            issuer    资产发行人
            can_freeze  能不能冻结  默认是
            can_recall    能不能召回  默认是
            can_whitelist  能不能设置白名单  默认是
            is_frozen  是不是已经被冻结   默认否  不冻结
            enforce_whitelist   是不是已经使用了白名单  默认否, 未使用白名单
            primary_key  主键
      */      
         struct currency_stats {
            asset          supply;
            asset          max_supply;
            account_name   issuer;
            bool can_freeze =true;
            bool can_recall = true;
            bool can_whitelist=true;
            bool is_frozen=false;
            bool enforce_whitelist=false;
            uint64_t primary_key()const { return supply.symbol.name(); }
         };
        
        
        
        
          //eosio::multi_index 是在boost::multi_index的基础上开发的自己的multi_index
         typedef eosio::multi_index<N(accounts), account> accounts;   //资产持有人列表类型
         typedef eosio::multi_index<N(stat), currency_stats> stats;   //资产列表类型
         //定义资产持有人资料和资产的资料放到multi_index 列表里,方便增删改查。
         
         
         /*
		  *说明:减少资产
          *参数说明:
		  *   owner    资产的拥有者
		  *   value    减少的资产值
		  *   st       该资产目前的状态
 		 */
         void sub_balance( account_name owner, asset value, const currency_stats& st );
       
         /*
		  *说明:增加资产
          *参数说明:
		  *   owner      资产的拥有者
		  *   value      增加的资产值
		  *   st         该资产目前的状态
		  *   ram_payer  为这个活动支付拥有系统内存的人, 在EOS系统中,智能合约处理数据的时候,是要占用系统内存的。
          *             这个系统内存空间是谁提供的呢?所以要定义一个ram_payer,让特定的人给系统运行提供内存。
 		 */ 
         void add_balance( account_name owner, asset value,const currency_stats& st, account_name ram_payer );         
        
      public:
         struct transfer_args {
            account_name  from;         //从哪
            account_name  to;           //到哪
            asset         quantity;     //数量
            string        memo;         //备注
         };
   };
   //实现内联函数
   asset token::get_supply( symbol_name sym )const
   {
      stats statstable( _self, sym );
      const auto& st = statstable.get( sym );
      return st.supply;
   }

   asset token::get_balance( account_name owner, symbol_name sym )const
   {
      accounts accountstable( _self, owner );
      const auto& ac = accountstable.get( sym );
      return ac.balance;
   }

} /// namespace eosio

 

/work/pcl-cross-compile/src/boost$ ls /work/pcl-cross-compile/install/boost/include/ algorithm dynamic_bitset.hpp make_unique.hpp qvm throw_exception.hpp align enable_shared_from_this.hpp math qvm.hpp timer aligned_storage.hpp exception math_fwd.hpp qvm_lite.hpp timer.hpp align.hpp exception_ptr.hpp move random token_functions.hpp archive foreach_fwd.hpp mpi random.hpp token_iterator.hpp asio foreach.hpp mpi.hpp range tokenizer.hpp asio.hpp format mpl range.hpp tti assign format.hpp multi_array rational.hpp tuple assign.hpp functional multi_array.hpp redis type_erasure call_traits.hpp functional.hpp multi_index redis.hpp type_index cerrno.hpp fusion multi_index_container_fwd.hpp regex type_index.hpp charconv geometry multi_index_container.hpp regex_fwd.hpp typeof charconv.hpp geometry.hpp multiprecision regex.h type_traits circular_buffer gil nondet_random.hpp regex.hpp type_traits.hpp circular_buffer_fwd.hpp gil.hpp none.hpp safe_numerics units circular_buffer.hpp graph none_t.hpp scoped_array.hpp unordered cobalt heap nowide scoped_ptr.hpp unordered_map.hpp cobalt.hpp histogram numeric scope_exit.hpp unordered_set.hpp compressed_pair.hpp histogram.hpp operators.hpp serialization url concept hof operators_v1.hpp shared_array.hpp url.hpp concept_archetype.hpp hof.hpp optional shared_ptr.hpp utility concept_check icl optional.hpp signals2 utility.hpp concept_check.hpp interprocess parameter signals2.hpp uuid config intrusive pending smart_ptr uuid.hpp config.hpp intrusive_ptr.hpp phoenix smart_ptr.hpp variant container io phoenix.hpp sort variant2 context io_fwd.hpp pointer_cast.hpp spirit variant2.hpp convert json pointer_to_other.hpp spirit.hpp variant.hpp convert.hpp json.hpp polygon stacktrace version.hpp cregex.hpp leaf pool stacktrace.hpp vmd cstdfloat.hpp leaf.hpp predef statechart wave cstdint.hpp limits.hpp predef.h static_assert.hpp wave.hpp cxx11_char_types.hpp locale program_options static_string weak_ptr.hpp date_time locale.hpp program_options.hpp static_string.hpp winapi date_time.hpp local_function progress.hpp stl_interfaces xpressive describe local_function.hpp property_map system yap describe.hpp lockfree proto system.hpp detail logic ptr_container test dynamic_bitset make_default.hpp python thread dynamic_bitset_fwd.hpp make_shared.hpp python.hpp thread.hpp uidq8326@hzh27145u:/work/pcl-cross-compile/src/boost$ ./b2 install toolset=gcc architecture=arm address-model=64 \--prefix=/work/pcl-cross-compile/install/boost -j$(nproc) Performing configuration checks - default address-model : 64-bit [1] - default architecture : arm [1] - symlinks supported : yes error: Unable to find file or target named error: '/boost/headers' error: referred to from project at error: 'libs/date_time/build'
最新发布
08-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FLy_鹏程万里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值