This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(providers): load previous logs before subscribing (#1264)
* feat(providers): load previous logs before subscribing Load previous logs and stream it back to the user before establishing a new stream for streaming future logs. Closes #988 * docs: add subscribe_logs example * fix clippy errors * refactor: use VecDeque and address review
- Loading branch information
1 parent
3df1527
commit b15d0f8
Showing
4 changed files
with
71 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use ethers::{abi::AbiDecode, prelude::*, utils::keccak256}; | ||
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) | ||
.topic0(ValueOrArray::Value(H256::from(keccak256("Transfer(address,address,uint256)")))); | ||
|
||
let mut stream = client.subscribe_logs(&erc20_transfer_filter).await?; | ||
|
||
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(()) | ||
} |