Skip to content

Instantly share code, notes, and snippets.

@bytemaster
Last active April 20, 2019 12:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bytemaster/b8bafd3f1d96c14995d070d40356e044 to your computer and use it in GitHub Desktop.
Save bytemaster/b8bafd3f1d96c14995d070d40356e044 to your computer and use it in GitHub Desktop.
EOSIO Simple Token Contract
#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) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment