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

Commit

Permalink
Dependency Injection Trait Locker for Uniques Pallet (#11025)
Browse files Browse the repository at this point in the history
* Create a dependency injection trait named Locker that can be implemented downstream to enable locking of an asset. Use case defined in RMRK substrate pallet PR76

* Formatting

* Change impl Locker function name to is_locked

* Remove unused import

* Add docstring header

* Remove impl_locker file and add Locker trait to frame_support::traits

* Expose Locker from frame_support::traits::misc

* Formatting

* Move to tokens folder

* Move to tokens folder

* Format and remove Locker from misc traits

* Punctuation

* Update frame/support/src/traits/tokens/misc.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Giles Cope <gilescope@gmail.com>
  • Loading branch information
4 people authored Apr 28, 2022
1 parent c4693eb commit a79c69a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,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
2 changes: 1 addition & 1 deletion frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use tokens::{
},
fungible, fungibles,
imbalance::{Imbalance, OnUnbalanced, SignedImbalance},
BalanceStatus, ExistenceRequirement, WithdrawReasons,
BalanceStatus, ExistenceRequirement, Locker, WithdrawReasons,
};

mod members;
Expand Down
2 changes: 1 addition & 1 deletion frame/support/src/traits/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ pub mod nonfungibles;
pub use imbalance::Imbalance;
pub use misc::{
AssetId, Balance, BalanceConversion, BalanceStatus, DepositConsequence, ExistenceRequirement,
WithdrawConsequence, WithdrawReasons,
Locker, WithdrawConsequence, WithdrawReasons,
};
16 changes: 16 additions & 0 deletions frame/support/src/traits/tokens/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,19 @@ pub trait BalanceConversion<InBalance, AssetId, OutBalance> {
type Error;
fn to_asset_balance(balance: InBalance, asset_id: AssetId) -> Result<OutBalance, Self::Error>;
}

/// Trait to handle asset locking mechanism to ensure interactions with the asset can be implemented
/// downstream to extend logic of Uniques current functionality.
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 () {
// 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
}
}
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,
tokens::Locker, BalanceStatus::Reserved, Currency, EnsureOriginWithArg, 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

0 comments on commit a79c69a

Please sign in to comment.