Skip to content

Commit

Permalink
feat(pallet-assets): define and implement sufficients traits for `pal…
Browse files Browse the repository at this point in the history
…let-assets`
  • Loading branch information
pandres95 committed Jan 7, 2024
1 parent 745c02c commit b1e3ae1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
26 changes: 26 additions & 0 deletions substrate/frame/assets/src/impl_sufficients.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::{
traits::sufficients::{Inspect, Mutate},
Asset, Config, Error, Pallet,
};

impl<T: Config<I>, I: 'static> Inspect<<T as Config<I>>::AssetId> for Pallet<T, I> {
fn is_sufficient(asset_id: <T as Config<I>>::AssetId) -> bool {
Asset::<T, I>::get(asset_id).map(|asset| asset.is_sufficient).unwrap_or(false)
}
}

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)?
}
})
}
}
4 changes: 4 additions & 0 deletions substrate/frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,13 @@ pub mod weights;

mod extra_mutator;
pub use extra_mutator::*;

mod functions;
mod impl_fungibles;
mod impl_stored_map;
mod impl_sufficients;

pub mod traits;
mod types;
pub use types::*;

Expand Down
14 changes: 13 additions & 1 deletion substrate/frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Tests for Assets pallet.

use super::*;
use crate::{mock::*, Error};
use crate::{mock::*, traits::*, Error};
use frame_support::{
assert_noop, assert_ok,
dispatch::GetDispatchInfo,
Expand Down Expand Up @@ -100,6 +100,18 @@ fn basic_minting_should_work() {
});
}

#[test]
fn insufficient_assets_can_turn_into_sufficient() {
use sufficients::{Inspect, Mutate};

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_eq!(Assets::is_sufficient(0), true);
});
}

#[test]
fn minting_too_many_insufficient_assets_fails() {
new_test_ext().execute_with(|| {
Expand Down
19 changes: 19 additions & 0 deletions substrate/frame/assets/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub mod sufficients {
use sp_runtime::DispatchResult;

/// Trait for providing the sufficient state of an asset.
pub trait Inspect<AssetId> {
/// 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.
///
/// ### Errors
///
/// - [`Unknown`][crate::Error::Unknown] when the asset ID is unknown.
fn set_sufficient(asset_id: AssetId, is_sufficient: bool) -> DispatchResult;
}
}

0 comments on commit b1e3ae1

Please sign in to comment.