Skip to content

Commit

Permalink
refactor(nonfungibles): CollectionConfig parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Nov 1, 2024
1 parent 58c3849 commit 45ac5ca
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 72 deletions.
19 changes: 4 additions & 15 deletions pallets/api/src/nonfungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ pub mod pallet {
use sp_runtime::BoundedVec;
use sp_std::vec::Vec;
use types::{
AccountIdOf, AttributeNamespaceOf, BalanceOf, CollectionDetailsFor, CollectionIdOf,
CreateCollectionConfigFor, ItemIdOf, ItemPriceOf, NextCollectionIdOf, NftsOf,
NftsWeightInfoOf,
AccountIdOf, AttributeNamespaceOf, BalanceOf, CollectionConfigFor, CollectionDetailsFor,
CollectionIdOf, ItemIdOf, ItemPriceOf, NextCollectionIdOf, NftsOf, NftsWeightInfoOf,
};

use super::*;
Expand Down Expand Up @@ -219,20 +218,10 @@ pub mod pallet {
origin: OriginFor<T>,
id: CollectionIdOf<T>,
admin: AccountIdOf<T>,
config: CreateCollectionConfigFor<T>,
config: CollectionConfigFor<T>,
) -> DispatchResult {
let creator = ensure_signed(origin.clone())?;
let collection_config = CollectionConfig {
settings: CollectionSettings::all_enabled(),
max_supply: config.max_supply,
mint_settings: MintSettings {
mint_type: config.mint_type,
start_block: config.start_block,
end_block: config.end_block,
..MintSettings::default()
},
};
NftsOf::<T>::create(origin, T::Lookup::unlookup(admin.clone()), collection_config)?;
NftsOf::<T>::create(origin, T::Lookup::unlookup(admin.clone()), config)?;
Self::deposit_event(Event::Created { id, admin, creator });
Ok(())
}
Expand Down
10 changes: 4 additions & 6 deletions pallets/api/src/nonfungibles/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use sp_runtime::{BoundedVec, DispatchError::BadOrigin};
use super::types::{CollectionIdOf, ItemIdOf};
use crate::{
mock::*,
nonfungibles::{CreateCollectionConfig, Event, Read::*, ReadResult},
nonfungibles::{Event, Read::*, ReadResult},
Read,
};

Expand Down Expand Up @@ -446,12 +446,10 @@ fn create_works() {
signed(owner),
next_collection_id,
owner,
CreateCollectionConfig {
CollectionConfig {
max_supply: None,
mint_type: pallet_nfts::MintType::Public,
price: None,
start_block: None,
end_block: None,
mint_settings: MintSettings::default(),
settings: CollectionSettings::all_enabled()
},
));
assert_eq!(Nfts::collection_owner(next_collection_id), Some(owner));
Expand Down
17 changes: 4 additions & 13 deletions pallets/api/src/nonfungibles/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::traits::{nonfungibles_v2::Inspect, Currency};
use frame_system::pallet_prelude::BlockNumberFor;
pub use pallet_nfts::{
AttributeNamespace, CollectionDetails, DestroyWitness, ItemDeposit, ItemDetails, MintType,
MintWitness,
AttributeNamespace, CollectionConfig, CollectionDetails, CollectionSetting, CollectionSettings,
DestroyWitness, ItemDeposit, ItemDetails, ItemSetting, MintSettings, MintType, MintWitness,
};
use scale_info::TypeInfo;

Check warning on line 8 in pallets/api/src/nonfungibles/types.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `scale_info::TypeInfo`

warning: unused import: `scale_info::TypeInfo` --> pallets/api/src/nonfungibles/types.rs:8:5 | 8 | use scale_info::TypeInfo; | ^^^^^^^^^^^^^^^^^^^^
use sp_runtime::RuntimeDebug;

Check warning on line 9 in pallets/api/src/nonfungibles/types.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `sp_runtime::RuntimeDebug`

warning: unused import: `sp_runtime::RuntimeDebug` --> pallets/api/src/nonfungibles/types.rs:9:5 | 9 | use sp_runtime::RuntimeDebug; | ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -26,14 +26,5 @@ pub(super) type ItemPriceOf<T, I = ()> = BalanceOf<T, I>;
pub(super) type CollectionDetailsFor<T, I = ()> =
CollectionDetails<AccountIdOf<T>, BalanceOf<T, I>>;
pub(super) type AttributeNamespaceOf<T> = AttributeNamespace<AccountIdOf<T>>;
pub(super) type CreateCollectionConfigFor<T, I = ()> =
CreateCollectionConfig<ItemPriceOf<T, I>, BlockNumberFor<T>, CollectionIdOf<T>>;

#[derive(Clone, Copy, Decode, Encode, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub struct CreateCollectionConfig<Price, BlockNumber, CollectionId> {
pub max_supply: Option<u32>,
pub mint_type: MintType<CollectionId>,
pub price: Option<Price>,
pub start_block: Option<BlockNumber>,
pub end_block: Option<BlockNumber>,
}
pub(super) type CollectionConfigFor<T, I = ()> =
CollectionConfig<ItemPriceOf<T, I>, BlockNumberFor<T>, CollectionIdOf<T>>;
2 changes: 2 additions & 0 deletions pop-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ name = "pop-api"
version = "0.0.0"

