Skip to content

Commit

Permalink
feat: add pallet-api/nonfungibles
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Dec 20, 2024
1 parent 04c69d7 commit 8d4ae0d
Showing 1 changed file with 51 additions and 70 deletions.
121 changes: 51 additions & 70 deletions pallets/api/src/nonfungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use weights::WeightInfo;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
mod impls;
#[cfg(test)]
mod tests;
pub mod weights;
Expand Down Expand Up @@ -46,7 +45,7 @@ pub(super) type CollectionOf<T> = pallet_nfts::Collection<T, NftsInstanceOf<T>>;
#[frame_support::pallet]
pub mod pallet {
use frame_support::{
dispatch::{DispatchResult, DispatchResultWithPostInfo},
dispatch::{DispatchResult, DispatchResultWithPostInfo, WithPostDispatchInfo},
pallet_prelude::*,
traits::Incrementable,
};
Expand All @@ -63,18 +62,18 @@ pub mod pallet {
#[repr(u8)]
#[allow(clippy::unnecessary_cast)]
pub enum Read<T: Config> {
/// Total item supply of a specified `collection`.
/// Total item supply of a specified collection.
#[codec(index = 0)]
TotalSupply(CollectionIdOf<T>),
/// Account balance for a specified `collection`.
/// Account balance for a specified collection.
#[codec(index = 1)]
BalanceOf {
/// The collection.
collection: CollectionIdOf<T>,
/// The owner of the collection .
owner: AccountIdOf<T>,
},
/// Allowance for an `operator` approved by an `owner`, for a specified collection or item.
/// Allowance for an operator approved by an owner, for a specified collection or item.
#[codec(index = 2)]
Allowance {
/// The collection.
Expand All @@ -94,7 +93,8 @@ pub mod pallet {
/// The collection item.
item: ItemIdOf<T>,
},
/// Attribute value of a specified collection item.
/// Attribute value of a specified collection item. (Error: bounded collection is not
/// partial)
#[codec(index = 6)]
GetAttribute {
/// The collection.
Expand Down Expand Up @@ -181,10 +181,9 @@ pub mod pallet {
pub enum Event<T: Config> {
/// Event emitted when allowance by `owner` to `operator` changes.
Approval {
/// The identifier of the collection.
/// The collection ID.
collection: CollectionIdOf<T>,
/// The item which is (dis)approved. `None` for all collection items owned by the
/// `owner`.
/// The item which is (dis)approved. `None` for all owner's items.
item: Option<ItemIdOf<T>>,
/// The owner providing the allowance.
owner: AccountIdOf<T>,
Expand Down Expand Up @@ -220,53 +219,44 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Transfers the collection item from the `origin` to account `to`.
/// Transfers the collection item from the caller's account to account `to`.
///
/// # Parameters
/// - `collection` - The collection of the item to be transferred.
/// - `collection` - The collection of the item to transfer.
/// - `item` - The item to transfer.
/// - `to` - The recipient account.
#[pallet::call_index(3)]
#[pallet::weight(NftsWeightInfoOf::<T>::transfer() + T::DbWeight::get().reads_writes(1, 0))]
#[pallet::weight(NftsWeightInfoOf::<T>::transfer())]
pub fn transfer(
origin: OriginFor<T>,
collection: CollectionIdOf<T>,
item: ItemIdOf<T>,
to: AccountIdOf<T>,
) -> DispatchResult {
ensure_signed(origin.clone())?;
let owner =
NftsOf::<T>::owner(collection, item).ok_or(NftsErrorOf::<T>::UnknownItem)?;
let from = ensure_signed(origin.clone())?;
NftsOf::<T>::transfer(origin, collection, item, T::Lookup::unlookup(to.clone()))?;
Self::deposit_event(Event::Transfer {
collection,
item,
from: Some(owner),
from: Some(from),
to: Some(to),
price: None,
});
Ok(())
}

/// Either approve or cancel approval for an `operator` to perform transfers of a specific
/// collection item or all collection items owned by the `origin`.
/// Approves `operator` to spend the collection item on behalf of the caller.
///
/// # Parameters
/// - `collection` - The identifier of the collection.
/// - `item` - An optional parameter specifying the item to approve for the delegated
/// transfer. If `None`, all owner's collection items will be approved.
/// - `operator` - The account being granted or revoked approval to transfer the specified
/// collection item(s).
/// - `approved` - A boolean indicating the desired approval status:
/// - `true` to approve the `operator`.
/// - `false` to cancel the approval granted to the `operator`.
/// - `collection` - The collection of the item to approve for a delegated transfer.
/// - `item` - The item to approve for a delegated transfer.
/// - `operator` - The account that is allowed to spend the collection item.
/// - `approved` - The approval status of the collection item.
#[pallet::call_index(4)]
#[pallet::weight(
NftsWeightInfoOf::<T>::approve_transfer() +
NftsWeightInfoOf::<T>::approve_collection_transfer() +
NftsWeightInfoOf::<T>::cancel_collection_approval() +
NftsWeightInfoOf::<T>::cancel_approval()
)]
NftsWeightInfoOf::<T>::approve_transfer(item.is_some() as u32) +

Check failure on line 257 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function takes 0 arguments but 1 argument was supplied

error[E0061]: this function takes 0 arguments but 1 argument was supplied --> pallets/api/src/nonfungibles/mod.rs:257:13 | 257 | NftsWeightInfoOf::<T>::approve_transfer(item.is_some() as u32) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --------------------- unexpected argument of type `u32` | note: associated function defined here --> /home/runner/work/pop-node/pop-node/pallets/nfts/src/weights.rs:62:5 | 62 | fn approve_transfer() -> Weight; | ^^^^^^^^^^^^^^^^ help: remove the extra argument | 257 - NftsWeightInfoOf::<T>::approve_transfer(item.is_some() as u32) + 257 + NftsWeightInfoOf::<T>::approve_transfer() + |
NftsWeightInfoOf::<T>::cancel_approval(item.is_some() as u32)

Check failure on line 258 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function takes 0 arguments but 1 argument was supplied

error[E0061]: this function takes 0 arguments but 1 argument was supplied --> pallets/api/src/nonfungibles/mod.rs:258:7 | 258 | NftsWeightInfoOf::<T>::cancel_approval(item.is_some() as u32) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --------------------- unexpected argument of type `u32` | note: associated function defined here --> /home/runner/work/pop-node/pop-node/pallets/nfts/src/weights.rs:65:5 | 65 | fn cancel_approval() -> Weight; | ^^^^^^^^^^^^^^^ help: remove the extra argument | 258 - NftsWeightInfoOf::<T>::cancel_approval(item.is_some() as u32) 258 + NftsWeightInfoOf::<T>::cancel_approval() |
)]
pub fn approve(
origin: OriginFor<T>,
collection: CollectionIdOf<T>,
Expand All @@ -275,43 +265,32 @@ pub mod pallet {
approved: bool,
) -> DispatchResultWithPostInfo {
let owner = ensure_signed(origin.clone())?;
let result = if approved {
Self::do_approve(origin, collection, item, &operator)
let weight = if approved {
NftsOf::<T>::approve_transfer(
origin,
collection,
item,

Check failure on line 272 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

error[E0308]: mismatched types --> pallets/api/src/nonfungibles/mod.rs:272:6 | 269 | NftsOf::<T>::approve_transfer( | ----------------------------- arguments to this function are incorrect ... 272 | item, | ^^^^ expected associated type, found `Option<<T as Config<...>>::ItemId>` | = note: expected associated type `<T as pallet_nfts::Config<<T as nonfungibles::pallet::Config>::NftsInstance>>::ItemId` found enum `std::option::Option<<T as pallet_nfts::Config<<T as nonfungibles::pallet::Config>::NftsInstance>>::ItemId>` note: associated function defined here --> /home/runner/work/pop-node/pop-node/pallets/nfts/src/lib.rs:1347:10 | 1347 | pub fn approve_transfer( | ^^^^^^^^^^^^^^^^ help: consider using `Option::expect` to unwrap the `std::option::Option<<T as pallet_nfts::Config<<T as nonfungibles::pallet::Config>::NftsInstance>>::ItemId>` value, panicking if the value is an `Option::None` | 272 | item.expect("REASON"), | +++++++++++++++++
T::Lookup::unlookup(operator.clone()),
None,
)
.map_err(|e| {
e.with_weight(NftsWeightInfoOf::<T>::approve_transfer(item.is_some() as u32))

Check failure on line 277 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function takes 0 arguments but 1 argument was supplied

error[E0061]: this function takes 0 arguments but 1 argument was supplied --> pallets/api/src/nonfungibles/mod.rs:277:20 | 277 | e.with_weight(NftsWeightInfoOf::<T>::approve_transfer(item.is_some() as u32)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --------------------- unexpected argument of type `u32` | note: associated function defined here --> /home/runner/work/pop-node/pop-node/pallets/nfts/src/weights.rs:62:5 | 62 | fn approve_transfer() -> Weight; | ^^^^^^^^^^^^^^^^ help: remove the extra argument | 277 - e.with_weight(NftsWeightInfoOf::<T>::approve_transfer(item.is_some() as u32)) 277 + e.with_weight(NftsWeightInfoOf::<T>::approve_transfer()) |
})?;
NftsWeightInfoOf::<T>::approve_transfer(item.is_some() as u32)

Check failure on line 279 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function takes 0 arguments but 1 argument was supplied

error[E0061]: this function takes 0 arguments but 1 argument was supplied --> pallets/api/src/nonfungibles/mod.rs:279:5 | 279 | NftsWeightInfoOf::<T>::approve_transfer(item.is_some() as u32) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --------------------- unexpected argument of type `u32` | note: associated function defined here --> /home/runner/work/pop-node/pop-node/pallets/nfts/src/weights.rs:62:5 | 62 | fn approve_transfer() -> Weight; | ^^^^^^^^^^^^^^^^ help: remove the extra argument | 279 - NftsWeightInfoOf::<T>::approve_transfer(item.is_some() as u32) 279 + NftsWeightInfoOf::<T>::approve_transfer() |
} else {
Self::do_cancel_approval(origin, collection, item, &operator)
NftsOf::<T>::cancel_approval(
origin,
collection,
item,

Check failure on line 284 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

error[E0308]: mismatched types --> pallets/api/src/nonfungibles/mod.rs:284:6 | 281 | NftsOf::<T>::cancel_approval( | ---------------------------- arguments to this function are incorrect ... 284 | item, | ^^^^ expected associated type, found `Option<<T as Config<...>>::ItemId>` | = note: expected associated type `<T as pallet_nfts::Config<<T as nonfungibles::pallet::Config>::NftsInstance>>::ItemId` found enum `std::option::Option<<T as pallet_nfts::Config<<T as nonfungibles::pallet::Config>::NftsInstance>>::ItemId>` note: associated function defined here --> /home/runner/work/pop-node/pop-node/pallets/nfts/src/lib.rs:1383:10 | 1383 | pub fn cancel_approval( | ^^^^^^^^^^^^^^^ help: consider using `Option::expect` to unwrap the `std::option::Option<<T as pallet_nfts::Config<<T as nonfungibles::pallet::Config>::NftsInstance>>::ItemId>` value, panicking if the value is an `Option::None` | 284 | item.expect("REASON"), | +++++++++++++++++
T::Lookup::unlookup(operator.clone()),
)
.map_err(|e| {
e.with_weight(NftsWeightInfoOf::<T>::cancel_approval(item.is_some() as u32))

Check failure on line 288 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function takes 0 arguments but 1 argument was supplied

error[E0061]: this function takes 0 arguments but 1 argument was supplied --> pallets/api/src/nonfungibles/mod.rs:288:20 | 288 | e.with_weight(NftsWeightInfoOf::<T>::cancel_approval(item.is_some() as u32)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --------------------- unexpected argument of type `u32` | note: associated function defined here --> /home/runner/work/pop-node/pop-node/pallets/nfts/src/weights.rs:65:5 | 65 | fn cancel_approval() -> Weight; | ^^^^^^^^^^^^^^^ help: remove the extra argument | 288 - e.with_weight(NftsWeightInfoOf::<T>::cancel_approval(item.is_some() as u32)) 288 + e.with_weight(NftsWeightInfoOf::<T>::cancel_approval()) |
})?;
NftsWeightInfoOf::<T>::cancel_approval(item.is_some() as u32)

Check failure on line 290 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function takes 0 arguments but 1 argument was supplied

error[E0061]: this function takes 0 arguments but 1 argument was supplied --> pallets/api/src/nonfungibles/mod.rs:290:5 | 290 | NftsWeightInfoOf::<T>::cancel_approval(item.is_some() as u32) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --------------------- unexpected argument of type `u32` | note: associated function defined here --> /home/runner/work/pop-node/pop-node/pallets/nfts/src/weights.rs:65:5 | 65 | fn cancel_approval() -> Weight; | ^^^^^^^^^^^^^^^ help: remove the extra argument | 290 - NftsWeightInfoOf::<T>::cancel_approval(item.is_some() as u32) 290 + NftsWeightInfoOf::<T>::cancel_approval() |
};
Self::deposit_event(Event::Approval { collection, item, operator, owner, approved });
result
}

/// Cancel all the approvals of a specific item.
///
/// # Parameters
/// - `collection` - The collection of the item of whose approvals will be cleared.
/// - `item` - The item of the collection of whose approvals will be cleared.
#[pallet::call_index(5)]
#[pallet::weight(NftsWeightInfoOf::<T>::clear_all_transfer_approvals())]
pub fn clear_all_transfer_approvals(
origin: OriginFor<T>,
collection: CollectionIdOf<T>,
item: ItemIdOf<T>,
) -> DispatchResult {
NftsOf::<T>::clear_all_transfer_approvals(origin, collection, item)
}

/// Cancel approvals to transfer all owner's collection items.
///
/// # Parameters
/// - `collection` - The collection whose approvals will be cleared.
/// - `limit` - The amount of collection approvals that will be cleared.
#[pallet::call_index(6)]
#[pallet::weight(NftsWeightInfoOf::<T>::clear_collection_approvals(*limit))]
pub fn clear_collection_approvals(
origin: OriginFor<T>,
collection: CollectionIdOf<T>,
limit: u32,
) -> DispatchResultWithPostInfo {
NftsOf::<T>::clear_collection_approvals(origin, collection, limit)
Ok(Some(weight).into())
}

/// Issue a new collection of non-fungible items from a public origin.
Expand Down Expand Up @@ -349,6 +328,8 @@ pub mod pallet {
witness.item_metadatas,
witness.item_configs,
witness.attributes,
witness.item_holders,

Check failure on line 331 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

no field `item_holders` on type `&pallet_nfts::DestroyWitness`

error[E0609]: no field `item_holders` on type `&pallet_nfts::DestroyWitness` --> pallets/api/src/nonfungibles/mod.rs:331:21 | 331 | witness.item_holders, | ^^^^^^^^^^^^ unknown field | = note: available fields are: `item_metadatas`, `item_configs`, `attributes`
witness.allowances,

Check failure on line 332 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

no field `allowances` on type `&pallet_nfts::DestroyWitness`

error[E0609]: no field `allowances` on type `&pallet_nfts::DestroyWitness` --> pallets/api/src/nonfungibles/mod.rs:332:21 | 332 | witness.allowances, | ^^^^^^^^^^ unknown field | = note: available fields are: `item_metadatas`, `item_configs`, `attributes`
))]
pub fn destroy(
origin: OriginFor<T>,
Expand Down Expand Up @@ -506,7 +487,7 @@ pub mod pallet {
item: ItemIdOf<T>,
witness: MintWitness<ItemIdOf<T>, ItemPriceOf<T>>,
) -> DispatchResult {
let owner = ensure_signed(origin.clone())?;
let account = ensure_signed(origin.clone())?;
let mint_price = witness.mint_price;
NftsOf::<T>::mint(
origin,
Expand All @@ -519,7 +500,7 @@ pub mod pallet {
collection,
item,
from: None,
to: Some(owner),
to: Some(account),
price: mint_price,
});
Ok(())
Expand All @@ -537,12 +518,12 @@ pub mod pallet {
collection: CollectionIdOf<T>,
item: ItemIdOf<T>,
) -> DispatchResult {
let owner = ensure_signed(origin.clone())?;
let account = ensure_signed(origin.clone())?;
NftsOf::<T>::burn(origin, collection, item)?;
Self::deposit_event(Event::Transfer {
collection,
item,
from: Some(owner),
from: Some(account),
to: None,
price: None,
});
Expand Down Expand Up @@ -588,7 +569,7 @@ pub mod pallet {
BalanceOf { collection, owner } =>
ReadResult::BalanceOf(AccountBalanceOf::<T>::get(collection, owner)),
Allowance { collection, owner, operator, item } => ReadResult::Allowance(
NftsOf::<T>::check_approval(&collection, &item, &owner, &operator).is_ok(),
NftsOf::<T>::check_allowance(&collection, &item, &owner, &operator).is_ok(),

Check failure on line 572 in pallets/api/src/nonfungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

no function or associated item named `check_allowance` found for struct `pallet_nfts::Pallet` in the current scope

error[E0599]: no function or associated item named `check_allowance` found for struct `pallet_nfts::Pallet` in the current scope --> pallets/api/src/nonfungibles/mod.rs:572:19 | 572 | NftsOf::<T>::check_allowance(&collection, &item, &owner, &operator).is_ok(), | ^^^^^^^^^^^^^^^ function or associated item not found in `Pallet<T, <T as Config>::NftsInstance>`
),
OwnerOf { collection, item } =>
ReadResult::OwnerOf(NftsOf::<T>::owner(collection, item)),
Expand Down

0 comments on commit 8d4ae0d

Please sign in to comment.