From c12e121fed0bcf33e24e5b13c8283f167c399cc7 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 24 Jun 2021 19:20:09 +0000 Subject: [PATCH] refactor: extract sponsorship primitives --- --- a/primitives/Cargo.toml +++ /dev/null @@ -1,30 +0,0 @@ -[package] -name = "nft-data-structs" -authors = ['Substrate DevHub '] -description = "Nft data structs definitions" -edition = "2018" -license = 'GPL-3.0' -homepage = "https://substrate.dev" -repository = 'https://github.com/clover-network/clover' -version = '0.9.0' - -[dependencies] -codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ['derive'] } -serde = { version = "1.0.119", features = ['derive'], default-features = false } -frame-support = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } -frame-system = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } -pallet-contracts = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } -sp-core = { version = "3.0.0", default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } -sp-runtime = { version = "3.0.0", default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } - -[features] -default = ["std"] -std = [ - "serde/std", - "codec/std", - "frame-system/std", - "frame-support/std", - "sp-runtime/std", - "sp-core/std", - "pallet-contracts/std", -] \ No newline at end of file --- /dev/null +++ b/primitives/nft/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "nft-data-structs" +authors = ['Substrate DevHub '] +description = "Nft data structs definitions" +edition = "2018" +license = 'GPL-3.0' +homepage = "https://substrate.dev" +repository = 'https://github.com/clover-network/clover' +version = '0.9.0' + +[dependencies] +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ['derive'] } +serde = { version = "1.0.119", features = ['derive'], default-features = false } +frame-support = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } +frame-system = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } +pallet-contracts = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } +sp-core = { version = "3.0.0", default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } +sp-runtime = { version = "3.0.0", default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } + +[features] +default = ["std"] +std = [ + "serde/std", + "codec/std", + "frame-system/std", + "frame-support/std", + "sp-runtime/std", + "sp-core/std", + "pallet-contracts/std", +] \ No newline at end of file --- /dev/null +++ b/primitives/nft/src/lib.rs @@ -0,0 +1,285 @@ + +#![cfg_attr(not(feature = "std"), no_std)] + +pub use serde::{Serialize, Deserialize}; + +use frame_system; +use sp_runtime::sp_std::prelude::Vec; +use codec::{Decode, Encode}; +pub use frame_support::{ + construct_runtime, decl_event, decl_module, decl_storage, decl_error, + dispatch::DispatchResult, + ensure, fail, parameter_types, + traits::{ + Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced, + Randomness, IsSubType, WithdrawReasons, + }, + weights::{ + constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND}, + DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight, + WeightToFeePolynomial, DispatchClass, + }, + StorageValue, + transactional, +}; + +pub const MAX_DECIMAL_POINTS: DecimalPoints = 30; +pub const MAX_REFUNGIBLE_PIECES: u128 = 1_000_000_000_000_000_000_000; +pub const MAX_SPONSOR_TIMEOUT: u32 = 10_368_000; +pub const MAX_TOKEN_OWNERSHIP: u32 = 10_000_000; + +pub type CollectionId = u32; +pub type TokenId = u32; +pub type DecimalPoints = u8; + +#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub enum CollectionMode { + Invalid, + NFT, + // decimal points + Fungible(DecimalPoints), + ReFungible, +} + +impl Default for CollectionMode { + fn default() -> Self { + Self::Invalid + } +} + +impl Into for CollectionMode { + fn into(self) -> u8 { + match self { + CollectionMode::Invalid => 0, + CollectionMode::NFT => 1, + CollectionMode::Fungible(_) => 2, + CollectionMode::ReFungible => 3, + } + } +} + +pub trait SponsoringResolve { + fn resolve( + who: &AccountId, + call: &Call) -> Option; +} + +#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub enum AccessMode { + Normal, + WhiteList, +} +impl Default for AccessMode { + fn default() -> Self { + Self::Normal + } +} + +#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub enum SchemaVersion { + ImageURL, + Unique, +} +impl Default for SchemaVersion { + fn default() -> Self { + Self::ImageURL + } +} + +#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct Ownership { + pub owner: AccountId, + pub fraction: u128, +} + +#[derive(Encode, Decode, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub enum SponsorshipState { + /// The fees are applied to the transaction sender + Disabled, + Unconfirmed(AccountId), + /// Transactions are sponsored by specified account + Confirmed(AccountId), +} + +impl SponsorshipState { + pub fn sponsor(&self) -> Option<&AccountId> { + match self { + Self::Confirmed(sponsor) => Some(sponsor), + _ => None, + } + } + + pub fn pending_sponsor(&self) -> Option<&AccountId> { + match self { + Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor), + _ => None, + } + } + + pub fn confirmed(&self) -> bool { + matches!(self, Self::Confirmed(_)) + } +} + +impl Default for SponsorshipState { + fn default() -> Self { + Self::Disabled + } +} + +#[derive(Encode, Decode, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct Collection { + pub owner: T::AccountId, + pub mode: CollectionMode, + pub access: AccessMode, + pub decimal_points: DecimalPoints, + pub name: Vec, // 64 include null escape char + pub description: Vec, // 256 include null escape char + pub token_prefix: Vec, // 16 include null escape char + pub mint_mode: bool, + pub offchain_schema: Vec, + pub schema_version: SchemaVersion, + pub sponsorship: SponsorshipState, + pub limits: CollectionLimits, // Collection private restrictions + pub variable_on_chain_schema: Vec, // + pub const_on_chain_schema: Vec, // +} + +#[derive(Encode, Decode, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct NftItemType { + pub owner: AccountId, + pub const_data: Vec, + pub variable_data: Vec, +} + +#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct FungibleItemType { + pub value: u128, +} + +#[derive(Encode, Decode, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct ReFungibleItemType { + pub owner: Vec>, + pub const_data: Vec, + pub variable_data: Vec, +} + + +#[derive(Encode, Decode, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct CollectionLimits { + pub account_token_ownership_limit: u32, + pub sponsored_data_size: u32, + /// None - setVariableMetadata is not sponsored + /// Some(v) - setVariableMetadata is sponsored + /// if there is v block between txs + pub sponsored_data_rate_limit: Option, + pub token_limit: u32, + + // Timeouts for item types in passed blocks + pub sponsor_transfer_timeout: u32, + pub owner_can_transfer: bool, + pub owner_can_destroy: bool, +} + +impl Default for CollectionLimits { + fn default() -> Self { + Self { + account_token_ownership_limit: 10_000_000, + token_limit: u32::max_value(), + sponsored_data_size: u32::MAX, + sponsored_data_rate_limit: None, + sponsor_transfer_timeout: 14400, + owner_can_transfer: true, + owner_can_destroy: true + } + } +} + +#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct ChainLimits { + pub collection_numbers_limit: u32, + pub account_token_ownership_limit: u32, + pub collections_admins_limit: u64, + pub custom_data_limit: u32, + + // Timeouts for item types in passed blocks + pub nft_sponsor_transfer_timeout: u32, + pub fungible_sponsor_transfer_timeout: u32, + pub refungible_sponsor_transfer_timeout: u32, + + // Schema limits + pub offchain_schema_limit: u32, + pub variable_on_chain_schema_limit: u32, + pub const_on_chain_schema_limit: u32, +} + + +#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct CreateNftData { + pub const_data: Vec, + pub variable_data: Vec, +} + +#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct CreateFungibleData { + pub value: u128, +} + +#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct CreateReFungibleData { + pub const_data: Vec, + pub variable_data: Vec, + pub pieces: u128, +} + +#[derive(Encode, Decode, Debug, Clone, PartialEq)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub enum CreateItemData { + NFT(CreateNftData), + Fungible(CreateFungibleData), + ReFungible(CreateReFungibleData), +} + +impl CreateItemData { + pub fn len(&self) -> usize { + let len = match self { + CreateItemData::NFT(data) => data.variable_data.len() + data.const_data.len(), + CreateItemData::ReFungible(data) => data.variable_data.len() + data.const_data.len(), + _ => 0 + }; + + return len; + } +} + +impl From for CreateItemData { + fn from(item: CreateNftData) -> Self { + CreateItemData::NFT(item) + } +} + +impl From for CreateItemData { + fn from(item: CreateReFungibleData) -> Self { + CreateItemData::ReFungible(item) + } +} + +impl From for CreateItemData { + fn from(item: CreateFungibleData) -> Self { + CreateItemData::Fungible(item) + } +} --- /dev/null +++ b/primitives/sponsorship/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "up-sponsorship" +version = "0.1.0" +edition = "2018" + +[dependencies] +impl-trait-for-tuples = "0.2.1" + +[features] +default = ["std"] +std = [] \ No newline at end of file --- /dev/null +++ b/primitives/sponsorship/src/lib.rs @@ -0,0 +1,42 @@ +#![no_std] + +pub trait SponsorshipHandler { + fn get_sponsor(who: &AccountId, call: &Call) -> Option; +} + +impl SponsorshipHandler for () { + fn get_sponsor(_who: &A, _call: &C) -> Option { + None + } +} + +macro_rules! impl_tuples { + ($($ident:ident)+) => { + impl SponsorshipHandler for ($($ident,)+) + where + $( + $ident: SponsorshipHandler + ),+ + { + fn get_sponsor(who: &AccountId, call: &Call) -> Option { + $( + if let Some(account) = $ident::get_sponsor(who, call) { + return Some(account); + } + )+ + None + } + } + } +} + +impl_tuples! {A} +impl_tuples! {A B} +impl_tuples! {A B C} +impl_tuples! {A B C D} +impl_tuples! {A B C D E} +impl_tuples! {A B C D E F} +impl_tuples! {A B C D E F G} +impl_tuples! {A B C D E F G H} +impl_tuples! {A B C D E F G H I} +impl_tuples! {A B C D E F G H I J} --- a/primitives/src/lib.rs +++ /dev/null @@ -1,285 +0,0 @@ - -#![cfg_attr(not(feature = "std"), no_std)] - -pub use serde::{Serialize, Deserialize}; - -use frame_system; -use sp_runtime::sp_std::prelude::Vec; -use codec::{Decode, Encode}; -pub use frame_support::{ - construct_runtime, decl_event, decl_module, decl_storage, decl_error, - dispatch::DispatchResult, - ensure, fail, parameter_types, - traits::{ - Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced, - Randomness, IsSubType, WithdrawReasons, - }, - weights::{ - constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND}, - DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight, - WeightToFeePolynomial, DispatchClass, - }, - StorageValue, - transactional, -}; - -pub const MAX_DECIMAL_POINTS: DecimalPoints = 30; -pub const MAX_REFUNGIBLE_PIECES: u128 = 1_000_000_000_000_000_000_000; -pub const MAX_SPONSOR_TIMEOUT: u32 = 10_368_000; -pub const MAX_TOKEN_OWNERSHIP: u32 = 10_000_000; - -pub type CollectionId = u32; -pub type TokenId = u32; -pub type DecimalPoints = u8; - -#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub enum CollectionMode { - Invalid, - NFT, - // decimal points - Fungible(DecimalPoints), - ReFungible, -} - -impl Default for CollectionMode { - fn default() -> Self { - Self::Invalid - } -} - -impl Into for CollectionMode { - fn into(self) -> u8 { - match self { - CollectionMode::Invalid => 0, - CollectionMode::NFT => 1, - CollectionMode::Fungible(_) => 2, - CollectionMode::ReFungible => 3, - } - } -} - -pub trait SponsoringResolve { - fn resolve( - who: &AccountId, - call: &Call) -> Option; -} - -#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub enum AccessMode { - Normal, - WhiteList, -} -impl Default for AccessMode { - fn default() -> Self { - Self::Normal - } -} - -#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub enum SchemaVersion { - ImageURL, - Unique, -} -impl Default for SchemaVersion { - fn default() -> Self { - Self::ImageURL - } -} - -#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub struct Ownership { - pub owner: AccountId, - pub fraction: u128, -} - -#[derive(Encode, Decode, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub enum SponsorshipState { - /// The fees are applied to the transaction sender - Disabled, - Unconfirmed(AccountId), - /// Transactions are sponsored by specified account - Confirmed(AccountId), -} - -impl SponsorshipState { - pub fn sponsor(&self) -> Option<&AccountId> { - match self { - Self::Confirmed(sponsor) => Some(sponsor), - _ => None, - } - } - - pub fn pending_sponsor(&self) -> Option<&AccountId> { - match self { - Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor), - _ => None, - } - } - - pub fn confirmed(&self) -> bool { - matches!(self, Self::Confirmed(_)) - } -} - -impl Default for SponsorshipState { - fn default() -> Self { - Self::Disabled - } -} - -#[derive(Encode, Decode, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub struct Collection { - pub owner: T::AccountId, - pub mode: CollectionMode, - pub access: AccessMode, - pub decimal_points: DecimalPoints, - pub name: Vec, // 64 include null escape char - pub description: Vec, // 256 include null escape char - pub token_prefix: Vec, // 16 include null escape char - pub mint_mode: bool, - pub offchain_schema: Vec, - pub schema_version: SchemaVersion, - pub sponsorship: SponsorshipState, - pub limits: CollectionLimits, // Collection private restrictions - pub variable_on_chain_schema: Vec, // - pub const_on_chain_schema: Vec, // -} - -#[derive(Encode, Decode, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub struct NftItemType { - pub owner: AccountId, - pub const_data: Vec, - pub variable_data: Vec, -} - -#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub struct FungibleItemType { - pub value: u128, -} - -#[derive(Encode, Decode, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub struct ReFungibleItemType { - pub owner: Vec>, - pub const_data: Vec, - pub variable_data: Vec, -} - - -#[derive(Encode, Decode, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub struct CollectionLimits { - pub account_token_ownership_limit: u32, - pub sponsored_data_size: u32, - /// None - setVariableMetadata is not sponsored - /// Some(v) - setVariableMetadata is sponsored - /// if there is v block between txs - pub sponsored_data_rate_limit: Option, - pub token_limit: u32, - - // Timeouts for item types in passed blocks - pub sponsor_transfer_timeout: u32, - pub owner_can_transfer: bool, - pub owner_can_destroy: bool, -} - -impl Default for CollectionLimits { - fn default() -> Self { - Self { - account_token_ownership_limit: 10_000_000, - token_limit: u32::max_value(), - sponsored_data_size: u32::MAX, - sponsored_data_rate_limit: None, - sponsor_transfer_timeout: 14400, - owner_can_transfer: true, - owner_can_destroy: true - } - } -} - -#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub struct ChainLimits { - pub collection_numbers_limit: u32, - pub account_token_ownership_limit: u32, - pub collections_admins_limit: u64, - pub custom_data_limit: u32, - - // Timeouts for item types in passed blocks - pub nft_sponsor_transfer_timeout: u32, - pub fungible_sponsor_transfer_timeout: u32, - pub refungible_sponsor_transfer_timeout: u32, - - // Schema limits - pub offchain_schema_limit: u32, - pub variable_on_chain_schema_limit: u32, - pub const_on_chain_schema_limit: u32, -} - - -#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub struct CreateNftData { - pub const_data: Vec, - pub variable_data: Vec, -} - -#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub struct CreateFungibleData { - pub value: u128, -} - -#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub struct CreateReFungibleData { - pub const_data: Vec, - pub variable_data: Vec, - pub pieces: u128, -} - -#[derive(Encode, Decode, Debug, Clone, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub enum CreateItemData { - NFT(CreateNftData), - Fungible(CreateFungibleData), - ReFungible(CreateReFungibleData), -} - -impl CreateItemData { - pub fn len(&self) -> usize { - let len = match self { - CreateItemData::NFT(data) => data.variable_data.len() + data.const_data.len(), - CreateItemData::ReFungible(data) => data.variable_data.len() + data.const_data.len(), - _ => 0 - }; - - return len; - } -} - -impl From for CreateItemData { - fn from(item: CreateNftData) -> Self { - CreateItemData::NFT(item) - } -} - -impl From for CreateItemData { - fn from(item: CreateReFungibleData) -> Self { - CreateItemData::ReFungible(item) - } -} - -impl From for CreateItemData { - fn from(item: CreateFungibleData) -> Self { - CreateItemData::Fungible(item) - } -} \ No newline at end of file -- gitstuff