currency.hpp 8.7 KB
Newer Older
D
Daniel Larimer 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <eosiolib/eosio.hpp>
#include <eosiolib/asset.hpp>
#include <eosiolib/multi_index.hpp>

namespace eosio {
   using std::string;
   using std::array;

   /**
    *  This contract enables the creation, issuance, and transfering of many different tokens.
    *
    */
   class currency {
      public:
         currency( account_name contract = current_receiver() )
         :_contract(contract) 
         { }

         struct create {
            account_name           issuer;
            asset                  maximum_supply;
22 23 24 25
           // array<char,32>         issuer_agreement_hash;
            uint8_t                issuer_can_freeze     = true;
            uint8_t                issuer_can_recall     = true;
            uint8_t                issuer_can_whitelist  = true;
D
Daniel Larimer 已提交
26

27 28
            /*(issuer_agreement_hash)*/
            EOSLIB_SERIALIZE( create, (issuer)(maximum_supply)(issuer_can_freeze)(issuer_can_recall)(issuer_can_whitelist) )
D
Daniel Larimer 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
         };

         struct transfer  
         {
            account_name from;
            account_name to;
            asset        quantity;
            string       memo;

            EOSLIB_SERIALIZE( transfer, (from)(to)(quantity)(memo) )
         };

         struct issue {
            account_name to;
            asset        quantity;
            string       memo;

            EOSLIB_SERIALIZE( issue, (to)(quantity)(memo) )
         };

         struct fee_schedule {
            auto primary_key() const { return 0; }

            array<extended_asset,7> fee_per_length;
            EOSLIB_SERIALIZE( fee_schedule, (fee_per_length) )
         };

         struct account {
            asset    balance;
            bool     frozen    = false;
            bool     whitelist = true;

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

            EOSLIB_SERIALIZE( account, (balance)(frozen)(whitelist) )
         };

         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           is_whitelist  = false;

76
            uint64_t primary_key() const { return supply.symbol.name(); }
D
Daniel Larimer 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

            EOSLIB_SERIALIZE( currency_stats, (supply)(max_supply)(issuer)(can_freeze)(can_recall)(can_whitelist)(is_frozen)(is_whitelist) )
         };

         typedef eosio::multi_index<N(accounts), account> accounts;
         typedef eosio::multi_index<N(stat), currency_stats> stats;


         asset get_balance( account_name owner, symbol_name symbol )const {
            accounts t( _contract, owner );
            return t.get(symbol).balance;
         }

         asset get_supply( symbol_name symbol )const {
            accounts t( _contract, symbol );
            return t.get(symbol).balance;
         }

D
Daniel Larimer 已提交
95 96
         static void inline_transfer( account_name from, account_name to, extended_asset amount, string memo = string(), permission_name perm = N(active) ) {
            action act( permission_level( from, perm ), amount.contract, N(transfer), transfer{from,to,amount,memo} );
D
Daniel Larimer 已提交
97 98 99
            act.send();
         }

D
Daniel Larimer 已提交
100 101
         void inline_transfer( account_name from, account_name to, asset amount, string memo = string(), permission_name perm = N(active) ) {
            action act( permission_level( from, perm ), _contract, N(transfer), transfer{from,to,amount,memo} );
D
Daniel Larimer 已提交
102 103 104
            act.send();
         }

D
Daniel Larimer 已提交
105

D
Daniel Larimer 已提交
106 107 108 109 110 111
         bool apply( account_name contract, action_name act ) {
            if( contract != _contract ) 
               return false;

            switch( act ) {
               case N(issue):
D
Daniel Larimer 已提交
112
                  print( "issue\n");
113
                 on( unpack_action<issue>() );
D
Daniel Larimer 已提交
114 115
                 return true;
               case N(transfer):
D
Daniel Larimer 已提交
116
                  print( "transfer\n");
117 118 119
                 on( unpack_action<transfer>() );
                 return true;
               case N(create):
D
Daniel Larimer 已提交
120
                  print( "create\n");
121
                 on( unpack_action<create>() );
D
Daniel Larimer 已提交
122 123 124 125 126
                 return true;
            }
            return false;
         }

D
Daniel Larimer 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
          /**
           * This is factored out so it can be used as a building block 
           */
          void create_currency( const create& c ) {
             auto sym = c.maximum_supply.symbol;
          //   eosio_assert( sym.is_valid(), "invalid symbol name" );

             stats statstable( _contract, sym.name() );
             auto existing = statstable.find( sym.name() );
             eosio_assert( existing == statstable.end(), "token with symbol already exists" );

             statstable.emplace( c.issuer, [&]( auto& s ) {
                s.supply.symbol = c.maximum_supply.symbol;
                s.max_supply    = c.maximum_supply;
                s.issuer        = c.issuer;
                s.can_freeze    = c.issuer_can_freeze;
                s.can_recall    = c.issuer_can_recall;
                s.can_whitelist = c.issuer_can_whitelist;
             });
          }

