Scorum
reward_balance_algorithm.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace scorum {
6 namespace chain {
7 
8 namespace database_ns {
9 
10 using namespace scorum::protocol;
11 
12 template <class TFundService> class reward_balance_algorithm
13 {
14  TFundService& _service;
15 
16  const percent_type _adjust_percent;
17  const uint32_t _guaranted_reward_supply_period_in_days;
18  const uint32_t _reward_increase_threshold_in_days;
19 
20 public:
21  reward_balance_algorithm(TFundService& reward_service,
23  uint32_t guaranted_reward_supply_period_in_days
25  uint32_t reward_increase_threshold_in_days = SCORUM_REWARD_INCREASE_THRESHOLD_IN_DAYS)
26  : _service(reward_service)
27  , _adjust_percent(adjust_percent)
28  , _guaranted_reward_supply_period_in_days(guaranted_reward_supply_period_in_days)
29  , _reward_increase_threshold_in_days(reward_increase_threshold_in_days)
30  {
31  }
32 
33  // return actual balance after increasing
34  const asset& increase_ballance(const asset& delta)
35  {
36  _service.update([&](typename TFundService::object_type& pool) { pool.balance += delta; });
37 
38  return _service.get().balance;
39  }
40 
42  {
43  const auto current_per_day_reward = _service.get().current_per_block_reward * SCORUM_BLOCKS_PER_DAY;
44  const auto symbol_type = current_per_day_reward.symbol();
45 
46  asset real_per_block_reward(0, symbol_type);
47 
48  _service.update([&](typename TFundService::object_type& pool) {
49  asset delta = pool.current_per_block_reward * utils::make_fraction(_adjust_percent, SCORUM_100_PERCENT);
50 
51  delta = std::max(asset(SCORUM_MIN_PER_BLOCK_REWARD, symbol_type), delta);
52 
53  if (pool.balance > current_per_day_reward * _reward_increase_threshold_in_days)
54  {
55  // recalculate
56  pool.current_per_block_reward += delta;
57  }
58  else if (pool.balance < current_per_day_reward * _guaranted_reward_supply_period_in_days)
59  {
60  // recalculate
61  pool.current_per_block_reward
62  = std::max(asset(SCORUM_MIN_PER_BLOCK_REWARD, symbol_type), pool.current_per_block_reward - delta);
63  }
64  else
65  {
66  // use current_perblock_reward
67  }
68 
69  if (pool.balance >= pool.current_per_block_reward) // balance must not be negative
70  {
71  pool.balance -= pool.current_per_block_reward;
72  real_per_block_reward = pool.current_per_block_reward;
73  }
74  else if (pool.balance.amount > 0)
75  {
76  real_per_block_reward = pool.balance;
77  pool.balance.amount = 0;
78  }
79  });
80 
81  return real_per_block_reward;
82  }
83 };
84 }
85 }
86 }
reward_balance_algorithm(TFundService &reward_service, percent_type adjust_percent=SCORUM_ADJUST_REWARD_PERCENT, uint32_t guaranted_reward_supply_period_in_days=SCORUM_GUARANTED_REWARD_SUPPLY_PERIOD_IN_DAYS, uint32_t reward_increase_threshold_in_days=SCORUM_REWARD_INCREASE_THRESHOLD_IN_DAYS)
#define SCORUM_ADJUST_REWARD_PERCENT
Definition: config.hpp:211
#define SCORUM_REWARD_INCREASE_THRESHOLD_IN_DAYS
Definition: config.hpp:145
#define SCORUM_GUARANTED_REWARD_SUPPLY_PERIOD_IN_DAYS
Definition: config.hpp:144
#define SCORUM_100_PERCENT
Definition: config.hpp:200
#define SCORUM_MIN_PER_BLOCK_REWARD
Definition: config.hpp:118
#define SCORUM_BLOCKS_PER_DAY
Definition: config.hpp:173
uint16_t percent_type
Definition: types.hpp:69
Definition: asset.cpp:15
share_type amount
Definition: asset.hpp:31