forked from steemit/steem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request steemit#3337 from steemit/3326-rewards-curve-api
Rewards API
- Loading branch information
Showing
7 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
file(GLOB HEADERS "include/steem/plugins/rewards_api/*.hpp") | ||
|
||
add_library( rewards_api_plugin | ||
${HEADERS} | ||
rewards_api.cpp | ||
rewards_api_plugin.cpp | ||
) | ||
|
||
target_link_libraries( rewards_api_plugin appbase steem_chain fc json_rpc_plugin ) | ||
target_include_directories( rewards_api_plugin | ||
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ) |
23 changes: 23 additions & 0 deletions
23
libraries/plugins/apis/rewards_api/include/steem/plugins/rewards_api/rewards_api.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <steem/plugins/rewards_api/rewards_api_args.hpp> | ||
#include <steem/plugins/json_rpc/utility.hpp> | ||
|
||
namespace steem { namespace plugins { namespace rewards_api { | ||
|
||
namespace detail { class rewards_api_impl; } | ||
|
||
class rewards_api | ||
{ | ||
public: | ||
rewards_api(); | ||
~rewards_api(); | ||
|
||
DECLARE_API( | ||
(simulate_curve_payouts) | ||
); | ||
private: | ||
std::unique_ptr< detail::rewards_api_impl > my; | ||
}; | ||
|
||
} } } //steem::plugins::rewards_api |
33 changes: 33 additions & 0 deletions
33
libraries/plugins/apis/rewards_api/include/steem/plugins/rewards_api/rewards_api_args.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
#include <string> | ||
#include <fc/uint128.hpp> | ||
#include <steem/protocol/misc_utilities.hpp> | ||
#include <steem/protocol/asset.hpp> | ||
#include <steem/plugins/json_rpc/utility.hpp> | ||
|
||
namespace steem { namespace plugins { namespace rewards_api { | ||
|
||
struct simulate_curve_payouts_element { | ||
protocol::account_name_type author; | ||
chainbase::shared_string permlink; | ||
protocol::asset payout; | ||
}; | ||
|
||
struct simulate_curve_payouts_args | ||
{ | ||
protocol::curve_id curve; | ||
std::string var1; | ||
}; | ||
|
||
struct simulate_curve_payouts_return | ||
{ | ||
std::string recent_claims; | ||
std::vector< simulate_curve_payouts_element > payouts; | ||
}; | ||
|
||
|
||
} } } // steem::plugins::rewards_api | ||
|
||
FC_REFLECT( steem::plugins::rewards_api::simulate_curve_payouts_element, (author)(permlink)(payout) ) | ||
FC_REFLECT( steem::plugins::rewards_api::simulate_curve_payouts_args, (curve)(var1) ) | ||
FC_REFLECT( steem::plugins::rewards_api::simulate_curve_payouts_return, (recent_claims)(payouts) ) |
36 changes: 36 additions & 0 deletions
36
libraries/plugins/apis/rewards_api/include/steem/plugins/rewards_api/rewards_api_plugin.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
#include <steem/chain/steem_fwd.hpp> | ||
#include <steem/plugins/rewards_api/rewards_api.hpp> | ||
#include <steem/plugins/json_rpc/json_rpc_plugin.hpp> | ||
|
||
#include <appbase/application.hpp> | ||
|
||
namespace steem { namespace plugins { namespace rewards_api { | ||
|
||
#define STEEM_REWARDS_API_PLUGIN_NAME "rewards_api" | ||
|
||
class rewards_api_plugin : public appbase::plugin< rewards_api_plugin > | ||
{ | ||
public: | ||
rewards_api_plugin(); | ||
virtual ~rewards_api_plugin(); | ||
|
||
APPBASE_PLUGIN_REQUIRES( | ||
(steem::plugins::json_rpc::json_rpc_plugin) | ||
) | ||
|
||
static const std::string& name() { static std::string name = STEEM_REWARDS_API_PLUGIN_NAME; return name; } | ||
|
||
virtual void set_program_options( | ||
boost::program_options::options_description& cli, | ||
boost::program_options::options_description& cfg ) override; | ||
virtual void plugin_initialize( const boost::program_options::variables_map& options ) override; | ||
virtual void plugin_startup() override; | ||
virtual void plugin_shutdown() override; | ||
|
||
private: | ||
std::unique_ptr< rewards_api > api; | ||
}; | ||
|
||
} } } // steem::plugins::rewards_api | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugin_name": "rewards_api", | ||
"plugin_namespace": "rewards_api", | ||
"plugin_project": "rewards_api_plugin" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include <chainbase/chainbase.hpp> | ||
#include <steem/chain/steem_objects.hpp> | ||
#include <steem/chain/util/reward.hpp> | ||
#include <steem/chain/util/uint256.hpp> | ||
#include <steem/plugins/chain/chain_plugin.hpp> | ||
#include <steem/plugins/rewards_api/rewards_api_plugin.hpp> | ||
#include <steem/plugins/rewards_api/rewards_api.hpp> | ||
|
||
namespace steem { namespace plugins { namespace rewards_api { | ||
|
||
namespace detail { | ||
|
||
class rewards_api_impl | ||
{ | ||
public: | ||
rewards_api_impl() : | ||
_db( appbase::app().get_plugin< steem::plugins::chain::chain_plugin >().db() ) {} | ||
|
||
DECLARE_API_IMPL( (simulate_curve_payouts) ); | ||
|
||
chain::database& _db; | ||
}; | ||
|
||
DEFINE_API_IMPL( rewards_api_impl, simulate_curve_payouts ) | ||
{ | ||
simulate_curve_payouts_return ret; | ||
|
||
const auto& cidx = _db.get_index< chain::comment_index, chain::by_cashout_time >(); | ||
|
||
auto current = cidx.begin(); | ||
|
||
fc::uint128_t sum_current_curve_vshares = 0; | ||
fc::uint128_t sum_simulated_vshares = 0; | ||
|
||
std::vector< fc::uint128_t > element_vshares; | ||
|
||
auto reward_fund_object = _db.get< chain::reward_fund_object, chain::by_name >( STEEM_POST_REWARD_FUND_NAME ); | ||
|
||
fc::uint128_t var1{ args.var1 }; | ||
|
||
while( current != cidx.end() && current->cashout_time < fc::time_point_sec::maximum() ) | ||
{ | ||
if ( current->net_rshares.value <= 0) | ||
{ | ||
++current; | ||
continue; | ||
} | ||
|
||
simulate_curve_payouts_element e; | ||
e.author = current->author; | ||
e.permlink = current->permlink; | ||
|
||
auto new_curve_vshares = chain::util::evaluate_reward_curve( current->net_rshares.value, args.curve, var1 ); | ||
sum_simulated_vshares = sum_simulated_vshares + new_curve_vshares; | ||
|
||
auto current_curve_vshares = chain::util::evaluate_reward_curve( current->net_rshares.value, reward_fund_object.author_reward_curve, reward_fund_object.content_constant ); | ||
sum_current_curve_vshares = sum_current_curve_vshares + current_curve_vshares; | ||
|
||
|
||
ret.payouts.push_back( std::move( e ) ); | ||
element_vshares.push_back( new_curve_vshares ); | ||
|
||
++current; | ||
} | ||
|
||
auto current_estimated_recent_claims = reward_fund_object.recent_claims + sum_current_curve_vshares; | ||
|
||
u256 simulated_recent_claims_u256; | ||
fc::uint128_t simulated_recent_claims; | ||
|
||
simulated_recent_claims_u256 = ( chain::util::to256( current_estimated_recent_claims ) * chain::util::to256( sum_simulated_vshares ) ) / chain::util::to256( sum_current_curve_vshares ); | ||
FC_ASSERT( ( simulated_recent_claims_u256 >> 64 ) <= u256( uint64_t( std::numeric_limits<int64_t>::max() ) ) ); | ||
simulated_recent_claims = fc::uint128_t( static_cast< int64_t >( simulated_recent_claims_u256 >> 64 ), static_cast< uint64_t >( simulated_recent_claims_u256 ) ); | ||
|
||
|
||
auto rf = chain::util::to256( reward_fund_object.reward_balance.amount.value ); | ||
auto total_claims = chain::util::to256( simulated_recent_claims ); | ||
|
||
for ( std::size_t i = 0; i < ret.payouts.size(); ++i ) | ||
{ | ||
auto payout_u256 = ( rf * chain::util::to256( element_vshares[ i ] ) ) / total_claims; | ||
FC_ASSERT( payout_u256 <= u256( uint64_t( std::numeric_limits<int64_t>::max() ) ) ); | ||
ret.payouts[ i ].payout = protocol::asset( static_cast< uint64_t >( payout_u256 ), STEEM_SYMBOL ); | ||
} | ||
|
||
ret.recent_claims = std::string{ simulated_recent_claims - sum_simulated_vshares }; | ||
|
||
return ret; | ||
} | ||
|
||
} // steem::plugins::rewards_api::detail | ||
|
||
rewards_api::rewards_api() : my( std::make_unique< detail::rewards_api_impl >() ) | ||
{ | ||
JSON_RPC_REGISTER_API( STEEM_REWARDS_API_PLUGIN_NAME ); | ||
} | ||
|
||
rewards_api::~rewards_api() {} | ||
|
||
DEFINE_READ_APIS( rewards_api, (simulate_curve_payouts) ) | ||
|
||
} } } // steem::plugins::rewards_api | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <steem/plugins/rewards_api/rewards_api_plugin.hpp> | ||
#include <steem/plugins/rewards_api/rewards_api.hpp> | ||
|
||
namespace steem { namespace plugins { namespace rewards_api { | ||
|
||
rewards_api_plugin::rewards_api_plugin() {} | ||
rewards_api_plugin::~rewards_api_plugin() {} | ||
|
||
void rewards_api_plugin::set_program_options( boost::program_options::options_description& cli, boost::program_options::options_description& cfg ) {} | ||
|
||
void rewards_api_plugin::plugin_initialize( const boost::program_options::variables_map& options ) | ||
{ | ||
api = std::make_unique< rewards_api >(); | ||
} | ||
|
||
void rewards_api_plugin::plugin_startup() | ||
{ | ||
elog( "NOTIFYALERT! ${name} is for testing purposes only, do not run in production", ("name", name()) ); | ||
} | ||
|
||
void rewards_api_plugin::plugin_shutdown() {} | ||
|
||
} } } // steem::plugins::rewards_api | ||
|