Skip to content

Commit

Permalink
make rex maturity settings as actions
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed May 8, 2024
1 parent 6d791ad commit 85ef99c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
20 changes: 8 additions & 12 deletions contracts/eosio.system/include/eosio.system/eosio.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@
// be set to 0.
#define CHANNEL_RAM_AND_NAMEBID_FEES_TO_REX 1

#ifdef MATURED_REX_SOLD_AND_BUY_REX_TO_SAVINGS
#undef MATURED_REX_SOLD_AND_BUY_REX_TO_SAVINGS
#endif
// MATURED_REX_SOLD_AND_BUY_REX_TO_SAVINGS macro determines whether matured REX is sold immediately and buying REX is moved immediately to REX savings.
// In order to enable this behavior, the macro must be set to 1.
// https://github.com/eosnetworkfoundation/eos-system-contracts/issues/134
// https://github.com/eosnetworkfoundation/eos-system-contracts/issues/135
#define MATURED_REX_SOLD_AND_BUY_REX_TO_SAVINGS 0

namespace eosiosystem {

using eosio::asset;
Expand Down Expand Up @@ -550,8 +541,8 @@ namespace eosiosystem {

struct [[eosio::table("rexmaturity"),eosio::contract("eosio.system")]] rex_maturity {
uint32_t num_of_maturity_buckets = 5;

EOSLIB_SERIALIZE( rex_maturity, (num_of_maturity_buckets) )
bool sell_matured_rex = false;
bool buy_rex_to_savings = false;
};

typedef eosio::singleton<"rexmaturity"_n, rex_maturity> rex_maturity_singleton;
Expand Down Expand Up @@ -1095,9 +1086,13 @@ namespace eosiosystem {
* Facilitates the modification of REX maturity buckets
*
* @param num_of_maturity_buckets - used to calculate maturity time of purchase REX tokens from end of the day UTC.
* @param sell_matured_rex - if true, matured REX is sold immediately.
* https://github.com/eosnetworkfoundation/eos-system-contracts/issues/134
* @param buy_rex_to_savings - if true, buying REX is moved immediately to REX savings.
* https://github.com/eosnetworkfoundation/eos-system-contracts/issues/135
*/
[[eosio::action]]
void rexmaturity(const uint32_t num_of_maturity_buckets);
void rexmaturity(const std::optional<uint32_t> num_of_maturity_buckets, const std::optional<bool> sell_matured_rex, const std::optional<bool> buy_rex_to_savings );

/**
* Undelegate bandwidth action, decreases the total tokens delegated by `from` to `receiver` and/or
Expand Down Expand Up @@ -1592,6 +1587,7 @@ namespace eosiosystem {
asset add_to_rex_pool( const asset& payment );
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 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
66 changes: 51 additions & 15 deletions contracts/eosio.system/src/rex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ namespace eosiosystem {
using eosio::token;
using eosio::seconds;

void system_contract::rexmaturity(const uint32_t num_of_maturity_buckets)
void system_contract::rexmaturity(const std::optional<uint32_t> num_of_maturity_buckets, const std::optional<bool> sell_matured_rex, const std::optional<bool> buy_rex_to_savings )
{
require_auth(get_self());

check(num_of_maturity_buckets > 0, "num_of_maturity_buckets must be positive");
check(num_of_maturity_buckets <= 30, "num_of_maturity_buckets must be less than or equal to 30");

auto state = _rexmaturity.get_or_default();
if ( _rexmaturity.exists() ) check(state.num_of_maturity_buckets != num_of_maturity_buckets, "num_of_maturity_buckets is the same as the current value");

state.num_of_maturity_buckets = num_of_maturity_buckets;
check(*num_of_maturity_buckets > 0, "num_of_maturity_buckets must be positive");
check(*num_of_maturity_buckets <= 30, "num_of_maturity_buckets must be less than or equal to 30");
if ( _rexmaturity.exists() && num_of_maturity_buckets ) check(state.num_of_maturity_buckets != *num_of_maturity_buckets, "num_of_maturity_buckets is the same as the current value");

if ( num_of_maturity_buckets ) state.num_of_maturity_buckets = *num_of_maturity_buckets;
if ( sell_matured_rex ) state.sell_matured_rex = *sell_matured_rex;
if ( buy_rex_to_savings ) state.buy_rex_to_savings = *buy_rex_to_savings;
_rexmaturity.set(state, get_self());
}

Expand Down Expand Up @@ -63,9 +65,18 @@ namespace eosiosystem {
runrex(2);
update_rex_account( from, asset( 0, core_symbol() ), delta_rex_stake );

#if MATURED_REX_SOLD_AND_BUY_REX_TO_SAVINGS
// 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 );
#endif
}

// sell any matured REX
// https://github.com/eosnetworkfoundation/eos-system-contracts/issues/134
if ( rex_maturity_state.sell_matured_rex ) {
process_sell_matured_rex( from );
}

// dummy action added so that amount of REX tokens purchased shows up in action trace
rex_results::buyresult_action buyrex_act( rex_account, std::vector<eosio::permission_level>{ } );
Expand Down Expand Up @@ -107,9 +118,18 @@ namespace eosiosystem {
runrex(2);
update_rex_account( owner, asset( 0, core_symbol() ), rex_stake_delta - payment, true );

#if MATURED_REX_SOLD_AND_BUY_REX_TO_SAVINGS
// 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 );
#endif
}

// sell any matured REX
// https://github.com/eosnetworkfoundation/eos-system-contracts/issues/134
if ( rex_maturity_state.sell_matured_rex ) {
process_sell_matured_rex( owner );
}

// dummy action added so that amount of REX tokens purchased shows up in action trace
rex_results::buyresult_action buyrex_act( rex_account, std::vector<eosio::permission_level>{ } );
Expand All @@ -120,6 +140,13 @@ namespace eosiosystem {
{
require_auth( from );
sell_rex( from, rex );

// sell any remaining matured REX
// https://github.com/eosnetworkfoundation/eos-system-contracts/issues/134
const auto rex_maturity_state = _rexmaturity.get_or_default();
if ( rex_maturity_state.sell_matured_rex ) {
process_sell_matured_rex( from );
}
}

void system_contract::sell_rex( const name& from, const asset& rex )
Expand Down Expand Up @@ -995,14 +1022,23 @@ namespace eosiosystem {
rb.matured_rex += rb.rex_maturities.front().second;
rb.rex_maturities.erase(rb.rex_maturities.begin());
}
#if MATURED_REX_SOLD_AND_BUY_REX_TO_SAVINGS
if ( rb.matured_rex > 0 ) {
sell_rex(rb.owner, asset(rb.matured_rex, rex_symbol));
}
#endif
});
}

/**
* @brief Sells matured REX tokens
* https://github.com/eosnetworkfoundation/eos-system-contracts/issues/134
*
* @param owner - owner account name
*/
void system_contract::process_sell_matured_rex( const name owner )
{
const auto itr = _rexbalance.find( owner.value );
if ( itr->matured_rex > 0 ) {
sell_rex(owner, asset(itr->matured_rex, rex_symbol));
}
}

/**
* @brief Consolidates REX maturity buckets into one
*
Expand Down

0 comments on commit 85ef99c

Please sign in to comment.