use alloy_chains::NamedChain;
use alloy_provider::ProviderBuilder;
use anyhow::{Context, Result};
use chrono::NaiveDate;
use semioscan::BlockWindowCalculator;
use std::env;
use tracing::{info, Level};
use tracing_subscriber::FmtSubscriber;
#[tokio::main]
async fn main() -> Result<()> {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber)
.context("Failed to set tracing subscriber")?;
dotenvy::dotenv().ok();
let rpc_url = env::var("RPC_URL").context("RPC_URL environment variable not set")?;
let api_key = env::var("API_KEY").context("API_KEY environment variable not set")?;
let day_str = env::var("DAY").unwrap_or_else(|_| "2025-10-16".to_string());
let cache_path = env::var("CACHE_PATH").unwrap_or_else(|_| "block_windows.json".to_string());
let full_rpc_url = format!("{rpc_url}{api_key}/");
info!(day_str, cache_path, "Starting daily block window example");
let date = NaiveDate::parse_from_str(&day_str, "%Y-%m-%d")
.context("Failed to parse DAY (expected format: YYYY-MM-DD)")?;
let provider = ProviderBuilder::new().connect_http(full_rpc_url.parse()?);
let calculator = BlockWindowCalculator::with_disk_cache(provider.clone(), cache_path)?;
info!(chain = ?NamedChain::Arbitrum, date = %date, "Calculating daily block window");
let window = calculator
.get_daily_window(NamedChain::Arbitrum, date)
.await?;
info!(
date = %date,
start_block = window.start_block,
end_block = window.end_block,
block_count = window.block_count().as_u64(),
start_ts = %window.start_ts,
end_ts_exclusive = %window.end_ts_exclusive,
"Daily block window calculated"
);
println!("\n=== Daily Block Window ===");
println!("Date: {date}");
println!(
"Block range: [{}, {}] (inclusive)",
window.start_block, window.end_block
);
println!("Block count: {}", window.block_count());
println!(
"UTC start: {} ({})",
window.start_ts,
chrono::DateTime::from_timestamp(window.start_ts.0, 0).unwrap()
);
println!(
"UTC end (exclusive): {} ({})",
window.end_ts_exclusive,
chrono::DateTime::from_timestamp(window.end_ts_exclusive.0, 0).unwrap()
);
Ok(())
}