From e4b3da04ee075d502fd4520a8ba4a04219cccd40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josejulio=20Mart=C3=ADnez?= Date: Mon, 11 Sep 2023 20:02:49 -0600 Subject: [PATCH] Starting computation of bprate --- .../eosio.system/include/delphioracle.hpp | 66 +++++++++++++++++++ .../include/eosio.system/eosio.system.hpp | 6 ++ contracts/eosio.system/src/producer_pay.cpp | 41 ++++++++++++ .../include/eosio.token/eosio.token.hpp | 4 +- tests/eosio.system_tests.cpp | 10 +++ 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 contracts/eosio.system/include/delphioracle.hpp diff --git a/contracts/eosio.system/include/delphioracle.hpp b/contracts/eosio.system/include/delphioracle.hpp new file mode 100644 index 00000000..8a5b9344 --- /dev/null +++ b/contracts/eosio.system/include/delphioracle.hpp @@ -0,0 +1,66 @@ +#include +#include + +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>, + indexed_by<"timestamp"_n, const_mem_fun> + > dailydatapointstable; + + typedef eosio::multi_index< + "averages"_n, + averages,indexed_by <"timestamp"_n, const_mem_fun> + > averagestable; + +} diff --git a/contracts/eosio.system/include/eosio.system/eosio.system.hpp b/contracts/eosio.system/include/eosio.system/eosio.system.hpp index 581c95a1..0cf4994f 100644 --- a/contracts/eosio.system/include/eosio.system/eosio.system.hpp +++ b/contracts/eosio.system/include/eosio.system/eosio.system.hpp @@ -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>; @@ -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); diff --git a/contracts/eosio.system/src/producer_pay.cpp b/contracts/eosio.system/src/producer_pay.cpp index 9518a98a..e7992f9e 100644 --- a/contracts/eosio.system/src/producer_pay.cpp +++ b/contracts/eosio.system/src/producer_pay.cpp @@ -1,5 +1,6 @@ #include #include +#include // 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 @@ -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 diff --git a/contracts/eosio.token/include/eosio.token/eosio.token.hpp b/contracts/eosio.token/include/eosio.token/eosio.token.hpp index ce8756c7..5113c4e3 100644 --- a/contracts/eosio.token/include/eosio.token/eosio.token.hpp +++ b/contracts/eosio.token/include/eosio.token/eosio.token.hpp @@ -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; }; } diff --git a/tests/eosio.system_tests.cpp b/tests/eosio.system_tests.cpp index 997c316f..96a57212 100644 --- a/tests/eosio.system_tests.cpp +++ b/tests/eosio.system_tests.cpp @@ -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();