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

Commit

Permalink
HoldReason: Improve usage (#13869)
Browse files Browse the repository at this point in the history
* HoldReason: Improve usage

`HoldReason` was switched recently to use the `composite_enum` attribute that will merge the enums
from all pallets in the runtime to `RuntimeHoldReason`. `pallet-nis` was still requiring that the
variant was passed as constant to call `hold`. The proper implementation is to use the `HoldReason`
from inside the pallet directly when calling `hold`. This is done by adding a `RuntimeHoldReason` as
type to the `Config` trait and requiring that `Currency` is using the same reason. Besides that the
pr changes the name `HoldIdentifier` in `pallet_balances::Config` to `RuntimeHoldReason`.

* Update frame/nis/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Review comment

* Fixes

---------

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
  • Loading branch information
bkchr and ggwpez authored May 24, 2023
1 parent b2b90a6 commit 79d37ef
Show file tree
Hide file tree
Showing 60 changed files with 97 additions and 127 deletions.
2 changes: 1 addition & 1 deletion bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
20 changes: 3 additions & 17 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ use pallet_nis::WithMaximumOf;
use pallet_session::historical as pallet_session_historical;
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
use scale_info::TypeInfo;
use sp_api::impl_runtime_apis;
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
use sp_consensus_grandpa::AuthorityId as GrandpaId;
Expand Down Expand Up @@ -439,17 +438,6 @@ parameter_types! {
pub const MaxReserves: u32 = 50;
}

/// A reason for placing a hold on funds.
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, Debug, TypeInfo,
)]
pub enum HoldReason {
/// The NIS Pallet has reserved it for a non-fungible receipt.
Nis,
/// Used by the NFT Fractionalization Pallet.
NftFractionalization,
}

impl pallet_balances::Config for Runtime {
type MaxLocks = MaxLocks;
type MaxReserves = MaxReserves;
Expand All @@ -462,7 +450,7 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = HoldReason;
type RuntimeHoldReason = RuntimeHoldReason;
type MaxHolds = ConstU32<2>;
}

Expand Down Expand Up @@ -1520,7 +1508,6 @@ parameter_types! {
pub const ThawThrottle: (Perquintill, BlockNumber) = (Perquintill::from_percent(25), 5);
pub Target: Perquintill = Perquintill::zero();
pub const NisPalletId: PalletId = PalletId(*b"py/nis ");
pub const NisHoldReason: HoldReason = HoldReason::Nis;
}

