BitShares-Core  6.1.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  protocol_ids, vesting_balance_object_type>
153  {
154  public:
156  account_id_type owner;
164 
166 
168  void deposit(const fc::time_point_sec& now, const asset& amount);
169  bool is_deposit_allowed(const fc::time_point_sec& now, const asset& amount)const;
170 
172  void deposit_vested(const fc::time_point_sec& now, const asset& amount);
173  bool is_deposit_vested_allowed(const fc::time_point_sec& now, const asset& amount)const;
174 
183  void withdraw(const fc::time_point_sec& now, const asset& amount);
184  bool is_withdraw_allowed(const fc::time_point_sec& now, const asset& amount)const;
185 
189  asset get_allowed_withdraw(const time_point_sec& now)const;
190  };
194  struct by_account;
195  // by_vesting_type index MUST NOT be used for iterating because order is not well-defined.
196  struct by_vesting_type;
197 
198 namespace detail {
199 
204  inline uint64_t vbo_mfs_hash(const account_id_type& account_id, const asset_id_type& asset_id)
205  {
206  return (asset_id.instance.value << 40) ^ account_id.instance.value;
207  }
208 
217  {
218  uint64_t operator()(const vesting_balance_object& vbo) const
219  {
221  {
222  return vbo_mfs_hash(vbo.owner, vbo.balance.asset_id);
223  }
224  return hash_value(vbo.id);
225  }
226  };
227 
236  {
237  bool operator() (const vesting_balance_object& lhs, const vesting_balance_object& rhs) const
238  {
240  ( lhs.balance_type == rhs.balance_type ) &&
241  ( lhs.owner == rhs.owner ) &&
242  ( lhs.balance.asset_id == rhs.balance.asset_id)
243  )
244  {
245  return true;
246  }
247  return ( lhs.id == rhs.id );
248  }
249  };
250 } // detail
251 
252  typedef multi_index_container<
254  indexed_by<
255  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id >
256  >,
257  ordered_non_unique< tag<by_account>,
258  member<vesting_balance_object, account_id_type, &vesting_balance_object::owner>
259  >,
260  hashed_unique< tag<by_vesting_type>,
261  identity<vesting_balance_object>,
264  >
265  >
271 
272 } } // graphene::chain
273 
275 
277  (begin_timestamp)
278  (vesting_cliff_seconds)
279  (vesting_duration_seconds)
280  (begin_balance)
281  )
282 
284  (vesting_seconds)
285  (start_claim)
286  (coin_seconds_earned)
287  (coin_seconds_earned_last_update)
288  )
289 
291 
293 
295  (owner)
296  (balance)
297  (policy)
298  (balance_type)
299  )
300 
302 
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:388
bool is_deposit_vested_allowed(const vesting_policy_context &) const
Definition: api.cpp:48
#define MAP_OBJECT_ID_TO_TYPE(OBJECT)
Definition: object_id.hpp:93
bool is_deposit_vested_allowed(const vesting_policy_context &) const
vesting_policy policy
The vesting policy stores details on when funds vest, and controls when they may be withdrawn...
uint64_t operator()(const vesting_balance_object &vbo) const
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
#define FC_THROW( ...)
Definition: exception.hpp:366
object_id_type id
Definition: object.hpp:69
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:85
share_type begin_balance
The total amount of asset to vest.
#define FC_REFLECT_EMPTY(TYPE)
Definition: reflect.hpp:395
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 &)
asset_id_type asset_id
Definition: asset.hpp:37
#define FC_REFLECT_DERIVED(TYPE, INHERITS, MEMBERS)
Specializes fc::reflector for TYPE where type inherits other reflected classes.
Definition: reflect.hpp:305
defines vesting in terms of coin-days accrued which allows for dynamic deposit/withdraw ...
base for all database objects
Definition: object.hpp:61
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)