alloy-flashblocks 0.1.0

Stream Base L2 flashblocks and query preconfirmation state using Alloy.
Documentation
//! Example: Query flashblock state
//!
//! Demonstrates how to query balances, nonces, and other state against
//! the current flashblock (pending state with preconfirmed transactions).
//!
//! Run with: cargo run --example flashblock_state

use alloy::{primitives::address, providers::ProviderBuilder};
use alloy_flashblocks::FlashblocksProviderExt;
use eyre::Result;

const RPC_URL: &str = "https://mainnet-preconf.base.org";

#[tokio::main]
async fn main() -> Result<()> {
    let provider = ProviderBuilder::new().connect_http(RPC_URL.parse()?);

    // Get current flashblock
    println!("Fetching flashblock state...\n");

    if let Some(block) = provider.flashblock().await? {
        println!(
            "Flashblock {} | {} txs | {} gas used",
            block.header.number,
            block.transactions.len(),
            block.header.gas_used
        );
    }

    // Query balance in flashblock state (USDC contract as example address)
    let addr = address!("833589fCD6eDb6E08f4c7C32D4f71b54bdA02913");

    let balance = provider.flashblock_balance(addr).await?;
    println!("\nBalance of {}: {} wei", addr, balance);

    // Query nonce in flashblock state
    let nonce = provider.flashblock_nonce(addr).await?;
    println!("Nonce of {}: {}", addr, nonce);

    Ok(())
}