From c7d92598d2cd763549aa5b97b10c9e692ea90f00 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Thu, 14 Mar 2024 13:40:03 -0400 Subject: [PATCH 01/10] feat(providers): http, ws --- Cargo.toml | 1 + examples/providers/Cargo.toml | 33 +++++++++++++++++++++++++++++ examples/providers/examples/http.rs | 27 +++++++++++++++++++++++ examples/providers/examples/ws.rs | 27 +++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 examples/providers/Cargo.toml create mode 100644 examples/providers/examples/http.rs create mode 100644 examples/providers/examples/ws.rs diff --git a/Cargo.toml b/Cargo.toml index 72c79195..d027b9b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ license = "MIT OR Apache-2.0" homepage = "https://github.com/alloy-rs/examples" repository = "https://github.com/alloy-rs/examples" publish = false +exclude = ["examples/"] [workspace.dependencies] alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "d5967ab", default-features = false } diff --git a/examples/providers/Cargo.toml b/examples/providers/Cargo.toml new file mode 100644 index 00000000..2d8f5ebd --- /dev/null +++ b/examples/providers/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "examples-providers" +version = "0.0.0" +publish = false + +authors.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true +exclude.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dev-dependencies] +alloy-primitives.workspace = true +alloy-sol-types = { workspace = true, features = ["json"] } +alloy-provider = { workspace = true, features = ["pubsub", "ws"] } +alloy-contract.workspace = true +alloy-pubsub.workspace = true +alloy-rpc-types.workspace = true +alloy-rpc-client.workspace = true +alloy-rpc-trace-types.workspace = true +alloy-node-bindings.workspace = true +alloy-transport.workspace = true +alloy-transport-http.workspace = true +alloy-transport-ws.workspace = true +alloy-network.workspace = true +tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"] } +eyre = "0.6.12" +reqwest = "0.11.26" +futures-util = "0.3" diff --git a/examples/providers/examples/http.rs b/examples/providers/examples/http.rs new file mode 100644 index 00000000..e9dd6555 --- /dev/null +++ b/examples/providers/examples/http.rs @@ -0,0 +1,27 @@ +use alloy_network::Ethereum; +use alloy_provider::{HttpProvider, Provider}; +use alloy_rpc_client::RpcClient; +use alloy_transport_http::Http; +use eyre::Result; +use reqwest::Client; + +#[tokio::main] +async fn main() -> Result<()> { + // Setup the HTTP transport which is consumed by the RPC client + let rpc_url = "https://eth.llamarpc.com".parse().unwrap(); + let http = Http::::new(rpc_url); + + // Create the RPC client + let rpc_client = RpcClient::new(http, false); + + // Provider can then be instantiated using the RPC client, HttpProvider is an alias + // RootProvider. RootProvider requires two generics N: Network and T: Transport + let provider = HttpProvider::::new(rpc_client); + + // Get latest block number + let latest_block = provider.get_block_number().await?; + + println!("Latest block number: {}", latest_block); + + Ok(()) +} diff --git a/examples/providers/examples/ws.rs b/examples/providers/examples/ws.rs new file mode 100644 index 00000000..ad2dfa6e --- /dev/null +++ b/examples/providers/examples/ws.rs @@ -0,0 +1,27 @@ +use alloy_network::Ethereum; +use alloy_provider::{Provider, RootProvider}; +use alloy_rpc_client::{RpcClient, WsConnect}; +use eyre::Result; +use futures_util::StreamExt; + +#[tokio::main] +async fn main() -> Result<()> { + let rpc_url = "wss://eth-mainnet.g.alchemy.com/v2/your-api-key"; + + let ws_transport = WsConnect::new(rpc_url); + + let rpc_client = RpcClient::connect_pubsub(ws_transport).await?; + + let provider = RootProvider::::new(rpc_client); + + let sub = provider.subscribe_blocks().await?; + + let mut stream = sub.into_stream().take(4); + println!("Awaiting blocks..."); + + while let Some(block) = stream.next().await { + println!("{:?}", block.header.number); + } + + Ok(()) +} From 2c055ae93410d1eb878d02186d6ee8296f353316 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:32:39 -0400 Subject: [PATCH 02/10] feat(providers): ipc transport --- Cargo.toml | 4 +++- examples/providers/Cargo.toml | 1 + examples/providers/examples/ipc.rs | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 examples/providers/examples/ipc.rs diff --git a/Cargo.toml b/Cargo.toml index d027b9b1..2315b985 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,9 @@ alloy-signer-ledger = { git = "https://github.com/alloy-rs/alloy", rev = "d5967a alloy-signer-trezor = { git = "https://github.com/alloy-rs/alloy", rev = "d5967ab", default-features = false } alloy-transport = { git = "https://github.com/alloy-rs/alloy", rev = "d5967ab", default-features = false } alloy-transport-http = { git = "https://github.com/alloy-rs/alloy", rev = "d5967ab", default-features = false } -alloy-transport-ipc = { git = "https://github.com/alloy-rs/alloy", rev = "d5967ab", default-features = false } +alloy-transport-ipc = { git = "https://github.com/alloy-rs/alloy", rev = "d5967ab", features = [ + "mock", +] } alloy-transport-ws = { git = "https://github.com/alloy-rs/alloy", rev = "d5967ab", default-features = false } alloy-core = { version = "0.6.4", default-features = false, features = ["std"] } diff --git a/examples/providers/Cargo.toml b/examples/providers/Cargo.toml index 2d8f5ebd..33ac5a9d 100644 --- a/examples/providers/Cargo.toml +++ b/examples/providers/Cargo.toml @@ -26,6 +26,7 @@ alloy-node-bindings.workspace = true alloy-transport.workspace = true alloy-transport-http.workspace = true alloy-transport-ws.workspace = true +alloy-transport-ipc.workspace = true alloy-network.workspace = true tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"] } eyre = "0.6.12" diff --git a/examples/providers/examples/ipc.rs b/examples/providers/examples/ipc.rs new file mode 100644 index 00000000..3b8b51d5 --- /dev/null +++ b/examples/providers/examples/ipc.rs @@ -0,0 +1,25 @@ +use alloy_network::Ethereum; +use alloy_provider::{Provider, RootProvider}; +use alloy_rpc_client::RpcClient; +use alloy_transport_ipc::IpcConnect; +use eyre::Result; + +#[tokio::main] +async fn main() -> Result<()> { + // Setup the IPC transport which is consumed by the RPC client + let ipc_path = "/tmp/reth.ipc"; + + // IPC transport + let ipc = IpcConnect::from(ipc_path.to_string()); + + // RPC client using IPC transport + let ipc_client = RpcClient::connect_pubsub(ipc).await?; + + let provider = RootProvider::::new(ipc_client); + + let latest_block = provider.get_block_number().await?; + + println!("Latest block: {}", latest_block); + + Ok(()) +} From 5220c26e2fea36947c21af0959343006ce776bf6 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Thu, 14 Mar 2024 15:57:57 -0400 Subject: [PATCH 03/10] feat(providers): using ProviderBuilder --- .DS_Store | Bin 0 -> 6148 bytes examples/.DS_Store | Bin 0 -> 6148 bytes examples/providers/Cargo.toml | 1 + examples/providers/examples/builder.rs | 45 +++++++++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 .DS_Store create mode 100644 examples/.DS_Store create mode 100644 examples/providers/examples/builder.rs diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e6e6c96b77cf0f28e996b77b3bced5c24be6ca5b GIT binary patch literal 6148 zcmeHLJxc>Y5S=wagEmnVf(TwCSP6nyS)3s@wkbu>e3%G{7h(hpi^CtFrHGB4g|;dL z?6eSU^eb_Q z4w1z3!}#0fOg<}H6E980 zCo-3LpT5eoc`+@lzX4Wt+_R$nT{+&rpYDvCe8z5HM_T$AbC_Iu<(NE94kVAi+TqM* zOAIM=y;Hy`;1p;n!25%T#^`Gd70RsxoqPoV#^IKRYk7tPJ$3+njiEw#U_uQAs-Z%^ zVhA-HeoyE58bgI@I0;!9^XSS#zo7_O9ez)ylkgR~-YMV|uoZ}@k4ZlNmwLbd+k@OE zr+`!7UnwBsnQSJFCF!#@vN%3#J+w76HqHwb$|30VaV#(RDCYkaq#@?<1<=Oet$a^nAKNEKiCq(s$hXTmq6`tGQ5cgWNIinvxkMtNYo>uh zj!~sDRj5g=+HQnxE35<7f#2o;f4jrfz&GpDF&W=)u2Ayp4JCK-sW+?cdtR~T7txyD zzu3Bde0^DsW0nVGHulV!*I7V{4yi5Il`1mU7Lzf-vv)B}UFTCD3lBIxly*2zQ(A@PN%w?ktQ{&Cs z*@~XCYaCNqJS(J!0@m}_1A8_{dQ_p!)&c8)b>Q0pULPD3Mpt8?P%a%v3EXmfl92<~ ze3oEzG`boCg=j$t)fK3^3Vp;7sypf(&2u#d3RQO!a%PO9XBPT|BIN9-cO;#JtI%fa zfOR13z>vDl@czFRfBp}5vUk=2>%hNqKqS3_m&YUNy*2W1yw^G?+bA5&3lz#ONP0V# g7rYhEpvXX*!xunTW1tW<2>VAs+F+A);71+!0twL0wg3PC literal 0 HcmV?d00001 diff --git a/examples/providers/Cargo.toml b/examples/providers/Cargo.toml index 33ac5a9d..c71ba9b3 100644 --- a/examples/providers/Cargo.toml +++ b/examples/providers/Cargo.toml @@ -28,6 +28,7 @@ alloy-transport-http.workspace = true alloy-transport-ws.workspace = true alloy-transport-ipc.workspace = true alloy-network.workspace = true +alloy-signer.workspace = true tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"] } eyre = "0.6.12" reqwest = "0.11.26" diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs new file mode 100644 index 00000000..e381ccb9 --- /dev/null +++ b/examples/providers/examples/builder.rs @@ -0,0 +1,45 @@ +use alloy_network::{Ethereum, EthereumSigner}; +use alloy_node_bindings::Anvil; +use alloy_primitives::{U256, U64}; +use alloy_provider::{Provider, ProviderBuilder, RootProvider}; +use alloy_rpc_client::RpcClient; +use alloy_rpc_types::TransactionRequest; +use alloy_signer::Wallet; +use alloy_transport_http::Http; +use eyre::Result; +use reqwest::Client; + +#[tokio::main] +async fn main() -> Result<()> { + // Setup the HTTP transport which is consumed by the RPC client + let anvil = Anvil::new().spawn(); + + let pk = &anvil.keys()[0]; + let from = anvil.addresses()[0]; + let signer = Wallet::from(pk.to_owned()); + + let rpc_client = RpcClient::new(Http::::new(anvil.endpoint().parse().unwrap()), false); + let provider_with_signer = ProviderBuilder::<_, Ethereum>::new() + .signer(EthereumSigner::from(signer)) + .network::() + .provider(RootProvider::new(rpc_client)); + + let to = anvil.addresses()[1]; + let tx_req = TransactionRequest { + to: Some(to), + value: Some(U256::from(100)), + nonce: Some(U64::from(0)), + gas_price: Some(U256::from(20e9)), + gas: Some(U256::from(21000)), + ..Default::default() + }; + + let pending_tx = provider_with_signer.send_transaction(tx_req).await?; + + println!("Pending transaction...{:?}", pending_tx.tx_hash()); + let receipt = pending_tx.get_receipt().await?; + + assert_eq!(receipt.from, from); + + Ok(()) +} From f967cbdbc79702d355c09118741babedcf233ee9 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Thu, 14 Mar 2024 16:43:28 -0400 Subject: [PATCH 04/10] ws auth in comments --- examples/providers/examples/ws.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/providers/examples/ws.rs b/examples/providers/examples/ws.rs index ad2dfa6e..85dd0ea5 100644 --- a/examples/providers/examples/ws.rs +++ b/examples/providers/examples/ws.rs @@ -9,6 +9,9 @@ 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?; From 92fda5dc9096053974c617f74da5a9fe2898a743 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Fri, 15 Mar 2024 10:20:06 +0000 Subject: [PATCH 05/10] stylistic update, pull in latest upstream changes --- README.md | 13 +++++-------- examples/providers/Cargo.toml | 26 ++++++++++++-------------- examples/providers/examples/builder.rs | 5 ++++- examples/providers/examples/http.rs | 2 ++ examples/providers/examples/ipc.rs | 2 ++ examples/providers/examples/ws.rs | 2 ++ 6 files changed, 27 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 3d3de244..35957965 100644 --- a/README.md +++ b/README.md @@ -48,14 +48,11 @@ cargo run --example mnemonic_signer - [ ] Signer - [ ] Time lag - [ ] Transformer -- [ ] Providers - - [ ] Http - - [ ] IPC - - [ ] Mock - - [ ] Quorum - - [ ] Retry - - [ ] RW - - [ ] WS +- [x] Providers + - [x] [Builder](./examples/providers/examples/builder.rs) + - [x] [HTTP](./examples/providers/examples/http.rs) + - [x] [IPC](./examples/providers/examples/ipc.rs) + - [x] [WS](./examples/providers/examples/ws.rs) - [ ] Queries - [ ] Blocks - [ ] Contracts diff --git a/examples/providers/Cargo.toml b/examples/providers/Cargo.toml index c71ba9b3..800e62e0 100644 --- a/examples/providers/Cargo.toml +++ b/examples/providers/Cargo.toml @@ -1,35 +1,33 @@ [package] name = "examples-providers" -version = "0.0.0" -publish = false -authors.workspace = true +publish.workspace = true +version.workspace = true edition.workspace = true rust-version.workspace = true +authors.workspace = true license.workspace = true homepage.workspace = true repository.workspace = true -exclude.workspace = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dev-dependencies] +alloy-network.workspace = true +alloy-node-bindings.workspace = true +alloy-contract.workspace = true alloy-primitives.workspace = true -alloy-sol-types = { workspace = true, features = ["json"] } alloy-provider = { workspace = true, features = ["pubsub", "ws"] } -alloy-contract.workspace = true alloy-pubsub.workspace = true +alloy-sol-types = { workspace = true, features = ["json"] } alloy-rpc-types.workspace = true alloy-rpc-client.workspace = true alloy-rpc-trace-types.workspace = true -alloy-node-bindings.workspace = true +alloy-signer-wallet.workspace = true alloy-transport.workspace = true alloy-transport-http.workspace = true alloy-transport-ws.workspace = true alloy-transport-ipc.workspace = true -alloy-network.workspace = true -alloy-signer.workspace = true -tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"] } -eyre = "0.6.12" -reqwest = "0.11.26" + +eyre.workspace = true futures-util = "0.3" +reqwest.workspace = true +tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index e381ccb9..3c686449 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -1,10 +1,12 @@ +//! Example of using the `ProviderBuilder` to create a provider with a signer and network. + use alloy_network::{Ethereum, EthereumSigner}; use alloy_node_bindings::Anvil; use alloy_primitives::{U256, U64}; use alloy_provider::{Provider, ProviderBuilder, RootProvider}; use alloy_rpc_client::RpcClient; use alloy_rpc_types::TransactionRequest; -use alloy_signer::Wallet; +use alloy_signer_wallet::Wallet; use alloy_transport_http::Http; use eyre::Result; use reqwest::Client; @@ -37,6 +39,7 @@ async fn main() -> Result<()> { let pending_tx = provider_with_signer.send_transaction(tx_req).await?; println!("Pending transaction...{:?}", pending_tx.tx_hash()); + let receipt = pending_tx.get_receipt().await?; assert_eq!(receipt.from, from); diff --git a/examples/providers/examples/http.rs b/examples/providers/examples/http.rs index e9dd6555..aa5ae7d8 100644 --- a/examples/providers/examples/http.rs +++ b/examples/providers/examples/http.rs @@ -1,3 +1,5 @@ +//! Example of using the HTTP provider to get the latest block number. + use alloy_network::Ethereum; use alloy_provider::{HttpProvider, Provider}; use alloy_rpc_client::RpcClient; diff --git a/examples/providers/examples/ipc.rs b/examples/providers/examples/ipc.rs index 3b8b51d5..2d642748 100644 --- a/examples/providers/examples/ipc.rs +++ b/examples/providers/examples/ipc.rs @@ -1,3 +1,5 @@ +//! Example of using the IPC provider to get the latest block number. + use alloy_network::Ethereum; use alloy_provider::{Provider, RootProvider}; use alloy_rpc_client::RpcClient; diff --git a/examples/providers/examples/ws.rs b/examples/providers/examples/ws.rs index 85dd0ea5..11184f7e 100644 --- a/examples/providers/examples/ws.rs +++ b/examples/providers/examples/ws.rs @@ -1,3 +1,5 @@ +//! Example of using the WS provider to subscribe to new blocks. + use alloy_network::Ethereum; use alloy_provider::{Provider, RootProvider}; use alloy_rpc_client::{RpcClient, WsConnect}; From 6efda72b9e335d800dd4962052c435e35b62475a Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Sun, 17 Mar 2024 20:24:26 -0400 Subject: [PATCH 06/10] use Tx Builder --- examples/providers/examples/builder.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index 3c686449..fec96ede 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -1,6 +1,6 @@ //! Example of using the `ProviderBuilder` to create a provider with a signer and network. -use alloy_network::{Ethereum, EthereumSigner}; +use alloy_network::{Ethereum, EthereumSigner, TransactionBuilder}; use alloy_node_bindings::Anvil; use alloy_primitives::{U256, U64}; use alloy_provider::{Provider, ProviderBuilder, RootProvider}; @@ -27,14 +27,14 @@ async fn main() -> Result<()> { .provider(RootProvider::new(rpc_client)); let to = anvil.addresses()[1]; - let tx_req = TransactionRequest { - to: Some(to), - value: Some(U256::from(100)), - nonce: Some(U64::from(0)), - gas_price: Some(U256::from(20e9)), - gas: Some(U256::from(21000)), - ..Default::default() - }; + + let mut tx_req = TransactionRequest::default() + .to(Some(to)) + .value(U256::from(100)) + .nonce(U64::from(0)) + .gas_limit(U256::from(21000)); + + tx_req.set_gas_price(U256::from(20e9)); let pending_tx = provider_with_signer.send_transaction(tx_req).await?; From 8e0af98f514779361d297ac70eeda8335de6c518 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Sun, 17 Mar 2024 21:24:45 -0400 Subject: [PATCH 07/10] feat(providers): ws_with_auth --- examples/providers/examples/ws.rs | 3 -- examples/providers/examples/ws_with_auth.rs | 46 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 examples/providers/examples/ws_with_auth.rs 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(()) +} From 9ba010ddaf1342e6d9c73671c29b71bcb39fd94c Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Tue, 19 Mar 2024 11:19:10 -0400 Subject: [PATCH 08/10] feat(providers): spawn task for listening to stream in ws --- examples/providers/examples/ws.rs | 8 +++++++ examples/providers/examples/ws_with_auth.rs | 23 ++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/examples/providers/examples/ws.rs b/examples/providers/examples/ws.rs index 2fd9a0b1..70a71797 100644 --- a/examples/providers/examples/ws.rs +++ b/examples/providers/examples/ws.rs @@ -25,5 +25,13 @@ async fn main() -> Result<()> { println!("{:?}", block.header.number); } + let handle = tokio::spawn(async move { + while let Some(block) = stream.next().await { + println!("{:?}", block.header.number); + } + }); + + handle.await?; + Ok(()) } diff --git a/examples/providers/examples/ws_with_auth.rs b/examples/providers/examples/ws_with_auth.rs index 624971a0..39f13ec6 100644 --- a/examples/providers/examples/ws_with_auth.rs +++ b/examples/providers/examples/ws_with_auth.rs @@ -34,13 +34,22 @@ async fn main() -> Result<()> { 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); - } + // Spawning the block processing for basic auth as a new task + let basic_handle = tokio::spawn(async move { + while let Some(block) = stream_basic.next().await { + println!("From basic {:?}", block.header.number); + } + }); + + // Similarly for bearer auth + let bearer_handle = tokio::spawn(async move { + while let Some(block) = stream_bearer.next().await { + println!("From bearer {:?}", block.header.number); + } + }); + + // Wait for both tasks to complete + let _ = tokio::try_join!(basic_handle, bearer_handle)?; Ok(()) } From 8216a688c06ad64d37cf438e92b5a004489cc039 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 25 Mar 2024 10:42:00 -0400 Subject: [PATCH 09/10] fix: imports --- examples/providers/Cargo.toml | 17 ++------------- examples/providers/examples/builder.rs | 24 ++++++++++----------- examples/providers/examples/http.rs | 13 ++++++----- examples/providers/examples/ipc.rs | 12 ++++++----- examples/providers/examples/ws.rs | 9 +++++--- examples/providers/examples/ws_with_auth.rs | 10 +++++---- 6 files changed, 38 insertions(+), 47 deletions(-) diff --git a/examples/providers/Cargo.toml b/examples/providers/Cargo.toml index 800e62e0..411034c2 100644 --- a/examples/providers/Cargo.toml +++ b/examples/providers/Cargo.toml @@ -11,21 +11,8 @@ homepage.workspace = true repository.workspace = true [dev-dependencies] -alloy-network.workspace = true -alloy-node-bindings.workspace = true -alloy-contract.workspace = true -alloy-primitives.workspace = true -alloy-provider = { workspace = true, features = ["pubsub", "ws"] } -alloy-pubsub.workspace = true -alloy-sol-types = { workspace = true, features = ["json"] } -alloy-rpc-types.workspace = true -alloy-rpc-client.workspace = true -alloy-rpc-trace-types.workspace = true -alloy-signer-wallet.workspace = true -alloy-transport.workspace = true -alloy-transport-http.workspace = true -alloy-transport-ws.workspace = true -alloy-transport-ipc.workspace = true +alloy.workspace = true + eyre.workspace = true futures-util = "0.3" diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index fec96ede..958f9679 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -1,15 +1,14 @@ //! Example of using the `ProviderBuilder` to create a provider with a signer and network. -use alloy_network::{Ethereum, EthereumSigner, TransactionBuilder}; -use alloy_node_bindings::Anvil; -use alloy_primitives::{U256, U64}; -use alloy_provider::{Provider, ProviderBuilder, RootProvider}; -use alloy_rpc_client::RpcClient; -use alloy_rpc_types::TransactionRequest; -use alloy_signer_wallet::Wallet; -use alloy_transport_http::Http; +use alloy::{ + network::{EthereumSigner, TransactionBuilder}, + node_bindings::Anvil, + primitives::U256, + providers::{Provider, ProviderBuilder, RootProvider}, + rpc::{client::RpcClient, types::eth::TransactionRequest}, + signers::wallet::Wallet, +}; use eyre::Result; -use reqwest::Client; #[tokio::main] async fn main() -> Result<()> { @@ -20,10 +19,9 @@ async fn main() -> Result<()> { let from = anvil.addresses()[0]; let signer = Wallet::from(pk.to_owned()); - let rpc_client = RpcClient::new(Http::::new(anvil.endpoint().parse().unwrap()), false); - let provider_with_signer = ProviderBuilder::<_, Ethereum>::new() + let rpc_client = RpcClient::new_http(anvil.endpoint().parse().unwrap()); + let provider_with_signer = ProviderBuilder::new() .signer(EthereumSigner::from(signer)) - .network::() .provider(RootProvider::new(rpc_client)); let to = anvil.addresses()[1]; @@ -31,7 +29,7 @@ async fn main() -> Result<()> { let mut tx_req = TransactionRequest::default() .to(Some(to)) .value(U256::from(100)) - .nonce(U64::from(0)) + .nonce(0) .gas_limit(U256::from(21000)); tx_req.set_gas_price(U256::from(20e9)); diff --git a/examples/providers/examples/http.rs b/examples/providers/examples/http.rs index aa5ae7d8..fcd42b2f 100644 --- a/examples/providers/examples/http.rs +++ b/examples/providers/examples/http.rs @@ -1,20 +1,19 @@ //! Example of using the HTTP provider to get the latest block number. -use alloy_network::Ethereum; -use alloy_provider::{HttpProvider, Provider}; -use alloy_rpc_client::RpcClient; -use alloy_transport_http::Http; +use alloy::{ + network::Ethereum, + providers::{HttpProvider, Provider}, + rpc::client::RpcClient, +}; use eyre::Result; -use reqwest::Client; #[tokio::main] async fn main() -> Result<()> { // Setup the HTTP transport which is consumed by the RPC client let rpc_url = "https://eth.llamarpc.com".parse().unwrap(); - let http = Http::::new(rpc_url); // Create the RPC client - let rpc_client = RpcClient::new(http, false); + let rpc_client = RpcClient::new_http(rpc_url); // Provider can then be instantiated using the RPC client, HttpProvider is an alias // RootProvider. RootProvider requires two generics N: Network and T: Transport diff --git a/examples/providers/examples/ipc.rs b/examples/providers/examples/ipc.rs index 2d642748..f3558385 100644 --- a/examples/providers/examples/ipc.rs +++ b/examples/providers/examples/ipc.rs @@ -1,9 +1,11 @@ //! Example of using the IPC provider to get the latest block number. -use alloy_network::Ethereum; -use alloy_provider::{Provider, RootProvider}; -use alloy_rpc_client::RpcClient; -use alloy_transport_ipc::IpcConnect; +use alloy::{ + network::Ethereum, + providers::{Provider, RootProvider}, + rpc::client::RpcClient, + transports::ipc::IpcConnect, +}; use eyre::Result; #[tokio::main] @@ -12,7 +14,7 @@ async fn main() -> Result<()> { let ipc_path = "/tmp/reth.ipc"; // IPC transport - let ipc = IpcConnect::from(ipc_path.to_string()); + let ipc = IpcConnect::new(ipc_path.to_string()); // RPC client using IPC transport let ipc_client = RpcClient::connect_pubsub(ipc).await?; diff --git a/examples/providers/examples/ws.rs b/examples/providers/examples/ws.rs index 70a71797..660ac807 100644 --- a/examples/providers/examples/ws.rs +++ b/examples/providers/examples/ws.rs @@ -1,8 +1,11 @@ //! Example of using the WS provider to subscribe to new blocks. -use alloy_network::Ethereum; -use alloy_provider::{Provider, RootProvider}; -use alloy_rpc_client::{RpcClient, WsConnect}; +use alloy::{ + network::Ethereum, + providers::{Provider, RootProvider}, + rpc::client::RpcClient, + transports::ws::WsConnect, +}; use eyre::Result; use futures_util::StreamExt; diff --git a/examples/providers/examples/ws_with_auth.rs b/examples/providers/examples/ws_with_auth.rs index 39f13ec6..c55d0a9b 100644 --- a/examples/providers/examples/ws_with_auth.rs +++ b/examples/providers/examples/ws_with_auth.rs @@ -1,9 +1,11 @@ //! 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 alloy::{ + network::Ethereum, + providers::{Provider, RootProvider}, + rpc::client::RpcClient, + transports::Authorization, +}; use eyre::Result; use futures_util::StreamExt; From 01372cf97d8b4bd897123759840bba3d51d7f9d3 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 25 Mar 2024 12:08:10 -0400 Subject: [PATCH 10/10] temp dependency fix --- examples/providers/Cargo.toml | 8 ++++++++ examples/providers/examples/ipc.rs | 2 +- examples/providers/examples/ws.rs | 11 +++++------ examples/providers/examples/ws_with_auth.rs | 11 +++++------ 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/examples/providers/Cargo.toml b/examples/providers/Cargo.toml index 411034c2..77070624 100644 --- a/examples/providers/Cargo.toml +++ b/examples/providers/Cargo.toml @@ -12,6 +12,14 @@ 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 = "fd8f065", features = [ + "pubsub", +] } +alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "fd8f065", features = [ + "pubsub", + "ws", +] } eyre.workspace = true diff --git a/examples/providers/examples/ipc.rs b/examples/providers/examples/ipc.rs index f3558385..078a6b86 100644 --- a/examples/providers/examples/ipc.rs +++ b/examples/providers/examples/ipc.rs @@ -3,9 +3,9 @@ use alloy::{ network::Ethereum, providers::{Provider, RootProvider}, - rpc::client::RpcClient, transports::ipc::IpcConnect, }; +use alloy_rpc_client::RpcClient; use eyre::Result; #[tokio::main] diff --git a/examples/providers/examples/ws.rs b/examples/providers/examples/ws.rs index 660ac807..65991af3 100644 --- a/examples/providers/examples/ws.rs +++ b/examples/providers/examples/ws.rs @@ -1,11 +1,10 @@ //! Example of using the WS provider to subscribe to new blocks. -use alloy::{ - network::Ethereum, - providers::{Provider, RootProvider}, - rpc::client::RpcClient, - transports::ws::WsConnect, -}; +use alloy::network::Ethereum; +// Temp Fix +use alloy_provider::{Provider, RootProvider}; +use alloy_rpc_client::{RpcClient, WsConnect}; +// use eyre::Result; use futures_util::StreamExt; diff --git a/examples/providers/examples/ws_with_auth.rs b/examples/providers/examples/ws_with_auth.rs index c55d0a9b..b9456cfa 100644 --- a/examples/providers/examples/ws_with_auth.rs +++ b/examples/providers/examples/ws_with_auth.rs @@ -1,11 +1,10 @@ //! Example of using the WS provider with auth to subscribe to new blocks. -use alloy::{ - network::Ethereum, - providers::{Provider, RootProvider}, - rpc::client::RpcClient, - transports::Authorization, -}; +use alloy::{network::Ethereum, transports::Authorization}; +// Temp Fix +use alloy_provider::{Provider, RootProvider}; +use alloy_rpc_client::{RpcClient, WsConnect}; +// use eyre::Result; use futures_util::StreamExt;