Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add trace_raw_transaction and trace_replay_block_transactions #925

Merged
merged 1 commit into from
Jun 17, 2024

Conversation

Ebolon
Copy link
Contributor

@Ebolon Ebolon commented Jun 17, 2024

Add methods for trace_raw_transaction and trace_replay_block_transactions to the trace api.

Example:

use std::str::FromStr;
use alloy_chains::{Chain};

use alloy_network::{EthereumWallet, TransactionBuilder};
use alloy_network::eip2718::Encodable2718;
use alloy_primitives::{Address, U256};
use alloy_provider::{ext::TraceApi, Provider, ProviderBuilder};
use alloy_rpc_types::{BlockNumberOrTag, TransactionRequest};
use alloy_rpc_types_trace::parity::TraceType;
use alloy_signer_local::PrivateKeySigner;

#[tokio::main]
async fn main() {
    let url = std::env::var("ETH_MAINNET_HTTP").expect("$ETH_MAINNET_HTTP must be set.");
    let node_url = url::Url::parse(url.as_str()).unwrap();
    let provider = ProviderBuilder::new().on_http(node_url);

    // trace_replay_block_transactions

    let result = provider
        .trace_replay_block_transactions(
            BlockNumberOrTag::Number(20111220),
            &[TraceType::Trace, TraceType::StateDiff],
        )
        .await
        .unwrap();
    println!("{:#?}", result);

    // trace_raw_transaction

    let pk = std::env::var("PK").expect("$PK must be set.");
    let signer = PrivateKeySigner::from_str(&pk).unwrap();
    let from_address = signer.address().clone();
    let wallet = EthereumWallet::from(signer);


    let tx = TransactionRequest::default()
        .with_to(Address::ZERO)
        .with_nonce(provider.get_transaction_count(from_address).await.unwrap())
        .with_chain_id(Chain::mainnet().id())
        .with_value(U256::from(100))
        .with_gas_limit(21_000)
        .with_max_priority_fee_per_gas(1_000_000_000)
        .with_max_fee_per_gas(100_000_000_000);

    let tx_envelope = tx.build(&wallet).await.unwrap();
    let tx_encoded = tx_envelope.encoded_2718();

    let result = provider
        .trace_raw_transaction(&tx_encoded,
            &[TraceType::Trace, TraceType::StateDiff],
        )
        .await
        .unwrap();

    println!("{:#?}", result);
}

@mattsse mattsse merged commit ad5c840 into alloy-rs:main Jun 17, 2024
22 checks passed
onbjerg added a commit that referenced this pull request Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants