Nfts attribute read interface (#13349)

* feat: add custom and system attributes to Inspect

* feat: add nfts runtime api

* fix: pass std feature to runtime api

* fix: api copyright

Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>

---------

Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
This commit is contained in:
Daniel Shiposha
2023-02-23 15:06:12 +00:00
committed by GitHub
parent 2568c6d48b
commit 4af011f418
10 changed files with 291 additions and 30 deletions
@@ -0,0 +1,28 @@
[package]
name = "pallet-nfts-runtime-api"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "Runtime API for the FRAME NFTs pallet."
readme = "README.md"
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../../support" }
pallet-nfts = { version = "4.0.0-dev", default-features = false, path = "../../nfts" }
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/api" }
[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"pallet-nfts/std",
"sp-api/std",
]
@@ -0,0 +1,3 @@
RPC runtime API for the FRAME NFTs pallet.
License: Apache-2.0
@@ -0,0 +1,57 @@
// This file is part of Substrate.
// Copyright (C) 2023 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Runtime API definition for the FRAME NFTs pallet.
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use frame_support::dispatch::Vec;
sp_api::decl_runtime_apis! {
pub trait NftsApi<AccountId, CollectionId, ItemId>
where
AccountId: Encode + Decode,
CollectionId: Encode,
ItemId: Encode,
{
fn owner(collection: CollectionId, item: ItemId) -> Option<AccountId>;
fn collection_owner(collection: CollectionId) -> Option<AccountId>;
fn attribute(
collection: CollectionId,
item: ItemId,
key: Vec<u8>,
) -> Option<Vec<u8>>;
fn custom_attribute(
account: AccountId,
collection: CollectionId,
item: ItemId,
key: Vec<u8>,
) -> Option<Vec<u8>>;
fn system_attribute(
collection: CollectionId,
item: ItemId,
key: Vec<u8>,
) -> Option<Vec<u8>>;
fn collection_attribute(collection: CollectionId, key: Vec<u8>) -> Option<Vec<u8>>;
}
}
+31 -1
View File
@@ -50,18 +50,48 @@ impl<T: Config<I>, I: 'static> Inspect<<T as SystemConfig>::AccountId> for Palle
fn attribute(
collection: &Self::CollectionId,
item: &Self::ItemId,
namespace: &AttributeNamespace<<T as SystemConfig>::AccountId>,
key: &[u8],
) -> Option<Vec<u8>> {
if key.is_empty() {
// We make the empty key map to the item metadata value.
ItemMetadataOf::<T, I>::get(collection, item).map(|m| m.data.into())
} else {
let namespace = AttributeNamespace::CollectionOwner;
let key = BoundedSlice::<_, _>::try_from(key).ok()?;
Attribute::<T, I>::get((collection, Some(item), namespace, key)).map(|a| a.0.into())
}
}
/// Returns the custom attribute value of `item` of `collection` corresponding to `key`.
///
/// By default this is `None`; no attributes are defined.
fn custom_attribute(
account: &T::AccountId,
collection: &Self::CollectionId,
item: &Self::ItemId,
key: &[u8],
) -> Option<Vec<u8>> {
let namespace = Account::<T, I>::get((account, collection, item))
.map(|_| AttributeNamespace::ItemOwner)
.unwrap_or_else(|| AttributeNamespace::Account(account.clone()));
let key = BoundedSlice::<_, _>::try_from(key).ok()?;
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`.
///
/// By default this is `None`; no attributes are defined.
fn system_attribute(
collection: &Self::CollectionId,
item: &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())
}
/// Returns the attribute value of `item` of `collection` corresponding to `key`.
///
/// When `key` is empty, we return the item metadata value.