From 9f4b9cdb92a8dbf0c9598d21856aa06b7994c532 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Thu, 12 May 2022 21:20:33 +0000 Subject: [PATCH] survive nonstandard eth_chainId --- web3-proxy/src/connection.rs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/web3-proxy/src/connection.rs b/web3-proxy/src/connection.rs index c4bbb07b..d3951a09 100644 --- a/web3-proxy/src/connection.rs +++ b/web3-proxy/src/connection.rs @@ -125,20 +125,31 @@ impl Web3Connection { // TODO: passing empty_params like this feels awkward. let empty_params: Option<()> = None; - let found_chain_id: String = active_request_handle + // TODO: some rpcs (on bsc and fantom) do not return an id + let found_chain_id: Result = active_request_handle .request("eth_chainId", empty_params) - .await - .unwrap(); + .await; - let found_chain_id = - usize::from_str_radix(found_chain_id.trim_start_matches("0x"), 16).unwrap(); + match found_chain_id { + Ok(found_chain_id) => { + let found_chain_id = + usize::from_str_radix(found_chain_id.trim_start_matches("0x"), 16).unwrap(); - if chain_id != found_chain_id { - return Err(anyhow::anyhow!( - "incorrect chain id! Expected {}. Found {}", - chain_id, - found_chain_id - )); + if chain_id != found_chain_id { + return Err(anyhow::anyhow!( + "incorrect chain id! Expected {}. Found {}", + chain_id, + found_chain_id + )); + } + } + Err(e) => { + // TODO: this is not the right way to use anyhow + let e_str = format!("{}", e); + let e = anyhow::Error::from(e).context(format!("{:?}: {}", connection, e_str)); + + return Err(e); + } } info!("Successful connection: {}", connection);