Skip to content

Commit

Permalink
fix: rename lock/unlock references to freeze/thaw; update ci-local ta…
Browse files Browse the repository at this point in the history
…rget
  • Loading branch information
Matthew Orris committed Jan 3, 2024
1 parent 4d1cef6 commit cabb0f6
Show file tree
Hide file tree
Showing 8 changed files with 770 additions and 506 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ lint-audit:
format-lint: format lint

.PHONY: ci-local
ci-local: check lint lint-audit test e2e-tests
ci-local: check lint lint-audit test js e2e-tests

.PHONY: upgrade
upgrade-local:
Expand Down
1,128 changes: 696 additions & 432 deletions e2e/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pallets/time-release/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-time-release"
description = "Provides scheduled balance locking mechanism, in a *graded release* way."
description = "Provides scheduled balance freezing mechanism, in a *graded release* way."
authors = ["Frequency"]
edition = "2021"
homepage = "https://frequency.xyz"
Expand Down
4 changes: 2 additions & 2 deletions pallets/time-release/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ This pallet is a fork of the [ORML-vesting]( [vesting](https://github.com/open-w

## Overview

Time-release module provides a means of scheduled balance lock on an account. It uses the *graded release* way, which unlocks a specific amount of balance every period of time, until all balance unlocked.
Time-release module provides a means of scheduled balance freeze on an account. It uses the *graded release* way, which thaws a specific amount of balance every period of time, until all balance thawed.

### Release Schedule

The schedule of a release on hold is described by data structure `ReleaseSchedule`: from the block number of `start`, for every `period` amount of blocks, `per_period` amount of balance would unlocked, until number of periods `period_count` reached. Note in release schedules, *time* is measured by block number. All `ReleaseSchedule`s under an account could be queried in chain state.
The schedule of a release on hold is described by data structure `ReleaseSchedule`: from the block number of `start`, for every `period` amount of blocks, `per_period` amount of balance would thawed, until number of periods `period_count` reached. Note in release schedules, *time* is measured by block number. All `ReleaseSchedule`s under an account could be queried in chain state.
72 changes: 36 additions & 36 deletions pallets/time-release/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
//!
//! ## Overview
//!
//! Time-release module provides a means of scheduled balance lock on an account. It
//! uses the *graded release* way, which unlocks a specific amount of balance
//! every period of time, until all balance unlocked.
//! Time-release module provides a means of scheduled balance freeze on an account. It
//! uses the *graded release* way, which thaws a specific amount of balance
//! every period of time, until all balances are thawed.
//!
//! ### Release Schedule
//!
//! The schedule of a release is described by data structure `ReleaseSchedule`:
//! from the block number of `start`, for every `period` amount of blocks,
//! `per_period` amount of balance would unlocked, until number of periods
//! `per_period` amount of balance would thaw, until number of periods
//! `period_count` reached. Note in release schedules, *time* is measured by
//! block number. All `ReleaseSchedule`s under an account could be queried in
//! chain state.
Expand All @@ -22,7 +22,7 @@
//! ### Dispatchable Functions
//!
//! - `transfer` - Add a new release schedule for an account.
//! - `claim` - Claim unlocked balances.
//! - `claim` - Claim thawed balances.
//! - `update_release_schedules` - Update all release schedules under an
//! account, `root` origin required.
// Substrate macros are tripping the clippy::expect_used lint.
Expand Down Expand Up @@ -97,7 +97,7 @@ pub mod module {
/// Creates a freeze reason for this pallet that is aggregated by `construct_runtime`.
#[pallet::composite_enum]
pub enum FreezeReason {
/// Funds are currently locked and are not yet liquid.
/// Funds are currently frozen and are not yet liquid.
TimeReleaseVesting,
}

Expand All @@ -115,7 +115,7 @@ pub mod module {
/// We need MaybeSerializeDeserialize because of the genesis config.
type Balance: Balance + MaybeSerializeDeserialize;

/// The currency trait used to set a lock on a balance.
/// The currency trait used to set a freeze on a balance.
type Currency: MutateFreeze<Self::AccountId, Id = Self::RuntimeFreezeReason>
+ InspectFungible<Self::AccountId, Balance = Self::Balance>
+ Mutate<Self::AccountId>;
Expand Down Expand Up @@ -143,8 +143,8 @@ pub mod module {
ZeroReleasePeriod,
/// Period-count is zero
ZeroReleasePeriodCount,
/// Insufficient amount of balance to lock
InsufficientBalanceToLock,
/// Insufficient amount of balance to freeze
InsufficientBalanceToFreeze,
/// This account have too many release schedules
TooManyReleaseSchedules,
/// The transfer amount is too low
Expand Down Expand Up @@ -255,7 +255,7 @@ pub mod module {

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Claim unlocked balances.
/// Claim thawed balances.
///
/// # Events
/// * [`Event::Claimed`]
Expand All @@ -264,9 +264,9 @@ pub mod module {
#[pallet::weight(T::WeightInfo::claim(<T as Config>::MaxReleaseSchedules::get() / 2))]
pub fn claim(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let locked_amount = Self::do_claim(&who)?;
let frozen_amount = Self::do_claim(&who)?;

Self::deposit_event(Event::Claimed { who, amount: locked_amount });
Self::deposit_event(Event::Claimed { who, amount: frozen_amount });
Ok(())
}

Expand All @@ -277,7 +277,7 @@ pub mod module {
///
/// # Errors
///
/// * [`Error::InsufficientBalanceToLock] - Insuffience amount of balance to lock.
/// * [`Error::InsufficientBalanceToFreeze] - Insufficient amount of balance to freeze.
/// * [`Error::MaxReleaseSchedulesExceeded] - Failed because the maximum release schedules was exceeded-
/// * [`ArithmeticError::Overflow] - Failed because of an overflow.
///
Expand All @@ -295,7 +295,7 @@ pub mod module {
ensure!(
T::Currency::balance(&from) >=
schedule.total_amount().ok_or(ArithmeticError::Overflow)?,
Error::<T>::InsufficientBalanceToLock,
Error::<T>::InsufficientBalanceToFreeze,
);
}

Expand All @@ -316,7 +316,7 @@ pub mod module {
///
/// # Errors
///
/// * [`Error::InsufficientBalanceToLock] - Insuffience amount of balance to lock.
/// * [`Error::InsufficientBalanceToFreeze] - Insufficient amount of balance to freeze.
/// * [`Error::MaxReleaseSchedulesExceeded] - Failed because the maximum release schedules was exceeded-
/// * [`ArithmeticError::Overflow] - Failed because of an overflow.
///
Expand All @@ -336,7 +336,7 @@ pub mod module {
Ok(())
}

/// Claim unlocked balances on behalf for an account.
/// Claim thawed balances on behalf for an account.
///
/// # Events
/// * [`Event::Claimed`]
Expand All @@ -349,24 +349,24 @@ pub mod module {
) -> DispatchResult {
ensure_signed(origin)?;
let who = T::Lookup::lookup(dest)?;
let locked_amount = Self::do_claim(&who)?;
let frozen_amount = Self::do_claim(&who)?;

Self::deposit_event(Event::Claimed { who, amount: locked_amount });
Self::deposit_event(Event::Claimed { who, amount: frozen_amount });
Ok(())
}
}
}

impl<T: Config> Pallet<T> {
fn do_claim(who: &T::AccountId) -> Result<BalanceOf<T>, DispatchError> {
let locked = Self::prune_and_get_locked_balance(who);
if locked.is_zero() {
Self::delete_lock(who)?;
let frozen = Self::prune_and_get_frozen_balance(who);
if frozen.is_zero() {
Self::delete_freeze(who)?;
} else {
Self::update_lock(who, locked)?;
Self::update_freeze(who, frozen)?;
}

Ok(locked)
Ok(frozen)
}

/// Deletes schedules that have released all funds up to a block-number.
Expand All @@ -375,7 +375,7 @@ impl<T: Config> Pallet<T> {
block_number: BlockNumberFor<T>,
) -> BoundedVec<ReleaseScheduleOf<T>, T::MaxReleaseSchedules> {
let mut schedules = Self::release_schedules(who);
schedules.retain(|schedule| !schedule.locked_amount(block_number).is_zero());
schedules.retain(|schedule| !schedule.frozen_amount(block_number).is_zero());

if schedules.is_empty() {
ReleaseSchedules::<T>::remove(who);
Expand All @@ -386,15 +386,15 @@ impl<T: Config> Pallet<T> {
schedules
}

/// Returns locked balance based on current block number.
fn prune_and_get_locked_balance(who: &T::AccountId) -> BalanceOf<T> {
/// Returns frozen balance based on current block number.
fn prune_and_get_frozen_balance(who: &T::AccountId) -> BalanceOf<T> {
let now = T::BlockNumberProvider::current_block_number();

let schedules = Self::prune_schedules_for(&who, now);

let total = schedules
.iter()
.fold(BalanceOf::<T>::zero(), |acc, schedule| acc + schedule.locked_amount(now));
.fold(BalanceOf::<T>::zero(), |acc, schedule| acc + schedule.frozen_amount(now));

total
}
Expand All @@ -406,13 +406,13 @@ impl<T: Config> Pallet<T> {
) -> DispatchResult {
let schedule_amount = ensure_valid_release_schedule::<T>(&schedule)?;

let total_amount = Self::prune_and_get_locked_balance(to)
let total_amount = Self::prune_and_get_frozen_balance(to)
.checked_add(&schedule_amount)
.ok_or(ArithmeticError::Overflow)?;

T::Currency::transfer(from, to, schedule_amount, Preservation::Expendable)?;

Self::update_lock(&to, total_amount)?;
Self::update_freeze(&to, total_amount)?;

<ReleaseSchedules<T>>::try_append(to, schedule)
.map_err(|_| Error::<T>::MaxReleaseSchedulesExceeded)?;
Expand All @@ -428,7 +428,7 @@ impl<T: Config> Pallet<T> {
BoundedVec::<ReleaseScheduleOf<T>, T::MaxReleaseSchedules>::try_from(schedules)
.map_err(|_| Error::<T>::MaxReleaseSchedulesExceeded)?;

// empty release schedules cleanup the storage and unlock the fund
// empty release schedules cleanup the storage and thaw the funds
if bounded_schedules.is_empty() {
Self::delete_release_schedules(who)?;
return Ok(())
Expand All @@ -443,20 +443,20 @@ impl<T: Config> Pallet<T> {
},
)?;

ensure!(T::Currency::balance(who) >= total_amount, Error::<T>::InsufficientBalanceToLock,);
ensure!(T::Currency::balance(who) >= total_amount, Error::<T>::InsufficientBalanceToFreeze,);

Self::update_lock(&who, total_amount)?;
Self::update_freeze(&who, total_amount)?;
Self::set_schedules_for(who, bounded_schedules);

Ok(())
}

fn update_lock(who: &T::AccountId, locked: BalanceOf<T>) -> DispatchResult {
T::Currency::set_freeze(&FreezeReason::TimeReleaseVesting.into(), who, locked)?;
fn update_freeze(who: &T::AccountId, frozen: BalanceOf<T>) -> DispatchResult {
T::Currency::set_freeze(&FreezeReason::TimeReleaseVesting.into(), who, frozen)?;
Ok(())
}

fn delete_lock(who: &T::AccountId) -> DispatchResult {
fn delete_freeze(who: &T::AccountId) -> DispatchResult {
T::Currency::thaw(&FreezeReason::TimeReleaseVesting.into(), who)?;
Ok(())
}
Expand All @@ -470,7 +470,7 @@ impl<T: Config> Pallet<T> {

fn delete_release_schedules(who: &T::AccountId) -> DispatchResult {
<ReleaseSchedules<T>>::remove(who);
Self::delete_lock(who)?;
Self::delete_freeze(who)?;
Ok(())
}
}
Expand Down
8 changes: 4 additions & 4 deletions pallets/time-release/src/migration/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where
ReleaseSchedules::<T>::iter()
.map(|(account_id, _)| account_id)
.for_each(|account_id| {
let (total_amount, cal_fn_weight) = calculate_total_scheduled_locks_for_account::<T>(&account_id);
let (total_amount, cal_fn_weight) = calculate_total_scheduled_frozen_for_account::<T>(&account_id);

let trans_fn_weight = MigrationToV2::<T, OldCurrency>::translate_lock_to_freeze(
&account_id,
Expand Down Expand Up @@ -164,7 +164,7 @@ where
}
}

fn calculate_total_scheduled_locks_for_account<T: Config>(
fn calculate_total_scheduled_frozen_for_account<T: Config>(
account_id: &T::AccountId,
) -> (BalanceOf<T>, Weight) {
let total = ReleaseSchedules::<T>::get(&account_id) // 1r
Expand Down Expand Up @@ -199,7 +199,7 @@ mod test {
(DAVE, 2, 3, 1, 5),
];

create_schedules_and_set_lock(schedules);
create_schedules_and_set_freeze(schedules);

assert_eq!(
pallet_balances::Pallet::<Test>::locks(&DAVE)
Expand Down Expand Up @@ -237,7 +237,7 @@ mod test {
})
}

fn create_schedules_and_set_lock(schedules: Vec<(AccountId, u32, u32, u32, u64)>) {
fn create_schedules_and_set_freeze(schedules: Vec<(AccountId, u32, u32, u32, u64)>) {
schedules.iter().for_each(|(who, start, period, period_count, per_period)| {
let mut bounded_schedules = ReleaseSchedules::<Test>::get(who);
let new_schedule = types::ReleaseSchedule {
Expand Down
Loading

0 comments on commit cabb0f6

Please sign in to comment.