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

Dependency Injection Trait Locker for Uniques Pallet #11025

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,7 @@ impl pallet_uniques::Config for Runtime {
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
type Locker = ();
}

impl pallet_transaction_storage::Config for Runtime {
Expand Down
6 changes: 3 additions & 3 deletions frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ pub use misc::{
Backing, ConstBool, ConstI128, ConstI16, ConstI32, ConstI64, ConstI8, ConstU128, ConstU16,
ConstU32, ConstU64, ConstU8, DefensiveSaturating, EnsureInherentsAreFirst, EqualPrivilegeOnly,
EstimateCallFee, ExecuteBlock, ExtrinsicCall, Get, GetBacking, GetDefault, HandleLifetime,
IsSubType, IsType, Len, OffchainWorker, OnKilledAccount, OnNewAccount, PreimageProvider,
PreimageRecipient, PrivilegeCmp, SameOrOther, Time, TryCollect, TryDrop, UnixTime,
WrapperKeepOpaque, WrapperOpaque,
IsSubType, IsType, Len, Locker, OffchainWorker, OnKilledAccount, OnNewAccount,
PreimageProvider, PreimageRecipient, PrivilegeCmp, SameOrOther, Time, TryCollect, TryDrop,
UnixTime, WrapperKeepOpaque, WrapperOpaque,
};
#[doc(hidden)]
pub use misc::{DEFENSIVE_OP_INTERNAL_ERROR, DEFENSIVE_OP_PUBLIC_ERROR};
Expand Down
18 changes: 18 additions & 0 deletions frame/support/src/traits/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,24 @@ impl<Hash> PreimageRecipient<Hash> for () {
fn unnote_preimage(_: &Hash) {}
}

/// Trait to handle asset locking mechanism to ensure interactions with the asset can be implemented
/// downstream to extend logic of Uniques current functionality
HashWarlock marked this conversation as resolved.
Show resolved Hide resolved
pub trait Locker<ClassId, InstanceId> {
/// Check if the asset should be locked and prevent interactions with the asset from executing.
fn is_locked(class: ClassId, instance: InstanceId) -> bool;
}

impl<ClassId, InstanceId> Locker<ClassId, InstanceId> for () {
/// Check if the asset should be locked and prevent interactions with the asset from executing.
/// Default will be false if not implemented downstream
///
/// Note: The logic check in this function must be constant time and consistent for benchmarks
/// to work
fn is_locked(_class: ClassId, _instance: InstanceId) -> bool {
false
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions frame/uniques/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
) -> DispatchResult {
let class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::UnknownClass)?;
ensure!(!class_details.is_frozen, Error::<T, I>::Frozen);
ensure!(!T::Locker::is_locked(class, instance), Error::<T, I>::Locked);

let mut details =
Asset::<T, I>::get(&class, &instance).ok_or(Error::<T, I>::UnknownClass)?;
Expand Down
7 changes: 6 additions & 1 deletion frame/uniques/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub mod weights;

use codec::{Decode, Encode};
use frame_support::traits::{
BalanceStatus::Reserved, Currency, EnsureOriginWithArg, ReservableCurrency,
BalanceStatus::Reserved, Currency, EnsureOriginWithArg, Locker, ReservableCurrency,
};
use frame_system::Config as SystemConfig;
use sp_runtime::{
Expand Down Expand Up @@ -108,6 +108,9 @@ pub mod pallet {
Self::ClassId,
>;

/// Locker trait to enable Locking mechanism downstream
type Locker: Locker<Self::ClassId, Self::InstanceId>;

/// The basic amount of funds that must be reserved for an asset class.
#[pallet::constant]
type ClassDeposit: Get<DepositBalanceOf<Self, I>>;
Expand Down Expand Up @@ -352,6 +355,8 @@ pub mod pallet {
Unapproved,
/// The named owner has not signed ownership of the class is acceptable.
Unaccepted,
/// The asset instance is locked.
Locked,
}

impl<T: Config<I>, I: 'static> Pallet<T, I> {
Expand Down
1 change: 1 addition & 0 deletions frame/uniques/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl Config for Test {
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<frame_system::EnsureSigned<u64>>;
type ForceOrigin = frame_system::EnsureRoot<u64>;
type Locker = ();
type ClassDeposit = ConstU64<2>;
type InstanceDeposit = ConstU64<1>;
type MetadataDepositBase = ConstU64<1>;
Expand Down