git.delta.rocks / unique-network / refs/commits / a66e7a4819dc

difftreelog

Add properties RPC

Daniel Shiposha2022-05-05parent: #a7c09a3.patch.diff
in: master

8 files changed

modifiedclient/rpc/src/lib.rsdiffbeforeafterboth
--- a/client/rpc/src/lib.rs
+++ b/client/rpc/src/lib.rs
@@ -19,7 +19,10 @@
 use codec::Decode;
 use jsonrpc_core::{Error as RpcError, ErrorCode, Result};
 use jsonrpc_derive::rpc;
-use up_data_structs::{RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId};
+use up_data_structs::{
+	RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId, Property, PropertyKey,
+	PropertyKeyPermission,
+};
 use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};
 use sp_blockchain::HeaderBackend;
 use up_rpc::UniqueApi as UniqueRuntimeApi;
@@ -76,6 +79,31 @@
 		at: Option<BlockHash>,
 	) -> Result<Vec<u8>>;
 
+	#[rpc(name = "unique_collectionProperties")]
+	fn collection_properties(
+		&self,
+		collection: CollectionId,
+		keys: Vec<String>,
+		at: Option<BlockHash>,
+	) -> Result<Vec<Property>>;
+
+	#[rpc(name = "unique_tokenProperties")]
+	fn token_properties(
+		&self,
+		collection: CollectionId,
+		token_id: TokenId,
+		properties: Vec<String>,
+		at: Option<BlockHash>,
+	) -> Result<Vec<Property>>;
+
+	#[rpc(name = "unique_propertyPermissions")]
+	fn property_permissions(
+		&self,
+		collection: CollectionId,
+		keys: Vec<String>,
+		at: Option<BlockHash>,
+	) -> Result<Vec<PropertyKeyPermission>>;
+
 	#[rpc(name = "unique_totalSupply")]
 	fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;
 	#[rpc(name = "unique_accountBalance")]
@@ -177,7 +205,9 @@
 
 macro_rules! pass_method {
 	(
-		$method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?
+		$method_name:ident(
+			$($(#[map(|$map_arg:ident| $map:expr)])? $name:ident: $ty:ty),* $(,)?
+		) -> $result:ty $(=> $mapper:expr)?
 		$(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*
 	) => {
 		fn $method_name(
@@ -205,7 +235,7 @@
 			let result = $(if _api_version < $ver {
 				api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))
 			} else)*
-			{ api.$method_name(&at, $($name),*) };
+			{ api.$method_name(&at, $($((|$map_arg: $ty| $map))? ($name)),*) };
 
 			let result = result.map_err(|e| RpcError {
 				code: ErrorCode::ServerError(Error::RuntimeError.into()),
@@ -242,6 +272,28 @@
 	pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);
 	pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);
 
+	pass_method!(collection_properties(
+		collection: CollectionId,
+
+		#[map(|keys| string_keys_to_bytes_keys(keys))]
+		keys: Vec<String>
+	) -> Vec<Property>);
+
+	pass_method!(token_properties(
+		collection: CollectionId,
+		token_id: TokenId,
+
+		#[map(|keys| string_keys_to_bytes_keys(keys))]
+		properties: Vec<String>
+	) -> Vec<Property>);
+
+	pass_method!(property_permissions(
+		collection: CollectionId,
+
+		#[map(|keys| string_keys_to_bytes_keys(keys))]
+		keys: Vec<String>
+	) -> Vec<PropertyKeyPermission>);
+
 	pass_method!(total_supply(collection: CollectionId) -> u32);
 	pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);
 	pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());
@@ -256,3 +308,7 @@
 	pass_method!(next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Option<u64>);
 	pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>);
 }
+
+fn string_keys_to_bytes_keys(keys: Vec<String>) -> Vec<Vec<u8>> {
+	keys.into_iter().map(|key| key.into_bytes()).collect()
+}
modifiedpallets/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,
modifiedpallets/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,
 		)
 	}
 
modifiedpallets/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);
modifiedprimitives/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;
modifiedprimitives/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>;
modifiedruntime/common/src/runtime_apis.rsdiffbeforeafterboth
7 $($custom_apis:tt)+7 $($custom_apis:tt)+
8 )?8 )?
9 ) => {9 ) => {
10 fn bytes_keys_to_property_keys(keys: Vec<Vec<u8>>) -> Result<Vec<PropertyKey>, DispatchError> {
11 keys.into_iter()
12 .map(|key| -> Result<PropertyKey, DispatchError> {
13 key.try_into().map_err(|_| DispatchError::Other("Can't read property key"))
14 })
15 .collect::<Result<Vec<PropertyKey>, DispatchError>>()
16 }
17
10 impl_runtime_apis! {18 impl_runtime_apis! {
11 $($($custom_apis)+)?19 $($($custom_apis)+)?
36 dispatch_unique_runtime!(collection.variable_metadata(token))44 dispatch_unique_runtime!(collection.variable_metadata(token))
37 }45 }
46
47 fn collection_properties(
48 collection: CollectionId,
49 keys: Vec<Vec<u8>>
50 ) -> Result<Vec<Property>, DispatchError> {
51 let keys = bytes_keys_to_property_keys(keys)?;
52
53 let properties = pallet_common::Pallet::<Runtime>::collection_properties(collection);
54
55 let properties = keys.into_iter()
56 .filter_map(|key| {
57 properties.get_property(&key)
58 .map(|value| {
59 Property {
60 key,
61 value: value.clone()
62 }
63 })
64 })
65 .collect();
66
67 Ok(properties)
68 }
69
70 fn token_properties(
71 collection: CollectionId,
72 token_id: TokenId,
73 keys: Vec<Vec<u8>>
74 ) -> Result<Vec<Property>, DispatchError> {
75 let keys = bytes_keys_to_property_keys(keys)?;
76
77 let properties = pallet_nonfungible::Pallet::<Runtime>::token_properties((collection, token_id));
78
79 let properties = keys.into_iter()
80 .filter_map(|key| {
81 properties.get_property(&key)
82 .map(|value| {
83 Property {
84 key,
85 value: value.clone()
86 }
87 })
88 })
89 .collect();
90
91 Ok(properties)
92 }
93
94 fn property_permissions(
95 collection: CollectionId,
96 keys: Vec<Vec<u8>>
97 ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {
98 let keys = bytes_keys_to_property_keys(keys)?;
99
100 let permissions = pallet_common::Pallet::<Runtime>::property_permissions(collection);
101
102 let key_permissions = keys.into_iter()
103 .filter_map(|key| {
104 permissions.get(&key)
105 .map(|permission| {
106 PropertyKeyPermission {
107 key,
108 permission: permission.clone()
109 }
110 })
111 })
112 .collect();
113
114 Ok(key_permissions)
115 }
38116
39 fn total_supply(collection: CollectionId) -> Result<u32, DispatchError> {117 fn total_supply(collection: CollectionId) -> Result<u32, DispatchError> {
40 dispatch_unique_runtime!(collection.total_supply())118 dispatch_unique_runtime!(collection.total_supply())
modifiedruntime/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::{