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:
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:
#![allow(unused)] fn main() { 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:
use ethers::{ core::{ abi::AbiDecode, types::{Address, BlockNumber, Filter, U256}, }, providers::{Middleware, Provider, StreamExt, Ws}, }; use eyre::Result; use std::sync::Arc; #[tokio::main] async fn main() -> Result<()> { let client = Provider::<Ws>::connect("wss://mainnet.infura.io/ws/v3/c60b0bb42f8a4c6481ecd229eddaca27") .await?; let client = Arc::new(client); let last_block = client.get_block(BlockNumber::Latest).await?.unwrap().number.unwrap(); println!("last_block: {last_block}"); let erc20_transfer_filter = Filter::new().from_block(last_block - 25).event("Transfer(address,address,uint256)"); let mut stream = client.subscribe_logs(&erc20_transfer_filter).await?.take(2); while let Some(log) = stream.next().await { println!( "block: {:?}, tx: {:?}, token: {:?}, from: {:?}, to: {:?}, amount: {:?}", log.block_number, log.transaction_hash, log.address, Address::from(log.topics[1]), Address::from(log.topics[2]), U256::decode(log.data) ); } Ok(()) }