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

Add asset_sufficient getter for fungibles #1768

Closed
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ where
ForeignAssets::asset_exists(asset)
}
}

/// Returns `Some(true)` if an `asset` exists and is sufficient.
fn asset_sufficient(asset: Self::AssetId) -> Option<bool> {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::asset_sufficient(asset)
} else {
ForeignAssets::asset_sufficient(asset)
}
}
}

impl<Assets, LocalAssetIdConverter, ForeignAssets> Mutate<AccountId>
Expand Down
4 changes: 4 additions & 0 deletions cumulus/primitives/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ mod tests {
fn asset_exists(_: Self::AssetId) -> bool {
todo!()
}

fn asset_sufficient(_: Self::AssetId) -> Option<bool> {
todo!()
}
}
impl fungibles::Mutate<TestAccountId> for TestAssets {}
impl fungibles::Balanced<TestAccountId> for TestAssets {
Expand Down
4 changes: 4 additions & 0 deletions substrate/frame/assets/src/impl_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ impl<T: Config<I>, I: 'static> fungibles::Inspect<<T as SystemConfig>::AccountId
fn asset_exists(asset: Self::AssetId) -> bool {
Asset::<T, I>::contains_key(asset)
}

fn asset_sufficient(asset: Self::AssetId) -> Option<bool> {
Asset::<T, I>::get(asset).map(|d| d.is_sufficient)
}
}

impl<T: Config<I>, I: 'static> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T, I> {
Expand Down
4 changes: 4 additions & 0 deletions substrate/frame/broker/src/test_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ where
fn asset_exists(asset: Self::AssetId) -> bool {
TestAssetOf::get().contains_key(&(Instance::get(), asset.encode()))
}

fn asset_sufficient(_: Self::AssetId) -> Option<bool> {
None
}
}

impl<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ pub trait Inspect<AccountId>: Sized {

/// Returns `true` if an `asset` exists.
fn asset_exists(asset: Self::AssetId) -> bool;

/// Returns `Some(true)` if an `asset` exists and is sufficient.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should it return None if the asset does not exist? And Some(false) if it exists and is not sufficient?
A better doc would be useful here

Copy link
Contributor Author

@pandres95 pandres95 Oct 5, 2023

Choose a reason for hiding this comment

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

You are right. It's done.

The new doc states the following:

/// Returns `Some(true)` if an `asset` exists and is sufficient. Returns `Some(false)`
/// if an `asset` exists, but is not sufficient. Returns `None` if an `asset` does not exist.

It's already ready for review again @juangirini 😊

fn asset_sufficient(asset: Self::AssetId) -> Option<bool>;
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if this concept is general enough, I cannot really say.
but an alternative can be a separate trait on assets pallet level, which lets you inspect some additional characteristics of an asset.
if you decide to continue in this direction, we need a good explanation here what sufficient means.
also the returned value can be just bool here, since you have asset_exists to check it is existence.

}

/// Special dust type which can be type-safely converted into a `Credit`.
Expand Down
Loading