Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pallet vesting / update to use fungible #1760

Closed
wants to merge 17 commits into from
51 changes: 39 additions & 12 deletions polkadot/runtime/common/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

use frame_support::{
ensure,
traits::{Currency, Get, IsSubType, VestingSchedule},
traits::{
tokens::{fungible, fungible::freeze::VestingSchedule, Balance},
Get, IsSubType,
},
weights::Weight,
DefaultNoBound,
};
Expand All @@ -41,8 +44,9 @@ use sp_std::{fmt::Debug, prelude::*};

type CurrencyOf<T> = <<T as Config>::VestingSchedule as VestingSchedule<
<T as frame_system::Config>::AccountId,
>>::Currency;
type BalanceOf<T> = <CurrencyOf<T> as Currency<<T as frame_system::Config>::AccountId>>::Balance;
>>::Fungible;
type BalanceOf<T> =
<CurrencyOf<T> as fungible::Inspect<<T as frame_system::Config>::AccountId>>::Balance;

pub trait WeightInfo {
fn claim() -> Weight;
Expand Down Expand Up @@ -161,6 +165,7 @@ pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_runtime::traits::MaybeSerializeDeserialize;

#[pallet::pallet]
#[pallet::without_storage_info]
Expand All @@ -171,7 +176,14 @@ pub mod pallet {
pub trait Config: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type VestingSchedule: VestingSchedule<Self::AccountId, Moment = BlockNumberFor<Self>>;
type VestingSchedule: VestingSchedule<
Self::AccountId,
Moment = BlockNumberFor<Self>,
Fungible = Self::Fungible,
>;
type Fungible: fungible::Inspect<Self::AccountId, Balance = Self::Balance>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type Fungible: fungible::Inspect<Self::AccountId, Balance = Self::Balance>
type Currency: fungible::Inspect<Self::AccountId, Balance = Self::Balance>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or type NativeToken, as suggested in #226 (comment)

+ fungible::Mutate<Self::AccountId>;
type Balance: Balance + MaybeSerializeDeserialize;
gilescope marked this conversation as resolved.
Show resolved Hide resolved
#[pallet::constant]
type Prefix: Get<&'static [u8]>;
type MoveClaimOrigin: EnsureOrigin<Self::RuntimeOrigin>;
Expand Down Expand Up @@ -551,6 +563,8 @@ impl<T: Config> Pallet<T> {
}

fn process_claim(signer: EthereumAddress, dest: T::AccountId) -> sp_runtime::DispatchResult {
use frame_support::traits::fungible::Mutate;

let balance_due = <Claims<T>>::get(&signer).ok_or(Error::<T>::SignerHasNoClaim)?;

let new_total = Self::total().checked_sub(&balance_due).ok_or(Error::<T>::PotUnderflow)?;
Expand All @@ -561,7 +575,9 @@ impl<T: Config> Pallet<T> {
}

// We first need to deposit the balance to ensure that the account exists.
CurrencyOf::<T>::deposit_creating(&dest, balance_due);
//TODO: we need to ensure that this creates the account if it does not already exist
// was: CurrencyOf::<T>::deposit_creating(&dest, balance_due);
CurrencyOf::<T>::mint_into(&dest, balance_due)?;

// Check if this claim should have a vesting schedule.
if let Some(vs) = vesting {
Expand Down Expand Up @@ -700,6 +716,7 @@ mod secp_utils {
#[cfg(test)]
mod tests {
use super::*;
use frame_support::traits::tokens::Preservation;
use hex_literal::hex;
use secp_utils::*;

Expand All @@ -713,7 +730,7 @@ mod tests {
assert_err, assert_noop, assert_ok,
dispatch::{GetDispatchInfo, Pays},
ord_parameter_types, parameter_types,
traits::{ConstU32, ExistenceRequirement, WithdrawReasons},
traits::{ConstU32, WithdrawReasons},
};
use pallet_balances;
use sp_runtime::{
Expand All @@ -731,7 +748,7 @@ mod tests {
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Vesting: pallet_vesting::{Pallet, Call, Storage, Config<T>, Event<T>},
Vesting: pallet_vesting::{Pallet, Call, Storage, Config<T>, Event<T>, FreezeReason},
Claims: claims::{Pallet, Call, Storage, Config<T>, Event<T>, ValidateUnsigned},
}
);
Expand Down Expand Up @@ -767,6 +784,7 @@ mod tests {

parameter_types! {
pub const ExistentialDeposit: u64 = 1;
pub const MaxFreezes: u32 = 1;
}

impl pallet_balances::Config for Test {
Expand All @@ -780,9 +798,9 @@ mod tests {
type ReserveIdentifier = [u8; 8];
type WeightInfo = ();
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type FreezeIdentifier = RuntimeFreezeReason;
type MaxHolds = ConstU32<1>;
type MaxFreezes = ConstU32<1>;
type MaxFreezes = MaxFreezes;
}

parameter_types! {
Expand All @@ -793,12 +811,18 @@ mod tests {

impl pallet_vesting::Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type Currency = Balances;
type Balance = u64;
type BlockNumberToBalance = Identity;
type MinVestedTransfer = MinVestedTransfer;
type MaxFreezes = MaxFreezes;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
const MAX_VESTING_SCHEDULES: u32 = 28;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
Expand All @@ -811,6 +835,8 @@ mod tests {
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type VestingSchedule = Vesting;
type Fungible = Balances;
type Balance = u64;
type Prefix = Prefix;
type MoveClaimOrigin = frame_system::EnsureSignedBy<Six, u64>;
type WeightInfo = TestWeightInfo;
Expand Down Expand Up @@ -1197,11 +1223,11 @@ mod tests {

// Make sure we can not transfer the vested balance.
assert_err!(
<Balances as Currency<_>>::transfer(
<Balances as fungible::Mutate<_>>::transfer(
&69,
&80,
180,
ExistenceRequirement::AllowDeath
Preservation::Expendable
),
TokenError::Frozen,
);
Expand Down Expand Up @@ -1291,7 +1317,8 @@ mod tests {
#[test]
fn claiming_while_vested_doesnt_work() {
new_test_ext().execute_with(|| {
CurrencyOf::<Test>::make_free_balance_be(&69, total_claims());
use frame_support::traits::tokens::fungible::Mutate;
CurrencyOf::<Test>::set_balance(&69, total_claims());
assert_eq!(Balances::free_balance(69), total_claims());
// A user is already vested
assert_ok!(<Test as Config>::VestingSchedule::add_vesting_schedule(
Expand Down
29 changes: 21 additions & 8 deletions polkadot/runtime/common/src/purchase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

use frame_support::{
pallet_prelude::*,
traits::{Currency, EnsureOrigin, ExistenceRequirement, Get, VestingSchedule},
traits::{
tokens::{fungible, fungible::freeze::VestingSchedule, Preservation},
EnsureOrigin, Get,
},
};
use frame_system::pallet_prelude::*;
pub use pallet::*;
Expand All @@ -32,7 +35,7 @@ use sp_runtime::{
use sp_std::prelude::*;

type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
<<T as Config>::Currency as fungible::Inspect<<T as frame_system::Config>::AccountId>>::Balance;

/// The kind of statement an account needs to make for a claim to be valid.
#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)]
Expand Down Expand Up @@ -101,13 +104,15 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// Balances Pallet
type Currency: Currency<Self::AccountId>;
type Currency: fungible::Inspect<Self::AccountId>
+ fungible::Mutate<Self::AccountId>
+ fungible::MutateFreeze<Self::AccountId>;

/// Vesting Pallet
type VestingSchedule: VestingSchedule<
Self::AccountId,
Moment = BlockNumberFor<Self>,
Currency = Self::Currency,
Fungible = Self::Currency,
>;

/// The origin allowed to set account status.
Expand Down Expand Up @@ -317,6 +322,7 @@ pub mod pallet {
Accounts::<T>::try_mutate(
&who,
|status: &mut AccountStatus<BalanceOf<T>>| -> DispatchResult {
use frame_support::traits::fungible::Mutate;
// Account has a valid status (not Invalid, Pending, or Completed)...
ensure!(status.validity.is_valid(), Error::<T>::InvalidAccount);

Expand All @@ -329,7 +335,7 @@ pub mod pallet {
&payment_account,
&who,
total_balance,
ExistenceRequirement::AllowDeath,
Preservation::Expendable,
)?;

if !status.locked_balance.is_zero() {
Expand Down Expand Up @@ -501,7 +507,7 @@ mod tests {
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Vesting: pallet_vesting::{Pallet, Call, Storage, Config<T>, Event<T>},
Vesting: pallet_vesting::{Pallet, Call, Storage, Config<T>, Event<T>, FreezeReason},
Purchase: purchase::{Pallet, Call, Storage, Event<T>},
}
);
Expand Down Expand Up @@ -539,6 +545,7 @@ mod tests {

parameter_types! {
pub const ExistentialDeposit: u64 = 1;
pub const MaxFreezes: u32 = 2; // Vesting + ?
}

impl pallet_balances::Config for Test {
Expand All @@ -552,9 +559,9 @@ mod tests {
type ReserveIdentifier = [u8; 8];
type WeightInfo = ();
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type FreezeIdentifier = RuntimeFreezeReason;
type MaxHolds = ConstU32<1>;
type MaxFreezes = ConstU32<1>;
type MaxFreezes = MaxFreezes;
}

parameter_types! {
Expand All @@ -565,12 +572,18 @@ mod tests {

impl pallet_vesting::Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type Currency = Balances;
type Balance = u64;
type BlockNumberToBalance = Identity;
type MinVestedTransfer = MinVestedTransfer;
type MaxFreezes = MaxFreezes;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
const MAX_VESTING_SCHEDULES: u32 = 28;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
Expand Down
17 changes: 13 additions & 4 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ parameter_types! {
pub const ExistentialDeposit: Balance = EXISTENTIAL_DEPOSIT;
pub const MaxLocks: u32 = 50;
pub const MaxReserves: u32 = 50;
pub const MaxFreezes: u32 = 2; // Vesting and ?
}

impl pallet_balances::Config for Runtime {
Expand All @@ -292,8 +293,8 @@ impl pallet_balances::Config for Runtime {
type MaxReserves = MaxReserves;
type ReserveIdentifier = [u8; 8];
type WeightInfo = weights::pallet_balances::WeightInfo<Runtime>;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<1>;
type FreezeIdentifier = RuntimeFreezeReason;
type MaxFreezes = MaxFreezes;
type RuntimeHoldReason = RuntimeHoldReason;
type MaxHolds = ConstU32<1>;
}
Expand Down Expand Up @@ -562,6 +563,8 @@ parameter_types! {
impl claims::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type VestingSchedule = Vesting;
type Fungible = Balances;
type Balance = Balance;
type Prefix = Prefix;
type MoveClaimOrigin = EnsureRoot<AccountId>;
type WeightInfo = weights::runtime_common_claims::WeightInfo<Runtime>;
Expand Down Expand Up @@ -664,12 +667,18 @@ parameter_types! {

impl pallet_vesting::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type Currency = Balances;
type Balance = Balance;
type BlockNumberToBalance = ConvertInto;
type MinVestedTransfer = MinVestedTransfer;
type MaxFreezes = MaxFreezes;
type WeightInfo = weights::pallet_vesting::WeightInfo<Runtime>;
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
const MAX_VESTING_SCHEDULES: u32 = 28;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
Expand Down Expand Up @@ -1060,7 +1069,7 @@ impl pallet_balances::Config<NisCounterpartInstance> for Runtime {
type ReserveIdentifier = [u8; 8];
type WeightInfo = weights::pallet_balances_nis_counterpart_balances::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type FreezeIdentifier = RuntimeFreezeReason;
type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
}
Expand Down Expand Up @@ -1265,7 +1274,7 @@ construct_runtime! {
Recovery: pallet_recovery::{Pallet, Call, Storage, Event<T>} = 27,

// Vesting. Usable initially, but removed once all vesting is finished.
Vesting: pallet_vesting::{Pallet, Call, Storage, Event<T>, Config<T>} = 28,
Vesting: pallet_vesting::{Pallet, Call, Storage, Event<T>, Config<T>, FreezeReason} = 28,

// System scheduler.
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 29,
Expand Down
15 changes: 12 additions & 3 deletions polkadot/runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ parameter_types! {
pub const ExistentialDeposit: Balance = 1 * CENTS;
pub storage MaxLocks: u32 = 50;
pub const MaxReserves: u32 = 50;
pub const MaxFreezes: u32 = 1;
}

impl pallet_balances::Config for Runtime {
Expand All @@ -229,9 +230,9 @@ impl pallet_balances::Config for Runtime {
type ReserveIdentifier = [u8; 8];
type WeightInfo = ();
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type FreezeIdentifier = RuntimeFreezeReason;
type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
type MaxFreezes = MaxFreezes;
}

parameter_types! {
Expand Down Expand Up @@ -452,6 +453,8 @@ parameter_types! {

impl claims::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Fungible = Balances;
type Balance = Balance;
type VestingSchedule = Vesting;
type Prefix = Prefix;
type MoveClaimOrigin = frame_system::EnsureRoot<AccountId>;
Expand All @@ -466,12 +469,18 @@ parameter_types! {

impl pallet_vesting::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type Currency = Balances;
type Balance = Balance;
type BlockNumberToBalance = ConvertInto;
type MinVestedTransfer = MinVestedTransfer;
type MaxFreezes = MaxFreezes;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
const MAX_VESTING_SCHEDULES: u32 = 28;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

impl pallet_sudo::Config for Runtime {
Expand Down Expand Up @@ -687,7 +696,7 @@ construct_runtime! {
Claims: claims::{Pallet, Call, Storage, Event<T>, Config<T>, ValidateUnsigned},

// Vesting. Usable initially, but removed once all vesting is finished.
Vesting: pallet_vesting::{Pallet, Call, Storage, Event<T>, Config<T>},
Vesting: pallet_vesting::{Pallet, Call, Storage, Event<T>, Config<T>, FreezeReason},

// Parachains runtime modules
Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>},
Expand Down
Loading
Loading