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

difftreelog

source

primitives/src/lib.rs8.1 KiBsourcehistory
12#![cfg_attr(not(feature = "std"), no_std)]34#[cfg(feature = "std")]5pub use serde::*;67use frame_system;8use sp_runtime::sp_std::prelude::Vec;9use codec::{Decode, Encode};10pub use frame_support::{11    construct_runtime, decl_event, decl_module, decl_storage, decl_error,12    dispatch::DispatchResult,13    ensure, fail, parameter_types,14    traits::{15        Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced,16        Randomness, IsSubType, WithdrawReasons,17    },18    weights::{19        constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},20        DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight,21        WeightToFeePolynomial, DispatchClass,22    },23    StorageValue,24    transactional,25};2627pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;28pub const MAX_REFUNGIBLE_PIECES: u128 = 1_000_000_000_000_000_000_000;29pub const MAX_SPONSOR_TIMEOUT: u32 = 10_368_000;30pub const MAX_TOKEN_OWNERSHIP: u32 = 10_000_000;3132pub type CollectionId = u32;33pub type TokenId = u32;34pub type DecimalPoints = u8;3536#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]37#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]38pub enum CollectionMode {39    Invalid,40    NFT,41    // decimal points42    Fungible(DecimalPoints),43    ReFungible,44}4546impl Default for CollectionMode {47    fn default() -> Self {48        Self::Invalid49    }50}5152impl Into<u8> for CollectionMode {53    fn into(self) -> u8 {54        match self {55            CollectionMode::Invalid => 0,56            CollectionMode::NFT => 1,57            CollectionMode::Fungible(_) => 2,58            CollectionMode::ReFungible => 3,59        }60    }61}6263pub trait SponsoringResolve<AccountId, Call> {64    fn resolve(65        who: &AccountId,66		call: &Call) -> Option<AccountId>;67}6869#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]70#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]71pub enum AccessMode {72    Normal,73    WhiteList,74}75impl Default for AccessMode {76    fn default() -> Self {77        Self::Normal78    }79}8081#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]82#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]83pub enum SchemaVersion {84    ImageURL,85    Unique,86}87impl Default for SchemaVersion {88    fn default() -> Self {89        Self::ImageURL90    }91}9293#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]94#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]95pub struct Ownership<AccountId> {96    pub owner: AccountId,97    pub fraction: u128,98}99100#[derive(Encode, Decode, Debug, Clone, PartialEq)]101#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]102pub enum SponsorshipState<AccountId> {103    /// The fees are applied to the transaction sender104    Disabled,105    Unconfirmed(AccountId),106    /// Transactions are sponsored by specified account107    Confirmed(AccountId),108}109110impl<AccountId> SponsorshipState<AccountId> {111    pub fn sponsor(&self) -> Option<&AccountId> {112        match self {113            Self::Confirmed(sponsor) => Some(sponsor),114            _ => None,115        }116    }117118    pub fn pending_sponsor(&self) -> Option<&AccountId> {119        match self {120            Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor),121            _ => None,122        }123    }124125    pub fn confirmed(&self) -> bool {126        matches!(self, Self::Confirmed(_))127    }128}129130impl<T> Default for SponsorshipState<T> {131    fn default() -> Self {132        Self::Disabled133    }134}135136#[derive(Encode, Decode, Clone, PartialEq)]137#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]138pub struct Collection<T: frame_system::Config> {139    pub owner: T::AccountId,140    pub mode: CollectionMode,141    pub access: AccessMode,142    pub decimal_points: DecimalPoints,143    pub name: Vec<u16>,        // 64 include null escape char144    pub description: Vec<u16>, // 256 include null escape char145    pub token_prefix: Vec<u8>, // 16 include null escape char146    pub mint_mode: bool,147    pub offchain_schema: Vec<u8>,148    pub schema_version: SchemaVersion,149    pub sponsorship: SponsorshipState<T::AccountId>,150    pub limits: CollectionLimits<T::BlockNumber>, // Collection private restrictions 151    pub variable_on_chain_schema: Vec<u8>, //152    pub const_on_chain_schema: Vec<u8>, //153}154155#[derive(Encode, Decode, Debug, Clone, PartialEq)]156#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]157pub struct NftItemType<AccountId> {158    pub owner: AccountId,159    pub const_data: Vec<u8>,160    pub variable_data: Vec<u8>,161}162163#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]164#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]165pub struct FungibleItemType {166    pub value: u128,167}168169#[derive(Encode, Decode, Debug, Clone, PartialEq)]170#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]171pub struct ReFungibleItemType<AccountId> {172    pub owner: Vec<Ownership<AccountId>>,173    pub const_data: Vec<u8>,174    pub variable_data: Vec<u8>,175}176177178#[derive(Encode, Decode, Debug, Clone, PartialEq)]179#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]180pub struct CollectionLimits<BlockNumber: Encode + Decode> {181    pub account_token_ownership_limit: u32,182    pub sponsored_data_size: u32,183    /// None - setVariableMetadata is not sponsored184    /// Some(v) - setVariableMetadata is sponsored 185    ///           if there is v block between txs186    pub sponsored_data_rate_limit: Option<BlockNumber>,187    pub token_limit: u32,188189    // Timeouts for item types in passed blocks190    pub sponsor_transfer_timeout: u32,191    pub owner_can_transfer: bool,192    pub owner_can_destroy: bool,193}194195impl<BlockNumber: Encode + Decode> Default for CollectionLimits<BlockNumber> {196    fn default() -> Self {197        Self { 198            account_token_ownership_limit: 10_000_000, 199            token_limit: u32::max_value(),200            sponsored_data_size: u32::MAX, 201            sponsored_data_rate_limit: None,202            sponsor_transfer_timeout: 14400,203            owner_can_transfer: true,204            owner_can_destroy: true205        }206    }207}208209#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]210#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]211pub struct ChainLimits {212    pub collection_numbers_limit: u32,213    pub account_token_ownership_limit: u32,214    pub collections_admins_limit: u64,215    pub custom_data_limit: u32,216217    // Timeouts for item types in passed blocks218    pub nft_sponsor_transfer_timeout: u32,219    pub fungible_sponsor_transfer_timeout: u32,220    pub refungible_sponsor_transfer_timeout: u32,221222    // Schema limits223    pub offchain_schema_limit: u32,224    pub variable_on_chain_schema_limit: u32,225    pub const_on_chain_schema_limit: u32,226}227228229#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]230#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]231pub struct CreateNftData {232    pub const_data: Vec<u8>,233    pub variable_data: Vec<u8>,234}235236#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]237#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]238pub struct CreateFungibleData {239    pub value: u128,240}241242#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]243#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]244pub struct CreateReFungibleData {245    pub const_data: Vec<u8>,246    pub variable_data: Vec<u8>,247    pub pieces: u128,248}249250#[derive(Encode, Decode, Debug, Clone, PartialEq)]251#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]252pub enum CreateItemData {253    NFT(CreateNftData),254    Fungible(CreateFungibleData),255    ReFungible(CreateReFungibleData),256}257258impl CreateItemData {259    pub fn len(&self) -> usize {260        let len = match self {261            CreateItemData::NFT(data) => data.variable_data.len() + data.const_data.len(),262            CreateItemData::ReFungible(data) => data.variable_data.len() + data.const_data.len(),263            _ => 0264        };265        266        return len;267    }268}269270impl From<CreateNftData> for CreateItemData {271    fn from(item: CreateNftData) -> Self {272        CreateItemData::NFT(item)273    }274}275276impl From<CreateReFungibleData> for CreateItemData {277    fn from(item: CreateReFungibleData) -> Self {278        CreateItemData::ReFungible(item)279    }280}281282impl From<CreateFungibleData> for CreateItemData {283    fn from(item: CreateFungibleData) -> Self {284        CreateItemData::Fungible(item)285    }286}