-
Notifications
You must be signed in to change notification settings - Fork 707
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
Add asset_sufficient
getter for fungibles
#1768
Conversation
User @pandres95, please sign the CLA here. |
e5dd40e
to
fb0d325
Compare
@@ -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. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😊
e5e5ca0
to
cb58dae
Compare
|
||
/// 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. | ||
fn asset_sufficient(asset: Self::AssetId) -> Option<bool>; |
There was a problem hiding this comment.
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.
Per @muharem's suggestion, I decided to close this PR and continue on #2872. |
Description
This Pull Request introduces a method inside the
fungibles::Inspect
trait calledasset_sufficient
. This method is useful since pallet implementors can benefit from having this (so far missing) piece of information, either for testing checks or for handling accounts touching on behalf of others.Use Cases
Use Case: Checking whether an asset was appropriately created as sufficient
A pallet might have a rule that allows creating assets, but only marking one of these (i.e. the first) as sufficient. A typical test for this would resemble something like this
Without this getter, implementing
assert_sufficiency
would require (as shown here) the following steps:sp_io::storage::get
.AssetDetails
struct that resembles the expected one, sinceis_sufficient
is a private field.With the getter, it would be something as simple as this, no further illegal accesses required:
Use Case: Checking sufficiency for an existing asset
Some pallets might see it useful to check whether an asset is sufficient, for some cases where it might be useful (e.g. some restricted versions of DEXes where pool implementors would like to check that those assets are sufficient).
Without the getter, this would require building the pallet as an extension of the Assets pallet. Some tricks like the one explained in the use case above would be impractical.
Implementation
The implementation is actually really simple.
fungibles::Inspect
trait. The method signature conveniently returnsOption<bool>
: to beNone
implies that the asset does not exist, so it's also a shortcut forasset_exists(asset) && asset_sufficient(asset)
, reducing code.impl_fungibles.rs
file, getting the asset's details, and mapping the response wheneverSome(d)
tod.is_sufficient
.LocalAndForeignAssets
.Checklist
T
required)
Optional Test Case