impl pallet_nis::Config for Runtime {
Expand All @@ -1544,7 +1531,7 @@ impl pallet_nis::Config for Runtime {
type IntakePeriod = IntakePeriod;
type MaxIntakeWeight = MaxIntakeWeight;
type ThawThrottle = ThawThrottle;
type HoldReason = NisHoldReason;
type RuntimeHoldReason = RuntimeHoldReason;
}

parameter_types! {
Expand Down Expand Up @@ -1618,7 +1605,6 @@ parameter_types! {
pub const NftFractionalizationPalletId: PalletId = PalletId(*b"fraction");
pub NewAssetSymbol: BoundedVec<u8, StringLimit> = (*b"FRAC").to_vec().try_into().unwrap();
pub NewAssetName: BoundedVec<u8, StringLimit> = (*b"Frac").to_vec().try_into().unwrap();
pub const NftFractionalizationHoldReason: HoldReason = HoldReason::NftFractionalization;
}

impl pallet_nft_fractionalization::Config for Runtime {
Expand All @@ -1636,7 +1622,7 @@ impl pallet_nft_fractionalization::Config for Runtime {
type Nfts = Nfts;
type PalletId = NftFractionalizationPalletId;
type WeightInfo = pallet_nft_fractionalization::weights::SubstrateWeight<Runtime>;
type HoldReason = NftFractionalizationHoldReason;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down
1 change: 0 additions & 1 deletion client/executor/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use sc_executor_common::{
};
use sc_executor_wasmtime::InstantiationStrategy;
use sc_runtime_test::wasm_binary_unwrap as test_runtime;
use sp_wasm_interface::HostFunctions as _;
use std::sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc,
Expand Down
2 changes: 1 addition & 1 deletion frame/alliance/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl pallet_balances::Config for Test {
type ReserveIdentifier = [u8; 8];
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/asset-rate/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type HoldIdentifier = ();
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type MaxHolds = ();
type MaxFreezes = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/assets/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type FreezeIdentifier = ();
type MaxHolds = ();
type MaxFreezes = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/atomic-swap/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/balances/src/impl_fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<T: Config<I>, I: 'static> fungible::Mutate<T::AccountId> for Pallet<T, I> {
impl<T: Config<I>, I: 'static> fungible::MutateHold<T::AccountId> for Pallet<T, I> {}

impl<T: Config<I>, I: 'static> fungible::InspectHold<T::AccountId> for Pallet<T, I> {
type Reason = T::HoldIdentifier;
type Reason = T::RuntimeHoldReason;

fn total_balance_on_hold(who: &T::AccountId) -> T::Balance {
Self::account(who).reserved
Expand Down
6 changes: 3 additions & 3 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ pub mod pallet {
/// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/`
type ReserveIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;

/// The ID type for holds.
type HoldIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;
/// The overarching hold reason.
type RuntimeHoldReason: Parameter + Member + MaxEncodedLen + Ord + Copy;

/// The ID type for freezes.
type FreezeIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;
Expand Down Expand Up @@ -437,7 +437,7 @@ pub mod pallet {
_,
Blake2_128Concat,
T::AccountId,
BoundedVec<IdAmount<T::HoldIdentifier, T::Balance>, T::MaxHolds>,
BoundedVec<IdAmount<T::RuntimeHoldReason, T::Balance>, T::MaxHolds>,
ValueQuery,
>;

Expand Down
2 changes: 1 addition & 1 deletion frame/balances/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Config for Test {
type MaxReserves = ConstU32<2>;
type ReserveIdentifier = TestId;
type WeightInfo = ();
type HoldIdentifier = TestId;
type RuntimeHoldReason = TestId;
type FreezeIdentifier = TestId;
type MaxFreezes = ConstU32<2>;
type MaxHolds = ConstU32<2>;
Expand Down
2 changes: 1 addition & 1 deletion frame/beefy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl pallet_balances::Config for Test {
type ExistentialDeposit = ConstU128<1>;
type AccountStore = System;
type WeightInfo = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/bounties/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}
parameter_types! {
Expand Down
2 changes: 1 addition & 1 deletion frame/child-bounties/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}
parameter_types! {
Expand Down
2 changes: 1 addition & 1 deletion frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/conviction-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}
parameter_types! {
Expand Down
2 changes: 1 addition & 1 deletion frame/election-provider-multi-phase/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl pallet_balances::Config for Runtime {
type AccountStore = System;
type MaxHolds = ConstU32<1>;
type MaxFreezes = traits::ConstU32<1>;
type HoldIdentifier = ();
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type WeightInfo = ();
}
Expand Down
2 changes: 1 addition & 1 deletion frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ mod tests {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/examples/basic/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/examples/dev-mode/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = RuntimeHoldReason;
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ mod tests {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<1>;
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ConstU32<1>;
}

Expand Down
2 changes: 1 addition & 1 deletion frame/fast-unstake/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/identity/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/indices/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/lottery/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/multisig/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
28 changes: 20 additions & 8 deletions frame/nft-fractionalization/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pub mod pallet {
sp_runtime::traits::{AccountIdConversion, StaticLookup},
traits::{
fungible::{
hold::{Inspect as HoldInspectFungible, Mutate as HoldMutateFungible},
Inspect as InspectFungible, Mutate as MutateFungible,
hold::Mutate as HoldMutateFungible, Inspect as InspectFungible,
Mutate as MutateFungible,
},
fungibles::{
metadata::{MetadataDeposit, Mutate as MutateMetadata},
Expand Down Expand Up @@ -96,11 +96,10 @@ pub mod pallet {
/// The currency mechanism, used for paying for deposits.
type Currency: InspectFungible<Self::AccountId>
+ MutateFungible<Self::AccountId>
+ HoldInspectFungible<Self::AccountId>
+ HoldMutateFungible<Self::AccountId>;
+ HoldMutateFungible<Self::AccountId, Reason = Self::RuntimeHoldReason>;

#[pallet::constant]
type HoldReason: Get<<Self::Currency as HoldInspectFungible<Self::AccountId>>::Reason>;
/// Overarching hold reason.
type RuntimeHoldReason: From<HoldReason>;

/// The deposit paid by the user locking an NFT. The deposit is returned to the original NFT
/// owner when the asset is unified and the NFT is unlocked.
Expand Down Expand Up @@ -201,6 +200,14 @@ pub mod pallet {
NftNotFractionalized,
}

/// A reason for the pallet placing a hold on funds.
#[pallet::composite_enum]
pub enum HoldReason {
/// Reserved for a fractionalized NFT.
#[codec(index = 0)]
Fractionalized,
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Lock the NFT and mint a new fungible asset.
Expand Down Expand Up @@ -239,7 +246,7 @@ pub mod pallet {

let pallet_account = Self::get_pallet_account();
let deposit = T::Deposit::get();
T::Currency::hold(&T::HoldReason::get(), &nft_owner, deposit)?;
T::Currency::hold(&HoldReason::Fractionalized.into(), &nft_owner, deposit)?;
Self::do_lock_nft(nft_collection_id, nft_id)?;
Self::do_create_asset(asset_id.clone(), pallet_account.clone())?;
Self::do_mint_asset(asset_id.clone(), &beneficiary, fractions)?;
Expand Down Expand Up @@ -303,7 +310,12 @@ pub mod pallet {
let asset_creator = details.asset_creator;
Self::do_burn_asset(asset_id.clone(), &who, details.fractions)?;
Self::do_unlock_nft(nft_collection_id, nft_id, &beneficiary)?;
T::Currency::release(&T::HoldReason::get(), &asset_creator, deposit, BestEffort)?;
T::Currency::release(
&HoldReason::Fractionalized.into(),
&asset_creator,
deposit,
BestEffort,
)?;

Self::deposit_event(Event::NftUnified {
nft_collection: nft_collection_id,
Expand Down
Loading

0 comments on commit 79d37ef

Please sign in to comment.