123456789101112131415161718192021#![cfg_attr(not(feature = "std"), no_std)]2223use core::{24 convert::{TryFrom, TryInto},25 fmt,26};27use frame_support::{28 storage::{bounded_btree_map::BoundedBTreeMap, bounded_btree_set::BoundedBTreeSet},29 traits::Get,30 parameter_types,31};3233#[cfg(feature = "serde")]34use serde::{Serialize, Deserialize};3536use sp_core::U256;37use sp_runtime::{ArithmeticError, sp_std::prelude::Vec, Permill};38use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};39use bondrewd::Bitfields;40use frame_support::{BoundedVec, traits::ConstU32};41use derivative::Derivative;42use scale_info::TypeInfo;434445use rmrk_traits::{46 CollectionInfo, NftInfo, ResourceInfo, PropertyInfo, BaseInfo, PartType, Theme, ThemeProperty,47 ResourceTypes, BasicResource, ComposableResource, SlotResource, EquippableList,48};49pub use rmrk_traits::{50 primitives::{51 CollectionId as RmrkCollectionId, NftId as RmrkNftId, BaseId as RmrkBaseId,52 SlotId as RmrkSlotId, PartId as RmrkPartId, ResourceId as RmrkResourceId,53 },54 NftChild as RmrkNftChild, AccountIdOrCollectionNftTuple as RmrkAccountIdOrCollectionNftTuple,55 FixedPart as RmrkFixedPart, SlotPart as RmrkSlotPart,56};5758mod bondrewd_codec;59mod bounded;60pub mod budget;61pub mod mapping;62mod migration;636465pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;666768pub const MAX_REFUNGIBLE_PIECES: u128 = 1_000_000_000_000_000_000_000;69pub const MAX_SPONSOR_TIMEOUT: u32 = 10_368_000;707172pub const MAX_TOKEN_OWNERSHIP: u32 = if cfg!(not(feature = "limit-testing")) {73 100_00074} else {75 1076};777879pub const COLLECTION_NUMBER_LIMIT: u32 = if cfg!(not(feature = "limit-testing")) {80 100_00081} else {82 1083};848586pub const CUSTOM_DATA_LIMIT: u32 = if cfg!(not(feature = "limit-testing")) {87 204888} else {89 1090};919293pub const COLLECTION_ADMINS_LIMIT: u32 = 5;949596pub const COLLECTION_TOKEN_LIMIT: u32 = u32::MAX;979899pub const ACCOUNT_TOKEN_OWNERSHIP_LIMIT: u32 = if cfg!(not(feature = "limit-testing")) {100 1_000_000101} else {102 10103};104105106pub const NFT_SPONSOR_TRANSFER_TIMEOUT: u32 = 5;107108pub const FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT: u32 = 5;109110pub const REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT: u32 = 5;111112113pub const SPONSOR_APPROVE_TIMEOUT: u32 = 5;114115116pub const OFFCHAIN_SCHEMA_LIMIT: u32 = 8192;117pub const VARIABLE_ON_CHAIN_SCHEMA_LIMIT: u32 = 8192;118pub const CONST_ON_CHAIN_SCHEMA_LIMIT: u32 = 32768;119120121pub const COLLECTION_FIELD_LIMIT: u32 = CONST_ON_CHAIN_SCHEMA_LIMIT;122123124pub const MAX_COLLECTION_NAME_LENGTH: u32 = 64;125126127pub const MAX_COLLECTION_DESCRIPTION_LENGTH: u32 = 256;128129130pub const MAX_TOKEN_PREFIX_LENGTH: u32 = 16;131132133pub const MAX_PROPERTY_KEY_LENGTH: u32 = 256;134135136pub const MAX_PROPERTY_VALUE_LENGTH: u32 = 32768;137138139pub const MAX_PROPERTIES_PER_ITEM: u32 = 64;140141142pub const MAX_AUX_PROPERTY_VALUE_LENGTH: u32 = 2048;143144145pub const MAX_COLLECTION_PROPERTIES_SIZE: u32 = 40960;146147148pub const MAX_TOKEN_PROPERTIES_SIZE: u32 = 32768;149150151152pub const MAX_ITEMS_PER_BATCH: u32 = 200;153154155pub type CustomDataLimit = ConstU32<CUSTOM_DATA_LIMIT>;156157158#[derive(159 Encode,160 Decode,161 PartialEq,162 Eq,163 PartialOrd,164 Ord,165 Clone,166 Copy,167 Debug,168 Default,169 TypeInfo,170 MaxEncodedLen,171)]172#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]173pub struct CollectionId(pub u32);174impl EncodeLike<u32> for CollectionId {}175impl EncodeLike<CollectionId> for u32 {}176177178#[derive(179 Encode,180 Decode,181 PartialEq,182 Eq,183 PartialOrd,184 Ord,185 Clone,186 Copy,187 Debug,188 Default,189 TypeInfo,190 MaxEncodedLen,191)]192#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]193pub struct TokenId(pub u32);194impl EncodeLike<u32> for TokenId {}195impl EncodeLike<TokenId> for u32 {}196197impl TokenId {198 199 200 201 pub fn try_next(self) -> Result<TokenId, ArithmeticError> {202 self.0203 .checked_add(1)204 .ok_or(ArithmeticError::Overflow)205 .map(Self)206 }207}208209impl From<TokenId> for U256 {210 fn from(t: TokenId) -> Self {211 t.0.into()212 }213}214215impl TryFrom<U256> for TokenId {216 type Error = &'static str;217218 fn try_from(value: U256) -> Result<Self, Self::Error> {219 Ok(TokenId(value.try_into().map_err(|_| "too large token id")?))220 }221}222223224#[struct_versioning::versioned(version = 2, upper)]225#[derive(Encode, Decode, Clone, PartialEq, TypeInfo)]226#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]227pub struct TokenData<CrossAccountId> {228 229 pub properties: Vec<Property>,230231 232 pub owner: Option<CrossAccountId>,233234 235 #[version(2.., upper(0))]236 pub pieces: u128,237}238239240pub struct OverflowError;241impl From<OverflowError> for &'static str {242 fn from(_: OverflowError) -> Self {243 "overflow occured"244 }245}246247248pub type DecimalPoints = u8;249250251252253254255#[derive(Encode, Decode, Eq, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]256#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]257pub enum CollectionMode {258 259 NFT,260 261 Fungible(DecimalPoints),262 263 ReFungible,264}265266impl CollectionMode {267 268 pub fn id(&self) -> u8 {269 match self {270 CollectionMode::NFT => 1,271 CollectionMode::Fungible(_) => 2,272 CollectionMode::ReFungible => 3,273 }274 }275}276277278pub trait SponsoringResolve<AccountId, Call> {279 fn resolve(who: &AccountId, call: &Call) -> Option<AccountId>;280}281282283#[derive(Encode, Decode, Eq, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]284#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]285pub enum AccessMode {286 287 Normal,288 289 AllowList,290}291impl Default for AccessMode {292 fn default() -> Self {293 Self::Normal294 }295}296297298#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]299#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]300pub enum SchemaVersion {301 ImageURL,302 Unique,303}304impl Default for SchemaVersion {305 fn default() -> Self {306 Self::ImageURL307 }308}309310311#[derive(Encode, Decode, Default, Debug, Clone, PartialEq, TypeInfo)]312#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]313pub struct Ownership<AccountId> {314 pub owner: AccountId,315 pub fraction: u128,316}317318319#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]320#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]321pub enum SponsorshipState<AccountId> {322 323 Disabled,324 325 326 Unconfirmed(AccountId),327 328 Confirmed(AccountId),329}330331impl<AccountId> SponsorshipState<AccountId> {332 333 pub fn sponsor(&self) -> Option<&AccountId> {334 match self {335 Self::Confirmed(sponsor) => Some(sponsor),336 _ => None,337 }338 }339340 341 pub fn pending_sponsor(&self) -> Option<&AccountId> {342 match self {343 Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor),344 _ => None,345 }346 }347348 349 pub fn confirmed(&self) -> bool {350 matches!(self, Self::Confirmed(_))351 }352}353354impl<T> Default for SponsorshipState<T> {355 fn default() -> Self {356 Self::Disabled357 }358}359360pub type CollectionName = BoundedVec<u16, ConstU32<MAX_COLLECTION_NAME_LENGTH>>;361pub type CollectionDescription = BoundedVec<u16, ConstU32<MAX_COLLECTION_DESCRIPTION_LENGTH>>;362pub type CollectionTokenPrefix = BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>;363364#[derive(Bitfields, Clone, Copy, PartialEq, Eq, Debug, Default)]365#[bondrewd(enforce_bytes = 1)]366pub struct CollectionFlags {367 368 #[bondrewd(bits = "0..1")]369 pub foreign: bool,370 371 #[bondrewd(bits = "1..2")]372 pub erc721metadata: bool,373 374 #[bondrewd(bits = "7..8")]375 pub external: bool,376377 #[bondrewd(reserve, bits = "2..7")]378 pub reserved: u8,379}380bondrewd_codec!(CollectionFlags);381382383384385386387388#[struct_versioning::versioned(version = 2, upper)]389#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]390pub struct Collection<AccountId> {391 392 pub owner: AccountId,393394 395 pub mode: CollectionMode,396397 398 #[version(..2)]399 pub access: AccessMode,400401 402 pub name: CollectionName,403404 405 pub description: CollectionDescription,406407 408 pub token_prefix: CollectionTokenPrefix,409410 #[version(..2)]411 pub mint_mode: bool,412413 #[version(..2)]414 pub offchain_schema: BoundedVec<u8, ConstU32<OFFCHAIN_SCHEMA_LIMIT>>,415416 #[version(..2)]417 pub schema_version: SchemaVersion,418419 420 pub sponsorship: SponsorshipState<AccountId>,421422 423 pub limits: CollectionLimits,424425 426 #[version(2.., upper(Default::default()))]427 pub permissions: CollectionPermissions,428429 #[version(2.., upper(Default::default()))]430 pub flags: CollectionFlags,431432 #[version(..2)]433 pub variable_on_chain_schema: BoundedVec<u8, ConstU32<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>>,434435 #[version(..2)]436 pub const_on_chain_schema: BoundedVec<u8, ConstU32<CONST_ON_CHAIN_SCHEMA_LIMIT>>,437438 #[version(..2)]439 pub meta_update_permission: MetaUpdatePermission,440}441442#[derive(Encode, Decode, Clone, PartialEq, TypeInfo)]443#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]444pub struct RpcCollectionFlags {445 446 pub foreign: bool,447 448 pub erc721metadata: bool,449}450451452#[struct_versioning::versioned(version = 2, upper)]453#[derive(Encode, Decode, Clone, PartialEq, TypeInfo)]454#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]455pub struct RpcCollection<AccountId> {456 457 pub owner: AccountId,458459 460 pub mode: CollectionMode,461462 463 pub name: Vec<u16>,464465 466 pub description: Vec<u16>,467468 469 pub token_prefix: Vec<u8>,470471 472 pub sponsorship: SponsorshipState<AccountId>,473474 475 pub limits: CollectionLimits,476477 478 pub permissions: CollectionPermissions,479480 481 pub token_property_permissions: Vec<PropertyKeyPermission>,482483 484 pub properties: Vec<Property>,485486 487 pub read_only: bool,488489 490 #[version(2.., upper(RpcCollectionFlags {foreign: false, erc721metadata: false}))]491 pub flags: RpcCollectionFlags,492}493494495496497#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, Derivative, MaxEncodedLen)]498#[derivative(Debug, Default(bound = ""))]499pub struct CreateCollectionData<AccountId> {500 501 #[derivative(Default(value = "CollectionMode::NFT"))]502 pub mode: CollectionMode,503504 505 pub access: Option<AccessMode>,506507 508 pub name: CollectionName,509510 511 pub description: CollectionDescription,512513 514 pub token_prefix: CollectionTokenPrefix,515516 517 pub pending_sponsor: Option<AccountId>,518519 520 pub limits: Option<CollectionLimits>,521522 523 pub permissions: Option<CollectionPermissions>,524525 526 pub token_property_permissions: CollectionPropertiesPermissionsVec,527528 529 pub properties: CollectionPropertiesVec,530}531532533534pub type CollectionPropertiesPermissionsVec =535 BoundedVec<PropertyKeyPermission, ConstU32<MAX_PROPERTIES_PER_ITEM>>;536537538pub type CollectionPropertiesVec = BoundedVec<Property, ConstU32<MAX_PROPERTIES_PER_ITEM>>;539540541542543544545546#[derive(Encode, Decode, Debug, Default, Clone, PartialEq, TypeInfo, MaxEncodedLen)]547#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]548549550551pub struct CollectionLimits {552 553 554 555 pub account_token_ownership_limit: Option<u32>,556557 558 559 560 pub sponsored_data_size: Option<u32>,561562 563 564 565 566 567 568 569 570 571 pub sponsored_data_rate_limit: Option<SponsoringRateLimit>,572 573574 575 576 577 578 pub token_limit: Option<u32>,579580 581 582 583 584 585 586 587 pub sponsor_transfer_timeout: Option<u32>,588589 590 591 592 593 pub sponsor_approve_timeout: Option<u32>,594595 596 597 598 pub owner_can_transfer: Option<bool>,599600 601 602 603 pub owner_can_destroy: Option<bool>,604605 606 607 608 pub transfers_enabled: Option<bool>,609}610611impl CollectionLimits {612 pub fn with_default_limits(collection_type: CollectionMode) -> Self {613 CollectionLimits {614 account_token_ownership_limit: Some(ACCOUNT_TOKEN_OWNERSHIP_LIMIT),615 sponsored_data_size: Some(CUSTOM_DATA_LIMIT),616 sponsored_data_rate_limit: Some(SponsoringRateLimit::SponsoringDisabled),617 token_limit: Some(COLLECTION_TOKEN_LIMIT),618 sponsor_transfer_timeout: match collection_type {619 CollectionMode::NFT => Some(NFT_SPONSOR_TRANSFER_TIMEOUT),620 CollectionMode::ReFungible => Some(REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT),621 CollectionMode::Fungible(_) => Some(FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT),622 },623 sponsor_approve_timeout: Some(SPONSOR_APPROVE_TIMEOUT),624 owner_can_transfer: Some(false),625 owner_can_destroy: Some(true),626 transfers_enabled: Some(true),627 }628 }629630 631 pub fn account_token_ownership_limit(&self) -> u32 {632 self.account_token_ownership_limit633 .unwrap_or(ACCOUNT_TOKEN_OWNERSHIP_LIMIT)634 .min(MAX_TOKEN_OWNERSHIP)635 }636637 638 pub fn sponsored_data_size(&self) -> u32 {639 self.sponsored_data_size640 .unwrap_or(CUSTOM_DATA_LIMIT)641 .min(CUSTOM_DATA_LIMIT)642 }643644 645 pub fn token_limit(&self) -> u32 {646 self.token_limit647 .unwrap_or(COLLECTION_TOKEN_LIMIT)648 .min(COLLECTION_TOKEN_LIMIT)649 }650651 652 653 pub fn sponsor_transfer_timeout(&self, default: u32) -> u32 {654 self.sponsor_transfer_timeout655 .unwrap_or(default)656 .min(MAX_SPONSOR_TIMEOUT)657 }658659 660 pub fn sponsor_approve_timeout(&self) -> u32 {661 self.sponsor_approve_timeout662 .unwrap_or(SPONSOR_APPROVE_TIMEOUT)663 .min(MAX_SPONSOR_TIMEOUT)664 }665666 667 pub fn owner_can_transfer(&self) -> bool {668 self.owner_can_transfer.unwrap_or(false)669 }670671 672 pub fn owner_can_transfer_instaled(&self) -> bool {673 self.owner_can_transfer.is_some()674 }675676 677 pub fn owner_can_destroy(&self) -> bool {678 self.owner_can_destroy.unwrap_or(true)679 }680681 682 pub fn transfers_enabled(&self) -> bool {683 self.transfers_enabled.unwrap_or(true)684 }685686 687 pub fn sponsored_data_rate_limit(&self) -> Option<u32> {688 match self689 .sponsored_data_rate_limit690 .unwrap_or(SponsoringRateLimit::SponsoringDisabled)691 {692 SponsoringRateLimit::SponsoringDisabled => None,693 SponsoringRateLimit::Blocks(v) => Some(v.min(MAX_SPONSOR_TIMEOUT)),694 }695 }696}697698699700701702703#[derive(Encode, Decode, Debug, Default, Clone, PartialEq, TypeInfo, MaxEncodedLen)]704#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]705706707pub struct CollectionPermissions {708 709 710 711 pub access: Option<AccessMode>,712713 714 715 716 pub mint_mode: Option<bool>,717718 719 720 721 722 723 724 pub nesting: Option<NestingPermissions>,725}726727impl CollectionPermissions {728 729 pub fn access(&self) -> AccessMode {730 self.access.unwrap_or(AccessMode::Normal)731 }732733 734 pub fn mint_mode(&self) -> bool {735 self.mint_mode.unwrap_or(false)736 }737738 739 pub fn nesting(&self) -> &NestingPermissions {740 static DEFAULT: NestingPermissions = NestingPermissions {741 token_owner: false,742 collection_admin: false,743 restricted: None,744 #[cfg(feature = "runtime-benchmarks")]745 permissive: false,746 };747 self.nesting.as_ref().unwrap_or(&DEFAULT)748 }749}750751752type OwnerRestrictedSetInner = BoundedBTreeSet<CollectionId, ConstU32<16>>;753754755#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen, Derivative)]756#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]757#[derivative(Debug)]758pub struct OwnerRestrictedSet(759 #[cfg_attr(feature = "serde1", serde(with = "bounded::set_serde"))]760 #[derivative(Debug(format_with = "bounded::set_debug"))]761 pub OwnerRestrictedSetInner,762);763764impl OwnerRestrictedSet {765 766 pub fn new() -> Self {767 Self(Default::default())768 }769}770impl core::ops::Deref for OwnerRestrictedSet {771 type Target = OwnerRestrictedSetInner;772 fn deref(&self) -> &Self::Target {773 &self.0774 }775}776impl core::ops::DerefMut for OwnerRestrictedSet {777 fn deref_mut(&mut self) -> &mut Self::Target {778 &mut self.0779 }780}781782783#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen, Derivative)]784#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]785#[derivative(Debug)]786pub struct NestingPermissions {787 788 pub token_owner: bool,789 790 pub collection_admin: bool,791 792 pub restricted: Option<OwnerRestrictedSet>,793794 #[cfg(feature = "runtime-benchmarks")]795 796 pub permissive: bool,797}798799800801802#[derive(Encode, Decode, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]803#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]804pub enum SponsoringRateLimit {805 806 SponsoringDisabled,807 808 Blocks(u32),809}810811812#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]813#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]814#[derivative(Debug)]815pub struct CreateNftData {816 817 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]818 #[derivative(Debug(format_with = "bounded::vec_debug"))]819 820 pub properties: CollectionPropertiesVec,821}822823824#[derive(Encode, Decode, MaxEncodedLen, Default, Debug, Clone, PartialEq, TypeInfo)]825#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]826pub struct CreateFungibleData {827 828 pub value: u128,829}830831832#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]833#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]834#[derivative(Debug)]835pub struct CreateReFungibleData {836 837 pub pieces: u128,838839 840 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]841 #[derivative(Debug(format_with = "bounded::vec_debug"))]842 pub properties: CollectionPropertiesVec,843}844845846#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]847#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]848pub enum MetaUpdatePermission {849 ItemOwner,850 Admin,851 None,852}853854855856#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]857#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]858pub enum CreateItemData {859 860 NFT(CreateNftData),861 862 Fungible(CreateFungibleData),863 864 ReFungible(CreateReFungibleData),865}866867868#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]869#[derivative(Debug)]870pub struct CreateNftExData<CrossAccountId> {871 872 #[derivative(Debug(format_with = "bounded::vec_debug"))]873 pub properties: CollectionPropertiesVec,874875 876 pub owner: CrossAccountId,877}878879880#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]881#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]882pub struct CreateRefungibleExMultipleOwners<CrossAccountId> {883 #[derivative(Debug(format_with = "bounded::map_debug"))]884 pub users: BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,885 #[derivative(Debug(format_with = "bounded::vec_debug"))]886 pub properties: CollectionPropertiesVec,887}888889890#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]891#[derivative(Debug(bound = "CrossAccountId: fmt::Debug"))]892pub struct CreateRefungibleExSingleOwner<CrossAccountId> {893 pub user: CrossAccountId,894 pub pieces: u128,895 #[derivative(Debug(format_with = "bounded::vec_debug"))]896 pub properties: CollectionPropertiesVec,897}898899900#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]901#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]902pub enum CreateItemExData<CrossAccountId> {903 904 NFT(905 #[derivative(Debug(format_with = "bounded::vec_debug"))]906 BoundedVec<CreateNftExData<CrossAccountId>, ConstU32<MAX_ITEMS_PER_BATCH>>,907 ),908909 910 Fungible(911 #[derivative(Debug(format_with = "bounded::map_debug"))]912 BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,913 ),914915 916 917 RefungibleMultipleItems(918 #[derivative(Debug(format_with = "bounded::vec_debug"))]919 BoundedVec<CreateRefungibleExSingleOwner<CrossAccountId>, ConstU32<MAX_ITEMS_PER_BATCH>>,920 ),921922 923 924 RefungibleMultipleOwners(CreateRefungibleExMultipleOwners<CrossAccountId>),925}926927impl From<CreateNftData> for CreateItemData {928 fn from(item: CreateNftData) -> Self {929 CreateItemData::NFT(item)930 }931}932933impl From<CreateReFungibleData> for CreateItemData {934 fn from(item: CreateReFungibleData) -> Self {935 CreateItemData::ReFungible(item)936 }937}938939impl From<CreateFungibleData> for CreateItemData {940 fn from(item: CreateFungibleData) -> Self {941 CreateItemData::Fungible(item)942 }943}944945946#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]947#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]948949pub struct TokenChild {950 951 pub token: TokenId,952953 954 pub collection: CollectionId,955}956957958#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]959#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]960pub struct CollectionStats {961 962 pub created: u32,963964 965 pub destroyed: u32,966967 968 pub alive: u32,969}970971972#[derive(Encode, Decode, Clone, Debug)]973#[cfg_attr(feature = "std", derive(PartialEq))]974pub struct PhantomType<T>(core::marker::PhantomData<T>);975976impl<T: TypeInfo + 'static> TypeInfo for PhantomType<T> {977 type Identity = PhantomType<T>;978979 fn type_info() -> scale_info::Type {980 use scale_info::{981 Type, Path,982 build::{FieldsBuilder, UnnamedFields},983 form::MetaForm,984 type_params,985 };986 Type::builder()987 .path(Path::new("up_data_structs", "PhantomType"))988 .type_params(type_params!(T))989 .composite(990 <FieldsBuilder<MetaForm, UnnamedFields>>::default().field(|b| b.ty::<[T; 0]>()),991 )992 }993}994impl<T> MaxEncodedLen for PhantomType<T> {995 fn max_encoded_len() -> usize {996 0997 }998}99910001001pub type BoundedBytes<S> = BoundedVec<u8, S>;100210031004pub type AuxPropertyValue = BoundedBytes<ConstU32<MAX_AUX_PROPERTY_VALUE_LENGTH>>;100510061007pub type PropertyKey = BoundedBytes<ConstU32<MAX_PROPERTY_KEY_LENGTH>>;100810091010pub type PropertyValue = BoundedBytes<ConstU32<MAX_PROPERTY_VALUE_LENGTH>>;101110121013#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone, Default)]1014#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]1015pub struct PropertyPermission {1016 1017 1018 1019 pub mutable: bool,10201021 1022 pub collection_admin: bool,10231024 1025 pub token_owner: bool,1026}10271028impl PropertyPermission {1029 1030 pub fn none() -> Self {1031 Self {1032 mutable: true,1033 collection_admin: false,1034 token_owner: false,1035 }1036 }1037}103810391040#[derive(Encode, Decode, Debug, TypeInfo, Clone, PartialEq, MaxEncodedLen)]1041#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]1042pub struct Property {1043 1044 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]1045 pub key: PropertyKey,10461047 1048 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]1049 pub value: PropertyValue,1050}10511052impl Into<(PropertyKey, PropertyValue)> for Property {1053 fn into(self) -> (PropertyKey, PropertyValue) {1054 (self.key, self.value)1055 }1056}105710581059#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone)]1060#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]1061pub struct PropertyKeyPermission {1062 1063 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]1064 pub key: PropertyKey,10651066 1067 pub permission: PropertyPermission,1068}10691070impl Into<(PropertyKey, PropertyPermission)> for PropertyKeyPermission {1071 fn into(self) -> (PropertyKey, PropertyPermission) {1072 (self.key, self.permission)1073 }1074}107510761077#[derive(Debug)]1078pub enum PropertiesError {1079 1080 1081 1082 1083 NoSpaceForProperty,10841085 1086 1087 1088 PropertyLimitReached,10891090 1091 InvalidCharacterInPropertyKey,10921093 1094 1095 1096 PropertyKeyIsTooLong,10971098 1099 EmptyPropertyKey,1100}11011102110311041105#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, PartialEq, Clone, Copy)]1106pub enum PropertyScope {1107 None,1108 Rmrk,1109}11101111impl PropertyScope {1112 1113 pub fn apply(self, key: PropertyKey) -> Result<PropertyKey, PropertiesError> {1114 let scope_str: &[u8] = match self {1115 Self::None => return Ok(key),1116 Self::Rmrk => b"rmrk",1117 };11181119 [scope_str, b":", key.as_slice()]1120 .concat()1121 .try_into()1122 .map_err(|_| PropertiesError::PropertyKeyIsTooLong)1123 }1124}112511261127pub trait TrySetProperty: Sized {1128 type Value;11291130 1131 fn try_scoped_set(1132 &mut self,1133 scope: PropertyScope,1134 key: PropertyKey,1135 value: Self::Value,1136 ) -> Result<Option<Self::Value>, PropertiesError>;11371138 1139 fn try_scoped_set_from_iter<I, KV>(1140 &mut self,1141 scope: PropertyScope,1142 iter: I,1143 ) -> Result<(), PropertiesError>1144 where1145 I: Iterator<Item = KV>,1146 KV: Into<(PropertyKey, Self::Value)>,1147 {1148 for kv in iter {1149 let (key, value) = kv.into();1150 self.try_scoped_set(scope, key, value)?;1151 }11521153 Ok(())1154 }11551156 1157 fn try_set(1158 &mut self,1159 key: PropertyKey,1160 value: Self::Value,1161 ) -> Result<Option<Self::Value>, PropertiesError> {1162 self.try_scoped_set(PropertyScope::None, key, value)1163 }11641165 1166 fn try_set_from_iter<I, KV>(&mut self, iter: I) -> Result<(), PropertiesError>1167 where1168 I: Iterator<Item = KV>,1169 KV: Into<(PropertyKey, Self::Value)>,1170 {1171 self.try_scoped_set_from_iter(PropertyScope::None, iter)1172 }1173}117411751176#[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)]1177#[derivative(Default(bound = ""))]1178pub struct PropertiesMap<Value>(1179 BoundedBTreeMap<PropertyKey, Value, ConstU32<MAX_PROPERTIES_PER_ITEM>>,1180);11811182impl<Value> PropertiesMap<Value> {1183 1184 pub fn new() -> Self {1185 Self(BoundedBTreeMap::new())1186 }11871188 1189 pub fn remove(&mut self, key: &PropertyKey) -> Result<Option<Value>, PropertiesError> {1190 Self::check_property_key(key)?;11911192 Ok(self.0.remove(key))1193 }11941195 1196 pub fn get(&self, key: &PropertyKey) -> Option<&Value> {1197 self.0.get(key)1198 }11991200 1201 pub fn contains_key(&self, key: &PropertyKey) -> bool {1202 self.0.contains_key(key)1203 }12041205 1206 fn check_property_key(key: &PropertyKey) -> Result<(), PropertiesError> {1207 if key.is_empty() {1208 return Err(PropertiesError::EmptyPropertyKey);1209 }12101211 for byte in key.as_slice().iter() {1212 let byte = *byte;12131214 if !byte.is_ascii_alphanumeric() && byte != b'_' && byte != b'-' && byte != b'.' {1215 return Err(PropertiesError::InvalidCharacterInPropertyKey);1216 }1217 }12181219 Ok(())1220 }12211222 pub fn values(&self) -> impl Iterator<Item = &Value> {1223 self.0.values()1224 }1225}12261227impl<Value> IntoIterator for PropertiesMap<Value> {1228 type Item = (PropertyKey, Value);1229 type IntoIter = <1230 BoundedBTreeMap<1231 PropertyKey,1232 Value,1233 ConstU32<MAX_PROPERTIES_PER_ITEM>1234 > as IntoIterator1235 >::IntoIter;12361237 fn into_iter(self) -> Self::IntoIter {1238 self.0.into_iter()1239 }1240}12411242impl<Value> TrySetProperty for PropertiesMap<Value> {1243 type Value = Value;12441245 fn try_scoped_set(1246 &mut self,1247 scope: PropertyScope,1248 key: PropertyKey,1249 value: Self::Value,1250 ) -> Result<Option<Self::Value>, PropertiesError> {1251 Self::check_property_key(&key)?;12521253 let key = scope.apply(key)?;1254 self.01255 .try_insert(key, value)1256 .map_err(|_| PropertiesError::PropertyLimitReached)1257 }1258}125912601261pub type PropertiesPermissionMap = PropertiesMap<PropertyPermission>;126212631264#[derive(Encode, Decode, TypeInfo, Clone, PartialEq, MaxEncodedLen)]1265pub struct Properties {1266 map: PropertiesMap<PropertyValue>,1267 consumed_space: u32,1268 space_limit: u32,1269}12701271impl Properties {1272 1273 pub fn new(space_limit: u32) -> Self {1274 Self {1275 map: PropertiesMap::new(),1276 consumed_space: 0,1277 space_limit,1278 }1279 }12801281 1282 pub fn remove(&mut self, key: &PropertyKey) -> Result<Option<PropertyValue>, PropertiesError> {1283 let value = self.map.remove(key)?;12841285 if let Some(ref value) = value {1286 let value_len = value.len() as u32;1287 self.consumed_space -= value_len;1288 }12891290 Ok(value)1291 }12921293 1294 pub fn get(&self, key: &PropertyKey) -> Option<&PropertyValue> {1295 self.map.get(key)1296 }12971298 1299 1300 pub fn recompute_consumed_space(&mut self) {1301 self.consumed_space = self.map.values().map(|value| value.len() as u32).sum();1302 }1303}13041305impl IntoIterator for Properties {1306 type Item = (PropertyKey, PropertyValue);1307 type IntoIter = <PropertiesMap<PropertyValue> as IntoIterator>::IntoIter;13081309 fn into_iter(self) -> Self::IntoIter {1310 self.map.into_iter()1311 }1312}13131314impl TrySetProperty for Properties {1315 type Value = PropertyValue;13161317 fn try_scoped_set(1318 &mut self,1319 scope: PropertyScope,1320 key: PropertyKey,1321 value: Self::Value,1322 ) -> Result<Option<Self::Value>, PropertiesError> {1323 let value_len = value.len();13241325 if self.consumed_space as usize + value_len > self.space_limit as usize1326 && !cfg!(feature = "runtime-benchmarks")1327 {1328 return Err(PropertiesError::NoSpaceForProperty);1329 }13301331 let value_len = value_len as u32;1332 let old_value = self.map.try_scoped_set(scope, key, value)?;13331334 let old_value_len = old_value.as_ref().map(|v| v.len() as u32).unwrap_or(0);13351336 if value_len > old_value_len {1337 self.consumed_space += value_len - old_value_len;1338 } else {1339 self.consumed_space -= old_value_len - value_len;1340 }13411342 Ok(old_value)1343 }1344}134513461347pub struct CollectionProperties;13481349impl Get<Properties> for CollectionProperties {1350 fn get() -> Properties {1351 Properties::new(MAX_COLLECTION_PROPERTIES_SIZE)1352 }1353}135413551356pub struct TokenProperties;13571358impl Get<Properties> for TokenProperties {1359 fn get() -> Properties {1360 Properties::new(MAX_TOKEN_PROPERTIES_SIZE)1361 }1362}1363136413651366parameter_types! {1367 #[derive(PartialEq, TypeInfo)]1368 pub const RmrkStringLimit: u32 = 128;1369 #[derive(PartialEq)]1370 pub const RmrkCollectionSymbolLimit: u32 = MAX_TOKEN_PREFIX_LENGTH;1371 #[derive(PartialEq)]1372 pub const RmrkResourceSymbolLimit: u32 = 10;1373 #[derive(PartialEq)]1374 pub const RmrkBaseSymbolLimit: u32 = MAX_TOKEN_PREFIX_LENGTH;1375 #[derive(PartialEq)]1376 pub const RmrkKeyLimit: u32 = 32;1377 #[derive(PartialEq)]1378 pub const RmrkValueLimit: u32 = 256;1379 #[derive(PartialEq)]1380 pub const RmrkMaxCollectionsEquippablePerPart: u32 = 100;1381 #[derive(PartialEq)]1382 pub const MaxPropertiesPerTheme: u32 = 5;1383 #[derive(PartialEq)]1384 pub const RmrkPartsLimit: u32 = 25;1385 #[derive(PartialEq)]1386 pub const RmrkMaxPriorities: u32 = 25;1387 #[derive(PartialEq)]1388 pub const MaxResourcesOnMint: u32 = 100;1389}13901391impl From<RmrkCollectionId> for CollectionId {1392 fn from(id: RmrkCollectionId) -> Self {1393 Self(id)1394 }1395}13961397impl From<RmrkNftId> for TokenId {1398 fn from(id: RmrkNftId) -> Self {1399 Self(id)1400 }1401}14021403pub type RmrkCollectionInfo<AccountId> =1404 CollectionInfo<RmrkString, RmrkCollectionSymbol, AccountId>;1405pub type RmrkInstanceInfo<AccountId> = NftInfo<AccountId, Permill, RmrkString>;1406pub type RmrkResourceInfo = ResourceInfo<RmrkString, RmrkBoundedParts>;1407pub type RmrkPropertyInfo = PropertyInfo<RmrkKeyString, RmrkValueString>;1408pub type RmrkBaseInfo<AccountId> = BaseInfo<AccountId, RmrkString>;1409pub type BoundedEquippableCollectionIds =1410 BoundedVec<RmrkCollectionId, RmrkMaxCollectionsEquippablePerPart>;1411pub type RmrkPartType = PartType<RmrkString, BoundedEquippableCollectionIds>;1412pub type RmrkEquippableList = EquippableList<BoundedEquippableCollectionIds>;1413pub type RmrkThemeProperty = ThemeProperty<RmrkString>;1414pub type RmrkTheme = Theme<RmrkString, Vec<RmrkThemeProperty>>;1415pub type RmrkBoundedTheme = Theme<RmrkString, BoundedVec<RmrkThemeProperty, MaxPropertiesPerTheme>>;1416pub type RmrkResourceTypes = ResourceTypes<RmrkString, RmrkBoundedParts>;14171418pub type RmrkBasicResource = BasicResource<RmrkString>;1419pub type RmrkComposableResource = ComposableResource<RmrkString, RmrkBoundedParts>;1420pub type RmrkSlotResource = SlotResource<RmrkString>;14211422pub type RmrkString = BoundedVec<u8, RmrkStringLimit>;1423pub type RmrkCollectionSymbol = BoundedVec<u8, RmrkCollectionSymbolLimit>;1424pub type RmrkBaseSymbol = BoundedVec<u8, RmrkBaseSymbolLimit>;1425pub type RmrkKeyString = BoundedVec<u8, RmrkKeyLimit>;1426pub type RmrkValueString = BoundedVec<u8, RmrkValueLimit>;1427pub type RmrkBoundedResource = BoundedVec<u8, RmrkResourceSymbolLimit>;1428pub type RmrkBoundedParts = BoundedVec<RmrkPartId, RmrkPartsLimit>; 14291430pub type RmrkRpcString = Vec<u8>;1431pub type RmrkThemeName = RmrkRpcString;1432pub type RmrkPropertyKey = RmrkRpcString;