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.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)
pallets/fungible/src/common.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use core::marker::PhantomData;1819use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};20use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::ArithmeticError;23use sp_std::{vec::Vec, vec};24use up_data_structs::{CustomDataLimit, Property, PropertyKey, PropertyKeyPermission};2526use crate::{27 Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,28};2930pub struct CommonWeights<T: Config>(PhantomData<T>);31impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {32 fn create_item() -> Weight {33 <SelfWeightOf<T>>::create_item()34 }3536 fn create_multiple_items(_amount: u32) -> Weight {37 Self::create_item()38 }3940 fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {41 match data {42 CreateItemExData::Fungible(f) => {43 <SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)44 }45 _ => 0,46 }47 }4849 fn burn_item() -> Weight {50 <SelfWeightOf<T>>::burn_item()51 }5253 fn set_collection_properties(amount: u32) -> Weight {54 <SelfWeightOf<T>>::set_collection_properties(amount)55 }5657 fn delete_collection_properties(amount: u32) -> Weight {58 <SelfWeightOf<T>>::delete_collection_properties(amount)59 }6061 fn set_token_properties(amount: u32) -> Weight {62 <SelfWeightOf<T>>::set_token_properties(amount)63 }6465 fn delete_token_properties(amount: u32) -> Weight {66 <SelfWeightOf<T>>::delete_token_properties(amount)67 }6869 fn set_property_permissions(amount: u32) -> Weight {70 <SelfWeightOf<T>>::set_property_permissions(amount)71 }7273 fn transfer() -> Weight {74 <SelfWeightOf<T>>::transfer()75 }7677 fn approve() -> Weight {78 <SelfWeightOf<T>>::approve()79 }8081 fn transfer_from() -> Weight {82 <SelfWeightOf<T>>::transfer_from()83 }8485 fn burn_from() -> Weight {86 <SelfWeightOf<T>>::burn_from()87 }8889 fn set_variable_metadata(_bytes: u32) -> Weight {90 // Error91 092 }93}9495impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {96 fn create_item(97 &self,98 sender: T::CrossAccountId,99 to: T::CrossAccountId,100 data: up_data_structs::CreateItemData,101 nesting_budget: &dyn Budget,102 ) -> DispatchResultWithPostInfo {103 match data {104 up_data_structs::CreateItemData::Fungible(data) => with_weight(105 <Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),106 <CommonWeights<T>>::create_item(),107 ),108 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),109 }110 }111112 fn create_multiple_items(113 &self,114 sender: T::CrossAccountId,115 to: T::CrossAccountId,116 data: Vec<up_data_structs::CreateItemData>,117 nesting_budget: &dyn Budget,118 ) -> DispatchResultWithPostInfo {119 let mut sum: u128 = 0;120 for data in data {121 match data {122 up_data_structs::CreateItemData::Fungible(data) => {123 sum = sum124 .checked_add(data.value)125 .ok_or(ArithmeticError::Overflow)?;126 }127 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),128 }129 }130131 with_weight(132 <Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),133 <CommonWeights<T>>::create_item(),134 )135 }136137 fn create_multiple_items_ex(138 &self,139 sender: <T>::CrossAccountId,140 data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,141 nesting_budget: &dyn Budget,142 ) -> DispatchResultWithPostInfo {143 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);144 let data = match data {145 up_data_structs::CreateItemExData::Fungible(f) => f,146 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),147 };148149 with_weight(150 <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),151 weight,152 )153 }154155 fn burn_item(156 &self,157 sender: T::CrossAccountId,158 token: TokenId,159 amount: u128,160 ) -> DispatchResultWithPostInfo {161 ensure!(162 token == TokenId::default(),163 <Error<T>>::FungibleItemsHaveNoId164 );165166 with_weight(167 <Pallet<T>>::burn(self, &sender, amount),168 <CommonWeights<T>>::burn_item(),169 )170 }171172 fn transfer(173 &self,174 from: T::CrossAccountId,175 to: T::CrossAccountId,176 token: TokenId,177 amount: u128,178 nesting_budget: &dyn Budget,179 ) -> DispatchResultWithPostInfo {180 ensure!(181 token == TokenId::default(),182 <Error<T>>::FungibleItemsHaveNoId183 );184185 with_weight(186 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),187 <CommonWeights<T>>::transfer(),188 )189 }190191 fn approve(192 &self,193 sender: T::CrossAccountId,194 spender: T::CrossAccountId,195 token: TokenId,196 amount: u128,197 ) -> DispatchResultWithPostInfo {198 ensure!(199 token == TokenId::default(),200 <Error<T>>::FungibleItemsHaveNoId201 );202203 with_weight(204 <Pallet<T>>::set_allowance(self, &sender, &spender, amount),205 <CommonWeights<T>>::approve(),206 )207 }208209 fn transfer_from(210 &self,211 sender: T::CrossAccountId,212 from: T::CrossAccountId,213 to: T::CrossAccountId,214 token: TokenId,215 amount: u128,216 nesting_budget: &dyn Budget,217 ) -> DispatchResultWithPostInfo {218 ensure!(219 token == TokenId::default(),220 <Error<T>>::FungibleItemsHaveNoId221 );222223 with_weight(224 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),225 <CommonWeights<T>>::transfer_from(),226 )227 }228229 fn burn_from(230 &self,231 sender: T::CrossAccountId,232 from: T::CrossAccountId,233 token: TokenId,234 amount: u128,235 nesting_budget: &dyn Budget,236 ) -> DispatchResultWithPostInfo {237 ensure!(238 token == TokenId::default(),239 <Error<T>>::FungibleItemsHaveNoId240 );241242 with_weight(243 <Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),244 <CommonWeights<T>>::burn_from(),245 )246 }247248 fn set_collection_properties(249 &self,250 _sender: T::CrossAccountId,251 _property: Vec<Property>,252 ) -> DispatchResultWithPostInfo {253 fail!(<Error<T>>::PropertiesNotAllowed)254 }255256 fn delete_collection_properties(257 &self,258 _sender: &T::CrossAccountId,259 _property_keys: Vec<PropertyKey>,260 ) -> DispatchResultWithPostInfo {261 fail!(<Error<T>>::PropertiesNotAllowed)262 }263264 fn set_token_properties(265 &self,266 _sender: T::CrossAccountId,267 _token_id: TokenId,268 _property: Vec<Property>,269 ) -> DispatchResultWithPostInfo {270 fail!(<Error<T>>::PropertiesNotAllowed)271 }272273 fn set_property_permissions(274 &self,275 _sender: &T::CrossAccountId,276 _property_permissions: Vec<PropertyKeyPermission>,277 ) -> DispatchResultWithPostInfo {278 fail!(<Error<T>>::PropertiesNotAllowed)279 }280281 fn delete_token_properties(282 &self,283 _sender: T::CrossAccountId,284 _token_id: TokenId,285 _property_keys: Vec<PropertyKey>,286 ) -> DispatchResultWithPostInfo {287 fail!(<Error<T>>::PropertiesNotAllowed)288 }289290 fn set_variable_metadata(291 &self,292 _sender: T::CrossAccountId,293 _token: TokenId,294 _data: BoundedVec<u8, CustomDataLimit>,295 ) -> DispatchResultWithPostInfo {296 fail!(<Error<T>>::FungibleItemsDontHaveData)297 }298299 fn check_nesting(300 &self,301 _sender: <T>::CrossAccountId,302 _from: (CollectionId, TokenId),303 _under: TokenId,304 _budget: &dyn Budget,305 ) -> sp_runtime::DispatchResult {306 fail!(<Error<T>>::FungibleDisallowsNesting)307 }308309 fn collection_tokens(&self) -> Vec<TokenId> {310 vec![TokenId::default()]311 }312313 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {314 if <Balance<T>>::get((self.id, account)) != 0 {315 vec![TokenId::default()]316 } else {317 vec![]318 }319 }320321 fn token_exists(&self, token: TokenId) -> bool {322 token == TokenId::default()323 }324325 fn last_token_id(&self) -> TokenId {326 TokenId::default()327 }328329 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {330 None331 }332 fn const_metadata(&self, _token: TokenId) -> Vec<u8> {333 Vec::new()334 }335 fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {336 Vec::new()337 }338339 fn total_supply(&self) -> u32 {340 1341 }342343 fn account_balance(&self, account: T::CrossAccountId) -> u32 {344 if <Balance<T>>::get((self.id, account)) != 0 {345 1346 } else {347 0348 }349 }350351 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {352 if token != TokenId::default() {353 return 0;354 }355 <Balance<T>>::get((self.id, account))356 }357358 fn allowance(359 &self,360 sender: T::CrossAccountId,361 spender: T::CrossAccountId,362 token: TokenId,363 ) -> u128 {364 if token != TokenId::default() {365 return 0;366 }367 <Allowance<T>>::get((self.id, sender, spender))368 }369}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use core::marker::PhantomData;1819use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};20use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::ArithmeticError;23use sp_std::{vec::Vec, vec};24use up_data_structs::{CustomDataLimit, Property, PropertyKey, PropertyKeyPermission};2526use crate::{27 Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,28};2930pub struct CommonWeights<T: Config>(PhantomData<T>);31impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {32 fn create_item() -> Weight {33 <SelfWeightOf<T>>::create_item()34 }3536 fn create_multiple_items(_amount: u32) -> Weight {37 Self::create_item()38 }3940 fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {41 match data {42 CreateItemExData::Fungible(f) => {43 <SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)44 }45 _ => 0,46 }47 }4849 fn burn_item() -> Weight {50 <SelfWeightOf<T>>::burn_item()51 }5253 fn set_collection_properties(amount: u32) -> Weight {54 <SelfWeightOf<T>>::set_collection_properties(amount)55 }5657 fn delete_collection_properties(amount: u32) -> Weight {58 <SelfWeightOf<T>>::delete_collection_properties(amount)59 }6061 fn set_token_properties(amount: u32) -> Weight {62 <SelfWeightOf<T>>::set_token_properties(amount)63 }6465 fn delete_token_properties(amount: u32) -> Weight {66 <SelfWeightOf<T>>::delete_token_properties(amount)67 }6869 fn set_property_permissions(amount: u32) -> Weight {70 <SelfWeightOf<T>>::set_property_permissions(amount)71 }7273 fn transfer() -> Weight {74 <SelfWeightOf<T>>::transfer()75 }7677 fn approve() -> Weight {78 <SelfWeightOf<T>>::approve()79 }8081 fn transfer_from() -> Weight {82 <SelfWeightOf<T>>::transfer_from()83 }8485 fn burn_from() -> Weight {86 <SelfWeightOf<T>>::burn_from()87 }8889 fn set_variable_metadata(_bytes: u32) -> Weight {90 // Error91 092 }93}9495impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {96 fn create_item(97 &self,98 sender: T::CrossAccountId,99 to: T::CrossAccountId,100 data: up_data_structs::CreateItemData,101 nesting_budget: &dyn Budget,102 ) -> DispatchResultWithPostInfo {103 match data {104 up_data_structs::CreateItemData::Fungible(data) => with_weight(105 <Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),106 <CommonWeights<T>>::create_item(),107 ),108 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),109 }110 }111112 fn create_multiple_items(113 &self,114 sender: T::CrossAccountId,115 to: T::CrossAccountId,116 data: Vec<up_data_structs::CreateItemData>,117 nesting_budget: &dyn Budget,118 ) -> DispatchResultWithPostInfo {119 let mut sum: u128 = 0;120 for data in data {121 match data {122 up_data_structs::CreateItemData::Fungible(data) => {123 sum = sum124 .checked_add(data.value)125 .ok_or(ArithmeticError::Overflow)?;126 }127 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),128 }129 }130131 with_weight(132 <Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),133 <CommonWeights<T>>::create_item(),134 )135 }136137 fn create_multiple_items_ex(138 &self,139 sender: <T>::CrossAccountId,140 data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,141 nesting_budget: &dyn Budget,142 ) -> DispatchResultWithPostInfo {143 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);144 let data = match data {145 up_data_structs::CreateItemExData::Fungible(f) => f,146 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),147 };148149 with_weight(150 <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),151 weight,152 )153 }154155 fn burn_item(156 &self,157 sender: T::CrossAccountId,158 token: TokenId,159 amount: u128,160 ) -> DispatchResultWithPostInfo {161 ensure!(162 token == TokenId::default(),163 <Error<T>>::FungibleItemsHaveNoId164 );165166 with_weight(167 <Pallet<T>>::burn(self, &sender, amount),168 <CommonWeights<T>>::burn_item(),169 )170 }171172 fn transfer(173 &self,174 from: T::CrossAccountId,175 to: T::CrossAccountId,176 token: TokenId,177 amount: u128,178 nesting_budget: &dyn Budget,179 ) -> DispatchResultWithPostInfo {180 ensure!(181 token == TokenId::default(),182 <Error<T>>::FungibleItemsHaveNoId183 );184185 with_weight(186 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),187 <CommonWeights<T>>::transfer(),188 )189 }190191 fn approve(192 &self,193 sender: T::CrossAccountId,194 spender: T::CrossAccountId,195 token: TokenId,196 amount: u128,197 ) -> DispatchResultWithPostInfo {198 ensure!(199 token == TokenId::default(),200 <Error<T>>::FungibleItemsHaveNoId201 );202203 with_weight(204 <Pallet<T>>::set_allowance(self, &sender, &spender, amount),205 <CommonWeights<T>>::approve(),206 )207 }208209 fn transfer_from(210 &self,211 sender: T::CrossAccountId,212 from: T::CrossAccountId,213 to: T::CrossAccountId,214 token: TokenId,215 amount: u128,216 nesting_budget: &dyn Budget,217 ) -> DispatchResultWithPostInfo {218 ensure!(219 token == TokenId::default(),220 <Error<T>>::FungibleItemsHaveNoId221 );222223 with_weight(224 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),225 <CommonWeights<T>>::transfer_from(),226 )227 }228229 fn burn_from(230 &self,231 sender: T::CrossAccountId,232 from: T::CrossAccountId,233 token: TokenId,234 amount: u128,235 nesting_budget: &dyn Budget,236 ) -> DispatchResultWithPostInfo {237 ensure!(238 token == TokenId::default(),239 <Error<T>>::FungibleItemsHaveNoId240 );241242 with_weight(243 <Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),244 <CommonWeights<T>>::burn_from(),245 )246 }247248 fn set_collection_properties(249 &self,250 _sender: T::CrossAccountId,251 _property: Vec<Property>,252 ) -> DispatchResultWithPostInfo {253 fail!(<Error<T>>::SettingPropertiesNotAllowed)254 }255256 fn delete_collection_properties(257 &self,258 _sender: &T::CrossAccountId,259 _property_keys: Vec<PropertyKey>,260 ) -> DispatchResultWithPostInfo {261 fail!(<Error<T>>::SettingPropertiesNotAllowed)262 }263264 fn set_token_properties(265 &self,266 _sender: T::CrossAccountId,267 _token_id: TokenId,268 _property: Vec<Property>,269 ) -> DispatchResultWithPostInfo {270 fail!(<Error<T>>::SettingPropertiesNotAllowed)271 }272273 fn set_property_permissions(274 &self,275 _sender: &T::CrossAccountId,276 _property_permissions: Vec<PropertyKeyPermission>,277 ) -> DispatchResultWithPostInfo {278 fail!(<Error<T>>::SettingPropertiesNotAllowed)279 }280281 fn delete_token_properties(282 &self,283 _sender: T::CrossAccountId,284 _token_id: TokenId,285 _property_keys: Vec<PropertyKey>,286 ) -> DispatchResultWithPostInfo {287 fail!(<Error<T>>::SettingPropertiesNotAllowed)288 }289290 fn set_variable_metadata(291 &self,292 _sender: T::CrossAccountId,293 _token: TokenId,294 _data: BoundedVec<u8, CustomDataLimit>,295 ) -> DispatchResultWithPostInfo {296 fail!(<Error<T>>::FungibleItemsDontHaveData)297 }298299 fn check_nesting(300 &self,301 _sender: <T>::CrossAccountId,302 _from: (CollectionId, TokenId),303 _under: TokenId,304 _budget: &dyn Budget,305 ) -> sp_runtime::DispatchResult {306 fail!(<Error<T>>::FungibleDisallowsNesting)307 }308309 fn collection_tokens(&self) -> Vec<TokenId> {310 vec![TokenId::default()]311 }312313 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {314 if <Balance<T>>::get((self.id, account)) != 0 {315 vec![TokenId::default()]316 } else {317 vec![]318 }319 }320321 fn token_exists(&self, token: TokenId) -> bool {322 token == TokenId::default()323 }324325 fn last_token_id(&self) -> TokenId {326 TokenId::default()327 }328329 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {330 None331 }332 fn const_metadata(&self, _token: TokenId) -> Vec<u8> {333 Vec::new()334 }335 fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {336 Vec::new()337 }338339 fn token_properties(340 &self,341 _token_id: TokenId,342 _keys: Vec<PropertyKey>343 ) -> Vec<Property> {344 Vec::new()345 }346347 fn total_supply(&self) -> u32 {348 1349 }350351 fn account_balance(&self, account: T::CrossAccountId) -> u32 {352 if <Balance<T>>::get((self.id, account)) != 0 {353 1354 } else {355 0356 }357 }358359 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {360 if token != TokenId::default() {361 return 0;362 }363 <Balance<T>>::get((self.id, account))364 }365366 fn allowance(367 &self,368 sender: T::CrossAccountId,369 spender: T::CrossAccountId,370 token: TokenId,371 ) -> u128 {372 if token != TokenId::default() {373 return 0;374 }375 <Allowance<T>>::get((self.id, sender, spender))376 }377}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> {