[dependencies]
bitflags = { version = "1.3.2" }
enumflags2 = "0.7.9"
ink = { git = "https://github.com/r0gue-io/ink", branch = "sub0", default-features = false }
polkavm-derive = "0.11.0"
pop-primitives = { path = "../primitives", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions pop-api/integration-tests/contracts/nonfungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use pop_api::{
nonfungibles::{
self as api,
events::{Approval, AttributeSet, Transfer},
AttributeNamespace, CancelAttributesApprovalWitness, CollectionDetails, CollectionId,
CreateCollectionConfig, DestroyWitness, ItemId, MintWitness,
AttributeNamespace, CancelAttributesApprovalWitness, CollectionConfig, CollectionDetails,
CollectionId, DestroyWitness, ItemId, MintWitness,
},
StatusCode,
};
Expand Down Expand Up @@ -136,7 +136,7 @@ mod nonfungibles {
&mut self,
id: CollectionId,
admin: AccountId,
config: CreateCollectionConfig,
config: CollectionConfig,
) -> Result<()> {
api::create(id, admin, config)
}
Expand Down
9 changes: 3 additions & 6 deletions pop-api/integration-tests/src/nonfungibles/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use frame_support::BoundedVec;
use pallet_nfts::CollectionConfig;
use pop_api::{
nonfungibles::{
events::{Approval, AttributeSet, Transfer},
Expand Down Expand Up @@ -352,12 +351,10 @@ fn create_works() {
&addr.clone(),
COLLECTION_ID,
account_id.clone(),
CreateCollectionConfig {
CollectionConfig {
max_supply: Some(100),
mint_type: MintType::Public,
price: None,
start_block: None,
end_block: None,
mint_settings: MintSettings::default(),
settings: CollectionSettings::all_enabled(),
}
));
assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions pop-api/integration-tests/src/nonfungibles/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub(super) fn create(
addr: &H160,
id: CollectionId,
admin: AccountId32,
config: CreateCollectionConfig,
config: CollectionConfig,
) -> Result<(), Error> {
let params = [id.encode(), admin.encode(), config.encode()].concat();
let result = do_bare_call("create", &addr, params);
Expand Down Expand Up @@ -329,8 +329,8 @@ pub(super) mod nfts {
}

pub(super) fn collection_config_with_all_settings_enabled(
) -> CollectionConfig<u128, BlockNumber, CollectionId> {
CollectionConfig {
) -> pallet_nfts::CollectionConfig<u128, BlockNumber, CollectionId> {
pallet_nfts::CollectionConfig {
settings: pallet_nfts::CollectionSettings::all_enabled(),
max_supply: None,
mint_settings: pallet_nfts::MintSettings::default(),
Expand Down
2 changes: 2 additions & 0 deletions pop-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use constants::DECODING_FAILED;
use ink::env::chain_extension::{ChainExtensionMethod, FromStatusCode};
pub use v0::*;

/// Module providing macros.
pub mod macros;
/// Module providing primitives types.
pub mod primitives;
/// The first version of the API.
Expand Down
53 changes: 53 additions & 0 deletions pop-api/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/// Implements encoding and decoding traits for a wrapper type that represents
/// bitflags. The wrapper type should contain a field of type `$size`, where
/// `$size` is an integer type (e.g., u8, u16, u32) that can represent the bitflags.
/// The `$bitflag_enum` type is the enumeration type that defines the individual bitflags.
///
/// This macro provides implementations for the following traits:
/// - `MaxEncodedLen`: Calculates the maximum encoded length for the wrapper type.
/// - `Encode`: Encodes the wrapper type using the provided encoding function.
/// - `EncodeLike`: Trait indicating the type can be encoded as is.
/// - `Decode`: Decodes the wrapper type from the input.
/// - `TypeInfo`: Provides type information for the wrapper type.
macro_rules! impl_codec_bitflags {
($wrapper:ty, $size:ty, $bitflag_enum:ty) => {
impl ink::scale::MaxEncodedLen for $wrapper {
fn max_encoded_len() -> usize {
<$size>::max_encoded_len()
}
}
impl ink::scale::Encode for $wrapper {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.bits().using_encoded(f)
}
}
impl ink::scale::EncodeLike for $wrapper {}
impl ink::scale::Decode for $wrapper {
fn decode<I: ink::scale::Input>(
input: &mut I,
) -> ::core::result::Result<Self, ink::scale::Error> {
let field = <$size>::decode(input)?;
Ok(Self(BitFlags::from_bits(field as $size).map_err(|_| "invalid value")?))
}
}

#[cfg(feature = "std")]
impl ink::scale_info::TypeInfo for $wrapper {
type Identity = Self;

fn type_info() -> ink::scale_info::Type {
ink::scale_info::Type::builder()
.path(ink::scale_info::Path::new("BitFlags", module_path!()))
.type_params(vec![ink::scale_info::TypeParameter::new(
"T",
Some(ink::scale_info::meta_type::<$bitflag_enum>()),
)])
.composite(
ink::scale_info::build::Fields::unnamed()
.field(|f| f.ty::<$size>().type_name(stringify!($bitflag_enum))),
)
}
}
};
}
pub(crate) use impl_codec_bitflags;
13 changes: 9 additions & 4 deletions pop-api/src/v0/fungibles/errors.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! A set of errors for use in smart contracts that interact with the fungibles api. This includes errors compliant to standards.
//! A set of errors for use in smart contracts that interact with the fungibles api. This includes
//! errors compliant to standards.
use super::*;
use ink::prelude::string::{String, ToString};

use super::*;

/// Represents various errors related to fungible tokens.
///
/// The `FungiblesError` provides a detailed and specific set of error types that can occur when
Expand Down Expand Up @@ -103,6 +105,11 @@ impl From<StatusCode> for PSP22Error {

#[cfg(test)]
mod tests {
use ink::{
prelude::string::String,
scale::{Decode, Encode},
};

use super::{FungiblesError, PSP22Error};
use crate::{
constants::{ASSETS, BALANCES},
Expand All @@ -114,8 +121,6 @@ mod tests {
},
StatusCode,
};
use ink::prelude::string::String;
use ink::scale::{Decode, Encode};

fn error_into_status_code(error: Error) -> StatusCode {
let mut encoded_error = error.encode();
Expand Down
4 changes: 3 additions & 1 deletion pop-api/src/v0/fungibles/traits.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Traits that can be used by contracts. Including standard compliant traits.
use super::*;
use core::result::Result;

use ink::prelude::string::String;

use super::*;

/// The PSP22 trait.
#[ink::trait_definition]
pub trait Psp22 {
Expand Down
4 changes: 2 additions & 2 deletions pop-api/src/v0/nonfungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ pub fn get_attribute(
}

#[inline]
pub fn create(id: CollectionId, admin: AccountId, config: CreateCollectionConfig) -> Result<()> {
pub fn create(id: CollectionId, admin: AccountId, config: CollectionConfig) -> Result<()> {
build_dispatch(CREATE)
.input::<(CollectionId, AccountId, CreateCollectionConfig)>()
.input::<(CollectionId, AccountId, CollectionConfig)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
.call(&(id, admin, config))?;
Expand Down
Loading

0 comments on commit 45ac5ca

Please sign in to comment.