git.delta.rocks / unique-network / refs/commits / 4154c5103c94

difftreelog

Add token_data RPC

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

10 files changed

modifiedclient/rpc/src/lib.rsdiffbeforeafterboth
--- a/client/rpc/src/lib.rs
+++ b/client/rpc/src/lib.rs
@@ -21,7 +21,7 @@
 use jsonrpc_derive::rpc;
 use up_data_structs::{
 	RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId, Property,
-	PropertyKeyPermission,
+	PropertyKeyPermission, TokenData,
 };
 use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};
 use sp_blockchain::HeaderBackend;
@@ -104,6 +104,15 @@
 		at: Option<BlockHash>,
 	) -> Result<Vec<PropertyKeyPermission>>;
 
+	#[rpc(name = "unique_tokenData")]
+	fn token_data(
+		&self,
+		collection: CollectionId,
+		token_id: TokenId,
+		keys: Vec<String>,
+		at: Option<BlockHash>,
+	) -> Result<TokenData<CrossAccountId>>;
+
 	#[rpc(name = "unique_totalSupply")]
 	fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;
 	#[rpc(name = "unique_accountBalance")]
@@ -294,6 +303,14 @@
 		keys: Vec<String>
 	) -> Vec<PropertyKeyPermission>);
 
+	pass_method!(token_data(
+		collection: CollectionId,
+		token_id: TokenId,
+
+		#[map(|keys| string_keys_to_bytes_keys(keys))]
+		keys: Vec<String>,
+	) -> TokenData<CrossAccountId>);
+
 	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());
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -36,7 +36,7 @@
 	CUSTOM_DATA_LIMIT, CollectionLimits, CustomDataLimit, CreateCollectionData, SponsorshipState,
 	CreateItemExData, SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField,
 	PhantomType, Property, Properties, PropertiesPermissionMap, PropertyKey, PropertyPermission,
-	PropertiesError, PropertyKeyPermission,
+	PropertiesError, PropertyKeyPermission, TokenData,
 };
 pub use pallet::*;
 use sp_core::H160;
@@ -454,6 +454,7 @@
 			CollectionStats,
 			CollectionId,
 			TokenId,
+			PhantomType<TokenData<T::CrossAccountId>>,
 			PhantomType<RpcCollection<T::AccountId>>,
 		),
 		QueryKind = OptionQuery,
@@ -843,6 +844,57 @@
 		Ok(())
 	}
 
