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
@@ -226,7 +226,7 @@ impl<
<F as nonfungibles::Inspect<AccountId>>::custom_attribute(account, &A::get(), item, key)
}
fn system_attribute(item: &Self::ItemId, key: &[u8]) -> Option<Vec<u8>> {
<F as nonfungibles::Inspect<AccountId>>::system_attribute(&A::get(), item, key)
<F as nonfungibles::Inspect<AccountId>>::system_attribute(&A::get(), Some(item), key)
}
fn typed_attribute<K: Encode, V: Decode>(item: &Self::ItemId, key: &K) -> Option<V> {
<F as nonfungibles::Inspect<AccountId>>::typed_attribute(&A::get(), item, key)
@@ -244,7 +244,7 @@ impl<
)
}
fn typed_system_attribute<K: Encode, V: Decode>(item: &Self::ItemId, key: &K) -> Option<V> {
<F as nonfungibles::Inspect<AccountId>>::typed_system_attribute(&A::get(), item, key)
<F as nonfungibles::Inspect<AccountId>>::typed_system_attribute(&A::get(), Some(item), key)
}
fn can_transfer(item: &Self::ItemId) -> bool {
<F as nonfungibles::Inspect<AccountId>>::can_transfer(&A::get(), item)
@@ -75,12 +75,14 @@ pub trait Inspect<AccountId> {
None
}
/// Returns the system attribute value of `item` of `collection` corresponding to `key`.
/// Returns the system attribute value of `item` of `collection` corresponding to `key` if
/// `item` is `Some`. Otherwise, returns the system attribute value of `collection`
/// corresponding to `key`.
///
/// By default this is `None`; no attributes are defined.
fn system_attribute(
_collection: &Self::CollectionId,
_item: &Self::ItemId,
_item: Option<&Self::ItemId>,
_key: &[u8],
) -> Option<Vec<u8>> {
None
@@ -113,13 +115,14 @@ pub trait Inspect<AccountId> {
.and_then(|v| V::decode(&mut &v[..]).ok())
}
/// Returns the strongly-typed system attribute value of `item` of `collection` corresponding to
/// `key`.
/// Returns the strongly-typed system attribute value of `item` corresponding to `key` if
/// `item` is `Some`. Otherwise, returns the strongly-typed system attribute value of
/// `collection` corresponding to `key`.
///
/// By default this just attempts to use `system_attribute`.
fn typed_system_attribute<K: Encode, V: Decode>(
collection: &Self::CollectionId,
item: &Self::ItemId,
item: Option<&Self::ItemId>,
key: &K,
) -> Option<V> {
key.using_encoded(|d| Self::system_attribute(collection, item, d))