Expose collection attributes from Inspect trait (#1914)

# Description

- What does this PR do?

While working with `pallet_nfts` through `nonfungibles_v2` traits
`Inspect, Mutate`, I found out that once you have set the collection
attribute with `<Nfts as Mutate>::set_collection_attribute()`, it's not
possible to read it with `<Nfts as Inspect>::collection_attribute()`
since they use different `namespace` values. When setting the attribute,
`AttributeNamespace::Pallet` is used, while
`AttributeNamespace::CollectionOwner` is used when reading.

more context:
https://github.com/freeverseio/laos/issues/7#issuecomment-1766137370

This PR makes `item` an optional parameter in
`Inspect::system_attribute()`, to be able to read collection attributes.

- Why are these changes needed?

To be able to read collection level attributes when reading attributes
of the collection. It will be possible to read collection attributes by
passing `None` for `item`

- How were these changes implemented and what do they affect?

`NftsApi` is also affected and `NftsApi::system_attribute()` now accepts
optional `item` parameter.

## Breaking change

Because of the change in the `NftsApi::system_attribute()` method's
`item` param, parachains who integrated the `NftsApi` need to update
their API code and frontend integrations accordingly. AssetHubs are
unaffected since the NftsApi wasn't released on those parachains yet.
This commit is contained in:
Dastan
2023-10-26 15:52:12 +06:00
committed by GitHub
parent bdf186870d
commit 0bcebac4fc
7 changed files with 101 additions and 16 deletions
+81 -1
View File
@@ -22,7 +22,7 @@ use enumflags2::BitFlags;
use frame_support::{
assert_noop, assert_ok,
traits::{
tokens::nonfungibles_v2::{Create, Destroy, Mutate},
tokens::nonfungibles_v2::{Create, Destroy, Inspect, Mutate},
Currency, Get,
},
};
@@ -982,6 +982,86 @@ fn set_collection_owner_attributes_should_work() {
});
}
#[test]
fn set_collection_system_attributes_should_work() {
new_test_ext().execute_with(|| {
Balances::make_free_balance_be(&account(1), 100);
assert_ok!(Nfts::force_create(
RuntimeOrigin::root(),
account(1),
collection_config_with_all_settings_enabled()
));
assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(1)), 0, 0, account(1), None));
let collection_id = 0;
let attribute_key = [0u8];
let attribute_value = [0u8];
assert_ok!(<Nfts as Mutate<AccountIdOf<Test>, ItemConfig>>::set_collection_attribute(
&collection_id,
&attribute_key,
&attribute_value
));
assert_eq!(attributes(0), vec![(None, AttributeNamespace::Pallet, bvec![0], bvec![0])]);
assert_eq!(
<Nfts as Inspect<AccountIdOf<Test>>>::system_attribute(
&collection_id,
None,
&attribute_key
),
Some(attribute_value.to_vec())
);
// test typed system attribute
let typed_attribute_key = [0u8; 32];
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
struct TypedAttributeValue(u32);
let typed_attribute_value = TypedAttributeValue(42);
assert_ok!(
<Nfts as Mutate<AccountIdOf<Test>, ItemConfig>>::set_typed_collection_attribute(
&collection_id,
&typed_attribute_key,
&typed_attribute_value
)
);
assert_eq!(
<Nfts as Inspect<AccountIdOf<Test>>>::typed_system_attribute(
&collection_id,
None,
&typed_attribute_key
),
Some(typed_attribute_value)
);
// check storage
assert_eq!(
attributes(collection_id),
[
(None, AttributeNamespace::Pallet, bvec![0], bvec![0]),
(
None,
AttributeNamespace::Pallet,
bvec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0
],
bvec![42, 0, 0, 0]
)
]
);
assert_ok!(Nfts::burn(RuntimeOrigin::root(), collection_id, 0));
let w = Nfts::get_destroy_witness(&0).unwrap();
assert_ok!(Nfts::destroy(RuntimeOrigin::signed(account(1)), collection_id, w));
assert_eq!(attributes(collection_id), vec![]);
})
}
#[test]
fn set_item_owner_attributes_should_work() {
new_test_ext().execute_with(|| {