Scorum
set_withdraw_scorumpower_route_evaluators.cpp
Go to the documentation of this file.
2 
3 #include <fc/exception/exception.hpp>
4 
6 
11 
15 
16 namespace scorum {
17 namespace chain {
18 
20 {
21 public:
22  set_withdraw_scorumpower_route_evaluator_impl(data_service_factory_i& services)
23  : _withdraw_scorumpower_route_service(services.withdraw_scorumpower_route_service())
24  , _withdraw_scorumpower_route_statistic_service(services.withdraw_scorumpower_route_statistic_service())
25  {
26  }
27 
28  template <typename FromObjectType, typename ToObjectType>
29  void do_apply(const FromObjectType& from_object,
30  const ToObjectType& to_object,
31  const uint16_t percent,
32  bool auto_vest = false)
33  {
34  if (!_withdraw_scorumpower_route_statistic_service.is_exists(from_object.id))
35  {
36  _withdraw_scorumpower_route_statistic_service.create(
37  [&](withdraw_scorumpower_route_statistic_object& wvdso) { wvdso.from_id = from_object.id; });
38  }
39 
41  = _withdraw_scorumpower_route_statistic_service.get(from_object.id);
42 
43  if (!_withdraw_scorumpower_route_service.is_exists(from_object.id, to_object.id))
44  {
45  FC_ASSERT(percent != 0, "Cannot create a 0% destination.");
46 
47  FC_ASSERT(statistic.withdraw_routes < SCORUM_MAX_WITHDRAW_ROUTES,
48  "Account already has the maximum number of routes.");
49 
50  _withdraw_scorumpower_route_service.create([&](withdraw_scorumpower_route_object& wvdo) {
51  wvdo.from_id = from_object.id;
52  wvdo.to_id = to_object.id;
53  wvdo.percent = percent;
54  wvdo.auto_vest = auto_vest;
55  });
56 
57  _withdraw_scorumpower_route_statistic_service.update(
58  statistic, [&](withdraw_scorumpower_route_statistic_object& wvdso) { wvdso.withdraw_routes++; });
59  }
60  else if (percent == 0)
61  {
62  const auto& wvr = _withdraw_scorumpower_route_service.get(from_object.id, to_object.id);
63  _withdraw_scorumpower_route_service.remove(wvr);
64 
65  _withdraw_scorumpower_route_statistic_service.update(
66  statistic, [&](withdraw_scorumpower_route_statistic_object& wvdso) { wvdso.withdraw_routes--; });
67 
68  if (!statistic.withdraw_routes)
69  {
70  _withdraw_scorumpower_route_statistic_service.remove(statistic);
71  }
72  }
73  else
74  {
75  const auto& wvr = _withdraw_scorumpower_route_service.get(from_object.id, to_object.id);
76 
77  _withdraw_scorumpower_route_service.update(wvr, [&](withdraw_scorumpower_route_object& wvdo) {
78  wvdo.percent = percent;
79  wvdo.auto_vest = auto_vest;
80  });
81  }
82 
83  uint16_t total_percent = _withdraw_scorumpower_route_service.total_percent(from_object.id);
84 
85  FC_ASSERT(total_percent <= SCORUM_100_PERCENT,
86  "More than 100% of vesting withdrawals allocated to destinations.");
87  }
88 
89 private:
90  withdraw_scorumpower_route_service_i& _withdraw_scorumpower_route_service;
91  withdraw_scorumpower_route_statistic_service_i& _withdraw_scorumpower_route_statistic_service;
92 };
93 
94 // set_withdraw_scorumpower_route_to_account_evaluator
95 
97  data_service_factory_i& services)
98  : evaluator_impl<data_service_factory_i, set_withdraw_scorumpower_route_to_account_evaluator>(services)
100  , _account_service(services.account_service())
101 {
102 }
103 
105 {
106  // required for _impl correct compilation
107 }
108 
111 {
112  _account_service.check_account_existence(op.from_account);
113  _account_service.check_account_existence(op.to_account);
114 
115  const auto& from_account = _account_service.get_account(op.from_account);
116  const auto& to_account = _account_service.get_account(op.to_account);
117 
118  _impl->do_apply(from_account, to_account, op.percent, op.auto_vest);
119 }
120 
121 // set_withdraw_scorumpower_route_to_dev_pool_evaluator
122 
124  data_service_factory_i& services)
125  : evaluator_impl<data_service_factory_i, set_withdraw_scorumpower_route_to_dev_pool_evaluator>(services)
126  , _impl(new set_withdraw_scorumpower_route_evaluator_impl(services))
127  , _account_service(services.account_service())
128  , _dev_pool_service(services.dev_pool_service())
129 {
130 }
131 
133 {
134  // required for _impl correct compilation
135 }
136 
139 {
140  _account_service.check_account_existence(op.from_account);
141 
142  const auto& from_account = _account_service.get_account(op.from_account);
143  const auto& to_pool = _dev_pool_service.get();
144 
145  _impl->do_apply(from_account, to_pool, op.percent, op.auto_vest);
146 }
147 
148 //
149 
151  const account_name_type& account,
152  uint16_t percent,
153  bool auto_vest)
154  : _services(services)
155  , _account(account)
156  , _percent(percent)
157  , _auto_vest(auto_vest)
158 {
159 }
160 
162 {
164 
165  dev_pool_service_i& dev_pool_service = ctx.services().dev_pool_service();
166  account_service_i& account_service = ctx.services().account_service();
167 
168  account_service.check_account_existence(ctx.account());
169 
170  const auto& from_pool = dev_pool_service.get();
171  const auto& to_account = account_service.get_account(ctx.account());
172 
173  impl.do_apply(from_pool, to_account, ctx.percent(), ctx.auto_vest());
174 }
175 }
176 }
set_withdraw_scorumpower_route_context(data_service_factory_i &services, const account_name_type &account, uint16_t percent, bool auto_vest)
void do_apply(const FromObjectType &from_object, const ToObjectType &to_object, const uint16_t percent, bool auto_vest=false)
#define SCORUM_MAX_WITHDRAW_ROUTES
Definition: config.hpp:188
#define SCORUM_100_PERCENT
Definition: config.hpp:200
fc::fixed_string_16 account_name_type
Definition: types.hpp:62
Definition: asset.cpp:15
virtual const account_object & get_account(const account_name_type &) const =0
virtual void check_account_existence(const account_name_type &, const optional< const char * > &context_type_name=optional< const char * >()) const =0
virtual void update(const modifier_type &modifier)=0
virtual const object_type & get() const =0
virtual const object_type & create(const modifier_type &modifier)=0
virtual const withdraw_scorumpower_route_object & get(const account_id_type &from, const account_id_type &to) const =0
virtual bool is_exists(const account_id_type &from, const account_id_type &to) const =0
virtual uint16_t total_percent(const account_id_type &from) const =0
virtual bool is_exists(const account_id_type &from) const =0
virtual const withdraw_scorumpower_route_statistic_object & get(const account_id_type &from) const =0