2015-06-08 15:50:35 +00:00
|
|
|
/*
|
2015-10-12 17:48:40 +00:00
|
|
|
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
|
|
|
|
|
*
|
2016-01-06 09:51:18 +00:00
|
|
|
* The MIT License
|
2015-10-12 17:48:40 +00:00
|
|
|
*
|
2016-01-06 09:51:18 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2015-10-12 17:48:40 +00:00
|
|
|
*
|
2016-01-06 09:51:18 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
2015-10-12 17:02:59 +00:00
|
|
|
*
|
2016-01-06 09:51:18 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
2015-06-08 15:50:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <graphene/chain/vesting_balance_object.hpp>
|
|
|
|
|
|
2019-05-13 22:18:12 +00:00
|
|
|
#include <fc/io/raw.hpp>
|
|
|
|
|
|
2015-06-08 15:50:35 +00:00
|
|
|
namespace graphene { namespace chain {
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
inline bool sum_below_max_shares(const asset& a, const asset& b)
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
2015-06-11 17:54:42 +00:00
|
|
|
assert(GRAPHENE_MAX_SHARE_SUPPLY + GRAPHENE_MAX_SHARE_SUPPLY > GRAPHENE_MAX_SHARE_SUPPLY);
|
|
|
|
|
return (a.amount <= GRAPHENE_MAX_SHARE_SUPPLY)
|
2015-06-08 15:50:35 +00:00
|
|
|
&& ( b.amount <= GRAPHENE_MAX_SHARE_SUPPLY)
|
2015-06-30 20:42:41 +00:00
|
|
|
&& ((a.amount + b.amount) <= GRAPHENE_MAX_SHARE_SUPPLY);
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-01 20:56:24 +00:00
|
|
|
asset linear_vesting_policy::get_allowed_withdraw( const vesting_policy_context& ctx )const
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
2015-07-01 20:56:24 +00:00
|
|
|
share_type allowed_withdraw = 0;
|
2015-06-08 15:50:35 +00:00
|
|
|
|
2015-07-01 20:56:24 +00:00
|
|
|
if( ctx.now > begin_timestamp )
|
|
|
|
|
{
|
|
|
|
|
const auto elapsed_seconds = (ctx.now - begin_timestamp).to_seconds();
|
|
|
|
|
assert( elapsed_seconds > 0 );
|
2015-06-08 15:50:35 +00:00
|
|
|
|
2015-07-01 20:56:24 +00:00
|
|
|
if( elapsed_seconds >= vesting_cliff_seconds )
|
|
|
|
|
{
|
2019-10-17 16:39:44 +00:00
|
|
|
// BLOCKBACK-154 fix, Begin balance for linear vesting applies only to initial account balance from genesis
|
|
|
|
|
// So, for any GPOS vesting, the begin balance would be 0 and should be able to withdraw balance amount based on lockin period
|
|
|
|
|
if(begin_balance == 0)
|
2015-07-01 20:56:24 +00:00
|
|
|
{
|
2019-10-17 16:39:44 +00:00
|
|
|
allowed_withdraw = ctx.balance.amount;
|
|
|
|
|
return asset( allowed_withdraw, ctx.balance.asset_id );
|
2015-07-01 20:56:24 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-10-17 16:39:44 +00:00
|
|
|
share_type total_vested = 0;
|
|
|
|
|
if( elapsed_seconds < vesting_duration_seconds )
|
|
|
|
|
{
|
|
|
|
|
total_vested = (fc::uint128_t( begin_balance.value ) * elapsed_seconds / vesting_duration_seconds).to_uint64();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
total_vested = begin_balance;
|
|
|
|
|
}
|
|
|
|
|
assert( total_vested >= 0 );
|
2015-06-08 15:50:35 +00:00
|
|
|
|
2019-10-17 16:39:44 +00:00
|
|
|
const share_type withdrawn_already = begin_balance - ctx.balance.amount;
|
|
|
|
|
assert( withdrawn_already >= 0 );
|
2015-06-08 15:50:35 +00:00
|
|
|
|
2019-10-17 16:39:44 +00:00
|
|
|
allowed_withdraw = total_vested - withdrawn_already;
|
|
|
|
|
assert( allowed_withdraw >= 0 );
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-01 20:56:24 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-25 17:09:19 +00:00
|
|
|
return asset( allowed_withdraw, ctx.balance.asset_id );
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
void linear_vesting_policy::on_deposit(const vesting_policy_context& ctx)
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
bool linear_vesting_policy::is_deposit_allowed(const vesting_policy_context& ctx)const
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
|
|
|
|
return (ctx.amount.asset_id == ctx.balance.asset_id)
|
2015-06-11 17:54:42 +00:00
|
|
|
&& sum_below_max_shares(ctx.amount, ctx.balance);
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
void linear_vesting_policy::on_withdraw(const vesting_policy_context& ctx)
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
bool linear_vesting_policy::is_withdraw_allowed(const vesting_policy_context& ctx)const
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
2015-07-01 20:56:24 +00:00
|
|
|
return (ctx.amount.asset_id == ctx.balance.asset_id)
|
|
|
|
|
&& (ctx.amount <= get_allowed_withdraw(ctx));
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
fc::uint128_t cdd_vesting_policy::compute_coin_seconds_earned(const vesting_policy_context& ctx)const
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
2015-06-11 17:54:42 +00:00
|
|
|
assert(ctx.now >= coin_seconds_earned_last_update);
|
2015-06-08 15:50:35 +00:00
|
|
|
int64_t delta_seconds = (ctx.now - coin_seconds_earned_last_update).to_seconds();
|
2015-06-11 17:54:42 +00:00
|
|
|
assert(delta_seconds >= 0);
|
2015-06-08 15:50:35 +00:00
|
|
|
|
|
|
|
|
fc::uint128_t delta_coin_seconds = ctx.balance.amount.value;
|
|
|
|
|
delta_coin_seconds *= delta_seconds;
|
|
|
|
|
|
|
|
|
|
fc::uint128_t coin_seconds_earned_cap = ctx.balance.amount.value;
|
2015-10-27 19:00:57 +00:00
|
|
|
coin_seconds_earned_cap *= std::max(vesting_seconds, 1u);
|
2015-06-08 15:50:35 +00:00
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
return std::min(coin_seconds_earned + delta_coin_seconds, coin_seconds_earned_cap);
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
void cdd_vesting_policy::update_coin_seconds_earned(const vesting_policy_context& ctx)
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
2015-06-11 17:54:42 +00:00
|
|
|
coin_seconds_earned = compute_coin_seconds_earned(ctx);
|
2015-06-08 15:50:35 +00:00
|
|
|
coin_seconds_earned_last_update = ctx.now;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
asset cdd_vesting_policy::get_allowed_withdraw(const vesting_policy_context& ctx)const
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
2015-06-23 14:12:53 +00:00
|
|
|
if(ctx.now <= start_claim)
|
|
|
|
|
return asset(0, ctx.balance.asset_id);
|
2015-06-11 17:54:42 +00:00
|
|
|
fc::uint128_t cs_earned = compute_coin_seconds_earned(ctx);
|
2015-10-27 19:00:57 +00:00
|
|
|
fc::uint128_t withdraw_available = cs_earned / std::max(vesting_seconds, 1u);
|
2015-06-11 17:54:42 +00:00
|
|
|
assert(withdraw_available <= ctx.balance.amount.value);
|
|
|
|
|
return asset(withdraw_available.to_uint64(), ctx.balance.asset_id);
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
void cdd_vesting_policy::on_deposit(const vesting_policy_context& ctx)
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
2015-06-11 17:54:42 +00:00
|
|
|
update_coin_seconds_earned(ctx);
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
void cdd_vesting_policy::on_deposit_vested(const vesting_policy_context& ctx)
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
2015-06-11 17:54:42 +00:00
|
|
|
on_deposit(ctx);
|
2015-10-27 19:00:57 +00:00
|
|
|
coin_seconds_earned += ctx.amount.amount.value * std::max(vesting_seconds, 1u);
|
2015-06-11 17:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void cdd_vesting_policy::on_withdraw(const vesting_policy_context& ctx)
|
|
|
|
|
{
|
|
|
|
|
update_coin_seconds_earned(ctx);
|
2015-06-08 15:50:35 +00:00
|
|
|
fc::uint128_t coin_seconds_needed = ctx.amount.amount.value;
|
2015-10-27 19:00:57 +00:00
|
|
|
coin_seconds_needed *= std::max(vesting_seconds, 1u);
|
2015-06-08 15:50:35 +00:00
|
|
|
// is_withdraw_allowed should forbid any withdrawal that
|
|
|
|
|
// would trigger this assert
|
2015-06-11 17:54:42 +00:00
|
|
|
assert(coin_seconds_needed <= coin_seconds_earned);
|
2015-06-08 15:50:35 +00:00
|
|
|
|
|
|
|
|
coin_seconds_earned -= coin_seconds_needed;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
bool cdd_vesting_policy::is_deposit_allowed(const vesting_policy_context& ctx)const
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
|
|
|
|
return (ctx.amount.asset_id == ctx.balance.asset_id)
|
2015-06-11 17:54:42 +00:00
|
|
|
&& sum_below_max_shares(ctx.amount, ctx.balance);
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
bool cdd_vesting_policy::is_deposit_vested_allowed(const vesting_policy_context& ctx) const
|
2015-06-08 15:50:35 +00:00
|
|
|
{
|
2015-06-11 17:54:42 +00:00
|
|
|
return is_deposit_allowed(ctx);
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
bool cdd_vesting_policy::is_withdraw_allowed(const vesting_policy_context& ctx)const
|
|
|
|
|
{
|
|
|
|
|
return (ctx.amount <= get_allowed_withdraw(ctx));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define VESTING_VISITOR(NAME, MAYBE_CONST) \
|
2015-06-08 15:50:35 +00:00
|
|
|
struct NAME ## _visitor \
|
|
|
|
|
{ \
|
|
|
|
|
typedef decltype( \
|
|
|
|
|
std::declval<linear_vesting_policy>().NAME( \
|
|
|
|
|
std::declval<vesting_policy_context>()) \
|
2015-06-11 17:54:42 +00:00
|
|
|
) result_type; \
|
2015-06-08 15:50:35 +00:00
|
|
|
\
|
|
|
|
|
NAME ## _visitor( \
|
|
|
|
|
const asset& balance, \
|
|
|
|
|
const time_point_sec& now, \
|
|
|
|
|
const asset& amount \
|
2015-06-11 17:54:42 +00:00
|
|
|
) \
|
|
|
|
|
: ctx(balance, now, amount) {} \
|
2015-06-08 15:50:35 +00:00
|
|
|
\
|
|
|
|
|
template< typename Policy > \
|
|
|
|
|
result_type \
|
2015-06-11 17:54:42 +00:00
|
|
|
operator()(MAYBE_CONST Policy& policy) MAYBE_CONST \
|
2015-06-08 15:50:35 +00:00
|
|
|
{ \
|
2015-06-11 17:54:42 +00:00
|
|
|
return policy.NAME(ctx); \
|
2015-06-08 15:50:35 +00:00
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
vesting_policy_context ctx; \
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
VESTING_VISITOR(on_deposit,);
|
|
|
|
|
VESTING_VISITOR(on_deposit_vested,);
|
|
|
|
|
VESTING_VISITOR(on_withdraw,);
|
|
|
|
|
VESTING_VISITOR(is_deposit_allowed, const);
|
|
|
|
|
VESTING_VISITOR(is_deposit_vested_allowed, const);
|
|
|
|
|
VESTING_VISITOR(is_withdraw_allowed, const);
|
2015-09-01 18:19:34 +00:00
|
|
|
VESTING_VISITOR(get_allowed_withdraw, const);
|
2015-06-08 15:50:35 +00:00
|
|
|
|
|
|
|
|
bool vesting_balance_object::is_deposit_allowed(const time_point_sec& now, const asset& amount)const
|
|
|
|
|
{
|
2015-06-11 17:54:42 +00:00
|
|
|
return policy.visit(is_deposit_allowed_visitor(balance, now, amount));
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool vesting_balance_object::is_withdraw_allowed(const time_point_sec& now, const asset& amount)const
|
|
|
|
|
{
|
2015-06-11 17:54:42 +00:00
|
|
|
bool result = policy.visit(is_withdraw_allowed_visitor(balance, now, amount));
|
2015-06-08 15:50:35 +00:00
|
|
|
// if some policy allows you to withdraw more than your balance,
|
|
|
|
|
// there's a programming bug in the policy algorithm
|
2015-06-11 17:54:42 +00:00
|
|
|
assert((amount <= balance) || (!result));
|
2015-06-08 15:50:35 +00:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vesting_balance_object::deposit(const time_point_sec& now, const asset& amount)
|
|
|
|
|
{
|
2015-06-11 17:54:42 +00:00
|
|
|
on_deposit_visitor vtor(balance, now, amount);
|
|
|
|
|
policy.visit(vtor);
|
2015-06-08 15:50:35 +00:00
|
|
|
balance += amount;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:54:42 +00:00
|
|
|
void vesting_balance_object::deposit_vested(const time_point_sec& now, const asset& amount)
|
|
|
|
|
{
|
|
|
|
|
on_deposit_vested_visitor vtor(balance, now, amount);
|
|
|
|
|
policy.visit(vtor);
|
|
|
|
|
balance += amount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool vesting_balance_object::is_deposit_vested_allowed(const time_point_sec& now, const asset& amount) const
|
|
|
|
|
{
|
|
|
|
|
return policy.visit(is_deposit_vested_allowed_visitor(balance, now, amount));
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-08 15:50:35 +00:00
|
|
|
void vesting_balance_object::withdraw(const time_point_sec& now, const asset& amount)
|
|
|
|
|
{
|
2015-06-11 17:54:42 +00:00
|
|
|
assert(amount <= balance);
|
|
|
|
|
on_withdraw_visitor vtor(balance, now, amount);
|
|
|
|
|
policy.visit(vtor);
|
2015-06-08 15:50:35 +00:00
|
|
|
balance -= amount;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-01 18:19:34 +00:00
|
|
|
asset vesting_balance_object::get_allowed_withdraw(const time_point_sec& now)const
|
|
|
|
|
{
|
|
|
|
|
asset amount = asset();
|
|
|
|
|
return policy.visit(get_allowed_withdraw_visitor(balance, now, amount));
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-08 15:50:35 +00:00
|
|
|
} } // graphene::chain
|
2019-05-13 22:18:12 +00:00
|
|
|
|
|
|
|
|
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::linear_vesting_policy )
|
|
|
|
|
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::cdd_vesting_policy )
|
|
|
|
|
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::vesting_balance_object )
|