From 0d866dc8636be609952ac0800600af02800c12b6 Mon Sep 17 00:00:00 2001 From: Waylon Jepsen Date: Thu, 23 Mar 2023 13:55:42 -0600 Subject: [PATCH] clean up sections --- book/subscriptions/logs.md | 36 ++++++++++++++- book/subscriptions/subscriptions.md | 68 +---------------------------- book/subscriptions/watch-blocks.md | 34 ++++++++++++++- 3 files changed, 69 insertions(+), 69 deletions(-) diff --git a/book/subscriptions/logs.md b/book/subscriptions/logs.md index 60878fc6..0d0c091f 100644 --- a/book/subscriptions/logs.md +++ b/book/subscriptions/logs.md @@ -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> { + let provider = Provider::::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 {{#include ../../examples/subscriptions/examples/subscribe_logs.rs}} diff --git a/book/subscriptions/subscriptions.md b/book/subscriptions/subscriptions.md index 92365e13..9b25f5eb 100644 --- a/book/subscriptions/subscriptions.md +++ b/book/subscriptions/subscriptions.md @@ -1,6 +1,6 @@ ## 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 @@ -24,72 +24,6 @@ Next, import the necessary components from the ethers-rs library: 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> { -let provider = Provider::::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> { - let provider = Provider::::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 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: diff --git a/book/subscriptions/watch-blocks.md b/book/subscriptions/watch-blocks.md index 60233b0d..8102f706 100644 --- a/book/subscriptions/watch-blocks.md +++ b/book/subscriptions/watch-blocks.md @@ -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> { +let provider = Provider::::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 {{#include ../../examples/subscriptions/examples/subscribe_blocks.rs}}