git.delta.rocks / unique-network / refs/commits / b92deacde238

difftreelog

feat return rpc error on not found collection

Yaroslav Bolyukin2021-12-06parent: #789924d.patch.diff
in: master

3 files changed

modifiedclient/rpc/src/lib.rsdiffbeforeafterboth
before · client/rpc/src/lib.rs
1use std::sync::Arc;23use codec::Decode;4use jsonrpc_core::{Error as RpcError, ErrorCode, Result};5use jsonrpc_derive::rpc;6use up_data_structs::{Collection, CollectionId, CollectionStats, TokenId};7use sp_api::{BlockId, BlockT, ProvideRuntimeApi};8use sp_blockchain::HeaderBackend;9use up_rpc::UniqueApi as UniqueRuntimeApi;1011#[rpc]12pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {13	#[rpc(name = "unique_accountTokens")]14	fn account_tokens(15		&self,16		collection: CollectionId,17		account: CrossAccountId,18		at: Option<BlockHash>,19	) -> Result<Vec<TokenId>>;20	#[rpc(name = "unique_tokenExists")]21	fn token_exists(22		&self,23		collection: CollectionId,24		token: TokenId,25		at: Option<BlockHash>,26	) -> Result<bool>;2728	#[rpc(name = "unique_tokenOwner")]29	fn token_owner(30		&self,31		collection: CollectionId,32		token: TokenId,33		at: Option<BlockHash>,34	) -> Result<CrossAccountId>;35	#[rpc(name = "unique_constMetadata")]36	fn const_metadata(37		&self,38		collection: CollectionId,39		token: TokenId,40		at: Option<BlockHash>,41	) -> Result<Vec<u8>>;42	#[rpc(name = "unique_variableMetadata")]43	fn variable_metadata(44		&self,45		collection: CollectionId,46		token: TokenId,47		at: Option<BlockHash>,48	) -> Result<Vec<u8>>;4950	#[rpc(name = "unique_collectionTokens")]51	fn collection_tokens(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;52	#[rpc(name = "unique_accountBalance")]53	fn account_balance(54		&self,55		collection: CollectionId,56		account: CrossAccountId,57		at: Option<BlockHash>,58	) -> Result<u32>;59	#[rpc(name = "unique_balance")]60	fn balance(61		&self,62		collection: CollectionId,63		account: CrossAccountId,64		token: TokenId,65		at: Option<BlockHash>,66	) -> Result<String>;67	#[rpc(name = "unique_allowance")]68	fn allowance(69		&self,70		collection: CollectionId,71		sender: CrossAccountId,72		spender: CrossAccountId,73		token: TokenId,74		at: Option<BlockHash>,75	) -> Result<String>;7677	#[rpc(name = "unique_adminlist")]78	fn adminlist(79		&self,80		collection: CollectionId,81		at: Option<BlockHash>,82	) -> Result<Vec<CrossAccountId>>;83	#[rpc(name = "unique_allowlist")]84	fn allowlist(85		&self,86		collection: CollectionId,87		at: Option<BlockHash>,88	) -> Result<Vec<CrossAccountId>>;89	#[rpc(name = "unique_allowed")]90	fn allowed(91		&self,92		collection: CollectionId,93		user: CrossAccountId,94		at: Option<BlockHash>,95	) -> Result<bool>;96	#[rpc(name = "unique_lastTokenId")]97	fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;98	#[rpc(name = "unique_collectionById")]99	fn collection_by_id(100		&self,101		collection: CollectionId,102		at: Option<BlockHash>,103	) -> Result<Option<Collection<AccountId>>>;104	#[rpc(name = "unique_collectionStats")]105	fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;106}107108pub struct Unique<C, P> {109	client: Arc<C>,110	_marker: std::marker::PhantomData<P>,111}112113impl<C, P> Unique<C, P> {114	pub fn new(client: Arc<C>) -> Self {115		Self {116			client,117			_marker: Default::default(),118		}119	}120}121122pub enum Error {123	RuntimeError,124}125126impl From<Error> for i64 {127	fn from(e: Error) -> i64 {128		match e {129			Error::RuntimeError => 1,130		}131	}132}133134macro_rules! pass_method {135	($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?) => {136		fn $method_name(137			&self,138			$(139				$name: $ty,140			)*141			at: Option<<Block as BlockT>::Hash>,142		) -> Result<$result> {143			let api = self.client.runtime_api();144			let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));145146			api.$method_name(&at, $($name),*).map_err(|e| RpcError {147				code: ErrorCode::ServerError(Error::RuntimeError.into()),148				message: "Unable to query".into(),149				data: Some(format!("{:?}", e).into()),150			}) $(151				.map($mapper)152			)?153		}154	};155}156157impl<C, Block, CrossAccountId, AccountId>158	UniqueApi<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>159where160	Block: BlockT,161	AccountId: Decode,162	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,163	C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,164	CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,165{166	pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);167	pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);168	pass_method!(token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId);169	pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);170	pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);171	pass_method!(collection_tokens(collection: CollectionId) -> u32);172	pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);173	pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());174	pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string());175176	pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>);177	pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>);178	pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool);179	pass_method!(last_token_id(collection: CollectionId) -> TokenId);180	pass_method!(collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>>);181	pass_method!(collection_stats() -> CollectionStats);182}
after · client/rpc/src/lib.rs
1use std::sync::Arc;23use codec::Decode;4use jsonrpc_core::{Error as RpcError, ErrorCode, Result};5use jsonrpc_derive::rpc;6use up_data_structs::{Collection, CollectionId, CollectionStats, TokenId};7use sp_api::{BlockId, BlockT, ProvideRuntimeApi};8use sp_blockchain::HeaderBackend;9use up_rpc::UniqueApi as UniqueRuntimeApi;1011#[rpc]12pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {13	#[rpc(name = "unique_accountTokens")]14	fn account_tokens(15		&self,16		collection: CollectionId,17		account: CrossAccountId,18		at: Option<BlockHash>,19	) -> Result<Vec<TokenId>>;20	#[rpc(name = "unique_tokenExists")]21	fn token_exists(22		&self,23		collection: CollectionId,24		token: TokenId,25		at: Option<BlockHash>,26	) -> Result<bool>;2728	#[rpc(name = "unique_tokenOwner")]29	fn token_owner(30		&self,31		collection: CollectionId,32		token: TokenId,33		at: Option<BlockHash>,34	) -> Result<CrossAccountId>;35	#[rpc(name = "unique_constMetadata")]36	fn const_metadata(37		&self,38		collection: CollectionId,39		token: TokenId,40		at: Option<BlockHash>,41	) -> Result<Vec<u8>>;42	#[rpc(name = "unique_variableMetadata")]43	fn variable_metadata(44		&self,45		collection: CollectionId,46		token: TokenId,47		at: Option<BlockHash>,48	) -> Result<Vec<u8>>;4950	#[rpc(name = "unique_collectionTokens")]51	fn collection_tokens(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;52	#[rpc(name = "unique_accountBalance")]53	fn account_balance(54		&self,55		collection: CollectionId,56		account: CrossAccountId,57		at: Option<BlockHash>,58	) -> Result<u32>;59	#[rpc(name = "unique_balance")]60	fn balance(61		&self,62		collection: CollectionId,63		account: CrossAccountId,64		token: TokenId,65		at: Option<BlockHash>,66	) -> Result<String>;67	#[rpc(name = "unique_allowance")]68	fn allowance(69		&self,70		collection: CollectionId,71		sender: CrossAccountId,72		spender: CrossAccountId,73		token: TokenId,74		at: Option<BlockHash>,75	) -> Result<String>;7677	#[rpc(name = "unique_adminlist")]78	fn adminlist(79		&self,80		collection: CollectionId,81		at: Option<BlockHash>,82	) -> Result<Vec<CrossAccountId>>;83	#[rpc(name = "unique_allowlist")]84	fn allowlist(85		&self,86		collection: CollectionId,87		at: Option<BlockHash>,88	) -> Result<Vec<CrossAccountId>>;89	#[rpc(name = "unique_allowed")]90	fn allowed(91		&self,92		collection: CollectionId,93		user: CrossAccountId,94		at: Option<BlockHash>,95	) -> Result<bool>;96	#[rpc(name = "unique_lastTokenId")]97	fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;98	#[rpc(name = "unique_collectionById")]99	fn collection_by_id(100		&self,101		collection: CollectionId,102		at: Option<BlockHash>,103	) -> Result<Option<Collection<AccountId>>>;104	#[rpc(name = "unique_collectionStats")]105	fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;106}107108pub struct Unique<C, P> {109	client: Arc<C>,110	_marker: std::marker::PhantomData<P>,111}112113impl<C, P> Unique<C, P> {114	pub fn new(client: Arc<C>) -> Self {115		Self {116			client,117			_marker: Default::default(),118		}119	}120}121122pub enum Error {123	RuntimeError,124}125126impl From<Error> for i64 {127	fn from(e: Error) -> i64 {128		match e {129			Error::RuntimeError => 1,130		}131	}132}133134macro_rules! pass_method {135	($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?) => {136		fn $method_name(137			&self,138			$(139				$name: $ty,140			)*141			at: Option<<Block as BlockT>::Hash>,142		) -> Result<$result> {143			let api = self.client.runtime_api();144			let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));145146			let result = api.$method_name(&at, $($name),*).map_err(|e| RpcError {147				code: ErrorCode::ServerError(Error::RuntimeError.into()),148				message: "Unable to query".into(),149				data: Some(format!("{:?}", e).into()),150			})?;151			result.map_err(|e| RpcError {152				code: ErrorCode::InvalidParams,153				message: "Runtime returned error".into(),154				data: Some(format!("{:?}", e).into()),155			})$(.map($mapper))?156		}157	};158}159160impl<C, Block, CrossAccountId, AccountId>161	UniqueApi<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>162where163	Block: BlockT,164	AccountId: Decode,165	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,166	C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,167	CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,168{169	pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);170	pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);171	pass_method!(token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId);172	pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);173	pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);174	pass_method!(collection_tokens(collection: CollectionId) -> u32);175	pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);176	pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());177	pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string());178179	pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>);180	pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>);181	pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool);182	pass_method!(last_token_id(collection: CollectionId) -> TokenId);183	pass_method!(collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>>);184	pass_method!(collection_stats() -> CollectionStats);185}
modifiedprimitives/rpc/src/lib.rsdiffbeforeafterboth
--- a/primitives/rpc/src/lib.rs
+++ b/primitives/rpc/src/lib.rs
@@ -4,37 +4,40 @@
 use sp_std::vec::Vec;
 use sp_core::H160;
 use codec::Decode;
