Skip to content

Commit

Permalink
fix vest start at
Browse files Browse the repository at this point in the history
  • Loading branch information
ark930 committed Aug 10, 2021
1 parent 666a3c6 commit c1d4bb9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
53 changes: 30 additions & 23 deletions pallets/vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ use frame_support::{
use frame_system::{ensure_root, ensure_signed, pallet_prelude::*};
pub use pallet::*;
use sp_runtime::{
traits::{AtLeast32BitUnsigned, Convert, MaybeSerializeDeserialize, StaticLookup, Zero},
traits::{
AtLeast32BitUnsigned, Convert, MaybeSerializeDeserialize, Saturating, StaticLookup, Zero,
},
RuntimeDebug,
};
use sp_std::{fmt::Debug, prelude::*};
Expand Down Expand Up @@ -94,10 +96,9 @@ impl<Balance: AtLeast32BitUnsigned + Copy, BlockNumber: AtLeast32BitUnsigned + C
start_at: Option<BlockNumber>,
) -> Balance {
// Number of blocks that count toward vesting
// Saturating to 0 when n < starting_block or n < start_at
let vested_block_count = match start_at {
Some(st) if st < n => n.saturating_sub(st.max(self.starting_block)),
_ => Zero::zero(),
Some(st) if st < n => n.saturating_sub(st),
_ => return self.locked,
};
let vested_block_count = BlockNumberToBalance::convert(vested_block_count);
// Return amount that is still locked in vesting
Expand Down Expand Up @@ -386,10 +387,7 @@ impl<T: Config> Pallet<T> {
let vesting = Self::vesting(&who).ok_or(Error::<T>::NotVesting)?;
let now = <frame_system::Pallet<T>>::block_number();

let start_at = match Self::vesting_start_at() {
Some(value) => Some(value.max(vesting.starting_block)),
None => Some(vesting.starting_block),
};
let start_at = Self::vesting_start_at().map(|st| st.saturating_add(vesting.starting_block));

let locked_now = vesting.locked_at::<T::BlockNumberToBalance>(now, start_at);

Expand Down Expand Up @@ -417,10 +415,8 @@ where
fn vesting_balance(who: &T::AccountId) -> Option<BalanceOf<T>> {
if let Some(v) = Self::vesting(who) {
let now = <frame_system::Pallet<T>>::block_number();
let start_at = match Self::vesting_start_at() {
Some(value) => Some(value.max(v.starting_block)),
None => Some(v.starting_block),
};
let start_at = Self::vesting_start_at().map(|st| st.saturating_add(v.starting_block));

let locked_now = v.locked_at::<T::BlockNumberToBalance>(now, start_at);

Some(T::Currency::free_balance(who).min(locked_now))
Expand Down Expand Up @@ -624,7 +620,7 @@ mod tests {
assert_eq!(Vesting::vesting(&12), Some(user12_vesting_schedule)); // Account 12 has a vesting schedule

// Account 1 has only 128 units vested from their illiquid 256 * 5 units at block 1
assert_eq!(Vesting::vesting_balance(&1), Some(128 * 9));
assert_eq!(Vesting::vesting_balance(&1), Some(256 * 5));
// Account 2 has their full balance locked
assert_eq!(Vesting::vesting_balance(&2), Some(user2_free_balance));
// Account 12 has only their illiquid funds locked
Expand All @@ -633,6 +629,8 @@ mod tests {
System::set_block_number(10);
assert_eq!(System::block_number(), 10);

assert_ok!(Vesting::init_vesting_start_at(Origin::root(), 0));

// Account 1 has fully vested by block 10
assert_eq!(Vesting::vesting_balance(&1), Some(0));
// Account 2 has started vesting by block 10
Expand All @@ -654,7 +652,9 @@ mod tests {
ExtBuilder::default().existential_deposit(10).build().execute_with(|| {
let user1_free_balance = Balances::free_balance(&1);
assert_eq!(user1_free_balance, 100); // Account 1 has free balance
// Account 1 has only 5 units vested at block 1 (plus 50 unvested)
assert_ok!(Vesting::init_vesting_start_at(Origin::root(), 10));
System::set_block_number(11);
// Account 1 has only 5 units vested at block 1 (plus 50 unvested)
assert_eq!(Vesting::vesting_balance(&1), Some(45));
assert_noop!(
Balances::transfer(Some(1).into(), 2, 56),
Expand All @@ -668,12 +668,11 @@ mod tests {
ExtBuilder::default().existential_deposit(10).build().execute_with(|| {
let user1_free_balance = Balances::free_balance(&1);
assert_eq!(user1_free_balance, 100); // Account 1 has free balance
// Account 1 has only 5 units vested at block 1 (plus 50 unvested)
assert_ok!(Vesting::init_vesting_start_at(Origin::root(), 10));
System::set_block_number(11);
// Account 1 has only 5 units vested at block 1 (plus 50 unvested)
assert_eq!(Vesting::vesting_balance(&1), Some(45));
assert_ok!(Vesting::vest(Some(1).into()));

println!("Account1 usable Balance: {:?}", Balances::usable_balance(&1));

assert_ok!(Balances::transfer(Some(1).into(), 2, 55));
});
}
Expand All @@ -683,7 +682,9 @@ mod tests {
ExtBuilder::default().existential_deposit(10).build().execute_with(|| {
let user1_free_balance = Balances::free_balance(&1);
assert_eq!(user1_free_balance, 100); // Account 1 has free balance
// Account 1 has only 5 units vested at block 1 (plus 50 unvested)
assert_ok!(Vesting::init_vesting_start_at(Origin::root(), 10));
System::set_block_number(11);
// Account 1 has only 5 units vested at block 1 (plus 50 unvested)
assert_eq!(Vesting::vesting_balance(&1), Some(45));
assert_ok!(Vesting::vest_other(Some(2).into(), 1));
assert_ok!(Balances::transfer(Some(1).into(), 2, 55));
Expand All @@ -702,6 +703,9 @@ mod tests {
let user2_free_balance = Balances::free_balance(&2);
assert_eq!(user2_free_balance, 300); // Account 2 has 100 more free balance than normal

assert_ok!(Vesting::init_vesting_start_at(Origin::root(), 10));
System::set_block_number(11);

// Account 1 has only 5 units vested at block 1 (plus 150 unvested)
assert_eq!(Vesting::vesting_balance(&1), Some(45));
assert_ok!(Vesting::vest(Some(1).into()));
Expand Down Expand Up @@ -762,6 +766,8 @@ mod tests {
// Account 4 has 5 * 256 locked.
assert_eq!(Vesting::vesting_balance(&4), Some(256 * 5));

assert_ok!(Vesting::init_vesting_start_at(Origin::root(), 0));

System::set_block_number(20);
assert_eq!(System::block_number(), 20);

Expand Down Expand Up @@ -854,14 +860,15 @@ mod tests {
System::set_block_number(20);
assert_eq!(System::block_number(), 20);

// Account 4 has 5 * 64 units vested by block 20.
assert_eq!(Vesting::vesting_balance(&4), Some(10 * 64));
// Account 4 has 5 * 256 locked.
assert_eq!(Vesting::vesting_balance(&4), Some(256 * 5));

System::set_block_number(30);
assert_eq!(System::block_number(), 30);

// Account 4 has fully vested.
assert_eq!(Vesting::vesting_balance(&4), Some(0));
assert_ok!(Vesting::init_vesting_start_at(Origin::root(), 10));

assert_eq!(Vesting::vesting_balance(&4), Some(64 * 10));
});
}

Expand Down
9 changes: 3 additions & 6 deletions runtime/bifrost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,8 @@ pub struct CallFilter;
impl Filter<Call> for CallFilter {
fn filter(c: &Call) -> bool {
match *c {
Call::Balances(pallet_balances::Call::<Runtime>::transfer(..)) => false,
Call::Balances(pallet_balances::Call::<Runtime>::transfer_keep_alive(..)) => false,
Call::Vesting(pallet_vesting::Call::<Runtime>::vest(..)) => false,
Call::Vesting(pallet_vesting::Call::<Runtime>::vest_other(..)) => false,
Call::Vesting(pallet_vesting::Call::<Runtime>::vested_transfer(..)) => false,
Call::Balances(..) => false,
Call::Vesting(..) => false,
_ => true,
}
}
Expand Down Expand Up @@ -544,7 +541,7 @@ construct_runtime! {
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 51,

// Vesting. Usable initially, but removed once all vesting is finished.
Vesting: pallet_vesting::{Pallet, Call, Storage, Event<T>, Config<T>} = 60,
BifrostVesting: bifrost_vesting::{Pallet, Call, Storage, Event<T>, Config<T>} = 60,
}
}

Expand Down

0 comments on commit c1d4bb9

Please sign in to comment.