Skip to content

Commit

Permalink
Account price based on rem.oracle (#127)
Browse files Browse the repository at this point in the history
* added min account price based on rem oracle

- account price 0.5 $ in token

* update system table in rem.swap

* tests added and code refactor

* added test cases to the gift_resources_tests

* move code to the get_min_account_stake

* fixed tests
* move min_account_price from table to code
* move account creation fee code to the get_min_account_stake

* fixed rem.orace tests

* refactor `get_min_account_stake` method

Co-authored-by: Roman Cherednik <[email protected]>
  • Loading branch information
Alladin9393 and roman-tik authored Feb 3, 2020
1 parent 0385f82 commit 46ede46
Show file tree
Hide file tree
Showing 10 changed files with 448 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace eosio {
using std::string;
using std::vector;

// A window in which producer can submit a new rate
static constexpr uint32_t setprice_window = 3600;

// Defines 'remprice' to be stored market price to the specified pairs
struct [[eosio::table, eosio::contract("rem.oracle")]] remprice {
name pair;
Expand Down
4 changes: 2 additions & 2 deletions contracts/contracts/rem.oracle/src/rem.oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace eosio {
time_point ct = current_time_point();

if (data_it != pricedata_tbl.end()) {
uint64_t ct_amount_hours = ct.sec_since_epoch() / 3600;
uint64_t last_amount_hours = data_it->last_update.to_time_point().sec_since_epoch() / 3600;
uint64_t ct_amount_hours = ct.sec_since_epoch() / setprice_window;
uint64_t last_amount_hours = data_it->last_update.to_time_point().sec_since_epoch() / setprice_window;
check(ct_amount_hours > last_amount_hours, "the frequency of price changes should not exceed 1 time during the current hour");

pricedata_tbl.modify(*data_it, producer, [&](auto &p) {
Expand Down
12 changes: 6 additions & 6 deletions contracts/contracts/rem.swap/src/system_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace eosio {
symbol core_symbol;

uint64_t max_ram_size = 64ll*1024 * 1024 * 1024;
uint64_t min_account_stake = 1000000; //minimum stake for new created account 100'0000 REM
uint64_t min_account_stake = 1000000; // minimum stake for new created account 100'0000 REM
uint64_t total_ram_bytes_reserved = 0;
int64_t total_ram_stake = 0;
//producer name and pervote factor
Expand All @@ -40,11 +40,11 @@ namespace eosio {

// explicit serialization macro is not necessary, used here only to improve compilation time
EOSLIB_SERIALIZE_DERIVED( eosio_global_state, eosio::blockchain_parameters, (core_symbol)(max_ram_size)(min_account_stake)
(total_ram_bytes_reserved)(total_ram_stake)(last_schedule)(standby)(last_schedule_version)
(current_round_start_time) (last_producer_schedule_update)(last_pervote_bucket_fill)
(perstake_bucket)(pervote_bucket)(perblock_bucket)(total_unpaid_blocks)(total_guardians_stake)
(total_activated_stake)(thresh_activated_stake_time)(last_producer_schedule_size)
(total_producer_vote_weight)(total_active_producer_vote_weight)(last_name_close) )
(total_ram_bytes_reserved)(total_ram_stake)(last_schedule)(standby)(last_schedule_version)
(current_round_start_time) (last_producer_schedule_update)(last_pervote_bucket_fill)
(perstake_bucket)(pervote_bucket)(perblock_bucket)(total_unpaid_blocks)(total_guardians_stake)
(total_activated_stake)(thresh_activated_stake_time)(last_producer_schedule_size)
(total_producer_vote_weight)(total_active_producer_vote_weight)(last_name_close) )
};

typedef eosio::singleton< "global"_n, eosio_global_state > global_state_singleton;
Expand Down
1 change: 1 addition & 0 deletions contracts/contracts/rem.system/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_include_directories(rem.system
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/../rem.token/include
${CMAKE_CURRENT_SOURCE_DIR}/../rem.attr/include
${CMAKE_CURRENT_SOURCE_DIR}/../rem.oracle/include
)

set_target_properties(rem.system
Expand Down
11 changes: 6 additions & 5 deletions contracts/contracts/rem.system/include/rem.system/rem.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ namespace eosiosystem {
static constexpr int64_t default_inflation_pay_factor = 50000; // producers pay share = 10000 / 50000 = 20% of the inflation
static constexpr int64_t default_votepay_factor = 40000; // per-block pay share = 10000 / 40000 = 25% of the producer pay

// 3742 bytes - is size of the new user account for current version
static constexpr int64_t min_account_ram = 3800;

static constexpr int64_t min_account_ram = 3800; // 3742 bytes - is size of the new user account for current version
static constexpr uint64_t min_account_price = 5000; // the minimum price for creating a new account 0.5 $

/**
*
Expand Down Expand Up @@ -152,7 +151,7 @@ namespace eosiosystem {
symbol core_symbol;

uint64_t max_ram_size = 64ll*1024 * 1024 * 1024;
uint64_t min_account_stake = 1000000; //minimum stake for new created account 100'0000 REM
uint64_t min_account_stake = 1000000; // the minimum stake for new created account 100'0000 REM
uint64_t total_ram_bytes_reserved = 0;
int64_t total_ram_stake = 0;
//producer name and pervote factor
Expand Down Expand Up @@ -664,6 +663,7 @@ namespace eosiosystem {
static constexpr eosio::name saving_account{"rem.saving"_n};
static constexpr eosio::name rex_account{"rem.rex"_n};
static constexpr eosio::name null_account{"rem.null"_n};
static constexpr eosio::name rem_usd_pair{"rem.usd"_n};
static constexpr symbol rex_symbol = symbol(symbol_code("REX"), 4);

static constexpr uint8_t max_block_producers = 21;
Expand Down Expand Up @@ -1460,10 +1460,11 @@ namespace eosiosystem {
private:
// Implementation details:

//defined in rem.system.cpp
// defined in rem.system.cpp
static eosio_global_state get_default_parameters();
static eosio_global_state4 get_default_inflation_parameters();
static eosio_global_rem_state get_default_rem_parameters();
uint64_t get_min_account_stake();
symbol core_symbol()const;
void update_ram_supply();

Expand Down
6 changes: 4 additions & 2 deletions contracts/contracts/rem.system/src/delegate_bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <rem.system/rem.system.hpp>
#include <rem.token/rem.token.hpp>
#include <rem.oracle/rem.oracle.hpp>

#include <cmath>
#include <map>
Expand Down Expand Up @@ -64,6 +65,7 @@ namespace eosiosystem {
{
require_auth( from );
check( stake_delta.amount != 0, "should stake non-zero amount" );
uint64_t min_account_stake = get_min_account_stake();

name source_stake_from = from;
if ( transfer ) {
Expand Down Expand Up @@ -116,14 +118,14 @@ namespace eosiosystem {
tot.own_stake_amount += stake_delta.amount;

// we have to decrease free bytes in case of own stake
const int64_t new_free_stake_amount = std::min( static_cast< int64_t >(_gstate.min_account_stake) - tot.own_stake_amount, tot.free_stake_amount);
const int64_t new_free_stake_amount = std::min( static_cast< int64_t >(min_account_stake) - tot.own_stake_amount, tot.free_stake_amount);
tot.free_stake_amount = std::max(new_free_stake_amount, 0LL);
}
});
}
check( 0 <= tot_itr->net_weight.amount, "insufficient staked total net bandwidth" );
check( 0 <= tot_itr->cpu_weight.amount, "insufficient staked total cpu bandwidth" );
check( _gstate.min_account_stake <= tot_itr->own_stake_amount + tot_itr->free_stake_amount, "insufficient minimal account stake for " + receiver.to_string() );
check( min_account_stake <= tot_itr->own_stake_amount + tot_itr->free_stake_amount, "insufficient minimal account stake for " + receiver.to_string() );

{
bool ram_managed = false;
Expand Down
17 changes: 16 additions & 1 deletion contracts/contracts/rem.system/src/rem.system.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <rem.system/rem.system.hpp>
#include <rem.token/rem.token.hpp>
#include <rem.attr/rem.attr.hpp>
#include <rem.oracle/rem.oracle.hpp>

#include <eosio/crypto.hpp>
#include <eosio/dispatcher.hpp>
Expand Down Expand Up @@ -159,6 +160,19 @@ namespace eosiosystem {
_gstate.min_account_stake = min_account_stake;
}

uint64_t system_contract::get_min_account_stake() {
eosio::remprice_idx remprice_table(oracle_account, oracle_account.value);
auto rem_usd_it = remprice_table.find(rem_usd_pair.value);
auto setprice_window_m = eosio::seconds(eosio::setprice_window + eosio::setprice_window * 0.2);
bool is_valid_price = ( rem_usd_it != remprice_table.end() ) && ( (current_time_point() - rem_usd_it->last_update.to_time_point()) <= setprice_window_m );
uint64_t oracle_min_account_stake = min_account_price / rem_usd_it->price;

if ( is_valid_price && oracle_min_account_stake > 0 ) {
return std::min(oracle_min_account_stake, _gstate.min_account_stake);
}
return _gstate.min_account_stake;
}

void system_contract::setram( uint64_t max_ram_size ) {
require_auth( get_self() );

Expand Down Expand Up @@ -318,10 +332,11 @@ namespace eosiosystem {
// 0 - 0.0000%, 100'0000 - 100.0000%
check( (discount >= 0) && (discount <= 100'0000), "discount value should be in range[0, 100'0000]" );
const auto discount_rate = discount / 100'0000.0;
uint64_t min_account_stake = get_min_account_stake();

const auto system_token_max_supply = eosio::token::get_max_supply(token_account, system_contract::get_core_symbol().code() );
const double bytes_per_token = (double)_gstate.max_ram_size / (double)system_token_max_supply.amount;
free_stake_amount = discount_rate * _gstate.min_account_stake;
free_stake_amount = discount_rate * min_account_stake;
free_gift_bytes = bytes_per_token * free_stake_amount;
}

Expand Down
Empty file added price
Empty file.
Loading

0 comments on commit 46ede46

Please sign in to comment.