Ví dụ của bác Dan về phát hành token EOS

Thảo luận trong 'EOS' bắt đầu bởi Cờ ríp tô cơ rừn si, 30/3/18. Trả lời: 0 Xem: 1,089.

  1. Cờ ríp tô cơ rừn si
    Bác Dan vừa đăng đàn thông báo đang cố gắng chạy đua với thời gian để tung ra bản Dawn 3.0 cho kịp thời gian. Bác ấy đưa ra một doạn snippet code ví dụ về cách phát hành ra một token trên blockchain của EOS, AE xem tham khảo và tham chiếu sang ERC-20 của Ethereum nhé:

    Mã:
    #include <eosiolib/eosio.hpp>
    
    class simpletoken : public eosio::contract {
       public:
          simpletoken( account_name self )
          :contract(self),_accounts(self, self){}
    
          /** User's call this method to transfer tokens */
          void transfer( account_name from, account_name to, uint64_t quantity ) {
             require_auth( from );
    
             const auto& fromacnt = _accounts.get( from );
             eosio_assert( fromacnt.balance >= quantity, "overdrawn balance" );
             _accounts.modify( fromacnt, from, [&]( auto& a ){
                    a.balance -= quantity;
             } );
    
             add_balance( from, to, quantity );
          }
    
          /** Contract owner can issue new tokens */
          void issue( account_name to, uint64_t quantity ) {
             require_auth( _self );
             add_balance( _self, to, quantity );
          }
    
       private:
          struct account {
             account_name owner;
             uint64_t     balance;
    
             uint64_t primary_key()const { return owner; }
          };
          /** account database maps owner to their balance */
          eosio::multi_index<N(accounts), account> _accounts;
    
          void add_balance( account_name payer, account_name to, uint64_t q ) {
             auto toitr = _accounts.find( to );
             if( toitr == _accounts.end() ) {
               _accounts.emplace( payer, [&]( auto& a ) {
                  a.owner = to;
                  a.balance = q;
               });
             } else {
               _accounts.modify( toitr, 0, [&]( auto& a ) {
                  a.balance += q;
                  eosio_assert( a.balance >= q, "overflow detected" );
               });
             }
          }
    };
    
    EOSIO_ABI( simpletoken, (transfer)(issue) )
    Nguồn: EOS.IO
    P/S: cộng đồng luôn cố gắng theo sát từ tin tức đến kỹ thuật lập trình trên nền tảng EOS.
     
    Tags:

Chia sẻ trang này lên mạng xã hội:

Đang tải...