Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nonfungibles): add pallet-api/nonfungibles #388

Open
wants to merge 1 commit into
base: chungquantin/feat-nfts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pallets/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ frame-benchmarking.workspace = true
frame-support.workspace = true
frame-system.workspace = true
pallet-assets.workspace = true
pallet-nfts.workspace = true
sp-runtime.workspace = true
sp-std.workspace = true

Expand All @@ -37,6 +38,7 @@ runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-nfts/runtime-benchmarks",
"pop-chain-extension/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
Expand All @@ -47,6 +49,7 @@ std = [
"frame-system/std",
"pallet-assets/std",
"pallet-balances/std",
"pallet-nfts/std",
"pop-chain-extension/std",
"scale-info/std",
"sp-core/std",
Expand All @@ -57,5 +60,6 @@ std = [
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-nfts/try-runtime",
"sp-runtime/try-runtime",
]
1 change: 1 addition & 0 deletions pallets/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use extension::Extension;
use frame_support::pallet_prelude::Weight;

pub mod extension;

Check warning on line 6 in pallets/api/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a module

warning: missing documentation for a module --> pallets/api/src/lib.rs:6:1 | 6 | pub mod extension; | ^^^^^^^^^^^^^^^^^
pub mod fungibles;
#[cfg(test)]
mod mock;
pub mod nonfungibles;

/// Trait for performing reads of runtime state.
pub trait Read {
Expand Down
93 changes: 90 additions & 3 deletions pallets/api/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use codec::{Decode, Encode};
use frame_support::{
derive_impl, parameter_types,
traits::{AsEnsureOriginWithArg, ConstU128, ConstU32, Everything},
traits::{AsEnsureOriginWithArg, ConstU128, ConstU32, ConstU64, Everything},
};
use frame_system::{EnsureRoot, EnsureSigned};
use pallet_nfts::PalletFeatures;
use scale_info::TypeInfo;
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Lazy, Verify},
BuildStorage,
};

Expand All @@ -29,6 +32,8 @@ frame_support::construct_runtime!(
Assets: pallet_assets::<Instance1>,
Balances: pallet_balances,
Fungibles: crate::fungibles,
Nfts: pallet_nfts::<Instance1>,
NonFungibles: crate::nonfungibles
}
);

Expand Down Expand Up @@ -91,7 +96,7 @@ impl pallet_assets::Config<AssetsInstance> for Test {
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
type CallbackHandle = ();
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<u64>>;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<Self::AccountId>>;
type Currency = Balances;
type Extra = ();
type ForceOrigin = EnsureRoot<u64>;
Expand All @@ -110,6 +115,88 @@ impl crate::fungibles::Config for Test {
type WeightInfo = ();
}

parameter_types! {
pub storage Features: PalletFeatures = PalletFeatures::all_enabled();
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, TypeInfo)]
pub struct Noop;

impl IdentifyAccount for Noop {
type AccountId = AccountId;

fn into_account(self) -> Self::AccountId {
0
}
}

impl Verify for Noop {
type Signer = Noop;

fn verify<L: Lazy<[u8]>>(
&self,
_msg: L,
_signer: &<Self::Signer as IdentifyAccount>::AccountId,
) -> bool {
false
}
}

#[cfg(feature = "runtime-benchmarks")]
impl pallet_nfts::pallet::BenchmarkHelper<u32, u32, Noop, u64, Noop> for () {
fn collection(i: u16) -> u32 {
i.into()
}

fn item(i: u16) -> u32 {
i.into()
}

fn signer() -> (Noop, u64) {
unimplemented!()
}

fn sign(signer: &Noop, message: &[u8]) -> Noop {
unimplemented!()
}
}

