difftreelog
Add properties RPC
in: master
8 files changed
client/rpc/src/lib.rsdiffbeforeafterboth20use jsonrpc_core::{Error as RpcError, ErrorCode, Result};20use jsonrpc_core::{Error as RpcError, ErrorCode, Result};21use jsonrpc_derive::rpc;21use jsonrpc_derive::rpc;22use up_data_structs::{RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId};22use up_data_structs::{23 RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId, Property, PropertyKey,24 PropertyKeyPermission,25};23use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};26use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};24use sp_blockchain::HeaderBackend;27use sp_blockchain::HeaderBackend;76 at: Option<BlockHash>,79 at: Option<BlockHash>,77 ) -> Result<Vec<u8>>;80 ) -> Result<Vec<u8>>;8182 #[rpc(name = "unique_collectionProperties")]83 fn collection_properties(84 &self,85 collection: CollectionId,86 keys: Vec<String>,87 at: Option<BlockHash>,88 ) -> Result<Vec<Property>>;8990 #[rpc(name = "unique_tokenProperties")]91 fn token_properties(92 &self,93 collection: CollectionId,94 token_id: TokenId,95 properties: Vec<String>,96 at: Option<BlockHash>,97 ) -> Result<Vec<Property>>;9899 #[rpc(name = "unique_propertyPermissions")]100 fn property_permissions(101 &self,102 collection: CollectionId,103 keys: Vec<String>,104 at: Option<BlockHash>,105 ) -> Result<Vec<PropertyKeyPermission>>;7810679 #[rpc(name = "unique_totalSupply")]107 #[rpc(name = "unique_totalSupply")]80 fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;108 fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;178macro_rules! pass_method {206macro_rules! pass_method {179 (207 (180 $method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?208 $method_name:ident(209 $($(#[map(|$map_arg:ident| $map:expr)])? $name:ident: $ty:ty),* $(,)?210 ) -> $result:ty $(=> $mapper:expr)?181 $(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*211 $(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*182 ) => {212 ) => {205 let result = $(if _api_version < $ver {235 let result = $(if _api_version < $ver {206 api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))236 api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))207 } else)*237 } else)*208 { api.$method_name(&at, $($name),*) };238 { api.$method_name(&at, $($((|$map_arg: $ty| $map))? ($name)),*) };209239210 let result = result.map_err(|e| RpcError {240 let result = result.map_err(|e| RpcError {211 code: ErrorCode::ServerError(Error::RuntimeError.into()),241 code: ErrorCode::ServerError(Error::RuntimeError.into()),242 pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);272 pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);243 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);273 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);274275 pass_method!(collection_properties(276 collection: CollectionId,277278 #[map(|keys| string_keys_to_bytes_keys(keys))]279 keys: Vec<String>280 ) -> Vec<Property>);281282 pass_method!(token_properties(283 collection: CollectionId,284 token_id: TokenId,285286 #[map(|keys| string_keys_to_bytes_keys(keys))]287 properties: Vec<String>288 ) -> Vec<Property>);289290 pass_method!(property_permissions(291 collection: CollectionId,292293 #[map(|keys| string_keys_to_bytes_keys(keys))]294 keys: Vec<String>295 ) -> Vec<PropertyKeyPermission>);244296245 pass_method!(total_supply(collection: CollectionId) -> u32);297 pass_method!(total_supply(collection: CollectionId) -> u32);246 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);298 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);257 pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>);309 pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>);258}310}311312fn string_keys_to_bytes_keys(keys: Vec<String>) -> Vec<Vec<u8>> {313 keys.into_iter().map(|key| key.into_bytes()).collect()314}259315pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -388,6 +388,7 @@
/// Collection properties
#[pallet::storage]
+ #[pallet::getter(fn collection_properties)]
pub type CollectionProperties<T> = StorageMap<
Hasher = Blake2_128Concat,
Key = CollectionId,
@@ -397,7 +398,7 @@
>;
#[pallet::storage]
- #[pallet::getter(fn property_permission)]
+ #[pallet::getter(fn property_permissions)]
pub type CollectionPropertyPermissions<T> = StorageMap<
Hasher = Blake2_128Concat,
Key = CollectionId,
@@ -656,9 +657,7 @@
CollectionProperties::<T>::insert(
id,
Properties::from_collection_props_vec(data.properties)
- .map_err(|e| -> Error::<T> {
- e.into()
- })?,
+ .map_err(|e| -> Error<T> { e.into() })?,
);
let token_props_permissions: PropertiesPermissionMap = data
@@ -667,9 +666,7 @@
.map(|property| (property.key, property.permission))
.collect::<BTreeMap<_, _>>()
.try_into()
- .map_err(|_| -> Error::<T> {
- PropertiesError::PropertyLimitReached.into()
- })?;
+ .map_err(|_| -> Error<T> { PropertiesError::PropertyLimitReached.into() })?;
CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);
@@ -754,9 +751,7 @@
CollectionProperties::<T>::try_mutate(collection.id, |properties| {
properties.try_set_property(property.clone())
})
- .map_err(|e| -> Error::<T> {
- e.into()
- })?;
+ .map_err(|e| -> Error<T> { e.into() })?;
Self::deposit_event(Event::CollectionPropertySet(collection.id, property));
@@ -786,7 +781,10 @@
properties.remove_property(&property_key);
});
- Self::deposit_event(Event::CollectionPropertyDeleted(collection.id, property_key));
+ Self::deposit_event(Event::CollectionPropertyDeleted(
+ collection.id,
+ property_key,
+ ));
Ok(())
}
@@ -823,9 +821,7 @@
let property_permission = property_permission.clone();
permissions.try_insert(property_permission.key, property_permission.permission)
})
- .map_err(|_| -> Error::<T> {
- PropertiesError::PropertyLimitReached.into()
- })?;
+ .map_err(|_| -> Error<T> { PropertiesError::PropertyLimitReached.into() })?;
Self::deposit_event(Event::PropertyPermissionSet(
collection.id,
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -184,7 +184,7 @@
with_weight(
<Pallet<T>>::delete_collection_properties(self, &sender, property_keys),
- weight
+ weight,
)
}
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -96,6 +96,7 @@
>;
#[pallet::storage]
+ #[pallet::getter(fn token_properties)]
pub type TokenProperties<T: Config> = StorageNMap<
Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
Value = up_data_structs::Properties,
@@ -265,9 +266,8 @@
<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
properties.try_set_property(property.clone())
- }).map_err(|e| -> CommonError::<T> {
- e.into()
- })?;
+ })
+ .map_err(|e| -> CommonError<T> { e.into() })?;
<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
collection.id,
@@ -318,7 +318,7 @@
token_id: TokenId,
property_key: &PropertyKey,
) -> DispatchResult {
- let permission = <PalletCommon<T>>::property_permission(collection.id)
+ let permission = <PalletCommon<T>>::property_permissions(collection.id)
.get(property_key)
.map(|p| p.clone())
.unwrap_or(PropertyPermission::None);
primitives/data-structs/src/lib.rsdiffbeforeafterboth--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -634,6 +634,7 @@
pub type PropertyValue = BoundedVec<u8, ConstU32<MAX_PROPERTY_VALUE_LENGTH>>;
#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone)]
+#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub enum PropertyPermission {
None,
AdminConst,
@@ -644,14 +645,21 @@
}
#[derive(Encode, Decode, Debug, TypeInfo, Clone, PartialEq, MaxEncodedLen)]
+#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Property {
+ #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]
pub key: PropertyKey,
+
+ #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]
pub value: PropertyValue,
}
#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone)]
+#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct PropertyKeyPermission {
+ #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]
pub key: PropertyKey,
+
pub permission: PropertyPermission,
}
@@ -723,6 +731,10 @@
pub fn get_property(&self, key: &PropertyKey) -> Option<&PropertyValue> {
self.map.get(key)
}
+
+ pub fn iter(&self) -> impl Iterator<Item = (&PropertyKey, &PropertyValue)> {
+ self.map.iter()
+ }
}
pub struct CollectionProperties;
primitives/rpc/src/lib.rsdiffbeforeafterboth--- a/primitives/rpc/src/lib.rs
+++ b/primitives/rpc/src/lib.rs
@@ -16,7 +16,10 @@
#![cfg_attr(not(feature = "std"), no_std)]
-use up_data_structs::{CollectionId, TokenId, RpcCollection, CollectionStats, CollectionLimits};
+use up_data_structs::{
+ CollectionId, TokenId, RpcCollection, CollectionStats, CollectionLimits, Property, PropertyKey,
+ PropertyKeyPermission,
+};
use sp_std::vec::Vec;
use codec::Decode;
use sp_runtime::DispatchError;
@@ -41,6 +44,19 @@
fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
+ fn collection_properties(collection: CollectionId, properties: Vec<Vec<u8>>) -> Result<Vec<Property>>;
+
+ fn token_properties(
+ collection: CollectionId,
+ token_id: TokenId,
+ properties: Vec<Vec<u8>>
+ ) -> Result<Vec<Property>>;
+
+ fn property_permissions(
+ collection: CollectionId,
+ properties: Vec<Vec<u8>>
+ ) -> Result<Vec<PropertyKeyPermission>>;
+
fn total_supply(collection: CollectionId) -> Result<u32>;
fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;
fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128>;
runtime/common/src/runtime_apis.rsdiffbeforeafterboth--- a/runtime/common/src/runtime_apis.rs
+++ b/runtime/common/src/runtime_apis.rs
@@ -7,6 +7,14 @@
$($custom_apis:tt)+
)?
) => {
+ fn bytes_keys_to_property_keys(keys: Vec<Vec<u8>>) -> Result<Vec<PropertyKey>, DispatchError> {
+ keys.into_iter()
+ .map(|key| -> Result<PropertyKey, DispatchError> {
+ key.try_into().map_err(|_| DispatchError::Other("Can't read property key"))
+ })
+ .collect::<Result<Vec<PropertyKey>, DispatchError>>()
+ }
+
impl_runtime_apis! {
$($($custom_apis)+)?
@@ -36,6 +44,76 @@
dispatch_unique_runtime!(collection.variable_metadata(token))
}
+ fn collection_properties(
+ collection: CollectionId,
+ keys: Vec<Vec<u8>>
+ ) -> Result<Vec<Property>, DispatchError> {
+ let keys = bytes_keys_to_property_keys(keys)?;
+
+ let properties = pallet_common::Pallet::<Runtime>::collection_properties(collection);
+
+ let properties = keys.into_iter()
+ .filter_map(|key| {
+ properties.get_property(&key)
+ .map(|value| {
+ Property {
+ key,
+ value: value.clone()
+ }
+ })
+ })
+ .collect();
+
+ Ok(properties)
+ }
+
+ fn token_properties(
+ collection: CollectionId,
+ token_id: TokenId,
+ keys: Vec<Vec<u8>>
+ ) -> Result<Vec<Property>, DispatchError> {
+ let keys = bytes_keys_to_property_keys(keys)?;
+
+ let properties = pallet_nonfungible::Pallet::<Runtime>::token_properties((collection, token_id));
+
+ let properties = keys.into_iter()
+ .filter_map(|key| {
+ properties.get_property(&key)
+ .map(|value| {
+ Property {
+ key,
+ value: value.clone()
+ }
+ })
+ })
+ .collect();
+
+ Ok(properties)
+ }
+
+ fn property_permissions(
+ collection: CollectionId,
+ keys: Vec<Vec<u8>>
+ ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {
+ let keys = bytes_keys_to_property_keys(keys)?;
+
+ let permissions = pallet_common::Pallet::<Runtime>::property_permissions(collection);
+
+ let key_permissions = keys.into_iter()
+ .filter_map(|key| {
+ permissions.get(&key)
+ .map(|permission| {
+ PropertyKeyPermission {
+ key,
+ permission: permission.clone()
+ }
+ })
+ })
+ .collect();
+
+ Ok(key_permissions)
+ }
+
fn total_supply(collection: CollectionId) -> Result<u32, DispatchError> {
dispatch_unique_runtime!(collection.total_supply())
}
runtime/opal/src/lib.rsdiffbeforeafterboth--- a/runtime/opal/src/lib.rs
+++ b/runtime/opal/src/lib.rs
@@ -67,7 +67,7 @@
},
};
use up_data_structs::mapping::{EvmTokenAddressMapping, CrossTokenAddressMapping};
-use up_data_structs::{CollectionId, TokenId, CollectionStats, CollectionLimits, RpcCollection};
+use up_data_structs::*;
// use pallet_contracts::weights::WeightInfo;
// #[cfg(any(feature = "std", test))]
use frame_system::{