Skip to content

Instantly share code, notes, and snippets.

@nsjames
Created October 28, 2020 20:56
Show Gist options
  • Save nsjames/1d0407c29010c384ec59eed276e0a162 to your computer and use it in GitHub Desktop.
Save nsjames/1d0407c29010c384ec59eed276e0a162 to your computer and use it in GitHub Desktop.
#include <eosio.token.hpp>
/*******************************************/
/* USAGE NOTICE */
/* --------------------------------------- */
/* This file was generated using a */
/* proprietary blockchain contract flow */
/* based programming (FBP) application. */
/* --------------------------------------- */
/* Unauthorized use is subject to legal */
/* ramifications. For more information */
/* please visit: ... */
/* --------------------------------------- */
/* Copyright ... 2019 */
/*******************************************/
ACTION eosio.token::create(name& issuer, asset& maximum_supply){
require_auth(get_self());
symbol sym = maximum_supply.symbol;
check((sym.is_valid() == true), "invalid symbol name");
check((maximum_supply.is_valid() == true), "invalid supply");
check((maximum_supply.amount > 0), "maximum_supply must be positive");
stat idD3uow0JzwFGojSmYibohuN(get_self(), sym.code().raw());
bool existing = idD3uow0JzwFGojSmYibohuN.find( sym.code().raw() ) != idD3uow0JzwFGojSmYibohuN.end();
check((existing == false), "token with symbol already exists");
stat id7bFLK10BC5x3qCpIOkGb7L(get_self(), sym.code().raw());
id7bFLK10BC5x3qCpIOkGb7L.emplace(get_self(), [&]( auto& row ){
row.supply.symbol = maximum_supply.symbol;
row.max_supply = maximum_supply;
row.issuer = issuer;
});
}
ACTION eosio.token::issue(name& to, asset& quantity, string& memo){
symbol sym = quantity.symbol;
check((sym.is_valid() == true), "invalid symbol name");
check((memo.size() <= 256), "memo has more than 256 bytes");
stat idz6lZEElmaQQLmK9zxTF4IS(get_self(), sym.code().raw());
bool existing = idz6lZEElmaQQLmK9zxTF4IS.find( sym.code().raw() ) != idz6lZEElmaQQLmK9zxTF4IS.end();
check((existing == true), "token with symbol does not exist, create token before issue");
stat idUpObzrvCFEHxMvSJ0jk5kc(get_self(), sym.code().raw());
const auto& token = idUpObzrvCFEHxMvSJ0jk5kc.get( sym.code().raw() );
check((to == token.issuer), "tokens can only be issued to issuer account");
require_auth(to);
check((quantity.is_valid() == true), "invalid quantity");
check((quantity.amount > 0), "must issue positive quantity");
check((quantity.symbol == token.supply.symbol), "symbol precision mismatch");
check((quantity.amount <= token.max_supply.amount - token.supply.amount), "quantity exceeds available supply");
stat idAbhW8NuYFNK7I0bxNL06SS(get_self(), get_self());
idAbhW8NuYFNK7I0bxNL06SS.modify(get_self(), get_self(), [&]( auto& row ){
row.supply += quantity;
});
add_balance(token.issuer, quantity, token.issuer)
}
ACTION eosio.token::retire(asset& quantity, string& memo){
symbol sym = quantity.symbol;
check((sym.is_valid() == true), "invalid symbol name");
check((memo.size() <= 256), "memo has more than 256 bytes");
stat idbrC61ODVq1hTyiHx5HB5QL(get_self(), sym.code().raw());
bool existing = idbrC61ODVq1hTyiHx5HB5QL.find( sym.code().raw() ) != idbrC61ODVq1hTyiHx5HB5QL.end();
check((existing == true), "token with symbol does not exist");
stat id4nWRaGp27GhakzthozckvW(get_self(), sym.code().raw());
const auto& token = id4nWRaGp27GhakzthozckvW.get( sym.code().raw() );
require_auth(token.issuer);
check((quantity.is_valid() == true), "invalid quantity");
check((quantity.amount > 0), "must retire positive quantity");
check((quantity.symbol == token.supply.symbol), "symbol precision mismatch");
stat idlnummAWsYcnFSkQqnkRU5l(get_self(), get_self());
idlnummAWsYcnFSkQqnkRU5l.modify(get_self(), get_self(), [&]( auto& row ){
row.supply -= quantity;
});
sub_balance(token.issuer, quantity)
}
ACTION eosio.token::transfer(name& from, name& to, asset& quantity, string& memo){
check((from != to), "cannot transfer to self");
require_auth(from);
check((is_account(to) == true), "to account does not exist");
symbol sym = quantity.symbol;
stat idphuP2AGUQhz603hwDyWQRj(get_self(), sym.code().raw());
bool existing = idphuP2AGUQhz603hwDyWQRj.find( sym.code().raw() ) != idphuP2AGUQhz603hwDyWQRj.end();
check((existing == true), "token with symbol does not exist");
stat idPWOjzOyTTnnHHTogTce8dX(get_self(), sym.code().raw());
const auto& token = idPWOjzOyTTnnHHTogTce8dX.get( sym.code().raw() );
check((quantity.is_valid() == true), "invalid quantity");
check((quantity.amount > 0), "must transfer positive quantity");
check((quantity.symbol == token.supply.symbol), "symbol precision mismatch");
check((memo.size() <= 256), "memo has more than 256 bytes");
name payer = (has_auth(to) == true) ? to : from;
sub_balance(from, quantity)
add_balance(to, quantity, payer)
}
void eosio.token::add_balance(name& owner, asset& value , name& ram_payer ){
accounts idI5y5vuI7iCw6iX3VDMiTcW(get_self(), get_self());
bool existing = idI5y5vuI7iCw6iX3VDMiTcW.find( value.symbol.code().raw() ) != idI5y5vuI7iCw6iX3VDMiTcW.end();
if((existing == true)){
accounts id7KIdjLdLguPEAUTWekfAAm(get_self(), get_self());
const auto& to = id7KIdjLdLguPEAUTWekfAAm.get( value.symbol.code().raw() );
accounts id22mPb2bTYltOWCzBpI4ZI5(get_self(), get_self());
id22mPb2bTYltOWCzBpI4ZI5.modify(get_self(), get_self(), [&]( auto& row ){
row.balance += value;
});
} else {
accounts idLrXPanbjFjhHa37LpcERs8(get_self(), owner.value);
idLrXPanbjFjhHa37LpcERs8.emplace(get_self(), [&]( auto& row ){
row.balance = value;
});
}
}
void eosio.token::sub_balance(name& owner, asset& value ){
accounts idFgdiAVAqLFUPkd4IMsySee(get_self(), get_self());
const auto& from = idFgdiAVAqLFUPkd4IMsySee.get( value.symbol.code().raw() );
check((from.balance.amount >= value.amount), "overdrawn balance");
accounts id4TvgJtDa7Exw2GyMT7IoQg(get_self(), get_self());
id4TvgJtDa7Exw2GyMT7IoQg.modify(get_self(), get_self(), [&]( auto& row ){
row.balance -= value;
});
}
extern "C" {
void apply(uint64_t receiver, uint64_t code, uint64_t action) {
auto self = receiver;
if( code == self ) switch(action) {
EOSIO_DISPATCH_HELPER( eosio.token, (create),(issue),(retire),(transfer) )
}
else {
}
}
};
#include <eosio/eosio.hpp>
using namespace eosio;
/*******************************************/
/* USAGE NOTICE */
/* --------------------------------------- */
/* This file was generated using a */
/* proprietary blockchain contract flow */
/* based programming (FBP) application. */
/* --------------------------------------- */
/* Unauthorized use is subject to legal */
/* ramifications. For more information */
/* please visit: ... */
/* --------------------------------------- */
/* Copyright ... 2019 */
/*******************************************/
CONTRACT eosio.token : public contract {
public:
using contract::contract;
eosio.token(name receiver, name code, datastream<const char*> ds):contract(receiver, code, ds) {}
ACTION create(name& issuer, asset& maximum_supply);
ACTION issue(name& to, asset& quantity, string& memo);
ACTION retire(asset& quantity, string& memo);
ACTION transfer(name& from, name& to, asset& quantity, string& memo);
using create_action = action_wrapper<"create"_n, &eosio.token::create>;
using issue_action = action_wrapper<"issue"_n, &eosio.token::issue>;
using retire_action = action_wrapper<"retire"_n, &eosio.token::retire>;
using transfer_action = action_wrapper<"transfer"_n, &eosio.token::transfer>;
private:
/* MODELS */
struct [[eosio::table]] account {
asset balance;
symbol primary_key() const { return balance.symbol; }
}
struct [[eosio::table]] currency_stats {
asset supply;
asset max_supply;
name issuer;
symbol primary_key() const { return supply.symbol; }
}
/* DATABASES */
typedef eosio::multi_index<"accounts"_n, account
> TABLE_accounts;
typedef eosio::multi_index<"stat"_n, currency_stats
> TABLE_stat;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment