difftreelog
Add token_data RPC
in: master
10 files changed
client/rpc/src/lib.rsdiffbeforeafterboth21use 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>>;106107 #[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>>;106115107 #[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>);305306 pass_method!(token_data(307 collection: CollectionId,308 token_id: TokenId,309310 #[map(|keys| string_keys_to_bytes_keys(keys))]311 keys: Vec<String>,312 ) -> TokenData<CrossAccountId>);296313297 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);pallets/common/src/lib.rsdiffbeforeafterboth36 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 }846847 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 error851 key.try_into().map_err(|_| DispatchError::Other("Can't read property key"))852 })853 .collect::<Result<Vec<PropertyKey>, DispatchError>>()854 }855856 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);861862 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();873874 Ok(properties)875 }876877 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);882883 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();894895 Ok(key_permissions)896 }845897846 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 tokens1122 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)pallets/fungible/src/common.rsdiffbeforeafterboth250 _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 }255255256 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 }263263264 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 }272272273 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 }280280281 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 }289289290 fn set_variable_metadata(290 fn set_variable_metadata(336 Vec::new()336 Vec::new()337 }337 }338339 fn token_properties(340 &self,341 _token_id: TokenId,342 _keys: Vec<PropertyKey>343 ) -> Vec<Property> {344 Vec::new()345 }338346339 fn total_supply(&self) -> u32 {347 fn total_supply(&self) -> u32 {340 1348 1pallets/fungible/src/lib.rsdiffbeforeafterboth61 FungibleItemsDontHaveData,61 FungibleItemsDontHaveData,62 /// Fungible token does not support nested62 /// Fungible token does not support nested63 FungibleDisallowsNesting,63 FungibleDisallowsNesting,64 /// Item properties are not allowed64 /// Setting item properties is not allowed65 PropertiesNotAllowed,65 SettingPropertiesNotAllowed,66 }66 }676768 #[pallet::config]68 #[pallet::config]pallets/nonfungible/src/common.rsdiffbeforeafterboth386 .into_inner()386 .into_inner()387 }387 }388389 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));395396 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 }388408389 fn total_supply(&self) -> u32 {409 fn total_supply(&self) -> u32 {390 <Pallet<T>>::total_supply(self)410 <Pallet<T>>::total_supply(self)pallets/refungible/src/common.rsdiffbeforeafterboth269 _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 }274274275 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 }282282283 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 }291291292 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 }299299300 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 }308308309 fn set_variable_metadata(309 fn set_variable_metadata(363 .into_inner()363 .into_inner()364 }364 }365366 fn token_properties(367 &self,368 _token_id: TokenId,369 _keys: Vec<PropertyKey>370 ) -> Vec<Property> {371 Vec::new()372 }365373366 fn total_supply(&self) -> u32 {374 fn total_supply(&self) -> u32 {367 <Pallet<T>>::total_supply(self)375 <Pallet<T>>::total_supply(self)pallets/refungible/src/lib.rsdiffbeforeafterboth62 WrongRefungiblePieces,62 WrongRefungiblePieces,63 /// Refungible token can't nest other tokens63 /// Refungible token can't nest other tokens64 RefungibleDisallowsNesting,64 RefungibleDisallowsNesting,65 /// Item properties are not allowed65 /// Setting item properties is not allowed66 PropertiesNotAllowed,66 SettingPropertiesNotAllowed,67 }67 }686869 #[pallet::config]69 #[pallet::config]primitives/data-structs/src/lib.rsdiffbeforeafterboth173 }173 }174}174}175176#[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}175183176pub struct OverflowError;184pub struct OverflowError;177impl From<OverflowError> for &'static str {185impl From<OverflowError> for &'static str {primitives/rpc/src/lib.rsdiffbeforeafterboth181819use 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>>;5960 fn token_data(collection: CollectionId, token_id: TokenId, keys: Vec<Vec<u8>>) -> Result<TokenData<CrossAccountId>>;596160 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>;runtime/common/src/runtime_apis.rsdiffbeforeafterboth7 $($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 }1718 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)?;524453 let properties = pallet_common::Pallet::<Runtime>::collection_properties(collection);45 pallet_common::Pallet::<Runtime>::filter_collection_properties(collection, keys)5455 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();6667 Ok(properties)68 }46 }694770 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)?;7677 let properties = pallet_nonfungible::Pallet::<Runtime>::token_properties((collection, token_id));7879 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();9091 Ok(properties)92 }55 }935694 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)?;9962100 let permissions = pallet_common::Pallet::<Runtime>::property_permissions(collection);63 pallet_common::Pallet::<Runtime>::filter_property_permissions(collection, keys)10164 }6566 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 };11376114 Ok(key_permissions)77 Ok(token_data)115 }78 }11679117 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())