Skip to content

Commit

Permalink
feat: add example for trace_call_many and debug_trace_call_many (#163)
Browse files Browse the repository at this point in the history
* feat: add example for trace_call_many and debug_trace_call_many

* update examples to use Reth bindings, ignore from CI, add to examples list

---------

Co-authored-by: Adegbite Ademola Kelvin <adegbiteademolakelvin@Adegbites-MacBook-Pro.local>
Co-authored-by: zerosnacks <zerosnacks@protonmail.com>
  • Loading branch information
3 people authored Dec 4, 2024
1 parent 85f3dd1 commit e650725
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ This repository contains the following examples:
- [x] [Decode input](./examples/transactions/examples/decode_input.rs)
- [x] [Encode and decode EIP-1559 transaction](./examples/transactions/examples/encode_decode_eip1559.rs)
- [x] [Get gas price in USD](./examples/transactions/examples/gas_price_usd.rs)
- [x] [Simulate using `debug_traceCallMany`](./examples/transactions/examples/debug_trace_call_many.rs)
- [x] [Simulate using `trace_callMany`](./examples/transactions/examples/trace_call_many.rs)
- [x] [Trace call](./examples/transactions/examples/trace_call.rs)
- [x] [Trace transaction](./examples/transactions/examples/trace_transaction.rs)
- [x] [Transfer ERC20 token](./examples/transactions/examples/transfer_erc20.rs)
Expand Down
53 changes: 53 additions & 0 deletions examples/transactions/examples/debug_trace_call_many.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Example of how to trace a transaction using `debug_trace_call_many`.
use alloy::{
network::TransactionBuilder,
node_bindings::Reth,
primitives::{address, U256},
providers::{ext::DebugApi, ProviderBuilder},
rpc::types::{
trace::geth::GethDebugTracingCallOptions, Bundle, StateContext, TransactionRequest,
},
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Reth node.
// Ensure `reth` is available in $PATH.
let reth = Reth::new().dev().disable_discovery().instance(1).spawn();
let provider = ProviderBuilder::new().on_http(reth.endpoint().parse()?);

// Get users, these have allocated balances in the dev genesis block.
let alice = address!("70997970C51812dc3A010C7d01b50e0d17dc79C8");
let bob = address!("3C44CdDdB6a900fa2b585dd299e03d12FA4293BC");
let charlie = address!("90F79bf6EB2c4f870365E785982E1f101E93b906");
let dan = address!("15d34AAf54267DB7D7c367839AAf71A00a2C6A65");

// Define transactions
let tx1 =
TransactionRequest::default().with_from(alice).with_to(bob).with_value(U256::from(150));
let tx2 =
TransactionRequest::default().with_from(charlie).with_to(dan).with_value(U256::from(250));

// Create the bundle of transactions.
let bundles = vec![Bundle { transactions: vec![tx1, tx2], block_override: None }];

// Define the State context and trace option
let state_context = StateContext::default();
let trace_options = GethDebugTracingCallOptions::default();

//Call `debug_trace_call_many` on the provider.
let result = provider.debug_trace_call_many(bundles, state_context, trace_options).await;

match result {
Ok(traces) => {
println!("Traces:\n{:?}", traces);
}
Err(err) => {
println!("Error tracing transactions: {:?}", err);
}
}

Ok(())
}
45 changes: 45 additions & 0 deletions examples/transactions/examples/trace_call_many.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! Example of how to trace a transaction using `trace_call_many`.
use alloy::{
network::TransactionBuilder,
node_bindings::Reth,
primitives::{address, U256},
providers::{ext::TraceApi, ProviderBuilder},
rpc::types::{trace::parity::TraceType, TransactionRequest},
};

use eyre::{Ok, Result};

#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Reth node.
// Ensure `reth` is available in $PATH.
let reth = Reth::new().dev().disable_discovery().instance(1).spawn();
let provider = ProviderBuilder::new().on_http(reth.endpoint().parse()?);

// Get users, these have allocated balances in the dev genesis block.
let alice = address!("70997970C51812dc3A010C7d01b50e0d17dc79C8");
let bob = address!("3C44CdDdB6a900fa2b585dd299e03d12FA4293BC");
let charlie = address!("90F79bf6EB2c4f870365E785982E1f101E93b906");
let dan = address!("15d34AAf54267DB7D7c367839AAf71A00a2C6A65");

// Define transactions
let tx1 =
TransactionRequest::default().with_from(alice).with_to(bob).with_value(U256::from(150));
let tx2 =
TransactionRequest::default().with_from(charlie).with_to(dan).with_value(U256::from(250));

// Define the trace for the trace_list
let trace_type: &[TraceType] = &[TraceType::Trace];

// Trace the transaction on top of the latest block.
let trace_call_list = &[(tx1, trace_type), (tx2, trace_type)];

let result = provider.trace_call_many(trace_call_list).await?;

// Print the trace results.
for (index, trace_result) in result.iter().enumerate() {
println!("Trace result for transaction {}: {:?}", index, trace_result);
}
Ok(())
}
2 changes: 2 additions & 0 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function main () {
| grep -v \
-e 'any_network' \
-e 'builtin' \
-e 'debug_trace_call_many' \
-e 'geth_local_instance' \
-e 'ipc' \
-e 'ledger_signer' \
Expand All @@ -24,6 +25,7 @@ function main () {
-e 'subscribe_all_logs' \
-e 'subscribe_logs' \
-e 'subscribe_pending_transactions' \
-e 'trace_call_many' \
-e 'trace_call' \
-e 'trace_transaction' \
-e 'trezor_signer' \
Expand Down

0 comments on commit e650725

Please sign in to comment.