Skip to content

Commit

Permalink
Add hold traits to pallet assets
Browse files Browse the repository at this point in the history
  • Loading branch information
darkforest0202 authored and olanod committed Dec 21, 2023
1 parent 789ffb6 commit a81240f
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ impl pallet_assets::Config<TrustBackedAssetsInstance> for Runtime {
type CallbackHandle = ();
type AssetAccountDeposit = AssetAccountDeposit;
type RemoveItemsLimit = frame_support::traits::ConstU32<1000>;
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down Expand Up @@ -311,6 +313,8 @@ impl pallet_assets::Config<PoolAssetsInstance> for Runtime {
type Extra = ();
type WeightInfo = weights::pallet_assets_pool::WeightInfo<Runtime>;
type CallbackHandle = ();
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down Expand Up @@ -385,6 +389,8 @@ impl pallet_assets::Config<ForeignAssetsInstance> for Runtime {
type CallbackHandle = ();
type AssetAccountDeposit = ForeignAssetsAssetAccountDeposit;
type RemoveItemsLimit = frame_support::traits::ConstU32<1000>;
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = xcm_config::XcmBenchmarkHelper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ impl pallet_assets::Config<TrustBackedAssetsInstance> for Runtime {
type CallbackHandle = ();
type AssetAccountDeposit = AssetAccountDeposit;
type RemoveItemsLimit = ConstU32<1000>;
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down Expand Up @@ -293,6 +295,8 @@ impl pallet_assets::Config<PoolAssetsInstance> for Runtime {
type Extra = ();
type WeightInfo = weights::pallet_assets_pool::WeightInfo<Runtime>;
type CallbackHandle = ();
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down Expand Up @@ -366,6 +370,8 @@ impl pallet_assets::Config<ForeignAssetsInstance> for Runtime {
type CallbackHandle = ();
type AssetAccountDeposit = ForeignAssetsAssetAccountDeposit;
type RemoveItemsLimit = frame_support::traits::ConstU32<1000>;
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = xcm_config::XcmBenchmarkHelper;
}
Expand Down Expand Up @@ -1725,7 +1731,7 @@ pub mod migrations {
let pool_asset_id = pool_info.lp_token.clone();
if old_pool_id.0.as_ref() != &invalid_native_asset {
// skip, if ok
continue
continue;
}

// fix new account
Expand Down
2 changes: 2 additions & 0 deletions cumulus/parachains/runtimes/testing/penpal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ impl pallet_assets::Config<pallet_assets::Instance1> for Runtime {
type CallbackHandle = ();
type AssetAccountDeposit = AssetAccountDeposit;
type RemoveItemsLimit = frame_support::traits::ConstU32<1000>;
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ impl pallet_assets::Config for Runtime {
type CallbackHandle = ();
type AssetAccountDeposit = AssetAccountDeposit;
type RemoveItemsLimit = frame_support::traits::ConstU32<1000>;
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down
2 changes: 2 additions & 0 deletions polkadot/xcm/xcm-builder/src/tests/pay/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ impl pallet_assets::Config for Test {
type RemoveItemsLimit = RemoveItemsLimit;
type AssetIdParameter = AssetIdForAssets;
type CallbackHandle = ();
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down
4 changes: 4 additions & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,8 @@ impl pallet_assets::Config<Instance1> for Runtime {
type CallbackHandle = ();
type WeightInfo = pallet_assets::weights::SubstrateWeight<Runtime>;
type RemoveItemsLimit = ConstU32<1000>;
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down Expand Up @@ -1633,6 +1635,8 @@ impl pallet_assets::Config<Instance2> for Runtime {
type WeightInfo = pallet_assets::weights::SubstrateWeight<Runtime>;
type RemoveItemsLimit = ConstU32<1000>;
type CallbackHandle = ();
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down
4 changes: 4 additions & 0 deletions substrate/frame/asset-conversion/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ impl pallet_assets::Config<Instance1> for Test {
type Extra = ();
type WeightInfo = ();
type CallbackHandle = ();
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
pallet_assets::runtime_benchmarks_enabled! {
type BenchmarkHelper = ();
}
Expand All @@ -136,6 +138,8 @@ impl pallet_assets::Config<Instance2> for Test {
type Extra = ();
type WeightInfo = ();
type CallbackHandle = ();
type MaxHolds = ConstU32<50>;
type RuntimeHoldReason = RuntimeHoldReason;
pallet_assets::runtime_benchmarks_enabled! {
type BenchmarkHelper = ();
}
Expand Down
48 changes: 24 additions & 24 deletions substrate/frame/assets/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
// allow accidental usage of all consumer references which could cause grief.
if !frame_system::Pallet::<T>::can_inc_consumer(who) {
frame_system::Pallet::<T>::dec_consumers(who);
return Err(Error::<T, I>::UnavailableConsumer.into())
return Err(Error::<T, I>::UnavailableConsumer.into());
}
ExistenceReason::Consumer
};
Expand Down Expand Up @@ -133,24 +133,24 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
None => return DepositConsequence::UnknownAsset,
};
if increase_supply && details.supply.checked_add(&amount).is_none() {
return DepositConsequence::Overflow
return DepositConsequence::Overflow;
}
if let Some(account) = Account::<T, I>::get(id, who) {
if account.status.is_blocked() {
return DepositConsequence::Blocked
return DepositConsequence::Blocked;
}
if account.balance.checked_add(&amount).is_none() {
return DepositConsequence::Overflow
return DepositConsequence::Overflow;
}
} else {
if amount < details.min_balance {
return DepositConsequence::BelowMinimum
return DepositConsequence::BelowMinimum;
}
if !details.is_sufficient && !frame_system::Pallet::<T>::can_accrue_consumers(who, 2) {
return DepositConsequence::CannotCreate
return DepositConsequence::CannotCreate;
}
if details.is_sufficient && details.sufficients.checked_add(1).is_none() {
return DepositConsequence::Overflow
return DepositConsequence::Overflow;
}
}

Expand All @@ -170,20 +170,20 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
None => return UnknownAsset,
};
if details.supply.checked_sub(&amount).is_none() {
return Underflow
return Underflow;
}
if details.status == AssetStatus::Frozen {
return Frozen
return Frozen;
}
if amount.is_zero() {
return Success
return Success;
}
let account = match Account::<T, I>::get(&id, who) {
Some(a) => a,
None => return BalanceLow,
};
if account.status.is_frozen() {
return Frozen
return Frozen;
}
if let Some(rest) = account.balance.checked_sub(&amount) {
if let Some(frozen) = T::Freezer::frozen_balance(id.clone(), who) {
Expand Down Expand Up @@ -267,7 +267,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Ok(dust) => actual.saturating_add(dust), //< guaranteed by reducible_balance
Err(e) => {
debug_assert!(false, "passed from reducible_balance; qed");
return Err(e)
return Err(e);
},
};

Expand Down Expand Up @@ -360,7 +360,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
debug_assert!(false, "refund did not result in dead account?!");
// deposit may have been refunded, need to update `Account`
Account::<T, I>::insert(id, &who, account);
return Ok(())
return Ok(());
}
Asset::<T, I>::insert(&id, details);
// Executing a hook here is safe, since it is not in a `mutate`.
Expand Down Expand Up @@ -391,12 +391,12 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
debug_assert!(false, "refund did not result in dead account?!");
// deposit may have been refunded, need to update `Account`
Account::<T, I>::insert(&id, &who, account);
return Ok(())
return Ok(());
}
Asset::<T, I>::insert(&id, details);
// Executing a hook here is safe, since it is not in a `mutate`.
T::Freezer::died(id, &who);
return Ok(())
return Ok(());
}

/// Increases the asset `id` balance of `beneficiary` by `amount`.
Expand Down Expand Up @@ -441,7 +441,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
) -> DispatchResult,
) -> DispatchResult {
if amount.is_zero() {
return Ok(())
return Ok(());
}

Self::can_increase(id.clone(), beneficiary, amount, true).into_result()?;
Expand Down Expand Up @@ -528,7 +528,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
) -> DispatchResult,
) -> Result<T::Balance, DispatchError> {
if amount.is_zero() {
return Ok(amount)
return Ok(amount);
}

let details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
Expand All @@ -551,7 +551,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
debug_assert!(account.balance.is_zero(), "checked in prep; qed");
target_died = Some(Self::dead_account(target, details, &account.reason, false));
if let Some(Remove) = target_died {
return Ok(())
return Ok(());
}
};
*maybe_account = Some(account);
Expand Down Expand Up @@ -604,7 +604,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
) -> Result<(T::Balance, Option<DeadConsequence>), DispatchError> {
// Early exit if no-op.
if amount.is_zero() {
return Ok((amount, None))
return Ok((amount, None));
}
let details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
ensure!(details.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
Expand All @@ -627,7 +627,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {

// Skip if source == dest
if source == dest {
return Ok(())
return Ok(());
}

// Burn any dust if needed.
Expand Down Expand Up @@ -672,7 +672,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Some(Self::dead_account(source, details, &source_account.reason, false));
if let Some(Remove) = source_died {
Account::<T, I>::remove(&id, &source);
return Ok(())
return Ok(());
}
}
Account::<T, I>::insert(&id, &source, &source_account);
Expand Down Expand Up @@ -753,7 +753,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
id: T::AssetId,
max_items: u32,
) -> Result<u32, DispatchError> {
let mut dead_accounts: Vec<T::AccountId> = vec![];
let mut dead_accounts: Vec<T::AccountId> = Vec::new();
let mut remaining_accounts = 0;
let _ =
Asset::<T, I>::try_mutate_exists(&id, |maybe_details| -> Result<(), DispatchError> {
Expand All @@ -776,7 +776,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
defensive!("destroy did not result in dead account?!");
}
if i + 1 >= (max_items as usize) {
break
break;
}
}
remaining_accounts = details.accounts;
Expand Down Expand Up @@ -817,7 +817,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
removed_approvals = removed_approvals.saturating_add(1);
details.approvals = details.approvals.saturating_sub(1);
if removed_approvals >= max_items {
break
break;
}
}
Self::deposit_event(Event::ApprovalsDestroyed {
Expand Down
Loading

0 comments on commit a81240f

Please sign in to comment.