From f3caa8e366367a0846b1c300485a4c66dd68b17c Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Wed, 27 Mar 2024 14:44:41 -0400 Subject: [PATCH 1/4] feat(providers): add connect_builtin transport example --- examples/providers/Cargo.toml | 8 ++- .../providers/examples/connect_builtin.rs | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 examples/providers/examples/connect_builtin.rs diff --git a/examples/providers/Cargo.toml b/examples/providers/Cargo.toml index 6e2768c3..9c2ab9bc 100644 --- a/examples/providers/Cargo.toml +++ b/examples/providers/Cargo.toml @@ -13,14 +13,18 @@ repository.workspace = true [dev-dependencies] alloy.workspace = true # Temp dependency fix to enable relevant features - Ref: https://github.com/alloy-rs/examples/pull/3#discussion_r1537842062 -alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "f7333c4", features = [ +alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0", features = [ "pubsub", ] } -alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "f7333c4", features = [ +alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0", features = [ "pubsub", "ws", + "ipc", ] } +alloy-network = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0" } + +alloy-transport = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0" } eyre.workspace = true futures-util = "0.3" diff --git a/examples/providers/examples/connect_builtin.rs b/examples/providers/examples/connect_builtin.rs new file mode 100644 index 00000000..d99e85c4 --- /dev/null +++ b/examples/providers/examples/connect_builtin.rs @@ -0,0 +1,52 @@ +//! Example of using the `RootProvider::connect_builtin` to create a provider +//! from a connection string. The connection string can be a HTTP, WS or IPC endpoint. + +use alloy::node_bindings::Anvil; +use alloy_network::Ethereum; +use alloy_provider::{Provider, RootProvider}; +use alloy_transport::BoxTransport; +use eyre::Result; +use futures_util::StreamExt; + +#[tokio::main] +async fn main() -> Result<()> { + let anvil = Anvil::new().block_time(1).try_spawn()?; + let http = anvil.endpoint(); + let ws = anvil.ws_endpoint(); + + // Instantiate a HTTP transport provider by passing the http endpoint url + let http_provider = + RootProvider::::connect_builtin(http.as_str()).await?; + + // Get latest block number + let block_number = http_provider.get_block_number().await?; + + println!("Latest block number: {:?}", block_number); + + // This requires the `pubsub` and `ws` features to be enabled on alloy-provider + let ws_provider = RootProvider::::connect_builtin(ws.as_str()).await?; + + let sub = ws_provider.subscribe_blocks().await?; + + let mut stream = sub.into_stream().take(2); + + println!("Awaiting blocks..."); + + let handle = tokio::spawn(async move { + while let Some(block) = stream.next().await { + println!("{:?}", block.header.number); + } + }); + + handle.await?; + + let ipc_path = "/tmp/reth.ipc"; + + // This requires the `pubsub` and `ipc` features to be enabled on alloy-provider + // This would throw a runtime error if the ipc does not exist + let ipc_provider = RootProvider::::connect_builtin(ipc_path).await?; + + let _block_number = ipc_provider.get_block_number().await?; + + Ok(()) +} From 1ab217c2c47e2488203967573fb6971c7300a275 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:02:32 -0400 Subject: [PATCH 2/4] apply temp fix for #26 --- examples/providers/Cargo.toml | 4 +++- examples/providers/examples/ipc.rs | 8 +++----- examples/providers/examples/ws.rs | 2 +- examples/providers/examples/ws_with_auth.rs | 3 ++- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/examples/providers/Cargo.toml b/examples/providers/Cargo.toml index 9c2ab9bc..33f5ce26 100644 --- a/examples/providers/Cargo.toml +++ b/examples/providers/Cargo.toml @@ -15,6 +15,8 @@ alloy.workspace = true # Temp dependency fix to enable relevant features - Ref: https://github.com/alloy-rs/examples/pull/3#discussion_r1537842062 alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0", features = [ "pubsub", + "ipc", + "ws", ] } alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0", features = [ "pubsub", @@ -25,7 +27,7 @@ alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0", f alloy-network = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0" } alloy-transport = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0" } - +alloy-transport-ipc = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0" } eyre.workspace = true futures-util = "0.3" tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } diff --git a/examples/providers/examples/ipc.rs b/examples/providers/examples/ipc.rs index 078a6b86..a42e4ba2 100644 --- a/examples/providers/examples/ipc.rs +++ b/examples/providers/examples/ipc.rs @@ -1,11 +1,9 @@ //! Example of using the IPC provider to get the latest block number. -use alloy::{ - network::Ethereum, - providers::{Provider, RootProvider}, - transports::ipc::IpcConnect, -}; +use alloy_network::Ethereum; +use alloy_provider::{Provider, RootProvider}; use alloy_rpc_client::RpcClient; +use alloy_transport_ipc::IpcConnect; use eyre::Result; #[tokio::main] diff --git a/examples/providers/examples/ws.rs b/examples/providers/examples/ws.rs index 65991af3..85a262bb 100644 --- a/examples/providers/examples/ws.rs +++ b/examples/providers/examples/ws.rs @@ -1,7 +1,7 @@ //! Example of using the WS provider to subscribe to new blocks. -use alloy::network::Ethereum; // Temp Fix +use alloy_network::Ethereum; use alloy_provider::{Provider, RootProvider}; use alloy_rpc_client::{RpcClient, WsConnect}; // diff --git a/examples/providers/examples/ws_with_auth.rs b/examples/providers/examples/ws_with_auth.rs index b9456cfa..53774958 100644 --- a/examples/providers/examples/ws_with_auth.rs +++ b/examples/providers/examples/ws_with_auth.rs @@ -1,9 +1,10 @@ //! Example of using the WS provider with auth to subscribe to new blocks. -use alloy::{network::Ethereum, transports::Authorization}; // Temp Fix +use alloy_network::Ethereum; use alloy_provider::{Provider, RootProvider}; use alloy_rpc_client::{RpcClient, WsConnect}; +use alloy_transport::Authorization; // use eyre::Result; use futures_util::StreamExt; From 61252e01597dd7bef22613792083e8b37a66c588 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Thu, 28 Mar 2024 12:26:35 -0400 Subject: [PATCH 3/4] ignore connect_builtin in ci --- .github/workflows/integration.yml | 4 ++-- .github/workflows/test.yml | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index c7e02224..962256ff 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -33,6 +33,7 @@ jobs: -e 'ipc' \ -e 'ws' \ -e 'ws_auth' \ + -e 'connect_builtin' \ | xargs -n1 echo )" @@ -52,7 +53,7 @@ jobs: export latest_alloy_commit=$(git ls-remote https://github.com/alloy-rs/alloy.git \ | grep refs/heads/main \ | cut -f 1) - + # Fetch the latest commit hash of the `main` branch from the Alloy Core repository export latest_alloy_core_commit=$(git ls-remote https://github.com/alloy-rs/core.git \ | grep refs/heads/main \ @@ -102,4 +103,3 @@ jobs: echo "Successfully ran: $example" fi done - \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 85b47f8f..8142a994 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -51,6 +51,7 @@ jobs: -e 'ipc' \ -e 'ws' \ -e 'ws_auth' \ + -e 'connect_builtin' \ | xargs -n1 echo )" From e78a249eca2d11eb977fa87150958bd5251249c7 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Thu, 28 Mar 2024 12:40:11 -0400 Subject: [PATCH 4/4] bump deps and nits --- examples/providers/Cargo.toml | 6 +++--- examples/providers/examples/connect_builtin.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/providers/Cargo.toml b/examples/providers/Cargo.toml index 0d5ff1ab..afdd066b 100644 --- a/examples/providers/Cargo.toml +++ b/examples/providers/Cargo.toml @@ -26,10 +26,10 @@ alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "66fa192", f "ipc", ] } -alloy-network = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0" } +alloy-network = { git = "https://github.com/alloy-rs/alloy", rev = "66fa192" } -alloy-transport = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0" } -alloy-transport-ipc = { git = "https://github.com/alloy-rs/alloy", rev = "68952c0" } +alloy-transport = { git = "https://github.com/alloy-rs/alloy", rev = "66fa192" } +alloy-transport-ipc = { git = "https://github.com/alloy-rs/alloy", rev = "66fa192" } eyre.workspace = true futures-util = "0.3" tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } diff --git a/examples/providers/examples/connect_builtin.rs b/examples/providers/examples/connect_builtin.rs index d99e85c4..f3748b8e 100644 --- a/examples/providers/examples/connect_builtin.rs +++ b/examples/providers/examples/connect_builtin.rs @@ -21,7 +21,7 @@ async fn main() -> Result<()> { // Get latest block number let block_number = http_provider.get_block_number().await?; - println!("Latest block number: {:?}", block_number); + println!("Latest block number: {block_number:?}"); // This requires the `pubsub` and `ws` features to be enabled on alloy-provider let ws_provider = RootProvider::::connect_builtin(ws.as_str()).await?;