1234567891011121314151617#![cfg_attr(not(feature = "std"), no_std)]1819use core::{20 convert::{TryFrom, TryInto},21 fmt,22};23use frame_support::{24 storage::{bounded_btree_map::BoundedBTreeMap, bounded_btree_set::BoundedBTreeSet},25};2627#[cfg(feature = "serde")]28use serde::{Serialize, Deserialize};2930use sp_core::U256;31use sp_runtime::{ArithmeticError, sp_std::prelude::Vec};32use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};33use frame_support::{BoundedVec, traits::ConstU32};34use derivative::Derivative;35use scale_info::TypeInfo;3637mod bounded;38pub mod budget;39pub mod mapping;40mod migration;4142pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;43pub const MAX_REFUNGIBLE_PIECES: u128 = 1_000_000_000_000_000_000_000;44pub const MAX_SPONSOR_TIMEOUT: u32 = 10_368_000;4546pub const MAX_TOKEN_OWNERSHIP: u32 = if cfg!(not(feature = "limit-testing")) {47 100_00048} else {49 1050};51pub const COLLECTION_NUMBER_LIMIT: u32 = if cfg!(not(feature = "limit-testing")) {52 100_00053} else {54 1055};56pub const CUSTOM_DATA_LIMIT: u32 = if cfg!(not(feature = "limit-testing")) {57 204858} else {59 1060};61pub const COLLECTION_ADMINS_LIMIT: u32 = 5;62pub const COLLECTION_TOKEN_LIMIT: u32 = u32::MAX;63pub const ACCOUNT_TOKEN_OWNERSHIP_LIMIT: u32 = if cfg!(not(feature = "limit-testing")) {64 1_000_00065} else {66 1067};686970pub const NFT_SPONSOR_TRANSFER_TIMEOUT: u32 = 5;71pub const FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT: u32 = 5;72pub const REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT: u32 = 5;7374pub const SPONSOR_APPROVE_TIMEOUT: u32 = 5;757677pub const OFFCHAIN_SCHEMA_LIMIT: u32 = 8192;78pub const VARIABLE_ON_CHAIN_SCHEMA_LIMIT: u32 = 8192;79pub const CONST_ON_CHAIN_SCHEMA_LIMIT: u32 = 32768;8081pub const MAX_COLLECTION_NAME_LENGTH: u32 = 64;82pub const MAX_COLLECTION_DESCRIPTION_LENGTH: u32 = 256;83pub const MAX_TOKEN_PREFIX_LENGTH: u32 = 16;84858687pub const MAX_ITEMS_PER_BATCH: u32 = 200;8889pub type CustomDataLimit = ConstU32<CUSTOM_DATA_LIMIT>;9091#[derive(92 Encode,93 Decode,94 PartialEq,95 Eq,96 PartialOrd,97 Ord,98 Clone,99 Copy,100 Debug,101 Default,102 TypeInfo,103 MaxEncodedLen,104)]105#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]106pub struct CollectionId(pub u32);107impl EncodeLike<u32> for CollectionId {}108impl EncodeLike<CollectionId> for u32 {}109110#[derive(111 Encode,112 Decode,113 PartialEq,114 Eq,115 PartialOrd,116 Ord,117 Clone,118 Copy,119 Debug,120 Default,121 TypeInfo,122 MaxEncodedLen,123)]124#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]125pub struct TokenId(pub u32);126impl EncodeLike<u32> for TokenId {}127impl EncodeLike<TokenId> for u32 {}128129impl TokenId {130 pub fn try_next(self) -> Result<TokenId, ArithmeticError> {131 self.0132 .checked_add(1)133 .ok_or(ArithmeticError::Overflow)134 .map(Self)135 }136}137138impl From<TokenId> for U256 {139 fn from(t: TokenId) -> Self {140 t.0.into()141 }142}143144impl TryFrom<U256> for TokenId {145 type Error = &'static str;146147 fn try_from(value: U256) -> Result<Self, Self::Error> {148 Ok(TokenId(value.try_into().map_err(|_| "too large token id")?))149 }150}151152pub struct OverflowError;153impl From<OverflowError> for &'static str {154 fn from(_: OverflowError) -> Self {155 "overflow occured"156 }157}158159pub type DecimalPoints = u8;160161#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]162#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]163pub enum CollectionMode {164 NFT,165 166 Fungible(DecimalPoints),167 ReFungible,168}169170impl CollectionMode {171 pub fn id(&self) -> u8 {172 match self {173 CollectionMode::NFT => 1,174 CollectionMode::Fungible(_) => 2,175 CollectionMode::ReFungible => 3,176 }177 }178}179180pub trait SponsoringResolve<AccountId, Call> {181 fn resolve(who: &AccountId, call: &Call) -> Option<AccountId>;182}183184#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]185#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]186pub enum AccessMode {187 Normal,188 AllowList,189}190impl Default for AccessMode {191 fn default() -> Self {192 Self::Normal193 }194}195196#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]197#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]198pub enum SchemaVersion {199 ImageURL,200 Unique,201}202impl Default for SchemaVersion {203 fn default() -> Self {204 Self::ImageURL205 }206}207208#[derive(Encode, Decode, Default, Debug, Clone, PartialEq, TypeInfo)]209#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]210pub struct Ownership<AccountId> {211 pub owner: AccountId,212 pub fraction: u128,213}214215#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]216#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]217pub enum SponsorshipState<AccountId> {218 219 Disabled,220 Unconfirmed(AccountId),221 222 Confirmed(AccountId),223}224225impl<AccountId> SponsorshipState<AccountId> {226 pub fn sponsor(&self) -> Option<&AccountId> {227 match self {228 Self::Confirmed(sponsor) => Some(sponsor),229 _ => None,230 }231 }232233 pub fn pending_sponsor(&self) -> Option<&AccountId> {234 match self {235 Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor),236 _ => None,237 }238 }239240 pub fn confirmed(&self) -> bool {241 matches!(self, Self::Confirmed(_))242 }243}244245impl<T> Default for SponsorshipState<T> {246 fn default() -> Self {247 Self::Disabled248 }249}250251#[struct_versioning::versioned(version = 2, upper)]252#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]253#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]254pub struct Collection<AccountId> {255 pub owner: AccountId,256 pub mode: CollectionMode,257 pub access: AccessMode,258 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]259 pub name: BoundedVec<u16, ConstU32<MAX_COLLECTION_NAME_LENGTH>>,260 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]261 pub description: BoundedVec<u16, ConstU32<MAX_COLLECTION_DESCRIPTION_LENGTH>>,262 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]263 pub token_prefix: BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>,264 pub mint_mode: bool,265 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]266 pub offchain_schema: BoundedVec<u8, ConstU32<OFFCHAIN_SCHEMA_LIMIT>>,267 pub schema_version: SchemaVersion,268 pub sponsorship: SponsorshipState<AccountId>,269270 #[version(..2)]271 pub limits: CollectionLimitsVersion1, 272 #[version(2.., upper(limits.into()))]273 pub limits: CollectionLimitsVersion2,274275 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]276 pub variable_on_chain_schema: BoundedVec<u8, ConstU32<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>>,277 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]278 pub const_on_chain_schema: BoundedVec<u8, ConstU32<CONST_ON_CHAIN_SCHEMA_LIMIT>>,279 pub meta_update_permission: MetaUpdatePermission,280}281282#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, Debug, Derivative, MaxEncodedLen)]283#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]284#[derivative(Default(bound = ""))]285pub struct CreateCollectionData<AccountId> {286 #[derivative(Default(value = "CollectionMode::NFT"))]287 pub mode: CollectionMode,288 pub access: Option<AccessMode>,289 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]290 pub name: BoundedVec<u16, ConstU32<MAX_COLLECTION_NAME_LENGTH>>,291 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]292 pub description: BoundedVec<u16, ConstU32<MAX_COLLECTION_DESCRIPTION_LENGTH>>,293 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]294 pub token_prefix: BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>,295 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]296 pub offchain_schema: BoundedVec<u8, ConstU32<OFFCHAIN_SCHEMA_LIMIT>>,297 pub schema_version: Option<SchemaVersion>,298 pub pending_sponsor: Option<AccountId>,299 pub limits: Option<CollectionLimits>,300 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]301 pub variable_on_chain_schema: BoundedVec<u8, ConstU32<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>>,302 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]303 pub const_on_chain_schema: BoundedVec<u8, ConstU32<CONST_ON_CHAIN_SCHEMA_LIMIT>>,304 pub meta_update_permission: Option<MetaUpdatePermission>,305}306307#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo)]308#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]309pub struct NftItemType<AccountId> {310 pub owner: AccountId,311 pub const_data: Vec<u8>,312 pub variable_data: Vec<u8>,313}314315#[derive(Encode, Decode, Default, Debug, Clone, PartialEq, TypeInfo)]316#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]317pub struct FungibleItemType {318 pub value: u128,319}320321#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo)]322#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]323pub struct ReFungibleItemType<AccountId> {324 pub owner: Vec<Ownership<AccountId>>,325 pub const_data: Vec<u8>,326 pub variable_data: Vec<u8>,327}328329330#[struct_versioning::versioned(version = 2, upper)]331#[derive(Encode, Decode, Debug, Default, Clone, PartialEq, TypeInfo, MaxEncodedLen)]332#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]333pub struct CollectionLimits {334 pub account_token_ownership_limit: Option<u32>,335 pub sponsored_data_size: Option<u32>,336 337 338 339 pub sponsored_data_rate_limit: Option<SponsoringRateLimit>,340 pub token_limit: Option<u32>,341342 343 pub sponsor_transfer_timeout: Option<u32>,344 pub sponsor_approve_timeout: Option<u32>,345 pub owner_can_transfer: Option<bool>,346 pub owner_can_destroy: Option<bool>,347 pub transfers_enabled: Option<bool>,348349 #[version(2.., upper(None))]350 pub nesting_rule: Option<NestingRule>,351}352353impl CollectionLimits {354 pub fn account_token_ownership_limit(&self) -> u32 {355 self.account_token_ownership_limit356 .unwrap_or(ACCOUNT_TOKEN_OWNERSHIP_LIMIT)357 .min(MAX_TOKEN_OWNERSHIP)358 }359 pub fn sponsored_data_size(&self) -> u32 {360 self.sponsored_data_size361 .unwrap_or(CUSTOM_DATA_LIMIT)362 .min(CUSTOM_DATA_LIMIT)363 }364 pub fn token_limit(&self) -> u32 {365 self.token_limit366 .unwrap_or(COLLECTION_TOKEN_LIMIT)367 .min(COLLECTION_TOKEN_LIMIT)368 }369 pub fn sponsor_transfer_timeout(&self, default: u32) -> u32 {370 self.sponsor_transfer_timeout371 .unwrap_or(default)372 .min(MAX_SPONSOR_TIMEOUT)373 }374 pub fn sponsor_approve_timeout(&self) -> u32 {375 self.sponsor_approve_timeout376 .unwrap_or(SPONSOR_APPROVE_TIMEOUT)377 .min(MAX_SPONSOR_TIMEOUT)378 }379 pub fn owner_can_transfer(&self) -> bool {380 self.owner_can_transfer.unwrap_or(true)381 }382 pub fn owner_can_destroy(&self) -> bool {383 self.owner_can_destroy.unwrap_or(true)384 }385 pub fn transfers_enabled(&self) -> bool {386 self.transfers_enabled.unwrap_or(true)387 }388 pub fn sponsored_data_rate_limit(&self) -> Option<u32> {389 match self390 .sponsored_data_rate_limit391 .unwrap_or(SponsoringRateLimit::SponsoringDisabled)392 {393 SponsoringRateLimit::SponsoringDisabled => None,394 SponsoringRateLimit::Blocks(v) => Some(v.min(MAX_SPONSOR_TIMEOUT)),395 }396 }397 pub fn nesting_rule(&self) -> &NestingRule {398 static DEFAULT: NestingRule = NestingRule::Owner;399 self.nesting_rule.as_ref().unwrap_or(&DEFAULT)400 }401}402403#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen, Derivative)]404#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]405#[derivative(Debug)]406pub enum NestingRule {407 408 Disabled,409 410 Owner,411 412 OwnerRestricted(413 #[cfg_attr(feature = "serde1", serde(with = "bounded::set_serde"))]414 #[derivative(Debug(format_with = "bounded::set_debug"))]415 BoundedBTreeSet<CollectionId, ConstU32<16>>,416 ),417}418419#[derive(Encode, Decode, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]420#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]421pub enum SponsoringRateLimit {422 SponsoringDisabled,423 Blocks(u32),424}425426#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]427#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]428#[derivative(Debug)]429pub struct CreateNftData {430 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]431 #[derivative(Debug(format_with = "bounded::vec_debug"))]432 pub const_data: BoundedVec<u8, CustomDataLimit>,433 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]434 #[derivative(Debug(format_with = "bounded::vec_debug"))]435 pub variable_data: BoundedVec<u8, CustomDataLimit>,436}437438#[derive(Encode, Decode, MaxEncodedLen, Default, Debug, Clone, PartialEq, TypeInfo)]439#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]440pub struct CreateFungibleData {441 pub value: u128,442}443444#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]445#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]446#[derivative(Debug)]447pub struct CreateReFungibleData {448 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]449 #[derivative(Debug(format_with = "bounded::vec_debug"))]450 pub const_data: BoundedVec<u8, CustomDataLimit>,451 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]452 #[derivative(Debug(format_with = "bounded::vec_debug"))]453 pub variable_data: BoundedVec<u8, CustomDataLimit>,454 pub pieces: u128,455}456457#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]458#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]459pub enum MetaUpdatePermission {460 ItemOwner,461 Admin,462 None,463}464465impl Default for MetaUpdatePermission {466 fn default() -> Self {467 Self::ItemOwner468 }469}470471#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]472#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]473pub enum CreateItemData {474 NFT(CreateNftData),475 Fungible(CreateFungibleData),476 ReFungible(CreateReFungibleData),477}478479#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]480#[derivative(Debug)]481pub struct CreateNftExData<CrossAccountId> {482 #[derivative(Debug(format_with = "bounded::vec_debug"))]483 pub const_data: BoundedVec<u8, CustomDataLimit>,484 #[derivative(Debug(format_with = "bounded::vec_debug"))]485 pub variable_data: BoundedVec<u8, CustomDataLimit>,486 pub owner: CrossAccountId,487}488489#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]490#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]491pub struct CreateRefungibleExData<CrossAccountId> {492 #[derivative(Debug(format_with = "bounded::vec_debug"))]493 pub const_data: BoundedVec<u8, CustomDataLimit>,494 #[derivative(Debug(format_with = "bounded::vec_debug"))]495 pub variable_data: BoundedVec<u8, CustomDataLimit>,496 #[derivative(Debug(format_with = "bounded::map_debug"))]497 pub users: BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,498}499500#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]501#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]502pub enum CreateItemExData<CrossAccountId> {503 NFT(504 #[derivative(Debug(format_with = "bounded::vec_debug"))]505 BoundedVec<CreateNftExData<CrossAccountId>, ConstU32<MAX_ITEMS_PER_BATCH>>,506 ),507 Fungible(508 #[derivative(Debug(format_with = "bounded::map_debug"))]509 BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,510 ),511 512 RefungibleMultipleItems(513 #[derivative(Debug(format_with = "bounded::vec_debug"))]514 BoundedVec<CreateRefungibleExData<CrossAccountId>, ConstU32<MAX_ITEMS_PER_BATCH>>,515 ),516 517 RefungibleMultipleOwners(CreateRefungibleExData<CrossAccountId>),518}519520impl CreateItemData {521 pub fn data_size(&self) -> usize {522 match self {523 CreateItemData::NFT(data) => data.variable_data.len() + data.const_data.len(),524 CreateItemData::ReFungible(data) => data.variable_data.len() + data.const_data.len(),525 _ => 0,526 }527 }528}529530impl From<CreateNftData> for CreateItemData {531 fn from(item: CreateNftData) -> Self {532 CreateItemData::NFT(item)533 }534}535536impl From<CreateReFungibleData> for CreateItemData {537 fn from(item: CreateReFungibleData) -> Self {538 CreateItemData::ReFungible(item)539 }540}541542impl From<CreateFungibleData> for CreateItemData {543 fn from(item: CreateFungibleData) -> Self {544 CreateItemData::Fungible(item)545 }546}547548#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]549#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]550pub struct CollectionStats {551 pub created: u32,552 pub destroyed: u32,553 pub alive: u32,554}