difftreelog
Add token_data RPC
in: master
10 files changed
client/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());
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.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
}
pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -61,8 +61,8 @@
FungibleItemsDontHaveData,
/// Fungible token does not support nested
FungibleDisallowsNesting,
- /// Item properties are not allowed
- PropertiesNotAllowed,
+ /// Setting item properties is not allowed
+ SettingPropertiesNotAllowed,
}
#[pallet::config]
pallets/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)
}
pallets/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)
}
pallets/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]
primitives/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 {
primitives/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>;
runtime/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> {