Skip to content

Commit 7a3ae85

Browse files
authored
feat: Run Scanner in a Separate Tokio Task (#138)
1 parent 78649d0 commit 7a3ae85

File tree

21 files changed

+362
-665
lines changed

21 files changed

+362
-665
lines changed

examples/historical_scanning/main.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
1-
use std::time::Duration;
2-
31
use alloy::{network::Ethereum, providers::ProviderBuilder, sol, sol_types::SolEvent};
42
use alloy_node_bindings::Anvil;
53

64
use event_scanner::{EventFilter, EventScanner, Message};
7-
use tokio::time::sleep;
85
use tokio_stream::StreamExt;
96
use tracing::{error, info};
107
use tracing_subscriber::EnvFilter;
118

129
sol! {
1310
// Built directly with solc 0.8.30+commit.73712a01.Darwin.appleclang
14-
#[sol(rpc,
15-
bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220471585b420a1ad0093820ff10129ec863f6df4bec186546249391fbc3cdbaa7c64736f6c634300081e0033"
16-
)] contract Counter {
17-
uint256 public count;
11+
#[sol(rpc, bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220471585b420a1ad0093820ff10129ec863f6df4bec186546249391fbc3cdbaa7c64736f6c634300081e0033")]
12+
contract Counter {
13+
uint256 public count;
1814

19-
event CountIncreased(uint256 newCount);
20-
event CountDecreased(uint256 newCount);
15+
event CountIncreased(uint256 newCount);
16+
event CountDecreased(uint256 newCount);
2117

22-
function increase() public {
23-
count += 1;
24-
emit CountIncreased(count);
25-
}
18+
function increase() public {
19+
count += 1;
20+
emit CountIncreased(count);
21+
}
2622

27-
function decrease() public {
28-
require(count > 0, "Count cannot be negative");
29-
count -= 1;
30-
emit CountDecreased(count);
31-
}
23+
function decrease() public {
24+
require(count > 0, "Count cannot be negative");
25+
count -= 1;
26+
emit CountDecreased(count);
27+
}
3228

33-
function getCount() public view returns (uint256) {
34-
return count;
29+
function getCount() public view returns (uint256) {
30+
return count;
31+
}
3532
}
3633
}
37-
}
3834

3935
#[tokio::main]
4036
async fn main() -> anyhow::Result<()> {
4137
let _ = tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).try_init();
4238

43-
let anvil = Anvil::new().block_time(1).try_spawn()?;
39+
let anvil = Anvil::new().block_time_f64(0.1).try_spawn()?;
4440
let wallet = anvil.wallet();
4541
let provider =
4642
ProviderBuilder::new().wallet(wallet.unwrap()).connect(anvil.endpoint().as_str()).await?;
@@ -59,7 +55,6 @@ async fn main() -> anyhow::Result<()> {
5955

6056
let mut stream = scanner.subscribe(increase_filter);
6157

62-
sleep(Duration::from_secs(10)).await;
6358
scanner.start().await.expect("failed to start scanner");
6459

6560
while let Some(message) = stream.next().await {

examples/latest_events_scanning/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use tracing_subscriber::EnvFilter;
77

88
sol! {
99
// Built directly with solc 0.8.30+commit.73712a01.Darwin.appleclang
10-
#[sol(rpc,
11-
bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220471585b420a1ad0093820ff10129ec863f6df4bec186546249391fbc3cdbaa7c64736f6c634300081e0033"
12-
)] contract Counter {
10+
#[sol(rpc, bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220471585b420a1ad0093820ff10129ec863f6df4bec186546249391fbc3cdbaa7c64736f6c634300081e0033")]
11+
contract Counter {
1312
uint256 public count;
1413

1514
event CountIncreased(uint256 newCount);

examples/live_scanning/main.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
use std::time::Duration;
2-
31
use alloy::{network::Ethereum, providers::ProviderBuilder, sol, sol_types::SolEvent};
42
use alloy_node_bindings::Anvil;
53
use event_scanner::{EventFilter, EventScanner, Message};
64

7-
use tokio::time::sleep;
85
use tokio_stream::StreamExt;
96
use tracing::{error, info};
107
use tracing_subscriber::EnvFilter;
118

129
sol! {
1310
// Built directly with solc 0.8.30+commit.73712a01.Darwin.appleclang
14-
#[sol(rpc,
15-
bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220471585b420a1ad0093820ff10129ec863f6df4bec186546249391fbc3cdbaa7c64736f6c634300081e0033"
16-
)] contract Counter {
11+
#[sol(rpc, bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220471585b420a1ad0093820ff10129ec863f6df4bec186546249391fbc3cdbaa7c64736f6c634300081e0033")]
12+
contract Counter {
1713
uint256 public count;
1814

1915
event CountIncreased(uint256 newCount);
@@ -56,19 +52,9 @@ async fn main() -> anyhow::Result<()> {
5652

5753
let mut stream = scanner.subscribe(increase_filter);
5854

59-
let task_1 = tokio::spawn(async move {
60-
scanner.start().await.expect("failed to start scanner");
61-
});
55+
scanner.start().await.expect("failed to start scanner");
6256

63-
let task_2 = tokio::spawn(async move {
64-
sleep(Duration::from_secs(5)).await;
65-
match counter_contract.increase().send().await {
66-
Ok(_) => info!("Tx sent succsefully"),
67-
Err(e) => {
68-
tracing::error!("Failed to send tx {:?}", e);
69-
}
70-
}
71-
});
57+
_ = counter_contract.increase().send().await?;
7258

7359
while let Some(message) = stream.next().await {
7460
match message {
@@ -86,8 +72,5 @@ async fn main() -> anyhow::Result<()> {
8672
}
8773
}
8874

89-
task_1.await.ok();
90-
task_2.await.ok();
91-
9275
Ok(())
9376
}

examples/sync_scanning/main.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use tracing_subscriber::EnvFilter;
1010

1111
sol! {
1212
// Built directly with solc 0.8.30+commit.73712a01.Darwin.appleclang
13-
#[sol(rpc,
14-
bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220471585b420a1ad0093820ff10129ec863f6df4bec186546249391fbc3cdbaa7c64736f6c634300081e0033"
15-
)] contract Counter {
13+
#[sol(rpc, bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220471585b420a1ad0093820ff10129ec863f6df4bec186546249391fbc3cdbaa7c64736f6c634300081e0033")]
14+
contract Counter {
1615
uint256 public count;
1716

1817
event CountIncreased(uint256 newCount);
@@ -62,9 +61,7 @@ async fn main() -> anyhow::Result<()> {
6261
let mut stream = scanner.subscribe(increase_filter);
6362

6463
info!("Starting sync scanner...");
65-
tokio::spawn(async move {
66-
scanner.start().await.expect("failed to start scanner");
67-
});
64+
scanner.start().await.expect("failed to start scanner");
6865

6966
info!("Creating live events...");
7067
for i in 0..2 {

0 commit comments

Comments
 (0)