eosnetworkfoundation / eos-system-contracts

Other
28 stars 20 forks source link

REX: matured positions automatically be sold #134

Closed DenisCarriere closed 3 months ago

DenisCarriere commented 6 months ago

Purpose

Force close matured REX positions to automatically be sold when REX is matured, balance is credited to user's balance

process_rex_maturities() automatically sellrex when matured_rex is > 0.

Feedback provided by

Code example

if ( matured_rex > 0) {
    sell_rex( owner, asset(matured_rex, rex_symbol) );
}

Preconditions

References

 /**
  * @brief Updates REX owner maturity buckets
  *
  * @param bitr - iterator pointing to rex_balance object
  */
 void system_contract::process_rex_maturities( const rex_balance_table::const_iterator& bitr )
 {
    const time_point_sec now = current_time_point();
    _rexbalance.modify( bitr, same_payer, [&]( auto& rb ) {
       while ( !rb.rex_maturities.empty() && rb.rex_maturities.front().first <= now ) {
          rb.matured_rex += rb.rex_maturities.front().second;
          rb.rex_maturities.erase(rb.rex_maturities.begin());
       }
    });
 }
 // `rex_balance` structure underlying the rex balance table. A rex balance table entry is defined by:
 // - `version` defaulted to zero,
 // - `owner` the owner of the rex fund,
 // - `vote_stake` the amount of CORE_SYMBOL currently included in owner's vote,
 // - `rex_balance` the amount of REX owned by owner,
 // - `matured_rex` matured REX available for selling
 struct [[eosio::table,eosio::contract("eosio.system")]] rex_balance {
    uint8_t version = 0;
    name    owner;
    asset   vote_stake;
    asset   rex_balance;
    int64_t matured_rex = 0;
    std::vector<pair_time_point_sec_int64> rex_maturities; /// REX daily maturity buckets

    uint64_t primary_key()const { return owner.value; }
 };

 typedef eosio::multi_index< "rexbal"_n, rex_balance > rex_balance_table;

Note: changes should be pushed to Antelope Reference contracts https://github.com/AntelopeIO/reference-contracts

DenisCarriere commented 6 months ago

Implemented via https://github.com/AntelopeIO/reference-contracts/commit/6d791adc113c081894fe582d088d15c7175ab569

Additional unit tests is required

DenisCarriere commented 3 months ago

Implemented in v3.5.0