-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.rs
More file actions
80 lines (62 loc) · 3.12 KB
/
main.rs
File metadata and controls
80 lines (62 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::{sync::Arc, time::Duration};
use alloy::{
eips::BlockNumberOrTag, network::Ethereum, providers::ProviderBuilder, rpc::types::Log, sol,
sol_types::SolEvent,
};
use alloy_node_bindings::Anvil;
use async_trait::async_trait;
use event_scanner::{EventCallback, EventFilter, event_scanner::EventScannerBuilder};
use tokio::time::sleep;
use tracing::info;
use tracing_subscriber::EnvFilter;
struct CounterCallback;
#[async_trait]
impl EventCallback for CounterCallback {
async fn on_event(&self, log: &Log) -> anyhow::Result<()> {
info!("Callback successfully executed with event {:?}", log.inner.data);
Ok(())
}
}
sol! {
#[allow(missing_docs)]
#[sol(rpc, bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220b846b706f79f5ae1fc4a4238319e723a092f47ce4051404186424739164ab02264736f6c634300081e0033")]
contract Counter {
uint256 public count;
event CountIncreased(uint256 newCount);
event CountDecreased(uint256 newCount);
function increase() public {
count += 1;
emit CountIncreased(count);
}
function decrease() public {
require(count > 0, "Count cannot be negative");
count -= 1;
emit CountDecreased(count);
}
function getCount() public view returns (uint256) {
return count;
}
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let _ = tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).try_init();
let anvil = Anvil::new().block_time(1).try_spawn()?;
let wallet = anvil.wallet();
let provider =
ProviderBuilder::new().wallet(wallet.unwrap()).connect(anvil.endpoint().as_str()).await?;
let counter_contract = Counter::deploy(provider).await?;
let contract_address = counter_contract.address();
let increase_filter = EventFilter {
contract_address: *contract_address,
event: Counter::CountIncreased::SIGNATURE.to_owned(),
callback: Arc::new(CounterCallback),
};
let _ = counter_contract.increase().send().await?.get_receipt().await?;
let mut builder = EventScannerBuilder::new();
builder.with_event_filter(increase_filter);
let mut scanner = builder.connect_ws::<Ethereum>(anvil.ws_endpoint_url()).await?;
sleep(Duration::from_secs(10)).await;
scanner.start(BlockNumberOrTag::Number(0), None).await.expect("failed to start scanner");
Ok(())
}