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
21use jsonrpc_derive::rpc;21use jsonrpc_derive::rpc;
22use up_data_structs::{22use up_data_structs::{
23 RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId, Property,23 RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId, Property,
24 PropertyKeyPermission,24 PropertyKeyPermission, TokenData,
25};25};
26use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};26use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};
27use sp_blockchain::HeaderBackend;27use sp_blockchain::HeaderBackend;
104 at: Option<BlockHash>,104 at: Option<BlockHash>,
105 ) -> Result<Vec<PropertyKeyPermission>>;105 ) -> Result<Vec<PropertyKeyPermission>>;
106
107 #[rpc(name = "unique_tokenData")]
108 fn token_data(
109 &self,
110 collection: CollectionId,
111 token_id: TokenId,
112 keys: Vec<String>,
113 at: Option<BlockHash>,
114 ) -> Result<TokenData<CrossAccountId>>;
106115
107 #[rpc(name = "unique_totalSupply")]116 #[rpc(name = "unique_totalSupply")]
108 fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;117 fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;
294 keys: Vec<String>303 keys: Vec<String>
295 ) -> Vec<PropertyKeyPermission>);304 ) -> Vec<PropertyKeyPermission>);
305
306 pass_method!(token_data(
307 collection: CollectionId,
308 token_id: TokenId,
309
310 #[map(|keys| string_keys_to_bytes_keys(keys))]
311 keys: Vec<String>,
312 ) -> TokenData<CrossAccountId>);
296313
297 pass_method!(total_supply(collection: CollectionId) -> u32);314 pass_method!(total_supply(collection: CollectionId) -> u32);
298 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);315 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
36 CUSTOM_DATA_LIMIT, CollectionLimits, CustomDataLimit, CreateCollectionData, SponsorshipState,36 CUSTOM_DATA_LIMIT, CollectionLimits, CustomDataLimit, CreateCollectionData, SponsorshipState,
37 CreateItemExData, SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField,37 CreateItemExData, SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField,
38 PhantomType, Property, Properties, PropertiesPermissionMap, PropertyKey, PropertyPermission,38 PhantomType, Property, Properties, PropertiesPermissionMap, PropertyKey, PropertyPermission,
39 PropertiesError, PropertyKeyPermission,39 PropertiesError, PropertyKeyPermission, TokenData,
40};40};
41pub use pallet::*;41pub use pallet::*;
42use sp_core::H160;42use sp_core::H160;
454 CollectionStats,454 CollectionStats,
455 CollectionId,455 CollectionId,
456 TokenId,456 TokenId,
457 PhantomType<TokenData<T::CrossAccountId>>,
457 PhantomType<RpcCollection<T::AccountId>>,458 PhantomType<RpcCollection<T::AccountId>>,
458 ),459 ),
459 QueryKind = OptionQuery,460 QueryKind = OptionQuery,
843 Ok(())844 Ok(())
844 }845 }
846
847 pub fn bytes_keys_to_property_keys(keys: Vec<Vec<u8>>) -> Result<Vec<PropertyKey>, DispatchError> {
848 keys.into_iter()
849 .map(|key| -> Result<PropertyKey, DispatchError> {
850 // TODO Fix error
851 key.try_into().map_err(|_| DispatchError::Other("Can't read property key"))
852 })
853 .collect::<Result<Vec<PropertyKey>, DispatchError>>()
854 }
855
856 pub fn filter_collection_properties(
857 collection_id: CollectionId,
858 keys: Vec<PropertyKey>
859 ) -> Result<Vec<Property>, DispatchError> {
860 let properties = Self::collection_properties(collection_id);
861
862 let properties = keys.into_iter()
863 .filter_map(|key| {
864 properties.get_property(&key)
865 .map(|value| {
866 Property {
867 key,
868 value: value.clone()
869 }
870 })
871 })
872 .collect();
873
874 Ok(properties)
875 }
876
877 pub fn filter_property_permissions(
878 collection_id: CollectionId,
879 keys: Vec<PropertyKey>
880 ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {
881 let permissions = Self::property_permissions(collection_id);
882
883 let key_permissions = keys.into_iter()
884 .filter_map(|key| {
885 permissions.get(&key)
886 .map(|permission| {
887 PropertyKeyPermission {
888 key,
889 permission: permission.clone()
890 }
891 })
892 })
893 .collect();
894
895 Ok(key_permissions)
896 }
845897
846 fn set_field_raw(898 fn set_field_raw(
847 collection_id: CollectionId,899 collection_id: CollectionId,
1117 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;1169 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;
1118 fn const_metadata(&self, token: TokenId) -> Vec<u8>;1170 fn const_metadata(&self, token: TokenId) -> Vec<u8>;
1119 fn variable_metadata(&self, token: TokenId) -> Vec<u8>;1171 fn variable_metadata(&self, token: TokenId) -> Vec<u8>;
11201172 fn token_properties(
1173 &self,
1174 token_id: TokenId,
1175 keys: Vec<PropertyKey>
1176 ) -> Vec<Property>;
1121 /// Amount of unique collection tokens1177 /// Amount of unique collection tokens
1122 fn total_supply(&self) -> u32;1178 fn total_supply(&self) -> u32;
1123 /// Amount of different tokens account has (Applicable to nonfungible/refungible)1179 /// Amount of different tokens account has (Applicable to nonfungible/refungible)
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
250 _sender: T::CrossAccountId,250 _sender: T::CrossAccountId,
251 _property: Vec<Property>,251 _property: Vec<Property>,
252 ) -> DispatchResultWithPostInfo {252 ) -> DispatchResultWithPostInfo {
253 fail!(<Error<T>>::PropertiesNotAllowed)253 fail!(<Error<T>>::SettingPropertiesNotAllowed)
254 }254 }
255255
256 fn delete_collection_properties(256 fn delete_collection_properties(
257 &self,257 &self,
258 _sender: &T::CrossAccountId,258 _sender: &T::CrossAccountId,
259 _property_keys: Vec<PropertyKey>,259 _property_keys: Vec<PropertyKey>,
260 ) -> DispatchResultWithPostInfo {260 ) -> DispatchResultWithPostInfo {
261 fail!(<Error<T>>::PropertiesNotAllowed)261 fail!(<Error<T>>::SettingPropertiesNotAllowed)
262 }262 }
263263
264 fn set_token_properties(264 fn set_token_properties(
267 _token_id: TokenId,267 _token_id: TokenId,
268 _property: Vec<Property>,268 _property: Vec<Property>,
269 ) -> DispatchResultWithPostInfo {269 ) -> DispatchResultWithPostInfo {
270 fail!(<Error<T>>::PropertiesNotAllowed)270 fail!(<Error<T>>::SettingPropertiesNotAllowed)
271 }271 }
272272
273 fn set_property_permissions(273 fn set_property_permissions(
274 &self,274 &self,
275 _sender: &T::CrossAccountId,275 _sender: &T::CrossAccountId,
276 _property_permissions: Vec<PropertyKeyPermission>,276 _property_permissions: Vec<PropertyKeyPermission>,
277 ) -> DispatchResultWithPostInfo {277 ) -> DispatchResultWithPostInfo {
278 fail!(<Error<T>>::PropertiesNotAllowed)278 fail!(<Error<T>>::SettingPropertiesNotAllowed)
279 }279 }
280280
281 fn delete_token_properties(281 fn delete_token_properties(
284 _token_id: TokenId,284 _token_id: TokenId,
285 _property_keys: Vec<PropertyKey>,285 _property_keys: Vec<PropertyKey>,
286 ) -> DispatchResultWithPostInfo {286 ) -> DispatchResultWithPostInfo {
287 fail!(<Error<T>>::PropertiesNotAllowed)287 fail!(<Error<T>>::SettingPropertiesNotAllowed)
288 }288 }
289289
290 fn set_variable_metadata(290 fn set_variable_metadata(
336 Vec::new()336 Vec::new()
337 }337 }
338
339 fn token_properties(
340 &self,
341 _token_id: TokenId,
342 _keys: Vec<PropertyKey>
343 ) -> Vec<Property> {
344 Vec::new()
345 }
338346
339 fn total_supply(&self) -> u32 {347 fn total_supply(&self) -> u32 {
340 1348 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
386 .into_inner()386 .into_inner()
387 }387 }
388
389 fn token_properties(
390 &self,
391 token_id: TokenId,
392 keys: Vec<PropertyKey>
393 ) -> Vec<Property> {
394 let properties = <Pallet<T>>::token_properties((self.id, token_id));
395
396 keys.into_iter()
397 .filter_map(|key| {
398 properties.get_property(&key)
399 .map(|value| {
400 Property {
401 key,
402 value: value.clone()
403 }
404 })
405 })
406 .collect()
407 }
388408
389 fn total_supply(&self) -> u32 {409 fn total_supply(&self) -> u32 {
390 <Pallet<T>>::total_supply(self)410 <Pallet<T>>::total_supply(self)
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
269 _sender: T::CrossAccountId,269 _sender: T::CrossAccountId,
270 _property: Vec<Property>,270 _property: Vec<Property>,
271 ) -> DispatchResultWithPostInfo {271 ) -> DispatchResultWithPostInfo {
272 fail!(<Error<T>>::PropertiesNotAllowed)272 fail!(<Error<T>>::SettingPropertiesNotAllowed)
273 }273 }
274274
275 fn delete_collection_properties(275 fn delete_collection_properties(
276 &self,276 &self,
277 _sender: &T::CrossAccountId,277 _sender: &T::CrossAccountId,
278 _property_keys: Vec<PropertyKey>,278 _property_keys: Vec<PropertyKey>,
279 ) -> DispatchResultWithPostInfo {279 ) -> DispatchResultWithPostInfo {
280 fail!(<Error<T>>::PropertiesNotAllowed)280 fail!(<Error<T>>::SettingPropertiesNotAllowed)
281 }281 }
282282
283 fn set_token_properties(283 fn set_token_properties(
286 _token_id: TokenId,286 _token_id: TokenId,
287 _property: Vec<Property>,287 _property: Vec<Property>,
288 ) -> DispatchResultWithPostInfo {288 ) -> DispatchResultWithPostInfo {
289 fail!(<Error<T>>::PropertiesNotAllowed)289 fail!(<Error<T>>::SettingPropertiesNotAllowed)
290 }290 }
291291
292 fn set_property_permissions(292 fn set_property_permissions(
293 &self,293 &self,
294 _sender: &T::CrossAccountId,294 _sender: &T::CrossAccountId,
295 _property_permissions: Vec<PropertyKeyPermission>,295 _property_permissions: Vec<PropertyKeyPermission>,
296 ) -> DispatchResultWithPostInfo {296 ) -> DispatchResultWithPostInfo {
297 fail!(<Error<T>>::PropertiesNotAllowed)297 fail!(<Error<T>>::SettingPropertiesNotAllowed)
298 }298 }
299299
300 fn delete_token_properties(300 fn delete_token_properties(
303 _token_id: TokenId,303 _token_id: TokenId,
304 _property_keys: Vec<PropertyKey>,304 _property_keys: Vec<PropertyKey>,
305 ) -> DispatchResultWithPostInfo {305 ) -> DispatchResultWithPostInfo {
306 fail!(<Error<T>>::PropertiesNotAllowed)306 fail!(<Error<T>>::SettingPropertiesNotAllowed)
307 }307 }
308308
309 fn set_variable_metadata(309 fn set_variable_metadata(
363 .into_inner()363 .into_inner()
364 }364 }
365
366 fn token_properties(
367 &self,
368 _token_id: TokenId,
369 _keys: Vec<PropertyKey>
370 ) -> Vec<Property> {
371 Vec::new()
372 }
365373
366 fn total_supply(&self) -> u32 {374 fn total_supply(&self) -> u32 {
367 <Pallet<T>>::total_supply(self)375 <Pallet<T>>::total_supply(self)
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
62 WrongRefungiblePieces,62 WrongRefungiblePieces,
63 /// Refungible token can't nest other tokens63 /// Refungible token can't nest other tokens
64 RefungibleDisallowsNesting,64 RefungibleDisallowsNesting,
65 /// Item properties are not allowed65 /// Setting item properties is not allowed
66 PropertiesNotAllowed,66 SettingPropertiesNotAllowed,
67 }67 }
6868
69 #[pallet::config]69 #[pallet::config]
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
173 }173 }
174}174}
175
176#[derive(Encode, Decode, Clone, PartialEq, TypeInfo)]
177#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
178pub struct TokenData<CrossAccountId> {
179 pub const_data: Vec<u8>,
180 pub properties: Vec<Property>,
181 pub owner: Option<CrossAccountId>,
182}
175183
176pub struct OverflowError;184pub struct OverflowError;
177impl From<OverflowError> for &'static str {185impl From<OverflowError> for &'static str {
modifiedprimitives/rpc/src/lib.rsdiffbeforeafterboth
1818
19use up_data_structs::{19use up_data_structs::{
20 CollectionId, TokenId, RpcCollection, CollectionStats, CollectionLimits, Property,20 CollectionId, TokenId, RpcCollection, CollectionStats, CollectionLimits, Property,
21 PropertyKeyPermission,21 PropertyKeyPermission, TokenData,
22};22};
23use sp_std::vec::Vec;23use sp_std::vec::Vec;
24use codec::Decode;24use codec::Decode;
57 properties: Vec<Vec<u8>>57 properties: Vec<Vec<u8>>
58 ) -> Result<Vec<PropertyKeyPermission>>;58 ) -> Result<Vec<PropertyKeyPermission>>;
59
60 fn token_data(collection: CollectionId, token_id: TokenId, keys: Vec<Vec<u8>>) -> Result<TokenData<CrossAccountId>>;
5961
60 fn total_supply(collection: CollectionId) -> Result<u32>;62 fn total_supply(collection: CollectionId) -> Result<u32>;
61 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;63 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;
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
18 impl_runtime_apis! {10 impl_runtime_apis! {
19 $($($custom_apis)+)?11 $($($custom_apis)+)?
48 collection: CollectionId,40 collection: CollectionId,
49 keys: Vec<Vec<u8>>41 keys: Vec<Vec<u8>>
50 ) -> Result<Vec<Property>, DispatchError> {42 ) -> Result<Vec<Property>, DispatchError> {
51 let keys = bytes_keys_to_property_keys(keys)?;43 let keys = pallet_common::Pallet::<Runtime>::bytes_keys_to_property_keys(keys)?;
5244
53 let properties = pallet_common::Pallet::<Runtime>::collection_properties(collection);45 pallet_common::Pallet::<Runtime>::filter_collection_properties(collection, keys)
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 }46 }
6947
70 fn token_properties(48 fn token_properties(
71 collection: CollectionId,49 collection: CollectionId,
72 token_id: TokenId,50 token_id: TokenId,
73 keys: Vec<Vec<u8>>51 keys: Vec<Vec<u8>>
74 ) -> Result<Vec<Property>, DispatchError> {52 ) -> Result<Vec<Property>, DispatchError> {
75 let keys = bytes_keys_to_property_keys(keys)?;53 let keys = pallet_common::Pallet::<Runtime>::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| {54 dispatch_unique_runtime!(collection.token_properties(token_id, keys))
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 }55 }
9356
94 fn property_permissions(57 fn property_permissions(
95 collection: CollectionId,58 collection: CollectionId,
96 keys: Vec<Vec<u8>>59 keys: Vec<Vec<u8>>
97 ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {60 ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {
98 let keys = bytes_keys_to_property_keys(keys)?;61 let keys = pallet_common::Pallet::<Runtime>::bytes_keys_to_property_keys(keys)?;
9962
100 let permissions = pallet_common::Pallet::<Runtime>::property_permissions(collection);63 pallet_common::Pallet::<Runtime>::filter_property_permissions(collection, keys)
10164 }
65
66 fn token_data(
67 collection: CollectionId,
68 token_id: TokenId,
69 keys: Vec<Vec<u8>>
70 ) -> Result<TokenData<CrossAccountId>, DispatchError> {
102 let key_permissions = keys.into_iter()71 let token_data = TokenData {
103 .filter_map(|key| {72 const_data: Self::const_metadata(collection, token_id)?,
104 permissions.get(&key)73 properties: Self::token_properties(collection, token_id, keys)?,
105 .map(|permission| {
106 PropertyKeyPermission {
107 key,
108 permission: permission.clone()74 owner: Self::token_owner(collection, token_id)?
109 }
110 })
111 })
112 .collect();75 };
11376
114 Ok(key_permissions)77 Ok(token_data)
115 }78 }
11679
117 fn total_supply(collection: CollectionId) -> Result<u32, DispatchError> {80 fn total_supply(collection: CollectionId) -> Result<u32, DispatchError> {
118 dispatch_unique_runtime!(collection.total_supply())81 dispatch_unique_runtime!(collection.total_supply())