Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit f735d3e

Browse files
authored
Fix vesting logic (#4864)
* Fix vesting logic * Bump runtime version * Docs.
1 parent 8b6b093 commit f735d3e

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
8181
// and set impl_version to 0. If only runtime
8282
// implementation changes and behavior does not, then leave spec_version as
8383
// is and increment impl_version.
84-
spec_version: 214,
85-
impl_version: 3,
84+
spec_version: 215,
85+
impl_version: 0,
8686
apis: RUNTIME_API_VERSIONS,
8787
};
8888

frame/support/src/traits.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,10 @@ pub trait VestingSchedule<AccountId> {
620620
///
621621
/// If there already exists a vesting schedule for the given account, an `Err` is returned
622622
/// and nothing is updated.
623+
///
624+
/// Is a no-op if the amount to be vested is zero.
625+
///
626+
/// NOTE: This doesn't alter the free balance of the account.
623627
fn add_vesting_schedule(
624628
who: &AccountId,
625629
locked: <Self::Currency as Currency<AccountId>>::Balance,
@@ -628,6 +632,8 @@ pub trait VestingSchedule<AccountId> {
628632
) -> DispatchResult;
629633

630634
/// Remove a vesting schedule for a given account.
635+
///
636+
/// NOTE: This doesn't alter the free balance of the account.
631637
fn remove_vesting_schedule(who: &AccountId);
632638
}
633639

frame/vesting/src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use codec::{Encode, Decode};
5252
use sp_runtime::{DispatchResult, RuntimeDebug, traits::{
5353
StaticLookup, Zero, SimpleArithmetic, MaybeSerializeDeserialize, Saturating, Convert
5454
}};
55-
use frame_support::{decl_module, decl_event, decl_storage, ensure, decl_error};
55+
use frame_support::{decl_module, decl_event, decl_storage, decl_error};
5656
use frame_support::traits::{
5757
Currency, LockableCurrency, VestingSchedule, WithdrawReason, LockIdentifier
5858
};
@@ -212,16 +212,18 @@ impl<T: Trait> Module<T> {
212212
/// (Re)set or remove the module's currency lock on `who`'s account in accordance with their
213213
/// current unvested amount.
214214
fn update_lock(who: T::AccountId) -> DispatchResult {
215-
ensure!(Vesting::<T>::contains_key(&who), Error::<T>::NotVesting);
216-
let unvested = Self::vesting_balance(&who);
217-
if unvested.is_zero() {
215+
let vesting = Self::vesting(&who).ok_or(Error::<T>::NotVesting)?;
216+
let now = <frame_system::Module<T>>::block_number();
217+
let locked_now = vesting.locked_at::<T::BlockNumberToBalance>(now);
218+
219+
if locked_now.is_zero() {
218220
T::Currency::remove_lock(VESTING_ID, &who);
219221
Vesting::<T>::remove(&who);
220222
Self::deposit_event(RawEvent::VestingCompleted(who));
221223
} else {
222224
let reasons = WithdrawReason::Transfer | WithdrawReason::Reserve;
223-
T::Currency::set_lock(VESTING_ID, &who, unvested, reasons);
224-
Self::deposit_event(RawEvent::VestingUpdated(who, unvested));
225+
T::Currency::set_lock(VESTING_ID, &who, locked_now, reasons);
226+
Self::deposit_event(RawEvent::VestingUpdated(who, locked_now));
225227
}
226228
Ok(())
227229
}
@@ -249,6 +251,10 @@ impl<T: Trait> VestingSchedule<T::AccountId> for Module<T> where
249251
/// If there already exists a vesting schedule for the given account, an `Err` is returned
250252
/// and nothing is updated.
251253
///
254+
/// On success, a linearly reducing amount of funds will be locked. In order to realise any
255+
/// reduction of the lock over time as it diminishes, the account owner must use `vest` or
256+
/// `vest_other`.
257+
///
252258
/// Is a no-op if the amount to be vested is zero.
253259
fn add_vesting_schedule(
254260
who: &T::AccountId,

0 commit comments

Comments
 (0)