2022-12-08 05:37:11 +00:00
|
|
|
use ethers::{abi::AbiDecode, prelude::*};
|
2022-05-16 15:11:25 +00:00
|
|
|
use eyre::Result;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-11-07 20:21:24 +00:00
|
|
|
// In order to run this example you need to include Ws and TLS features
|
|
|
|
// Run this example with `cargo run -p ethers --example subscribe_logs --features="ws","rustls"`
|
2022-05-16 15:11:25 +00:00
|
|
|
#[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();
|
2022-11-07 23:43:11 +00:00
|
|
|
println!("last_block: {last_block}");
|
2022-05-16 15:11:25 +00:00
|
|
|
|
2022-11-15 21:00:40 +00:00
|
|
|
let erc20_transfer_filter =
|
|
|
|
Filter::new().from_block(last_block - 25).event("Transfer(address,address,uint256)");
|
2022-05-16 15:11:25 +00:00
|
|
|
|
2022-11-26 16:42:41 +00:00
|
|
|
let mut stream = client.subscribe_logs(&erc20_transfer_filter).await?.take(2);
|
2022-05-16 15:11:25 +00:00
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|