BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
vesting_balance_object.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 
28 
29 #include <boost/multi_index/composite_key.hpp>
30 #include <boost/multi_index/hashed_index.hpp>
31 #include <boost/multi_index/identity.hpp>
32 
33 #include <fc/static_variant.hpp>
34 #include <fc/uint128.hpp>
35 
36 namespace graphene { namespace chain {
37  using namespace graphene::db;
38  using namespace graphene::protocol;
39 
41  {
43  asset _balance,
44  fc::time_point_sec _now,
45  asset _amount)
46  : balance(_balance), now(_now), amount(_amount) {}
47 
51  };
52 
62  {
66  uint32_t vesting_cliff_seconds = 0;
68  uint32_t vesting_duration_seconds = 0;
71 
72  asset get_allowed_withdraw(const vesting_policy_context& ctx)const;
73  bool is_deposit_allowed(const vesting_policy_context& ctx)const;
74  bool is_deposit_vested_allowed(const vesting_policy_context&)const { return false; }
75  bool is_withdraw_allowed(const vesting_policy_context& ctx)const;
76  void on_deposit(const vesting_policy_context& ctx);
78  { FC_THROW( "May not deposit vested into a linear vesting balance." ); }
79  void on_withdraw(const vesting_policy_context& ctx);
80  };
81 
90  {
91  uint32_t vesting_seconds = 0;
92  fc::uint128_t coin_seconds_earned;
96 
102  fc::uint128_t compute_coin_seconds_earned(const vesting_policy_context& ctx)const;
103 
109  void update_coin_seconds_earned(const vesting_policy_context& ctx);
110 
111  asset get_allowed_withdraw(const vesting_policy_context& ctx)const;
112  bool is_deposit_allowed(const vesting_policy_context& ctx)const;
113  bool is_deposit_vested_allowed(const vesting_policy_context& ctx)const;
114  bool is_withdraw_allowed(const vesting_policy_context& ctx)const;
115  void on_deposit(const vesting_policy_context& ctx);
116  void on_deposit_vested(const vesting_policy_context& ctx);
117  void on_withdraw(const vesting_policy_context& ctx);
118  };
119 
127  {
128  asset get_allowed_withdraw(const vesting_policy_context& ctx)const;
129  bool is_deposit_allowed(const vesting_policy_context& ctx)const;
130  bool is_deposit_vested_allowed(const vesting_policy_context&)const { return false; }
131  bool is_withdraw_allowed(const vesting_policy_context& ctx)const;
132  void on_deposit(const vesting_policy_context& ctx);
133  void on_deposit_vested(const vesting_policy_context&);
134  void on_withdraw(const vesting_policy_context& ctx);
135  };
136 
137  typedef fc::static_variant<
142 
144  cashback,
145  worker,
146  witness,
151  class vesting_balance_object : public abstract_object<vesting_balance_object>
152  {
153  public:
154  static const uint8_t space_id = protocol_ids;
155  static const uint8_t type_id = vesting_balance_object_type;
156 
158  account_id_type owner;
166 
168 
170  void deposit(const fc::time_point_sec& now, const asset& amount);
171  bool is_deposit_allowed(const fc::time_point_sec& now, const asset& amount)const;
172 
174  void deposit_vested(const fc::time_point_sec& now, const asset& amount);
175  bool is_deposit_vested_allowed(const fc::time_point_sec& now, const asset& amount)const;
176 
185  void withdraw(const fc::time_point_sec& now, const asset& amount);
186  bool is_withdraw_allowed(const fc::time_point_sec& now, const asset& amount)const;
187 
191  asset get_allowed_withdraw(const time_point_sec& now)const;
192  };
196  struct by_account;
197  // by_vesting_type index MUST NOT be used for iterating because order is not well-defined.
198  struct by_vesting_type;
199 
200 namespace detail {
201 
206  inline uint64_t vbo_mfs_hash(const account_id_type& account_id, const asset_id_type& asset_id)
207  {
208  return (asset_id.instance.value << 40) ^ account_id.instance.value;
209  }
210 
219  {
220  uint64_t operator()(const vesting_balance_object& vbo) const
221  {
223  {
224  return vbo_mfs_hash(vbo.owner, vbo.balance.asset_id);
225  }
226  return hash_value(vbo.id);
227  }
228  };
229 
238  {
239  bool operator() (const vesting_balance_object& lhs, const vesting_balance_object& rhs) const
240  {
242  ( lhs.balance_type == rhs.balance_type ) &&
243  ( lhs.owner == rhs.owner ) &&
244  ( lhs.balance.asset_id == rhs.balance.asset_id)
245  )
246  {
247  return true;
248  }
249  return ( lhs.id == rhs.id );
250  }
251  };
252 } // detail
253 
254  typedef multi_index_container<
256  indexed_by<
257  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id >
258  >,
259  ordered_non_unique< tag<by_account>,
260  member<vesting_balance_object, account_id_type, &vesting_balance_object::owner>
261  >,
262  hashed_unique< tag<by_vesting_type>,
263  identity<vesting_balance_object>,
266  >
267  >
273 
274 } } // graphene::chain
275 
277 
279  (begin_timestamp)
280  (vesting_cliff_seconds)
281  (vesting_duration_seconds)
282  (begin_balance)
283  )
284 
286  (vesting_seconds)
287  (start_claim)
288  (coin_seconds_earned)
289  (coin_seconds_earned_last_update)
290  )
291 
293 
295 
297  (owner)
298  (balance)
299  (policy)
300  (balance_type)
301  )
302 
304 
305 GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::chain::linear_vesting_policy )
306 GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::chain::cdd_vesting_policy )
307 GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::chain::vesting_balance_object )
generic_index< vesting_balance_object, vesting_balance_multi_index_type > vesting_balance_index
multi_index_container< vesting_balance_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type,&object::id > >, ordered_non_unique< tag< by_account >, member< vesting_balance_object, account_id_type,&vesting_balance_object::owner > >, hashed_unique< tag< by_vesting_type >, identity< vesting_balance_object >, detail::vesting_balance_object_hash, detail::vesting_balance_object_equal > > > vesting_balance_multi_index_type
fc::time_point_sec begin_timestamp
This is the time at which funds begin vesting.
vesting_balance_type balance_type
type of the vesting balance
fc::static_variant< linear_vesting_policy, cdd_vesting_policy, instant_vesting_policy > vesting_policy
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:386
Definition: api.cpp:56
#define MAP_OBJECT_ID_TO_TYPE(OBJECT)
Definition: object_id.hpp:98
uint64_t operator()(const vesting_balance_object &vbo) const
#define FC_THROW(...)
Definition: exception.hpp:366
vesting_policy policy
The vesting policy stores details on when funds vest, and controls when they may be withdrawn...
FC_REFLECT_DERIVED(graphene::app::extended_asset_object,(graphene::chain::asset_object),(total_in_collateral)(total_backing_collateral))
FC_REFLECT_TYPENAME(fc::log_message)
FC_REFLECT_ENUM(graphene::net::core_message_type_enum,(trx_message_type)(block_message_type)(core_message_type_first)(item_ids_inventory_message_type)(blockchain_item_ids_inventory_message_type)(fetch_blockchain_item_ids_message_type)(fetch_items_message_type)(item_not_available_message_type)(hello_message_type)(connection_accepted_message_type)(connection_rejected_message_type)(address_request_message_type)(address_message_type)(closing_connection_message_type)(current_time_request_message_type)(current_time_reply_message_type)(check_firewall_message_type)(check_firewall_reply_message_type)(get_current_connections_request_message_type)(get_current_connections_reply_message_type)(core_message_type_last))(different_chain)(already_connected)(connected_to_self)(not_accepting_connections)(blocked)(invalid_hello_message)(client_too_old))(inbound)(outbound))(firewalled)(not_firewalled))(unable_to_connect)(connection_successful)) namespace std
object_id_type id
Definition: object.hpp:73
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:73
share_type begin_balance
The total amount of asset to vest.
#define FC_REFLECT_EMPTY(TYPE)
Definition: reflect.hpp:393
bool is_deposit_vested_allowed(const vesting_policy_context &) const
Use the Curiously Recurring Template Pattern to automatically add the ability to clone, serialize, and move objects polymorphically.
Definition: object.hpp:90
uint64_t vbo_mfs_hash(const account_id_type &account_id, const asset_id_type &asset_id)
void on_deposit_vested(const vesting_policy_context &)
bool is_deposit_vested_allowed(const vesting_policy_context &) const
asset_id_type asset_id
Definition: asset.hpp:39
defines vesting in terms of coin-days accrued which allows for dynamic deposit/withdraw ...
Linear vesting balance with cliff.
account_id_type owner
Account which owns and may withdraw from this vesting balance.
vesting_policy_context(asset _balance, fc::time_point_sec _now, asset _amount)