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

HoldReason: Improve usage #13869

Merged
merged 5 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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
15 changes: 2 additions & 13 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 @@ -436,15 +435,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,
}

impl pallet_balances::Config for Runtime {
type MaxLocks = MaxLocks;
type MaxReserves = MaxReserves;
Expand All @@ -457,7 +447,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<1>;
}

Expand Down Expand Up @@ -1500,7 +1490,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 @@ -1524,7 +1513,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
2 changes: 1 addition & 1 deletion frame/alliance/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 ReserveIdentifier = [u8; 8];
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

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 @@ -146,7 +146,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 @@ -254,8 +254,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 @@ -430,7 +430,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 @@ -135,7 +135,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 @@ -321,7 +321,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
2 changes: 1 addition & 1 deletion frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,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/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,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
2 changes: 1 addition & 1 deletion frame/nfts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,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/nicks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ mod tests {
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

Expand Down
27 changes: 12 additions & 15 deletions frame/nis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,10 @@ pub mod pallet {
type Currency: FunInspect<Self::AccountId, Balance = Self::CurrencyBalance>
+ FunMutate<Self::AccountId>
+ FunBalanced<Self::AccountId>
+ FunHoldInspect<Self::AccountId>
+ FunHoldMutate<Self::AccountId>;
+ FunHoldMutate<Self::AccountId, Reason = Self::RuntimeHoldReason>;

/// The identifier of the hold reason.

#[pallet::constant]
type HoldReason: Get<<Self::Currency as FunHoldInspect<Self::AccountId>>::Reason>;
/// Overarching hold reason.
type RuntimeHoldReason: Member + MaxEncodedLen + From<HoldReason>;
ggwpez marked this conversation as resolved.
Show resolved Hide resolved

/// Just the `Currency::Balance` type; we have this item to allow us to constrain it to
/// `From<u64>`.
Expand Down Expand Up @@ -569,14 +566,14 @@ pub mod pallet {
|q| -> Result<(u32, BalanceOf<T>), DispatchError> {
let queue_full = q.len() == T::MaxQueueLen::get() as usize;
ensure!(!queue_full || q[0].amount < amount, Error::<T>::BidTooLow);
T::Currency::hold(&T::HoldReason::get(), &who, amount)?;
T::Currency::hold(&HoldReason::NftReceipt.into(), &who, amount)?;

// queue is <Ordered: Lowest ... Highest><Fifo: Last ... First>
let mut bid = Bid { amount, who: who.clone() };
let net = if queue_full {
sp_std::mem::swap(&mut q[0], &mut bid);
let _ = T::Currency::release(
&T::HoldReason::get(),
&HoldReason::NftReceipt.into(),
&bid.who,
bid.amount,
BestEffort,
Expand Down Expand Up @@ -637,7 +634,7 @@ pub mod pallet {
queue.remove(pos);
let new_len = queue.len() as u32;

T::Currency::release(&T::HoldReason::get(), &bid.who, bid.amount, BestEffort)?;
T::Currency::release(&HoldReason::NftReceipt.into(), &bid.who, bid.amount, BestEffort)?;

Queues::<T>::insert(duration, queue);
QueueTotals::<T>::mutate(|qs| {
Expand Down Expand Up @@ -729,7 +726,7 @@ pub mod pallet {
let dropped = receipt.proportion.is_zero();

if amount > on_hold {
T::Currency::release(&T::HoldReason::get(), &who, on_hold, Exact)?;
T::Currency::release(&HoldReason::NftReceipt.into(), &who, on_hold, Exact)?;
let deficit = amount - on_hold;
// Try to transfer deficit from pot to receipt owner.
summary.receipts_on_hold.saturating_reduce(on_hold);
Expand All @@ -744,7 +741,7 @@ pub mod pallet {
// Transfer excess of `on_hold` to the pot if we have now fully compensated for
// the receipt.
T::Currency::transfer_on_hold(
&T::HoldReason::get(),
&HoldReason::NftReceipt.into(),
&who,
&our_account,
on_hold,
Expand All @@ -760,7 +757,7 @@ pub mod pallet {
)?;
summary.receipts_on_hold.saturating_reduce(on_hold);
}
T::Currency::release(&T::HoldReason::get(), &who, amount, Exact)?;
T::Currency::release(&HoldReason::NftReceipt.into(), &who, amount, Exact)?;
}

if dropped {
Expand Down Expand Up @@ -852,7 +849,7 @@ pub mod pallet {
ensure!(owner == who, Error::<T>::NotOwner);

// Unreserve and transfer the funds to the pot.
let reason = T::HoldReason::get();
let reason = HoldReason::NftReceipt.into();
let us = Self::account_id();
T::Currency::transfer_on_hold(&reason, &who, &us, on_hold, Exact, Free, Polite)
.map_err(|_| Error::<T>::Unfunded)?;
Expand Down Expand Up @@ -903,7 +900,7 @@ pub mod pallet {
)?;

// Transfer the funds from the pot to the owner and reserve
let reason = T::HoldReason::get();
let reason = HoldReason::NftReceipt.into();
let us = Self::account_id();
T::Currency::transfer_and_hold(&reason, &us, &who, amount, Exact, Expendable, Polite)?;

Expand Down Expand Up @@ -959,7 +956,7 @@ pub mod pallet {
let mut item = Receipts::<T>::get(index).ok_or(TokenError::UnknownAsset)?;
let (owner, on_hold) = item.owner.take().ok_or(Error::<T>::AlreadyCommunal)?;

let reason = T::HoldReason::get();
let reason = HoldReason::NftReceipt.into();
T::Currency::transfer_on_hold(&reason, &owner, dest, on_hold, Exact, OnHold, Polite)?;

item.owner = Some((dest.clone(), on_hold));
Expand Down
Loading