Permalink
Switch branches/tags
1771-QA 1972-QA 2032 2153-QA 4064 4254-snapshots 4971-enable-stale-production-fix 4986-alternative-fix-2 5674-test-of-free-port 5674-under-min-avail DAWN-551-Contract-Debugging-Merge DAWN-551-Contract-Debugging april-26-workshop ben-demo-docs bft-dpos binaryen-recursion bios-boot-tutorial-block-age2 bitshares-fc-variant-fixes block_summaries bnet2 build-containers-on-commit buildkite-mojave cleos-R cleos-keos cleos-pretty-get-account-cleos-keos cleos-pretty-get-account cleos_warn_on_nonwasm cmake_boost_dep_cleanup constitution contextfree contract-refactor contracts-use-new-dbapi dan-slim dan-slim2 dan2 dawn-2.x dawn-617-genesis-claim dawn-690-gh#1844-enable-tests deferred-transactions deferred_transaction_fix develop dhaneshvb-malicious-contract diffie-hellman-key-exchange-545 docker-eosiocpp-fix docker-mongodb docs-112318 docs-pull-3991 doxygen-cpp-sdk ecc-r1 eos-noon-network eos-noon eos-repo-contract-refactor eos2 eosbios-gcc-compatibility eosio.system exchange2 feature/backport-docker-eosiocpp-fix feature/binary_release feature/bump-version-to-1.1.6 feature/bump-version-to-1.2.1 feature/bump-version-to-1.2.4 feature/bump-version-to-1.5.0-rc2 feature/logging-changes-for-testing feature/logging-changes feature/port-6222 feature/revert-6222 feature/1.0.8-security-omnibus feature/5057-subjectively-fail-ambiguous-dtrx-case final_keosd_unix_socket_changes fix_unused_lambda_captures fix/wasm_call_depth_limit fork-db-refactor gh#3612-bnet history-plugin-fix identity issue1760 issue2898 issue3187 jit-schedule-512 json-forever-loop-fix june-28-workshop keosd_daemon long-running-test-branch master merge/1.5.0-to-develop metric metric2 mongo-filter mongo-shard moxygen multisig native-contract-debugging network-buffer networkv2 new-checktime-injection new-ci-image-testing p2p-always-send p2p-connection-refactor p2p-issue-3017-fix packed-txn paul port-1.1.4 producer-txns read-mode-irreversible regularize-replay-logging release/1.0.x release/1.1.x release/1.2.x release/1.3.x release/1.4.x release/1.5.x remove-docker-nodeos-wallet-plugin-settings replay-irreversible-mode sdk sigabrt-net-plugin sim skip_skip_trx_checks slim snapshot-plugin staging superhero-01-25 system-contract-deferred-unstaking system-contract-upgrade system-test-fix-draft system systemcontract taokayan-abiarray token-refactor trivial_wallet_dir_lock txn_gen_modified txn_test_plugin_wasm_mt update-block-num vote-max-transaction-delay-2 vote-max-transaction-delay wasm32
Nothing to show
Find file Copy path
121 lines (94 sloc) 3.7 KB
/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include "eosio.token.hpp"
namespace eosio {
void token::create( account_name issuer,
asset maximum_supply )
{
require_auth( _self );
auto sym = maximum_supply.symbol;
eosio_assert( sym.is_valid(), "invalid symbol name" );
eosio_assert( maximum_supply.is_valid(), "invalid supply");
eosio_assert( maximum_supply.amount > 0, "max-supply must be positive");
stats statstable( _self, sym.name() );
auto existing = statstable.find( sym.name() );
eosio_assert( existing == statstable.end(), "token with symbol already exists" );
statstable.emplace( _self, [&]( auto& s ) {
s.supply.symbol = maximum_supply.symbol;
s.max_supply = maximum_supply;
s.issuer = issuer;
});
}
void token::issue( account_name to, asset quantity, string memo )
{
auto sym = quantity.symbol;
eosio_assert( sym.is_valid(), "invalid symbol name" );
eosio_assert( memo.size() <= 256, "memo has more than 256 bytes" );
auto sym_name = sym.name();
stats statstable( _self, sym_name );
auto existing = statstable.find( sym_name );
eosio_assert( existing != statstable.end(), "token with symbol does not exist, create token before issue" );
const auto& st = *existing;
require_auth( st.issuer );
eosio_assert( quantity.is_valid(), "invalid quantity" );
eosio_assert( quantity.amount > 0, "must issue positive quantity" );
eosio_assert( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );
eosio_assert( quantity.amount <= st.max_supply.amount - st.supply.amount, "quantity exceeds available supply");
statstable.modify( st, 0, [&]( auto& s ) {
s.supply += quantity;
});
add_balance( st.issuer, quantity, st.issuer );
if( to != st.issuer ) {
SEND_INLINE_ACTION( *this, transfer, {st.issuer,N(active)}, {st.issuer, to, quantity, memo} );
}
}
void token::transfer( account_name from,
account_name to,
asset quantity,
string memo )
{
eosio_assert( from != to, "cannot transfer to self" );
require_auth( from );
eosio_assert( is_account( to ), "to account does not exist");
auto sym = quantity.symbol.name();
stats statstable( _self, sym );
const auto& st = statstable.get( sym );
require_recipient( from );
require_recipient( to );
eosio_assert( quantity.is_valid(), "invalid quantity" );
eosio_assert( quantity.amount > 0, "must transfer positive quantity" );
eosio_assert( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );
eosio_assert( memo.size() <= 256, "memo has more than 256 bytes" );
sub_balance( from, quantity );
add_balance( to, quantity, from );
}
void token::sub_balance( account_name owner, asset value ) {
accounts from_acnts( _self, owner );
const auto& from = from_acnts.get( value.symbol.name(), "no balance object found" );
eosio_assert( from.balance.amount >= value.amount, "overdrawn balance" );
if( from.balance.amount == value.amount ) {
from_acnts.erase( from );
} else {
from_acnts.modify( from, owner, [&]( auto& a ) {
a.balance -= value;
});
}
}
void token::add_balance( account_name owner, asset value, account_name ram_payer )
{
accounts to_acnts( _self, owner );
auto to = to_acnts.find( value.symbol.name() );
if( to == to_acnts.end() ) {
to_acnts.emplace( ram_payer, [&]( auto& a ){
a.balance = value;
});
} else {
to_acnts.modify( to, 0, [&]( auto& a ) {
a.balance += value;
});
}
}
} /// namespace eosio
EOSIO_ABI( eosio::token, (create)(issue)(transfer) )