clean up sections

This commit is contained in:
Waylon Jepsen 2023-03-23 13:55:42 -06:00
parent 4dcb1a9441
commit 0d866dc863
3 changed files with 69 additions and 69 deletions

View File

@ -1,4 +1,38 @@
# Subscribe logs # 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:
```rust ```rust
{{#include ../../examples/subscriptions/examples/subscribe_logs.rs}} {{#include ../../examples/subscriptions/examples/subscribe_logs.rs}}

View File

@ -1,6 +1,6 @@
## Ethers-rs: Subscriptions ## Ethers-rs: Subscriptions
In this section of the mdbook, we will discuss how to use `ethers-rs` to subscribe and listen to blocks, events, and logs. Subscriptions provide a way to receive real-time updates on various activities on the Ethereum blockchain, allowing you to monitor the network and react to changes as they happen. Here we will discuss how to use `ethers-rs` to subscribe and listen to blocks, events, and logs. Subscriptions provide a way to receive real-time updates on various activities on the Ethereum blockchain, allowing you to monitor the network and react to changes as they happen.
## Overview ## Overview
@ -24,72 +24,6 @@ Next, import the necessary components from the ethers-rs library:
use ethers::{prelude::\*,types::H256,}; use ethers::{prelude::\*,types::H256,};
``` ```
## Subscribing to New Blocks
To subscribe to new blocks, create a Provider instance and call the subscribe_blocks method:
```rust
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let provider = Provider::<Http>::try_from("http://localhost:8545")?;
let mut stream = provider.subscribe_blocks().await?;
// Your code to handle new blocks goes here.
Ok(())
}
```
You can now listen to new blocks as they are mined:
```rust
while let Some(block) = stream.next().await {
match block {
Ok(block) => {
println!("New block: {:?}", block);
}
Err(e) => {
eprintln!("Error: {:?}", e);
}
}
}
```
### 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);
}
}
}
```
### Subscribing to Events ### Subscribing to Events
As we discussed in the previous section on events, you can subscribe to specific events emitted by smart contracts using the EventWatcher struct. To create a SubscriptionStream, call the subscribe method on your EventWatcher: As we discussed in the previous section on events, you can subscribe to specific events emitted by smart contracts using the EventWatcher struct. To create a SubscriptionStream, call the subscribe method on your EventWatcher:

View File

@ -1,4 +1,36 @@
# Watch blocks # Subscribing to New Blocks
To subscribe to new blocks, create a Provider instance and call the subscribe_blocks method:
```rust
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let provider = Provider::<Http>::try_from("http://localhost:8545")?;
let mut stream = provider.subscribe_blocks().await?;
// Your code to handle new blocks goes here.
Ok(())
}
```
You can now listen to new blocks as they are mined:
```rust
while let Some(block) = stream.next().await {
match block {
Ok(block) => {
println!("New block: {:?}", block);
}
Err(e) => {
eprintln!("Error: {:?}", e);
}
}
}
```
Here is another example of subscribing to new blocks:
```rust ```rust
{{#include ../../examples/subscriptions/examples/subscribe_blocks.rs}} {{#include ../../examples/subscriptions/examples/subscribe_blocks.rs}}