difftreelog
feat implement collection nesting rules field
in: master
3 files changed
pallets/common/src/lib.rsdiffbeforeafterboth192 type CrossTokenAddressMapping: TokenAddressMapping<Self::CrossAccountId>;192 type CrossTokenAddressMapping: TokenAddressMapping<Self::CrossAccountId>;193 }193 }194195 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);194196195 #[pallet::pallet]197 #[pallet::pallet]198 #[pallet::storage_version(STORAGE_VERSION)]196 #[pallet::generate_store(pub(super) trait Store)]199 #[pallet::generate_store(pub(super) trait Store)]197 pub struct Pallet<T>(_);200 pub struct Pallet<T>(_);198201394 pub type DummyStorageValue<T> =397 pub type DummyStorageValue<T> =395 StorageValue<Value = (CollectionStats, CollectionId, TokenId), QueryKind = OptionQuery>;398 StorageValue<Value = (CollectionStats, CollectionId, TokenId), QueryKind = OptionQuery>;399400 #[pallet::hooks]401 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {402 fn on_runtime_upgrade() -> Weight {403 let mut weight = 0;404405 if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {406 use up_data_structs::{CollectionVersion1, CollectionVersion2};407 <CollectionById<T>>::translate_values::<CollectionVersion1<T::AccountId>, _>(|v| {408 Some(CollectionVersion2::from(v))409 });410 }411412 weight413 }414 }396}415}397416398impl<T: Config> Pallet<T> {417impl<T: Config> Pallet<T> {primitives/data-structs/Cargo.tomldiffbeforeafterboth24sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" }24sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" }25sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" }25sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" }26derivative = "2.2.0"26derivative = "2.2.0"27struct-versioning = { path = "../../crates/struct-versioning" }272828[features]29[features]29default = ["std"]30default = ["std"]primitives/data-structs/src/lib.rsdiffbeforeafterboth248 }248 }249}249}250250251#[struct_versioning::versioned(version = 2, upper)]251#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]252#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]252#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]253#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]253pub struct Collection<AccountId> {254pub struct Collection<AccountId> {266 pub schema_version: SchemaVersion,267 pub schema_version: SchemaVersion,267 pub sponsorship: SponsorshipState<AccountId>,268 pub sponsorship: SponsorshipState<AccountId>,269270 #[version(..2)]268 pub limits: CollectionLimits, // Collection private restrictions271 pub limits: CollectionLimitsVersion1, // Collection private restrictions272 #[version(2.., upper(limits.into()))]273 pub limits: CollectionLimitsVersion2,274269 #[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]275 #[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]270 pub variable_on_chain_schema: BoundedVec<u8, ConstU32<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>>,276 pub variable_on_chain_schema: BoundedVec<u8, ConstU32<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>>,321}327}322328323/// All fields are wrapped in `Option`s, where None means chain default329/// All fields are wrapped in `Option`s, where None means chain default330#[struct_versioning::versioned(version = 2, upper)]324#[derive(Encode, Decode, Debug, Default, Clone, PartialEq, TypeInfo, MaxEncodedLen)]331#[derive(Encode, Decode, Debug, Default, Clone, PartialEq, TypeInfo, MaxEncodedLen)]325#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]332#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]326pub struct CollectionLimits {333pub struct CollectionLimits {339 pub owner_can_destroy: Option<bool>,346 pub owner_can_destroy: Option<bool>,340 pub transfers_enabled: Option<bool>,347 pub transfers_enabled: Option<bool>,348349 #[version(2.., upper(None))]350 pub nesting_rule: Option<NestingRule>,341}351}342352343impl CollectionLimits {353impl CollectionLimits {384 SponsoringRateLimit::Blocks(v) => Some(v.min(MAX_SPONSOR_TIMEOUT)),394 SponsoringRateLimit::Blocks(v) => Some(v.min(MAX_SPONSOR_TIMEOUT)),385 }395 }386 }396 }397 pub fn nesting_rule(&self) -> &NestingRule {398 static DEFAULT: NestingRule = NestingRule::Owner;399 self.nesting_rule.as_ref().unwrap_or(&DEFAULT)400 }387}401}402403#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen, Derivative)]404#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]405#[derivative(Debug)]406pub enum NestingRule {407 /// No one can nest tokens408 Disabled,409 /// Owner can nest any tokens410 Owner,411 /// Owner can nest tokens from specified collections412 OwnerRestricted(413 #[cfg_attr(feature = "serde1", serde(with = "bounded_set_serde"))]414 #[derivative(Debug(format_with = "bounded_set_debug"))]415 BoundedBTreeSet<CollectionId, ConstU32<16>>,416 ),417}388418389#[derive(Encode, Decode, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]419#[derive(Encode, Decode, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]390#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]420#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]482 (&v as &BTreeMap<K, V>).fmt(f)512 (&v as &BTreeMap<K, V>).fmt(f)483}513}514515#[cfg(feature = "serde1")]516#[allow(dead_code)]517mod bounded_set_serde {518 use core::convert::TryFrom;519 use sp_std::collections::btree_set::BTreeSet;520 use frame_support::{traits::Get, storage::bounded_btree_set::BoundedBTreeSet};521 use serde::{522 ser::{self, Serialize},523 de::{self, Deserialize, Error},524 };525 pub fn serialize<D, K, S>(526 value: &BoundedBTreeSet<K, S>,527 serializer: D,528 ) -> Result<D::Ok, D::Error>529 where530 D: ser::Serializer,531 K: Serialize + Ord,532 {533 (value as &BTreeSet<_>).serialize(serializer)534 }535536 pub fn deserialize<'de, D, K, S>(deserializer: D) -> Result<BoundedBTreeSet<K, S>, D::Error>537 where538 D: de::Deserializer<'de>,539 K: de::Deserialize<'de> + Ord,540 S: Get<u32>,541 {542 let map = <BTreeSet<K>>::deserialize(deserializer)?;543 let len = map.len();544 TryFrom::try_from(map).map_err(|_| D::Error::invalid_length(len, &"lesser size"))545 }546}547548fn bounded_set_debug<K, S>(549 v: &BoundedBTreeSet<K, S>,550 f: &mut fmt::Formatter,551) -> Result<(), fmt::Error>552where553 K: fmt::Debug + Ord,554{555 use core::fmt::Debug;556 (&v as &BTreeSet<K>).fmt(f)557}484558485#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]559#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]486#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]560#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]