From 6379fa1f4e8b138dbff385dc06f856dbaf301e3b Mon Sep 17 00:00:00 2001 From: Alex Bean Date: Wed, 18 Oct 2023 12:33:29 +0200 Subject: [PATCH] Trading trait and deal with metadata in Mutate trait for nonfungibles_v2 (#1561) I have added some Traits that are missing and are useful for dealing with non-fungible tokens on other pallets and their implementations for NFTs pallet. - In the Mutate trait, added methods for dealing with the metadata: `set_metadata`, `set_collection_metadata`, `clear_metadata` and `clear_collection_metadata`. The motivation of adding this methods coming from a StackExchange question asking for it: [Setting metadata of an item of the Nfts pallet in a custom pallet](https://substrate.stackexchange.com/questions/9974/setting-metadata-of-an-item-of-the-nfts-pallet-in-a-custom-pallet) - A Trait for trading non-fungible items. The methods in that Trait are `buy_item`, `set_price` and `item_price` An example of where this Trait can be useful is a pallet that deals with [NFT Royalties](https://forum.polkadot.network/t/nfts-royalty-pallet/3766) and needs to perform this actions. --------- Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com> --- substrate/frame/nfts/src/impl_nonfungibles.rs | 67 ++++++++++++++++++ substrate/frame/support/src/traits.rs | 3 +- .../src/traits/tokens/nonfungible_v2.rs | 16 ++++- .../src/traits/tokens/nonfungibles_v2.rs | 70 ++++++++++++++++++- 4 files changed, 153 insertions(+), 3 deletions(-) diff --git a/substrate/frame/nfts/src/impl_nonfungibles.rs b/substrate/frame/nfts/src/impl_nonfungibles.rs index 4e2593b4057d..4a6b70eb9973 100644 --- a/substrate/frame/nfts/src/impl_nonfungibles.rs +++ b/substrate/frame/nfts/src/impl_nonfungibles.rs @@ -320,6 +320,33 @@ impl, I: 'static> Mutate<::AccountId, ItemConfig }) } + fn set_item_metadata( + who: Option<&T::AccountId>, + collection: &Self::CollectionId, + item: &Self::ItemId, + data: &[u8], + ) -> DispatchResult { + Self::do_set_item_metadata( + who.cloned(), + *collection, + *item, + Self::construct_metadata(data.to_vec())?, + None, + ) + } + + fn set_collection_metadata( + who: Option<&T::AccountId>, + collection: &Self::CollectionId, + data: &[u8], + ) -> DispatchResult { + Self::do_set_collection_metadata( + who.cloned(), + *collection, + Self::construct_metadata(data.to_vec())?, + ) + } + fn clear_attribute( collection: &Self::CollectionId, item: &Self::ItemId, @@ -362,6 +389,21 @@ impl, I: 'static> Mutate<::AccountId, ItemConfig >::clear_collection_attribute(collection, k) }) } + + fn clear_item_metadata( + who: Option<&T::AccountId>, + collection: &Self::CollectionId, + item: &Self::ItemId, + ) -> DispatchResult { + Self::do_clear_item_metadata(who.cloned(), *collection, *item) + } + + fn clear_collection_metadata( + who: Option<&T::AccountId>, + collection: &Self::CollectionId, + ) -> DispatchResult { + Self::do_clear_collection_metadata(who.cloned(), *collection) + } } impl, I: 'static> Transfer for Pallet { @@ -398,6 +440,31 @@ impl, I: 'static> Transfer for Pallet { } } +impl, I: 'static> Trading> for Pallet { + fn buy_item( + collection: &Self::CollectionId, + item: &Self::ItemId, + buyer: &T::AccountId, + bid_price: &ItemPrice, + ) -> DispatchResult { + Self::do_buy_item(*collection, *item, buyer.clone(), *bid_price) + } + + fn set_price( + collection: &Self::CollectionId, + item: &Self::ItemId, + sender: &T::AccountId, + price: Option>, + whitelisted_buyer: Option, + ) -> DispatchResult { + Self::do_set_price(*collection, *item, sender.clone(), price, whitelisted_buyer) + } + + fn item_price(collection: &Self::CollectionId, item: &Self::ItemId) -> Option> { + ItemPriceOf::::get(collection, item).map(|a| a.0) + } +} + impl, I: 'static> InspectEnumerable for Pallet { type CollectionsIterator = KeyPrefixIterator<>::CollectionId>; type ItemsIterator = KeyPrefixIterator<>::ItemId>; diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index 69885b2873e3..1532d67facd3 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -27,7 +27,8 @@ pub use tokens::{ }, fungible, fungibles, imbalance::{Imbalance, OnUnbalanced, SignedImbalance}, - nonfungible, nonfungibles, BalanceStatus, ExistenceRequirement, Locker, WithdrawReasons, + nonfungible, nonfungible_v2, nonfungibles, nonfungibles_v2, BalanceStatus, + ExistenceRequirement, Locker, WithdrawReasons, }; mod members; diff --git a/substrate/frame/support/src/traits/tokens/nonfungible_v2.rs b/substrate/frame/support/src/traits/tokens/nonfungible_v2.rs index c4463e0070f9..788a4d25e810 100644 --- a/substrate/frame/support/src/traits/tokens/nonfungible_v2.rs +++ b/substrate/frame/support/src/traits/tokens/nonfungible_v2.rs @@ -119,7 +119,7 @@ pub trait InspectEnumerable: Inspect { } /// Trait for providing an interface for NFT-like items which may be minted, burned and/or have -/// attributes set on them. +/// attributes and metadata set on them. pub trait Mutate: Inspect { /// Mint some `item` to be owned by `who`. /// @@ -158,6 +158,13 @@ pub trait Mutate: Inspect { key.using_encoded(|k| value.using_encoded(|v| Self::set_attribute(item, k, v))) } + /// Set the metadata `data` of an `item`. + /// + /// By default, this is not a supported operation. + fn set_metadata(_who: &AccountId, _item: &Self::ItemId, _data: &[u8]) -> DispatchResult { + Err(TokenError::Unsupported.into()) + } + /// Clear attribute of `item`'s `key`. /// /// By default, this is not a supported operation. @@ -171,6 +178,13 @@ pub trait Mutate: Inspect { fn clear_typed_attribute(item: &Self::ItemId, key: &K) -> DispatchResult { key.using_encoded(|k| Self::clear_attribute(item, k)) } + + /// Clear the metadata of an `item`. + /// + /// By default, this is not a supported operation. + fn clear_metadata(_who: &AccountId, _item: &Self::ItemId) -> DispatchResult { + Err(TokenError::Unsupported.into()) + } } /// Trait for transferring and controlling the transfer of non-fungible sets of items. diff --git a/substrate/frame/support/src/traits/tokens/nonfungibles_v2.rs b/substrate/frame/support/src/traits/tokens/nonfungibles_v2.rs index ec064bdebf62..868afbdf7eee 100644 --- a/substrate/frame/support/src/traits/tokens/nonfungibles_v2.rs +++ b/substrate/frame/support/src/traits/tokens/nonfungibles_v2.rs @@ -233,7 +233,7 @@ pub trait Destroy: Inspect { } /// Trait for providing an interface for multiple collections of NFT-like items which may be -/// minted, burned and/or have attributes set on them. +/// minted, burned and/or have attributes and metadata set on them. pub trait Mutate: Inspect { /// Mint some `item` of `collection` to be owned by `who`. /// @@ -307,6 +307,29 @@ pub trait Mutate: Inspect { }) } + /// Set the metadata `data` of an `item` of `collection`. + /// + /// By default, this is not a supported operation. + fn set_item_metadata( + _who: Option<&AccountId>, + _collection: &Self::CollectionId, + _item: &Self::ItemId, + _data: &[u8], + ) -> DispatchResult { + Err(TokenError::Unsupported.into()) + } + + /// Set the metadata `data` of a `collection`. + /// + /// By default, this is not a supported operation. + fn set_collection_metadata( + _who: Option<&AccountId>, + _collection: &Self::CollectionId, + _data: &[u8], + ) -> DispatchResult { + Err(TokenError::Unsupported.into()) + } + /// Clear attribute of `item` of `collection`'s `key`. /// /// By default, this is not a supported operation. @@ -345,6 +368,27 @@ pub trait Mutate: Inspect { ) -> DispatchResult { key.using_encoded(|k| Self::clear_collection_attribute(collection, k)) } + + /// Clear the metadata of an `item` of `collection`. + /// + /// By default, this is not a supported operation. + fn clear_item_metadata( + _who: Option<&AccountId>, + _collection: &Self::CollectionId, + _item: &Self::ItemId, + ) -> DispatchResult { + Err(TokenError::Unsupported.into()) + } + + /// Clear the metadata of a `collection`. + /// + /// By default, this is not a supported operation. + fn clear_collection_metadata( + _who: Option<&AccountId>, + _collection: &Self::CollectionId, + ) -> DispatchResult { + Err(TokenError::Unsupported.into()) + } } /// Trait for transferring non-fungible sets of items. @@ -370,3 +414,27 @@ pub trait Transfer: Inspect { Err(TokenError::Unsupported.into()) } } + +/// Trait for trading non-fungible items. +pub trait Trading: Inspect { + /// Allows `buyer` to buy an `item` of `collection` if it's up for sale with a `bid_price` to + /// pay. + fn buy_item( + collection: &Self::CollectionId, + item: &Self::ItemId, + buyer: &AccountId, + bid_price: &ItemPrice, + ) -> DispatchResult; + + /// Sets the item price for `item` to make it available for sale. + fn set_price( + collection: &Self::CollectionId, + item: &Self::ItemId, + sender: &AccountId, + price: Option, + whitelisted_buyer: Option, + ) -> DispatchResult; + + /// Returns the item price of `item` or `None` if the item is not for sale. + fn item_price(collection: &Self::CollectionId, item: &Self::ItemId) -> Option; +}