Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pallet-assets): define and implement sufficients traits for pallet-assets #2872

Closed
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions substrate/frame/assets/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,22 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
})
}

/// Set the sufficiency of an asset class
///
/// ### Errors
///
/// - [`Unknown`][crate::Error::Unknown] when the asset ID is unknown.
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
39 changes: 39 additions & 0 deletions substrate/frame/assets/src/impl_sufficiency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Assets pallet's `StoredMap` implementation.

use crate::{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe traits and impls? otherwise the list of files will grow here pretty quick.
I also have only strong opinion on keeping our folders/files/modules style consistent, since there is no yet agreement on it, I would just try to keep it close to what we have now, and now we tend to have less files than many.

Copy link
Contributor Author

@pandres95 pandres95 Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this mean also moving impl_fungibles and impl_stored_map onto an impls file/folder? I see the point on making the file structure lighter, but seeing that other impls are not that light (i.e. impl_fungibles is 311 lines) a bit of not-flatness might be good for the sake of readibility from time to time.

My suggestion:

...
impls/
├─ fungibles.rs
├─ stored_maps.rs
├─ sufficiency.rs
├─ mod.rs
...
traits.rs
...

pandres95 marked this conversation as resolved.
Show resolved Hide resolved
traits::sufficiency::{IsSufficient, SetSufficiency},
Asset, Config, Pallet,
};

impl<T: Config<I>, I: 'static> IsSufficient<<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> SetSufficiency<<T as Config<I>>::AssetId> for Pallet<T, I> {
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)
}
}
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_sufficiency;

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

Expand Down
16 changes: 15 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,20 @@ fn basic_minting_should_work() {
});
}

#[test]
fn insufficient_assets_can_turn_into_sufficient() {
use sufficiency::{IsSufficient, SetSufficiency};

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::make_sufficient(0));
assert_eq!(Assets::is_sufficient(0), true);
pandres95 marked this conversation as resolved.
Show resolved Hide resolved
assert_ok!(Assets::make_insufficient(0));
assert_eq!(Assets::is_sufficient(0), false);
});
}

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

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

/// Trait for mutating the sufficiency of an asset
pub trait SetSufficiency<AssetId> {
/// Makes the asset sufficient.
fn make_sufficient(asset_id: AssetId) -> DispatchResult;

/// Makes the asset insufficient.
fn make_insufficient(asset_id: AssetId) -> DispatchResult;
}
}
Loading