diff --git a/async_substrate_interface/async_substrate.py b/async_substrate_interface/async_substrate.py index 2cda046..3ccf8af 100644 --- a/async_substrate_interface/async_substrate.py +++ b/async_substrate_interface/async_substrate.py @@ -1407,7 +1407,8 @@ async def retrieve_pending_extrinsics(self) -> list: runtime = await self.init_runtime() result_data = await self.rpc_request("author_pendingExtrinsics", []) - + if "error" in result_data: + raise SubstrateRequestException(result_data["error"]["message"]) extrinsics = [] for extrinsic_data in result_data["result"]: @@ -2141,6 +2142,8 @@ async def get_parent_block_hash(self, block_hash) -> str: async def _get_parent_block_hash(self, block_hash) -> str: block_header = await self.rpc_request("chain_getHeader", [block_hash]) + if "error" in block_header: + raise SubstrateRequestException(block_header["error"]["message"]) if block_header["result"] is None: raise SubstrateRequestException(f'Block not found for "{block_hash}"') @@ -2172,15 +2175,7 @@ async def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any: response = await self.rpc_request( "state_getStorage", [storage_key, block_hash] ) - - if "result" in response: - return response.get("result") - elif "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - else: - raise SubstrateRequestException( - "Unknown error occurred during retrieval of events" - ) + return response.get("result") @cached_fetcher(max_size=SUBSTRATE_RUNTIME_CACHE_SIZE) async def get_block_runtime_info(self, block_hash: str) -> dict: @@ -2236,9 +2231,6 @@ async def get_block_metadata( params = [block_hash] response = await self.rpc_request("state_getMetadata", params) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - if (result := response.get("result")) and decode: metadata_decoder = runtime_config.create_scale_object( "MetadataVersioned", data=ScaleBytes(result) @@ -2553,7 +2545,7 @@ async def _get_block_hash(self, block_id: int) -> str: return (await self.rpc_request("chain_getBlockHash", [block_id]))["result"] async def get_chain_head(self) -> str: - result = await self._make_rpc_request( + response = await self._make_rpc_request( [ self.make_payload( "rpc_request", @@ -2562,8 +2554,11 @@ async def get_chain_head(self) -> str: ) ] ) - self.last_block_hash = result["rpc_request"][0]["result"] - return result["rpc_request"][0]["result"] + result = response["rpc_request"][0] + if "error" in result: + raise SubstrateRequestException(result["error"]["message"]) + self.last_block_hash = result["result"] + return result["result"] async def compose_call( self, @@ -2690,9 +2685,6 @@ async def query_multi( runtime=runtime, ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - result = [] storage_key_map = {s.to_hex(): s for s in storage_keys} @@ -3044,12 +3036,7 @@ async def get_chain_finalised_head(self): """ response = await self.rpc_request("chain_getFinalizedHead", []) - - if response is not None: - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - - return response.get("result") + return response["result"] async def _do_runtime_call_old( self, @@ -3092,6 +3079,8 @@ async def _do_runtime_call_old( [f"{api}_{method}", param_data.hex(), block_hash], runtime=runtime, ) + if "error" in result_data: + raise SubstrateRequestException(result_data["error"]["message"]) result_vec_u8_bytes = hex_to_bytes(result_data["result"]) result_bytes = await self.decode_scale( "Vec", result_vec_u8_bytes, runtime=runtime @@ -3185,6 +3174,8 @@ async def runtime_call( [f"{api}_{method}", param_data.hex(), block_hash], runtime=runtime, ) + if "error" in result_data: + raise SubstrateRequestException(result_data["error"]["message"]) output_type_string = f"scale_info::{runtime_call_def['output']}" # Decode result @@ -3237,6 +3228,8 @@ async def get_account_next_index(self, account_address: str) -> int: nonce_obj = await self.rpc_request( "account_nextIndex", [account_address] ) + if "error" in nonce_obj: + raise SubstrateRequestException(nonce_obj["error"]["message"]) self._nonces[account_address] = nonce_obj["result"] else: self._nonces[account_address] += 1 @@ -3622,9 +3615,6 @@ async def query_map( method="state_getKeys", params=[prefix, block_hash], runtime=runtime ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - result_keys = response.get("result") result = [] @@ -3640,8 +3630,6 @@ async def query_map( params=[result_keys, block_hash], runtime=runtime, ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) for result_group in response["result"]: result = decode_query_map( result_group["changes"], @@ -3680,8 +3668,6 @@ async def query_map( ) ) for response in all_responses: - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) for result_group in response["result"]: changes.extend(result_group["changes"]) @@ -3905,9 +3891,6 @@ async def result_handler(message: dict, subscription_id) -> tuple[dict, bool]: "author_submitExtrinsic", [str(extrinsic.data)] ) - if "result" not in response: - raise SubstrateRequestException(response.get("error")) - result = AsyncExtrinsicReceipt( substrate=self, extrinsic_hash=response["result"] ) @@ -3994,12 +3977,8 @@ async def get_block_number(self, block_hash: Optional[str] = None) -> int: """Async version of `substrateinterface.base.get_block_number` method.""" response = await self.rpc_request("chain_getHeader", [block_hash]) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - - elif "result" in response: - if response["result"]: - return int(response["result"]["number"], 16) + if response["result"]: + return int(response["result"]["number"], 16) raise SubstrateRequestException( f"Unable to retrieve block number for {block_hash}" ) diff --git a/async_substrate_interface/sync_substrate.py b/async_substrate_interface/sync_substrate.py index 61efc54..1622d78 100644 --- a/async_substrate_interface/sync_substrate.py +++ b/async_substrate_interface/sync_substrate.py @@ -1709,8 +1709,6 @@ def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any: if "result" in response: return response.get("result") - elif "error" in response: - raise SubstrateRequestException(response["error"]["message"]) else: raise SubstrateRequestException( "Unknown error occurred during retrieval of events" @@ -1761,9 +1759,6 @@ def get_block_metadata( params = [block_hash] response = self.rpc_request("state_getMetadata", params) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - if (result := response.get("result")) and decode: metadata_decoder = self.runtime_config.create_scale_object( "MetadataVersioned", data=ScaleBytes(result) @@ -2073,7 +2068,7 @@ def get_block_hash(self, block_id: int) -> str: return self.rpc_request("chain_getBlockHash", [block_id])["result"] def get_chain_head(self) -> str: - result = self._make_rpc_request( + response = self._make_rpc_request( [ self.make_payload( "rpc_request", @@ -2082,8 +2077,11 @@ def get_chain_head(self) -> str: ) ] ) - self.last_block_hash = result["rpc_request"][0]["result"] - return result["rpc_request"][0]["result"] + result = response["rpc_request"][0] + if "error" in result: + raise SubstrateRequestException(result["error"]["message"]) + self.last_block_hash = result["result"] + return result["result"] def compose_call( self, @@ -2191,9 +2189,6 @@ def query_multi( "state_queryStorageAt", [[s.to_hex() for s in storage_keys], block_hash] ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - result = [] storage_key_map = {s.to_hex(): s for s in storage_keys} @@ -2528,12 +2523,7 @@ def get_chain_finalised_head(self): """ response = self.rpc_request("chain_getFinalizedHead", []) - - if response is not None: - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - - return response.get("result") + return response["result"] def _do_runtime_call_old( self, @@ -3051,9 +3041,6 @@ def query_map( params=[prefix, page_size, start_key, block_hash], ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - result_keys = response.get("result") result = [] @@ -3067,9 +3054,6 @@ def query_map( method="state_queryStorageAt", params=[result_keys, block_hash] ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - for result_group in response["result"]: result = decode_query_map( result_group["changes"], @@ -3376,15 +3360,12 @@ def get_block_number(self, block_hash: Optional[str] = None) -> int: """Async version of `substrateinterface.base.get_block_number` method.""" response = self.rpc_request("chain_getHeader", [block_hash]) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - - elif "result" in response: - if response["result"]: - return int(response["result"]["number"], 16) - raise SubstrateRequestException( - f"Unable to determine block number for {block_hash}" - ) + if response["result"]: + return int(response["result"]["number"], 16) + else: + raise SubstrateRequestException( + f"Unable to determine block number for {block_hash}" + ) def close(self): """