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
143 let api = self.client.runtime_api();143 let api = self.client.runtime_api();
144 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));144 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
145145
146 api.$method_name(&at, $($name),*).map_err(|e| RpcError {146 let result = api.$method_name(&at, $($name),*).map_err(|e| RpcError {
147 code: ErrorCode::ServerError(Error::RuntimeError.into()),147 code: ErrorCode::ServerError(Error::RuntimeError.into()),
148 message: "Unable to query".into(),148 message: "Unable to query".into(),
149 data: Some(format!("{:?}", e).into()),149 data: Some(format!("{:?}", e).into()),
150 }) $(150 })?;
151 result.map_err(|e| RpcError {
152 code: ErrorCode::InvalidParams,
153 message: "Runtime returned error".into(),
154 data: Some(format!("{:?}", e).into()),
151 .map($mapper)155 })$(.map($mapper))?
152 )?
153 }156 }
154 };157 };
modifiedprimitives/rpc/src/lib.rsdiffbeforeafterboth
4use sp_std::vec::Vec;4use sp_std::vec::Vec;
5use sp_core::H160;5use sp_core::H160;
6use codec::Decode;6use codec::Decode;
7use sp_runtime::DispatchError;
8
9type Result<T> = core::result::Result<T, DispatchError>;
710
8sp_api::decl_runtime_apis! {11sp_api::decl_runtime_apis! {
9 pub trait UniqueApi<CrossAccountId, AccountId> where12 pub trait UniqueApi<CrossAccountId, AccountId> where
10 AccountId: Decode,13 AccountId: Decode,
11 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,14 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,
12 {15 {
13 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>;16 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>>;
14 fn token_exists(collection: CollectionId, token: TokenId) -> bool;17 fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool>;
1518
16 fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId;19 fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId>;
17 fn const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;20 fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
18 fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;21 fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
1922
20 fn collection_tokens(collection: CollectionId) -> u32;23 fn collection_tokens(collection: CollectionId) -> Result<u32>;
21 fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32;24 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;
22 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128;25 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128>;
23 fn allowance(26 fn allowance(
24 collection: CollectionId,27 collection: CollectionId,
25 sender: CrossAccountId,28 sender: CrossAccountId,
26 spender: CrossAccountId,29 spender: CrossAccountId,
27 token: TokenId,30 token: TokenId,
28 ) -> u128;31 ) -> Result<u128>;
2932
30 /// Used for ethereum integration33 /// Used for ethereum integration
31 fn eth_contract_code(account: H160) -> Option<Vec<u8>>;34 fn eth_contract_code(account: H160) -> Option<Vec<u8>>;
3235
33 fn adminlist(collection: CollectionId) -> Vec<CrossAccountId>;36 fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;
34 fn allowlist(collection: CollectionId) -> Vec<CrossAccountId>;37 fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;
35 fn allowed(collection: CollectionId, user: CrossAccountId) -> bool;38 fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool>;
36 fn last_token_id(collection: CollectionId) -> TokenId;39 fn last_token_id(collection: CollectionId) -> Result<TokenId>;
37 fn collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>>;40 fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>>;
38 fn collection_stats() -> CollectionStats;41 fn collection_stats() -> Result<CollectionStats>;
39 }42 }
40}43}
4144
modifiedruntime/src/lib.rsdiffbeforeafterboth
1616
17use sp_api::impl_runtime_apis;17use sp_api::impl_runtime_apis;
18use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H256, U256, H160};18use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H256, U256, H160};
19use sp_runtime::DispatchError;
19// #[cfg(any(feature = "std", test))]20// #[cfg(any(feature = "std", test))]
20// pub use sp_runtime::BuildStorage;21// pub use sp_runtime::BuildStorage;
2122
1019 ($collection:ident.$method:ident($($name:ident),*)) => {{1020 ($collection:ident.$method:ident($($name:ident),*)) => {{
1020 use pallet_unique::dispatch::Dispatched;1021 use pallet_unique::dispatch::Dispatched;
10211022
1022 let collection = Dispatched::dispatch(<pallet_common::CollectionHandle<Runtime>>::new($collection).unwrap());1023 let collection = Dispatched::dispatch(<pallet_common::CollectionHandle<Runtime>>::try_get($collection)?);
1023 let dispatch = collection.as_dyn();1024 let dispatch = collection.as_dyn();
10241025
1025 dispatch.$method($($name),*)1026 Ok(dispatch.$method($($name),*))
1026 }};1027 }};
1027}1028}
1028impl_runtime_apis! {1029impl_runtime_apis! {
1029 impl up_rpc::UniqueApi<Block, CrossAccountId, AccountId>1030 impl up_rpc::UniqueApi<Block, CrossAccountId, AccountId>
1030 for Runtime1031 for Runtime
1031 {1032 {
1032 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId> {1033 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>, DispatchError> {
1033 dispatch_unique_runtime!(collection.account_tokens(account))1034 dispatch_unique_runtime!(collection.account_tokens(account))
1034 }1035 }
1035 fn token_exists(collection: CollectionId, token: TokenId) -> bool {1036 fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool, DispatchError> {
1036 dispatch_unique_runtime!(collection.token_exists(token))1037 dispatch_unique_runtime!(collection.token_exists(token))
1037 }1038 }
10381039
1039 fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId {1040 fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId, DispatchError> {
1040 dispatch_unique_runtime!(collection.token_owner(token))1041 dispatch_unique_runtime!(collection.token_owner(token))
1041 }1042 }
1042 fn const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8> {1043 fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {
1043 dispatch_unique_runtime!(collection.const_metadata(token))1044 dispatch_unique_runtime!(collection.const_metadata(token))
1044 }1045 }
1045 fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8> {1046 fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {
1046 dispatch_unique_runtime!(collection.variable_metadata(token))1047 dispatch_unique_runtime!(collection.variable_metadata(token))
1047 }1048 }
10481049
1049 fn collection_tokens(collection: CollectionId) -> u32 {1050 fn collection_tokens(collection: CollectionId) -> Result<u32, DispatchError> {
1050 dispatch_unique_runtime!(collection.collection_tokens())1051 dispatch_unique_runtime!(collection.collection_tokens())
1051 }1052 }
1052 fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32 {1053 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32, DispatchError> {
1053 dispatch_unique_runtime!(collection.account_balance(account))1054 dispatch_unique_runtime!(collection.account_balance(account))
1054 }1055 }
1055 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128 {1056 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128, DispatchError> {
1056 dispatch_unique_runtime!(collection.balance(account, token))1057 dispatch_unique_runtime!(collection.balance(account, token))
1057 }1058 }
1058 fn allowance(1059 fn allowance(
1059 collection: CollectionId,1060 collection: CollectionId,
1060 sender: CrossAccountId,1061 sender: CrossAccountId,
1061 spender: CrossAccountId,1062 spender: CrossAccountId,
1062 token: TokenId,1063 token: TokenId,
1063 ) -> u128 {1064 ) -> Result<u128, DispatchError> {
1064 dispatch_unique_runtime!(collection.allowance(sender, spender, token))1065 dispatch_unique_runtime!(collection.allowance(sender, spender, token))
1065 }1066 }
10661067
1069 .or_else(|| <pallet_evm_migration::OnMethodCall<Runtime>>::get_code(&account))1070 .or_else(|| <pallet_evm_migration::OnMethodCall<Runtime>>::get_code(&account))
1070 .or_else(|| <pallet_evm_contract_helpers::HelpersOnMethodCall<Self>>::get_code(&account))1071 .or_else(|| <pallet_evm_contract_helpers::HelpersOnMethodCall<Self>>::get_code(&account))
1071 }1072 }
1072 fn adminlist(collection: CollectionId) -> Vec<CrossAccountId> {1073 fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {
1073 <pallet_common::Pallet<Runtime>>::adminlist(collection)1074 Ok(<pallet_common::Pallet<Runtime>>::adminlist(collection))
1074 }1075 }
1075 fn allowlist(collection: CollectionId) -> Vec<CrossAccountId> {1076 fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {
1076 <pallet_common::Pallet<Runtime>>::allowlist(collection)1077 Ok(<pallet_common::Pallet<Runtime>>::allowlist(collection))
1077 }1078 }
1078 fn allowed(collection: CollectionId, user: CrossAccountId) -> bool {1079 fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool, DispatchError> {
1079 <pallet_common::Pallet<Runtime>>::allowed(collection, user)1080 Ok(<pallet_common::Pallet<Runtime>>::allowed(collection, user))
1080 }1081 }
1081 fn last_token_id(collection: CollectionId) -> TokenId {1082 fn last_token_id(collection: CollectionId) -> Result<TokenId, DispatchError> {
1082 dispatch_unique_runtime!(collection.last_token_id())1083 dispatch_unique_runtime!(collection.last_token_id())
1083 }1084 }
1084 fn collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>> {1085 fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>, DispatchError> {
1085 <pallet_common::CollectionById<Runtime>>::get(collection)1086 Ok(<pallet_common::CollectionById<Runtime>>::get(collection))
1086 }1087 }
1087 fn collection_stats() -> CollectionStats {1088 fn collection_stats() -> Result<CollectionStats, DispatchError> {
1088 <pallet_common::Pallet<Runtime>>::collection_stats()1089 Ok(<pallet_common::Pallet<Runtime>>::collection_stats())
1089 }1090 }
1090 }1091 }
10911092