+	pub fn bytes_keys_to_property_keys(keys: Vec<Vec<u8>>) -> Result<Vec<PropertyKey>, DispatchError> {
+		keys.into_iter()
+			.map(|key| -> Result<PropertyKey, DispatchError> {
+				// TODO Fix error
+				key.try_into().map_err(|_| DispatchError::Other("Can't read property key"))
+			})
+			.collect::<Result<Vec<PropertyKey>, DispatchError>>()
+	}
+
+	pub fn filter_collection_properties(
+		collection_id: CollectionId,
+		keys: Vec<PropertyKey>
+	) -> Result<Vec<Property>, DispatchError> {
+		let properties = Self::collection_properties(collection_id);
+
+		let properties = keys.into_iter()
+			.filter_map(|key| {
+				properties.get_property(&key)
+					.map(|value| {
+						Property {
+							key,
+							value: value.clone()
+						}
+					})
+			})
+			.collect();
+
+		Ok(properties)
+	}
+
+	pub fn filter_property_permissions(
+		collection_id: CollectionId,
+		keys: Vec<PropertyKey>
+	) -> Result<Vec<PropertyKeyPermission>, DispatchError> {
+		let permissions = Self::property_permissions(collection_id);
+
+		let key_permissions = keys.into_iter()
+			.filter_map(|key| {
+				permissions.get(&key)
+					.map(|permission| {
+						PropertyKeyPermission {
+							key,
+							permission: permission.clone()
+						}
+					})
+			})
+			.collect();
+
+		Ok(key_permissions)
+	}
+
 	fn set_field_raw(
 		collection_id: CollectionId,
 		field: CollectionField,
@@ -1117,7 +1169,11 @@
 	fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;
 	fn const_metadata(&self, token: TokenId) -> Vec<u8>;
 	fn variable_metadata(&self, token: TokenId) -> Vec<u8>;
-
+	fn token_properties(
+		&self,
+		token_id: TokenId,
+		keys: Vec<PropertyKey>
+	) -> Vec<Property>;
 	/// Amount of unique collection tokens
 	fn total_supply(&self) -> u32;
 	/// Amount of different tokens account has (Applicable to nonfungible/refungible)
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -250,7 +250,7 @@
 		_sender: T::CrossAccountId,
 		_property: Vec<Property>,
 	) -> DispatchResultWithPostInfo {
-		fail!(<Error<T>>::PropertiesNotAllowed)
+		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
 	fn delete_collection_properties(
@@ -258,7 +258,7 @@
 		_sender: &T::CrossAccountId,
 		_property_keys: Vec<PropertyKey>,
 	) -> DispatchResultWithPostInfo {
-		fail!(<Error<T>>::PropertiesNotAllowed)
+		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
 	fn set_token_properties(
@@ -267,7 +267,7 @@
 		_token_id: TokenId,
 		_property: Vec<Property>,
 	) -> DispatchResultWithPostInfo {
-		fail!(<Error<T>>::PropertiesNotAllowed)
+		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
 	fn set_property_permissions(
@@ -275,7 +275,7 @@
 		_sender: &T::CrossAccountId,
 		_property_permissions: Vec<PropertyKeyPermission>,
 	) -> DispatchResultWithPostInfo {
-		fail!(<Error<T>>::PropertiesNotAllowed)
+		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
 	fn delete_token_properties(
@@ -284,7 +284,7 @@
 		_token_id: TokenId,
 		_property_keys: Vec<PropertyKey>,
 	) -> DispatchResultWithPostInfo {
-		fail!(<Error<T>>::PropertiesNotAllowed)
+		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
 	fn set_variable_metadata(
@@ -336,6 +336,14 @@
 		Vec::new()
 	}
 
+	fn token_properties(
+		&self,
+		_token_id: TokenId,
+		_keys: Vec<PropertyKey>
+	) -> Vec<Property> {
+		Vec::new()
+	}
+
 	fn total_supply(&self) -> u32 {
 		1
 	}
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
61 FungibleItemsDontHaveData,61 FungibleItemsDontHaveData,
62 /// Fungible token does not support nested62 /// Fungible token does not support nested
63 FungibleDisallowsNesting,63 FungibleDisallowsNesting,
64 /// Item properties are not allowed64 /// Setting item properties is not allowed
65 PropertiesNotAllowed,65 SettingPropertiesNotAllowed,
66 }66 }
6767
68 #[pallet::config]68 #[pallet::config]
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -386,6 +386,26 @@
 			.into_inner()
 	}
 
+	fn token_properties(
+		&self,
+		token_id: TokenId,
+		keys: Vec<PropertyKey>
+	) -> Vec<Property> {
+		let properties = <Pallet<T>>::token_properties((self.id, token_id));
+
+		keys.into_iter()
+			.filter_map(|key| {
+				properties.get_property(&key)
+					.map(|value| {
+						Property {
+							key,
+							value: value.clone()
+						}
+					})
+			})
+			.collect()
+	}
+
 	fn total_supply(&self) -> u32 {
 		<Pallet<T>>::total_supply(self)
 	}
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -269,7 +269,7 @@
 		_sender: T::CrossAccountId,
 		_property: Vec<Property>,
 	) -> DispatchResultWithPostInfo {
-		fail!(<Error<T>>::PropertiesNotAllowed)
+		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
 	fn delete_collection_properties(
@@ -277,7 +277,7 @@
 		_sender: &T::CrossAccountId,
 		_property_keys: Vec<PropertyKey>,
 	) -> DispatchResultWithPostInfo {
-		fail!(<Error<T>>::PropertiesNotAllowed)
+		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
 	fn set_token_properties(
@@ -286,7 +286,7 @@
 		_token_id: TokenId,
 		_property: Vec<Property>,
 	) -> DispatchResultWithPostInfo {
-		fail!(<Error<T>>::PropertiesNotAllowed)
+		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
 	fn set_property_permissions(
@@ -294,7 +294,7 @@
 		_sender: &T::CrossAccountId,
 		_property_permissions: Vec<PropertyKeyPermission>,
 	) -> DispatchResultWithPostInfo {
-		fail!(<Error<T>>::PropertiesNotAllowed)
+		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
 	fn delete_token_properties(
@@ -303,7 +303,7 @@
 		_token_id: TokenId,
 		_property_keys: Vec<PropertyKey>,
 	) -> DispatchResultWithPostInfo {
-		fail!(<Error<T>>::PropertiesNotAllowed)
+		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
 	fn set_variable_metadata(
@@ -363,6 +363,14 @@
 			.into_inner()
 	}
 
+	fn token_properties(
+		&self,
+		_token_id: TokenId,
+		_keys: Vec<PropertyKey>
+	) -> Vec<Property> {
+		Vec::new()
+	}
+
 	fn total_supply(&self) -> u32 {
 		<Pallet<T>>::total_supply(self)
 	}
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -62,8 +62,8 @@
 		WrongRefungiblePieces,
 		/// Refungible token can't nest other tokens
 		RefungibleDisallowsNesting,
-		/// Item properties are not allowed
-		PropertiesNotAllowed,
+		/// Setting item properties is not allowed
+		SettingPropertiesNotAllowed,
 	}
 
 	#[pallet::config]
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -173,6 +173,14 @@
 	}
 }
 
+#[derive(Encode, Decode, Clone, PartialEq, TypeInfo)]
+#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
+pub struct TokenData<CrossAccountId> {
+	pub const_data: Vec<u8>,
+	pub properties: Vec<Property>,
+	pub owner: Option<CrossAccountId>,
+}
+
 pub struct OverflowError;
 impl From<OverflowError> for &'static str {
 	fn from(_: OverflowError) -> Self {
modifiedprimitives/rpc/src/lib.rsdiffbeforeafterboth
--- a/primitives/rpc/src/lib.rs
+++ b/primitives/rpc/src/lib.rs
@@ -18,7 +18,7 @@
 
 use up_data_structs::{
 	CollectionId, TokenId, RpcCollection, CollectionStats, CollectionLimits, Property,
-	PropertyKeyPermission,
+	PropertyKeyPermission, TokenData,
 };
 use sp_std::vec::Vec;
 use codec::Decode;
@@ -57,6 +57,8 @@
 			properties: Vec<Vec<u8>>
 		) -> Result<Vec<PropertyKeyPermission>>;
 
+		fn token_data(collection: CollectionId, token_id: TokenId, keys: Vec<Vec<u8>>) -> Result<TokenData<CrossAccountId>>;
+
 		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
--- a/runtime/common/src/runtime_apis.rs
+++ b/runtime/common/src/runtime_apis.rs
@@ -7,14 +7,6 @@
             $($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)+)?
 
@@ -48,23 +40,9 @@
                     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();
+                    let keys = pallet_common::Pallet::<Runtime>::bytes_keys_to_property_keys(keys)?;
 
-                    Ok(properties)
+                    pallet_common::Pallet::<Runtime>::filter_collection_properties(collection, keys)
                 }
 
                 fn token_properties(
@@ -72,46 +50,31 @@
                     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)
+                    let keys = pallet_common::Pallet::<Runtime>::bytes_keys_to_property_keys(keys)?;
+                    dispatch_unique_runtime!(collection.token_properties(token_id, keys))
                 }
 
                 fn property_permissions(
                     collection: CollectionId,
                     keys: Vec<Vec<u8>>
                 ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {
-                    let keys = bytes_keys_to_property_keys(keys)?;
+                    let keys = pallet_common::Pallet::<Runtime>::bytes_keys_to_property_keys(keys)?;
 
-                    let permissions = pallet_common::Pallet::<Runtime>::property_permissions(collection);
+                    pallet_common::Pallet::<Runtime>::filter_property_permissions(collection, keys)
+                }
 
-                    let key_permissions = keys.into_iter()
-                        .filter_map(|key| {
-                            permissions.get(&key)
-                                .map(|permission| {
-                                    PropertyKeyPermission {
-                                        key,
-                                        permission: permission.clone()
-                                    }
-                                })
-                        })
-                        .collect();
+                fn token_data(
+                    collection: CollectionId,
+                    token_id: TokenId,
+                    keys: Vec<Vec<u8>>
+                ) -> Result<TokenData<CrossAccountId>, DispatchError> {
+                    let token_data = TokenData {
+                        const_data: Self::const_metadata(collection, token_id)?,
+                        properties: Self::token_properties(collection, token_id, keys)?,
+                        owner: Self::token_owner(collection, token_id)?
+                    };
 
-                    Ok(key_permissions)
+                    Ok(token_data)
                 }
 
                 fn total_supply(collection: CollectionId) -> Result<u32, DispatchError> {