type NftsInstance = pallet_nfts::Instance1;
impl pallet_nfts::Config<NftsInstance> for Test {
type ApprovalsLimit = ConstU32<10>;
type AttributeDepositBase = ConstU128<1>;
type CollectionDeposit = ConstU128<2>;
type CollectionId = u32;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<u64>>;
type Currency = Balances;
type DepositPerByte = ConstU128<1>;
type Features = Features;
type ForceOrigin = frame_system::EnsureRoot<Self::AccountId>;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type ItemAttributesApprovalsLimit = ConstU32<2>;
type ItemDeposit = ConstU128<1>;
type ItemId = u32;
type KeyLimit = ConstU32<50>;
type Locker = ();
type MaxAttributesPerCall = ConstU32<2>;
type MaxDeadlineDuration = ConstU64<10000>;
type MaxTips = ConstU32<10>;
type MetadataDepositBase = ConstU128<1>;
type OffchainPublic = Noop;
type OffchainSignature = Noop;
type RuntimeEvent = RuntimeEvent;
type StringLimit = ConstU32<50>;
type ValueLimit = ConstU32<50>;
type WeightInfo = ();
}

impl crate::nonfungibles::Config for Test {
type NftsInstance = NftsInstance;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default()
.build_storage()
Expand Down
110 changes: 110 additions & 0 deletions pallets/api/src/nonfungibles/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! Benchmarking setup for pallet_api::nonfungibles

use frame_benchmarking::{account, v2::*};
use frame_support::{traits::nonfungibles_v2::Inspect, BoundedVec};
use sp_runtime::traits::Zero;

use super::{AttributeNamespace, CollectionIdOf, Config, ItemIdOf, NftsInstanceOf, Pallet, Read};
use crate::Read as _;

const SEED: u32 = 1;

#[benchmarks(
where
<pallet_nfts::Pallet<T, NftsInstanceOf<T>> as Inspect<<T as frame_system::Config>::AccountId>>::ItemId: Zero,
<pallet_nfts::Pallet<T, NftsInstanceOf<T>> as Inspect<<T as frame_system::Config>::AccountId>>::CollectionId: Zero,
)]
mod benchmarks {
use super::*;

#[benchmark]
// Storage: `Collection`
fn total_supply() {
#[block]
{
Pallet::<T>::read(Read::TotalSupply(CollectionIdOf::<T>::zero()));
}
}

#[benchmark]
// Storage: `AccountBalance`
fn balance_of() {
#[block]
{
Pallet::<T>::read(Read::BalanceOf {
collection: CollectionIdOf::<T>::zero(),
owner: account("Alice", 0, SEED),
});
}
}

#[benchmark]
// Storage: `Allowances`, `Item`
fn allowance() {
#[block]
{
Pallet::<T>::read(Read::Allowance {
collection: CollectionIdOf::<T>::zero(),
owner: account("Alice", 0, SEED),
operator: account("Bob", 0, SEED),
item: Some(ItemIdOf::<T>::zero()),
});
}
}

#[benchmark]
// Storage: `Item`
fn owner_of() {
#[block]
{
Pallet::<T>::read(Read::OwnerOf {
collection: CollectionIdOf::<T>::zero(),
item: ItemIdOf::<T>::zero(),
});
}
}

#[benchmark]
// Storage: `Attribute`
fn get_attribute() {
#[block]
{
Pallet::<T>::read(Read::GetAttribute {
key: BoundedVec::default(),
collection: CollectionIdOf::<T>::zero(),
item: ItemIdOf::<T>::zero(),
namespace: AttributeNamespace::CollectionOwner,
});
}
}

#[benchmark]
// Storage: `Collection`
fn collection() {
#[block]
{
Pallet::<T>::read(Read::Collection(CollectionIdOf::<T>::zero()));
}
}

#[benchmark]
// Storage: `NextCollectionId`
fn next_collection_id() {
#[block]
{
Pallet::<T>::read(Read::NextCollectionId);
}
}

#[benchmark]
// Storage: `ItemMetadata`
fn item_metadata() {
#[block]
{
Pallet::<T>::read(Read::ItemMetadata {
collection: CollectionIdOf::<T>::zero(),
item: ItemIdOf::<T>::zero(),
});
}
}
}
Loading
Loading