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, 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 613 pub fn account_token_ownership_limit(&self) -> u32 {614 self.account_token_ownership_limit615 .unwrap_or(ACCOUNT_TOKEN_OWNERSHIP_LIMIT)616 .min(MAX_TOKEN_OWNERSHIP)617 }618619 620 pub fn sponsored_data_size(&self) -> u32 {621 self.sponsored_data_size622 .unwrap_or(CUSTOM_DATA_LIMIT)623 .min(CUSTOM_DATA_LIMIT)624 }625626 627 pub fn token_limit(&self) -> u32 {628 self.token_limit629 .unwrap_or(COLLECTION_TOKEN_LIMIT)630 .min(COLLECTION_TOKEN_LIMIT)631 }632633 634 635 pub fn sponsor_transfer_timeout(&self, default: u32) -> u32 {636 self.sponsor_transfer_timeout637 .unwrap_or(default)638 .min(MAX_SPONSOR_TIMEOUT)639 }640641 642 pub fn sponsor_approve_timeout(&self) -> u32 {643 self.sponsor_approve_timeout644 .unwrap_or(SPONSOR_APPROVE_TIMEOUT)645 .min(MAX_SPONSOR_TIMEOUT)646 }647648 649 pub fn owner_can_transfer(&self) -> bool {650 self.owner_can_transfer.unwrap_or(false)651 }652653 654 pub fn owner_can_transfer_instaled(&self) -> bool {655 self.owner_can_transfer.is_some()656 }657658 659 pub fn owner_can_destroy(&self) -> bool {660 self.owner_can_destroy.unwrap_or(true)661 }662663 664 pub fn transfers_enabled(&self) -> bool {665 self.transfers_enabled.unwrap_or(true)666 }667668 669 pub fn sponsored_data_rate_limit(&self) -> Option<u32> {670 match self671 .sponsored_data_rate_limit672 .unwrap_or(SponsoringRateLimit::SponsoringDisabled)673 {674 SponsoringRateLimit::SponsoringDisabled => None,675 SponsoringRateLimit::Blocks(v) => Some(v.min(MAX_SPONSOR_TIMEOUT)),676 }677 }678}679680681682683684685#[derive(Encode, Decode, Debug, Default, Clone, PartialEq, TypeInfo, MaxEncodedLen)]686#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]687688689pub struct CollectionPermissions {690 691 692 693 pub access: Option<AccessMode>,694695 696 697 698 pub mint_mode: Option<bool>,699700 701 702 703 704 705 706 pub nesting: Option<NestingPermissions>,707}708709impl CollectionPermissions {710 711 pub fn access(&self) -> AccessMode {712 self.access.unwrap_or(AccessMode::Normal)713 }714715 716 pub fn mint_mode(&self) -> bool {717 self.mint_mode.unwrap_or(false)718 }719720 721 pub fn nesting(&self) -> &NestingPermissions {722 static DEFAULT: NestingPermissions = NestingPermissions {723 token_owner: false,724 collection_admin: false,725 restricted: None,726 #[cfg(feature = "runtime-benchmarks")]727 permissive: false,728 };729 self.nesting.as_ref().unwrap_or(&DEFAULT)730 }731}732733734type OwnerRestrictedSetInner = BoundedBTreeSet<CollectionId, ConstU32<16>>;735736737#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen, Derivative)]738#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]739#[derivative(Debug)]740pub struct OwnerRestrictedSet(741 #[cfg_attr(feature = "serde1", serde(with = "bounded::set_serde"))]742 #[derivative(Debug(format_with = "bounded::set_debug"))]743 pub OwnerRestrictedSetInner,744);745746impl OwnerRestrictedSet {747 748 pub fn new() -> Self {749 Self(Default::default())750 }751}752impl core::ops::Deref for OwnerRestrictedSet {753 type Target = OwnerRestrictedSetInner;754 fn deref(&self) -> &Self::Target {755 &self.0756 }757}758impl core::ops::DerefMut for OwnerRestrictedSet {759 fn deref_mut(&mut self) -> &mut Self::Target {760 &mut self.0761 }762}763764765#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen, Derivative)]766#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]767#[derivative(Debug)]768pub struct NestingPermissions {769 770 pub token_owner: bool,771 772 pub collection_admin: bool,773 774 pub restricted: Option<OwnerRestrictedSet>,775776 #[cfg(feature = "runtime-benchmarks")]777 778 pub permissive: bool,779}780781782783784#[derive(Encode, Decode, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]785#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]786pub enum SponsoringRateLimit {787 788 SponsoringDisabled,789 790 Blocks(u32),791}792793794#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]795#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]796#[derivative(Debug)]797pub struct CreateNftData {798 799 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]800 #[derivative(Debug(format_with = "bounded::vec_debug"))]801 802 pub properties: CollectionPropertiesVec,803}804805806#[derive(Encode, Decode, MaxEncodedLen, Default, Debug, Clone, PartialEq, TypeInfo)]807#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]808pub struct CreateFungibleData {809 810 pub value: u128,811}812813814#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]815#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]816#[derivative(Debug)]817pub struct CreateReFungibleData {818 819 pub pieces: u128,820821 822 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]823 #[derivative(Debug(format_with = "bounded::vec_debug"))]824 pub properties: CollectionPropertiesVec,825}826827828#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]829#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]830pub enum MetaUpdatePermission {831 ItemOwner,832 Admin,833 None,834}835836837838#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]839#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]840pub enum CreateItemData {841 842 NFT(CreateNftData),843 844 Fungible(CreateFungibleData),845 846 ReFungible(CreateReFungibleData),847}848849850#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]851#[derivative(Debug)]852pub struct CreateNftExData<CrossAccountId> {853 854 #[derivative(Debug(format_with = "bounded::vec_debug"))]855 pub properties: CollectionPropertiesVec,856857 858 pub owner: CrossAccountId,859}860861862#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]863#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]864pub struct CreateRefungibleExMultipleOwners<CrossAccountId> {865 #[derivative(Debug(format_with = "bounded::map_debug"))]866 pub users: BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,867 #[derivative(Debug(format_with = "bounded::vec_debug"))]868 pub properties: CollectionPropertiesVec,869}870871872#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]873#[derivative(Debug(bound = "CrossAccountId: fmt::Debug"))]874pub struct CreateRefungibleExSingleOwner<CrossAccountId> {875 pub user: CrossAccountId,876 pub pieces: u128,877 #[derivative(Debug(format_with = "bounded::vec_debug"))]878 pub properties: CollectionPropertiesVec,879}880881882#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]883#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]884pub enum CreateItemExData<CrossAccountId> {885 886 NFT(887 #[derivative(Debug(format_with = "bounded::vec_debug"))]888 BoundedVec<CreateNftExData<CrossAccountId>, ConstU32<MAX_ITEMS_PER_BATCH>>,889 ),890891 892 Fungible(893 #[derivative(Debug(format_with = "bounded::map_debug"))]894 BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,895 ),896897 898 899 RefungibleMultipleItems(900 #[derivative(Debug(format_with = "bounded::vec_debug"))]901 BoundedVec<CreateRefungibleExSingleOwner<CrossAccountId>, ConstU32<MAX_ITEMS_PER_BATCH>>,902 ),903904 905 906 RefungibleMultipleOwners(CreateRefungibleExMultipleOwners<CrossAccountId>),907}908909impl From<CreateNftData> for CreateItemData {910 fn from(item: CreateNftData) -> Self {911 CreateItemData::NFT(item)912 }913}914915impl From<CreateReFungibleData> for CreateItemData {916 fn from(item: CreateReFungibleData) -> Self {917 CreateItemData::ReFungible(item)918 }919}920921impl From<CreateFungibleData> for CreateItemData {922 fn from(item: CreateFungibleData) -> Self {923 CreateItemData::Fungible(item)924 }925}926927928#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]929#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]930931pub struct TokenChild {932 933 pub token: TokenId,934935 936 pub collection: CollectionId,937}938939940#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]941#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]942pub struct CollectionStats {943 944 pub created: u32,945946 947 pub destroyed: u32,948949 950 pub alive: u32,951}952953954#[derive(Encode, Decode, Clone, Debug)]955#[cfg_attr(feature = "std", derive(PartialEq))]956pub struct PhantomType<T>(core::marker::PhantomData<T>);957958impl<T: TypeInfo + 'static> TypeInfo for PhantomType<T> {959 type Identity = PhantomType<T>;960961 fn type_info() -> scale_info::Type {962 use scale_info::{963 Type, Path,964 build::{FieldsBuilder, UnnamedFields},965 type_params,966 };967 Type::builder()968 .path(Path::new("up_data_structs", "PhantomType"))969 .type_params(type_params!(T))970 .composite(<FieldsBuilder<UnnamedFields>>::default().field(|b| b.ty::<[T; 0]>()))971 }972}973impl<T> MaxEncodedLen for PhantomType<T> {974 fn max_encoded_len() -> usize {975 0976 }977}978979980pub type BoundedBytes<S> = BoundedVec<u8, S>;981982983pub type AuxPropertyValue = BoundedBytes<ConstU32<MAX_AUX_PROPERTY_VALUE_LENGTH>>;984985986pub type PropertyKey = BoundedBytes<ConstU32<MAX_PROPERTY_KEY_LENGTH>>;987988989pub type PropertyValue = BoundedBytes<ConstU32<MAX_PROPERTY_VALUE_LENGTH>>;990991992#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone)]993#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]994pub struct PropertyPermission {995 996 997 998 pub mutable: bool,9991000 1001 pub collection_admin: bool,10021003 1004 pub token_owner: bool,1005}10061007impl PropertyPermission {1008 1009 pub fn none() -> Self {1010 Self {1011 mutable: true,1012 collection_admin: false,1013 token_owner: false,1014 }1015 }1016}101710181019#[derive(Encode, Decode, Debug, TypeInfo, Clone, PartialEq, MaxEncodedLen)]1020#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]1021pub struct Property {1022 1023 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]1024 pub key: PropertyKey,10251026 1027 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]1028 pub value: PropertyValue,1029}10301031impl Into<(PropertyKey, PropertyValue)> for Property {1032 fn into(self) -> (PropertyKey, PropertyValue) {1033 (self.key, self.value)1034 }1035}103610371038#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone)]1039#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]1040pub struct PropertyKeyPermission {1041 1042 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]1043 pub key: PropertyKey,10441045 1046 pub permission: PropertyPermission,1047}10481049impl Into<(PropertyKey, PropertyPermission)> for PropertyKeyPermission {1050 fn into(self) -> (PropertyKey, PropertyPermission) {1051 (self.key, self.permission)1052 }1053}105410551056#[derive(Debug)]1057pub enum PropertiesError {1058 1059 1060 1061 1062 NoSpaceForProperty,10631064 1065 1066 1067 PropertyLimitReached,10681069 1070 InvalidCharacterInPropertyKey,10711072 1073 1074 1075 PropertyKeyIsTooLong,10761077 1078 EmptyPropertyKey,1079}10801081108210831084#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, PartialEq, Clone, Copy)]1085pub enum PropertyScope {1086 None,1087 Rmrk,1088}10891090impl PropertyScope {1091 1092 pub fn apply(self, key: PropertyKey) -> Result<PropertyKey, PropertiesError> {1093 let scope_str: &[u8] = match self {1094 Self::None => return Ok(key),1095 Self::Rmrk => b"rmrk",1096 };10971098 [scope_str, b":", key.as_slice()]1099 .concat()1100 .try_into()1101 .map_err(|_| PropertiesError::PropertyKeyIsTooLong)1102 }1103}110411051106pub trait TrySetProperty: Sized {1107 type Value;11081109 1110 fn try_scoped_set(1111 &mut self,1112 scope: PropertyScope,1113 key: PropertyKey,1114 value: Self::Value,1115 ) -> Result<(), PropertiesError>;11161117 1118 fn try_scoped_set_from_iter<I, KV>(1119 &mut self,1120 scope: PropertyScope,1121 iter: I,1122 ) -> Result<(), PropertiesError>1123 where1124 I: Iterator<Item = KV>,1125 KV: Into<(PropertyKey, Self::Value)>,1126 {1127 for kv in iter {1128 let (key, value) = kv.into();1129 self.try_scoped_set(scope, key, value)?;1130 }11311132 Ok(())1133 }11341135 1136 fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError> {1137 self.try_scoped_set(PropertyScope::None, key, value)1138 }11391140 1141 fn try_set_from_iter<I, KV>(&mut self, iter: I) -> Result<(), PropertiesError>1142 where1143 I: Iterator<Item = KV>,1144 KV: Into<(PropertyKey, Self::Value)>,1145 {1146 self.try_scoped_set_from_iter(PropertyScope::None, iter)1147 }1148}114911501151#[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)]1152#[derivative(Default(bound = ""))]1153pub struct PropertiesMap<Value>(1154 BoundedBTreeMap<PropertyKey, Value, ConstU32<MAX_PROPERTIES_PER_ITEM>>,1155);11561157impl<Value> PropertiesMap<Value> {1158 1159 pub fn new() -> Self {1160 Self(BoundedBTreeMap::new())1161 }11621163 1164 pub fn remove(&mut self, key: &PropertyKey) -> Result<Option<Value>, PropertiesError> {1165 Self::check_property_key(key)?;11661167 Ok(self.0.remove(key))1168 }11691170 1171 pub fn get(&self, key: &PropertyKey) -> Option<&Value> {1172 self.0.get(key)1173 }11741175 1176 pub fn contains_key(&self, key: &PropertyKey) -> bool {1177 self.0.contains_key(key)1178 }11791180 1181 fn check_property_key(key: &PropertyKey) -> Result<(), PropertiesError> {1182 if key.is_empty() {1183 return Err(PropertiesError::EmptyPropertyKey);1184 }11851186 for byte in key.as_slice().iter() {1187 let byte = *byte;11881189 if !byte.is_ascii_alphanumeric() && byte != b'_' && byte != b'-' && byte != b'.' {1190 return Err(PropertiesError::InvalidCharacterInPropertyKey);1191 }1192 }11931194 Ok(())1195 }1196}11971198impl<Value> IntoIterator for PropertiesMap<Value> {1199 type Item = (PropertyKey, Value);1200 type IntoIter = <1201 BoundedBTreeMap<1202 PropertyKey,1203 Value,1204 ConstU32<MAX_PROPERTIES_PER_ITEM>1205 > as IntoIterator1206 >::IntoIter;12071208 fn into_iter(self) -> Self::IntoIter {1209 self.0.into_iter()1210 }1211}12121213impl<Value> TrySetProperty for PropertiesMap<Value> {1214 type Value = Value;12151216 fn try_scoped_set(1217 &mut self,1218 scope: PropertyScope,1219 key: PropertyKey,1220 value: Self::Value,1221 ) -> Result<(), PropertiesError> {1222 Self::check_property_key(&key)?;12231224 let key = scope.apply(key)?;1225 self.01226 .try_insert(key, value)1227 .map_err(|_| PropertiesError::PropertyLimitReached)?;12281229 Ok(())1230 }1231}123212331234pub type PropertiesPermissionMap = PropertiesMap<PropertyPermission>;123512361237#[derive(Encode, Decode, TypeInfo, Clone, PartialEq, MaxEncodedLen)]1238pub struct Properties {1239 map: PropertiesMap<PropertyValue>,1240 consumed_space: u32,1241 space_limit: u32,1242}12431244impl Properties {1245 1246 pub fn new(space_limit: u32) -> Self {1247 Self {1248 map: PropertiesMap::new(),1249 consumed_space: 0,1250 space_limit,1251 }1252 }12531254 1255 pub fn remove(&mut self, key: &PropertyKey) -> Result<Option<PropertyValue>, PropertiesError> {1256 let value = self.map.remove(key)?;12571258 if let Some(ref value) = value {1259 let value_len = value.len() as u32;1260 self.consumed_space -= value_len;1261 }12621263 Ok(value)1264 }12651266 1267 pub fn get(&self, key: &PropertyKey) -> Option<&PropertyValue> {1268 self.map.get(key)1269 }1270}12711272impl IntoIterator for Properties {1273 type Item = (PropertyKey, PropertyValue);1274 type IntoIter = <PropertiesMap<PropertyValue> as IntoIterator>::IntoIter;12751276 fn into_iter(self) -> Self::IntoIter {1277 self.map.into_iter()1278 }1279}12801281impl TrySetProperty for Properties {1282 type Value = PropertyValue;12831284 fn try_scoped_set(1285 &mut self,1286 scope: PropertyScope,1287 key: PropertyKey,1288 value: Self::Value,1289 ) -> Result<(), PropertiesError> {1290 let value_len = value.len();12911292 if self.consumed_space as usize + value_len > self.space_limit as usize1293 && !cfg!(feature = "runtime-benchmarks")1294 {1295 return Err(PropertiesError::NoSpaceForProperty);1296 }12971298 self.map.try_scoped_set(scope, key, value)?;12991300 self.consumed_space += value_len as u32;13011302 Ok(())1303 }1304}130513061307pub struct CollectionProperties;13081309impl Get<Properties> for CollectionProperties {1310 fn get() -> Properties {1311 Properties::new(MAX_COLLECTION_PROPERTIES_SIZE)1312 }1313}131413151316pub struct TokenProperties;13171318impl Get<Properties> for TokenProperties {1319 fn get() -> Properties {1320 Properties::new(MAX_TOKEN_PROPERTIES_SIZE)1321 }1322}1323132413251326parameter_types! {1327 #[derive(PartialEq, TypeInfo)]1328 pub const RmrkStringLimit: u32 = 128;1329 #[derive(PartialEq)]1330 pub const RmrkCollectionSymbolLimit: u32 = MAX_TOKEN_PREFIX_LENGTH;1331 #[derive(PartialEq)]1332 pub const RmrkResourceSymbolLimit: u32 = 10;1333 #[derive(PartialEq)]1334 pub const RmrkBaseSymbolLimit: u32 = MAX_TOKEN_PREFIX_LENGTH;1335 #[derive(PartialEq)]1336 pub const RmrkKeyLimit: u32 = 32;1337 #[derive(PartialEq)]1338 pub const RmrkValueLimit: u32 = 256;1339 #[derive(PartialEq)]1340 pub const RmrkMaxCollectionsEquippablePerPart: u32 = 100;1341 #[derive(PartialEq)]1342 pub const MaxPropertiesPerTheme: u32 = 5;1343 #[derive(PartialEq)]1344 pub const RmrkPartsLimit: u32 = 25;1345 #[derive(PartialEq)]1346 pub const RmrkMaxPriorities: u32 = 25;1347 #[derive(PartialEq)]1348 pub const MaxResourcesOnMint: u32 = 100;1349}13501351impl From<RmrkCollectionId> for CollectionId {1352 fn from(id: RmrkCollectionId) -> Self {1353 Self(id)1354 }1355}13561357impl From<RmrkNftId> for TokenId {1358 fn from(id: RmrkNftId) -> Self {1359 Self(id)1360 }1361}13621363pub type RmrkCollectionInfo<AccountId> =1364 CollectionInfo<RmrkString, RmrkCollectionSymbol, AccountId>;1365pub type RmrkInstanceInfo<AccountId> = NftInfo<AccountId, Permill, RmrkString>;1366pub type RmrkResourceInfo = ResourceInfo<RmrkString, RmrkBoundedParts>;1367pub type RmrkPropertyInfo = PropertyInfo<RmrkKeyString, RmrkValueString>;1368pub type RmrkBaseInfo<AccountId> = BaseInfo<AccountId, RmrkString>;1369pub type BoundedEquippableCollectionIds =1370 BoundedVec<RmrkCollectionId, RmrkMaxCollectionsEquippablePerPart>;1371pub type RmrkPartType = PartType<RmrkString, BoundedEquippableCollectionIds>;1372pub type RmrkEquippableList = EquippableList<BoundedEquippableCollectionIds>;1373pub type RmrkThemeProperty = ThemeProperty<RmrkString>;1374pub type RmrkTheme = Theme<RmrkString, Vec<RmrkThemeProperty>>;1375pub type RmrkBoundedTheme = Theme<RmrkString, BoundedVec<RmrkThemeProperty, MaxPropertiesPerTheme>>;1376pub type RmrkResourceTypes = ResourceTypes<RmrkString, RmrkBoundedParts>;13771378pub type RmrkBasicResource = BasicResource<RmrkString>;1379pub type RmrkComposableResource = ComposableResource<RmrkString, RmrkBoundedParts>;1380pub type RmrkSlotResource = SlotResource<RmrkString>;13811382pub type RmrkString = BoundedVec<u8, RmrkStringLimit>;1383pub type RmrkCollectionSymbol = BoundedVec<u8, RmrkCollectionSymbolLimit>;1384pub type RmrkBaseSymbol = BoundedVec<u8, RmrkBaseSymbolLimit>;1385pub type RmrkKeyString = BoundedVec<u8, RmrkKeyLimit>;1386pub type RmrkValueString = BoundedVec<u8, RmrkValueLimit>;1387pub type RmrkBoundedResource = BoundedVec<u8, RmrkResourceSymbolLimit>;1388pub type RmrkBoundedParts = BoundedVec<RmrkPartId, RmrkPartsLimit>; 13891390pub type RmrkRpcString = Vec<u8>;1391pub type RmrkThemeName = RmrkRpcString;1392pub type RmrkPropertyKey = RmrkRpcString;