Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose collection attributes from Inspect trait #1914

Merged
merged 14 commits into from
Oct 26, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -1065,10 +1065,10 @@ impl_runtime_apis! {

fn system_attribute(
collection: u32,
item: u32,
item: Option<u32>,
key: Vec<u8>,
) -> Option<Vec<u8>> {
<Nfts as Inspect<AccountId>>::system_attribute(&collection, &item, &key)
<Nfts as Inspect<AccountId>>::system_attribute(&collection, item.as_ref(), &key)
}

fn collection_attribute(collection: u32, key: Vec<u8>) -> Option<Vec<u8>> {
Expand Down
4 changes: 2 additions & 2 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2627,10 +2627,10 @@ impl_runtime_apis! {

fn system_attribute(
collection: u32,
item: u32,
item: Option<u32>,
key: Vec<u8>,
) -> Option<Vec<u8>> {
<Nfts as Inspect<AccountId>>::system_attribute(&collection, &item, &key)
<Nfts as Inspect<AccountId>>::system_attribute(&collection, item.as_ref(), &key)
}

fn collection_attribute(collection: u32, key: Vec<u8>) -> Option<Vec<u8>> {
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/nfts/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ sp_api::decl_runtime_apis! {

fn system_attribute(
collection: CollectionId,
item: ItemId,
item: Option<ItemId>,
key: Vec<u8>,
) -> Option<Vec<u8>>;

Expand Down
8 changes: 5 additions & 3 deletions substrate/frame/nfts/src/impl_nonfungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,19 @@ impl<T: Config<I>, I: 'static> Inspect<<T as SystemConfig>::AccountId> for Palle
Attribute::<T, I>::get((collection, Some(item), namespace, key)).map(|a| a.0.into())
}

/// 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>> {
let namespace = AttributeNamespace::Pallet;
let key = BoundedSlice::<_, _>::try_from(key).ok()?;
Attribute::<T, I>::get((collection, Some(item), namespace, key)).map(|a| a.0.into())
Attribute::<T, I>::get((collection, item, namespace, key)).map(|a| a.0.into())
}

/// Returns the attribute value of `item` of `collection` corresponding to `key`.
Expand Down
22 changes: 16 additions & 6 deletions substrate/frame/support/src/traits/tokens/nonfungible_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ pub trait Inspect<AccountId> {
None
}

/// Returns the system attribute value of `item` 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(_item: &Self::ItemId, _key: &[u8]) -> Option<Vec<u8>> {
fn system_attribute(_item: Option<&Self::ItemId>, _key: &[u8]) -> Option<Vec<u8>> {
dastansam marked this conversation as resolved.
Show resolved Hide resolved
None
}

Expand All @@ -87,10 +89,15 @@ pub trait Inspect<AccountId> {
.and_then(|v| V::decode(&mut &v[..]).ok())
}

/// Returns the strongly-typed system attribute value of `item` 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>(item: &Self::ItemId, key: &K) -> Option<V> {
fn typed_system_attribute<K: Encode, V: Decode>(
item: Option<&Self::ItemId>,
key: &K,
) -> Option<V> {
key.using_encoded(|d| Self::system_attribute(item, d))
.and_then(|v| V::decode(&mut &v[..]).ok())
}
Expand Down Expand Up @@ -211,7 +218,7 @@ impl<
fn custom_attribute(account: &AccountId, item: &Self::ItemId, key: &[u8]) -> Option<Vec<u8>> {
<F as nonfungibles::Inspect<AccountId>>::custom_attribute(account, &A::get(), item, key)
}
fn system_attribute(item: &Self::ItemId, key: &[u8]) -> Option<Vec<u8>> {
fn system_attribute(item: Option<&Self::ItemId>, key: &[u8]) -> Option<Vec<u8>> {
<F as nonfungibles::Inspect<AccountId>>::system_attribute(&A::get(), item, key)
}
dastansam marked this conversation as resolved.
Show resolved Hide resolved
fn typed_attribute<K: Encode, V: Decode>(item: &Self::ItemId, key: &K) -> Option<V> {
Expand All @@ -229,7 +236,10 @@ impl<
key,
)
}
fn typed_system_attribute<K: Encode, V: Decode>(item: &Self::ItemId, key: &K) -> Option<V> {
fn typed_system_attribute<K: Encode, V: Decode>(
item: Option<&Self::ItemId>,
key: &K,
) -> Option<V> {
<F as nonfungibles::Inspect<AccountId>>::typed_system_attribute(&A::get(), item, key)
}
dastansam marked this conversation as resolved.
Show resolved Hide resolved
fn can_transfer(item: &Self::ItemId) -> bool {
Expand Down
13 changes: 8 additions & 5 deletions substrate/frame/support/src/traits/tokens/nonfungibles_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
Loading