From 4bc0e480539d3636186a97daa51790bc7698b05a Mon Sep 17 00:00:00 2001 From: Antony1060 Date: Wed, 22 Nov 2023 13:56:17 +0100 Subject: [PATCH] Remove leftovers and fix wasm support --- Cargo.toml | 3 +++ src/middleware.rs | 15 ++++----------- src/utils.rs | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index edc7c2a..394fbee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,9 @@ ethers-core = "2.0.11" ethers-providers = "2.0.11" futures-util = "0.3.29" +[target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.2", features = ["js"] } + [dev-dependencies] tokio = { version = "1.34.0", features = ["macros", "rt-multi-thread"] } ethers = "2.0.11" diff --git a/src/middleware.rs b/src/middleware.rs index deee138..15bc10f 100644 --- a/src/middleware.rs +++ b/src/middleware.rs @@ -18,7 +18,7 @@ use reqwest::Url; use serde_json::Value; use crate::ccip::handle_ccip; -use crate::utils::{decode_bytes, dns_encode}; +use crate::utils::{build_reqwest, decode_bytes, dns_encode}; use crate::CCIPReadMiddlewareError; #[derive(Debug, Clone)] @@ -67,11 +67,7 @@ impl CCIPReadMiddlewareBuilder { Ok(CCIPReadMiddleware { provider: self.provider.ok_or("provider is required".to_string())?, ens: self.ens.unwrap_or(ens::ENS_ADDRESS), - reqwest_client: reqwest::Client::builder() - // TODO: discuss an appropriate default timeout, 4 seconds is a very random decision - .timeout(self.timeout.unwrap_or(Duration::from_secs(4))) - .build() - .expect("should be a valid reqwest client"), + reqwest_client: build_reqwest(self.timeout.unwrap_or(Duration::from_secs(10))), max_redirect_attempt: self.max_redirect_attempt.unwrap_or(10), }) } @@ -226,7 +222,8 @@ impl CCIPReadMiddleware { Ok(H160::zero()) } - #[async_recursion] + #[cfg_attr(target_arch = "wasm32", async_recursion(?Send))] + #[cfg_attr(not(target_arch = "wasm32"), async_recursion)] async fn _call( &self, transaction: &TypedTransaction, @@ -365,8 +362,6 @@ where /// Resolve a field of an ENS name async fn resolve_field(&self, ens_name: &str, field: &str) -> Result { - println!("calling resolve field"); - let field: String = self .query_resolver_parameters( ParamType::String, @@ -524,8 +519,6 @@ mod tests { assert_eq!(record, email); } - // todo: requires modification in ethers-rs, uncomment when merged - // #[tokio::test] async fn test_mismatched_sender() { let resolver_address = "0xC1735677a60884ABbCF72295E88d47764BeDa282"; diff --git a/src/utils.rs b/src/utils.rs index f9bf910..36f99fe 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,6 @@ use ethers_core::abi; use ethers_core::abi::{Detokenize, ParamType}; +use std::time::Duration; pub(crate) fn truncate_str(src: &str, side: usize) -> String { if src.len() < side * 2 + 3 { @@ -14,6 +15,20 @@ pub(crate) fn decode_bytes(param: ParamType, bytes: &[u8]) -> Res T::from_tokens(tokens).map_err(|err| abi::Error::Other(err.to_string().into())) } +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn build_reqwest(timeout: Duration) -> reqwest::Client { + reqwest::Client::builder() + .timeout(timeout) + .build() + .expect("should be a valid reqwest client") +} + +#[cfg(target_arch = "wasm32")] +pub(crate) fn build_reqwest(_timeout: Duration) -> reqwest::Client { + // reqwest doesn't support timeouts on wasm + reqwest::Client::new() +} + /// Encodes a domain name into its binary representation according to the DNS /// wire format. Each label (i.e., substring separated by dots) in the domain /// is prefixed with its length, and the encoded domain name is terminated