Skip to content

Commit

Permalink
Move to savings when buying REX & sell REX when matured
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed May 3, 2024
1 parent 67c2429 commit 6d791ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 10 additions & 0 deletions contracts/eosio.system/include/eosio.system/eosio.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
// 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 @@ -1588,6 +1597,7 @@ namespace eosiosystem {
int64_t read_rex_savings( const rex_balance_table::const_iterator& bitr );
void put_rex_savings( const rex_balance_table::const_iterator& bitr, int64_t rex );
void update_rex_stake( const name& voter );
void sell_rex( const name& from, const asset& rex );

void add_loan_to_rex_pool( const asset& payment, int64_t rented_tokens, bool new_loan );
void remove_loan_from_rex_pool( const rex_loan& loan );
Expand Down
19 changes: 18 additions & 1 deletion contracts/eosio.system/src/rex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ namespace eosiosystem {
const asset delta_rex_stake = add_to_rex_balance( from, amount, rex_received );
runrex(2);
update_rex_account( from, asset( 0, core_symbol() ), delta_rex_stake );
mvtosavings( from, rex_received );

#if MATURED_REX_SOLD_AND_BUY_REX_TO_SAVINGS
mvtosavings( from, rex_received );
#endif

// 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 @@ -103,6 +106,11 @@ namespace eosiosystem {
auto rex_stake_delta = add_to_rex_balance( owner, payment, rex_received );
runrex(2);
update_rex_account( owner, asset( 0, core_symbol() ), rex_stake_delta - payment, true );

#if MATURED_REX_SOLD_AND_BUY_REX_TO_SAVINGS
mvtosavings( owner, rex_received );
#endif

// 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>{ } );
buyrex_act.send( rex_received );
Expand All @@ -111,7 +119,11 @@ namespace eosiosystem {
void system_contract::sellrex( const name& from, const asset& rex )
{
require_auth( from );
sell_rex( from, rex );
}

void system_contract::sell_rex( const name& from, const asset& rex )
{
runrex(2);

auto bitr = _rexbalance.require_find( from.value, "user must first buyrex" );
Expand Down Expand Up @@ -983,6 +995,11 @@ 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
});
}

Expand Down

0 comments on commit 6d791ad

Please sign in to comment.