          void issue_currency( const issue& i ) {
             auto sym = i.quantity.symbol.name();
             stats statstable( _contract, sym );
             const auto& st = statstable.get( sym );

             statstable.modify( st, 0, [&]( auto& s ) {
                s.supply.amount += i.quantity.amount;
                eosio_assert( s.supply.amount >= 0, "underflow" );
             });

             add_balance( st.issuer, i.quantity, st, st.issuer );
          }

D
Daniel Larimer 已提交
161 162 163 164 165
      private:
          void sub_balance( account_name owner, asset value, const currency_stats& st ) {
             accounts from_acnts( _contract, owner );

             const auto& from = from_acnts.get( value.symbol );
166
             eosio_assert( from.balance.amount >= value.amount, "overdrawn balance" );
D
Daniel Larimer 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

             if( has_auth( owner ) ) {
                eosio_assert( !st.can_freeze || !from.frozen, "account is frozen by issuer" );
                eosio_assert( !st.is_whitelist || from.whitelist, "account is not white listed" );
             } else if( has_auth( st.issuer ) ) {
                eosio_assert( st.can_recall, "issuer may not recall token" );
             } else {
                eosio_assert( false, "insufficient authority" );
             }

             from_acnts.modify( from, owner, [&]( auto& a ) {
                 a.balance.amount -= value.amount;
             });
          }

          void add_balance( account_name owner, asset value, const currency_stats& st, account_name ram_payer )
          {
             accounts to_acnts( _contract, owner );
             auto to    = to_acnts.find( value.symbol );
             if( to == to_acnts.end() ) {
                eosio_assert( !st.is_whitelist, "can only transfer to white listed accounts" );
                to_acnts.emplace( ram_payer, [&]( auto& a ){
                  a.balance = value;
                });
             } else {
                eosio_assert( !st.is_whitelist || to->whitelist, "receiver requires whitelist by issuer" );
                to_acnts.modify( to, 0, [&]( auto& a ) {
                  a.balance.amount += value.amount;
                });
             }
          }


D
Daniel Larimer 已提交
200 201 202
          void on( const create& c ) {
             require_auth( c.issuer );
             create_currency( c );
D
Daniel Larimer 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

             /*
             auto fee = get_fee_schedule()[c.maximum_supply.name_length()];
             if( fee.amount > 0 ) {
                inline_transfer( c.issuer, _contract, fee, "symbol registration fee" );
             }
             */
          }

          void on( const issue& i ) {
             auto sym = i.quantity.symbol.name();
             stats statstable( _contract, sym );
             const auto& st = statstable.get( sym );

             require_auth( st.issuer );
             eosio_assert( i.quantity.amount > 0, "cannot transfer negative quantity" );

             statstable.modify( st, 0, [&]( auto& s ) {
                s.supply.amount += i.quantity.amount;
             });

             add_balance( st.issuer, i.quantity, st, st.issuer );

             if( i.to != st.issuer )
227
             {
D
Daniel Larimer 已提交
228
                inline_transfer( st.issuer, i.to, i.quantity, i.memo );
229
             }
D
Daniel Larimer 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
          }

          void on( const transfer& t ) {
             auto sym = t.quantity.symbol.name();
             stats statstable( _contract, sym );
             const auto& st = statstable.get( sym );

             require_recipient( t.to );

             eosio_assert( t.quantity.amount > 0, "cannot transfer negative quantity" );
             sub_balance( t.from, t.quantity, st );
             add_balance( t.to, t.quantity, st, t.from );
          }


      private:
         account_name _contract;
   };

}