ethers-rs/book/subscriptions/logs.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
958 B
Markdown
Raw Normal View History

2023-03-23 19:55:42 +00:00
# Subscribing to Logs
To subscribe to logs, create a Filter object that specifies the criteria for the logs you want to listen to. Then, pass the filter to the Provider's subscribe_logs method:
```rust
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let provider = Provider::<Http>::try_from("http://localhost:8545")?;
let filter = Filter::new().address("0xcontract_address_here".parse()?);
let mut stream = provider.subscribe_logs(filter).await?;
// Your code to handle logs goes here.
Ok(())
}
```
You can now listen to logs that match your filter criteria:
```rust
while let Some(log) = stream.next().await {
match log {
Ok(log) => {
println!("New log: {:?}", log);
}
Err(e) => {
eprintln!("Error: {:?}", e);
}
}
}
```
Here is another example of subscribing to logs:
2023-03-23 19:50:28 +00:00
```rust
{{#include ../../examples/subscriptions/examples/subscribe_logs.rs}}
```