Some linear vesting cleanup
This commit is contained in:
parent
5a41114d94
commit
4d176e73b2
7 changed files with 164 additions and 169 deletions
|
|
@ -249,8 +249,6 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
create<block_summary_object>([&](block_summary_object&) {});
|
create<block_summary_object>([&](block_summary_object&) {});
|
||||||
|
|
||||||
// Create initial accounts
|
// Create initial accounts
|
||||||
if( !genesis_state.initial_accounts.empty() )
|
|
||||||
{
|
|
||||||
for( const auto& account : genesis_state.initial_accounts )
|
for( const auto& account : genesis_state.initial_accounts )
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
|
|
@ -286,7 +284,7 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
apply_operation(genesis_eval_state, op);
|
apply_operation(genesis_eval_state, op);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
const auto& accounts_by_name = get_index_type<account_index>().indices().get<by_name>();
|
const auto& accounts_by_name = get_index_type<account_index>().indices().get<by_name>();
|
||||||
auto get_account_id = [&accounts_by_name](const string& name) {
|
auto get_account_id = [&accounts_by_name](const string& name) {
|
||||||
auto itr = accounts_by_name.find(name);
|
auto itr = accounts_by_name.find(name);
|
||||||
|
|
@ -301,8 +299,7 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
FC_ASSERT(itr != assets_by_symbol.end());
|
FC_ASSERT(itr != assets_by_symbol.end());
|
||||||
return itr->get_id();
|
return itr->get_id();
|
||||||
};
|
};
|
||||||
if( !genesis_state.initial_assets.empty() )
|
|
||||||
{
|
|
||||||
for( const genesis_state_type::initial_asset_type& asset : genesis_state.initial_assets )
|
for( const genesis_state_type::initial_asset_type& asset : genesis_state.initial_assets )
|
||||||
{
|
{
|
||||||
asset_dynamic_data_id_type dynamic_data_id;
|
asset_dynamic_data_id_type dynamic_data_id;
|
||||||
|
|
@ -375,12 +372,9 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
a.bitasset_data_id = bitasset_data_id;
|
a.bitasset_data_id = bitasset_data_id;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Create initial balances
|
// Create initial balances
|
||||||
share_type total_allocation;
|
share_type total_allocation;
|
||||||
if( !genesis_state.initial_balances.empty() )
|
|
||||||
{
|
|
||||||
for( const auto& handout : genesis_state.initial_balances )
|
for( const auto& handout : genesis_state.initial_balances )
|
||||||
{
|
{
|
||||||
create<balance_object>([&handout,&assets_by_symbol,total_allocation](balance_object& b) {
|
create<balance_object>([&handout,&assets_by_symbol,total_allocation](balance_object& b) {
|
||||||
|
|
@ -389,26 +383,24 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
});
|
});
|
||||||
total_allocation += handout.amount;
|
total_allocation += handout.amount;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Create initial vesting balances
|
// Create initial vesting balances
|
||||||
if( !genesis_state.initial_vesting_balances.empty() )
|
|
||||||
{
|
|
||||||
for( const genesis_state_type::initial_vesting_balance_type& vest : genesis_state.initial_vesting_balances )
|
for( const genesis_state_type::initial_vesting_balance_type& vest : genesis_state.initial_vesting_balances )
|
||||||
{
|
{
|
||||||
create<balance_object>([&](balance_object& b) {
|
create<balance_object>([&](balance_object& b) {
|
||||||
b.balance = asset(vest.amount, assets_by_symbol.find(vest.asset_symbol)->get_id());
|
|
||||||
b.owner = vest.owner;
|
b.owner = vest.owner;
|
||||||
|
b.balance = asset( vest.amount, assets_by_symbol.find( vest.asset_symbol )->get_id() );
|
||||||
|
|
||||||
linear_vesting_policy policy;
|
linear_vesting_policy policy;
|
||||||
policy.earliest_withdraw_time = vest.earliest_withdrawal_date;
|
policy.begin_timestamp = vest.begin_timestamp;
|
||||||
policy.begin_date = vest.vesting_start_date;
|
policy.vesting_cliff_seconds = 0;
|
||||||
policy.vesting_seconds = (vest.vesting_end_date - vest.vesting_start_date).to_seconds();
|
policy.vesting_duration_seconds = vest.vesting_duration_seconds;
|
||||||
policy.begin_balance = b.balance.amount;
|
policy.begin_balance = vest.begin_balance;
|
||||||
b.vesting_policy = policy;
|
|
||||||
|
b.vesting_policy = std::move( policy );
|
||||||
});
|
});
|
||||||
total_allocation += vest.amount;
|
total_allocation += vest.amount;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Set current supply based on allocations, if they happened
|
// Set current supply based on allocations, if they happened
|
||||||
if( total_allocation > 0 )
|
if( total_allocation > 0 )
|
||||||
|
|
|
||||||
|
|
@ -66,9 +66,9 @@ struct genesis_state_type {
|
||||||
address owner;
|
address owner;
|
||||||
string asset_symbol;
|
string asset_symbol;
|
||||||
share_type amount;
|
share_type amount;
|
||||||
time_point_sec vesting_start_date;
|
time_point_sec begin_timestamp;
|
||||||
time_point_sec earliest_withdrawal_date;
|
uint32_t vesting_duration_seconds = 0;
|
||||||
time_point_sec vesting_end_date;
|
share_type begin_balance;
|
||||||
};
|
};
|
||||||
struct initial_witness_type {
|
struct initial_witness_type {
|
||||||
/// Must correspond to one of the initial accounts
|
/// Must correspond to one of the initial accounts
|
||||||
|
|
@ -97,7 +97,7 @@ FC_REFLECT(graphene::chain::genesis_state_type::initial_account_type, (name)(own
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_balance_type,
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_balance_type,
|
||||||
(owner)(asset_symbol)(amount))
|
(owner)(asset_symbol)(amount))
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_vesting_balance_type,
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_vesting_balance_type,
|
||||||
(owner)(asset_symbol)(amount)(vesting_start_date)(earliest_withdrawal_date)(vesting_end_date))
|
(owner)(asset_symbol)(amount)(begin_timestamp)(vesting_duration_seconds)(begin_balance))
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_witness_type, (owner_name)(block_signing_key)(initial_secret))
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_witness_type, (owner_name)(block_signing_key)(initial_secret))
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_committee_member_type, (owner_name))
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_committee_member_type, (owner_name))
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_asset_type::initial_bitasset_options::initial_collateral_position,
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_asset_type::initial_bitasset_options::initial_collateral_position,
|
||||||
|
|
|
||||||
|
|
@ -1239,10 +1239,10 @@ namespace graphene { namespace chain {
|
||||||
|
|
||||||
struct linear_vesting_policy_initializer
|
struct linear_vesting_policy_initializer
|
||||||
{
|
{
|
||||||
/** while vesting begins on begin_date, none may be claimed before the start_claim time */
|
/** while vesting begins on begin_timestamp, none may be claimed before vesting_cliff_seconds have passed */
|
||||||
fc::time_point_sec start_claim;
|
fc::time_point_sec begin_timestamp;
|
||||||
fc::time_point_sec begin_date;
|
uint32_t vesting_cliff_seconds = 0;
|
||||||
uint32_t vesting_seconds = 0;
|
uint32_t vesting_duration_seconds = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct cdd_vesting_policy_initializer
|
struct cdd_vesting_policy_initializer
|
||||||
|
|
@ -1673,6 +1673,6 @@ FC_REFLECT( graphene::chain::balance_claim_operation,
|
||||||
FC_REFLECT_TYPENAME( graphene::chain::operation )
|
FC_REFLECT_TYPENAME( graphene::chain::operation )
|
||||||
FC_REFLECT_TYPENAME( graphene::chain::operation_result )
|
FC_REFLECT_TYPENAME( graphene::chain::operation_result )
|
||||||
FC_REFLECT_TYPENAME( fc::flat_set<graphene::chain::vote_id_type> )
|
FC_REFLECT_TYPENAME( fc::flat_set<graphene::chain::vote_id_type> )
|
||||||
FC_REFLECT(graphene::chain::linear_vesting_policy_initializer, (start_claim)(begin_date)(vesting_seconds) )
|
FC_REFLECT(graphene::chain::linear_vesting_policy_initializer, (begin_timestamp)(vesting_cliff_seconds)(vesting_duration_seconds) )
|
||||||
FC_REFLECT(graphene::chain::cdd_vesting_policy_initializer, (start_claim)(vesting_seconds) )
|
FC_REFLECT(graphene::chain::cdd_vesting_policy_initializer, (start_claim)(vesting_seconds) )
|
||||||
FC_REFLECT_TYPENAME( graphene::chain::vesting_policy_initializer )
|
FC_REFLECT_TYPENAME( graphene::chain::vesting_policy_initializer )
|
||||||
|
|
|
||||||
|
|
@ -54,17 +54,14 @@ namespace graphene { namespace chain {
|
||||||
*/
|
*/
|
||||||
struct linear_vesting_policy
|
struct linear_vesting_policy
|
||||||
{
|
{
|
||||||
/// No amount may be withdrawn before this time, regardless of how much has vested.
|
|
||||||
fc::time_point_sec earliest_withdraw_time;
|
|
||||||
/// This is the time at which funds begin vesting.
|
/// This is the time at which funds begin vesting.
|
||||||
/// Note that withdrawals are still not available until @ref earliest_withdraw_time
|
fc::time_point_sec begin_timestamp;
|
||||||
fc::time_point_sec begin_date;
|
/// No amount may be withdrawn before this many seconds of the vesting period have elapsed.
|
||||||
/// Duration of vesting period, in seconds. Must be greater than zero.
|
uint32_t vesting_cliff_seconds = 0;
|
||||||
uint32_t vesting_seconds = 0;
|
/// Duration of the vesting period, in seconds. Must be greater than 0 and greater than vesting_cliff_seconds.
|
||||||
/// The total amount of asset to vest
|
uint32_t vesting_duration_seconds = 0;
|
||||||
|
/// The total amount of asset to vest.
|
||||||
share_type begin_balance;
|
share_type begin_balance;
|
||||||
/// The total amount of asset which has already been withdrawn
|
|
||||||
share_type total_withdrawn;
|
|
||||||
|
|
||||||
asset get_allowed_withdraw(const vesting_policy_context& ctx)const;
|
asset get_allowed_withdraw(const vesting_policy_context& ctx)const;
|
||||||
bool is_deposit_allowed(const vesting_policy_context& ctx)const;
|
bool is_deposit_allowed(const vesting_policy_context& ctx)const;
|
||||||
|
|
@ -174,11 +171,10 @@ namespace graphene { namespace chain {
|
||||||
} } // graphene::chain
|
} } // graphene::chain
|
||||||
|
|
||||||
FC_REFLECT(graphene::chain::linear_vesting_policy,
|
FC_REFLECT(graphene::chain::linear_vesting_policy,
|
||||||
(earliest_withdraw_time)
|
(begin_timestamp)
|
||||||
(begin_date)
|
(vesting_cliff_seconds)
|
||||||
(vesting_seconds)
|
(vesting_duration_seconds)
|
||||||
(begin_balance)
|
(begin_balance)
|
||||||
(total_withdrawn)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
FC_REFLECT(graphene::chain::cdd_vesting_policy,
|
FC_REFLECT(graphene::chain::cdd_vesting_policy,
|
||||||
|
|
|
||||||
|
|
@ -54,9 +54,9 @@ struct init_policy_visitor
|
||||||
void operator()( const linear_vesting_policy_initializer& i )const
|
void operator()( const linear_vesting_policy_initializer& i )const
|
||||||
{
|
{
|
||||||
linear_vesting_policy policy;
|
linear_vesting_policy policy;
|
||||||
policy.vesting_seconds = i.vesting_seconds;
|
policy.begin_timestamp = i.begin_timestamp;
|
||||||
policy.begin_date = i.begin_date;
|
policy.vesting_cliff_seconds = i.vesting_cliff_seconds;
|
||||||
policy.earliest_withdraw_time = i.start_claim;
|
policy.vesting_duration_seconds = i.vesting_duration_seconds;
|
||||||
policy.begin_balance = init_balance;
|
policy.begin_balance = init_balance;
|
||||||
p = policy;
|
p = policy;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,28 +30,35 @@ inline bool sum_below_max_shares(const asset& a, const asset& b)
|
||||||
|
|
||||||
asset linear_vesting_policy::get_allowed_withdraw( const vesting_policy_context& ctx )const
|
asset linear_vesting_policy::get_allowed_withdraw( const vesting_policy_context& ctx )const
|
||||||
{
|
{
|
||||||
if(ctx.now <= earliest_withdraw_time)
|
share_type allowed_withdraw = 0;
|
||||||
return asset(0, ctx.balance.asset_id);
|
|
||||||
if(ctx.now <= begin_date)
|
|
||||||
return asset(0, ctx.balance.asset_id);
|
|
||||||
if(vesting_seconds == 0)
|
|
||||||
return ctx.balance;
|
|
||||||
|
|
||||||
int64_t elapsed_seconds = (ctx.now - begin_date).to_seconds();
|
if( ctx.now > begin_timestamp )
|
||||||
|
{
|
||||||
// if elapsed_seconds <= 0, then ctx.now <= begin_date,
|
const auto elapsed_seconds = (ctx.now - begin_timestamp).to_seconds();
|
||||||
// and we should have returned above.
|
|
||||||
assert( elapsed_seconds > 0 );
|
assert( elapsed_seconds > 0 );
|
||||||
|
|
||||||
fc::uint128_t total_allowed = begin_balance.value;
|
if( elapsed_seconds >= vesting_cliff_seconds )
|
||||||
total_allowed *= uint64_t(elapsed_seconds);
|
{
|
||||||
total_allowed /= vesting_seconds;
|
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 );
|
||||||
|
|
||||||
if(total_allowed <= total_withdrawn.value)
|
const share_type withdrawn_already = begin_balance - ctx.balance.amount;
|
||||||
return asset(0, ctx.balance.asset_id);
|
assert( withdrawn_already >= 0 );
|
||||||
total_allowed -= total_withdrawn.value;
|
|
||||||
FC_ASSERT(total_allowed <= GRAPHENE_MAX_SHARE_SUPPLY);
|
allowed_withdraw = total_vested - withdrawn_already;
|
||||||
return asset(total_allowed.to_uint64(), ctx.balance.asset_id);
|
assert( allowed_withdraw >= 0 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return asset( allowed_withdraw, ctx.amount.asset_id );
|
||||||
}
|
}
|
||||||
|
|
||||||
void linear_vesting_policy::on_deposit(const vesting_policy_context& ctx)
|
void linear_vesting_policy::on_deposit(const vesting_policy_context& ctx)
|
||||||
|
|
@ -66,12 +73,12 @@ bool linear_vesting_policy::is_deposit_allowed(const vesting_policy_context& ctx
|
||||||
|
|
||||||
void linear_vesting_policy::on_withdraw(const vesting_policy_context& ctx)
|
void linear_vesting_policy::on_withdraw(const vesting_policy_context& ctx)
|
||||||
{
|
{
|
||||||
total_withdrawn += ctx.amount.amount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool linear_vesting_policy::is_withdraw_allowed(const vesting_policy_context& ctx)const
|
bool linear_vesting_policy::is_withdraw_allowed(const vesting_policy_context& ctx)const
|
||||||
{
|
{
|
||||||
return (ctx.amount <= get_allowed_withdraw(ctx));
|
return (ctx.amount.asset_id == ctx.balance.asset_id)
|
||||||
|
&& (ctx.amount <= get_allowed_withdraw(ctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
fc::uint128_t cdd_vesting_policy::compute_coin_seconds_earned(const vesting_policy_context& ctx)const
|
fc::uint128_t cdd_vesting_policy::compute_coin_seconds_earned(const vesting_policy_context& ctx)const
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit 7bd47af88eaba93711da58cf7ddfe866deb21948
|
Subproject commit d462be0e92a576b93a83194e9649af528162bb4a
|
||||||
Loading…
Reference in a new issue