Skip to content

Commit 16876a8

Browse files
committed
remove callback
1 parent ee08def commit 16876a8

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

README.md

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
## About
1010

11-
Event Scanner is a Rust library for monitoring EVM-based smart contract events. It is built on top of the [`alloy`](https://github.com/alloy-rs/alloy) ecosystem and focuses on in-memory scanning without a backing database. Applications provide event filters and callback implementations; the scanner takes care of subscribing to historical ranges, bridging into live mode, and delivering events with retry-aware execution strategies.
11+
Event Scanner is a Rust library for monitoring EVM-based smart contract events. It is built on top of the [`alloy`](https://github.com/alloy-rs/alloy) ecosystem and focuses on in-memory scanning without a backing database. Applications provide event filters; the scanner takes care of subscribing to historical ranges, bridging into live mode, all whilst delivering the events as streams.
1212

1313
---
1414

@@ -31,7 +31,7 @@ Event Scanner is a Rust library for monitoring EVM-based smart contract events.
3131
- **Historical replay** – scan block ranges.
3232
- **Live subscriptions** – stay up to date with latest blocks via WebSocket or IPC transports.
3333
- **Hybrid flow** – automatically transition from historical catch-up into streaming mode.
34-
- **Composable filters** – register one or many contract + event signature pairs with their own callbacks.
34+
- **Composable filters** – register one or many contract + event signature pairs with their own event streams.
3535
- **No database** – processing happens in-memory; persistence is left to the host application.
3636

3737
---
@@ -55,37 +55,43 @@ Add `event-scanner` to your `Cargo.toml`:
5555
event-scanner = "0.1.0-alpha.1"
5656
```
5757

58-
```rust
59-
use std::{sync::{Arc, atomic::{AtomicUsize, Ordering}}};
60-
use alloy::{eips::BlockNumberOrTag, network::Ethereum, rpc::types::Log, sol_types::SolEvent};
61-
use async_trait::async_trait;
62-
use event_scanner::{event_scanner::EventScannerBuilder, EventCallback, EventFilter};
63-
64-
struct CounterCallback { processed: Arc<AtomicUsize> }
65-
66-
#[async_trait]
67-
impl EventCallback for CounterCallback {
68-
async fn on_event(&self, _log: &Log) -> anyhow::Result<()> {
69-
self.processed.fetch_add(1, Ordering::SeqCst);
70-
Ok(())
71-
}
72-
}
73-
74-
async fn run_scanner(ws_url: alloy::transports::http::reqwest::Url, contract: alloy::primitives::Address) -> anyhow::Result<()> {
75-
let filter = EventFilter::new()
76-
.with_contract_address(contract)
77-
.with_event(MyContract::SomeEvent::SIGNATURE)
78-
.with_callback(Arc::new(CounterCallback { processed: Arc::new(AtomicUsize::new(0)) }));
79-
80-
let mut client = EventScannerBuilder::new()
81-
.with_event_filter(filter)
82-
.connect_ws::<Ethereum>(ws_url)
83-
.await?;
84-
85-
client.start_scanner(BlockNumberOrTag::Latest, None).await?;
86-
Ok(())
87-
}
88-
```
58+
```rust
59+
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
60+
use alloy::{eips::BlockNumberOrTag, network::Ethereum, rpc::types::Log, sol_types::SolEvent};
61+
use event_scanner::{event_filter::EventFilter, event_scanner::EventScanner};
62+
use tokio_stream::StreamExt;
63+
64+
struct CounterHandler { };
65+
66+
impl CounterHandler {
67+
async fn handle_event(&self, log: &Log) -> anyhow::Result<()> {
68+
println!("Processed event: {:?}", log);
69+
Ok(())
70+
}
71+
}
72+
73+
async fn run_scanner(ws_url: alloy::transports::http::reqwest::Url, contract: alloy::primitives::Address) -> anyhow::Result<()> {
74+
let filter = EventFilter::new()
75+
.with_contract_address(contract)
76+
.with_event(MyContract::SomeEvent::SIGNATURE);
77+
78+
let mut client = EventScanner::new().connect_ws::<Ethereum>(ws_url).await?;
79+
let mut stream = client.create_event_stream(filter);
80+
81+
tokio::spawn(async move {
82+
client.start_scanner(BlockNumberOrTag::Latest, None).await
83+
});
84+
85+
let handler = CounterHandler { };
86+
while let Some(Ok(logs)) = stream.next().await {
87+
for log in logs {
88+
handler.handle_event(&log).await?;
89+
}
90+
}
91+
92+
Ok(())
93+
}
94+
```
8995

9096
---
9197

@@ -100,12 +106,12 @@ async fn run_scanner(ws_url: alloy::transports::http::reqwest::Url, contract: al
100106
- `with_reorg_rewind_depth` - how many blocks to rewind when a reorg is detected
101107
- `with_block_confirmations` - how many confirmations to wait for before considering a block final
102108

103-
Once configured, connect using either `connect_ws::<Ethereum>(ws_url)` or `connect_ipc::<Ethereum>(path)`. This will `build` the `EventScanner` and allow you to call run to start in various [modes](#scanning-Modes).
109+
Once configured, connect using either `connect_ws::<Ethereum>(ws_url)` or `connect_ipc::<Ethereum>(path)`. This will build the `EventScanner` and allow you to create event streams and start scanning in various [modes](#scanning-modes).
104110

105111

106112
### Defining Event Filters
107113

108-
Create an `EventFilter` for each contract/event pair you want to track. The filter bundles the contract address, the event signature (from `SolEvent::SIGNATURE`).
114+
Create an `EventFilter` for each contract/event pair you want to track. The filter specifies the contract address and the event signature (from `SolEvent::SIGNATURE`).
109115

110116
Both `contract_address` and `event` fields are optional, allowing for flexible event tracking.
111117

@@ -169,7 +175,7 @@ This flexibility allows you to build sophisticated event monitoring systems that
169175
- **Historical mode**`start(BlockNumberOrTag::Number(start, Some(BlockNumberOrTag::Number(end)))`, scanner fetches events from a historical block range.
170176
- **Historical → Live**`start(BlockNumberOrTag::Number(start, None)` replays from `start` to current head, then streams future blocks.
171177

172-
For now modes are deduced from the `start` and `end` parameters. In the future, we might add explicit commands to select the mode.
178+
For now modes are deduced from the `start` and `end` parameters. In the future, we might add explicit commands to select the mode. Use `create_event_stream` to obtain a stream of events for processing.
173179

174180
See the integration tests under `tests/live_mode`, `tests/historic_mode`, and `tests/historic_to_live` for concrete examples.
175181

@@ -188,7 +194,7 @@ RUST_LOG=info cargo run -p simple_counter
188194
RUST_LOG=info cargo run -p historical_scanning
189195
```
190196

191-
Both examples spin up a local `anvil` instance and deploy a demo counter contract before starting the scanner.
197+
Both examples spin up a local `anvil` instance, deploy a demo counter contract, and demonstrate using event streams to process events.
192198

193199
---
194200

0 commit comments

Comments
 (0)