Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

[NFTs] Implement missed methods to set the attributes from other pallets #12919

Merged
merged 5 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,6 @@ impl pallet_uniques::Config for Runtime {

parameter_types! {
pub Features: PalletFeatures = PalletFeatures::all_enabled();
pub const NftsPalletId: PalletId = PalletId(*b"py/nfts_");
}

impl pallet_nfts::Config for Runtime {
Expand All @@ -1580,7 +1579,6 @@ impl pallet_nfts::Config for Runtime {
type MaxDeadlineDuration = MaxDeadlineDuration;
type Features = Features;
type WeightInfo = pallet_nfts::weights::SubstrateWeight<Runtime>;
type PalletId = NftsPalletId;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
Expand Down
58 changes: 58 additions & 0 deletions frame/nfts/src/impl_nonfungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,64 @@ impl<T: Config<I>, I: 'static> Mutate<<T as SystemConfig>::AccountId, ItemConfig
Ok(())
})
}

fn set_attribute(
collection: &Self::CollectionId,
item: &Self::ItemId,
key: &[u8],
value: &[u8],
) -> DispatchResult {
Self::do_force_set_attribute(
None,
*collection,
Some(*item),
AttributeNamespace::Pallet,
Self::construct_attribute_key(key.to_vec())?,
Self::construct_attribute_value(value.to_vec())?,
)
}

fn set_typed_attribute<K: Encode, V: Encode>(
collection: &Self::CollectionId,
item: &Self::ItemId,
key: &K,
value: &V,
) -> DispatchResult {
key.using_encoded(|k| {
value.using_encoded(|v| {
<Self as Mutate<T::AccountId, ItemConfig>>::set_attribute(collection, item, k, v)
})
})
}

fn set_collection_attribute(
collection: &Self::CollectionId,
key: &[u8],
value: &[u8],
) -> DispatchResult {
Self::do_force_set_attribute(
None,
*collection,
None,
AttributeNamespace::Pallet,
Self::construct_attribute_key(key.to_vec())?,
Self::construct_attribute_value(value.to_vec())?,
)
}

fn set_typed_collection_attribute<K: Encode, V: Encode>(
collection: &Self::CollectionId,
key: &K,
value: &V,
) -> DispatchResult {
key.using_encoded(|k| {
value.using_encoded(|v| {
<Self as Mutate<T::AccountId, ItemConfig>>::set_collection_attribute(
collection, k, v,
)
})
})
}
}

impl<T: Config<I>, I: 'static> Transfer<T::AccountId> for Pallet<T, I> {
Expand Down
8 changes: 2 additions & 6 deletions frame/nfts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type AccountIdLookupOf<T> = <<T as SystemConfig>::Lookup as StaticLookup>::Sourc
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{pallet_prelude::*, traits::ExistenceRequirement, PalletId};
use frame_support::{pallet_prelude::*, traits::ExistenceRequirement};
use frame_system::pallet_prelude::*;

