Skip to content

Commit

Permalink
update rex tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed May 11, 2024
1 parent 8028ff7 commit 981608a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,7 @@ namespace eosiosystem {
void add_to_rex_return_pool( const asset& fee );
void process_rex_maturities( const rex_balance_table::const_iterator& bitr );
void process_sell_matured_rex( const name owner );
void process_buy_rex_to_savings( const name owner, const asset rex );
void consolidate_rex_balance( const rex_balance_table::const_iterator& bitr,
const asset& rex_in_sell_order );
int64_t read_rex_savings( const rex_balance_table::const_iterator& bitr );
Expand Down
29 changes: 17 additions & 12 deletions contracts/eosio.system/src/rex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ namespace eosiosystem {
runrex(2);
update_rex_account( from, asset( 0, core_symbol() ), delta_rex_stake );

// buying REX immediately moves to REX savings
// https://github.com/eosnetworkfoundation/eos-system-contracts/issues/135
const auto rex_maturity_state = _rexmaturity.get_or_default();
if ( rex_maturity_state.buy_rex_to_savings ) {
mvtosavings( from, rex_received );
}
process_buy_rex_to_savings( from, rex_received );
process_sell_matured_rex( from );

// dummy action added so that amount of REX tokens purchased shows up in action trace
Expand Down Expand Up @@ -112,12 +107,7 @@ namespace eosiosystem {
runrex(2);
update_rex_account( owner, asset( 0, core_symbol() ), rex_stake_delta - payment, true );

// unstake to REX immediately moves to REX savings
// https://github.com/eosnetworkfoundation/eos-system-contracts/issues/135
const auto rex_maturity_state = _rexmaturity.get_or_default();
if ( rex_maturity_state.buy_rex_to_savings ) {
mvtosavings( owner, rex_received );
}
process_buy_rex_to_savings( owner, rex_received );
process_sell_matured_rex( owner );

// dummy action added so that amount of REX tokens purchased shows up in action trace
Expand Down Expand Up @@ -1025,6 +1015,21 @@ namespace eosiosystem {
}
}

/**
* @brief Move new REX tokens to savings
* https://github.com/eosnetworkfoundation/eos-system-contracts/issues/135
*
* @param owner - owner account name
* @param rex - amount of REX tokens to be moved to savings
*/
void system_contract::process_buy_rex_to_savings( const name owner, const asset rex )
{
const auto rex_maturity_state = _rexmaturity.get_or_default();
if ( rex_maturity_state.buy_rex_to_savings && rex.amount > 0 ) {
mvtosavings( owner, rex );
}
}

/**
* @brief Consolidates REX maturity buckets into one
*
Expand Down
49 changes: 42 additions & 7 deletions tests/eosio.system_rex_matured_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ BOOST_FIXTURE_TEST_CASE( buy_sell_matured_rex, eosio_system_tester ) try {
// https://github.com/eosnetworkfoundation/eos-system-contracts/issues/132
// https://github.com/eosnetworkfoundation/eos-system-contracts/issues/134
// https://github.com/eosnetworkfoundation/eos-system-contracts/issues/135
BOOST_REQUIRE_EQUAL( success(), rexmaturity( 21, true, true ) );

// setup accounts
const int64_t ratio = 10000;
const asset init_rent = core_sym::from_string("20000.0000");
const asset init_balance = core_sym::from_string("1000.0000");
const std::vector<account_name> accounts = { "alice"_n, "bob"_n };
account_name alice = accounts[0], bob = accounts[1];
const std::vector<account_name> accounts = { "alice"_n, "bob"_n, "charly"_n, "david"_n };
account_name alice = accounts[0], bob = accounts[1], charly = accounts[2], david = accounts[3];
setup_rex_accounts( accounts, init_balance );

// 1. set `num_of_maturity_buckets=21` to test increasing maturity time of buying REX tokens.
BOOST_REQUIRE_EQUAL( success(), rexmaturity( 21, false, false ) );
BOOST_REQUIRE_EQUAL( asset::from_string("25000.0000 REX"), get_buyrex_result( alice, core_sym::from_string("2.5000") ) );
BOOST_REQUIRE_EQUAL( success(), mvfrsavings( alice, asset::from_string("25000.0000 REX") ) );

produce_blocks(2);
produce_block(fc::days(5));
BOOST_REQUIRE_EQUAL( wasm_assert_msg("insufficient available rex"), sellrex( alice, asset::from_string("25000.0000 REX") ) );
Expand All @@ -48,12 +48,47 @@ BOOST_FIXTURE_TEST_CASE( buy_sell_matured_rex, eosio_system_tester ) try {
BOOST_REQUIRE_EQUAL( init_rent, rex_pool["total_rent"].as<asset>() );
BOOST_REQUIRE_EQUAL( get_rex_balance(alice), rex_pool["total_rex"].as<asset>() );

// set `buy_rex_to_savings=false` to test buying REX without moving it to REX savings
// 2. set `buy_rex_to_savings=false` to test buying REX without moving it to REX savings
BOOST_REQUIRE_EQUAL( success(), rexmaturity( 21, true, false ) );
BOOST_REQUIRE_EQUAL( asset::from_string("25000.0000 REX"), get_buyrex_result( bob, core_sym::from_string("2.5000") ) );
produce_blocks(2);
produce_block(fc::days(5));
BOOST_REQUIRE_EQUAL( wasm_assert_msg("insufficient available rex"), sellrex( bob, asset::from_string("25000.0000 REX") ) );
produce_block(fc::days(16));
BOOST_REQUIRE_EQUAL( core_sym::from_string("2.5000"), get_rex_vote_stake( bob ) );
BOOST_REQUIRE_EQUAL( asset::from_string("10000.0000 REX"), get_buyrex_result( bob, core_sym::from_string("1.0000") ) ); // will also triggers sell matured REX
BOOST_REQUIRE_EQUAL( core_sym::from_string("1.0000"), get_rex_vote_stake( bob ) );

// 3. set `sell_matured_rex=false` to test selling matured REX
BOOST_REQUIRE_EQUAL( success(), rexmaturity( 21, false, true ) );
BOOST_REQUIRE_EQUAL( asset::from_string("25000.0000 REX"), get_buyrex_result( charly, core_sym::from_string("2.5000") ) ); // when buying REX, it will automatically be moved to REX savings
BOOST_REQUIRE_EQUAL( success(), mvfrsavings( charly, asset::from_string("25000.0000 REX") ) ); // move REX from savings to initiate matured REX unstaking process
produce_blocks(2);
produce_block(fc::days(5));
BOOST_REQUIRE_EQUAL( wasm_assert_msg("insufficient available rex"), sellrex( charly, asset::from_string("25000.0000 REX") ) );
produce_block(fc::days(16));
BOOST_REQUIRE_EQUAL( success(), updaterex( charly ) ); // triggers sell matured REX (any REX action causes sell matured REX)
BOOST_REQUIRE_EQUAL( core_sym::from_string("2.5000"), get_sellrex_result( charly, asset::from_string("25000.0000 REX") ) );
BOOST_REQUIRE_EQUAL( core_sym::from_string("0.0000"), get_rex_vote_stake( charly ) );
BOOST_REQUIRE_EQUAL( init_balance, get_rex_fund( charly ) );

// 4. legacy holders with matured REX
BOOST_REQUIRE_EQUAL( success(), rexmaturity( 5, false, false ) );
BOOST_REQUIRE_EQUAL( asset::from_string("25000.0000 REX"), get_buyrex_result( david, core_sym::from_string("2.5000") ) ); // legacy 5 days maturity
produce_blocks(2);
produce_block(fc::days(5));
BOOST_REQUIRE_EQUAL( success(), rexmaturity( 21, true, true ) );
BOOST_REQUIRE_EQUAL( asset::from_string("10000.0000 REX"), get_buyrex_result( david, core_sym::from_string("1.0000") ) ); // new 21 days maturity & triggers sell matured REX
BOOST_REQUIRE_EQUAL( success(), mvfrsavings( david, asset::from_string("10000.0000 REX") ) ); // must move REX from savings to initiate matured REX unstaking process
BOOST_REQUIRE_EQUAL( wasm_assert_msg("insufficient available rex"), sellrex( david, asset::from_string("25000.0000 REX") ) ); // already sold when previously buying REX
produce_blocks(2);
produce_block(fc::days(5));
BOOST_REQUIRE_EQUAL( wasm_assert_msg("insufficient available rex"), sellrex( david, asset::from_string("10000.0000 REX") ) ); // 21 day REX not matured yet
produce_blocks(2);
produce_block(fc::days(21));
BOOST_REQUIRE_EQUAL( core_sym::from_string("2.5000"), get_sellrex_result( bob, asset::from_string("25000.0000 REX") ) );
BOOST_REQUIRE_EQUAL( core_sym::from_string("1.0000"), get_sellrex_result( david, asset::from_string("10000.0000 REX") ) );
BOOST_REQUIRE_EQUAL( core_sym::from_string("0.0000"), get_rex_vote_stake( david ) );
BOOST_REQUIRE_EQUAL( init_balance, get_rex_fund( david ) );

} FC_LOG_AND_RETHROW()

Expand Down

0 comments on commit 981608a

Please sign in to comment.