Skip to content

Commit

Permalink
Read Telos price from averages tables instead of medians
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirpashaa committed Nov 28, 2023
1 parent 8c32cd6 commit 3d270c2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 26 deletions.
2 changes: 2 additions & 0 deletions contracts/eosio.system/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ endif()

target_include_directories(eosio.system PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/../eosio.token/include
#// TELOS BEGIN
${CMAKE_CURRENT_SOURCE_DIR}/../../libs/eosio.tedp/include
${CMAKE_CURRENT_SOURCE_DIR}/../../libs/delphioracle/include)
#// TELOS END

set_target_properties(eosio.system PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")

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

[[eosio::action]]
void pay();

using unregreason_action = eosio::action_wrapper<"unregreason"_n, &system_contract::unregreason>;
using votebpout_action = eosio::action_wrapper<"votebpout"_n, &system_contract::votebpout>;
using setpayrates_action = eosio::action_wrapper<"setpayrates"_n, &system_contract::setpayrates>;
using distviarex_action = eosio::action_wrapper<"distviarex"_n, &system_contract::distviarex>;
using pay_action = eosio::action_wrapper<"pay"_n, &system_contract::pay>;
// TELOS END

private:
Expand Down Expand Up @@ -1738,8 +1741,7 @@ namespace eosiosystem {
// TELOS BEGIN
// defined in producer_pay.cpp
void claimrewards_snapshot();
void pay();

uint64_t get_telos_average_price();

double inverse_vote_weight(double staked, double amountVotedProducers);
void recalculate_votes();
Expand Down
62 changes: 38 additions & 24 deletions contracts/eosio.system/src/producer_pay.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <eosio.system/eosio.system.hpp>
#include <eosio.token/eosio.token.hpp>
// TELOS BEGIN
#include <eosio.tedp/eosio.tedp.hpp>
#include <delphioracle/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
// TELOS END
Expand Down Expand Up @@ -244,6 +244,37 @@ namespace eosiosystem {
*/
}

// TELOS BEGIN
uint64_t system_contract::get_telos_average_price() {
// Reads the delphi oracle TLOS/USD price
delphioracle::averagestable averages_table(delphi_oracle_account, "tlosusd"_n.value);

// Gets monthly average TLOS price
for (auto itr = averages_table.begin(); itr != averages_table.end(); ++itr) {
if (itr->type == delphioracle::averages::get_type(average_types::last_30_days)) {
return itr->value;
}
}

// Gets 14 days average if monthly average is not available
for (auto itr = averages_table.begin(); itr != averages_table.end(); ++itr) {
if (itr->type == delphioracle::averages::get_type(average_types::last_14_days)) {
return itr->value;
}
}

// Gets 7 days average if 14 days average is not available
for (auto itr = averages_table.begin(); itr != averages_table.end(); ++itr) {
if (itr->type == delphioracle::averages::get_type(average_types::last_7_days)) {
return itr->value;
}
}

// Returns smallest non zero value if no price is available
return 1;
}
// TELOS END

void system_contract::claimrewards_snapshot() {
check(_gstate.thresh_activated_stake_time > time_point(), "cannot take snapshot until chain is activated");

Expand All @@ -258,21 +289,12 @@ namespace eosiosystem {

if (usecs_since_last_fill > 0 && _gstate.last_pervote_bucket_fill > time_point())
{
// Reads the delphi oracle TLOS/USD price
delphioracle::medianstable medians_table(delphi_oracle_account, "tlosusd"_n.value);
auto medians_timestamp_index = medians_table.get_index<"timestamp"_n>();

// Gets daily median TLOS price
uint64_t tlos_price = 0;
for (auto itr = medians_timestamp_index.rbegin(); itr != medians_timestamp_index.rend(); ++itr) {
if (itr->type == delphioracle::medians::get_type(median_types::day)) {
tlos_price = itr->value / itr->request_count;
break;
}
}
// TELOS BEGIN
uint64_t tlos_price = get_telos_average_price();
auto to_workers = static_cast<int64_t>((12 * double(_gpayrate.worker_amount) * double(usecs_since_last_fill)) / double(useconds_per_year));
double bp_pay_per_month = std::min((double(378000) * std::pow(tlos_price/10000.0,-0.516)),double(882000)) * 10000;
auto to_producers = static_cast<int64_t>((bp_pay_per_month * 12 * double(usecs_since_last_fill)) / double(useconds_per_year));
// TELOS END
auto new_tokens = to_workers + to_producers;

//NOTE: This line can cause failure if eosio.tedp doesn't have a balance emplacement
Expand Down Expand Up @@ -373,22 +395,13 @@ namespace eosiosystem {
}
}

// TELOS BEGIN
void system_contract::pay() {
// Reads the payouts table
tedp::payout_table payouts(tedp_account, tedp_account.value);

// Reads the delphi oracle TLOS/USD price
delphioracle::medianstable medians_table(delphi_oracle_account, "tlosusd"_n.value);
auto medians_timestamp_index = medians_table.get_index<"timestamp"_n>();

// Gets daily median TLOS price
uint64_t tlos_price = 0;
for (auto itr = medians_timestamp_index.rbegin(); itr != medians_timestamp_index.rend(); ++itr) {
if (itr->type == delphioracle::medians::get_type(median_types::day)) {
tlos_price = itr->value / itr->request_count;
break;
}
}
uint64_t tlos_price = get_telos_average_price();

uint64_t now_ms = current_time_point().sec_since_epoch();
bool payouts_made = false;
Expand Down Expand Up @@ -460,5 +473,6 @@ namespace eosiosystem {
std::make_tuple()
).send();
}
// TELOS END

} //namespace eosiosystem
49 changes: 49 additions & 0 deletions libs/delphioracle/include/delphioracle/delphioracle.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TELOS BEGIN
/*
delphioracle
Expand Down Expand Up @@ -29,6 +30,14 @@ static const std::string system_str("system");

static const asset one_larimer = asset(1, symbol("TLOS", 4));

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

enum class median_types : uint8_t {
day = 0,
week = 1,
Expand Down Expand Up @@ -132,6 +141,7 @@ CONTRACT delphioracle : public eosio::contract {
uint64_t id;
uint64_t total_datapoints_count;
asset total_claimed = asset(0, symbol("TLOS", 4));
uint32_t last_daily_average_run;

//constants
uint64_t datapoints_per_instrument = 21;
Expand All @@ -145,6 +155,8 @@ CONTRACT delphioracle : public eosio::contract {
uint64_t paid = 21;
uint64_t min_bounty_delay = 604800;
uint64_t new_bounty_delay = 259200;
uint64_t daily_datapoints_per_instrument = 45;
uint32_t daily_average_timeout = 3600;

uint64_t primary_key() const { return id; }
};
Expand All @@ -157,6 +169,31 @@ CONTRACT delphioracle : public eosio::contract {
uint64_t primary_key() const { return id; }
};


// holds the daily datapoints used to compute the averages
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);
}
};

//Holds the last datapoints_count datapoints from qualified oracles
TABLE datapoints {
uint64_t id;
Expand Down Expand Up @@ -325,6 +362,13 @@ CONTRACT delphioracle : public eosio::contract {
typedef eosio::multi_index<"pairs"_n, pairs> pairstable;
typedef eosio::multi_index<"npairs"_n, pairs> npairstable;

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;

typedef eosio::multi_index<"datapoints"_n, datapoints,
indexed_by<"value"_n, const_mem_fun<datapoints, uint64_t, &datapoints::by_value>>,
indexed_by<"timestamp"_n, const_mem_fun<datapoints, uint64_t, &datapoints::by_timestamp>>> datapointstable;
Expand Down Expand Up @@ -425,6 +469,10 @@ CONTRACT delphioracle : public eosio::contract {
const time_point& median_timestamp, const uint64_t median_value, const uint64_t median_request_count = 1);
bool is_active_current_week() const;
std::vector<median_types> GetUpdateMedians(median_types current_type) const;
void update_daily_datapoints(name instrument);
uint64_t compute_last_days_average(name scope, uint8_t days);
void update_averages(name instrument);
std::optional<std::pair<time_point, uint64_t>> get_daily_median(name instrument);

//Check if calling account is a qualified oracle
bool check_oracle(const name owner) {
Expand Down Expand Up @@ -739,3 +787,4 @@ CONTRACT delphioracle : public eosio::contract {
return flag_medians_instance.exists() && flag_medians_instance.get().is_active;
}
};
// TELOS END
2 changes: 2 additions & 0 deletions libs/eosio.tedp/include/eosio.tedp/eosio.tedp.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TELOS BEGIN
#include <eosio/eosio.hpp>
#include <eosio/system.hpp>
#include <eosio/asset.hpp>
Expand Down Expand Up @@ -103,3 +104,4 @@ class [[eosio::contract("eosio.tedp")]] tedp : public contract
config_table configuration;
};

// TELOS END
2 changes: 2 additions & 0 deletions libs/eosio.tedp/include/eosio.tedp/tedp.constants.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TELOS BEGIN
// tf 1mil
// econdevfunds 500k
// rex 1mil
Expand Down Expand Up @@ -49,3 +50,4 @@ static constexpr VNAME EVM_ACCOUNT = "eosio.evm"_n;
static constexpr VNAME REX_ACCOUNT = "eosio.rex"_n;
static constexpr VNAME IGNITE_ACCOUNT = "ignitegrants"_n;
static constexpr VNAME FUEL_ACCOUNT = "tlosfuelfund"_n;
// TELOS END

0 comments on commit 3d270c2

Please sign in to comment.