currency.hpp 9.0 KB
Newer Older
1
#pragma once
D
Daniel Larimer 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#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:
16
         currency( account_name contract )
17
         :_contract(contract)
D
Daniel Larimer 已提交
18 19 20 21 22
         { }

         struct create {
            account_name           issuer;
            asset                  maximum_supply;
23 24 25 26
           // 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 已提交
27

28 29
            /*(issuer_agreement_hash)*/
            EOSLIB_SERIALIZE( create, (issuer)(maximum_supply)(issuer_can_freeze)(issuer_can_recall)(issuer_can_whitelist) )
D
Daniel Larimer 已提交
30 31
         };

32
         struct transfer
D
Daniel Larimer 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
         {
            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 {
51
            uint64_t primary_key()const { return 0; }
D
Daniel Larimer 已提交
52 53 54 55 56 57 58 59 60 61

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

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

62
            uint64_t primary_key()const { return balance.symbol.name(); }
D
Daniel Larimer 已提交
63 64 65 66 67 68 69 70

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

         struct currency_stats {
            asset          supply;
            asset          max_supply;
            account_name   issuer;
71 72 73 74 75
            bool           can_freeze         = true;
            bool           can_recall         = true;
            bool           can_whitelist      = true;
            bool           is_frozen          = false;
            bool           enforce_whitelist  = false;
D
Daniel Larimer 已提交
76

77
            uint64_t primary_key()const { return supply.symbol.name(); }
D
Daniel Larimer 已提交
78

79
            EOSLIB_SERIALIZE( currency_stats, (supply)(max_supply)(issuer)(can_freeze)(can_recall)(can_whitelist)(is_frozen)(enforce_whitelist) )
D
Daniel Larimer 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
         };

         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;
         }

96 97
         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 已提交
98 99 100
            act.send();
         }

101 102
         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 已提交
103 104 105
            act.send();
         }

D
Daniel Larimer 已提交
106

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

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

D
Daniel Larimer 已提交
128
          /**
129
           * This is factored out so it can be used as a building block
D
Daniel Larimer 已提交
130 131
           */
          void create_currency( const create& c ) {
132 133
            auto sym = c.maximum_supply.symbol;
            eosio_assert( sym.is_valid(), "invalid symbol name" );
D
Daniel Larimer 已提交
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 161

             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 已提交
162

D
Daniel Larimer 已提交
163 164 165
          void on( const create& c ) {
             require_auth( c.issuer );
             create_currency( c );
D
Daniel Larimer 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

             /*
             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 );
181 182
             eosio_assert( i.quantity.is_valid(), "invalid quantity" );
             eosio_assert( i.quantity.amount > 0, "must issue positive quantity" );
D
Daniel Larimer 已提交
183 184 185 186 187 188 189 190

             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 )
191
             {
D
Daniel Larimer 已提交
192
                inline_transfer( st.issuer, i.to, i.quantity, i.memo );
193
             }
D
Daniel Larimer 已提交
194 195 196
          }

          void on( const transfer& t ) {
B
Bucky Kittinger 已提交
197
             require_auth(t.from);
D
Daniel Larimer 已提交
198 199 200 201 202 203
             auto sym = t.quantity.symbol.name();
             stats statstable( _contract, sym );
             const auto& st = statstable.get( sym );

             require_recipient( t.to );

204 205
             eosio_assert( t.quantity.is_valid(), "invalid quantity" );
             eosio_assert( t.quantity.amount > 0, "must transfer positive quantity" );
D
Daniel Larimer 已提交
206 207 208 209 210
             sub_balance( t.from, t.quantity, st );
             add_balance( t.to, t.quantity, st, t.from );
          }


211 212 213 214
      private:
          void sub_balance( account_name owner, asset value, const currency_stats& st ) {
             accounts from_acnts( _contract, owner );

215
             const auto& from = from_acnts.get( value.symbol.name() );
216 217 218 219
             eosio_assert( from.balance.amount >= value.amount, "overdrawn balance" );

             if( has_auth( owner ) ) {
                eosio_assert( !st.can_freeze || !from.frozen, "account is frozen by issuer" );
220 221
                eosio_assert( !st.can_freeze || !st.is_frozen, "all transfers are frozen by issuer" );
                eosio_assert( !st.enforce_whitelist || from.whitelist, "account is not white listed" );
222 223 224 225 226 227 228 229 230 231 232 233 234 235
             } 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 );
236
             auto to = to_acnts.find( value.symbol.name() );
237
             if( to == to_acnts.end() ) {
238
                eosio_assert( !st.enforce_whitelist, "can only transfer to white listed accounts" );
239 240 241 242
                to_acnts.emplace( ram_payer, [&]( auto& a ){
                  a.balance = value;
                });
             } else {
243
                eosio_assert( !st.enforce_whitelist || to->whitelist, "receiver requires whitelist by issuer" );
244 245 246 247 248 249
                to_acnts.modify( to, 0, [&]( auto& a ) {
                  a.balance.amount += value.amount;
                });
             }
          }

D
Daniel Larimer 已提交
250 251 252 253 254
      private:
         account_name _contract;
   };

}