1. eosio.token.hpp

eosio.token.hpp

1. 整体评注

EOS 的代币智能合约 eosio.token,用于代币相关的所有操作,包括:创建新代币、释放代币、代币转移等。

智能合约的开发严重依赖于 eosio.cdt 中的相关代码,并通过 eosiolib 中的头文件进行引用,最新版是从 eosio 中引用所有头文件。

合约 eosio.token 中创建了两个重要的数据结构:account, currency_stats。其中:account 描述了账户当前的余额,currency_stats 描述了代币的基础信息,如:issuer, supply, max_supploy 等。

合约 eosio.token 中创建了两个存储结构:accounts, stats。其中:accounts 存储了所有的 account 信息,stats 存储了所有的 currency_stats 信息。这些数据结构都是基于 boost::multi_index 数据结构来进行存储和索引的。

2. 源代码及注释

/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
#pragma once

#include 
#include 

#include 

namespace eosiosystem {
   class system_contract;
}

namespace eosio {

   using std::string;

   class [[eosio::contract("eosio.token")]] token : public contract {
      public:
         using contract::contract;

         [[eosio::action]]
         void create( name   issuer,
                      asset  maximum_supply);

         [[eosio::action]]
         void issue( name to, asset quantity, string memo );

         [[eosio::action]]
         void retire( asset quantity, string memo );

         [[eosio::action]]
         void transfer( name    from,
                        name    to,
                        asset   quantity,
                        string  memo );

         [[eosio::action]]
         void open( name owner, const symbol& symbol, name ram_payer );

         [[eosio::action]]
         void close( name owner, const symbol& symbol );

         static asset get_supply( name token_contract_account, symbol_code sym_code )
         {
            stats statstable( token_contract_account, sym_code.raw() );
            const auto& st = statstable.get( sym_code.raw() );
            return st.supply;
         }

         static asset get_balance( name token_contract_account, name owner, symbol_code sym_code )
         {
            accounts accountstable( token_contract_account, owner.value );
            const auto& ac = accountstable.get( sym_code.raw() );
            return ac.balance;
         }

         using create_action = eosio::action_wrapper<"create"_n, &token::create>;
         using issue_action = eosio::action_wrapper<"issue"_n, &token::issue>;
         using retire_action = eosio::action_wrapper<"retire"_n, &token::retire>;
         using transfer_action = eosio::action_wrapper<"transfer"_n, &token::transfer>;
         using open_action = eosio::action_wrapper<"open"_n, &token::open>;
         using close_action = eosio::action_wrapper<"close"_n, &token::close>;
      private:
         struct [[eosio::table]] account {
            asset    balance;

            uint64_t primary_key()const { return balance.symbol.code().raw(); }
         };

         struct [[eosio::table]] currency_stats {
            asset    supply;
            asset    max_supply;
            name     issuer;

            uint64_t primary_key()const { return supply.symbol.code().raw(); }
         };

         typedef eosio::multi_index< "accounts"_n, account > accounts;
         typedef eosio::multi_index< "stat"_n, currency_stats > stats;

         void sub_balance( name owner, asset value );
         void add_balance( name owner, asset value, name ram_payer );
   };

} /// namespace eosio


Reference

  1. https://github.com/EOSIO/eosio.contracts

Contributor

  1. Windstamp, https://github.com/windstamp

你可能感兴趣的:(1. eosio.token.hpp)