+use sp_runtime::DispatchError;
+
+type Result<T> = core::result::Result<T, DispatchError>;
 
 sp_api::decl_runtime_apis! {
 	pub trait UniqueApi<CrossAccountId, AccountId> where
 		AccountId: Decode,
 		CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,
 	{
-		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>;
-		fn token_exists(collection: CollectionId, token: TokenId) -> bool;
+		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>>;
+		fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool>;
 
-		fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId;
-		fn const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;
-		fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;
+		fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId>;
+		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) -> u32;
-		fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32;
-		fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128;
+		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(
 			collection: CollectionId,
 			sender: CrossAccountId,
 			spender: CrossAccountId,
 			token: TokenId,
-		) -> u128;
+		) -> Result<u128>;
 
 		/// Used for ethereum integration
 		fn eth_contract_code(account: H160) -> Option<Vec<u8>>;
 
-		fn adminlist(collection: CollectionId) -> Vec<CrossAccountId>;
-		fn allowlist(collection: CollectionId) -> Vec<CrossAccountId>;
-		fn allowed(collection: CollectionId, user: CrossAccountId) -> bool;
-		fn last_token_id(collection: CollectionId) -> TokenId;
-		fn collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>>;
-		fn collection_stats() -> CollectionStats;
+		fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;
+		fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;
+		fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool>;
+		fn last_token_id(collection: CollectionId) -> Result<TokenId>;
+		fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>>;
+		fn collection_stats() -> Result<CollectionStats>;
 	}
 }
