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#[derive(Encode, Decode, Clone, PartialEq, TypeInfo)]225#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]226pub struct TokenData<CrossAccountId> {227 228 pub properties: Vec<Property>,229230 231 pub owner: Option<CrossAccountId>,232233 234 pub pieces: u128,235}236237238pub struct OverflowError;239impl From<OverflowError> for &'static str {240 fn from(_: OverflowError) -> Self {241 "overflow occured"242 }243}244245246pub type DecimalPoints = u8;247248249250251252253#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]254#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]255pub enum CollectionMode {256 257 NFT,258 259 Fungible(DecimalPoints),260 261 ReFungible,262}263264impl CollectionMode {265 266 pub fn id(&self) -> u8 {267 match self {268 CollectionMode::NFT => 1,269 CollectionMode::Fungible(_) => 2,270 CollectionMode::ReFungible => 3,271 }272 }273}274275276pub trait SponsoringResolve<AccountId, Call> {277 fn resolve(who: &AccountId, call: &Call) -> Option<AccountId>;278}279280281#[derive(Encode, Decode, Eq, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]282#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]283pub enum AccessMode {284 285 Normal,286 287 AllowList,288}289impl Default for AccessMode {290 fn default() -> Self {291 Self::Normal292 }293}294295296#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]297#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]298pub enum SchemaVersion {299 ImageURL,300 Unique,301}302impl Default for SchemaVersion {303 fn default() -> Self {304 Self::ImageURL305 }306}307308309#[derive(Encode, Decode, Default, Debug, Clone, PartialEq, TypeInfo)]310#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]311pub struct Ownership<AccountId> {312 pub owner: AccountId,313 pub fraction: u128,314}315316317#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]318#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]319pub enum SponsorshipState<AccountId> {320 321 Disabled,322 323 324 Unconfirmed(AccountId),325 326 Confirmed(AccountId),327}328329impl<AccountId> SponsorshipState<AccountId> {330 331 pub fn sponsor(&self) -> Option<&AccountId> {332 match self {333 Self::Confirmed(sponsor) => Some(sponsor),334 _ => None,335 }336 }337338 339 pub fn pending_sponsor(&self) -> Option<&AccountId> {340 match self {341 Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor),342 _ => None,343 }344 }345346 347 pub fn confirmed(&self) -> bool {348 matches!(self, Self::Confirmed(_))349 }350}351352impl<T> Default for SponsorshipState<T> {353 fn default() -> Self {354 Self::Disabled355 }356}357358pub type CollectionName = BoundedVec<u16, ConstU32<MAX_COLLECTION_NAME_LENGTH>>;359pub type CollectionDescription = BoundedVec<u16, ConstU32<MAX_COLLECTION_DESCRIPTION_LENGTH>>;360pub type CollectionTokenPrefix = BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>;361362#[derive(Bitfields, Clone, Copy, PartialEq, Eq, Debug, Default)]363#[bondrewd(enforce_bytes = 1)]364pub struct CollectionFlags {365 366 #[bondrewd(bits = "0..1")]367 pub foreign: bool,368 369 #[bondrewd(bits = "1..2")]370 pub erc721metadata: bool,371 372 #[bondrewd(bits = "7..8")]373 pub external: bool,374375 #[bondrewd(reserve, bits = "2..7")]376 pub reserved: u8,377}378bondrewd_codec!(CollectionFlags);379380381382383384385386#[struct_versioning::versioned(version = 2, upper)]387#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]388pub struct Collection<AccountId> {389 390 pub owner: AccountId,391392 393 pub mode: CollectionMode,394395 396 #[version(..2)]397 pub access: AccessMode,398399 400 pub name: CollectionName,401402 403 pub description: CollectionDescription,404405 406 pub token_prefix: CollectionTokenPrefix,407408 #[version(..2)]409 pub mint_mode: bool,410411 #[version(..2)]412 pub offchain_schema: BoundedVec<u8, ConstU32<OFFCHAIN_SCHEMA_LIMIT>>,413414 #[version(..2)]415 pub schema_version: SchemaVersion,416417 418 pub sponsorship: SponsorshipState<AccountId>,419420 421 pub limits: CollectionLimits,422423 424 #[version(2.., upper(Default::default()))]425 pub permissions: CollectionPermissions,426427 #[version(2.., upper(Default::default()))]428 pub flags: CollectionFlags,429430 #[version(..2)]431 pub variable_on_chain_schema: BoundedVec<u8, ConstU32<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>>,432433 #[version(..2)]434 pub const_on_chain_schema: BoundedVec<u8, ConstU32<CONST_ON_CHAIN_SCHEMA_LIMIT>>,435436 #[version(..2)]437 pub meta_update_permission: MetaUpdatePermission,438}439440#[derive(Encode, Decode, Clone, PartialEq, TypeInfo)]441#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]442pub struct RpcCollectionFlags {443 444 pub foreign: bool,445 446 pub erc721metadata: bool,447}448449450#[derive(Encode, Decode, Clone, PartialEq, TypeInfo)]451#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]452pub struct RpcCollection<AccountId> {453 454 pub owner: AccountId,455456 457 pub mode: CollectionMode,458459 460 pub name: Vec<u16>,461462 463 pub description: Vec<u16>,464465 466 pub token_prefix: Vec<u8>,467468 469 pub sponsorship: SponsorshipState<AccountId>,470471 472 pub limits: CollectionLimits,473474 475 pub permissions: CollectionPermissions,476477 478 pub token_property_permissions: Vec<PropertyKeyPermission>,479480 481 pub properties: Vec<Property>,482483 484 pub read_only: bool,485486 487 pub flags: RpcCollectionFlags,488}489490491492493#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, Derivative, MaxEncodedLen)]494#[derivative(Debug, Default(bound = ""))]495pub struct CreateCollectionData<AccountId> {496 497 #[derivative(Default(value = "CollectionMode::NFT"))]498 pub mode: CollectionMode,499500 501 pub access: Option<AccessMode>,502503 504 pub name: CollectionName,505506 507 pub description: CollectionDescription,508509 510 pub token_prefix: CollectionTokenPrefix,511512 513 pub pending_sponsor: Option<AccountId>,514515 516 pub limits: Option<CollectionLimits>,517518 519 pub permissions: Option<CollectionPermissions>,520521 522 pub token_property_permissions: CollectionPropertiesPermissionsVec,523524 525 pub properties: CollectionPropertiesVec,526}527528529530pub type CollectionPropertiesPermissionsVec =531 BoundedVec<PropertyKeyPermission, ConstU32<MAX_PROPERTIES_PER_ITEM>>;532533534pub type CollectionPropertiesVec = BoundedVec<Property, ConstU32<MAX_PROPERTIES_PER_ITEM>>;535536537538539540541542#[derive(Encode, Decode, Debug, Default, Clone, PartialEq, TypeInfo, MaxEncodedLen)]543#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]544545546547pub struct CollectionLimits {548 549 550 551 pub account_token_ownership_limit: Option<u32>,552553 554 555 556 pub sponsored_data_size: Option<u32>,557558 559 560 561 562 563 564 565 566 567 pub sponsored_data_rate_limit: Option<SponsoringRateLimit>,568 569570 571 572 573 574 pub token_limit: Option<u32>,575576 577 578 579 580 581 582 583 pub sponsor_transfer_timeout: Option<u32>,584585 586 587 588 589 pub sponsor_approve_timeout: Option<u32>,590591 592 593 594 pub owner_can_transfer: Option<bool>,595596 597 598 599 pub owner_can_destroy: Option<bool>,600601 602 603 604 pub transfers_enabled: Option<bool>,605}606607impl CollectionLimits {608 609 pub fn account_token_ownership_limit(&self) -> u32 {610 self.account_token_ownership_limit611 .unwrap_or(ACCOUNT_TOKEN_OWNERSHIP_LIMIT)612 .min(MAX_TOKEN_OWNERSHIP)613 }614615 616 pub fn sponsored_data_size(&self) -> u32 {617 self.sponsored_data_size618 .unwrap_or(CUSTOM_DATA_LIMIT)619 .min(CUSTOM_DATA_LIMIT)620 }621622 623 pub fn token_limit(&self) -> u32 {624 self.token_limit625 .unwrap_or(COLLECTION_TOKEN_LIMIT)626 .min(COLLECTION_TOKEN_LIMIT)627 }628629 630 631 pub fn sponsor_transfer_timeout(&self, default: u32) -> u32 {632 self.sponsor_transfer_timeout633 .unwrap_or(default)634 .min(MAX_SPONSOR_TIMEOUT)635 }636637 638 pub fn sponsor_approve_timeout(&self) -> u32 {639 self.sponsor_approve_timeout640 .unwrap_or(SPONSOR_APPROVE_TIMEOUT)641 .min(MAX_SPONSOR_TIMEOUT)642 }643644 645 pub fn owner_can_transfer(&self) -> bool {646 self.owner_can_transfer.unwrap_or(false)647 }648649 650 pub fn owner_can_transfer_instaled(&self) -> bool {651 self.owner_can_transfer.is_some()652 }653654 655 pub fn owner_can_destroy(&self) -> bool {656 self.owner_can_destroy.unwrap_or(true)657 }658659 660 pub fn transfers_enabled(&self) -> bool {661 self.transfers_enabled.unwrap_or(true)662 }663664 665 pub fn sponsored_data_rate_limit(&self) -> Option<u32> {666 match self667 .sponsored_data_rate_limit668 .unwrap_or(SponsoringRateLimit::SponsoringDisabled)669 {670 SponsoringRateLimit::SponsoringDisabled => None,671 SponsoringRateLimit::Blocks(v) => Some(v.min(MAX_SPONSOR_TIMEOUT)),672 }673 }674}675676677678679680681#[derive(Encode, Decode, Debug, Default, Clone, PartialEq, TypeInfo, MaxEncodedLen)]682#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]683684685pub struct CollectionPermissions {686 687 688 689 pub access: Option<AccessMode>,690691 692 693 694 pub mint_mode: Option<bool>,695696 697 698 699 700 701 702 pub nesting: Option<NestingPermissions>,703}704705impl CollectionPermissions {706 707 pub fn access(&self) -> AccessMode {708 self.access.unwrap_or(AccessMode::Normal)709 }710711 712 pub fn mint_mode(&self) -> bool {713 self.mint_mode.unwrap_or(false)714 }715716 717 pub fn nesting(&self) -> &NestingPermissions {718 static DEFAULT: NestingPermissions = NestingPermissions {719 token_owner: false,720 collection_admin: false,721 restricted: None,722 #[cfg(feature = "runtime-benchmarks")]723 permissive: false,724 };725 self.nesting.as_ref().unwrap_or(&DEFAULT)726 }727}728729730type OwnerRestrictedSetInner = BoundedBTreeSet<CollectionId, ConstU32<16>>;731732733#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen, Derivative)]734#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]735#[derivative(Debug)]736pub struct OwnerRestrictedSet(737 #[cfg_attr(feature = "serde1", serde(with = "bounded::set_serde"))]738 #[derivative(Debug(format_with = "bounded::set_debug"))]739 pub OwnerRestrictedSetInner,740);741742impl OwnerRestrictedSet {743 744 pub fn new() -> Self {745 Self(Default::default())746 }747}748impl core::ops::Deref for OwnerRestrictedSet {749 type Target = OwnerRestrictedSetInner;750 fn deref(&self) -> &Self::Target {751 &self.0752 }753}754impl core::ops::DerefMut for OwnerRestrictedSet {755 fn deref_mut(&mut self) -> &mut Self::Target {756 &mut self.0757 }758}759760761#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen, Derivative)]762#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]763#[derivative(Debug)]764pub struct NestingPermissions {765 766 pub token_owner: bool,767 768 pub collection_admin: bool,769 770 pub restricted: Option<OwnerRestrictedSet>,771772 #[cfg(feature = "runtime-benchmarks")]773 774 pub permissive: bool,775}776777778779780#[derive(Encode, Decode, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]781#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]782pub enum SponsoringRateLimit {783 784 SponsoringDisabled,785 786 Blocks(u32),787}788789790#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]791#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]792#[derivative(Debug)]793pub struct CreateNftData {794 795 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]796 #[derivative(Debug(format_with = "bounded::vec_debug"))]797 798 pub properties: CollectionPropertiesVec,799}800801802#[derive(Encode, Decode, MaxEncodedLen, Default, Debug, Clone, PartialEq, TypeInfo)]803#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]804pub struct CreateFungibleData {805 806 pub value: u128,807}808809810#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]811#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]812#[derivative(Debug)]813pub struct CreateReFungibleData {814 815 pub pieces: u128,816817 818 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]819 #[derivative(Debug(format_with = "bounded::vec_debug"))]820 pub properties: CollectionPropertiesVec,821}822823824#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]825#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]826pub enum MetaUpdatePermission {827 ItemOwner,828 Admin,829 None,830}831832833834#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]835#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]836pub enum CreateItemData {837 838 NFT(CreateNftData),839 840 Fungible(CreateFungibleData),841 842 ReFungible(CreateReFungibleData),843}844845846#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]847#[derivative(Debug)]848pub struct CreateNftExData<CrossAccountId> {849 850 #[derivative(Debug(format_with = "bounded::vec_debug"))]851 pub properties: CollectionPropertiesVec,852853 854 pub owner: CrossAccountId,855}856857858#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]859#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]860pub struct CreateRefungibleExMultipleOwners<CrossAccountId> {861 #[derivative(Debug(format_with = "bounded::map_debug"))]862 pub users: BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,863 #[derivative(Debug(format_with = "bounded::vec_debug"))]864 pub properties: CollectionPropertiesVec,865}866867868#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]869#[derivative(Debug(bound = "CrossAccountId: fmt::Debug"))]870pub struct CreateRefungibleExSingleOwner<CrossAccountId> {871 pub user: CrossAccountId,872 pub pieces: u128,873 #[derivative(Debug(format_with = "bounded::vec_debug"))]874 pub properties: CollectionPropertiesVec,875}876877878#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]879#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]880pub enum CreateItemExData<CrossAccountId> {881 882 NFT(883 #[derivative(Debug(format_with = "bounded::vec_debug"))]884 BoundedVec<CreateNftExData<CrossAccountId>, ConstU32<MAX_ITEMS_PER_BATCH>>,885 ),886887 888 Fungible(889 #[derivative(Debug(format_with = "bounded::map_debug"))]890 BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,891 ),892893 894 895 RefungibleMultipleItems(896 #[derivative(Debug(format_with = "bounded::vec_debug"))]897 BoundedVec<CreateRefungibleExSingleOwner<CrossAccountId>, ConstU32<MAX_ITEMS_PER_BATCH>>,898 ),899900 901 902 RefungibleMultipleOwners(CreateRefungibleExMultipleOwners<CrossAccountId>),903}904905impl From<CreateNftData> for CreateItemData {906 fn from(item: CreateNftData) -> Self {907 CreateItemData::NFT(item)908 }909}910911impl From<CreateReFungibleData> for CreateItemData {912 fn from(item: CreateReFungibleData) -> Self {913 CreateItemData::ReFungible(item)914 }915}916917impl From<CreateFungibleData> for CreateItemData {918 fn from(item: CreateFungibleData) -> Self {919 CreateItemData::Fungible(item)920 }921}922923924#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]925#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]926927pub struct TokenChild {928 929 pub token: TokenId,930931 932 pub collection: CollectionId,933}934935936#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug, TypeInfo)]937#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]938pub struct CollectionStats {939 940 pub created: u32,941942 943 pub destroyed: u32,944945 946 pub alive: u32,947}948949950#[derive(Encode, Decode, Clone, Debug)]951#[cfg_attr(feature = "std", derive(PartialEq))]952pub struct PhantomType<T>(core::marker::PhantomData<T>);953954impl<T: TypeInfo + 'static> TypeInfo for PhantomType<T> {955 type Identity = PhantomType<T>;956957 fn type_info() -> scale_info::Type {958 use scale_info::{959 Type, Path,960 build::{FieldsBuilder, UnnamedFields},961 type_params,962 };963 Type::builder()964 .path(Path::new("up_data_structs", "PhantomType"))965 .type_params(type_params!(T))966 .composite(<FieldsBuilder<UnnamedFields>>::default().field(|b| b.ty::<[T; 0]>()))967 }968}969impl<T> MaxEncodedLen for PhantomType<T> {970 fn max_encoded_len() -> usize {971 0972 }973}974975976pub type BoundedBytes<S> = BoundedVec<u8, S>;977978979pub type AuxPropertyValue = BoundedBytes<ConstU32<MAX_AUX_PROPERTY_VALUE_LENGTH>>;980981982pub type PropertyKey = BoundedBytes<ConstU32<MAX_PROPERTY_KEY_LENGTH>>;983984985pub type PropertyValue = BoundedBytes<ConstU32<MAX_PROPERTY_VALUE_LENGTH>>;986987988#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone)]989#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]990pub struct PropertyPermission {991 992 993 994 pub mutable: bool,995996 997 pub collection_admin: bool,998999 1000 pub token_owner: bool,1001}10021003impl PropertyPermission {1004 1005 pub fn none() -> Self {1006 Self {1007 mutable: true,1008 collection_admin: false,1009 token_owner: false,1010 }1011 }1012}101310141015#[derive(Encode, Decode, Debug, TypeInfo, Clone, PartialEq, MaxEncodedLen)]1016#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]1017pub struct Property {1018 1019 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]1020 pub key: PropertyKey,10211022 1023 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]1024 pub value: PropertyValue,1025}10261027impl Into<(PropertyKey, PropertyValue)> for Property {1028 fn into(self) -> (PropertyKey, PropertyValue) {1029 (self.key, self.value)1030 }1031}103210331034#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone)]1035#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]1036pub struct PropertyKeyPermission {1037 1038 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]1039 pub key: PropertyKey,10401041 1042 pub permission: PropertyPermission,1043}10441045impl Into<(PropertyKey, PropertyPermission)> for PropertyKeyPermission {1046 fn into(self) -> (PropertyKey, PropertyPermission) {1047 (self.key, self.permission)1048 }1049}105010511052#[derive(Debug)]1053pub enum PropertiesError {1054 1055 1056 1057 1058 NoSpaceForProperty,10591060 1061 1062 1063 PropertyLimitReached,10641065 1066 InvalidCharacterInPropertyKey,10671068 1069 1070 1071 PropertyKeyIsTooLong,10721073 1074 EmptyPropertyKey,1075}10761077107810791080#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, PartialEq, Clone, Copy)]1081pub enum PropertyScope {1082 None,1083 Rmrk,1084}10851086impl PropertyScope {1087 1088 pub fn apply(self, key: PropertyKey) -> Result<PropertyKey, PropertiesError> {1089 let scope_str: &[u8] = match self {1090 Self::None => return Ok(key),1091 Self::Rmrk => b"rmrk",1092 };10931094 [scope_str, b":", key.as_slice()]1095 .concat()1096 .try_into()1097 .map_err(|_| PropertiesError::PropertyKeyIsTooLong)1098 }1099}110011011102pub trait TrySetProperty: Sized {1103 type Value;11041105 1106 fn try_scoped_set(1107 &mut self,1108 scope: PropertyScope,1109 key: PropertyKey,1110 value: Self::Value,1111 ) -> Result<(), PropertiesError>;11121113 1114 fn try_scoped_set_from_iter<I, KV>(1115 &mut self,1116 scope: PropertyScope,1117 iter: I,1118 ) -> Result<(), PropertiesError>1119 where1120 I: Iterator<Item = KV>,1121 KV: Into<(PropertyKey, Self::Value)>,1122 {1123 for kv in iter {1124 let (key, value) = kv.into();1125 self.try_scoped_set(scope, key, value)?;1126 }11271128 Ok(())1129 }11301131 1132 fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError> {1133 self.try_scoped_set(PropertyScope::None, key, value)1134 }11351136 1137 fn try_set_from_iter<I, KV>(&mut self, iter: I) -> Result<(), PropertiesError>1138 where1139 I: Iterator<Item = KV>,1140 KV: Into<(PropertyKey, Self::Value)>,1141 {1142 self.try_scoped_set_from_iter(PropertyScope::None, iter)1143 }1144}114511461147#[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)]1148#[derivative(Default(bound = ""))]1149pub struct PropertiesMap<Value>(1150 BoundedBTreeMap<PropertyKey, Value, ConstU32<MAX_PROPERTIES_PER_ITEM>>,1151);11521153impl<Value> PropertiesMap<Value> {1154 1155 pub fn new() -> Self {1156 Self(BoundedBTreeMap::new())1157 }11581159 1160 pub fn remove(&mut self, key: &PropertyKey) -> Result<Option<Value>, PropertiesError> {1161 Self::check_property_key(key)?;11621163 Ok(self.0.remove(key))1164 }11651166 1167 pub fn get(&self, key: &PropertyKey) -> Option<&Value> {1168 self.0.get(key)1169 }11701171 1172 pub fn contains_key(&self, key: &PropertyKey) -> bool {1173 self.0.contains_key(key)1174 }11751176 1177 fn check_property_key(key: &PropertyKey) -> Result<(), PropertiesError> {1178 if key.is_empty() {1179 return Err(PropertiesError::EmptyPropertyKey);1180 }11811182 for byte in key.as_slice().iter() {1183 let byte = *byte;11841185 if !byte.is_ascii_alphanumeric() && byte != b'_' && byte != b'-' && byte != b'.' {1186 return Err(PropertiesError::InvalidCharacterInPropertyKey);1187 }1188 }11891190 Ok(())1191 }1192}11931194impl<Value> IntoIterator for PropertiesMap<Value> {1195 type Item = (PropertyKey, Value);1196 type IntoIter = <1197 BoundedBTreeMap<1198 PropertyKey,1199 Value,1200 ConstU32<MAX_PROPERTIES_PER_ITEM>1201 > as IntoIterator1202 >::IntoIter;12031204 fn into_iter(self) -> Self::IntoIter {1205 self.0.into_iter()1206 }1207}12081209impl<Value> TrySetProperty for PropertiesMap<Value> {1210 type Value = Value;12111212 fn try_scoped_set(1213 &mut self,1214 scope: PropertyScope,1215 key: PropertyKey,1216 value: Self::Value,1217 ) -> Result<(), PropertiesError> {1218 Self::check_property_key(&key)?;12191220 let key = scope.apply(key)?;1221 self.01222 .try_insert(key, value)1223 .map_err(|_| PropertiesError::PropertyLimitReached)?;12241225 Ok(())1226 }1227}122812291230pub type PropertiesPermissionMap = PropertiesMap<PropertyPermission>;123112321233#[derive(Encode, Decode, TypeInfo, Clone, PartialEq, MaxEncodedLen)]1234pub struct Properties {1235 map: PropertiesMap<PropertyValue>,1236 consumed_space: u32,1237 space_limit: u32,1238}12391240impl Properties {1241 1242 pub fn new(space_limit: u32) -> Self {1243 Self {1244 map: PropertiesMap::new(),1245 consumed_space: 0,1246 space_limit,1247 }1248 }12491250 1251 pub fn remove(&mut self, key: &PropertyKey) -> Result<Option<PropertyValue>, PropertiesError> {1252 let value = self.map.remove(key)?;12531254 if let Some(ref value) = value {1255 let value_len = value.len() as u32;1256 self.consumed_space -= value_len;1257 }12581259 Ok(value)1260 }12611262 1263 pub fn get(&self, key: &PropertyKey) -> Option<&PropertyValue> {1264 self.map.get(key)1265 }1266}12671268impl IntoIterator for Properties {1269 type Item = (PropertyKey, PropertyValue);1270 type IntoIter = <PropertiesMap<PropertyValue> as IntoIterator>::IntoIter;12711272 fn into_iter(self) -> Self::IntoIter {1273 self.map.into_iter()1274 }1275}12761277impl TrySetProperty for Properties {1278 type Value = PropertyValue;12791280 fn try_scoped_set(1281 &mut self,1282 scope: PropertyScope,1283 key: PropertyKey,1284 value: Self::Value,1285 ) -> Result<(), PropertiesError> {1286 let value_len = value.len();12871288 if self.consumed_space as usize + value_len > self.space_limit as usize1289 && !cfg!(feature = "runtime-benchmarks")1290 {1291 return Err(PropertiesError::NoSpaceForProperty);1292 }12931294 self.map.try_scoped_set(scope, key, value)?;12951296 self.consumed_space += value_len as u32;12971298 Ok(())1299 }1300}130113021303pub struct CollectionProperties;13041305impl Get<Properties> for CollectionProperties {1306 fn get() -> Properties {1307 Properties::new(MAX_COLLECTION_PROPERTIES_SIZE)1308 }1309}131013111312pub struct TokenProperties;13131314impl Get<Properties> for TokenProperties {1315 fn get() -> Properties {1316 Properties::new(MAX_TOKEN_PROPERTIES_SIZE)1317 }1318}1319132013211322parameter_types! {1323 #[derive(PartialEq, TypeInfo)]1324 pub const RmrkStringLimit: u32 = 128;1325 #[derive(PartialEq)]1326 pub const RmrkCollectionSymbolLimit: u32 = MAX_TOKEN_PREFIX_LENGTH;1327 #[derive(PartialEq)]1328 pub const RmrkResourceSymbolLimit: u32 = 10;1329 #[derive(PartialEq)]1330 pub const RmrkBaseSymbolLimit: u32 = MAX_TOKEN_PREFIX_LENGTH;1331 #[derive(PartialEq)]1332 pub const RmrkKeyLimit: u32 = 32;1333 #[derive(PartialEq)]1334 pub const RmrkValueLimit: u32 = 256;1335 #[derive(PartialEq)]1336 pub const RmrkMaxCollectionsEquippablePerPart: u32 = 100;1337 #[derive(PartialEq)]1338 pub const MaxPropertiesPerTheme: u32 = 5;1339 #[derive(PartialEq)]1340 pub const RmrkPartsLimit: u32 = 25;1341 #[derive(PartialEq)]1342 pub const RmrkMaxPriorities: u32 = 25;1343 #[derive(PartialEq)]1344 pub const MaxResourcesOnMint: u32 = 100;1345}13461347impl From<RmrkCollectionId> for CollectionId {1348 fn from(id: RmrkCollectionId) -> Self {1349 Self(id)1350 }1351}13521353impl From<RmrkNftId> for TokenId {1354 fn from(id: RmrkNftId) -> Self {1355 Self(id)1356 }1357}13581359pub type RmrkCollectionInfo<AccountId> =1360 CollectionInfo<RmrkString, RmrkCollectionSymbol, AccountId>;1361pub type RmrkInstanceInfo<AccountId> = NftInfo<AccountId, Permill, RmrkString>;1362pub type RmrkResourceInfo = ResourceInfo<RmrkString, RmrkBoundedParts>;1363pub type RmrkPropertyInfo = PropertyInfo<RmrkKeyString, RmrkValueString>;1364pub type RmrkBaseInfo<AccountId> = BaseInfo<AccountId, RmrkString>;1365pub type BoundedEquippableCollectionIds =1366 BoundedVec<RmrkCollectionId, RmrkMaxCollectionsEquippablePerPart>;1367pub type RmrkPartType = PartType<RmrkString, BoundedEquippableCollectionIds>;1368pub type RmrkEquippableList = EquippableList<BoundedEquippableCollectionIds>;1369pub type RmrkThemeProperty = ThemeProperty<RmrkString>;1370pub type RmrkTheme = Theme<RmrkString, Vec<RmrkThemeProperty>>;1371pub type RmrkBoundedTheme = Theme<RmrkString, BoundedVec<RmrkThemeProperty, MaxPropertiesPerTheme>>;1372pub type RmrkResourceTypes = ResourceTypes<RmrkString, RmrkBoundedParts>;13731374pub type RmrkBasicResource = BasicResource<RmrkString>;1375pub type RmrkComposableResource = ComposableResource<RmrkString, RmrkBoundedParts>;1376pub type RmrkSlotResource = SlotResource<RmrkString>;13771378pub type RmrkString = BoundedVec<u8, RmrkStringLimit>;1379pub type RmrkCollectionSymbol = BoundedVec<u8, RmrkCollectionSymbolLimit>;1380pub type RmrkBaseSymbol = BoundedVec<u8, RmrkBaseSymbolLimit>;1381pub type RmrkKeyString = BoundedVec<u8, RmrkKeyLimit>;1382pub type RmrkValueString = BoundedVec<u8, RmrkValueLimit>;1383pub type RmrkBoundedResource = BoundedVec<u8, RmrkResourceSymbolLimit>;1384pub type RmrkBoundedParts = BoundedVec<RmrkPartId, RmrkPartsLimit>; 13851386pub type RmrkRpcString = Vec<u8>;1387pub type RmrkThemeName = RmrkRpcString;1388pub type RmrkPropertyKey = RmrkRpcString;