difftreelog
fix(rpc) wrong return type for collectionTokens call
in: master
7 files changed
client/rpc/src/lib.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 std::sync::Arc;1819use codec::Decode;20use jsonrpc_core::{Error as RpcError, ErrorCode, Result};21use jsonrpc_derive::rpc;22use up_data_structs::{RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId};23use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};24use sp_blockchain::HeaderBackend;25use up_rpc::UniqueApi as UniqueRuntimeApi;2627#[rpc]28pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {29 #[rpc(name = "unique_accountTokens")]30 fn account_tokens(31 &self,32 collection: CollectionId,33 account: CrossAccountId,34 at: Option<BlockHash>,35 ) -> Result<Vec<TokenId>>;36 #[rpc(name = "unique_tokenExists")]37 fn token_exists(38 &self,39 collection: CollectionId,40 token: TokenId,41 at: Option<BlockHash>,42 ) -> Result<bool>;4344 #[rpc(name = "unique_tokenOwner")]45 fn token_owner(46 &self,47 collection: CollectionId,48 token: TokenId,49 at: Option<BlockHash>,50 ) -> Result<Option<CrossAccountId>>;51 #[rpc(name = "unique_topmostTokenOwner")]52 fn topmost_token_owner(53 &self,54 collection: CollectionId,55 token: TokenId,56 at: Option<BlockHash>,57 ) -> Result<Option<CrossAccountId>>;58 #[rpc(name = "unique_constMetadata")]59 fn const_metadata(60 &self,61 collection: CollectionId,62 token: TokenId,63 at: Option<BlockHash>,64 ) -> Result<Vec<u8>>;65 #[rpc(name = "unique_variableMetadata")]66 fn variable_metadata(67 &self,68 collection: CollectionId,69 token: TokenId,70 at: Option<BlockHash>,71 ) -> Result<Vec<u8>>;7273 #[rpc(name = "unique_collectionTokens")]74 fn collection_tokens(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;75 #[rpc(name = "unique_accountBalance")]76 fn account_balance(77 &self,78 collection: CollectionId,79 account: CrossAccountId,80 at: Option<BlockHash>,81 ) -> Result<u32>;82 #[rpc(name = "unique_balance")]83 fn balance(84 &self,85 collection: CollectionId,86 account: CrossAccountId,87 token: TokenId,88 at: Option<BlockHash>,89 ) -> Result<String>;90 #[rpc(name = "unique_allowance")]91 fn allowance(92 &self,93 collection: CollectionId,94 sender: CrossAccountId,95 spender: CrossAccountId,96 token: TokenId,97 at: Option<BlockHash>,98 ) -> Result<String>;99100 #[rpc(name = "unique_adminlist")]101 fn adminlist(102 &self,103 collection: CollectionId,104 at: Option<BlockHash>,105 ) -> Result<Vec<CrossAccountId>>;106 #[rpc(name = "unique_allowlist")]107 fn allowlist(108 &self,109 collection: CollectionId,110 at: Option<BlockHash>,111 ) -> Result<Vec<CrossAccountId>>;112 #[rpc(name = "unique_allowed")]113 fn allowed(114 &self,115 collection: CollectionId,116 user: CrossAccountId,117 at: Option<BlockHash>,118 ) -> Result<bool>;119 #[rpc(name = "unique_lastTokenId")]120 fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;121 #[rpc(name = "unique_collectionById")]122 fn collection_by_id(123 &self,124 collection: CollectionId,125 at: Option<BlockHash>,126 ) -> Result<Option<RpcCollection<AccountId>>>;127 #[rpc(name = "unique_collectionStats")]128 fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;129130 #[rpc(name = "unique_nextSponsored")]131 fn next_sponsored(132 &self,133 collection: CollectionId,134 account: CrossAccountId,135 token: TokenId,136 at: Option<BlockHash>,137 ) -> Result<Option<u64>>;138 #[rpc(name = "unique_effectiveCollectionLimits")]139 fn effective_collection_limits(140 &self,141 collection_id: CollectionId,142 at: Option<BlockHash>,143 ) -> Result<Option<CollectionLimits>>;144}145146pub struct Unique<C, P> {147 client: Arc<C>,148 _marker: std::marker::PhantomData<P>,149}150151impl<C, P> Unique<C, P> {152 pub fn new(client: Arc<C>) -> Self {153 Self {154 client,155 _marker: Default::default(),156 }157 }158}159160pub enum Error {161 RuntimeError,162}163164impl From<Error> for i64 {165 fn from(e: Error) -> i64 {166 match e {167 Error::RuntimeError => 1,168 }169 }170}171172macro_rules! pass_method {173 (174 $method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?175 $(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*176 ) => {177 fn $method_name(178 &self,179 $(180 $name: $ty,181 )*182 at: Option<<Block as BlockT>::Hash>,183 ) -> Result<$result> {184 let api = self.client.runtime_api();185 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));186 let _api_version = if let Ok(Some(api_version)) =187 api.api_version::<dyn UniqueRuntimeApi<Block, CrossAccountId, AccountId>>(&at)188 {189 api_version190 } else {191 // unreachable for our runtime192 return Err(RpcError {193 code: ErrorCode::InvalidParams,194 message: "Api is not available".into(),195 data: None,196 })197 };198199 let result = $(if _api_version < $ver {200 api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))201 } else)*202 { api.$method_name(&at, $($name),*) };203204 let result = result.map_err(|e| RpcError {205 code: ErrorCode::ServerError(Error::RuntimeError.into()),206 message: "Unable to query".into(),207 data: Some(format!("{:?}", e).into()),208 })?;209 result.map_err(|e| RpcError {210 code: ErrorCode::InvalidParams,211 message: "Runtime returned error".into(),212 data: Some(format!("{:?}", e).into()),213 })$(.map($mapper))?214 }215 };216}217218#[allow(deprecated)]219impl<C, Block, CrossAccountId, AccountId>220 UniqueApi<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>221where222 Block: BlockT,223 AccountId: Decode,224 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,225 C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,226 CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,227{228 pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);229 pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);230 pass_method!(231 token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>;232 changed_in 2, token_owner_before_version_2(collection, token) => |u| Some(u)233 );234 pass_method!(topmost_token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>);235 pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);236 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);237 pass_method!(collection_tokens(collection: CollectionId) -> u32);238 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);239 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());240 pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string());241242 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>);243 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>);244 pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool);245 pass_method!(last_token_id(collection: CollectionId) -> TokenId);246 pass_method!(collection_by_id(collection: CollectionId) -> Option<RpcCollection<AccountId>>);247 pass_method!(collection_stats() -> CollectionStats);248 pass_method!(next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Option<u64>);249 pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>);250}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 std::sync::Arc;1819use codec::Decode;20use jsonrpc_core::{Error as RpcError, ErrorCode, Result};21use jsonrpc_derive::rpc;22use up_data_structs::{RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId};23use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};24use sp_blockchain::HeaderBackend;25use up_rpc::UniqueApi as UniqueRuntimeApi;2627#[rpc]28pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {29 #[rpc(name = "unique_accountTokens")]30 fn account_tokens(31 &self,32 collection: CollectionId,33 account: CrossAccountId,34 at: Option<BlockHash>,35 ) -> Result<Vec<TokenId>>;36 #[rpc(name = "unique_collectionTokens")]37 fn collection_tokens(38 &self,39 collection: CollectionId,40 at: Option<BlockHash>,41 ) -> Result<Vec<TokenId>>;42 #[rpc(name = "unique_tokenExists")]43 fn token_exists(44 &self,45 collection: CollectionId,46 token: TokenId,47 at: Option<BlockHash>,48 ) -> Result<bool>;4950 #[rpc(name = "unique_tokenOwner")]51 fn token_owner(52 &self,53 collection: CollectionId,54 token: TokenId,55 at: Option<BlockHash>,56 ) -> Result<Option<CrossAccountId>>;57 #[rpc(name = "unique_topmostTokenOwner")]58 fn topmost_token_owner(59 &self,60 collection: CollectionId,61 token: TokenId,62 at: Option<BlockHash>,63 ) -> Result<Option<CrossAccountId>>;64 #[rpc(name = "unique_constMetadata")]65 fn const_metadata(66 &self,67 collection: CollectionId,68 token: TokenId,69 at: Option<BlockHash>,70 ) -> Result<Vec<u8>>;71 #[rpc(name = "unique_variableMetadata")]72 fn variable_metadata(73 &self,74 collection: CollectionId,75 token: TokenId,76 at: Option<BlockHash>,77 ) -> Result<Vec<u8>>;7879 #[rpc(name = "unique_accountBalance")]80 fn account_balance(81 &self,82 collection: CollectionId,83 account: CrossAccountId,84 at: Option<BlockHash>,85 ) -> Result<u32>;86 #[rpc(name = "unique_balance")]87 fn balance(88 &self,89 collection: CollectionId,90 account: CrossAccountId,91 token: TokenId,92 at: Option<BlockHash>,93 ) -> Result<String>;94 #[rpc(name = "unique_allowance")]95 fn allowance(96 &self,97 collection: CollectionId,98 sender: CrossAccountId,99 spender: CrossAccountId,100 token: TokenId,101 at: Option<BlockHash>,102 ) -> Result<String>;103104 #[rpc(name = "unique_adminlist")]105 fn adminlist(106 &self,107 collection: CollectionId,108 at: Option<BlockHash>,109 ) -> Result<Vec<CrossAccountId>>;110 #[rpc(name = "unique_allowlist")]111 fn allowlist(112 &self,113 collection: CollectionId,114 at: Option<BlockHash>,115 ) -> Result<Vec<CrossAccountId>>;116 #[rpc(name = "unique_allowed")]117 fn allowed(118 &self,119 collection: CollectionId,120 user: CrossAccountId,121 at: Option<BlockHash>,122 ) -> Result<bool>;123 #[rpc(name = "unique_lastTokenId")]124 fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;125 #[rpc(name = "unique_collectionById")]126 fn collection_by_id(127 &self,128 collection: CollectionId,129 at: Option<BlockHash>,130 ) -> Result<Option<RpcCollection<AccountId>>>;131 #[rpc(name = "unique_collectionStats")]132 fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;133134 #[rpc(name = "unique_nextSponsored")]135 fn next_sponsored(136 &self,137 collection: CollectionId,138 account: CrossAccountId,139 token: TokenId,140 at: Option<BlockHash>,141 ) -> Result<Option<u64>>;142 #[rpc(name = "unique_effectiveCollectionLimits")]143 fn effective_collection_limits(144 &self,145 collection_id: CollectionId,146 at: Option<BlockHash>,147 ) -> Result<Option<CollectionLimits>>;148}149150pub struct Unique<C, P> {151 client: Arc<C>,152 _marker: std::marker::PhantomData<P>,153}154155impl<C, P> Unique<C, P> {156 pub fn new(client: Arc<C>) -> Self {157 Self {158 client,159 _marker: Default::default(),160 }161 }162}163164pub enum Error {165 RuntimeError,166}167168impl From<Error> for i64 {169 fn from(e: Error) -> i64 {170 match e {171 Error::RuntimeError => 1,172 }173 }174}175176macro_rules! pass_method {177 (178 $method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?179 $(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*180 ) => {181 fn $method_name(182 &self,183 $(184 $name: $ty,185 )*186 at: Option<<Block as BlockT>::Hash>,187 ) -> Result<$result> {188 let api = self.client.runtime_api();189 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));190 let _api_version = if let Ok(Some(api_version)) =191 api.api_version::<dyn UniqueRuntimeApi<Block, CrossAccountId, AccountId>>(&at)192 {193 api_version194 } else {195 // unreachable for our runtime196 return Err(RpcError {197 code: ErrorCode::InvalidParams,198 message: "Api is not available".into(),199 data: None,200 })201 };202203 let result = $(if _api_version < $ver {204 api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))205 } else)*206 { api.$method_name(&at, $($name),*) };207208 let result = result.map_err(|e| RpcError {209 code: ErrorCode::ServerError(Error::RuntimeError.into()),210 message: "Unable to query".into(),211 data: Some(format!("{:?}", e).into()),212 })?;213 result.map_err(|e| RpcError {214 code: ErrorCode::InvalidParams,215 message: "Runtime returned error".into(),216 data: Some(format!("{:?}", e).into()),217 })$(.map($mapper))?218 }219 };220}221222#[allow(deprecated)]223impl<C, Block, CrossAccountId, AccountId>224 UniqueApi<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>225where226 Block: BlockT,227 AccountId: Decode,228 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,229 C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,230 CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,231{232 pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);233 pass_method!(collection_tokens(collection: CollectionId) -> Vec<TokenId>);234 pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);235 pass_method!(236 token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>;237 changed_in 2, token_owner_before_version_2(collection, token) => |u| Some(u)238 );239 pass_method!(topmost_token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>);240 pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);241 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);242 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);243 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());244 pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string());245246 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>);247 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>);248 pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool);249 pass_method!(last_token_id(collection: CollectionId) -> TokenId);250 pass_method!(collection_by_id(collection: CollectionId) -> Option<RpcCollection<AccountId>>);251 pass_method!(collection_stats() -> CollectionStats);252 pass_method!(next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Option<u64>);253 pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>);254}pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -924,6 +924,7 @@
) -> DispatchResult;
fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;
+ fn collection_tokens(&self) -> Vec<TokenId>;
fn token_exists(&self, token: TokenId) -> bool;
fn last_token_id(&self) -> TokenId;
@@ -931,8 +932,6 @@
fn const_metadata(&self, token: TokenId) -> Vec<u8>;
fn variable_metadata(&self, token: TokenId) -> Vec<u8>;
- /// How many tokens collection contains (Applicable to nonfungible/refungible)
- fn collection_tokens(&self) -> u32;
/// Amount of different tokens account has (Applicable to nonfungible/refungible)
fn account_balance(&self, account: T::CrossAccountId) -> u32;
/// Amount of specific token account have (Applicable to fungible/refungible)
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -270,8 +270,8 @@
Vec::new()
}
- fn collection_tokens(&self) -> u32 {
- 1
+ fn collection_tokens(&self) -> Vec<TokenId> {
+ vec![TokenId::default()]
}
fn account_balance(&self, account: T::CrossAccountId) -> u32 {
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -264,6 +264,12 @@
.collect()
}
+ fn collection_tokens(&self) -> Vec<TokenId> {
+ <TokenData<T>>::iter_prefix((self.id,))
+ .map(|(id, _)| id)
+ .collect()
+ }
+
fn token_exists(&self, token: TokenId) -> bool {
<Pallet<T>>::token_exists(self, token)
}
@@ -286,10 +292,6 @@
.map(|t| t.variable_data)
.unwrap_or_default()
.into_inner()
- }
-
- fn collection_tokens(&self) -> u32 {
- <Pallet<T>>::total_supply(self)
}
fn account_balance(&self, account: T::CrossAccountId) -> u32 {
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -273,6 +273,12 @@
.collect()
}
+ fn collection_tokens(&self) -> Vec<TokenId> {
+ <TokenData<T>>::iter_prefix((self.id,))
+ .map(|(id, _)| id)
+ .collect()
+ }
+
fn token_exists(&self, token: TokenId) -> bool {
<Pallet<T>>::token_exists(self, token)
}
@@ -293,10 +299,6 @@
<TokenData<T>>::get((self.id, token))
.variable_data
.into_inner()
- }
-
- fn collection_tokens(&self) -> u32 {
- <Pallet<T>>::total_supply(self)
}
fn account_balance(&self, account: T::CrossAccountId) -> u32 {
primitives/rpc/src/lib.rsdiffbeforeafterboth--- a/primitives/rpc/src/lib.rs
+++ b/primitives/rpc/src/lib.rs
@@ -33,6 +33,7 @@
fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId>;
fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>>;
+ fn collection_tokens(collection: CollectionId) -> Result<Vec<TokenId>>;
fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool>;
fn token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>>;
@@ -40,7 +41,6 @@
fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
- fn collection_tokens(collection: CollectionId) -> Result<u32>;
fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;
fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128>;
fn allowance(
runtime/common/src/runtime_apis.rsdiffbeforeafterboth--- a/runtime/common/src/runtime_apis.rs
+++ b/runtime/common/src/runtime_apis.rs
@@ -33,7 +33,7 @@
dispatch_unique_runtime!(collection.variable_metadata(token))
}
- fn collection_tokens(collection: CollectionId) -> Result<u32, DispatchError> {
+ fn collection_tokens(collection: CollectionId) -> Result<Vec<TokenId>, DispatchError> {
dispatch_unique_runtime!(collection.collection_tokens())
}
fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32, DispatchError> {