From 48e9793ca2b51c32b6879f0f8bd794a3dedda616 Mon Sep 17 00:00:00 2001 From: "Muhammad-Jibril B.A" Date: Tue, 21 Dec 2021 15:07:43 +0800 Subject: [PATCH 1/2] Change of mind, make vesting for only SETM. #680 Change of mind, make vesting for only SETM. #680 --- Cargo.lock | 18 ++- lib-serml/vesting/Cargo.toml | 1 - lib-serml/vesting/src/lib.rs | 117 ++++++--------- lib-serml/vesting/src/mock.rs | 80 ++--------- lib-serml/vesting/src/tests.rs | 200 ++++++++++++++++---------- lib-serml/vesting/src/weights.rs | 6 +- runtime/Cargo.toml | 7 +- runtime/src/lib.rs | 4 +- runtime/src/weights/module_vesting.rs | 6 +- 9 files changed, 206 insertions(+), 233 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ef353b594..fd0e79ee4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4177,7 +4177,6 @@ dependencies = [ "frame-support", "frame-system", "module-support", - "orml-tokens", "orml-traits", "pallet-balances", "parity-scale-codec", @@ -4721,6 +4720,21 @@ dependencies = [ "sp-std", ] +[[package]] +name = "orml-vesting" +version = "0.4.1-dev" +dependencies = [ + "frame-support", + "frame-system", + "pallet-balances", + "parity-scale-codec", + "serde", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", +] + [[package]] name = "owning_ref" version = "0.4.1" @@ -7955,7 +7969,6 @@ dependencies = [ "module-support", "module-transaction-pause", "module-transaction-payment", - "module-vesting", "orml-auction", "orml-authority", "orml-benchmarking", @@ -7964,6 +7977,7 @@ dependencies = [ "orml-oracle-rpc-runtime-api", "orml-tokens", "orml-traits", + "orml-vesting", "pallet-authority-discovery", "pallet-authorship", "pallet-babe", diff --git a/lib-serml/vesting/Cargo.toml b/lib-serml/vesting/Cargo.toml index 41106beaf..62229d78d 100644 --- a/lib-serml/vesting/Cargo.toml +++ b/lib-serml/vesting/Cargo.toml @@ -20,7 +20,6 @@ orml-traits = { path = "../../lib-orml/traits", default-features = false } [dev-dependencies] sp-core = { git = "https://github.com/paritytech/substrate" , branch = "polkadot-v0.9.10" } pallet-balances = { git = "https://github.com/paritytech/substrate" , branch = "polkadot-v0.9.10" } -orml-tokens = { path = "../../lib-orml/tokens" } [features] default = ["std"] diff --git a/lib-serml/vesting/src/lib.rs b/lib-serml/vesting/src/lib.rs index cef9d30d0..b7248da86 100644 --- a/lib-serml/vesting/src/lib.rs +++ b/lib-serml/vesting/src/lib.rs @@ -52,7 +52,7 @@ use codec::{HasCompact, MaxEncodedLen}; use frame_support::{ ensure, pallet_prelude::*, - traits::{EnsureOrigin, Get}, + traits::{Currency, EnsureOrigin, ExistenceRequirement, Get, LockIdentifier, LockableCurrency, WithdrawReasons}, transactional, BoundedVec, }; use frame_system::{ensure_root, ensure_signed, pallet_prelude::*}; @@ -64,10 +64,7 @@ use sp_std::{ cmp::{Eq, PartialEq}, convert::TryInto, vec::Vec, -};use orml_traits::{ - LockIdentifier, MultiCurrency, MultiLockableCurrency, }; -use primitives::CurrencyId; mod mock; mod tests; @@ -133,13 +130,10 @@ pub mod module { use super::*; pub(crate) type BalanceOf = - <::MultiCurrency as MultiCurrency<::AccountId>>::Balance; - pub(crate) type CurrencyIdOf = - <::MultiCurrency as MultiCurrency<::AccountId>>::CurrencyId; + <::Currency as Currency<::AccountId>>::Balance; pub(crate) type VestingScheduleOf = VestingSchedule<::BlockNumber, BalanceOf>; pub type ScheduledItem = ( ::AccountId, - CurrencyIdOf, ::BlockNumber, ::BlockNumber, u32, @@ -150,7 +144,7 @@ pub mod module { pub trait Config: frame_system::Config { type Event: From> + IsType<::Event>; - type MultiCurrency: MultiLockableCurrency; + type Currency: LockableCurrency; #[pallet::constant] /// The minimum amount transferred to call `vested_transfer`. @@ -162,7 +156,7 @@ pub mod module { /// Weight information for extrinsics in this module. type WeightInfo: WeightInfo; - /// The maximum vesting schedules for SETM + /// The maximum vesting schedules type MaxVestingSchedules: Get; } @@ -186,12 +180,12 @@ pub mod module { #[pallet::generate_deposit(fn deposit_event)] #[pallet::metadata(T::AccountId = "AccountId", VestingScheduleOf = "VestingScheduleOf", BalanceOf = "Balance")] pub enum Event { - /// Added new vesting schedule. \[currency_id, from, to, vesting_schedule\] - VestingScheduleAdded(CurrencyIdOf, T::AccountId, T::AccountId, VestingScheduleOf), - /// Claimed vesting. \[who, currency_id, locked_amount\] - Claimed(T::AccountId, CurrencyIdOf, BalanceOf), - /// Updated vesting schedules. \[currency_id, who\] - VestingSchedulesUpdated(CurrencyIdOf, T::AccountId), + /// Added new vesting schedule. \[from, to, vesting_schedule\] + VestingScheduleAdded(T::AccountId, T::AccountId, VestingScheduleOf), + /// Claimed vesting. \[who, locked_amount\] + Claimed(T::AccountId, BalanceOf), + /// Updated vesting schedules. \[who\] + VestingSchedulesUpdated(T::AccountId), } /// Vesting schedules of an account. @@ -199,12 +193,10 @@ pub mod module { /// VestingSchedules: map AccountId => Vec #[pallet::storage] #[pallet::getter(fn vesting_schedules)] - pub type VestingSchedules = StorageDoubleMap< + pub type VestingSchedules = StorageMap< _, Blake2_128Concat, T::AccountId, - Blake2_128Concat, - CurrencyIdOf, BoundedVec, T::MaxVestingSchedules>, ValueQuery, >; @@ -226,7 +218,7 @@ pub mod module { fn build(&self) { self.vesting .iter() - .for_each(|(who, currency_id, start, period, period_count, per_period)| { + .for_each(|(who, start, period, period_count, per_period)| { let total = *per_period * Into::>::into(*period_count); let bounded_schedule: BoundedVec, T::MaxVestingSchedules> = @@ -240,13 +232,12 @@ pub mod module { .expect("Max vesting schedules exceeded"); assert!( - T::MultiCurrency::free_balance(*currency_id, who) >= total, + T::Currency::free_balance(who) >= total, "Account do not have enough balance" ); - if T::MultiCurrency::set_lock(VESTING_LOCK_ID, *currency_id, who, total).is_ok() { - VestingSchedules::::insert(who, currency_id, bounded_schedule) - } + T::Currency::set_lock(VESTING_LOCK_ID, who, total, WithdrawReasons::all()); + VestingSchedules::::insert(who, bounded_schedule); }); } } @@ -260,56 +251,50 @@ pub mod module { #[pallet::call] impl Pallet { #[pallet::weight(T::WeightInfo::claim((::MaxVestingSchedules::get() / 2) as u32))] - pub fn claim(origin: OriginFor, currency_id: CurrencyIdOf) -> DispatchResult { + pub fn claim(origin: OriginFor) -> DispatchResult { let who = ensure_signed(origin)?; - let locked_amount = Self::do_claim(currency_id, &who); + let locked_amount = Self::do_claim(&who); - Self::deposit_event(Event::Claimed(who, currency_id, locked_amount)); + Self::deposit_event(Event::Claimed(who, locked_amount)); Ok(()) } #[pallet::weight(T::WeightInfo::vested_transfer())] pub fn vested_transfer( origin: OriginFor, - currency_id: CurrencyIdOf, dest: ::Source, - schedule: VestingScheduleOf + schedule: VestingScheduleOf, ) -> DispatchResult { let from = T::VestedTransferOrigin::ensure_origin(origin)?; let to = T::Lookup::lookup(dest)?; - Self::do_vested_transfer(currency_id, &from, &to, schedule.clone())?; + Self::do_vested_transfer(&from, &to, schedule.clone())?; - Self::deposit_event(Event::VestingScheduleAdded(currency_id, from, to, schedule)); + Self::deposit_event(Event::VestingScheduleAdded(from, to, schedule)); Ok(()) } #[pallet::weight(T::WeightInfo::update_vesting_schedules(vesting_schedules.len() as u32))] pub fn update_vesting_schedules( origin: OriginFor, - currency_id: CurrencyIdOf, who: ::Source, vesting_schedules: Vec>, ) -> DispatchResult { ensure_root(origin)?; let account = T::Lookup::lookup(who)?; - Self::do_update_vesting_schedules(currency_id, &account, vesting_schedules)?; + Self::do_update_vesting_schedules(&account, vesting_schedules)?; - Self::deposit_event(Event::VestingSchedulesUpdated(currency_id, account)); + Self::deposit_event(Event::VestingSchedulesUpdated(account)); Ok(()) } #[pallet::weight(T::WeightInfo::claim((::MaxVestingSchedules::get() / 2) as u32))] - pub fn claim_for( - origin: OriginFor, - currency_id: CurrencyIdOf, - dest: ::Source - ) -> DispatchResult { + pub fn claim_for(origin: OriginFor, dest: ::Source) -> DispatchResult { let _ = ensure_signed(origin)?; let who = T::Lookup::lookup(dest)?; - let locked_amount = Self::do_claim(currency_id, &who); + let locked_amount = Self::do_claim(&who); - Self::deposit_event(Event::Claimed(who, currency_id, locked_amount)); + Self::deposit_event(Event::Claimed(who, locked_amount)); Ok(()) } } @@ -324,22 +309,22 @@ impl BlockNumberProvider for Pallet { } impl Pallet { - fn do_claim(currency_id: CurrencyIdOf, who: &T::AccountId) -> BalanceOf { - let locked = Self::locked_balance(currency_id, who); + fn do_claim(who: &T::AccountId) -> BalanceOf { + let locked = Self::locked_balance(who); if locked.is_zero() { - >::remove(who, currency_id); - T::MultiCurrency::remove_lock(VESTING_LOCK_ID, currency_id, who).unwrap(); + // cleanup the storage and unlock the fund + >::remove(who); + T::Currency::remove_lock(VESTING_LOCK_ID, who); } else { - T::MultiCurrency::set_lock(VESTING_LOCK_ID, currency_id, who, locked).unwrap(); + T::Currency::set_lock(VESTING_LOCK_ID, who, locked, WithdrawReasons::all()); } locked } /// Returns locked balance based on current block number. - fn locked_balance(currency_id: CurrencyIdOf, who: &T::AccountId) -> BalanceOf { + fn locked_balance(who: &T::AccountId) -> BalanceOf { let now = ::current_block_number(); - // cleanup the storage and unlock the fund - >::mutate_exists(who, currency_id, |maybe_schedules| { + >::mutate_exists(who, |maybe_schedules| { let total = if let Some(schedules) = maybe_schedules.as_mut() { let mut total: BalanceOf = Zero::zero(); schedules.retain(|s| { @@ -354,42 +339,33 @@ impl Pallet { if total.is_zero() { *maybe_schedules = None; } - return total + total }) } #[transactional] - fn do_vested_transfer( - currency_id: CurrencyIdOf, - from: &T::AccountId, - to: &T::AccountId, - schedule: VestingScheduleOf - ) -> DispatchResult { + fn do_vested_transfer(from: &T::AccountId, to: &T::AccountId, schedule: VestingScheduleOf) -> DispatchResult { let schedule_amount = Self::ensure_valid_vesting_schedule(&schedule)?; - let total_amount = Self::locked_balance(currency_id, to) + let total_amount = Self::locked_balance(to) .checked_add(&schedule_amount) .ok_or(ArithmeticError::Overflow)?; - T::MultiCurrency::transfer(currency_id, from, to, schedule_amount)?; - T::MultiCurrency::set_lock(VESTING_LOCK_ID, currency_id, to, total_amount)?; - >::try_append(to, currency_id, schedule).map_err(|_| Error::::MaxVestingSchedulesExceeded)?; + T::Currency::transfer(from, to, schedule_amount, ExistenceRequirement::AllowDeath)?; + T::Currency::set_lock(VESTING_LOCK_ID, to, total_amount, WithdrawReasons::all()); + >::try_append(to, schedule).map_err(|_| Error::::MaxVestingSchedulesExceeded)?; Ok(()) } - fn do_update_vesting_schedules( - currency_id: CurrencyIdOf, - who: &T::AccountId, - schedules: Vec> - ) -> DispatchResult { + fn do_update_vesting_schedules(who: &T::AccountId, schedules: Vec>) -> DispatchResult { let bounded_schedules: BoundedVec, T::MaxVestingSchedules> = schedules .try_into() .map_err(|_| Error::::MaxVestingSchedulesExceeded)?; // empty vesting schedules cleanup the storage and unlock the fund if bounded_schedules.len().is_zero() { - >::remove(who, currency_id); - T::MultiCurrency::remove_lock(VESTING_LOCK_ID, currency_id, who).unwrap(); + >::remove(who); + T::Currency::remove_lock(VESTING_LOCK_ID, who); return Ok(()); } @@ -400,12 +376,13 @@ impl Pallet { Ok(acc_amount + amount) })?; ensure!( - T::MultiCurrency::free_balance(currency_id, who) >= total_amount, + T::Currency::free_balance(who) >= total_amount, Error::::InsufficientBalanceToLock, ); - T::MultiCurrency::set_lock(VESTING_LOCK_ID, currency_id, who, total_amount)?; - >::insert(who, currency_id, bounded_schedules); + T::Currency::set_lock(VESTING_LOCK_ID, who, total_amount, WithdrawReasons::all()); + >::insert(who, bounded_schedules); + Ok(()) } diff --git a/lib-serml/vesting/src/mock.rs b/lib-serml/vesting/src/mock.rs index a92cce172..385c15b0b 100644 --- a/lib-serml/vesting/src/mock.rs +++ b/lib-serml/vesting/src/mock.rs @@ -21,12 +21,6 @@ parameter_types! { pub type AccountId = u128; -pub const SETR: CurrencyId = CurrencyId::Token(TokenSymbol::SETR); -pub const SETUSD: CurrencyId = CurrencyId::Token(TokenSymbol::SETUSD); -pub const SETM: CurrencyId = CurrencyId::Token(TokenSymbol::SETM); -pub const SERP: CurrencyId = CurrencyId::Token(TokenSymbol::SERP); -pub const DNAR: CurrencyId = CurrencyId::Token(TokenSymbol::DNAR); - impl frame_system::Config for Runtime { type Origin = Origin; type Call = Call; @@ -90,52 +84,18 @@ impl EnsureOrigin for EnsureAliceOrBob { } } -parameter_type_with_key! { - pub ExistentialDeposits: |_currency_id: CurrencyId| -> Balance { - Default::default() - }; -} - -impl orml_tokens::Config for Runtime { - type Event = Event; - type Balance = Balance; - type Amount = Amount; - type CurrencyId = CurrencyId; - type WeightInfo = (); - type ExistentialDeposits = ExistentialDeposits; - type OnDust = (); - type MaxLocks = MaxLocks; - type DustRemovalWhitelist = (); -} - -parameter_types! { - pub StableCurrencyIds: Vec = vec![ - SETR, - SETUSD, - ]; - pub static MockBlockNumberProvider: u64 = 0; -} - -impl BlockNumberProvider for MockBlockNumberProvider { - type BlockNumber = u64; - - fn current_block_number() -> Self::BlockNumber { - Self::get() - } -} - parameter_types! { - pub const MaxVestingSchedules: u32 = 2; + pub const MaxVestingSchedule: u32 = 2; pub const MinVestedTransfer: u64 = 5; } impl Config for Runtime { type Event = Event; - type MultiCurrency = Tokens; + type Currency = PalletBalances; type MinVestedTransfer = MinVestedTransfer; type VestedTransferOrigin = EnsureAliceOrBob; type WeightInfo = (); - type MaxVestingSchedules = MaxVestingSchedules; + type MaxVestingSchedules = MaxVestingSchedule; } type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; @@ -150,7 +110,6 @@ construct_runtime!( System: frame_system::{Pallet, Call, Storage, Config, Event}, Vesting: vesting::{Pallet, Storage, Call, Event, Config}, PalletBalances: pallet_balances::{Pallet, Call, Storage, Config, Event}, - Tokens: orml_tokens::{Pallet, Storage, Event, Config} } ); @@ -158,44 +117,23 @@ pub const ALICE: AccountId = 1; pub const BOB: AccountId = 2; pub const CHARLIE: AccountId = 3; -pub struct ExtBuilder { - balances: Vec<(AccountId, CurrencyId, Balance)>, -} - -impl Default for ExtBuilder { - fn default() -> Self { - Self { - balances: vec![ - (ALICE, SETM, 10000), - (ALICE, SERP, 1000), - (ALICE, DNAR, 1000), - (ALICE, SETR, 1000), - (ALICE, SETUSD, 1000), - (CHARLIE, SETM, 10000), - (CHARLIE, SERP, 1000), - (CHARLIE, DNAR, 1000), - (CHARLIE, SETR, 1000), - (CHARLIE, SETUSD, 1000) - ], - } - } -} +#[derive(Default)] +pub struct ExtBuilder; impl ExtBuilder { - pub fn build(self) -> sp_io::TestExternalities { + pub fn build() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default() .build_storage::() .unwrap(); - orml_tokens::GenesisConfig:: { - balances: self.balances, + pallet_balances::GenesisConfig:: { + balances: vec![(ALICE, 100), (CHARLIE, 30)], } .assimilate_storage(&mut t) .unwrap(); vesting::GenesisConfig:: { - // who, start, period, period_count, per_period - vesting: vec![(CHARLIE, SETM, 2, 3, 4, 5)], + vesting: vec![(CHARLIE, 2, 3, 4, 5)], // who, start, period, period_count, per_period } .assimilate_storage(&mut t) .unwrap(); diff --git a/lib-serml/vesting/src/tests.rs b/lib-serml/vesting/src/tests.rs index 415064ab7..070176eb4 100644 --- a/lib-serml/vesting/src/tests.rs +++ b/lib-serml/vesting/src/tests.rs @@ -4,19 +4,22 @@ use super::*; use frame_support::{assert_noop, assert_ok, error::BadOrigin}; -use mock::{Event, SETM, *}; +use mock::{Event, *}; +use pallet_balances::{BalanceLock, Reasons}; #[test] fn vesting_from_chain_spec_works() { - ExtBuilder::default().build().execute_with(|| { - assert_ok!(Tokens::ensure_can_withdraw( - SETM, + ExtBuilder::build().execute_with(|| { + assert_ok!(PalletBalances::ensure_can_withdraw( &CHARLIE, - 10 + 10, + WithdrawReasons::TRANSFER, + 20 )); + assert!(PalletBalances::ensure_can_withdraw(&CHARLIE, 11, WithdrawReasons::TRANSFER, 19).is_err()); assert_eq!( - Vesting::vesting_schedules(&CHARLIE, SETM), + Vesting::vesting_schedules(&CHARLIE), vec![VestingSchedule { start: 2u64, period: 3u64, @@ -27,29 +30,32 @@ fn vesting_from_chain_spec_works() { System::set_block_number(13); - assert_ok!(Vesting::claim(Origin::signed(CHARLIE), SETM)); + assert_ok!(Vesting::claim(Origin::signed(CHARLIE))); - assert_ok!(Tokens::ensure_can_withdraw( - SETM, + assert_ok!(PalletBalances::ensure_can_withdraw( &CHARLIE, - 25 + 25, + WithdrawReasons::TRANSFER, + 5 )); + assert!(PalletBalances::ensure_can_withdraw(&CHARLIE, 26, WithdrawReasons::TRANSFER, 4).is_err()); System::set_block_number(14); - assert_ok!(Vesting::claim(Origin::signed(CHARLIE), SETM)); + assert_ok!(Vesting::claim(Origin::signed(CHARLIE))); - assert_ok!(Tokens::ensure_can_withdraw( - SETM, + assert_ok!(PalletBalances::ensure_can_withdraw( &CHARLIE, - 30 + 30, + WithdrawReasons::TRANSFER, + 0 )); }); } #[test] fn vested_transfer_works() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { System::set_block_number(1); let schedule = VestingSchedule { @@ -58,22 +64,22 @@ fn vested_transfer_works() { period_count: 1u32, per_period: 100u64, }; - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule.clone())); - assert_eq!(Vesting::vesting_schedules(&BOB, SETM), vec![schedule.clone()]); - System::assert_last_event(Event::Vesting(crate::Event::VestingScheduleAdded(SETM, ALICE, BOB, schedule))); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule.clone())); + assert_eq!(Vesting::vesting_schedules(&BOB), vec![schedule.clone()]); + System::assert_last_event(Event::Vesting(crate::Event::VestingScheduleAdded(ALICE, BOB, schedule))); }); } #[test] fn add_new_vesting_schedule_merges_with_current_locked_balance_and_until() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 0u64, period: 10u64, period_count: 2u32, per_period: 10u64, }; - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule)); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule)); System::set_block_number(12); @@ -83,27 +89,36 @@ fn add_new_vesting_schedule_merges_with_current_locked_balance_and_until() { period_count: 1u32, per_period: 7u64, }; - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, another_schedule)); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, another_schedule)); + + assert_eq!( + PalletBalances::locks(&BOB).get(0), + Some(&BalanceLock { + id: VESTING_LOCK_ID, + amount: 17u64, + reasons: Reasons::All, + }) + ); }); } #[test] fn cannot_use_fund_if_not_claimed() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 10u64, period: 10u64, period_count: 1u32, per_period: 50u64, }; - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule)); - assert!(Tokens::ensure_can_withdraw(SETM, &BOB, 1).is_err()); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule)); + assert!(PalletBalances::ensure_can_withdraw(&BOB, 1, WithdrawReasons::TRANSFER, 49).is_err()); }); } #[test] fn vested_transfer_fails_if_zero_period_or_count() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 1u64, period: 0u64, @@ -111,7 +126,7 @@ fn vested_transfer_fails_if_zero_period_or_count() { per_period: 100u64, }; assert_noop!( - Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule), + Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule), Error::::ZeroVestingPeriod ); @@ -122,15 +137,31 @@ fn vested_transfer_fails_if_zero_period_or_count() { per_period: 100u64, }; assert_noop!( - Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule), + Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule), Error::::ZeroVestingPeriodCount ); }); } +#[test] +fn vested_transfer_fails_if_transfer_err() { + ExtBuilder::build().execute_with(|| { + let schedule = VestingSchedule { + start: 1u64, + period: 1u64, + period_count: 1u32, + per_period: 100u64, + }; + assert_noop!( + Vesting::vested_transfer(Origin::signed(BOB), ALICE, schedule), + pallet_balances::Error::::InsufficientBalance, + ); + }); +} + #[test] fn vested_transfer_fails_if_overflow() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 1u64, period: 1u64, @@ -138,7 +169,7 @@ fn vested_transfer_fails_if_overflow() { per_period: u64::MAX, }; assert_noop!( - Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule), + Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule), ArithmeticError::Overflow, ); @@ -149,7 +180,7 @@ fn vested_transfer_fails_if_overflow() { per_period: 1u64, }; assert_noop!( - Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, another_schedule), + Vesting::vested_transfer(Origin::signed(ALICE), BOB, another_schedule), ArithmeticError::Overflow, ); }); @@ -157,7 +188,7 @@ fn vested_transfer_fails_if_overflow() { #[test] fn vested_transfer_fails_if_bad_origin() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 0u64, period: 10u64, @@ -165,7 +196,7 @@ fn vested_transfer_fails_if_bad_origin() { per_period: 100u64, }; assert_noop!( - Vesting::vested_transfer(Origin::signed(CHARLIE), SETM, BOB, schedule), + Vesting::vested_transfer(Origin::signed(CHARLIE), BOB, schedule), BadOrigin ); }); @@ -173,32 +204,31 @@ fn vested_transfer_fails_if_bad_origin() { #[test] fn claim_works() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 0u64, period: 10u64, period_count: 2u32, per_period: 10u64, }; - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule)); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule)); System::set_block_number(11); // remain locked if not claimed - assert!(Tokens::transfer(Origin::signed(BOB), ALICE, SETM, 10).is_err()); + assert!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 10).is_err()); // unlocked after claiming - assert_ok!(Vesting::claim(Origin::signed(BOB), SETM)); - assert!(VestingSchedules::::contains_key(BOB, SETM)); - assert_ok!(Tokens::transfer(Origin::signed(BOB), ALICE, SETM, 10)); + assert_ok!(Vesting::claim(Origin::signed(BOB))); + assert!(VestingSchedules::::contains_key(BOB)); + assert_ok!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 10)); // more are still locked - assert!(Tokens::transfer(Origin::signed(BOB), ALICE, SETM, 1).is_err()); + assert!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 1).is_err()); System::set_block_number(21); // claim more - assert_ok!(Vesting::claim(Origin::signed(BOB), SETM)); - assert!(!VestingSchedules::::contains_key(BOB, SETM)); - assert_ok!(Tokens::transfer(Origin::signed(BOB), ALICE, SETM, 10)); + assert_ok!(Vesting::claim(Origin::signed(BOB))); + assert!(!VestingSchedules::::contains_key(BOB)); + assert_ok!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 10)); // all used up - assert_eq!(Tokens::free_balance(SETM, &BOB), 0); assert_eq!(PalletBalances::free_balance(BOB), 0); // no locks anymore @@ -208,39 +238,47 @@ fn claim_works() { #[test] fn claim_for_works() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 0u64, period: 10u64, period_count: 2u32, per_period: 10u64, }; - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule)); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule)); - assert_ok!(Vesting::claim_for(Origin::signed(ALICE), SETM, BOB)); + assert_ok!(Vesting::claim_for(Origin::signed(ALICE), BOB)); - assert!(VestingSchedules::::contains_key(&BOB, SETM)); + assert_eq!( + PalletBalances::locks(&BOB).get(0), + Some(&BalanceLock { + id: VESTING_LOCK_ID, + amount: 20u64, + reasons: Reasons::All, + }) + ); + assert!(VestingSchedules::::contains_key(&BOB)); System::set_block_number(21); - assert_ok!(Vesting::claim_for(Origin::signed(ALICE), SETM, BOB)); + assert_ok!(Vesting::claim_for(Origin::signed(ALICE), BOB)); // no locks anymore assert_eq!(PalletBalances::locks(&BOB), vec![]); - assert!(!VestingSchedules::::contains_key(&BOB, SETM)); + assert!(!VestingSchedules::::contains_key(&BOB)); }); } #[test] fn update_vesting_schedules_works() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 0u64, period: 10u64, period_count: 2u32, per_period: 10u64, }; - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule)); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule)); let updated_schedule = VestingSchedule { start: 0u64, @@ -250,39 +288,45 @@ fn update_vesting_schedules_works() { }; assert_ok!(Vesting::update_vesting_schedules( Origin::root(), - SETM, BOB, vec![updated_schedule] )); System::set_block_number(11); - assert_ok!(Vesting::claim(Origin::signed(BOB), SETM)); - assert!(Tokens::transfer(Origin::signed(BOB), ALICE, SETM, 1).is_err()); + assert_ok!(Vesting::claim(Origin::signed(BOB))); + assert!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 1).is_err()); System::set_block_number(21); - assert_ok!(Vesting::claim(Origin::signed(BOB), SETM)); - assert_ok!(Tokens::transfer(Origin::signed(BOB), ALICE, SETM, 10)); + assert_ok!(Vesting::claim(Origin::signed(BOB))); + assert_ok!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 10)); // empty vesting schedules cleanup the storage and unlock the fund - assert!(VestingSchedules::::contains_key(BOB, SETM)); - - assert_ok!(Vesting::update_vesting_schedules(Origin::root(), SETM, BOB, vec![])); - assert!(!VestingSchedules::::contains_key(BOB, SETM)); + assert!(VestingSchedules::::contains_key(BOB)); + assert_eq!( + PalletBalances::locks(&BOB).get(0), + Some(&BalanceLock { + id: VESTING_LOCK_ID, + amount: 10u64, + reasons: Reasons::All, + }) + ); + assert_ok!(Vesting::update_vesting_schedules(Origin::root(), BOB, vec![])); + assert!(!VestingSchedules::::contains_key(BOB)); assert_eq!(PalletBalances::locks(&BOB), vec![]); }); } #[test] fn update_vesting_schedules_fails_if_unexpected_existing_locks() { - ExtBuilder::default().build().execute_with(|| { - assert_ok!(Tokens::transfer(Origin::signed(ALICE), BOB, SETM, 1)); - Tokens::set_lock(*b"prelocks", SETM, &BOB, 0u64); + ExtBuilder::build().execute_with(|| { + assert_ok!(PalletBalances::transfer(Origin::signed(ALICE), BOB, 1)); + PalletBalances::set_lock(*b"prelocks", &BOB, 0u64, WithdrawReasons::all()); }); } #[test] fn vested_transfer_check_for_min() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 1u64, period: 1u64, @@ -290,7 +334,7 @@ fn vested_transfer_check_for_min() { per_period: 3u64, }; assert_noop!( - Vesting::vested_transfer(Origin::signed(BOB), SETM, ALICE, schedule), + Vesting::vested_transfer(Origin::signed(BOB), ALICE, schedule), Error::::AmountLow ); }); @@ -298,14 +342,14 @@ fn vested_transfer_check_for_min() { #[test] fn multiple_vesting_schedule_claim_works() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 0u64, period: 10u64, period_count: 2u32, per_period: 10u64, }; - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule.clone())); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule.clone())); let schedule2 = VestingSchedule { start: 0u64, @@ -313,21 +357,21 @@ fn multiple_vesting_schedule_claim_works() { period_count: 3u32, per_period: 10u64, }; - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule2.clone())); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule2.clone())); - assert_eq!(Vesting::vesting_schedules(&BOB, SETM), vec![schedule, schedule2.clone()]); + assert_eq!(Vesting::vesting_schedules(&BOB), vec![schedule, schedule2.clone()]); System::set_block_number(21); - assert_ok!(Vesting::claim(Origin::signed(BOB), SETM)); + assert_ok!(Vesting::claim(Origin::signed(BOB))); - assert_eq!(Vesting::vesting_schedules(&BOB, SETM), vec![schedule2]); + assert_eq!(Vesting::vesting_schedules(&BOB), vec![schedule2]); System::set_block_number(31); - assert_ok!(Vesting::claim(Origin::signed(BOB), SETM)); + assert_ok!(Vesting::claim(Origin::signed(BOB))); - assert!(!VestingSchedules::::contains_key(&BOB, SETM)); + assert!(!VestingSchedules::::contains_key(&BOB)); assert_eq!(PalletBalances::locks(&BOB), vec![]); }); @@ -335,24 +379,24 @@ fn multiple_vesting_schedule_claim_works() { #[test] fn exceeding_maximum_schedules_should_fail() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::build().execute_with(|| { let schedule = VestingSchedule { start: 0u64, period: 10u64, period_count: 2u32, per_period: 10u64, }; - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule.clone())); - assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule.clone())); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule.clone())); + assert_ok!(Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule.clone())); assert_noop!( - Vesting::vested_transfer(Origin::signed(ALICE), SETM, BOB, schedule.clone()), + Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule.clone()), Error::::MaxVestingSchedulesExceeded ); let schedules = vec![schedule.clone(), schedule.clone(), schedule]; assert_noop!( - Vesting::update_vesting_schedules(Origin::root(), SETM, BOB, schedules), + Vesting::update_vesting_schedules(Origin::root(), BOB, schedules), Error::::MaxVestingSchedulesExceeded ); }); diff --git a/lib-serml/vesting/src/weights.rs b/lib-serml/vesting/src/weights.rs index 40bc2ecf1..5c2f350f3 100644 --- a/lib-serml/vesting/src/weights.rs +++ b/lib-serml/vesting/src/weights.rs @@ -1,4 +1,4 @@ -//! Autogenerated weights for module_vesting +//! Autogenerated weights for orlm_vesting //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 //! DATE: 2021-05-04, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] @@ -10,7 +10,7 @@ // --chain=dev // --steps=50 // --repeat=20 -// --pallet=module_vesting +// --pallet=orlm_vesting // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -28,7 +28,7 @@ use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; use sp_std::marker::PhantomData; -/// Weight functions needed for module_vesting. +/// Weight functions needed for orml_vesting. pub trait WeightInfo { fn vested_transfer() -> Weight; fn claim(i: u32, ) -> Weight; diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 94b82d46e..5871a97c5 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -113,7 +113,7 @@ module-nft = { path = "../lib-serml/nft", default-features = false } module-prices = { path = "../lib-serml/prices", default-features = false } module-support = { path = "../lib-serml/support", default-features = false } -module-vesting = { path = "../lib-serml/vesting", default-features = false } +orml-vesting = { path = "../lib-orml/vesting", default-features = false } primitives = { package = "setheum-primitives", path = "../primitives", default-features = false } runtime-common = { path = "common", default-features = false } @@ -184,6 +184,7 @@ std = [ "orml-tokens/std", "orml-authority/std", "orml-benchmarking/std", + "orml-vesting/std", "module-airdrop/std", "auction-manager/std", @@ -203,7 +204,7 @@ std = [ "module-prices/std", "module-support/std", "module-transaction-pause/std", - "module-vesting/std", + "orml-vesting/std", "primitives/std", @@ -232,5 +233,5 @@ runtime-benchmarks = [ "module-nft/runtime-benchmarks", "orml-benchmarking", "orml-authority/runtime-benchmarks", - "module-vesting/runtime-benchmarks", + "orml-vesting/runtime-benchmarks", ] \ No newline at end of file diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index b7b896077..c1727231d 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -2037,7 +2037,7 @@ impl_runtime_apis! { orml_list_benchmark!(list, extra, module_prices, benchmarking::prices); // orml_list_benchmark!(list, extra, module_evm_accounts, benchmarking::evm_accounts); orml_list_benchmark!(list, extra, module_currencies, benchmarking::currencies); - // orml_list_benchmark!(list, extra, module_vesting, benchmarking::vesting); + orml_list_benchmark!(list, extra, orml_vesting, benchmarking::vesting); orml_list_benchmark!(list, extra, orml_tokens, benchmarking::tokens); orml_list_benchmark!(list, extra, orml_auction, benchmarking::auction); @@ -2097,7 +2097,7 @@ impl_runtime_apis! { orml_add_benchmark!(params, batches, orml_tokens, benchmarking::tokens); orml_add_benchmark!(params, batches, orml_auction, benchmarking::auction); - // orml_add_benchmark!(params, batches, module_vesting, benchmarking::vesting); + orml_add_benchmark!(params, batches, orml_vesting, benchmarking::vesting); orml_add_benchmark!(params, batches, orml_authority, benchmarking::authority); orml_add_benchmark!(params, batches, orml_oracle, benchmarking::oracle); diff --git a/runtime/src/weights/module_vesting.rs b/runtime/src/weights/module_vesting.rs index 2d0bccf57..c6b532f98 100644 --- a/runtime/src/weights/module_vesting.rs +++ b/runtime/src/weights/module_vesting.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Autogenerated weights for module_vesting +//! Autogenerated weights for orml_vesting //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 //! DATE: 2021-02-28, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] @@ -42,9 +42,9 @@ use frame_support::{traits::Get, weights::Weight}; use sp_std::marker::PhantomData; -/// Weight functions for module_vesting. +/// Weight functions for orml_vesting. pub struct WeightInfo(PhantomData); -impl module_vesting::WeightInfo for WeightInfo { +impl orml_vesting::WeightInfo for WeightInfo { fn vested_transfer() -> Weight { (72_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(4 as Weight)) From 0524b141f08e26c4bf3ba8ed910fb55d519b4c39 Mon Sep 17 00:00:00 2001 From: "Muhammad-Jibril B.A" Date: Tue, 21 Dec 2021 15:10:59 +0800 Subject: [PATCH 2/2] vesting cement --- Cargo.lock | 17 +---------------- lib-serml/vesting/src/weights.rs | 2 +- runtime/Cargo.toml | 8 ++++---- runtime/src/lib.rs | 6 +++--- runtime/src/weights/module_vesting.rs | 6 +++--- 5 files changed, 12 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fd0e79ee4..525bb135e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4720,21 +4720,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "orml-vesting" -version = "0.4.1-dev" -dependencies = [ - "frame-support", - "frame-system", - "pallet-balances", - "parity-scale-codec", - "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", -] - [[package]] name = "owning_ref" version = "0.4.1" @@ -7969,6 +7954,7 @@ dependencies = [ "module-support", "module-transaction-pause", "module-transaction-payment", + "module-vesting", "orml-auction", "orml-authority", "orml-benchmarking", @@ -7977,7 +7963,6 @@ dependencies = [ "orml-oracle-rpc-runtime-api", "orml-tokens", "orml-traits", - "orml-vesting", "pallet-authority-discovery", "pallet-authorship", "pallet-babe", diff --git a/lib-serml/vesting/src/weights.rs b/lib-serml/vesting/src/weights.rs index 5c2f350f3..bbe618b55 100644 --- a/lib-serml/vesting/src/weights.rs +++ b/lib-serml/vesting/src/weights.rs @@ -28,7 +28,7 @@ use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; use sp_std::marker::PhantomData; -/// Weight functions needed for orml_vesting. +/// Weight functions needed for module_vesting. pub trait WeightInfo { fn vested_transfer() -> Weight; fn claim(i: u32, ) -> Weight; diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 5871a97c5..0ce33ff6f 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -113,7 +113,7 @@ module-nft = { path = "../lib-serml/nft", default-features = false } module-prices = { path = "../lib-serml/prices", default-features = false } module-support = { path = "../lib-serml/support", default-features = false } -orml-vesting = { path = "../lib-orml/vesting", default-features = false } +module-vesting = { path = "../lib-serml/vesting", default-features = false } primitives = { package = "setheum-primitives", path = "../primitives", default-features = false } runtime-common = { path = "common", default-features = false } @@ -184,7 +184,7 @@ std = [ "orml-tokens/std", "orml-authority/std", "orml-benchmarking/std", - "orml-vesting/std", + "module-vesting/std", "module-airdrop/std", "auction-manager/std", @@ -204,7 +204,7 @@ std = [ "module-prices/std", "module-support/std", "module-transaction-pause/std", - "orml-vesting/std", + "module-vesting/std", "primitives/std", @@ -233,5 +233,5 @@ runtime-benchmarks = [ "module-nft/runtime-benchmarks", "orml-benchmarking", "orml-authority/runtime-benchmarks", - "orml-vesting/runtime-benchmarks", + "module-vesting/runtime-benchmarks", ] \ No newline at end of file diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index c1727231d..e89a02542 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1207,7 +1207,7 @@ parameter_types! { impl module_vesting::Config for Runtime { type Event = Event; - type MultiCurrency = Currencies; + type Currency = pallet_balances::Pallet; type MinVestedTransfer = MinVestedTransfer; type MaxVestingSchedules = MaxVestingSchedules; type VestedTransferOrigin = EnsureWeb3SettersClub; @@ -2037,7 +2037,7 @@ impl_runtime_apis! { orml_list_benchmark!(list, extra, module_prices, benchmarking::prices); // orml_list_benchmark!(list, extra, module_evm_accounts, benchmarking::evm_accounts); orml_list_benchmark!(list, extra, module_currencies, benchmarking::currencies); - orml_list_benchmark!(list, extra, orml_vesting, benchmarking::vesting); + orml_list_benchmark!(list, extra, module_vesting, benchmarking::vesting); orml_list_benchmark!(list, extra, orml_tokens, benchmarking::tokens); orml_list_benchmark!(list, extra, orml_auction, benchmarking::auction); @@ -2097,7 +2097,7 @@ impl_runtime_apis! { orml_add_benchmark!(params, batches, orml_tokens, benchmarking::tokens); orml_add_benchmark!(params, batches, orml_auction, benchmarking::auction); - orml_add_benchmark!(params, batches, orml_vesting, benchmarking::vesting); + orml_add_benchmark!(params, batches, module_vesting, benchmarking::vesting); orml_add_benchmark!(params, batches, orml_authority, benchmarking::authority); orml_add_benchmark!(params, batches, orml_oracle, benchmarking::oracle); diff --git a/runtime/src/weights/module_vesting.rs b/runtime/src/weights/module_vesting.rs index c6b532f98..2d0bccf57 100644 --- a/runtime/src/weights/module_vesting.rs +++ b/runtime/src/weights/module_vesting.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Autogenerated weights for orml_vesting +//! Autogenerated weights for module_vesting //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 //! DATE: 2021-02-28, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] @@ -42,9 +42,9 @@ use frame_support::{traits::Get, weights::Weight}; use sp_std::marker::PhantomData; -/// Weight functions for orml_vesting. +/// Weight functions for module_vesting. pub struct WeightInfo(PhantomData); -impl orml_vesting::WeightInfo for WeightInfo { +impl module_vesting::WeightInfo for WeightInfo { fn vested_transfer() -> Weight { (72_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(4 as Weight))