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

feat(providers): add connect_builtin example #34

Merged
merged 5 commits into from
Mar 29, 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
4 changes: 2 additions & 2 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
-e 'ipc' \
-e 'ws' \
-e 'ws_auth' \
-e 'connect_builtin' \
| xargs -n1 echo
)"

Expand All @@ -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 \
Expand Down Expand Up @@ -102,4 +103,3 @@ jobs:
echo "Successfully ran: $example"
fi
done

1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
-e 'ipc' \
-e 'ws' \
-e 'ws_auth' \
-e 'connect_builtin' \
| xargs -n1 echo
)"

Expand Down
6 changes: 6 additions & 0 deletions examples/providers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ 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 = "66fa192", features = [
"pubsub",
"ipc",
"ws",
] }
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "66fa192", features = [
"pubsub",
"ws",
"ipc",
] }

alloy-network = { git = "https://github.com/alloy-rs/alloy", rev = "66fa192" }

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"] }
52 changes: 52 additions & 0 deletions examples/providers/examples/connect_builtin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! Example of using the `RootProvider<N, T: BoxTransport>::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::<Ethereum, BoxTransport>::connect_builtin(http.as_str()).await?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of a general comment and a personal opinion but I'm not a fan of the connect_builtin name. I don't think it is particularly clear to users and sounds like an internal method rather than the equivalent of a default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly beyond the scope of the intended PR but I would also prefer to see the parameter being strongly typed (Url-like) instead of a &str


// 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::<Ethereum, BoxTransport>::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::<Ethereum, BoxTransport>::connect_builtin(ipc_path).await?;

let _block_number = ipc_provider.get_block_number().await?;

Ok(())
}
8 changes: 3 additions & 5 deletions examples/providers/examples/ipc.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion examples/providers/examples/ws.rs
Original file line number Diff line number Diff line change
@@ -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};
//
Expand Down
3 changes: 2 additions & 1 deletion examples/providers/examples/ws_with_auth.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading