BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
vesting_balance_evaluator.cpp
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 
29 #include <graphene/chain/hardfork.hpp>
30 
31 namespace graphene { namespace chain {
32 
34 { try {
35  const database& d = db();
36 
37  const account_object& creator_account = op.creator( d );
38  /* const account_object& owner_account = */ op.owner( d );
39 
40  // TODO: Check asset authorizations and withdrawals
41 
42  FC_ASSERT( op.amount.amount > 0 );
43  FC_ASSERT( d.get_balance( creator_account.id, op.amount.asset_id ) >= op.amount );
44  FC_ASSERT( !op.amount.asset_id(d).is_transfer_restricted() );
45 
46  return void_result();
47 } FC_CAPTURE_AND_RETHROW( (op) ) }
48 
50 {
51  typedef void result_type;
52 
54  const share_type& begin_balance,
55  const fc::time_point_sec& n ):p(po),init_balance(begin_balance),now(n){}
56 
60 
62  {
63  linear_vesting_policy policy;
67  policy.begin_balance = init_balance;
68  p = policy;
69  }
70 
72  {
73  cdd_vesting_policy policy;
75  policy.start_claim = i.start_claim;
76  policy.coin_seconds_earned = 0;
78  p = policy;
79  }
80 
82  {
84  }
85 
86 };
87 
89 { try {
90  database& d = db();
91  const time_point_sec now = d.head_block_time();
92 
93  FC_ASSERT( d.get_balance( op.creator, op.amount.asset_id ) >= op.amount );
94  d.adjust_balance( op.creator, -op.amount );
95 
97  {
98  //WARNING: The logic to create a vesting balance object is replicated in vesting_balance_worker_type::initializer::init.
99  // If making changes to this logic, check if those changes should also be made there as well.
100  obj.owner = op.owner;
101  obj.balance = op.amount;
102  op.policy.visit( init_policy_visitor( obj.policy, op.amount.amount, now ) );
103  } );
104 
105 
106  return vbo.id;
107 } FC_CAPTURE_AND_RETHROW( (op) ) }
108 
110 { try {
111  const database& d = db();
112  const time_point_sec now = d.head_block_time();
113 
114  const vesting_balance_object& vbo = op.vesting_balance( d );
115  FC_ASSERT( op.owner == vbo.owner, "", ("op.owner", op.owner)("vbo.owner", vbo.owner) );
116  FC_ASSERT( vbo.is_withdraw_allowed( now, op.amount ), "", ("now", now)("op", op)("vbo", vbo) );
117  assert( op.amount <= vbo.balance ); // is_withdraw_allowed should fail before this check is reached
118 
119  /* const account_object& owner_account = */ op.owner( d );
120  // TODO: Check asset authorizations and withdrawals
121  return void_result();
122 } FC_CAPTURE_AND_RETHROW( (op) ) }
123 
125 { try {
126  database& d = db();
127  const time_point_sec now = d.head_block_time();
128 
129  const vesting_balance_object& vbo = op.vesting_balance( d );
130 
131  // Allow zero balance objects to stick around, (1) to comply
132  // with the chain's "objects live forever" design principle, (2)
133  // if it's cashback or worker, it'll be filled up again.
134 
135  d.modify( vbo, [&]( vesting_balance_object& vbo )
136  {
137  vbo.withdraw( now, op.amount );
138  } );
139 
140  d.adjust_balance( op.owner, op.amount );
141 
142  // TODO: Check asset authorizations and withdrawals
143  return void_result();
144 } FC_CAPTURE_AND_RETHROW( (op) ) }
145 
146 } } // graphene::chain
init_policy_visitor(vesting_policy &po, const share_type &begin_balance, const fc::time_point_sec &n)
fc::time_point_sec begin_timestamp
This is the time at which funds begin vesting.
void modify(const T &obj, const Lambda &m)
uint32_t vesting_duration_seconds
Duration of the vesting period, in seconds. Must be greater than 0 and greater than vesting_cliff_sec...
void adjust_balance(account_id_type account, asset delta)
Adjust a particular account&#39;s balance in a given asset by a delta.
Definition: db_balance.cpp:54
uint32_t vesting_cliff_seconds
No amount may be withdrawn before this many seconds of the vesting period have elapsed.
This class represents an account on the object graphAccounts are the primary unit of authority on the...
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
void operator()(const cdd_vesting_policy_initializer &i) const
account_id_type owner
Must be vesting_balance.owner.
Definition: vesting.hpp:107
Definition: api.cpp:56
asset get_balance(account_id_type owner, asset_id_type asset_id) const
Retrieve a particular account&#39;s balance in a given asset.
Definition: db_balance.cpp:35
visitor::result_type visit(visitor &v)
void_result do_apply(const vesting_balance_withdraw_operation &op)
object_id_type id
Definition: object.hpp:73
time_point_sec head_block_time() const
Definition: db_getter.cpp:64
void_result do_evaluate(const vesting_balance_withdraw_operation &op)
Create a vesting balance.The chain allows a user to create a vesting balance. Normally, vesting balances are created automatically as part of cashback and worker operations. This operation allows vesting balances to be created manually as well.
Definition: vesting.hpp:74
share_type begin_balance
The total amount of asset to vest.
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:478
void operator()(const instant_vesting_policy_initializer &i) const
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
void withdraw(const fc::time_point_sec &now, const asset &amount)
object_id_type do_apply(const vesting_balance_create_operation &op)
account_id_type owner
Who is able to withdraw the balance.
Definition: vesting.hpp:80
Withdraw from a vesting balance.Withdrawal from a not-completely-mature vesting balance will result i...
Definition: vesting.hpp:101
asset_id_type asset_id
Definition: asset.hpp:39
defines vesting in terms of coin-days accrued which allows for dynamic deposit/withdraw ...
bool is_withdraw_allowed(const fc::time_point_sec &now, const asset &amount) const
void operator()(const linear_vesting_policy_initializer &i) const
account_id_type creator
Who provides funds initially.
Definition: vesting.hpp:79
const T & create(F &&constructor)
Linear vesting balance with cliff.
void_result do_evaluate(const vesting_balance_create_operation &op)
account_id_type owner
Account which owns and may withdraw from this vesting balance.