Scorum
process_vesting_withdrawals.cpp
Go to the documentation of this file.
2 
6 
8 
10 
11 namespace scorum {
12 namespace chain {
13 namespace database_ns {
14 
16 {
17  debug_log(ctx.get_block_info(), "process_vesting_withdrawals BEGIN");
18 
19  withdraw_scorumpower_service_i& withdraw_scorumpower_service = ctx.services().withdraw_scorumpower_service();
20  withdraw_scorumpower_route_service_i& withdraw_scorumpower_route_service
21  = ctx.services().withdraw_scorumpower_route_service();
22  dynamic_global_property_service_i& dprops_service = ctx.services().dynamic_global_property_service();
23 
24  withdrawable_actors_impl actors_impl(ctx);
25 
26  for (const withdraw_scorumpower_object& wvo :
27  withdraw_scorumpower_service.get_until(dprops_service.head_block_time()))
28  {
29  asset scorumpower = actors_impl.get_available_scorumpower(wvo.from_id);
30  asset to_withdraw = asset(0, SP_SYMBOL);
31 
32  // use to_withdraw value in (0, wvo.vesting_withdraw_rate]
33  if (wvo.to_withdraw - wvo.withdrawn < wvo.vesting_withdraw_rate)
34  {
35  to_withdraw = std::min(scorumpower, wvo.to_withdraw % wvo.vesting_withdraw_rate);
36  }
37  else
38  {
39  to_withdraw = std::min(scorumpower, wvo.vesting_withdraw_rate);
40  }
41 
42  if (to_withdraw.amount <= 0) // invalid data for vesting
43  {
44  withdraw_scorumpower_service.remove(wvo);
45  continue;
46  }
47 
48  actors_impl.decrease_sp(wvo.from_id, to_withdraw);
49 
50  asset deposited_scorumpower = asset(0, SP_SYMBOL);
51 
52  for (const withdraw_scorumpower_route_object& wvro : withdraw_scorumpower_route_service.get_all(wvo.from_id))
53  {
54  asset to_deposit = to_withdraw * utils::make_fraction(wvro.percent, SCORUM_100_PERCENT);
55 
56  if (to_deposit.amount > 0)
57  {
58  deposited_scorumpower += to_deposit;
59 
60  if (wvro.auto_vest) // withdraw SP
61  {
62  actors_impl.update_statistic(wvro.from_id, wvro.to_id, to_deposit);
63  actors_impl.increase_sp(wvro.to_id, to_deposit);
64  }
65  else // convert SP to SCR and withdraw SCR
66  {
67  auto converted_scorum = asset(to_deposit.amount, SCORUM_SYMBOL);
68 
69  actors_impl.update_statistic(wvro.from_id, wvro.to_id, converted_scorum);
70  actors_impl.increase_scr(wvro.to_id, converted_scorum);
71  }
72  }
73  }
74 
75  // withdraw the rest as SCR or process vesting without routing
76 
77  asset to_convert = to_withdraw - deposited_scorumpower;
78  FC_ASSERT(to_convert.amount >= 0, "Deposited more scorum power than were supposed to be withdrawn");
79 
80  asset converted_scorum = asset(to_convert.amount, SCORUM_SYMBOL);
81 
82  actors_impl.update_statistic(wvo.from_id, wvo.from_id, converted_scorum);
83  actors_impl.increase_scr(wvo.from_id, converted_scorum);
84 
85  scorumpower -= to_withdraw;
86 
87  withdraw_scorumpower_service.update(wvo, [&](withdraw_scorumpower_object& o) {
88  o.withdrawn += to_withdraw;
90  });
91 
92  if (wvo.withdrawn >= wvo.to_withdraw || scorumpower.amount == 0)
93  {
94  actors_impl.update_statistic(wvo.from_id);
95  withdraw_scorumpower_service.remove(wvo);
96  }
97  }
98 
99  debug_log(ctx.get_block_info(), "process_vesting_withdrawals END");
100 }
101 }
102 }
103 }
data_service_factory_i & services() const
Definition: block_tasks.hpp:25
void update_statistic(const withdrawable_id_type &from, const withdrawable_id_type &to, const asset &amount)
asset get_available_scorumpower(const withdrawable_id_type &from)
void decrease_sp(const withdrawable_id_type &id, const asset &amount)
void increase_sp(const withdrawable_id_type &id, const asset &amount)
void increase_scr(const withdrawable_id_type &id, const asset &amount)
time_point_sec next_vesting_withdrawal
after every withdrawal this is incremented by 1 week
#define SP_SYMBOL
Definition: config.hpp:104
#define SCORUM_SYMBOL
Definition: config.hpp:102
#define SCORUM_100_PERCENT
Definition: config.hpp:200
#define SCORUM_VESTING_WITHDRAW_INTERVAL_SECONDS
Definition: config.hpp:156
#define debug_log(CTX, FORMAT,...)
Definition: debug_log.hpp:3
Definition: asset.cpp:15
virtual void update(const modifier_type &modifier)=0
virtual fc::time_point_sec head_block_time() const =0
virtual withdraw_scorumpower_route_refs_type get_all(const withdrawable_id_type &from) const =0
virtual withdraw_scorumpower_refs_type get_until(const time_point_sec &until) const =0
share_type amount
Definition: asset.hpp:31