Skip to content

Commit

Permalink
Starting computation of bprate
Browse files Browse the repository at this point in the history
  • Loading branch information
josejulio committed Sep 12, 2023
1 parent 87022a6 commit e4b3da0
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
66 changes: 66 additions & 0 deletions contracts/eosio.system/include/delphioracle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <eosio/eosio.hpp>
#include <math.h>

using eosio::time_point;
using eosio::const_mem_fun;
using eosio::indexed_by;

namespace delphioracle {

const eosio::time_point NULL_TIME_POINT = eosio::time_point(eosio::microseconds(0));


enum class average_types: uint8_t {
last_7_days = 0,
last_14_days = 1,
last_30_days = 2,
last_45_days = 3,
none = 255,
};

TABLE daily_datapoints {
uint64_t id;
uint64_t value;
time_point timestamp;

uint64_t primary_key() const {
return id;
}
uint64_t by_timestamp() const {
return timestamp.elapsed.to_seconds();
}
uint64_t by_value() const {
return value;
}
};

TABLE averages {
uint64_t id;
uint8_t type = get_type(average_types::none);
uint64_t value = 0;
time_point timestamp = NULL_TIME_POINT;
uint64_t primary_key() const {
return id;
}
uint64_t by_timestamp() const {
return timestamp.elapsed.to_seconds();
}

static uint8_t get_type(average_types type) {
return static_cast < uint8_t > (type);
}
};

typedef eosio::multi_index<
"dailydatapnt"_n,
daily_datapoints,
indexed_by<"value"_n, const_mem_fun<daily_datapoints, uint64_t, &daily_datapoints::by_value>>,
indexed_by<"timestamp"_n, const_mem_fun<daily_datapoints,uint64_t, &daily_datapoints::by_timestamp>>
> dailydatapointstable;

typedef eosio::multi_index<
"averages"_n,
averages,indexed_by <"timestamp"_n, const_mem_fun<averages, uint64_t, &averages::by_timestamp>>
> averagestable;

}
6 changes: 6 additions & 0 deletions contracts/eosio.system/include/eosio.system/eosio.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,10 @@ namespace eosiosystem {
[[eosio::action]]
void distviarex(name from, asset amount);

// Defined in producer_pay.cpp
[[eosio::action]]
void bpayrate(uint64_t tlos_price, asset total_telos_supply);


using unregreason_action = eosio::action_wrapper<"unregreason"_n, &system_contract::unregreason>;
using votebpout_action = eosio::action_wrapper<"votebpout"_n, &system_contract::votebpout>;
Expand Down Expand Up @@ -1737,6 +1741,8 @@ namespace eosiosystem {
// TELOS BEGIN
// defined in producer_pay.cpp
void claimrewards_snapshot();
double compute_bpay_rate(uint64_t tlos_price, asset total_telos_supply);
asset get_total_telos_supply();


double inverse_vote_weight(double staked, double amountVotedProducers);
Expand Down
41 changes: 41 additions & 0 deletions contracts/eosio.system/src/producer_pay.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <eosio.system/eosio.system.hpp>
#include <eosio.token/eosio.token.hpp>
#include <delphioracle.hpp>
// TELOS BEGIN
#include "system_kick.cpp"
#define MAX_PRODUCERS 42 // revised for TEDP 2 Phase 2, also set in system_rotation.cpp, change in both places
Expand Down Expand Up @@ -360,4 +361,44 @@ namespace eosiosystem {
}
}

double system_contract::compute_bpay_rate(uint64_t tlos_price, asset total_telos_supply) {
const double MULTIPLIER_CONSTANT = 8.34;
const double POWER_OF_CONSTANT = -0.516;
const double ACTIVE_BP_COUNT = 21;
const double STANDBY_BP_COUNT = 21;

// 30-minutes divided by the Number-of-minutes-in-a-year times 100k
// This was pulled out of the bpay_rate formula
const double HALF_HOURS_MINUTES_TIMES_100K = 5.707762557;


const double raw_tlos_price = double(tlos_price) / 10'000.0;
const double tlos_per_bp = MULTIPLIER_CONSTANT * pow((double) raw_tlos_price, POWER_OF_CONSTANT);
const double tlos_in_30_mins = tlos_per_bp * (ACTIVE_BP_COUNT + 0.5 * STANDBY_BP_COUNT);

double total_telos_supply_double = double(total_telos_supply.amount) / pow(10.0, total_telos_supply.symbol.precision());

const uint64_t bp_rate = round(
(HALF_HOURS_MINUTES_TIMES_100K* tlos_in_30_mins) /
(total_telos_supply_double)
);

return bp_rate;
}

asset system_contract::get_total_telos_supply() {
name eosio_token("eosio.token");
name tlos("tlos");

token::stats stat(eosio_token, tlos.value);
check(stat.begin() != stat.end(), "TLOS supply not found");

return stat.begin()->supply;
}

void system_contract::bpayrate(uint64_t tlos_price, asset total_telos_supply) {
require_auth(_self);
print("bpay_rate: ", compute_bpay_rate(tlos_price, total_telos_supply));
}

} //namespace eosiosystem
4 changes: 3 additions & 1 deletion contracts/eosio.token/include/eosio.token/eosio.token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@ namespace eosio {
};

typedef eosio::multi_index< "accounts"_n, account > accounts;
typedef eosio::multi_index< "stat"_n, currency_stats > stats;

void sub_balance( const name& owner, const asset& value );
void add_balance( const name& owner, const asset& value, const name& ram_payer );

public:
typedef eosio::multi_index< "stat"_n, currency_stats > stats;
};

}
10 changes: 10 additions & 0 deletions tests/eosio.system_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,16 @@ BOOST_FIXTURE_TEST_CASE( stake_while_pending_refund, eosio_system_tester ) try {

} FC_LOG_AND_RETHROW()

BOOST_FIXTURE_TEST_CASE( test_bps_rate, eosio_system_tester ) try {
BOOST_REQUIRE_EQUAL(
success(),
push_action("eosio"_n, "bpayrate", mvo()
("tlos_price", 200),
("total_telos_supply"), core_sym::from_string("420000000.0000")
)
);
} FC_LOG_AND_RETHROW()

BOOST_FIXTURE_TEST_CASE( fail_without_auth, eosio_system_tester ) try {
// TELOS BEGIN
activate_network();
Expand Down

0 comments on commit e4b3da0

Please sign in to comment.