Add asset_sufficient getter for fungibles#1768
Add asset_sufficient getter for fungibles#1768pandres95 wants to merge 12 commits intoparitytech:masterfrom
asset_sufficient getter for fungibles#1768Conversation
|
User @pandres95, please sign the CLA here. |
e5dd40e to
fb0d325
Compare
| /// 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.
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.
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.
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::Inspecttrait 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_sufficiencywould require (as shown here) the following steps:sp_io::storage::get.AssetDetailsstruct that resembles the expected one, sinceis_sufficientis 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::Inspecttrait. The method signature conveniently returnsOption<bool>: to beNoneimplies that the asset does not exist, so it's also a shortcut forasset_exists(asset) && asset_sufficient(asset), reducing code.impl_fungibles.rsfile, getting the asset's details, and mapping the response wheneverSome(d)tod.is_sufficient.LocalAndForeignAssets.Checklist
Trequired)
Optional Test Case