diff --git a/examples/providers/examples/ws.rs b/examples/providers/examples/ws.rs index 11184f7e..2fd9a0b1 100644 --- a/examples/providers/examples/ws.rs +++ b/examples/providers/examples/ws.rs @@ -11,9 +11,6 @@ async fn main() -> Result<()> { let rpc_url = "wss://eth-mainnet.g.alchemy.com/v2/your-api-key"; let ws_transport = WsConnect::new(rpc_url); - // To use auth - // let auth = alloy_transport::Authorization::bearer("token"); - // let ws_transport = WsConnect::with_auth(rpc_url, Some(auth)); let rpc_client = RpcClient::connect_pubsub(ws_transport).await?; diff --git a/examples/providers/examples/ws_with_auth.rs b/examples/providers/examples/ws_with_auth.rs new file mode 100644 index 00000000..624971a0 --- /dev/null +++ b/examples/providers/examples/ws_with_auth.rs @@ -0,0 +1,46 @@ +//! Example of using the WS provider with auth to subscribe to new blocks. + +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; + +#[tokio::main] +async fn main() -> Result<()> { + let rpc_url = "wss://your-ws-endpoint.com/"; + + let auth = Authorization::basic("username", "password"); + let auth_bearer = Authorization::bearer("bearer-token"); + + let ws_transport_basic = WsConnect::with_auth(rpc_url, Some(auth)); + + let ws_transport_bearer = WsConnect::with_auth(rpc_url, Some(auth_bearer)); + + let rpc_client_basic = RpcClient::connect_pubsub(ws_transport_basic).await?; + + let rpc_client_bearer = RpcClient::connect_pubsub(ws_transport_bearer).await?; + + let provider_basic = RootProvider::::new(rpc_client_basic); + + let provider_bearer = RootProvider::::new(rpc_client_bearer); + + let sub_basic = provider_basic.subscribe_blocks(); + let sub_bearer = provider_bearer.subscribe_blocks(); + + let mut stream_basic = sub_basic.await?.into_stream().take(4); + let mut stream_bearer = sub_bearer.await?.into_stream().take(4); + + println!("Awaiting blocks..."); + + while let Some(block) = stream_basic.next().await { + println!("From basic {:?}", block.header.number); + } + + while let Some(block) = stream_bearer.next().await { + println!("From bearer {:?}", block.header.number); + } + + Ok(()) +}