This is a FRAME pallet that defines and implements a non-fungible token (NFT) interface as well as an interface for managing a set of such assets, including asset ownership, creation, destruction and transfer.
This package defines two public traits (Rust interfaces) for working with NFTs: the NFT
trait and the
UniqueAssets
trait.
NFT
TraitThe NFT
trait uses two types to define a unique asset:
ID
: a URI for the assetInfo
: a set of attributes that uniquely describe the assetAssets with equivalent attributes (as defined by the Info
type) must have an equal ID
and assets with different
ID
s must not have equivalent attributes.
UniqueAssets
TraitThis trait is generic with respect to a type that implements the NFT
trait; it defines the type abstractions and
public functions needed to manage a set of unique assets.
AccountId
: the type used to identify asset ownersAssetLimit
: the maximum number of assets, expressed as an unsigned 128-bit integer, that may exist in this set at
onceUserAssetLimit
: the maximum number of assets, expressed as an unsigned 64-bit integer, that any single account may
own from this set at oncetotal() -> u128
: returns the total number of assets in this set of assetsburned() -> u128
: returns the total number of assets from this set that have been burnedtotal_for_account(AccountId) -> u64
: returns the total number of asset from this set that are owned by a given
accountassets_for_account(AccountId) -> Vec<NFT>
: returns the list of assets from this set that are owned by a given
accountowner_of(NFT::Id) -> AccountId
: returns the ID of the account that owns the given asset from this setmint(AccountId, NFT::Info) -> Result<AssetID, DispatchError>
: use the given attributes to create a new unique asset
that belongs to this set and assign ownership of it to the given account
burn(NFT::Id) -> DispatchResult
: destroy the given asset
transfer(AccountId, NFT::Id) -> DispatchResult
: transfer ownership of the given asset from this set from its current
owner to a given target account
The reference implementation defined in this project is referred to as a "commodity" - a unique asset that is designed for frequent trading. In order to optimize for this use case, sorted lists of assets are stored per owner. Although maintaining a sorted list is trivial with Rust vectors, which implement a binary search API that can be used for sorted insertion, it introduces significant overhead when an asset is created because the entire list must be decoded from the backing trie in order to insert the new asset in the correct spot. Maintaining a sorted asset list is desireable for the commodity use case, however, because it allows assets to be efficiently located when destroying or transferring them. An alternative implementation, the Keepsake pallet, is in the works :rocket:
Refer to the mock runtime and provided tests to see the NFT implementation in action.
This project was inspired by works such as the following:
Thanks to the following people who helped me overcome my relatively limited understanding of Rust.
This project was forked from the Substrate DevHub Pallet Template.