git.delta.rocks / unique-network / refs/commits / 665fb9fa8027

difftreelog

doc: add documentation for nonfungible palette

Grigoriy Simonov2022-07-21parent: #3750ef0.patch.diff
in: master

2 files changed

modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -133,6 +133,8 @@
 	}
 }
 
+/// Implementation of `CommonCollectionOperations` for `NonfungibleHandle`. It wraps Nonfungible Pallete
+/// methods and adds weight info.
 impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {
 	fn create_item(
 		&self,
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
14// You should have received a copy of the GNU General Public License14// You should have received a copy of the GNU General Public License
15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
16
17//! # Nonfungible Pallet
18//!
19//! The Nonfungible pallet provides functionality for handling nonfungible collections and tokens.
20//!
21//! - [`Config`]
22//! - [`NonfungibleHandle`]
23//! - [`Pallet`]
24//! - [`CommonWeights`]
25//!
26//! ## Overview
27//!
28//! The Nonfungible pallet provides functions for:
29//!
30//! - NFT collection creation and removal
31//! - Minting and burning of NFT tokens
32//! - Retrieving account balances
33//! - Transfering NFT tokens
34//! - Setting and checking allowance for NFT tokens
35//! - Setting properties and permissions for NFT collections and tokens
36//! - Nesting and unnesting tokens
37//!
38//! ### Terminology
39//!
40//! - **NFT token:** Non fungible token.
41//!
42//! - **NFT Collection:** A collection of NFT tokens. All NFT tokens are part of a collection.
43//! Each collection can define it's own properties, properties for it's tokens and set of permissions.
44//!
45//! - **Balance:** Number of NFT tokens owned by an account
46//!
47//! - **Allowance:** NFT tokens owned by one account that another account is allowed to make operations on
48//!
49//! - **Burning:** The process of “deleting” a token from a collection and from
50//! an account balance of the owner.
51//!
52//! - **Nesting:** Setting up parent-child relationship between tokens. Nested tokens are inhereting
53//! owner from their parent. There could be multiple levels of nesting. Token couldn't be nested in
54//! it's child token i.e. parent-child relationship graph shouldn't have cycles.
55//!
56//! - **Properties:** Key-Values pairs. Token properties are attached to a token. Collection properties are
57//! attached to a collection. Set of permissions could be defined for each property.
58//!
59//! ### Implementations
60//!
61//! The Nonfungible pallet provides implementations for the following traits. If these traits provide
62//! the functionality that you need, then you can avoid coupling with the Nonfungible pallet.
63//!
64//! - [`CommonWeightInfo`](pallet_common::CommonWeightInfo): Functions for retrieval of transaction weight
65//! - [`CommonCollectionOperations`](pallet_common::CommonCollectionOperations): Functions for dealing
66//! with collections
67//!
68//! ## Interface
69//!
70//! ### Dispatchable Functions
71//!
72//! - `init_collection` - Create NFT collection. NFT collection can be configured to allow or deny access for
73//! some accounts.
74//! - `destroy_collection` - Destroy exising NFT collection. There should be no tokens in the collection.
75//! - `burn` - Burn NFT token owned by account.
76//! - `transfer` - Transfer NFT token. Transfers should be enabled for NFT collection.
77//! Nests the NFT token if it is sent to another token.
78//! - `create_item` - Mint NFT token in collection. Sender should have permission to mint tokens.
79//! - `set_allowance` - Set allowance for another account.
80//! - `set_token_property` - Set token property value.
81//! - `delete_token_property` - Remove property from the token.
82//! - `set_collection_properties` - Set collection properties.
83//! - `delete_collection_properties` - Remove properties from the collection.
84//! - `set_property_permission` - Set collection property permission.
85//! - `set_token_property_permissions` - Set token property permissions.
86//!
87//! ## Assumptions
88//!
89//! * Total number of tokens of all types shouldn't be greater than `up_data_structs::MAX_TOKEN_PREFIX_LENGTH`.
90//! * Sender should be in collection's allow list to perform operations on tokens.
1691
17#![cfg_attr(not(feature = "std"), no_std)]92#![cfg_attr(not(feature = "std"), no_std)]
1893
102 #[pallet::generate_store(pub(super) trait Store)]177 #[pallet::generate_store(pub(super) trait Store)]
103 pub struct Pallet<T>(_);178 pub struct Pallet<T>(_);
104179
180 /// Amount of tokens minted for collection.
105 #[pallet::storage]181 #[pallet::storage]
106 pub type TokensMinted<T: Config> =182 pub type TokensMinted<T: Config> =
107 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;183 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;
184
185 /// Amount of burnt tokens for collection.
108 #[pallet::storage]186 #[pallet::storage]
109 pub type TokensBurnt<T: Config> =187 pub type TokensBurnt<T: Config> =
110 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;188 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;
111189
190 /// Custom data serialized to bytes for token.
112 #[pallet::storage]191 #[pallet::storage]
113 pub type TokenData<T: Config> = StorageNMap<192 pub type TokenData<T: Config> = StorageNMap<
114 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),193 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
115 Value = ItemData<T::CrossAccountId>,194 Value = ItemData<T::CrossAccountId>,
116 QueryKind = OptionQuery,195 QueryKind = OptionQuery,
117 >;196 >;
118197
198 /// Key-Value map stored for token.
119 #[pallet::storage]199 #[pallet::storage]
120 #[pallet::getter(fn token_properties)]200 #[pallet::getter(fn token_properties)]
121 pub type TokenProperties<T: Config> = StorageNMap<201 pub type TokenProperties<T: Config> = StorageNMap<
125 OnEmpty = up_data_structs::TokenProperties,205 OnEmpty = up_data_structs::TokenProperties,
126 >;206 >;
127207
208 /// Custom data that is serialized to bytes and attached to a token property.
128 #[pallet::storage]209 #[pallet::storage]
129 #[pallet::getter(fn token_aux_property)]210 #[pallet::getter(fn token_aux_property)]
130 pub type TokenAuxProperties<T: Config> = StorageNMap<211 pub type TokenAuxProperties<T: Config> = StorageNMap<
138 QueryKind = OptionQuery,219 QueryKind = OptionQuery,
139 >;220 >;
140221
141 /// Used to enumerate tokens owned by account222 /// Used to enumerate tokens owned by account.
142 #[pallet::storage]223 #[pallet::storage]
143 pub type Owned<T: Config> = StorageNMap<224 pub type Owned<T: Config> = StorageNMap<
144 Key = (225 Key = (
150 QueryKind = ValueQuery,231 QueryKind = ValueQuery,
151 >;232 >;
152233
153 /// Used to enumerate token's children234 /// Used to enumerate token's children.
154 #[pallet::storage]235 #[pallet::storage]
155 #[pallet::getter(fn token_children)]236 #[pallet::getter(fn token_children)]
156 pub type TokenChildren<T: Config> = StorageNMap<237 pub type TokenChildren<T: Config> = StorageNMap<
163 QueryKind = ValueQuery,244 QueryKind = ValueQuery,
164 >;245 >;
165246
247 /// Amount of tokens owned by account.
166 #[pallet::storage]248 #[pallet::storage]
167 pub type AccountBalance<T: Config> = StorageNMap<249 pub type AccountBalance<T: Config> = StorageNMap<
168 Key = (250 Key = (
173 QueryKind = ValueQuery,255 QueryKind = ValueQuery,
174 >;256 >;
175257
258 /// Allowance set by an owner for a spender for a token.
176 #[pallet::storage]259 #[pallet::storage]
177 pub type Allowance<T: Config> = StorageNMap<260 pub type Allowance<T: Config> = StorageNMap<
178 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),261 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
273}356}
274357
275impl<T: Config> Pallet<T> {358impl<T: Config> Pallet<T> {
359 /// Get number of NFT tokens in collection.
276 pub fn total_supply(collection: &NonfungibleHandle<T>) -> u32 {360 pub fn total_supply(collection: &NonfungibleHandle<T>) -> u32 {
277 <TokensMinted<T>>::get(collection.id) - <TokensBurnt<T>>::get(collection.id)361 <TokensMinted<T>>::get(collection.id) - <TokensBurnt<T>>::get(collection.id)
278 }362 }
363
364 /// Check that NFT token exists.
365 ///
366 /// - `token`: Token ID.
279 pub fn token_exists(collection: &NonfungibleHandle<T>, token: TokenId) -> bool {367 pub fn token_exists(collection: &NonfungibleHandle<T>, token: TokenId) -> bool {
280 <TokenData<T>>::contains_key((collection.id, token))368 <TokenData<T>>::contains_key((collection.id, token))
281 }369 }
282370
371 /// Set the token property with the scope.
372 ///
373 /// - `property`: Contains key-value pair.
283 pub fn set_scoped_token_property(374 pub fn set_scoped_token_property(
284 collection_id: CollectionId,375 collection_id: CollectionId,
285 token_id: TokenId,376 token_id: TokenId,
294 Ok(())385 Ok(())
295 }386 }
296387
388 /// Batch operation to set multiple properties with the same scope.
297 pub fn set_scoped_token_properties(389 pub fn set_scoped_token_properties(
298 collection_id: CollectionId,390 collection_id: CollectionId,
299 token_id: TokenId,391 token_id: TokenId,
308 Ok(())400 Ok(())
309 }401 }
310402
403 /// Add or edit auxiliary data for the property.
404 ///
405 /// - `f`: function that adds or edits auxiliary data.
311 pub fn try_mutate_token_aux_property<R, E>(406 pub fn try_mutate_token_aux_property<R, E>(
312 collection_id: CollectionId,407 collection_id: CollectionId,
313 token_id: TokenId,408 token_id: TokenId,
318 <TokenAuxProperties<T>>::try_mutate((collection_id, token_id, scope, key), f)413 <TokenAuxProperties<T>>::try_mutate((collection_id, token_id, scope, key), f)
319 }414 }
320415
416 /// Remove auxiliary data for the property.
321 pub fn remove_token_aux_property(417 pub fn remove_token_aux_property(
322 collection_id: CollectionId,418 collection_id: CollectionId,
323 token_id: TokenId,419 token_id: TokenId,
327 <TokenAuxProperties<T>>::remove((collection_id, token_id, scope, key));423 <TokenAuxProperties<T>>::remove((collection_id, token_id, scope, key));
328 }424 }
329425
426 /// Get all auxiliary data in a given scope.
427 ///
428 /// Returns iterator over Property Key - Data pairs.
330 pub fn iterate_token_aux_properties(429 pub fn iterate_token_aux_properties(
331 collection_id: CollectionId,430 collection_id: CollectionId,
332 token_id: TokenId,431 token_id: TokenId,
335 <TokenAuxProperties<T>>::iter_prefix((collection_id, token_id, scope))434 <TokenAuxProperties<T>>::iter_prefix((collection_id, token_id, scope))
336 }435 }
337436
437 /// Get ID of the last minted token
338 pub fn current_token_id(collection_id: CollectionId) -> TokenId {438 pub fn current_token_id(collection_id: CollectionId) -> TokenId {
339 TokenId(<TokensMinted<T>>::get(collection_id))439 TokenId(<TokensMinted<T>>::get(collection_id))
340 }440 }
341}441}
342442
343// unchecked calls skips any permission checks443// unchecked calls skips any permission checks
344impl<T: Config> Pallet<T> {444impl<T: Config> Pallet<T> {
445 /// Create ТFT collection
446 ///
447 /// `init_collection` will take non-refundable deposit for collection creation.
448 ///
449 /// - `data`: Contains settings for collection limits and permissions.
345 pub fn init_collection(450 pub fn init_collection(
346 owner: T::CrossAccountId,451 owner: T::CrossAccountId,
347 data: CreateCollectionData<T::AccountId>,452 data: CreateCollectionData<T::AccountId>,
350 <PalletCommon<T>>::init_collection(owner, data, is_external)455 <PalletCommon<T>>::init_collection(owner, data, is_external)
351 }456 }
457
458 /// Destroy ТFT collection
459 ///
460 /// `destroy_collection` will throw error if collection contains any tokens.
461 /// Only owner can destroy collection.
352 pub fn destroy_collection(462 pub fn destroy_collection(
353 collection: NonfungibleHandle<T>,463 collection: NonfungibleHandle<T>,
354 sender: &T::CrossAccountId,464 sender: &T::CrossAccountId,
373 Ok(())483 Ok(())
374 }484 }
375485
486 /// Burn NFT token
487 ///
488 /// `burn` removes `token` from the `collection`, from it's owner and from the parent token
489 /// if the token is nested.
490 /// Only the owner can `burn` the token. The `token` shouldn't have any nested tokens.
491 /// Also removes all corresponding properties and auxiliary properties.
492 ///
493 /// - `token`: Token that should be burned
494 /// - `collection`: Collection that contains the token
376 pub fn burn(495 pub fn burn(
377 collection: &NonfungibleHandle<T>,496 collection: &NonfungibleHandle<T>,
378 sender: &T::CrossAccountId,497 sender: &T::CrossAccountId,
442 Ok(())561 Ok(())
443 }562 }
444563
564 /// Same as [`burn`] but burns all the tokens that are nested in the token first
565 ///
566 /// - `self_budget`: Limit for searching children in depth.
567 /// - `breadth_budget`: Limit of breadth of searching children.
568 ///
569 /// [`burn`]: struct.Pallet.html#method.burn
445 #[transactional]570 #[transactional]
446 pub fn burn_recursively(571 pub fn burn_recursively(
447 collection: &NonfungibleHandle<T>,572 collection: &NonfungibleHandle<T>,
481 })606 })
482 }607 }
483608
609 /// Batch operation to add, edit or remove properties for the token
610 ///
611 /// All affected properties should have mutable permission and sender should have
612 /// permission to edit those properties.
613 ///
614 /// - `nesting_budget`: Limit for searching parents in depth to check ownership.
615 /// - `is_token_create`: Indicates that method is called during token initialization.
616 /// Allows to bypass ownership check.
484 #[transactional]617 #[transactional]
485 fn modify_token_properties(618 fn modify_token_properties(
486 collection: &NonfungibleHandle<T>,619 collection: &NonfungibleHandle<T>,
574 Ok(())707 Ok(())
575 }708 }
576709
710 /// Batch operation to add or edit properties for the token
711 ///
712 /// Same as [`modify_token_properties`] but doesn't allow to remove properties
713 ///
714 /// [`modify_token_properties`]: struct.Pallet.html#method.modify_token_properties
577 pub fn set_token_properties(715 pub fn set_token_properties(
578 collection: &NonfungibleHandle<T>,716 collection: &NonfungibleHandle<T>,
579 sender: &T::CrossAccountId,717 sender: &T::CrossAccountId,
592 )730 )
593 }731 }
594732
733 /// Add or edit single property for the token
734 ///
735 /// Calls [`set_token_properties`] internally
736 ///
737 /// [`set_token_properties`]: struct.Pallet.html#method.set_token_properties
595 pub fn set_token_property(738 pub fn set_token_property(
596 collection: &NonfungibleHandle<T>,739 collection: &NonfungibleHandle<T>,
597 sender: &T::CrossAccountId,740 sender: &T::CrossAccountId,
611 )754 )
612 }755 }
613756
757 /// Batch operation to remove properties from the token
758 ///
759 /// Same as [`modify_token_properties`] but doesn't allow to add or edit properties
760 ///
761 /// [`modify_token_properties`]: struct.Pallet.html#method.modify_token_properties
614 pub fn delete_token_properties(762 pub fn delete_token_properties(
615 collection: &NonfungibleHandle<T>,763 collection: &NonfungibleHandle<T>,
616 sender: &T::CrossAccountId,764 sender: &T::CrossAccountId,
630 )778 )
631 }779 }
632780
781 /// Remove single property from the token
782 ///
783 /// Calls [`delete_token_properties`] internally
784 ///
785 /// [`delete_token_properties`]: struct.Pallet.html#method.delete_token_properties
633 pub fn delete_token_property(786 pub fn delete_token_property(
634 collection: &NonfungibleHandle<T>,787 collection: &NonfungibleHandle<T>,
635 sender: &T::CrossAccountId,788 sender: &T::CrossAccountId,
646 )799 )
647 }800 }
648801
802 /// Add or edit properties for the collection
649 pub fn set_collection_properties(803 pub fn set_collection_properties(
650 collection: &NonfungibleHandle<T>,804 collection: &NonfungibleHandle<T>,
651 sender: &T::CrossAccountId,805 sender: &T::CrossAccountId,
654 <PalletCommon<T>>::set_collection_properties(collection, sender, properties)808 <PalletCommon<T>>::set_collection_properties(collection, sender, properties)
655 }809 }
656810
811 /// Remove properties from the collection
657 pub fn delete_collection_properties(812 pub fn delete_collection_properties(
658 collection: &CollectionHandle<T>,813 collection: &CollectionHandle<T>,
659 sender: &T::CrossAccountId,814 sender: &T::CrossAccountId,
662 <PalletCommon<T>>::delete_collection_properties(collection, sender, property_keys)817 <PalletCommon<T>>::delete_collection_properties(collection, sender, property_keys)
663 }818 }
664819
820 /// Set property permissions for the token.
821 ///
822 /// Sender should be the owner or admin of token's collection.
665 pub fn set_token_property_permissions(823 pub fn set_token_property_permissions(
666 collection: &CollectionHandle<T>,824 collection: &CollectionHandle<T>,
667 sender: &T::CrossAccountId,825 sender: &T::CrossAccountId,
670 <PalletCommon<T>>::set_token_property_permissions(collection, sender, property_permissions)828 <PalletCommon<T>>::set_token_property_permissions(collection, sender, property_permissions)
671 }829 }
672830
831 /// Set property permissions for the collection.
832 ///
833 /// Sender should be the owner or admin of the collection.
673 pub fn set_property_permission(834 pub fn set_property_permission(
674 collection: &CollectionHandle<T>,835 collection: &CollectionHandle<T>,
675 sender: &T::CrossAccountId,836 sender: &T::CrossAccountId,
678 <PalletCommon<T>>::set_property_permission(collection, sender, permission)839 <PalletCommon<T>>::set_property_permission(collection, sender, permission)
679 }840 }
680841
842 /// Transfer NFT token from one account to another.
843 ///
844 /// `from` account stops being the owner and `to` account becomes the owner of the token.
845 /// If `to` is token than `to` becomes owner of the token and the token become nested.
846 /// Unnests token from previous parent if it was nested before.
847 /// Removes allowance for the token if there was any.
848 ///
849 /// - `nesting_budget`: Limit for token nesting depth
681 pub fn transfer(850 pub fn transfer(
682 collection: &NonfungibleHandle<T>,851 collection: &NonfungibleHandle<T>,
683 from: &T::CrossAccountId,852 from: &T::CrossAccountId,
769 Ok(())938 Ok(())
770 }939 }
771940
941 /// Batch operation to mint multiple NFT tokens.
942 ///
943 /// The sender should be the owner/admin of the collection or collection should be configured
944 /// to allow public minting.
945 ///
946 /// - `data`: Contains list of token properties and users who will become the owners of the corresponging tokens.
947 /// - `nesting_budget`: Limit for token nesting depth
772 pub fn create_multiple_items(948 pub fn create_multiple_items(
773 collection: &NonfungibleHandle<T>,949 collection: &NonfungibleHandle<T>,
774 sender: &T::CrossAccountId,950 sender: &T::CrossAccountId,
953 }1129 }
954 }1130 }
9551131
1132 /// Set allowance for the spender to `transfer` or `burn` sender's token.
1133 ///
1134 /// - `token`: Token the spender is allowed to `transfer` or `burn`.
956 pub fn set_allowance(1135 pub fn set_allowance(
957 collection: &NonfungibleHandle<T>,1136 collection: &NonfungibleHandle<T>,
958 sender: &T::CrossAccountId,1137 sender: &T::CrossAccountId,
985 Ok(())1164 Ok(())
986 }1165 }
9871166
1167 /// Checks allowance for the spender to use the token.
988 fn check_allowed(1168 fn check_allowed(
989 collection: &NonfungibleHandle<T>,1169 collection: &NonfungibleHandle<T>,
990 spender: &T::CrossAccountId,1170 spender: &T::CrossAccountId,
1027 Ok(())1207 Ok(())
1028 }1208 }
10291209
1210 /// Transfer NFT token from one account to another.
1211 ///
1212 /// Same as the [`transfer`] but spender doesn't needs to be the owner of the token.
1213 /// The owner should set allowance for the spender to transfer token.
1214 ///
1215 /// [`transfer`]: struct.Pallet.html#method.transfer
1030 pub fn transfer_from(1216 pub fn transfer_from(
1031 collection: &NonfungibleHandle<T>,1217 collection: &NonfungibleHandle<T>,
1032 spender: &T::CrossAccountId,1218 spender: &T::CrossAccountId,
1043 Self::transfer(collection, from, to, token, nesting_budget)1229 Self::transfer(collection, from, to, token, nesting_budget)
1044 }1230 }
10451231
1232 /// Burn NFT token for `from` account.
1233 ///
1234 /// Same as the [`burn`] but spender doesn't need to be an owner of the token. The owner should
1235 /// set allowance for the spender to burn token.
1236 ///
1237 /// [`burn`]: struct.Pallet.html#method.burn
1046 pub fn burn_from(1238 pub fn burn_from(
1047 collection: &NonfungibleHandle<T>,1239 collection: &NonfungibleHandle<T>,
1048 spender: &T::CrossAccountId,1240 spender: &T::CrossAccountId,
1057 Self::burn(collection, from, token)1249 Self::burn(collection, from, token)
1058 }1250 }
10591251
1252 /// Check that `from` token could be nested in `under` token.
1253 ///
1060 pub fn check_nesting(1254 pub fn check_nesting(
1061 handle: &NonfungibleHandle<T>,1255 handle: &NonfungibleHandle<T>,
1062 sender: T::CrossAccountId,1256 sender: T::CrossAccountId,
1126 .collect()1320 .collect()
1127 }1321 }
11281322
1323 /// Mint single NFT token.
1324 ///
1129 /// Delegated to `create_multiple_items`1325 /// Delegated to [`create_multiple_items`]
1326 ///
1327 /// [`create_multiple_items`]: struct.Pallet.html#method.create_multiple_items
1130 pub fn create_item(1328 pub fn create_item(
1131 collection: &NonfungibleHandle<T>,1329 collection: &NonfungibleHandle<T>,
1132 sender: &T::CrossAccountId,1330 sender: &T::CrossAccountId,