Daniel Larimer introduces the new EOS smart contract architecture on his blog (the EOS team is just too fast to keep up). He gave the simplest smart contract code for a new currency, only 49 lines to complete the development of a new currency, a new “IIO” was born. Let’s do it step by step.

First, implement private membership, and set up an account structure that holds the accounts and balances of all the people who hold our tokens.

   private:
      / / the account structure
      struct account {
         / / EOS account name
         account_name owner;
         / / the balance
         uint64_t     balance;
         / / the primary key
         uint64_t primary_key()const { return owner; }

Copy the code

The next step is to take advantage of the multi-index list in the Boost library and put the above declared structure into a list for easy query and modification.

      eosio::multi_index<N(accounts), account> _accounts;
Copy the code

Next, we implement the add_balance() function, a private function whose purpose is to add a specific token to a specific EOS account.

      void add_balance( account_name payer, account_name to, uint64_t q ) {
         // Query the list to see if the user to collect coins is already in the list.
         auto toitr = _accounts.find( to );
         // If it is not in the list, it indicates that the user has never held this coin, and the user should be added to the list
         if( toitr == _accounts.end() ) {
            // Add a user
           _accounts.emplace( payer, [&]( auto& a ) {
              a.owner = to;
              // Since there is no such coin before, the balance under the user name is the amount to receive
              a.balance = q;
           });
           // If the user is in the list, it indicates that the user has already held or held the coin
         } else {
           _accounts.modify( toitr, 0, [and] (auto& a ) {
               // Directly increase the balance by the amount to be transferred
              a.balance += q;
              // Determine whether the user balance overflows (the balance increases by Q, and then the amount should be greater than q)
              eosio_assert( a.balance >= q, "overflow detected"); }); }}Copy the code

After that, it’s time to implement the public methods, starting with the constructor, and don’t forget to initialize the _accounts list.

  public:
      simpletoken( account_name self )
      :contract(self),_accounts( _self, _self){}
Copy the code

Implement a public transfer function to transfer tokens from one account to another.

      void transfer( account_name from, account_name to, uint64_t quantity ) {
         // Obtain permissions from the payer
         require_auth( from );
         // Search the originator account from the list
         const auto& fromacnt = _accounts.get( from );
         // Verify the payer's balance, whether it is overdrawn
         eosio_assert( fromacnt.balance >= quantity, "overdrawn balance" );
         // Subtract tokens from the payer
         _accounts.modify( fromacnt, from, [&]( auto& a ){ a.balance -= quantity; });// The receiver adds tokens (previously implemented private function)
         add_balance( from, to, quantity );
      }
Copy the code

Okay, did you think you were done? There is also the most important issue function, where else “print money”?

      void issue( account_name to, uint64_t quantity ) {
         // Get the contract owner's permission
         require_auth( _self );
         // Print money directly
         add_balance( _self, to, quantity );

Copy the code

As a final step, we provide our transfer and issue function interfaces to EOS, which can be quickly implemented with a macro.

EOSIO_ABI( simpletoken, (transfer)(issue) )
Copy the code

What about this macro? If you look at the definition of this macro in dispacher.hpp, it actually implements apply for the developer, allowing the developer to focus on the business logic.

#define EOSIO_ABI( TYPE, MEMBERS ) \
extern "C" { \
   void apply( uint64_t receiver, uint64_t code, uint64_t action ) { \
      auto self = receiver; \
      if( code == self ) { \ TYPE thiscontract( self ); \ switch( action ) { \ EOSIO_API( TYPE, MEMBERS ) \ } \ eosio_exit(0); \} \} \Copy the code

We’re done. Let’s look at all the code. Is it 49 lines? However, EOS said that there would be a system of standard tokens in the future, even the above specific logic we do not need to achieve, but this code for the system to learn THE EOS smart contract architecture is still very meaningful.

#include <eosiolib/eosio.hpp>

class simpletoken : public eosio::contract {
   public:
      simpletoken( account_name self )
      :contract(self),_accounts( _self, _self){}

      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 ); }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 { returnowner; }}; 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, [and] (auto& a ) {
              a.balance += q;
              eosio_assert( a.balance >= q, "overflow detected"); }); }}}; EOSIO_ABI( simpletoken, (transfer)(issue) )Copy the code

Related articles and videos recommended

[Xu Xiaodi] Case Analysis of EOS Smart Contract (1)

Yuan Yuan Institute brings together a large number of blockchain masters to create high-quality blockchain technology courses. There are high quality free public courses on all major platforms for a long time, welcome to sign up and watch. Open course address: ke.qq.com/course/3451…