modifiedruntime/src/lib.rsdiffbeforeafterboth
--- a/runtime/src/lib.rs
+++ b/runtime/src/lib.rs
@@ -16,6 +16,7 @@
 
 use sp_api::impl_runtime_apis;
 use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H256, U256, H160};
+use sp_runtime::DispatchError;
 // #[cfg(any(feature = "std", test))]
 // pub use sp_runtime::BuildStorage;
 
@@ -1019,40 +1020,40 @@
 	($collection:ident.$method:ident($($name:ident),*)) => {{
 		use pallet_unique::dispatch::Dispatched;
 
-		let collection = Dispatched::dispatch(<pallet_common::CollectionHandle<Runtime>>::new($collection).unwrap());
+		let collection = Dispatched::dispatch(<pallet_common::CollectionHandle<Runtime>>::try_get($collection)?);
 		let dispatch = collection.as_dyn();
 
-		dispatch.$method($($name),*)
+		Ok(dispatch.$method($($name),*))
 	}};
 }
 impl_runtime_apis! {
 	impl up_rpc::UniqueApi<Block, CrossAccountId, AccountId>
 		for Runtime
 	{
-		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId> {
+		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>, DispatchError> {
 			dispatch_unique_runtime!(collection.account_tokens(account))
 		}
-		fn token_exists(collection: CollectionId, token: TokenId) -> bool {
+		fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool, DispatchError> {
 			dispatch_unique_runtime!(collection.token_exists(token))
 		}
 
-		fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId {
+		fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId, DispatchError> {
 			dispatch_unique_runtime!(collection.token_owner(token))
 		}
-		fn const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8> {
+		fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {
 			dispatch_unique_runtime!(collection.const_metadata(token))
 		}
-		fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8> {
+		fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {
 			dispatch_unique_runtime!(collection.variable_metadata(token))
 		}
 
-		fn collection_tokens(collection: CollectionId) -> u32 {
+		fn collection_tokens(collection: CollectionId) -> Result<u32, DispatchError> {
 			dispatch_unique_runtime!(collection.collection_tokens())
 		}
-		fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32 {
+		fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32, DispatchError> {
 			dispatch_unique_runtime!(collection.account_balance(account))
 		}
-		fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128 {
+		fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128, DispatchError> {
 			dispatch_unique_runtime!(collection.balance(account, token))
 		}
 		fn allowance(
@@ -1060,7 +1061,7 @@
 			sender: CrossAccountId,
 			spender: CrossAccountId,
 			token: TokenId,
-		) -> u128 {
+		) -> Result<u128, DispatchError> {
 			dispatch_unique_runtime!(collection.allowance(sender, spender, token))
 		}
 
@@ -1069,23 +1070,23 @@
 				.or_else(|| <pallet_evm_migration::OnMethodCall<Runtime>>::get_code(&account))
 				.or_else(|| <pallet_evm_contract_helpers::HelpersOnMethodCall<Self>>::get_code(&account))
 		}
-		fn adminlist(collection: CollectionId) -> Vec<CrossAccountId> {
-			<pallet_common::Pallet<Runtime>>::adminlist(collection)
+		fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {
+			Ok(<pallet_common::Pallet<Runtime>>::adminlist(collection))
 		}
-		fn allowlist(collection: CollectionId) -> Vec<CrossAccountId> {
-			<pallet_common::Pallet<Runtime>>::allowlist(collection)
+		fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {
+			Ok(<pallet_common::Pallet<Runtime>>::allowlist(collection))
 		}
-		fn allowed(collection: CollectionId, user: CrossAccountId) -> bool {
-			<pallet_common::Pallet<Runtime>>::allowed(collection, user)
+		fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool, DispatchError> {
+			Ok(<pallet_common::Pallet<Runtime>>::allowed(collection, user))
 		}
-		fn last_token_id(collection: CollectionId) -> TokenId {
+		fn last_token_id(collection: CollectionId) -> Result<TokenId, DispatchError> {
 			dispatch_unique_runtime!(collection.last_token_id())
 		}
-		fn collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>> {
-			<pallet_common::CollectionById<Runtime>>::get(collection)
+		fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>, DispatchError> {
+			Ok(<pallet_common::CollectionById<Runtime>>::get(collection))
 		}
-		fn collection_stats() -> CollectionStats {
-			<pallet_common::Pallet<Runtime>>::collection_stats()
+		fn collection_stats() -> Result<CollectionStats, DispatchError> {
+			Ok(<pallet_common::Pallet<Runtime>>::collection_stats())
 		}
 	}