#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::unused_unit)]
use codec::HasCompact;
use frame_support::{
ensure,
pallet_prelude::*,
traits::{Currency, EnsureOrigin, ExistenceRequirement, Get, LockIdentifier, LockableCurrency, WithdrawReasons},
transactional,
};
use frame_system::{ensure_root, ensure_signed, pallet_prelude::*};
use sp_runtime::{
traits::{AtLeast32Bit, CheckedAdd, Saturating, StaticLookup, Zero},
DispatchResult, RuntimeDebug,
};
use sp_std::{
cmp::{Eq, PartialEq},
vec::Vec,
};
mod default_weight;
mod mock;
mod tests;
pub use module::*;
pub const MAX_VESTINGS: usize = 20;
pub const VESTING_LOCK_ID: LockIdentifier = *b"ormlvest";
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug)]
pub struct VestingSchedule<BlockNumber, Balance: HasCompact> {
pub start: BlockNumber,
pub period: BlockNumber,
pub period_count: u32,
#[codec(compact)]
pub per_period: Balance,
}
impl<BlockNumber: AtLeast32Bit + Copy, Balance: AtLeast32Bit + Copy> VestingSchedule<BlockNumber, Balance> {
pub fn end(&self) -> Option<BlockNumber> {
self.period
.checked_mul(&self.period_count.into())?
.checked_add(&self.start)
}
pub fn total_amount(&self) -> Option<Balance> {
self.per_period.checked_mul(&self.period_count.into())
}
pub fn locked_amount(&self, time: BlockNumber) -> Balance {
let full = time
.saturating_sub(self.start)
.checked_div(&self.period)
.expect("ensured non-zero period; qed");
let unrealized = self.period_count.saturating_sub(full.unique_saturated_into());
self.per_period
.checked_mul(&unrealized.into())
.expect("ensured non-overflow total amount; qed")
}
}
#[frame_support::pallet]
pub mod module {
use super::*;
pub trait WeightInfo {
fn vested_transfer() -> Weight;
fn claim(i: u32) -> Weight;
fn update_vesting_schedules(i: u32) -> Weight;
}
pub(crate) type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub(crate) type VestingScheduleOf<T> = VestingSchedule<<T as frame_system::Config>::BlockNumber, BalanceOf<T>>;
pub type ScheduledItem<T> = (
<T as frame_system::Config>::AccountId,
<T as frame_system::Config>::BlockNumber,
<T as frame_system::Config>::BlockNumber,
u32,
BalanceOf<T>,
);
#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>;
#[pallet::constant]
type MinVestedTransfer: Get<BalanceOf<Self>>;
type VestedTransferOrigin: EnsureOrigin<Self::Origin, Success = Self::AccountId>;
type WeightInfo: WeightInfo;
}
#[pallet::error]
pub enum Error<T> {
ZeroVestingPeriod,
ZeroVestingPeriodCount,
NumOverflow,
InsufficientBalanceToLock,
TooManyVestingSchedules,
AmountLow,
}
#[pallet::event]
#[pallet::generate_deposit(fn deposit_event)]
pub enum Event<T: Config> {
VestingScheduleAdded(T::AccountId, T::AccountId, VestingScheduleOf<T>),
Claimed(T::AccountId, BalanceOf<T>),
VestingSchedulesUpdated(T::AccountId),
}
#[pallet::storage]
#[pallet::getter(fn vesting_schedules)]
pub type VestingSchedules<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, Vec<VestingScheduleOf<T>>, ValueQuery>;
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub vesting: Vec<ScheduledItem<T>>,
}
#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig { vesting: vec![] }
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
self.vesting
.iter()
.for_each(|(who, start, period, period_count, per_period)| {
let total = *per_period * Into::<BalanceOf<T>>::into(*period_count);
assert!(
T::Currency::free_balance(who) >= total,
"Account do not have enough balance"
);
T::Currency::set_lock(VESTING_LOCK_ID, who, total, WithdrawReasons::all());
VestingSchedules::<T>::insert(
who,
vec![VestingSchedule {
start: *start,
period: *period,
period_count: *period_count,
per_period: *per_period,
}],
);
});
}
}
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(T::WeightInfo::claim((MAX_VESTINGS / 2) as u32))]
pub fn claim(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let locked_amount = Self::do_claim(&who);
Self::deposit_event(Event::Claimed(who, locked_amount));
Ok(().into())
}
#[pallet::weight(T::WeightInfo::vested_transfer())]
pub fn vested_transfer(
origin: OriginFor<T>,
dest: <T::Lookup as StaticLookup>::Source,
schedule: VestingScheduleOf<T>,
) -> DispatchResultWithPostInfo {
let from = T::VestedTransferOrigin::ensure_origin(origin)?;
let to = T::Lookup::lookup(dest)?;
Self::do_vested_transfer(&from, &to, schedule.clone())?;
Self::deposit_event(Event::VestingScheduleAdded(from, to, schedule));
Ok(().into())
}
#[pallet::weight(T::WeightInfo::update_vesting_schedules(vesting_schedules.len() as u32))]
pub fn update_vesting_schedules(
origin: OriginFor<T>,
who: <T::Lookup as StaticLookup>::Source,
vesting_schedules: Vec<VestingScheduleOf<T>>,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
let account = T::Lookup::lookup(who)?;
Self::do_update_vesting_schedules(&account, vesting_schedules)?;
Self::deposit_event(Event::VestingSchedulesUpdated(account));
Ok(().into())
}
}
}
impl<T: Config> Pallet<T> {
fn do_claim(who: &T::AccountId) -> BalanceOf<T> {
let locked = Self::locked_balance(who);
if locked.is_zero() {
T::Currency::remove_lock(VESTING_LOCK_ID, who);
} else {
T::Currency::set_lock(VESTING_LOCK_ID, who, locked, WithdrawReasons::all());
}
locked
}
fn locked_balance(who: &T::AccountId) -> BalanceOf<T> {
let now = <frame_system::Module<T>>::block_number();
<VestingSchedules<T>>::mutate_exists(who, |maybe_schedules| {
let total = if let Some(schedules) = maybe_schedules.as_mut() {
let mut total: BalanceOf<T> = Zero::zero();
schedules.retain(|s| {
let amount = s.locked_amount(now);
total = total.saturating_add(amount);
!amount.is_zero()
});
total
} else {
Zero::zero()
};
if total.is_zero() {
*maybe_schedules = None;
}
total
})
}
#[transactional]
fn do_vested_transfer(from: &T::AccountId, to: &T::AccountId, schedule: VestingScheduleOf<T>) -> DispatchResult {
let schedule_amount = Self::ensure_valid_vesting_schedule(&schedule)?;
ensure!(
<VestingSchedules<T>>::decode_len(to).unwrap_or(0) < MAX_VESTINGS,
Error::<T>::TooManyVestingSchedules
);
let total_amount = Self::locked_balance(to)
.checked_add(&schedule_amount)
.ok_or(Error::<T>::NumOverflow)?;
T::Currency::transfer(from, to, schedule_amount, ExistenceRequirement::AllowDeath)?;
T::Currency::set_lock(VESTING_LOCK_ID, to, total_amount, WithdrawReasons::all());
<VestingSchedules<T>>::append(to, schedule);
Ok(())
}
fn do_update_vesting_schedules(who: &T::AccountId, schedules: Vec<VestingScheduleOf<T>>) -> DispatchResult {
let total_amount = schedules.iter().try_fold::<_, _, Result<BalanceOf<T>, Error<T>>>(
Zero::zero(),
|acc_amount, schedule| {
let amount = Self::ensure_valid_vesting_schedule(schedule)?;
Ok(acc_amount + amount)
},
)?;
ensure!(
T::Currency::free_balance(who) >= total_amount,
Error::<T>::InsufficientBalanceToLock,
);
T::Currency::set_lock(VESTING_LOCK_ID, who, total_amount, WithdrawReasons::all());
<VestingSchedules<T>>::insert(who, schedules);
Ok(())
}
fn ensure_valid_vesting_schedule(schedule: &VestingScheduleOf<T>) -> Result<BalanceOf<T>, Error<T>> {
ensure!(!schedule.period.is_zero(), Error::<T>::ZeroVestingPeriod);
ensure!(!schedule.period_count.is_zero(), Error::<T>::ZeroVestingPeriodCount);
ensure!(schedule.end().is_some(), Error::<T>::NumOverflow);
let total = schedule.total_amount().ok_or(Error::<T>::NumOverflow)?;
ensure!(total >= T::MinVestedTransfer::get(), Error::<T>::AmountLow);
Ok(total)
}
}