Skip to content

Commit

Permalink
change(pallet-assets): improve sufficiency traits and enhance documen…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
pandres95 committed Jan 7, 2024
1 parent b1e3ae1 commit ac13956
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
12 changes: 12 additions & 0 deletions substrate/frame/assets/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,18 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
})
}

/// Do set sufficiency
pub(super) fn do_set_sufficiency(asset_id: T::AssetId, is_sufficient: bool) -> DispatchResult {
Asset::<T, I>::try_mutate(asset_id, |maybe_asset| {
if let Some(asset) = maybe_asset {
asset.is_sufficient = is_sufficient;
Ok(())
} else {
Err(Error::<T, I>::Unknown)?
}
})
}

/// Calculate the metadata deposit for the provided data.
pub(super) fn calc_metadata_deposit(name: &[u8], symbol: &[u8]) -> DepositBalanceOf<T, I> {
T::MetadataDepositPerByte::get()
Expand Down
18 changes: 6 additions & 12 deletions substrate/frame/assets/src/impl_sufficients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,11 @@ impl<T: Config<I>, I: 'static> Inspect<<T as Config<I>>::AssetId> for Pallet<T,
}

impl<T: Config<I>, I: 'static> Mutate<<T as Config<I>>::AssetId> for Pallet<T, I> {
fn set_sufficient(
asset_id: <T as Config<I>>::AssetId,
is_sufficient: bool,
) -> sp_runtime::DispatchResult {
Asset::<T, I>::try_mutate(asset_id, |maybe_asset| {
if let Some(asset) = maybe_asset {
asset.is_sufficient = is_sufficient;
Ok(())
} else {
Err(Error::<T, I>::Unknown)?
}
})
fn make_sufficient(asset_id: <T as Config<I>>::AssetId) -> sp_runtime::DispatchResult {
Pallet::<T, I>::do_set_sufficiency(asset_id, true)
}

fn make_insufficient(asset_id: <T as Config<I>>::AssetId) -> sp_runtime::DispatchResult {
Pallet::<T, I>::do_set_sufficiency(asset_id, false)
}
}
2 changes: 1 addition & 1 deletion substrate/frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn insufficient_assets_can_turn_into_sufficient() {
new_test_ext().execute_with(|| {
assert_ok!(Assets::force_create(RuntimeOrigin::root(), 0, 1, false, 1));
assert_eq!(Assets::is_sufficient(0), false);
assert_ok!(Assets::set_sufficient(0, true));
assert_ok!(Assets::make_sufficient(0));
assert_eq!(Assets::is_sufficient(0), true);
});
}
Expand Down
13 changes: 10 additions & 3 deletions substrate/frame/assets/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ pub mod sufficients {

/// Trait for providing the sufficient state of an asset.
pub trait Inspect<AssetId> {
/// Returns whether an asset is sufficient or not
/// Returns whether an asset is sufficient or not.
fn is_sufficient(asset_id: AssetId) -> bool;
}

/// Trait for mutating the sufficient state of an asset
pub trait Mutate<AssetId> {
/// Sets the `is_sufficient` value of an asset.
/// Makes the asset to be sufficient.
///
/// ### Errors
///
/// - [`Unknown`][crate::Error::Unknown] when the asset ID is unknown.
fn set_sufficient(asset_id: AssetId, is_sufficient: bool) -> DispatchResult;
fn make_sufficient(asset_id: AssetId) -> DispatchResult;

/// Makes the asset to be insufficient.
///
/// ### Errors
///
/// - [`Unknown`][crate::Error::Unknown] when the asset ID is unknown.
fn make_insufficient(asset_id: AssetId) -> DispatchResult;
}
}

0 comments on commit ac13956

Please sign in to comment.