Skip to content

Commit

Permalink
Merge pull request #7 from Lezek123/configurable-vesting-lock
Browse files Browse the repository at this point in the history
Configurable vesting lock
  • Loading branch information
mnaamani authored Aug 30, 2022
2 parents 6cbe177 + f9fd7d9 commit 3f915f0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
5 changes: 4 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use frame_support::{
traits::{
AsEnsureOriginWithArg, ConstU128, ConstU16, ConstU32, Currency, EnsureOneOf,
EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter, KeyOwnerProofSystem,
LockIdentifier, Nothing, OnUnbalanced, U128CurrencyToVote,
LockIdentifier, Nothing, OnUnbalanced, U128CurrencyToVote, WithdrawReasons,
},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
Expand Down Expand Up @@ -1314,6 +1314,8 @@ impl pallet_society::Config for Runtime {

parameter_types! {
pub const MinVestedTransfer: Balance = 100 * DOLLARS;
pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons =
WithdrawReasons::except(WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE);
}

impl pallet_vesting::Config for Runtime {
Expand All @@ -1322,6 +1324,7 @@ impl pallet_vesting::Config for Runtime {
type BlockNumberToBalance = ConvertInto;
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = pallet_vesting::weights::SubstrateWeight<Runtime>;
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
// `VestingInfo` encode length is 36bytes. 28 schedules gets encoded as 1009 bytes, which is the
// highest number of schedules that encodes less than 2^10.
const MAX_VESTING_SCHEDULES: u32 = 28;
Expand Down
3 changes: 2 additions & 1 deletion frame/vesting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

A simple module providing a means of placing a linear curve on an account's locked balance. This
module ensures that there is a lock in place preventing the balance to drop below the *unvested*
amount for any reason other than transaction fee payment.
amount for reason other than the ones specified in `UnvestedFundsAllowedWithdrawReasons`
configuration value.

As the amount vested increases over time, the amount unvested reduces. However, locks remain in
place and explicit action is needed on behalf of the user to ensure that the amount locked is
Expand Down
13 changes: 10 additions & 3 deletions frame/vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
//!
//! A simple pallet providing a means of placing a linear curve on an account's locked balance. This
//! pallet ensures that there is a lock in place preventing the balance to drop below the *unvested*
//! amount for any reason other than transaction fee payment.
//! amount for any reason other than the ones specified in `UnvestedFundsAllowedWithdrawReasons`
//! configuration value.
//!
//! As the amount vested increases over time, the amount unvested reduces. However, locks remain in
//! place and explicit action is needed on behalf of the user to ensure that the amount locked is
Expand Down Expand Up @@ -169,6 +170,10 @@ pub mod pallet {
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;

/// Reasons that determine under which conditions the balance may drop below
/// the unvested amount.
type UnvestedFundsAllowedWithdrawReasons: Get<WithdrawReasons>;

/// Maximum number of vesting schedules an account may have at a given moment.
const MAX_VESTING_SCHEDULES: u32;
}
Expand Down Expand Up @@ -248,7 +253,9 @@ pub mod pallet {
Vesting::<T>::try_append(who, vesting_info)
.expect("Too many vesting schedules at genesis.");

let reasons = WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE;
let reasons =
WithdrawReasons::except(T::UnvestedFundsAllowedWithdrawReasons::get());

T::Currency::set_lock(VESTING_ID, who, locked, reasons);
}
}
Expand Down Expand Up @@ -571,7 +578,7 @@ impl<T: Config> Pallet<T> {
T::Currency::remove_lock(VESTING_ID, who);
Self::deposit_event(Event::<T>::VestingCompleted { account: who.clone() });
} else {
let reasons = WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE;
let reasons = WithdrawReasons::except(T::UnvestedFundsAllowedWithdrawReasons::get());
T::Currency::set_lock(VESTING_ID, who, total_locked_now, reasons);
Self::deposit_event(Event::<T>::VestingUpdated {
account: who.clone(),
Expand Down
5 changes: 4 additions & 1 deletion frame/vesting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use frame_support::{
parameter_types,
traits::{ConstU32, ConstU64, GenesisBuild},
traits::{ConstU32, ConstU64, GenesisBuild, WithdrawReasons},
};
use sp_core::H256;
use sp_runtime::{
Expand Down Expand Up @@ -87,6 +87,8 @@ impl pallet_balances::Config for Test {
}
parameter_types! {
pub const MinVestedTransfer: u64 = 256 * 2;
pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons =
WithdrawReasons::except(WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE);
pub static ExistentialDeposit: u64 = 0;
}
impl Config for Test {
Expand All @@ -96,6 +98,7 @@ impl Config for Test {
const MAX_VESTING_SCHEDULES: u32 = 3;
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
}

pub struct ExtBuilder {
Expand Down

0 comments on commit 3f915f0

Please sign in to comment.