generated from recmo/rust-service-template
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
15,340 additions
and
11,784 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,60 @@ | ||
use ethers::providers::Middleware; | ||
use ethers::types::{Address, BlockNumber, Filter, FilterBlockOption, Log, Topic, ValueOrArray}; | ||
|
||
pub struct BlockScanner<T> { | ||
read_provider: T, | ||
current_block: u64, | ||
window_size: u64, | ||
} | ||
|
||
impl<T> BlockScanner<T> | ||
where | ||
T: Middleware, | ||
<T as Middleware>::Error: 'static, | ||
{ | ||
pub async fn new_latest(read_provider: T, window_size: u64) -> anyhow::Result<Self> { | ||
let latest_block = read_provider.get_block_number().await?; | ||
|
||
Ok(Self { | ||
read_provider, | ||
current_block: latest_block.as_u64(), | ||
window_size, | ||
}) | ||
} | ||
|
||
pub async fn next( | ||
&mut self, | ||
address: Option<ValueOrArray<Address>>, | ||
topics: [Option<Topic>; 4], | ||
) -> anyhow::Result<Vec<Log>> { | ||
let latest_block = self.read_provider.get_block_number().await?.as_u64(); | ||
|
||
if self.current_block >= latest_block { | ||
return Ok(Vec::new()); | ||
} | ||
|
||
let from_block = self.current_block; | ||
let to_block = latest_block.min(from_block + self.window_size); | ||
|
||
let next_current_block = to_block + 1; | ||
|
||
let from_block = Some(BlockNumber::Number(from_block.into())); | ||
let to_block = Some(BlockNumber::Number(to_block.into())); | ||
|
||
let logs = self | ||
.read_provider | ||
.get_logs(&Filter { | ||
block_option: FilterBlockOption::Range { | ||
from_block, | ||
to_block, | ||
}, | ||
address, | ||
topics, | ||
}) | ||
.await?; | ||
|
||
self.current_block = next_current_block; | ||
|
||
Ok(logs) | ||
} | ||
} |
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
Oops, something went wrong.