git.delta.rocks / unique-network / refs/commits / 44bd99aa7cbe

difftreelog

source

primitives/nft/src/lib.rs9.6 KiBsourcehistory
1#![cfg_attr(not(feature = "std"), no_std)]23#[cfg(feature = "serde")]4pub use serde::{Serialize, Deserialize};56use sp_runtime::sp_std::prelude::Vec;7use codec::{Decode, Encode};8use max_encoded_len::MaxEncodedLen;9pub use frame_support::{10	BoundedVec, construct_runtime, decl_event, decl_module, decl_storage, decl_error,11	dispatch::DispatchResult,12	ensure, fail, parameter_types,13	traits::{14		Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced,15		Randomness, IsSubType, WithdrawReasons,16	},17	weights::{18		constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},19		DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight,20		WeightToFeePolynomial, DispatchClass,21	},22	StorageValue, transactional,23};24use derivative::Derivative;2526pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;27pub const MAX_REFUNGIBLE_PIECES: u128 = 1_000_000_000_000_000_000_000;28pub const MAX_SPONSOR_TIMEOUT: u32 = 10_368_000;29pub const MAX_TOKEN_OWNERSHIP: u32 = 10_000_000;3031pub const COLLECTION_NUMBER_LIMIT: u32 = if cfg!(not(feature = "limit-testing")) {32	10000033} else {34	1035};36pub const CUSTOM_DATA_LIMIT: u32 = if cfg!(not(feature = "limit-testing")) {37	204838} else {39	1040};41pub const COLLECTION_ADMINS_LIMIT: u64 = 5;42pub const ACCOUNT_TOKEN_OWNERSHIP_LIMIT: u32 = if cfg!(not(feature = "limit-testing")) {43	100000044} else {45	1046};4748// Timeouts for item types in passed blocks49pub const NFT_SPONSOR_TRANSFER_TIMEOUT: u32 = 5;50pub const FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT: u32 = 5;51pub const REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT: u32 = 5;5253// Schema limits54pub const OFFCHAIN_SCHEMA_LIMIT: u32 = 1024;55pub const VARIABLE_ON_CHAIN_SCHEMA_LIMIT: u32 = 1024;56pub const CONST_ON_CHAIN_SCHEMA_LIMIT: u32 = 1024;5758pub const MAX_COLLECTION_NAME_LENGTH: usize = 64;59pub const MAX_COLLECTION_DESCRIPTION_LENGTH: usize = 256;60pub const MAX_TOKEN_PREFIX_LENGTH: usize = 16;6162/// How much items can be created per single63/// create_many call64pub const MAX_ITEMS_PER_BATCH: u32 = 200;6566parameter_types! {67	pub const CustomDataLimit: u32 = CUSTOM_DATA_LIMIT;68}6970pub type CollectionId = u32;71pub type TokenId = u32;72pub type DecimalPoints = u8;7374#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]75#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]76pub enum CollectionMode {77	Invalid,78	NFT,79	// decimal points80	Fungible(DecimalPoints),81	ReFungible,82}8384impl Default for CollectionMode {85	fn default() -> Self {86		Self::Invalid87	}88}8990impl CollectionMode {91	pub fn id(&self) -> u8 {92		match self {93			CollectionMode::Invalid => 0,94			CollectionMode::NFT => 1,95			CollectionMode::Fungible(_) => 2,96			CollectionMode::ReFungible => 3,97		}98	}99}100101pub trait SponsoringResolve<AccountId, Call> {102	fn resolve(who: &AccountId, call: &Call) -> Option<AccountId>;103}104105#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]106#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]107pub enum AccessMode {108	Normal,109	WhiteList,110}111impl Default for AccessMode {112	fn default() -> Self {113		Self::Normal114	}115}116117#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]118#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]119pub enum SchemaVersion {120	ImageURL,121	Unique,122}123impl Default for SchemaVersion {124	fn default() -> Self {125		Self::ImageURL126	}127}128129#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]130#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]131pub struct Ownership<AccountId> {132	pub owner: AccountId,133	pub fraction: u128,134}135136#[derive(Encode, Decode, Debug, Clone, PartialEq)]137#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]138pub enum SponsorshipState<AccountId> {139	/// The fees are applied to the transaction sender140	Disabled,141	Unconfirmed(AccountId),142	/// Transactions are sponsored by specified account143	Confirmed(AccountId),144}145146impl<AccountId> SponsorshipState<AccountId> {147	pub fn sponsor(&self) -> Option<&AccountId> {148		match self {149			Self::Confirmed(sponsor) => Some(sponsor),150			_ => None,151		}152	}153154	pub fn pending_sponsor(&self) -> Option<&AccountId> {155		match self {156			Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor),157			_ => None,158		}159	}160161	pub fn confirmed(&self) -> bool {162		matches!(self, Self::Confirmed(_))163	}164}165166impl<T> Default for SponsorshipState<T> {167	fn default() -> Self {168		Self::Disabled169	}170}171172#[derive(Encode, Decode, Clone, PartialEq)]173#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]174pub struct Collection<T: frame_system::Config> {175	pub owner: T::AccountId,176	pub mode: CollectionMode,177	pub access: AccessMode,178	pub decimal_points: DecimalPoints,179	pub name: Vec<u16>,        // 64 include null escape char180	pub description: Vec<u16>, // 256 include null escape char181	pub token_prefix: Vec<u8>, // 16 include null escape char182	pub mint_mode: bool,183	pub offchain_schema: Vec<u8>,184	pub schema_version: SchemaVersion,185	pub sponsorship: SponsorshipState<T::AccountId>,186	pub limits: CollectionLimits<T::BlockNumber>, // Collection private restrictions187	pub variable_on_chain_schema: Vec<u8>,        //188	pub const_on_chain_schema: Vec<u8>,           //189	pub transfers_enabled: bool,190}191192#[derive(Encode, Decode, Debug, Clone, PartialEq)]193#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]194pub struct NftItemType<AccountId> {195	pub owner: AccountId,196	pub const_data: Vec<u8>,197	pub variable_data: Vec<u8>,198}199200#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]201#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]202pub struct FungibleItemType {203	pub value: u128,204}205206#[derive(Encode, Decode, Debug, Clone, PartialEq)]207#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]208pub struct ReFungibleItemType<AccountId> {209	pub owner: Vec<Ownership<AccountId>>,210	pub const_data: Vec<u8>,211	pub variable_data: Vec<u8>,212}213214#[derive(Encode, Decode, Debug, Clone, PartialEq)]215#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]216pub struct CollectionLimits<BlockNumber: Encode + Decode> {217	pub account_token_ownership_limit: u32,218	pub sponsored_data_size: u32,219	/// None - setVariableMetadata is not sponsored220	/// Some(v) - setVariableMetadata is sponsored221	///           if there is v block between txs222	pub sponsored_data_rate_limit: Option<BlockNumber>,223	pub token_limit: u32,224225	// Timeouts for item types in passed blocks226	pub sponsor_transfer_timeout: u32,227	pub owner_can_transfer: bool,228	pub owner_can_destroy: bool,229}230231impl<BlockNumber: Encode + Decode> Default for CollectionLimits<BlockNumber> {232	fn default() -> Self {233		Self {234			account_token_ownership_limit: 10_000_000,235			token_limit: u32::max_value(),236			sponsored_data_size: u32::MAX,237			sponsored_data_rate_limit: None,238			sponsor_transfer_timeout: 14400,239			owner_can_transfer: true,240			owner_can_destroy: true,241		}242	}243}244245/// BoundedVec doesn't supports serde246#[cfg(feature = "serde1")]247mod bounded_serde {248	use core::convert::TryFrom;249	use frame_support::{BoundedVec, traits::Get};250	use serde::{251		ser::{self, Serialize},252		de::{self, Deserialize, Error},253	};254	use sp_std::vec::Vec;255256	pub fn serialize<D, V, S>(value: &BoundedVec<V, S>, serializer: D) -> Result<D::Ok, D::Error>257	where258		D: ser::Serializer,259		V: Serialize,260	{261		let vec: &Vec<_> = &value;262		vec.serialize(serializer)263	}264265	pub fn deserialize<'de, D, V, S>(deserializer: D) -> Result<BoundedVec<V, S>, D::Error>266	where267		D: de::Deserializer<'de>,268		V: de::Deserialize<'de>,269		S: Get<u32>,270	{271		// TODO: Implement custom visitor, which will limit vec size at parse time? Will serde only be used by chainspec?272		let vec = <Vec<V>>::deserialize(deserializer)?;273		let len = vec.len();274		TryFrom::try_from(vec).map_err(|_| D::Error::invalid_length(len, &"lesser size"))275	}276}277278#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative)]279#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]280#[derivative(Debug)]281pub struct CreateNftData {282	#[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]283	#[derivative(Debug = "ignore")]284	pub const_data: BoundedVec<u8, CustomDataLimit>,285	#[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]286	#[derivative(Debug = "ignore")]287	pub variable_data: BoundedVec<u8, CustomDataLimit>,288}289290#[derive(Encode, Decode, MaxEncodedLen, Default, Debug, Clone, PartialEq)]291#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]292pub struct CreateFungibleData {293	pub value: u128,294}295296#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative)]297#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]298#[derivative(Debug)]299pub struct CreateReFungibleData {300	#[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]301	#[derivative(Debug = "ignore")]302	pub const_data: BoundedVec<u8, CustomDataLimit>,303	#[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]304	#[derivative(Debug = "ignore")]305	pub variable_data: BoundedVec<u8, CustomDataLimit>,306	pub pieces: u128,307}308309#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, Debug)]310#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]311pub enum CreateItemData {312	NFT(CreateNftData),313	Fungible(CreateFungibleData),314	ReFungible(CreateReFungibleData),315}316317impl CreateItemData {318	pub fn data_size(&self) -> usize {319		match self {320			CreateItemData::NFT(data) => data.variable_data.len() + data.const_data.len(),321			CreateItemData::ReFungible(data) => data.variable_data.len() + data.const_data.len(),322			_ => 0,323		}324	}325}326327impl From<CreateNftData> for CreateItemData {328	fn from(item: CreateNftData) -> Self {329		CreateItemData::NFT(item)330	}331}332333impl From<CreateReFungibleData> for CreateItemData {334	fn from(item: CreateReFungibleData) -> Self {335		CreateItemData::ReFungible(item)336	}337}338339impl From<CreateFungibleData> for CreateItemData {340	fn from(item: CreateFungibleData) -> Self {341		CreateItemData::Fungible(item)342	}343}