git.delta.rocks / unique-network / refs/commits / c12e121fed0b

difftreelog

source

primitives/nft/src/lib.rs8.1 KiBsourcehistory
12#![cfg_attr(not(feature = "std"), no_std)]34pub use serde::{Serialize, Deserialize};56use frame_system;7use sp_runtime::sp_std::prelude::Vec;8use codec::{Decode, Encode};9pub use frame_support::{10    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,23    transactional,24};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 type CollectionId = u32;32pub type TokenId = u32;33pub type DecimalPoints = u8;3435#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]36#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]37pub enum CollectionMode {38    Invalid,39    NFT,40    // decimal points41    Fungible(DecimalPoints),42    ReFungible,43}4445impl Default for CollectionMode {46    fn default() -> Self {47        Self::Invalid48    }49}5051impl Into<u8> for CollectionMode {52    fn into(self) -> u8 {53        match self {54            CollectionMode::Invalid => 0,55            CollectionMode::NFT => 1,56            CollectionMode::Fungible(_) => 2,57            CollectionMode::ReFungible => 3,58        }59    }60}6162pub trait SponsoringResolve<AccountId, Call> {63    fn resolve(64        who: &AccountId,65		call: &Call) -> Option<AccountId>;66}6768#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]69#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]70pub enum AccessMode {71    Normal,72    WhiteList,73}74impl Default for AccessMode {75    fn default() -> Self {76        Self::Normal77    }78}7980#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]81#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]82pub enum SchemaVersion {83    ImageURL,84    Unique,85}86impl Default for SchemaVersion {87    fn default() -> Self {88        Self::ImageURL89    }90}9192#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]93#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]94pub struct Ownership<AccountId> {95    pub owner: AccountId,96    pub fraction: u128,97}9899#[derive(Encode, Decode, Debug, Clone, PartialEq)]100#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]101pub enum SponsorshipState<AccountId> {102    /// The fees are applied to the transaction sender103    Disabled,104    Unconfirmed(AccountId),105    /// Transactions are sponsored by specified account106    Confirmed(AccountId),107}108109impl<AccountId> SponsorshipState<AccountId> {110    pub fn sponsor(&self) -> Option<&AccountId> {111        match self {112            Self::Confirmed(sponsor) => Some(sponsor),113            _ => None,114        }115    }116117    pub fn pending_sponsor(&self) -> Option<&AccountId> {118        match self {119            Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor),120            _ => None,121        }122    }123124    pub fn confirmed(&self) -> bool {125        matches!(self, Self::Confirmed(_))126    }127}128129impl<T> Default for SponsorshipState<T> {130    fn default() -> Self {131        Self::Disabled132    }133}134135#[derive(Encode, Decode, Clone, PartialEq)]136#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]137pub struct Collection<T: frame_system::Config> {138    pub owner: T::AccountId,139    pub mode: CollectionMode,140    pub access: AccessMode,141    pub decimal_points: DecimalPoints,142    pub name: Vec<u16>,        // 64 include null escape char143    pub description: Vec<u16>, // 256 include null escape char144    pub token_prefix: Vec<u8>, // 16 include null escape char145    pub mint_mode: bool,146    pub offchain_schema: Vec<u8>,147    pub schema_version: SchemaVersion,148    pub sponsorship: SponsorshipState<T::AccountId>,149    pub limits: CollectionLimits<T::BlockNumber>, // Collection private restrictions 150    pub variable_on_chain_schema: Vec<u8>, //151    pub const_on_chain_schema: Vec<u8>, //152}153154#[derive(Encode, Decode, Debug, Clone, PartialEq)]155#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]156pub struct NftItemType<AccountId> {157    pub owner: AccountId,158    pub const_data: Vec<u8>,159    pub variable_data: Vec<u8>,160}161162#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]163#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]164pub struct FungibleItemType {165    pub value: u128,166}167168#[derive(Encode, Decode, Debug, Clone, PartialEq)]169#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]170pub struct ReFungibleItemType<AccountId> {171    pub owner: Vec<Ownership<AccountId>>,172    pub const_data: Vec<u8>,173    pub variable_data: Vec<u8>,174}175176177#[derive(Encode, Decode, Debug, Clone, PartialEq)]178#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]179pub struct CollectionLimits<BlockNumber: Encode + Decode> {180    pub account_token_ownership_limit: u32,181    pub sponsored_data_size: u32,182    /// None - setVariableMetadata is not sponsored183    /// Some(v) - setVariableMetadata is sponsored 184    ///           if there is v block between txs185    pub sponsored_data_rate_limit: Option<BlockNumber>,186    pub token_limit: u32,187188    // Timeouts for item types in passed blocks189    pub sponsor_transfer_timeout: u32,190    pub owner_can_transfer: bool,191    pub owner_can_destroy: bool,192}193194impl<BlockNumber: Encode + Decode> Default for CollectionLimits<BlockNumber> {195    fn default() -> Self {196        Self { 197            account_token_ownership_limit: 10_000_000, 198            token_limit: u32::max_value(),199            sponsored_data_size: u32::MAX, 200            sponsored_data_rate_limit: None,201            sponsor_transfer_timeout: 14400,202            owner_can_transfer: true,203            owner_can_destroy: true204        }205    }206}207208#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]209#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]210pub struct ChainLimits {211    pub collection_numbers_limit: u32,212    pub account_token_ownership_limit: u32,213    pub collections_admins_limit: u64,214    pub custom_data_limit: u32,215216    // Timeouts for item types in passed blocks217    pub nft_sponsor_transfer_timeout: u32,218    pub fungible_sponsor_transfer_timeout: u32,219    pub refungible_sponsor_transfer_timeout: u32,220221    // Schema limits222    pub offchain_schema_limit: u32,223    pub variable_on_chain_schema_limit: u32,224    pub const_on_chain_schema_limit: u32,225}226227228#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]229#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]230pub struct CreateNftData {231    pub const_data: Vec<u8>,232    pub variable_data: Vec<u8>,233}234235#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]236#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]237pub struct CreateFungibleData {238    pub value: u128,239}240241#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]242#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]243pub struct CreateReFungibleData {244    pub const_data: Vec<u8>,245    pub variable_data: Vec<u8>,246    pub pieces: u128,247}248249#[derive(Encode, Decode, Debug, Clone, PartialEq)]250#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]251pub enum CreateItemData {252    NFT(CreateNftData),253    Fungible(CreateFungibleData),254    ReFungible(CreateReFungibleData),255}256257impl CreateItemData {258    pub fn len(&self) -> usize {259        let len = match self {260            CreateItemData::NFT(data) => data.variable_data.len() + data.const_data.len(),261            CreateItemData::ReFungible(data) => data.variable_data.len() + data.const_data.len(),262            _ => 0263        };264        265        return len;266    }267}268269impl From<CreateNftData> for CreateItemData {270    fn from(item: CreateNftData) -> Self {271        CreateItemData::NFT(item)272    }273}274275impl From<CreateReFungibleData> for CreateItemData {276    fn from(item: CreateReFungibleData) -> Self {277        CreateItemData::ReFungible(item)278    }279}280281impl From<CreateFungibleData> for CreateItemData {282    fn from(item: CreateFungibleData) -> Self {283        CreateItemData::Fungible(item)284    }285}