Skip to content

Commit

Permalink
Implement setmaxsupply
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Apr 8, 2024
1 parent 477b696 commit a451ca4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
9 changes: 9 additions & 0 deletions contracts/eosio.token/include/eosio.token/eosio.token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ namespace eosio {
[[eosio::action]]
void issuefixed( const name& to, const asset& supply, const string& memo );

/**
* Set the maximum supply of the token.
*
* @param issuer - the issuer account setting the maximum supply.
* @param maximum_supply - the maximum supply of the token.
*/
[[eosio::action]]
void setmaxsupply( const name& issuer, const asset& maximum_supply );

/**
* The opposite for create action, if all validations succeed,
* it debits the statstable.supply amount.
Expand Down
13 changes: 13 additions & 0 deletions contracts/eosio.token/ricardian/eosio.token.contracts.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ This action will not result any any tokens being issued into circulation.

RAM will deducted from {{$action.account}}’s resources to create the necessary records.

<h1 class="contract">setmaxsupply</h1>

---
spec_version: "0.2.0"
title: Set Max Supply
summary: 'Set max supply for token'
icon: @ICON_BASE_URL@/@TOKEN_ICON_URI@
---

{{issuer}} will be allowed to issue tokens into circulation, up to a maximum supply of {{maximum_supply}}.

This action will not result any any tokens being issued into circulation.

<h1 class="contract">issue</h1>

---
Expand Down
19 changes: 19 additions & 0 deletions contracts/eosio.token/src/eosio.token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ void token::issuefixed( const name& to, const asset& supply, const string& memo
issue( to, quantity, memo );
}

void token::setmaxsupply( const name& issuer, const asset& maximum_supply )
{
auto sym = maximum_supply.symbol;
check( maximum_supply.is_valid(), "invalid supply");
check( maximum_supply.amount > 0, "max-supply must be positive");

stats statstable( get_self(), sym.code().raw() );
auto & st = statstable.get( sym.code().raw(), "token supply does not exist" );
check( issuer == st.issuer, "only issuer can set token maximum supply" );
require_auth( st.issuer );

check( maximum_supply.symbol == st.supply.symbol, "symbol precision mismatch" );
check( maximum_supply.amount >= st.supply.amount, "max supply is less than available supply");

statstable.modify( st, same_payer, [&]( auto& s ) {
s.max_supply = maximum_supply;
});
}

void token::retire( const asset& quantity, const string& memo )
{
auto sym = quantity.symbol;
Expand Down
49 changes: 46 additions & 3 deletions tests/eosio.token_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,21 @@ class eosio_token_tester : public tester {
);
}

action_result issuefixed( account_name issuer, asset supply, string memo ) {
return push_action( issuer, "issuefixed"_n, mvo()
( "to", issuer)
action_result issuefixed( account_name to, asset supply, string memo ) {
return push_action( to, "issuefixed"_n, mvo()
( "to", to)
( "supply", supply)
( "memo", memo)
);
}

action_result setmaxsupply( account_name issuer, asset maximum_supply ) {
return push_action( issuer, "setmaxsupply"_n, mvo()
( "issuer", issuer)
( "maximum_supply", maximum_supply)
);
}

action_result retire( account_name issuer, asset quantity, string memo ) {
return push_action( issuer, "retire"_n, mvo()
( "quantity", quantity)
Expand Down Expand Up @@ -270,6 +277,42 @@ BOOST_FIXTURE_TEST_CASE( issuefixed_tests, eosio_token_tester ) try {

} FC_LOG_AND_RETHROW()

BOOST_FIXTURE_TEST_CASE( setmaxsupply_tests, eosio_token_tester ) try {

auto token = create( "alice"_n, asset::from_string("1000.000 TKN"));
produce_blocks(1);

issue( "alice"_n, asset::from_string("1000.000 TKN"), "issue active supply" );

BOOST_REQUIRE_EQUAL( wasm_assert_msg( "quantity exceeds available supply" ),
issue( "alice"_n, asset::from_string("1000.000 TKN"), "quantity exceeds available supply" )
);

setmaxsupply( "alice"_n, asset::from_string("2000.000 TKN") );

issue( "alice"_n, asset::from_string("1000.000 TKN"), "issue active supply" );

auto stats = get_stats("3,TKN");
// REQUIRE_MATCHING_OBJECT( stats, mvo()
// ("supply", "2000.000 TKN")
// ("max_supply", "2000.000 TKN")
// ("issuer", "alice")
// );

BOOST_REQUIRE_EQUAL( wasm_assert_msg( "symbol precision mismatch" ),
setmaxsupply( "alice"_n, asset::from_string("3000 TKN") )
);

BOOST_REQUIRE_EQUAL( wasm_assert_msg( "only issuer can set token maximum supply" ),
setmaxsupply( "bob"_n, asset::from_string("1000.000 TKN") )
);

BOOST_REQUIRE_EQUAL( wasm_assert_msg( "max supply is less than available supply" ),
setmaxsupply( "alice"_n, asset::from_string("1000.000 TKN") )
);

} FC_LOG_AND_RETHROW()

BOOST_FIXTURE_TEST_CASE( retire_tests, eosio_token_tester ) try {

auto token = create( "alice"_n, asset::from_string("1000.000 TKN"));
Expand Down

0 comments on commit a451ca4

Please sign in to comment.