Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rpc-client): add test for BuiltInConnString.connect_boxed #1331

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/provider/src/provider/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl<N: Network> RootProvider<BoxTransport, N> {
/// Creates a new root provider from the provided connection details.
pub async fn connect_builtin(s: &str) -> Result<Self, TransportError> {
let conn: BuiltInConnectionString = s.parse()?;

let client = ClientBuilder::default().connect_boxed(conn).await?;
Ok(Self::new(client))
}
Expand Down
18 changes: 18 additions & 0 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ mod tests {
use alloy_network::AnyNetwork;
use alloy_node_bindings::Anvil;
use alloy_primitives::{address, b256, bytes, keccak256};
use alloy_rpc_client::BuiltInConnectionString;
use alloy_rpc_types_eth::{request::TransactionRequest, Block};
// For layer transport tests
#[cfg(feature = "hyper")]
Expand Down Expand Up @@ -1672,6 +1673,23 @@ mod tests {
}
}

#[tokio::test]
async fn builtin_connect_boxed() {
init_tracing();
let anvil = Anvil::new().spawn();

let conn: BuiltInConnectionString = anvil.endpoint().parse().unwrap();

let transport = conn.connect_boxed().await.unwrap();

let client = alloy_rpc_client::RpcClient::new(transport, true);

let provider = RootProvider::<BoxTransport, Ethereum>::new(client);

let num = provider.get_block_number().await.unwrap();
assert_eq!(0, num);
}

#[tokio::test]
async fn test_uncle_count() {
init_tracing();
Expand Down
21 changes: 12 additions & 9 deletions crates/rpc-client/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,15 @@ impl BuiltInConnectionString {
// reqwest is enabled, hyper is not
#[cfg(all(not(feature = "hyper"), feature = "reqwest"))]
Self::Http(url) => {
Ok(
alloy_transport::Transport::boxed(
alloy_transport_http::Http::<reqwest::Client>::new(url.clone())
)
)
},
Ok(alloy_transport::Transport::boxed(
alloy_transport_http::Http::<reqwest::Client>::new(url.clone()),
))
}

// hyper is enabled, reqwest is not
#[cfg(feature = "hyper")]
Self::Http(_) => Err(TransportErrorKind::custom_str(
"hyper not supported by BuiltinConnectionString. Please instantiate a hyper client manually",
Self::Http(url) => Ok(alloy_transport::Transport::boxed(
alloy_transport_http::HyperTransport::new_hyper(url.clone()),
)),

#[cfg(all(not(target_arch = "wasm32"), feature = "ws"))]
Expand All @@ -95,7 +93,12 @@ impl BuiltInConnectionString {
.await
.map(alloy_transport::Transport::boxed),

#[cfg(not(any(feature = "reqwest", feature = "hyper", feature = "ws", feature = "ipc")))]
#[cfg(not(any(
feature = "reqwest",
feature = "hyper",
feature = "ws",
feature = "ipc"
)))]
_ => Err(TransportErrorKind::custom_str(
"No transports enabled. Enable one of: reqwest, hyper, ws, ipc",
)),
Expand Down