diff --git a/pallets/vesting/src/lib.rs b/pallets/vesting/src/lib.rs index cc2d4001d..c70f2754a 100644 --- a/pallets/vesting/src/lib.rs +++ b/pallets/vesting/src/lib.rs @@ -198,18 +198,15 @@ pub mod pallet { /// Start at #[pallet::storage] - #[pallet::getter(fn vesting_start_at)] pub(super) type VestingStartAt = StorageValue<_, BlockNumberFor>; /// Cliff vesting #[pallet::storage] - #[pallet::getter(fn cliffs)] pub(super) type Cliff = StorageMap<_, Blake2_128Concat, T::AccountId, BlockNumberFor>; /// Information regarding the vesting of a given account. #[pallet::storage] - #[pallet::getter(fn vesting)] pub type Vesting = StorageMap< _, Blake2_128Concat, @@ -425,7 +422,7 @@ pub mod pallet { let target = T::Lookup::lookup(target)?; let index = index as usize; Self::do_vest(target.clone())?; - let schedules = Self::vesting(&target).ok_or(Error::::NotVesting)?; + let schedules = Vesting::::get(&target).ok_or(Error::::NotVesting)?; ensure!(schedules.len() > index, Error::::ScheduleIndexOutOfBounds); ensure!(schedules[index].per_block() != per_block, Error::::SamePerBlock); @@ -509,7 +506,7 @@ pub mod pallet { let schedule1_index = schedule1_index as usize; let schedule2_index = schedule2_index as usize; - let schedules = Self::vesting(&who).ok_or(Error::::NotVesting)?; + let schedules = Vesting::::get(&who).ok_or(Error::::NotVesting)?; let merge_action = VestingAction::Merge { index1: schedule1_index, index2: schedule2_index }; @@ -542,9 +539,9 @@ impl Pallet { schedule2: VestingInfo, BlockNumberFor>, ) -> Option, BlockNumberFor>> { let schedule1_start_at = - Self::vesting_start_at().map(|st| st.saturating_add(schedule1.starting_block())); + VestingStartAt::::get().map(|st| st.saturating_add(schedule1.starting_block())); let schedule2_start_at = - Self::vesting_start_at().map(|st| st.saturating_add(schedule2.starting_block())); + VestingStartAt::::get().map(|st| st.saturating_add(schedule2.starting_block())); let schedule1_ending_block = schedule1.ending_block_as_balance::(); let schedule2_ending_block = schedule2.ending_block_as_balance::(); let now_as_balance = T::BlockNumberToBalance::convert(now); @@ -650,8 +647,8 @@ impl Pallet { let filtered_schedules = action .pick_schedules::(schedules) .filter(|schedule| { - let start_at = - Self::vesting_start_at().map(|st| st.saturating_add(schedule.starting_block())); + let start_at = VestingStartAt::::get() + .map(|st| st.saturating_add(schedule.starting_block())); let locked_now = schedule.locked_at::(now, start_at); let keep = !locked_now.is_zero(); if keep { @@ -700,7 +697,7 @@ impl Pallet { /// Unlock any vested funds of `who`. fn do_vest(who: T::AccountId) -> DispatchResult { - let schedules = Self::vesting(&who).ok_or(Error::::NotVesting)?; + let schedules = Vesting::::get(&who).ok_or(Error::::NotVesting)?; let (schedules, locked_now) = Self::exec_action(schedules.to_vec(), VestingAction::Passive)?; @@ -736,7 +733,7 @@ impl Pallet { // 1) need to add it to the accounts vesting schedule collection, schedules.push(new_schedule); // (we use `locked_at` in case this is a schedule that started in the past) - let start_at = Self::vesting_start_at() + let start_at = VestingStartAt::::get() .map(|st| st.saturating_add(new_schedule.starting_block())); let new_schedule_locked = new_schedule.locked_at::(now, start_at); @@ -767,11 +764,11 @@ where /// Get the amount that is currently being vested and cannot be transferred out of this account. fn vesting_balance(who: &T::AccountId) -> Option> { - if let Some(v) = Self::vesting(who) { + if let Some(v) = Vesting::::get(who) { let now = >::block_number(); let total_locked_now = v.iter().fold(Zero::zero(), |total, schedule| { - let start_at = - Self::vesting_start_at().map(|st| st.saturating_add(schedule.starting_block())); + let start_at = VestingStartAt::::get() + .map(|st| st.saturating_add(schedule.starting_block())); schedule .locked_at::(now, start_at) .saturating_add(total) @@ -810,7 +807,7 @@ where return Err(Error::::InvalidScheduleParams.into()); }; - let mut schedules = Self::vesting(who).unwrap_or_default(); + let mut schedules = Vesting::::get(who).unwrap_or_default(); // NOTE: we must push the new schedule so that `exec_action` // will give the correct new locked amount. @@ -848,7 +845,7 @@ where /// Remove a vesting schedule for a given account. fn remove_vesting_schedule(who: &T::AccountId, schedule_index: u32) -> DispatchResult { - let schedules = Self::vesting(who).ok_or(Error::::NotVesting)?; + let schedules = Vesting::::get(who).ok_or(Error::::NotVesting)?; let remove_action = VestingAction::Remove { index: schedule_index as usize }; let (schedules, locked_now) = Self::exec_action(schedules.to_vec(), remove_action)?; diff --git a/pallets/vesting/src/tests.rs b/pallets/vesting/src/tests.rs index 29aad6a74..ce02da117 100644 --- a/pallets/vesting/src/tests.rs +++ b/pallets/vesting/src/tests.rs @@ -21,7 +21,10 @@ use frame_system::RawOrigin; use sp_runtime::{traits::Identity, TokenError}; use super::{Vesting as VestingStorage, *}; -use crate::mock::{Balances, ExtBuilder, System, Test, Vesting}; +use crate::{ + mock::{Balances, ExtBuilder, System, Test, Vesting}, + Vesting as vesting, +}; const ED: u64 = 1000; @@ -71,9 +74,9 @@ fn check_vesting_status() { CHAR_PER_BLOCK, // Vesting over 20 blocks 10u64, ); - assert_eq!(Vesting::vesting(&ALICE).unwrap().to_vec(), vec![user1_vesting_schedule]); - assert_eq!(Vesting::vesting(&BOB).unwrap().to_vec(), vec![user2_vesting_schedule]); - assert_eq!(Vesting::vesting(&CHAR).unwrap().to_vec(), vec![user3_vesting_schedule]); + assert_eq!(vesting::::get(&ALICE).unwrap().to_vec(), vec![user1_vesting_schedule]); + assert_eq!(vesting::::get(&BOB).unwrap().to_vec(), vec![user2_vesting_schedule]); + assert_eq!(vesting::::get(&CHAR).unwrap().to_vec(), vec![user3_vesting_schedule]); }); } @@ -160,7 +163,7 @@ fn vested_transfer_should_work() { user1_vesting_schedule_2 )); assert_eq!( - Vesting::vesting(&ALICE).unwrap().to_vec(), + vesting::::get(&ALICE).unwrap().to_vec(), vec![user1_vesting_schedule_1, user1_vesting_schedule_2] ); }) @@ -208,7 +211,7 @@ fn do_vest_with_cliff_should_work() { //set start_at to 0 assert_ok!(Vesting::init_vesting_start_at(RawOrigin::Root.into(), 0)); assert_ok!(Vesting::force_set_cliff(RawOrigin::Root.into(), ALICE, 4)); - assert_eq!(Vesting::cliffs(ALICE), Some(4)); + assert_eq!(Cliff::::get(ALICE), Some(4)); //set block to 4 System::set_block_number(4); @@ -283,7 +286,7 @@ fn set_vesting_per_block_should_work() { //check result start_at 10 > now 5 => 10 start_at let user_vesting_schedule_1 = VestingInfo::new(20000, 100, 10); - assert_eq!(Vesting::vesting(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_1]); + assert_eq!(vesting::::get(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_1]); //set block to 15 System::set_block_number(15); @@ -298,7 +301,7 @@ fn set_vesting_per_block_should_work() { //old_start_at = old_start_block 10 + absolute_start 0 //remained_vesting = 20000 - 5 * 100 let user_vesting_schedule_2 = VestingInfo::new(20000 - 5 * 100, 10, 15); - assert_eq!(Vesting::vesting(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_2]); + assert_eq!(vesting::::get(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_2]); let bob_locked_2 = Balances::locks(BOB).to_vec()[0].amount; assert_eq!(bob_locked_1 - 5 * 100, bob_locked_2); }) @@ -323,7 +326,7 @@ fn set_vesting_per_block_with_start_at_should_work() { //check result old_start_at 12 > now 5 => 10 start_at let user_vesting_schedule_1 = VestingInfo::new(20000, 100, 10); - assert_eq!(Vesting::vesting(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_1]); + assert_eq!(vesting::::get(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_1]); //set block to 15 System::set_block_number(15); @@ -338,7 +341,7 @@ fn set_vesting_per_block_with_start_at_should_work() { //old_start_at 12 < now 15 => now 15 - absolute_start 2 = 13 //remained_vesting = 20000 - 3 * 100 let user_vesting_schedule_2 = VestingInfo::new(20000 - 3 * 100, 10, 13); - assert_eq!(Vesting::vesting(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_2]); + assert_eq!(vesting::::get(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_2]); let bob_locked_2 = Balances::locks(BOB).to_vec()[0].amount; assert_eq!(bob_locked_1 - 3 * 100, bob_locked_2); }) @@ -365,7 +368,7 @@ fn repeatedly_set_vesting_per_block_should_work() { user_vesting_schedule_3 )); assert_eq!( - Vesting::vesting(&BOB).unwrap().to_vec(), + vesting::::get(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_1, user_vesting_schedule_2, user_vesting_schedule_3] ); @@ -388,7 +391,7 @@ fn repeatedly_set_vesting_per_block_should_work() { //check result old_start_at 12 > now 5 => 12 start_at let new_user_vesting_schedule_1 = VestingInfo::new(10000, 100, 12); assert_eq!( - Vesting::vesting(&BOB).unwrap().to_vec(), + vesting::::get(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_1, new_user_vesting_schedule_1, user_vesting_schedule_3] ); @@ -406,7 +409,7 @@ fn repeatedly_set_vesting_per_block_should_work() { //remained_vesting = 20000 - 3 * 1000 let new_user_vesting_schedule_2 = VestingInfo::new(20000 - 3 * 1000, 10, 13); assert_eq!( - Vesting::vesting(&BOB).unwrap().to_vec(), + vesting::::get(&BOB).unwrap().to_vec(), vec![new_user_vesting_schedule_2, new_user_vesting_schedule_1, user_vesting_schedule_3] ); @@ -445,7 +448,7 @@ fn merge_schedules_has_not_started_should_work() { user_vesting_schedule_2 )); assert_eq!( - Vesting::vesting(&BOB).unwrap().to_vec(), + vesting::::get(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_1, user_vesting_schedule_2] ); @@ -460,7 +463,7 @@ fn merge_schedules_has_not_started_should_work() { assert_ok!(Vesting::merge_schedules(RawOrigin::Signed(BOB).into(), 0, 1)); assert_eq!( - Vesting::vesting(&BOB).unwrap().to_vec(), + vesting::::get(&BOB).unwrap().to_vec(), vec![VestingInfo::new(BOB_INIT_LOCKED * 2, 2000, 12)] ); assert_eq!(40000, Balances::locks(BOB).to_vec()[0].amount); @@ -482,7 +485,7 @@ fn merge_ongoing_schedules_should_work() { user_vesting_schedule_2 )); assert_eq!( - Vesting::vesting(&BOB).unwrap().to_vec(), + vesting::::get(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_1, user_vesting_schedule_2] ); @@ -497,7 +500,7 @@ fn merge_ongoing_schedules_should_work() { assert_ok!(Vesting::merge_schedules(RawOrigin::Signed(BOB).into(), 0, 1)); assert_eq!( - Vesting::vesting(&BOB).unwrap().to_vec(), + vesting::::get(&BOB).unwrap().to_vec(), vec![VestingInfo::new(BOB_INIT_LOCKED, 1000, 40)] ); assert_eq!(12000, Balances::locks(BOB).to_vec()[0].amount); @@ -519,7 +522,7 @@ fn merge_finished_schedules_should_work() { user_vesting_schedule_2 )); assert_eq!( - Vesting::vesting(&BOB).unwrap().to_vec(), + vesting::::get(&BOB).unwrap().to_vec(), vec![user_vesting_schedule_1, user_vesting_schedule_2] ); @@ -530,7 +533,7 @@ fn merge_finished_schedules_should_work() { //None assert_ok!(Vesting::merge_schedules(RawOrigin::Signed(BOB).into(), 0, 1)); - assert_eq!(Vesting::vesting(&BOB), None); + assert_eq!(vesting::::get(&BOB), None); assert_eq!(0, Balances::locks(BOB).to_vec().len()); }) } @@ -544,12 +547,12 @@ fn merge_schedules_that_have_not_started() { ED, // Vest over 20 blocks. 10, ); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0]); assert_eq!(Balances::usable_balance(&2), 0); // Add a schedule that is identical to the one that already exists. assert_ok!(Vesting::vested_transfer(Some(4).into(), 2, sched0)); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0, sched0]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0, sched0]); assert_eq!(Balances::usable_balance(&2), 0); assert_ok!(Vesting::merge_schedules(Some(2).into(), 0, 1)); @@ -560,7 +563,7 @@ fn merge_schedules_that_have_not_started() { sched0.per_block() * 2, 10, // Starts at the block the schedules are merged/ ); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched1]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched1]); assert_eq!(Balances::usable_balance(&2), 0); }); @@ -577,7 +580,7 @@ fn merge_ongoing_schedules() { ED, // Vest over 20 blocks. 10, ); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0]); let sched1 = VestingInfo::new( ED * 10, @@ -585,10 +588,10 @@ fn merge_ongoing_schedules() { sched0.starting_block() + 5, // Start at block 15. ); assert_ok!(Vesting::vested_transfer(Some(4).into(), 2, sched1)); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0, sched1]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0, sched1]); // assert_ok!(Vesting::merge_schedules(Some(2).into(), 0, 1)); - // assert_eq!(Vesting::vesting(&2).unwrap(), vec![]); + // assert_eq!(vesting::::get(&2).unwrap(), vec![]); // Got to half way through the second schedule where both schedules are actively vesting. let cur_block = 20; @@ -620,7 +623,7 @@ fn merge_ongoing_schedules() { let sched2_per_block = sched2_locked / sched2_duration; let sched2 = VestingInfo::new(sched2_locked, sched2_per_block, cur_block); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched2]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched2]); // And just to double check, we assert the new merged schedule we be cleaned up as expected. System::set_block_number(30); @@ -663,7 +666,7 @@ fn merging_shifts_other_schedules_index() { ); // Account 3 starts out with no schedules, - assert_eq!(Vesting::vesting(&3), None); + assert_eq!(vesting::::get(&3), None); // and some usable balance. let usable_balance = Balances::usable_balance(&3); assert_eq!(usable_balance, 30 * ED); @@ -677,7 +680,7 @@ fn merging_shifts_other_schedules_index() { assert_ok!(Vesting::vested_transfer(Some(4).into(), 3, sched2)); // With no schedules vested or merged they are in the order they are created - assert_eq!(Vesting::vesting(&3).unwrap(), vec![sched0, sched1, sched2]); + assert_eq!(vesting::::get(&3).unwrap(), vec![sched0, sched1, sched2]); // and the usable balance has not changed. assert_eq!(usable_balance, Balances::usable_balance(&3)); @@ -698,7 +701,7 @@ fn merging_shifts_other_schedules_index() { let sched3 = VestingInfo::new(sched3_locked, sched3_per_block, sched3_start); // The not touched schedule moves left and the new merged schedule is appended. - assert_eq!(Vesting::vesting(&3).unwrap(), vec![sched1, sched3]); + assert_eq!(vesting::::get(&3).unwrap(), vec![sched1, sched3]); // The usable balance hasn't changed since none of the schedules have started. assert_eq!(Balances::usable_balance(&3), usable_balance); }); @@ -716,7 +719,7 @@ fn merge_ongoing_and_yet_to_be_started_schedules() { ED, // Vesting over 20 blocks 10, ); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0]); // Fast forward to half way through the life of sched1. let mut cur_block = @@ -769,7 +772,7 @@ fn merge_ongoing_and_yet_to_be_started_schedules() { let sched2_per_block = sched2_locked / sched2_duration; let sched2 = VestingInfo::new(sched2_locked, sched2_per_block, sched2_start); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched2]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched2]); }); } @@ -785,7 +788,7 @@ fn merge_finished_and_ongoing_schedules() { ED, // Vesting over 20 blocks. 10, ); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0]); let sched1 = VestingInfo::new( ED * 40, @@ -804,7 +807,7 @@ fn merge_finished_and_ongoing_schedules() { assert_ok!(Vesting::vested_transfer(Some(3).into(), 2, sched2)); // The schedules are in expected order prior to merging. - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0, sched1, sched2]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0, sched1, sched2]); // Fast forward to sched0's end block. let cur_block = sched0.ending_block_as_balance::(); @@ -819,7 +822,7 @@ fn merge_finished_and_ongoing_schedules() { // sched2 is now the first, since sched0 & sched1 get filtered out while "merging". // sched1 gets treated like the new merged schedule by getting pushed onto back // of the vesting schedules vec. Note: sched0 finished at the current block. - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched2, sched1]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched2, sched1]); // sched0 has finished, so its funds are fully unlocked. let sched0_unlocked_now = sched0.locked(); @@ -850,7 +853,7 @@ fn merge_finishing_schedules_does_not_create_a_new_one() { ED, // 20 block duration. 10, ); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0]); // Create sched1 and transfer it to account 2. let sched1 = VestingInfo::new( @@ -859,7 +862,7 @@ fn merge_finishing_schedules_does_not_create_a_new_one() { 10, ); assert_ok!(Vesting::vested_transfer(Some(3).into(), 2, sched1)); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0, sched1]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0, sched1]); let all_scheds_end = sched0 .ending_block_as_balance::() @@ -893,7 +896,7 @@ fn merge_finished_and_yet_to_be_started_schedules() { ED, // 20 block duration. 10, // Ends at block 30 ); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0]); let sched1 = VestingInfo::new( ED * 30, @@ -901,7 +904,7 @@ fn merge_finished_and_yet_to_be_started_schedules() { 35, ); assert_ok!(Vesting::vested_transfer(Some(13).into(), 2, sched1)); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0, sched1]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0, sched1]); let sched2 = VestingInfo::new( ED * 40, @@ -910,7 +913,7 @@ fn merge_finished_and_yet_to_be_started_schedules() { ); // Add a 3rd schedule to demonstrate how sched1 shifts. assert_ok!(Vesting::vested_transfer(Some(13).into(), 2, sched2)); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0, sched1, sched2]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0, sched1, sched2]); System::set_block_number(30); @@ -925,7 +928,7 @@ fn merge_finished_and_yet_to_be_started_schedules() { // sched0 is removed since it finished, and sched1 is removed and then pushed on the back // because it is treated as the merged schedule - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched2, sched1]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched2, sched1]); // The usable balance is updated because merging fully unlocked sched0. assert_eq!(Balances::usable_balance(&2), sched0.locked()); @@ -941,7 +944,7 @@ fn merge_schedules_throws_proper_errors() { ED, // 20 block duration. 10, ); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0]); // Account 2 only has 1 vesting schedule. assert_noop!( @@ -950,12 +953,12 @@ fn merge_schedules_throws_proper_errors() { ); // Account 4 has 0 vesting schedules. - assert_eq!(Vesting::vesting(&4), None); + assert_eq!(vesting::::get(&4), None); assert_noop!(Vesting::merge_schedules(Some(4).into(), 0, 1), Error::::NotVesting); // There are enough schedules to merge but an index is non-existent. Vesting::vested_transfer(Some(3).into(), 2, sched0).unwrap(); - assert_eq!(Vesting::vesting(&2).unwrap(), vec![sched0, sched0]); + assert_eq!(vesting::::get(&2).unwrap(), vec![sched0, sched0]); assert_noop!( Vesting::merge_schedules(Some(2).into(), 0, 2), Error::::ScheduleIndexOutOfBounds