#[pallet::pallet]
Expand Down Expand Up @@ -171,10 +171,6 @@ pub mod pallet {
#[pallet::constant]
type Features: Get<PalletFeatures>;

/// The pallet's id.
#[pallet::constant]
type PalletId: Get<PalletId>;

#[cfg(feature = "runtime-benchmarks")]
/// A set of helper functions for benchmarking.
type Helper: BenchmarkHelper<Self::CollectionId, Self::ItemId>;
Expand Down Expand Up @@ -782,7 +778,7 @@ pub mod pallet {
let key = (
&collection_id,
Some(owner_of_item),
AttributeNamespace::Pallet(T::PalletId::get()),
AttributeNamespace::Pallet,
&attribute_key,
);
let already_claimed = Attribute::<T, I>::contains_key(key.clone());
Expand Down
3 changes: 0 additions & 3 deletions frame/nfts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate as pallet_nfts;
use frame_support::{
construct_runtime, parameter_types,
traits::{AsEnsureOriginWithArg, ConstU32, ConstU64},
PalletId,
};
use sp_core::H256;
use sp_runtime::{
Expand Down Expand Up @@ -87,7 +86,6 @@ impl pallet_balances::Config for Test {

parameter_types! {
pub storage Features: PalletFeatures = PalletFeatures::all_enabled();
pub const NftsPalletId: PalletId = PalletId(*b"py/nfts_");
}

impl Config for Test {
Expand All @@ -112,7 +110,6 @@ impl Config for Test {
type MaxDeadlineDuration = ConstU64<10000>;
type Features = Features;
type WeightInfo = ();
type PalletId = NftsPalletId;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
}
Expand Down
12 changes: 11 additions & 1 deletion frame/nfts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use enumflags2::BitFlags;
use frame_support::{
assert_noop, assert_ok,
dispatch::Dispatchable,
traits::{tokens::nonfungibles_v2::Destroy, Currency, Get},
traits::{
tokens::nonfungibles_v2::{Destroy, Mutate},
Currency, Get,
},
};
use pallet_balances::Error as BalancesError;
use sp_core::bounded::BoundedVec;
Expand Down Expand Up @@ -975,12 +978,19 @@ fn validate_deposit_required_setting() {
bvec![2],
bvec![0],
));
assert_ok!(<Nfts as Mutate<<Test as SystemConfig>::AccountId, ItemConfig>>::set_attribute(
&0,
&0,
&[3],
&[0],
));
assert_eq!(
attributes(0),
vec![
(Some(0), AttributeNamespace::CollectionOwner, bvec![0], bvec![0]),
(Some(0), AttributeNamespace::ItemOwner, bvec![1], bvec![0]),
(Some(0), AttributeNamespace::Account(3), bvec![2], bvec![0]),
(Some(0), AttributeNamespace::Pallet, bvec![3], bvec![0]),
]
);
assert_eq!(Balances::reserved_balance(1), 0);
Expand Down
4 changes: 2 additions & 2 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub use sp_runtime::{
self, print, traits::Printable, ConsensusEngineId, MAX_MODULE_ERROR_ENCODED_SIZE,
};

use codec::{Decode, Encode, MaxEncodedLen};
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::TypeId;

Expand All @@ -127,7 +127,7 @@ pub const LOG_TARGET: &str = "runtime::frame-support";
pub enum Never {}

/// A pallet identifier. These are per pallet and should be stored in a registry somewhere.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[derive(Clone, Copy, Eq, PartialEq, Encode, Decode, TypeInfo)]
pub struct PalletId(pub [u8; 8]);

impl TypeId for PalletId {
Expand Down
3 changes: 1 addition & 2 deletions frame/support/src/traits/tokens/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

//! Miscellaneous types.

use crate::PalletId;
use codec::{Decode, Encode, FullCodec, MaxEncodedLen};
use sp_arithmetic::traits::{AtLeast32BitUnsigned, Zero};
use sp_core::RuntimeDebug;
Expand Down Expand Up @@ -133,7 +132,7 @@ pub enum BalanceStatus {
)]
pub enum AttributeNamespace<AccountId> {
/// An attribute was set by the pallet.
Pallet(PalletId),
Pallet,
/// An attribute was set by collection's owner.
CollectionOwner,
/// An attribute was set by item's owner.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ error[E0277]: the trait bound `Vec<u8>: MaxEncodedLen` is not satisfied
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5)
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6)
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7)
and 80 others
and 78 others
= note: required for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageMyStorage<T>, Vec<u8>>` to implement `StorageInfoTrait`
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error[E0277]: the trait bound `Bar: EncodeLike` is not satisfied
<&[(T,)] as EncodeLike<BinaryHeap<LikeT>>>
<&[(T,)] as EncodeLike<LinkedList<LikeT>>>
<&[T] as EncodeLike<Vec<U>>>
and 279 others
and 278 others
= note: required for `Bar` to implement `FullEncode`
= note: required for `Bar` to implement `FullCodec`
= note: required for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>` to implement `PartialStorageInfoTrait`
Expand Down Expand Up @@ -69,7 +69,7 @@ error[E0277]: the trait bound `Bar: TypeInfo` is not satisfied
(A, B, C, D)
(A, B, C, D, E)
(A, B, C, D, E, F)
and 162 others
and 161 others
= note: required for `Bar` to implement `StaticTypeInfo`
= note: required for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>` to implement `StorageEntryMetadataBuilder`

Expand Down Expand Up @@ -103,7 +103,7 @@ error[E0277]: the trait bound `Bar: EncodeLike` is not satisfied
<&[(T,)] as EncodeLike<BinaryHeap<LikeT>>>
<&[(T,)] as EncodeLike<LinkedList<LikeT>>>
<&[T] as EncodeLike<Vec<U>>>
and 279 others
and 278 others
= note: required for `Bar` to implement `FullEncode`
= note: required for `Bar` to implement `FullCodec`
= note: required for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>` to implement `StorageEntryMetadataBuilder`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error[E0277]: the trait bound `Bar: EncodeLike` is not satisfied
<&[(T,)] as EncodeLike<BinaryHeap<LikeT>>>
<&[(T,)] as EncodeLike<LinkedList<LikeT>>>
<&[T] as EncodeLike<Vec<U>>>
and 279 others
and 278 others
= note: required for `Bar` to implement `FullEncode`
= note: required for `Bar` to implement `FullCodec`
= note: required for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>` to implement `PartialStorageInfoTrait`
Expand Down Expand Up @@ -69,7 +69,7 @@ error[E0277]: the trait bound `Bar: TypeInfo` is not satisfied
(A, B, C, D)
(A, B, C, D, E)
(A, B, C, D, E, F)
and 162 others
and 161 others
= note: required for `Bar` to implement `StaticTypeInfo`
= note: required for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>` to implement `StorageEntryMetadataBuilder`

Expand Down Expand Up @@ -103,7 +103,7 @@ error[E0277]: the trait bound `Bar: EncodeLike` is not satisfied
<&[(T,)] as EncodeLike<BinaryHeap<LikeT>>>
<&[(T,)] as EncodeLike<LinkedList<LikeT>>>
<&[T] as EncodeLike<Vec<U>>>
and 279 others
and 278 others
= note: required for `Bar` to implement `FullEncode`
= note: required for `Bar` to implement `FullCodec`
= note: required for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>` to implement `StorageEntryMetadataBuilder`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ error[E0277]: the trait bound `Bar: MaxEncodedLen` is not satisfied
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5)
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6)
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7)
and 80 others
and 78 others
= note: required for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>` to implement `StorageInfoTrait`
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ error[E0277]: the trait bound `Bar: MaxEncodedLen` is not satisfied
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5)
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6)
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7)
and 80 others
and 79 others
= note: required for `Key<frame_support::Twox64Concat, Bar>` to implement `KeyGeneratorMaxEncodedLen`
= note: required for `frame_support::pallet_prelude::StorageNMap<_GeneratedPrefixForStorageFoo<T>, Key<frame_support::Twox64Concat, Bar>, u32>` to implement `StorageInfoTrait`