git.delta.rocks / unique-network / refs/commits / 6b16ace53bf5

difftreelog

source

primitives/nft/src/lib.rs7.5 KiBsourcehistory
1#![cfg_attr(not(feature = "std"), no_std)]23pub use serde::{Serialize, Deserialize};45use sp_runtime::sp_std::prelude::Vec;6use codec::{Decode, Encode};7pub use frame_support::{8	construct_runtime, decl_event, decl_module, decl_storage, decl_error,9	dispatch::DispatchResult,10	ensure, fail, parameter_types,11	traits::{12		Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced,13		Randomness, IsSubType, WithdrawReasons,14	},15	weights::{16		constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},17		DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight,18		WeightToFeePolynomial, DispatchClass,19	},20	StorageValue, transactional,21};2223pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;24pub const MAX_REFUNGIBLE_PIECES: u128 = 1_000_000_000_000_000_000_000;25pub const MAX_SPONSOR_TIMEOUT: u32 = 10_368_000;26pub const MAX_TOKEN_OWNERSHIP: u32 = 10_000_000;2728pub type CollectionId = u32;29pub type TokenId = u32;30pub type DecimalPoints = u8;3132#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]33#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]34pub enum CollectionMode {35	Invalid,36	NFT,37	// decimal points38	Fungible(DecimalPoints),39	ReFungible,40}4142impl Default for CollectionMode {43	fn default() -> Self {44		Self::Invalid45	}46}4748impl CollectionMode {49	pub fn id(&self) -> u8 {50		match self {51			CollectionMode::Invalid => 0,52			CollectionMode::NFT => 1,53			CollectionMode::Fungible(_) => 2,54			CollectionMode::ReFungible => 3,55		}56	}57}5859pub trait SponsoringResolve<AccountId, Call> {60	fn resolve(who: &AccountId, call: &Call) -> Option<AccountId>;61}6263#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]64#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]65pub enum AccessMode {66	Normal,67	WhiteList,68}69impl Default for AccessMode {70	fn default() -> Self {71		Self::Normal72	}73}7475#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]76#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]77pub enum SchemaVersion {78	ImageURL,79	Unique,80}81impl Default for SchemaVersion {82	fn default() -> Self {83		Self::ImageURL84	}85}8687#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]88#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]89pub struct Ownership<AccountId> {90	pub owner: AccountId,91	pub fraction: u128,92}9394#[derive(Encode, Decode, Debug, Clone, PartialEq)]95#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]96pub enum SponsorshipState<AccountId> {97	/// The fees are applied to the transaction sender98	Disabled,99	Unconfirmed(AccountId),100	/// Transactions are sponsored by specified account101	Confirmed(AccountId),102}103104impl<AccountId> SponsorshipState<AccountId> {105	pub fn sponsor(&self) -> Option<&AccountId> {106		match self {107			Self::Confirmed(sponsor) => Some(sponsor),108			_ => None,109		}110	}111112	pub fn pending_sponsor(&self) -> Option<&AccountId> {113		match self {114			Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor),115			_ => None,116		}117	}118119	pub fn confirmed(&self) -> bool {120		matches!(self, Self::Confirmed(_))121	}122}123124impl<T> Default for SponsorshipState<T> {125	fn default() -> Self {126		Self::Disabled127	}128}129130#[derive(Encode, Decode, Clone, PartialEq)]131#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]132pub struct Collection<T: frame_system::Config> {133	pub owner: T::AccountId,134	pub mode: CollectionMode,135	pub access: AccessMode,136	pub decimal_points: DecimalPoints,137	pub name: Vec<u16>,        // 64 include null escape char138	pub description: Vec<u16>, // 256 include null escape char139	pub token_prefix: Vec<u8>, // 16 include null escape char140	pub mint_mode: bool,141	pub offchain_schema: Vec<u8>,142	pub schema_version: SchemaVersion,143	pub sponsorship: SponsorshipState<T::AccountId>,144	pub limits: CollectionLimits<T::BlockNumber>, // Collection private restrictions145	pub variable_on_chain_schema: Vec<u8>,        //146	pub const_on_chain_schema: Vec<u8>,           //147	pub transfers_enabled: bool,148}149150#[derive(Encode, Decode, Debug, Clone, PartialEq)]151#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]152pub struct NftItemType<AccountId> {153	pub owner: AccountId,154	pub const_data: Vec<u8>,155	pub variable_data: Vec<u8>,156}157158#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]159#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]160pub struct FungibleItemType {161	pub value: u128,162}163164#[derive(Encode, Decode, Debug, Clone, PartialEq)]165#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]166pub struct ReFungibleItemType<AccountId> {167	pub owner: Vec<Ownership<AccountId>>,168	pub const_data: Vec<u8>,169	pub variable_data: Vec<u8>,170}171172#[derive(Encode, Decode, Debug, Clone, PartialEq)]173#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]174pub struct CollectionLimits<BlockNumber: Encode + Decode> {175	pub account_token_ownership_limit: u32,176	pub sponsored_data_size: u32,177	/// None - setVariableMetadata is not sponsored178	/// Some(v) - setVariableMetadata is sponsored179	///           if there is v block between txs180	pub sponsored_data_rate_limit: Option<BlockNumber>,181	pub token_limit: u32,182183	// Timeouts for item types in passed blocks184	pub sponsor_transfer_timeout: u32,185	pub owner_can_transfer: bool,186	pub owner_can_destroy: bool,187}188189impl<BlockNumber: Encode + Decode> Default for CollectionLimits<BlockNumber> {190	fn default() -> Self {191		Self {192			account_token_ownership_limit: 10_000_000,193			token_limit: u32::max_value(),194			sponsored_data_size: u32::MAX,195			sponsored_data_rate_limit: None,196			sponsor_transfer_timeout: 14400,197			owner_can_transfer: true,198			owner_can_destroy: true,199		}200	}201}202203#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]204#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]205pub struct ChainLimits {206	pub collection_numbers_limit: u32,207	pub account_token_ownership_limit: u32,208	pub collections_admins_limit: u64,209	pub custom_data_limit: u32,210211	// Timeouts for item types in passed blocks212	pub nft_sponsor_transfer_timeout: u32,213	pub fungible_sponsor_transfer_timeout: u32,214	pub refungible_sponsor_transfer_timeout: u32,215216	// Schema limits217	pub offchain_schema_limit: u32,218	pub variable_on_chain_schema_limit: u32,219	pub const_on_chain_schema_limit: u32,220}221222#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]223#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]224pub struct CreateNftData {225	pub const_data: Vec<u8>,226	pub variable_data: Vec<u8>,227}228229#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]230#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]231pub struct CreateFungibleData {232	pub value: u128,233}234235#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]236#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]237pub struct CreateReFungibleData {238	pub const_data: Vec<u8>,239	pub variable_data: Vec<u8>,240	pub pieces: u128,241}242243#[derive(Encode, Decode, Debug, Clone, PartialEq)]244#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]245pub enum CreateItemData {246	NFT(CreateNftData),247	Fungible(CreateFungibleData),248	ReFungible(CreateReFungibleData),249}250251impl CreateItemData {252	pub fn data_size(&self) -> usize {253		match self {254			CreateItemData::NFT(data) => data.variable_data.len() + data.const_data.len(),255			CreateItemData::ReFungible(data) => data.variable_data.len() + data.const_data.len(),256			_ => 0,257		}258	}259}260261impl From<CreateNftData> for CreateItemData {262	fn from(item: CreateNftData) -> Self {263		CreateItemData::NFT(item)264	}265}266267impl From<CreateReFungibleData> for CreateItemData {268	fn from(item: CreateReFungibleData) -> Self {269		CreateItemData::ReFungible(item)270	}271}272273impl From<CreateFungibleData> for CreateItemData {274	fn from(item: CreateFungibleData) -> Self {275		CreateItemData::Fungible(item)276	}277}