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>
This commit is contained in:
Alex Bean
2023-10-18 12:33:29 +02:00
committed by GitHub
parent d3ea69b7ac
commit 3aaf62add4
4 changed files with 153 additions and 3 deletions
+2 -1
View File
@@ -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;
@@ -119,7 +119,7 @@ pub trait InspectEnumerable<AccountId>: Inspect<AccountId> {
}
/// 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<AccountId, ItemConfig>: Inspect<AccountId> {
/// Mint some `item` to be owned by `who`.
///
@@ -158,6 +158,13 @@ pub trait Mutate<AccountId, ItemConfig>: Inspect<AccountId> {
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<AccountId, ItemConfig>: Inspect<AccountId> {
fn clear_typed_attribute<K: Encode>(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.
@@ -233,7 +233,7 @@ pub trait Destroy<AccountId>: Inspect<AccountId> {
}
/// 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<AccountId, ItemConfig>: Inspect<AccountId> {
/// Mint some `item` of `collection` to be owned by `who`.
///
@@ -307,6 +307,29 @@ pub trait Mutate<AccountId, ItemConfig>: Inspect<AccountId> {
})
}
/// 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<AccountId, ItemConfig>: Inspect<AccountId> {
) -> 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<AccountId>: Inspect<AccountId> {
Err(TokenError::Unsupported.into())
}
}
/// Trait for trading non-fungible items.
pub trait Trading<AccountId, ItemPrice>: Inspect<AccountId> {
/// 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<ItemPrice>,
whitelisted_buyer: Option<AccountId>,
) -> 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<ItemPrice>;
}