forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace_transaction.rs
53 lines (43 loc) · 1.92 KB
/
trace_transaction.rs
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
//! Example of how to trace a transaction using `trace_transaction`.
use alloy::{
primitives::b256,
providers::{ext::DebugApi, ProviderBuilder},
rpc::types::trace::geth::{
GethDebugBuiltInTracerType, GethDebugTracerType, GethDebugTracingOptions,
GethDefaultTracingOptions,
},
};
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
// Spin up a forked Anvil node.
// Ensure `anvil` is available in $PATH.
let rpc_url = "https://eth.merkle.io";
let provider = ProviderBuilder::new().on_anvil_with_config(|anvil| anvil.fork(rpc_url));
// Hash of the tx we want to trace.
let hash = b256!("97a02abf405d36939e5b232a5d4ef5206980c5a6661845436058f30600c52df7");
// Trace with the default tracer.
let default_options = GethDebugTracingOptions::default();
let result = provider.debug_trace_transaction(hash, default_options).await?;
println!("DEFAULT_TRACE: {result:?}");
// Trace with built-in call tracer.
let call_options = GethDebugTracingOptions {
config: GethDefaultTracingOptions {
disable_storage: Some(true),
enable_memory: Some(false),
..Default::default()
},
tracer: Some(GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer)),
..Default::default()
};
let result = provider.debug_trace_transaction(hash, call_options).await?;
println!("CALL_TRACE: {result:?}");
// Trace using a custom JavaScript tracer.
let js_options = GethDebugTracingOptions {
tracer: Some(GethDebugTracerType::JsTracer("{data: [], fault: function(log) {}, step: function(log) { if(log.op.toString() == \"DELEGATECALL\") this.data.push(log.stack.peek(0)); }, result: function() { return this.data; }}".into())),
..Default::default()
};
let result = provider.debug_trace_transaction(hash, js_options).await?;
println!("JS_TRACER: {result:?}");
Ok(())
}