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

Commit 79d37ef

Browse files
bkchrggwpez
andauthored
HoldReason: Improve usage (#13869)
* 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 <[email protected]> * Review comment * Fixes --------- Co-authored-by: Oliver Tale-Yazdi <[email protected]>
1 parent b2b90a6 commit 79d37ef

File tree

60 files changed

+97
-127
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+97
-127
lines changed

bin/node-template/runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl pallet_balances::Config for Runtime {
245245
type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
246246
type FreezeIdentifier = ();
247247
type MaxFreezes = ();
248-
type HoldIdentifier = ();
248+
type RuntimeHoldReason = ();
249249
type MaxHolds = ();
250250
}
251251

bin/node/runtime/src/lib.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ use pallet_nis::WithMaximumOf;
5959
use pallet_session::historical as pallet_session_historical;
6060
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
6161
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
62-
use scale_info::TypeInfo;
6362
use sp_api::impl_runtime_apis;
6463
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
6564
use sp_consensus_grandpa::AuthorityId as GrandpaId;
@@ -439,17 +438,6 @@ parameter_types! {
439438
pub const MaxReserves: u32 = 50;
440439
}
441440

442-
/// A reason for placing a hold on funds.
443-
#[derive(
444-
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, Debug, TypeInfo,
445-
)]
446-
pub enum HoldReason {
447-
/// The NIS Pallet has reserved it for a non-fungible receipt.
448-
Nis,
449-
/// Used by the NFT Fractionalization Pallet.
450-
NftFractionalization,
451-
}
452-
453441
impl pallet_balances::Config for Runtime {
454442
type MaxLocks = MaxLocks;
455443
type MaxReserves = MaxReserves;
@@ -462,7 +450,7 @@ impl pallet_balances::Config for Runtime {
462450
type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
463451
type FreezeIdentifier = ();
464452
type MaxFreezes = ();
465-
type HoldIdentifier = HoldReason;
453+
type RuntimeHoldReason = RuntimeHoldReason;
466454
type MaxHolds = ConstU32<2>;
467455
}
468456

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

15261513
impl pallet_nis::Config for Runtime {
@@ -1544,7 +1531,7 @@ impl pallet_nis::Config for Runtime {
15441531
type IntakePeriod = IntakePeriod;
15451532
type MaxIntakeWeight = MaxIntakeWeight;
15461533
type ThawThrottle = ThawThrottle;
1547-
type HoldReason = NisHoldReason;
1534+
type RuntimeHoldReason = RuntimeHoldReason;
15481535
}
15491536

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

16241610
impl pallet_nft_fractionalization::Config for Runtime {
@@ -1636,7 +1622,7 @@ impl pallet_nft_fractionalization::Config for Runtime {
16361622
type Nfts = Nfts;
16371623
type PalletId = NftFractionalizationPalletId;
16381624
type WeightInfo = pallet_nft_fractionalization::weights::SubstrateWeight<Runtime>;
1639-
type HoldReason = NftFractionalizationHoldReason;
1625+
type RuntimeHoldReason = RuntimeHoldReason;
16401626
#[cfg(feature = "runtime-benchmarks")]
16411627
type BenchmarkHelper = ();
16421628
}

client/executor/benches/bench.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use sc_executor_common::{
2525
};
2626
use sc_executor_wasmtime::InstantiationStrategy;
2727
use sc_runtime_test::wasm_binary_unwrap as test_runtime;
28-
use sp_wasm_interface::HostFunctions as _;
2928
use std::sync::{
3029
atomic::{AtomicBool, AtomicUsize, Ordering},
3130
Arc,

frame/alliance/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl pallet_balances::Config for Test {
8989
type ReserveIdentifier = [u8; 8];
9090
type FreezeIdentifier = ();
9191
type MaxFreezes = ();
92-
type HoldIdentifier = ();
92+
type RuntimeHoldReason = ();
9393
type MaxHolds = ();
9494
}
9595

frame/asset-rate/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl pallet_balances::Config for Test {
7777
type MaxLocks = ();
7878
type MaxReserves = ();
7979
type ReserveIdentifier = [u8; 8];
80-
type HoldIdentifier = ();
80+
type RuntimeHoldReason = RuntimeHoldReason;
8181
type FreezeIdentifier = ();
8282
type MaxHolds = ();
8383
type MaxFreezes = ();

frame/assets/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl pallet_balances::Config for Test {
8787
type MaxLocks = ();
8888
type MaxReserves = ();
8989
type ReserveIdentifier = [u8; 8];
90-
type HoldIdentifier = ();
90+
type RuntimeHoldReason = ();
9191
type FreezeIdentifier = ();
9292
type MaxHolds = ();
9393
type MaxFreezes = ();

frame/atomic-swap/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl pallet_balances::Config for Test {
6464
type WeightInfo = ();
6565
type FreezeIdentifier = ();
6666
type MaxFreezes = ();
67-
type HoldIdentifier = ();
67+
type RuntimeHoldReason = ();
6868
type MaxHolds = ();
6969
}
7070

frame/babe/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl pallet_balances::Config for Test {
145145
type WeightInfo = ();
146146
type FreezeIdentifier = ();
147147
type MaxFreezes = ();
148-
type HoldIdentifier = ();
148+
type RuntimeHoldReason = ();
149149
type MaxHolds = ();
150150
}
151151

frame/balances/src/impl_fungible.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl<T: Config<I>, I: 'static> fungible::Mutate<T::AccountId> for Pallet<T, I> {
207207
impl<T: Config<I>, I: 'static> fungible::MutateHold<T::AccountId> for Pallet<T, I> {}
208208

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

212212
fn total_balance_on_hold(who: &T::AccountId) -> T::Balance {
213213
Self::account(who).reserved

frame/balances/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ pub mod pallet {
257257
/// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/`
258258
type ReserveIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;
259259

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

263263
/// The ID type for freezes.
264264
type FreezeIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;
@@ -437,7 +437,7 @@ pub mod pallet {
437437
_,
438438
Blake2_128Concat,
439439
T::AccountId,
440-
BoundedVec<IdAmount<T::HoldIdentifier, T::Balance>, T::MaxHolds>,
440+
BoundedVec<IdAmount<T::RuntimeHoldReason, T::Balance>, T::MaxHolds>,
441441
ValueQuery,
442442
>;
443443

frame/balances/src/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl Config for Test {
137137
type MaxReserves = ConstU32<2>;
138138
type ReserveIdentifier = TestId;
139139
type WeightInfo = ();
140-
type HoldIdentifier = TestId;
140+
type RuntimeHoldReason = TestId;
141141
type FreezeIdentifier = TestId;
142142
type MaxFreezes = ConstU32<2>;
143143
type MaxHolds = ConstU32<2>;

frame/beefy/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl pallet_balances::Config for Test {
161161
type ExistentialDeposit = ConstU128<1>;
162162
type AccountStore = System;
163163
type WeightInfo = ();
164-
type HoldIdentifier = ();
164+
type RuntimeHoldReason = ();
165165
type MaxHolds = ();
166166
type FreezeIdentifier = ();
167167
type MaxFreezes = ();

frame/bounties/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl pallet_balances::Config for Test {
102102
type WeightInfo = ();
103103
type FreezeIdentifier = ();
104104
type MaxFreezes = ();
105-
type HoldIdentifier = ();
105+
type RuntimeHoldReason = ();
106106
type MaxHolds = ();
107107
}
108108
parameter_types! {

frame/child-bounties/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl pallet_balances::Config for Test {
105105
type WeightInfo = ();
106106
type FreezeIdentifier = ();
107107
type MaxFreezes = ();
108-
type HoldIdentifier = ();
108+
type RuntimeHoldReason = ();
109109
type MaxHolds = ();
110110
}
111111
parameter_types! {

frame/contracts/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl pallet_balances::Config for Test {
322322
type WeightInfo = ();
323323
type FreezeIdentifier = ();
324324
type MaxFreezes = ();
325-
type HoldIdentifier = ();
325+
type RuntimeHoldReason = ();
326326
type MaxHolds = ();
327327
}
328328

frame/conviction-voting/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl pallet_balances::Config for Test {
9494
type WeightInfo = ();
9595
type FreezeIdentifier = ();
9696
type MaxFreezes = ();
97-
type HoldIdentifier = ();
97+
type RuntimeHoldReason = ();
9898
type MaxHolds = ();
9999
}
100100

frame/democracy/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl pallet_balances::Config for Test {
146146
type WeightInfo = ();
147147
type FreezeIdentifier = ();
148148
type MaxFreezes = ();
149-
type HoldIdentifier = ();
149+
type RuntimeHoldReason = ();
150150
type MaxHolds = ();
151151
}
152152
parameter_types! {

frame/election-provider-multi-phase/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl pallet_balances::Config for Runtime {
256256
type WeightInfo = ();
257257
type FreezeIdentifier = ();
258258
type MaxFreezes = ();
259-
type HoldIdentifier = ();
259+
type RuntimeHoldReason = ();
260260
type MaxHolds = ();
261261
}
262262

frame/election-provider-multi-phase/test-staking-e2e/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl pallet_balances::Config for Runtime {
126126
type AccountStore = System;
127127
type MaxHolds = ConstU32<1>;
128128
type MaxFreezes = traits::ConstU32<1>;
129-
type HoldIdentifier = ();
129+
type RuntimeHoldReason = RuntimeHoldReason;
130130
type FreezeIdentifier = ();
131131
type WeightInfo = ();
132132
}

frame/elections-phragmen/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ mod tests {
13521352
type WeightInfo = ();
13531353
type FreezeIdentifier = ();
13541354
type MaxFreezes = ();
1355-
type HoldIdentifier = ();
1355+
type RuntimeHoldReason = ();
13561356
type MaxHolds = ();
13571357
}
13581358

frame/examples/basic/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl pallet_balances::Config for Test {
8989
type WeightInfo = ();
9090
type FreezeIdentifier = ();
9191
type MaxFreezes = ();
92-
type HoldIdentifier = ();
92+
type RuntimeHoldReason = ();
9393
type MaxHolds = ();
9494
}
9595

frame/examples/dev-mode/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl pallet_balances::Config for Test {
8383
type WeightInfo = ();
8484
type FreezeIdentifier = ();
8585
type MaxFreezes = ();
86-
type HoldIdentifier = ();
86+
type RuntimeHoldReason = RuntimeHoldReason;
8787
type MaxHolds = ();
8888
}
8989

frame/executive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ mod tests {
886886
type WeightInfo = ();
887887
type FreezeIdentifier = ();
888888
type MaxFreezes = ConstU32<1>;
889-
type HoldIdentifier = ();
889+
type RuntimeHoldReason = ();
890890
type MaxHolds = ConstU32<1>;
891891
}
892892

frame/fast-unstake/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl pallet_balances::Config for Runtime {
9191
type WeightInfo = ();
9292
type FreezeIdentifier = ();
9393
type MaxFreezes = ();
94-
type HoldIdentifier = ();
94+
type RuntimeHoldReason = ();
9595
type MaxHolds = ();
9696
}
9797

frame/grandpa/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl pallet_balances::Config for Test {
144144
type WeightInfo = ();
145145
type FreezeIdentifier = ();
146146
type MaxFreezes = ();
147-
type HoldIdentifier = ();
147+
type RuntimeHoldReason = ();
148148
type MaxHolds = ();
149149
}
150150

frame/identity/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl pallet_balances::Config for Test {
8787
type WeightInfo = ();
8888
type FreezeIdentifier = ();
8989
type MaxFreezes = ();
90-
type HoldIdentifier = ();
90+
type RuntimeHoldReason = ();
9191
type MaxHolds = ();
9292
}
9393

frame/indices/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl pallet_balances::Config for Test {
7878
type WeightInfo = ();
7979
type FreezeIdentifier = ();
8080
type MaxFreezes = ();
81-
type HoldIdentifier = ();
81+
type RuntimeHoldReason = ();
8282
type MaxHolds = ();
8383
}
8484

frame/lottery/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl pallet_balances::Config for Test {
9191
type WeightInfo = ();
9292
type FreezeIdentifier = ();
9393
type MaxFreezes = ();
94-
type HoldIdentifier = ();
94+
type RuntimeHoldReason = ();
9595
type MaxHolds = ();
9696
}
9797

frame/multisig/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl pallet_balances::Config for Test {
8787
type WeightInfo = ();
8888
type FreezeIdentifier = ();
8989
type MaxFreezes = ();
90-
type HoldIdentifier = ();
90+
type RuntimeHoldReason = ();
9191
type MaxHolds = ();
9292
}
9393

frame/nft-fractionalization/src/lib.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ pub mod pallet {
6363
sp_runtime::traits::{AccountIdConversion, StaticLookup},
6464
traits::{
6565
fungible::{
66-
hold::{Inspect as HoldInspectFungible, Mutate as HoldMutateFungible},
67-
Inspect as InspectFungible, Mutate as MutateFungible,
66+
hold::Mutate as HoldMutateFungible, Inspect as InspectFungible,
67+
Mutate as MutateFungible,
6868
},
6969
fungibles::{
7070
metadata::{MetadataDeposit, Mutate as MutateMetadata},
@@ -96,11 +96,10 @@ pub mod pallet {
9696
/// The currency mechanism, used for paying for deposits.
9797
type Currency: InspectFungible<Self::AccountId>
9898
+ MutateFungible<Self::AccountId>
99-
+ HoldInspectFungible<Self::AccountId>
100-
+ HoldMutateFungible<Self::AccountId>;
99+
+ HoldMutateFungible<Self::AccountId, Reason = Self::RuntimeHoldReason>;
101100

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

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

203+
/// A reason for the pallet placing a hold on funds.
204+
#[pallet::composite_enum]
205+
pub enum HoldReason {
206+
/// Reserved for a fractionalized NFT.
207+
#[codec(index = 0)]
208+
Fractionalized,
209+
}
210+
204211
#[pallet::call]
205212
impl<T: Config> Pallet<T> {
206213
/// Lock the NFT and mint a new fungible asset.
@@ -239,7 +246,7 @@ pub mod pallet {
239246

240247
let pallet_account = Self::get_pallet_account();
241248
let deposit = T::Deposit::get();
242-
T::Currency::hold(&T::HoldReason::get(), &nft_owner, deposit)?;
249+
T::Currency::hold(&HoldReason::Fractionalized.into(), &nft_owner, deposit)?;
243250
Self::do_lock_nft(nft_collection_id, nft_id)?;
244251
Self::do_create_asset(asset_id.clone(), pallet_account.clone())?;
245252
Self::do_mint_asset(asset_id.clone(), &beneficiary, fractions)?;
@@ -303,7 +310,12 @@ pub mod pallet {
303310
let asset_creator = details.asset_creator;
304311
Self::do_burn_asset(asset_id.clone(), &who, details.fractions)?;
305312
Self::do_unlock_nft(nft_collection_id, nft_id, &beneficiary)?;
306-
T::Currency::release(&T::HoldReason::get(), &asset_creator, deposit, BestEffort)?;
313+
T::Currency::release(
314+
&HoldReason::Fractionalized.into(),
315+
&asset_creator,
316+
deposit,
317+
BestEffort,
318+
)?;
307319

308320
Self::deposit_event(Event::NftUnified {
309321
nft_collection: nft_collection_id,

0 commit comments

Comments
 (0)