From f9cbbc54c614dd0a5cc93a08f346e715ab64250c Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Mon, 22 Apr 2024 14:17:59 +0000 Subject: [PATCH 1/2] update comments, flag subscribe_logs as non-runnable example --- .../{watch_contract_event.rs => poll_logs.rs} | 4 ++-- ...external_provider.rs => subscribe_logs.rs} | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) rename examples/subscriptions/examples/{watch_contract_event.rs => poll_logs.rs} (96%) rename examples/subscriptions/examples/{subscribe_to_log_external_provider.rs => subscribe_logs.rs} (67%) diff --git a/examples/subscriptions/examples/watch_contract_event.rs b/examples/subscriptions/examples/poll_logs.rs similarity index 96% rename from examples/subscriptions/examples/watch_contract_event.rs rename to examples/subscriptions/examples/poll_logs.rs index 5ea913ae..0d0bda91 100644 --- a/examples/subscriptions/examples/watch_contract_event.rs +++ b/examples/subscriptions/examples/poll_logs.rs @@ -1,4 +1,4 @@ -//! Example of subscribing to blocks and watching contract events by WebSocket subscription. +//! Example of watching and polling for contract events by WebSocket subscription. use alloy::{node_bindings::Anvil, providers::ProviderBuilder, rpc::client::WsConnect, sol}; use eyre::Result; @@ -58,7 +58,7 @@ async fn main() -> Result<()> { let _ = decrement_call.send().await?; } - // Listen for the events. + // Poll for logs. increment_filter .into_stream() .take(2) diff --git a/examples/subscriptions/examples/subscribe_to_log_external_provider.rs b/examples/subscriptions/examples/subscribe_logs.rs similarity index 67% rename from examples/subscriptions/examples/subscribe_to_log_external_provider.rs rename to examples/subscriptions/examples/subscribe_logs.rs index 8dff5849..cf03a348 100644 --- a/examples/subscriptions/examples/subscribe_to_log_external_provider.rs +++ b/examples/subscriptions/examples/subscribe_logs.rs @@ -1,4 +1,4 @@ -//! Example of subscribing to logs from the Ethereum network using an external provider. +//! Example of subscribing and listening for contract events by WebSocket subscription. use alloy::{ primitives::{address, b256}, @@ -13,6 +13,14 @@ use futures_util::stream::StreamExt; #[tokio::main] async fn main() -> Result<()> { + // Set up the WS transport which is consumed by the RPC client. + let rpc_url = "wss://eth-mainnet.g.alchemy.com/v2/your-api-key"; + + // Create the provider. + let ws = WsConnect::new(rpc_url); + let provider = ProviderBuilder::new().on_ws(ws).await?; + + // Create a filter to watch for Uniswap token transfers. let uniswap_token_address = address!("1f9840a85d5aF5bf1D1762F925BDADdC4201F984"); let tranfer_event_signature = b256!("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"); @@ -21,13 +29,8 @@ async fn main() -> Result<()> { .event_signature(tranfer_event_signature) .from_block(BlockNumberOrTag::Latest); - let rpc_url = "wss://eth.merkle.io"; // DON'T use wss://eth.merkle.io _> this filters wrongly, tested alchemy.io's to be working fine - - // Create the provider. - let ws = WsConnect::new(rpc_url); - let provider = ProviderBuilder::new().on_ws(ws).await.unwrap(); - - let sub = provider.subscribe_logs(&filter).await.expect("Failed to subscribe to logs"); + // Subscribe to logs. + let sub = provider.subscribe_logs(&filter).await?; let mut stream = sub.into_stream(); while let Some(log) = stream.next().await { From e16ce61b4ab5f0f7c0365c341a67be01c992181d Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Mon, 22 Apr 2024 14:19:54 +0000 Subject: [PATCH 2/2] tag as non-runnable --- .github/workflows/integration.yml | 1 + .github/workflows/test.yml | 1 + CONTRIBUTING.md | 1 + README.md | 3 ++- 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index f53c3b7b..92a4d734 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -34,6 +34,7 @@ jobs: -e 'ws' \ -e 'ws_auth' \ -e 'connect_builtin' \ + -e 'subscribe_logs' \ | xargs -n1 echo )" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8142a994..5b25eb24 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -52,6 +52,7 @@ jobs: -e 'ws' \ -e 'ws_auth' \ -e 'connect_builtin' \ + -e 'subscribe_logs' \ | xargs -n1 echo )" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index db418c87..d7d8c4b3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -147,6 +147,7 @@ cargo run --example 2>&1 \ -e 'ws' \ -e 'ws_auth' \ -e 'connect_builtin' \ + -e 'subscribe_logs' \ | xargs -I {} sh -c 'if cargo run --example {} --quiet 1>/dev/null; then \ echo "Successfully ran: {}"; \ else \ diff --git a/README.md b/README.md index 9ad9d3f1..d65a960c 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,8 @@ This repository contains the following examples: - [x] [Signer management filler](./examples/fillers/examples/signer_filler.rs) - [x] Subscriptions - [x] [Subscribe and watch blocks](./examples/subscriptions/examples/subscribe_blocks.rs) - - [x] [Subscribe to contract events and watch logs](./examples/subscriptions/examples/watch_contract_event.rs) + - [x] [Watch and poll for contract event logs](./examples/subscriptions/examples/poll_logs.rs) + - [x] [Subscribe and listen for contract event logs](./examples/subscriptions/examples/subscribe_logs.rs) - [x] [Event multiplexer](./examples/subscriptions/examples/event_multiplexer.rs) - [x] Providers - [x] [Builder](./examples/providers/examples/builder.rs)