Skip to content

Commit

Permalink
Update BP rate
Browse files Browse the repository at this point in the history
  • Loading branch information
josejulio committed Oct 6, 2023
1 parent e4b3da0 commit 990ece2
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 31 deletions.
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ ExternalProject_Add(
INSTALL_COMMAND ""
BUILD_ALWAYS 1)

ExternalProject_Add(
nativetests
SOURCE_DIR ${CMAKE_SOURCE_DIR}/native_tests
BINARY_DIR ${CMAKE_BINARY_DIR}/native_tests
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
-DCMAKE_TOOLCHAIN_FILE=${CDT_ROOT}/lib/cmake/cdt/CDTWasmToolchain.cmake
-DSYSTEM_CONFIGURABLE_WASM_LIMITS=${SYSTEM_CONFIGURABLE_WASM_LIMITS}
-DSYSTEM_BLOCKCHAIN_PARAMETERS=${SYSTEM_BLOCKCHAIN_PARAMETERS}
UPDATE_COMMAND ""
PATCH_COMMAND ""
TEST_COMMAND ""
INSTALL_COMMAND ""
BUILD_ALWAYS 1
)

if(APPLE)
set(OPENSSL_ROOT "/usr/local/opt/openssl")
elseif(UNIX)
Expand All @@ -61,3 +76,7 @@ if(BUILD_TESTS)
else()
message(STATUS "Unit tests will not be built. To build unit tests, set BUILD_TESTS to ON.")
endif()

include (CTest)
enable_testing()
add_test(native_tests ${CMAKE_BINARY_DIR}/native_tests/bpay_rate_test)
1 change: 1 addition & 0 deletions contracts/eosio.system/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_contract(
${CMAKE_CURRENT_SOURCE_DIR}/src/name_bidding.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/native.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/producer_pay.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/bpay_rate.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/powerup.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/rex.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/voting.cpp
Expand Down
5 changes: 5 additions & 0 deletions contracts/eosio.system/include/eosio.system/bpay_rate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <eosio/asset.hpp>

namespace eosiosystem {
double compute_bpay_rate(uint64_t tlos_price, eosio::asset total_telos_supply);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace delphioracle {


enum class average_types: uint8_t {
last_7_days = 0,
last_7_days = 0,
last_14_days = 1,
last_30_days = 2,
last_45_days = 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1741,8 +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();
uint64_t get_last_30_days_tlosusd_average();


double inverse_vote_weight(double staked, double amountVotedProducers);
Expand Down
30 changes: 30 additions & 0 deletions contracts/eosio.system/src/bpay_rate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <eosio.system/bpay_rate.hpp>

using eosio::asset;

namespace eosiosystem {
double 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;

// 100k divided by (30-minutes divided by the Number-of-minutes-in-a-year)
// 100k / 0.00005707762557
// This was pulled out of the bpay_rate formula
const double _100K_DIVIDED_BY_TIME_PERIOD = 1752000000;

const double raw_tlos_price = double(tlos_price) / 100;
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 double bp_rate = round(
(_100K_DIVIDED_BY_TIME_PERIOD* tlos_in_30_mins) /
total_telos_supply_double
);

return bp_rate;
}
}
51 changes: 22 additions & 29 deletions contracts/eosio.system/src/producer_pay.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <eosio.system/eosio.system.hpp>
#include <eosio.token/eosio.token.hpp>
#include <delphioracle.hpp>
#include <eosio.system/delphioracle.hpp>
// TELOS BEGIN
#include "system_kick.cpp"
#include <eosio.system/bpay_rate.hpp>
#define MAX_PRODUCERS 42 // revised for TEDP 2 Phase 2, also set in system_rotation.cpp, change in both places
// TELOS END

Expand Down Expand Up @@ -258,7 +259,8 @@ namespace eosiosystem {

if (usecs_since_last_fill > 0 && _gstate.last_pervote_bucket_fill > time_point())
{
double bpay_rate = double(_gpayrate.bpay_rate) / double(100000); //NOTE: both bpay_rate and divisor were int64s which evaluated to 0. The divisor must be a double to get percentage.
double bpay_rate = compute_bpay_rate(get_last_30_days_tlosusd_average(), get_total_telos_supply());

auto to_workers = static_cast<int64_t>((12 * double(_gpayrate.worker_amount) * double(usecs_since_last_fill)) / double(useconds_per_year));
auto to_producers = static_cast<int64_t>((bpay_rate * double(token_supply.amount) * double(usecs_since_last_fill)) / double(useconds_per_year));
auto new_tokens = to_workers + to_producers;
Expand Down Expand Up @@ -361,44 +363,35 @@ 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");
name tlos("TLOS");

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

return stat.begin()->supply;
}

uint64_t system_contract::get_last_30_days_tlosusd_average() {
name delphioracle("delphioracle");
name tlosusd("tlosusd");

delphioracle::averagestable averages(delphioracle, tlosusd.value);
for (auto itr = averages.begin(); itr != averages.end(); ++itr) {
if (itr->type == delphioracle::averages::get_type(delphioracle::average_types::last_30_days)) {
return itr->value;
}
}

check(false, "Last 30 day average not found");
// Silence warning
return 0;
}

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));
check(false, "Failing tx");
}

} //namespace eosiosystem
14 changes: 14 additions & 0 deletions native_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.5)

project(native_tests)

find_package(cdt)

add_native_executable(
bpay_rate_test
bpay_rate_test
${CMAKE_CURRENT_SOURCE_DIR}/bpay_rate_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../contracts/eosio.system/src/bpay_rate.cpp
)

target_include_directories( bpay_rate_test PUBLIC ${CMAKE_SOURCE_DIR}/../contracts/eosio.system/include )
28 changes: 28 additions & 0 deletions native_tests/bpay_rate_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#undef NDEBUG
#include <cassert>
#include <eosio/print.hpp>
#include <eosio.system/bpay_rate.hpp>
#include <cstdio>

using eosiosystem::compute_bpay_rate;

void test_bpay_tests() {
eosio::asset TLOS_TOTAL_SUPPLY = eosio::asset(
4200000000000,
eosio::symbol("TLOS", 4)
);

assert(compute_bpay_rate(2, TLOS_TOTAL_SUPPLY) == 8250);
assert(compute_bpay_rate(100, TLOS_TOTAL_SUPPLY) == 1096);
assert(compute_bpay_rate(200, TLOS_TOTAL_SUPPLY) == 766);
assert(compute_bpay_rate(500, TLOS_TOTAL_SUPPLY) == 478);
assert(compute_bpay_rate(1000, TLOS_TOTAL_SUPPLY) == 334);
assert(compute_bpay_rate(5000, TLOS_TOTAL_SUPPLY) == 146);
assert(compute_bpay_rate(10000, TLOS_TOTAL_SUPPLY) == 102);
}


int main(int argc, char** argv) {
test_bpay_tests();
return 0;
}

0 comments on commit 990ece2

Please sign in to comment.