Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Fungibles and Non-Fungibles Create and Destroy Traits + Assets and Uniques Implementation #9844

Merged
7 commits merged into from
Sep 26, 2021

Conversation

shawntabrizi
Copy link
Member

@shawntabrizi shawntabrizi commented Sep 23, 2021

This PR introduces the traits Create and Destroy to the overarching fungibles and nonfungibles definition.

These traits, as they sound, allow you to create and destroy assets within some pallet which manages multiple fungible or nonfungible assets.

We implement this trait in the Assets and Uniques pallet by doing a trivial refactor of the logic in the destroy and force_create extrinsic, and then using that logic for the trait.

This should help other developers who want to build on top of the Assets or Uniques pallet as a basis for managing tokens in their runtime.

Replaces: #9824

@shawntabrizi shawntabrizi added B7-runtimenoteworthy C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Sep 23, 2021
@shawntabrizi shawntabrizi changed the title Fungibles Create and Destroy Traits + Assets Implementation Fungibles and Non-Fungibles Create and Destroy Traits + Assets and Uniques Implementation Sep 23, 2021
@shawntabrizi shawntabrizi added the D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed. label Sep 23, 2021
Comment on lines -411 to -412
// NOTE: could use postinfo to reflect the actual number of
// accounts/sufficient/approvals
Copy link
Member Author

@shawntabrizi shawntabrizi Sep 23, 2021

Choose a reason for hiding this comment

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

I fixed this TODO, you can see we now refund the weight using proper details.

This should be the only "new" logic

Comment on lines -497 to -499
Asset::<T, I>::try_mutate_exists(id, |maybe_details| {
let mut details = maybe_details.take().ok_or(Error::<T, I>::Unknown)?;
if let Some(check_owner) = maybe_check_owner {
Copy link
Member Author

Choose a reason for hiding this comment

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

Just copied this logic directly into do_destroy

Comment on lines -441 to -444
ensure!(!Asset::<T, I>::contains_key(id), Error::<T, I>::InUse);
ensure!(!min_balance.is_zero(), Error::<T, I>::MinBalanceZero);

Asset::<T, I>::insert(
Copy link
Member Author

Choose a reason for hiding this comment

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

Just copied this logic into do_force_create

Comment on lines -389 to -391
Class::<T, I>::try_mutate_exists(class, |maybe_details| {
let class_details = maybe_details.take().ok_or(Error::<T, I>::Unknown)?;
if let Some(check_owner) = maybe_check_owner {
Copy link
Member Author

Choose a reason for hiding this comment

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

Just moved this logic into do_destroy_class

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

seems like 0 logic changes and only changing abstractions, right?

@shawntabrizi
Copy link
Member Author

seems like 0 logic changes and only changing abstractions, right?

Yes, zero logic changes other than the weight refund

frame/assets/src/functions.rs Outdated Show resolved Hide resolved
Comment on lines +260 to +263
fn destroy(
id: Self::AssetId,
witness: Self::DestroyWitness,
maybe_check_owner: Option<AccountId>,
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: add an auto-implemented destroy function without the witness parameter (and maybe rename this to destroy_with_witness)

Copy link
Member Author

Choose a reason for hiding this comment

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

No, I think that would be a dangerous API. Destroy can have varying complexity based on the number of assets it contains. The user should always investigate the witness information before executing the operation. If they truly with to be oblivious of this check, they can always provide a witness with max values, but I don't think we should encourage it.

@emostov
Copy link
Contributor

emostov commented Sep 24, 2021

LGTM. The only caveat is I have not thought too deeply on exactly how other pallets may be using this trait, but for the use cases I understand this seems clean and sensible.

@shawntabrizi
Copy link
Member Author

bot merge

@ghost
Copy link

ghost commented Sep 26, 2021

Trying merge.

@ghost ghost merged commit 58649c4 into master Sep 26, 2021
@ghost ghost deleted the shawntabrizi-asset-destroy-trait branch September 26, 2021 22:05
green-jay pushed a commit to green-jay/substrate that referenced this pull request Oct 5, 2021
…iques Implementation (paritytech#9844)

* refactor `do_destroy`

* destroy trait

* refactor do_force_create

* impl create trait

* do not bleed weight into api

* Do the same for uniques

* add docs
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants