#![allow(clippy::too_many_arguments)]
use std::sync::Arc;
use std::future::Future;
use std::sync::LazyLock;
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::{Semaphore, mpsc};
use tokio_util::sync::CancellationToken;
use tracing::{debug, info, warn};
static ENRICH_RPC_SEMAPHORE: LazyLock<Semaphore> = LazyLock::new(|| Semaphore::new(8));
fn spawn_cancellable<F>(cancel: CancellationToken, fut: F) -> tokio::task::JoinHandle<()>
where
F: Future<Output = ()> + Send + 'static,
{
tokio::spawn(async move {
tokio::select! {
_ = fut => {}
_ = cancel.cancelled() => {
debug!("Address sub-task cancelled on navigation");
}
}
})
}
use crate::app::actions::{Action, Source};
use crate::data::types::SnTransaction;
use crate::data::{DataSource, FilterKind};
use crate::decode::AbiRegistry;
use crate::decode::events::decode_event;
use crate::decode::functions::parse_multicall;
use crate::utils::felt_to_u128;
use super::block::spawn_voyager_prefetch;
use super::dune;
use super::dune_source_update;
use super::helpers;
use super::voyager;
async fn decode_events_dedup(
events: &[crate::data::types::SnEvent],
abi_reg: &Arc<AbiRegistry>,
) -> Vec<crate::decode::events::DecodedEvent> {
use futures::stream::StreamExt;
let unique: std::collections::HashSet<starknet::core::types::Felt> =
events.iter().map(|e| e.from_address).collect();
let abi_map: std::collections::HashMap<_, _> = futures::stream::iter(unique)
.map(|addr| {
let abi_reg = Arc::clone(abi_reg);
async move {
let abi = abi_reg.get_abi_for_address(&addr).await;
(addr, abi)
}
})
.buffer_unordered(8)
.collect()
.await;
events
.iter()
.map(|e| {
let abi = abi_map.get(&e.from_address).and_then(|o| o.as_deref());
decode_event(e, abi)
})
.collect()
}
pub(super) struct QueryGuard {
tx: mpsc::UnboundedSender<Action>,
key: String,
}
impl QueryGuard {
pub(super) fn new(
tx: &mpsc::UnboundedSender<Action>,
key: impl Into<String>,
label: String,
) -> Self {
let key = key.into();
let _ = tx.send(Action::SetActiveQuery {
key: key.clone(),
label: Some(label),
});
Self {
tx: tx.clone(),
key,
}
}
}
impl Drop for QueryGuard {
fn drop(&mut self) {
let _ = self.tx.send(Action::SetActiveQuery {
key: std::mem::take(&mut self.key),
label: None,
});
}
}
pub(super) fn query_addr_prefix(address: &starknet::core::types::Felt) -> String {
let hex = format!("{:064x}", address);
hex.trim_start_matches('0')
.chars()
.take(6)
.collect::<String>()
}
const UDC_CONTRACT_DEPLOYED: &str =
"0x26b160f10156dea0639bec90696772c640b9706a47f5b8c52ea1abe5858b34d";
#[derive(Debug, Clone, Copy)]
pub(crate) enum EventQueryKind {
Account,
Contract,
}
pub(super) async fn fetch_events_routed(
kind: EventQueryKind,
pf: Option<&Arc<crate::data::pathfinder::PathfinderClient>>,
ds: &Arc<dyn DataSource>,
address: starknet::core::types::Felt,
from_block: Option<u64>,
to_block: Option<u64>,
limit: usize,
) -> crate::error::Result<Vec<crate::data::types::SnEvent>> {
async fn from_pf(
kind: EventQueryKind,
pf: &Arc<crate::data::pathfinder::PathfinderClient>,
address: starknet::core::types::Felt,
from_block: Option<u64>,
to_block: Option<u64>,
limit: usize,
) -> crate::error::Result<Vec<crate::data::types::SnEvent>> {
let res = match kind {
EventQueryKind::Contract => {
pf.get_contract_events(
address,
from_block.unwrap_or(0),
to_block,
&[],
limit as u32,
None,
)
.await
}
EventQueryKind::Account => {
pf.get_events_for_address(
address,
from_block.unwrap_or(0),
to_block,
limit as u32,
None,
)
.await
}
};
res.map(|(events, _token)| events)
.map_err(|e| crate::error::StarktopError::Provider(e.to_string()))
}
async fn from_ds(
kind: EventQueryKind,
ds: &Arc<dyn DataSource>,
address: starknet::core::types::Felt,
from_block: Option<u64>,
to_block: Option<u64>,
limit: usize,
) -> crate::error::Result<Vec<crate::data::types::SnEvent>> {
match kind {
EventQueryKind::Contract => {
ds.get_contract_events(address, from_block, to_block, limit)
.await
}
EventQueryKind::Account => {
ds.get_events_for_address(address, from_block, to_block, limit)
.await
}
}
}
match kind {
EventQueryKind::Contract => {
if let Some(pf_client) = pf {
match from_pf(kind, pf_client, address, from_block, to_block, limit).await {
Ok(events) => return Ok(events),
Err(e) => {
warn!(error = %e, "PF contract events failed, falling back to RPC");
}
}
}
from_ds(kind, ds, address, from_block, to_block, limit).await
}
EventQueryKind::Account => {
match from_ds(kind, ds, address, from_block, to_block, limit).await {
Ok(events) => Ok(events),
Err(rpc_err) => {
if let Some(pf_client) = pf {
warn!(error = %rpc_err, "RPC account events failed, falling back to PF");
match from_pf(kind, pf_client, address, from_block, to_block, limit).await {
Ok(events) => Ok(events),
Err(pf_err) => {
warn!(error = %pf_err, "PF account events fallback also failed");
Err(rpc_err)
}
}
} else {
Err(rpc_err)
}
}
}
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct AddressActivityPage {
pub events: Vec<crate::data::types::SnEvent>,
pub tx_rows: Vec<crate::data::pathfinder::TxByHashData>,
pub unique_hashes: Vec<starknet::core::types::Felt>,
pub tx_block_map: std::collections::HashMap<starknet::core::types::Felt, u64>,
pub next_token: Option<u64>,
}
pub(crate) async fn fetch_address_activity(
address: starknet::core::types::Felt,
kind: EventQueryKind,
from_block: u64,
to_block: Option<u64>,
continuation_token: Option<u64>,
limit: u32,
pf: &Arc<crate::data::pathfinder::PathfinderClient>,
) -> crate::error::Result<AddressActivityPage> {
use starknet::core::types::Felt;
let (events, next_token) = match kind {
EventQueryKind::Account => pf
.get_events_for_address(address, from_block, to_block, limit, continuation_token)
.await
.map_err(|e| crate::error::StarktopError::Provider(e.to_string()))?,
EventQueryKind::Contract => pf
.get_contract_events(
address,
from_block,
to_block,
&[],
limit,
continuation_token,
)
.await
.map_err(|e| crate::error::StarktopError::Provider(e.to_string()))?,
};
let mut unique_hashes: Vec<Felt> = Vec::with_capacity(events.len());
let mut seen: std::collections::HashSet<Felt> = std::collections::HashSet::new();
let mut tx_block_map: std::collections::HashMap<Felt, u64> = std::collections::HashMap::new();
for e in &events {
if e.transaction_hash != Felt::ZERO && seen.insert(e.transaction_hash) {
unique_hashes.push(e.transaction_hash);
}
tx_block_map
.entry(e.transaction_hash)
.or_insert(e.block_number);
}
let tx_rows = if unique_hashes.is_empty() {
Vec::new()
} else {
pf.get_txs_by_hash(&unique_hashes)
.await
.map_err(|e| crate::error::StarktopError::Provider(e.to_string()))?
};
Ok(AddressActivityPage {
events,
tx_rows,
unique_hashes,
tx_block_map,
next_token,
})
}
const KNOWN_TOKENS: &[(&str, &str, u8)] = &[
(
"0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d",
"STRK",
18,
),
(
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"ETH",
18,
),
(
"0x03fe2b97c1fd336e750087d68b9b867997fd64a2661ff3ca5a7c771641e8e7ac",
"wBTC",
8,
),
(
"0x033068f6539f8e6e6b131e6b2b814e6c34a5224bc66947c47dab9dfee93b35fb",
"USDC",
6,
),
(
"0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8",
"USDC.e",
6,
),
(
"0x068f5c6a61780768455de69077e07e89787839bf8166decfbf92b645209c0fb8",
"USDT",
6,
),
(
"0x075afe6402ad5a5c20dd25e10ec3b3986acaa647b77e4ae24b0cbc9a54a27a87",
"EKUBO",
18,
),
(
"0x04daa17763b286d1e59b97c283c0b8c949994c361e426a28f743c67bdfe9a32f",
"tBTC",
18,
),
(
"0x0787150e306e6eae6e3f79dea881770e8bbff2c1b8eb490f969669ee945b3135",
"strkBTC",
8,
),
(
"0x0057912720381af14b0e5c87aa4718ed5e527eab60b3801ebf702ab09139e38b",
"wstETH",
18,
),
(
"0x05ce53b9b68fb8e9ecab9283a96d97948914733fd6ed8d9a53a276a419497841",
"ZEC",
8,
),
];
pub(crate) async fn fetch_token_balances(
address: starknet::core::types::Felt,
ds: &Arc<dyn DataSource>,
) -> Vec<crate::data::types::TokenBalance> {
let balance_of_camel = starknet::core::utils::get_selector_from_name("balanceOf").unwrap();
let balance_of_snake = starknet::core::utils::get_selector_from_name("balance_of").unwrap();
let tokens: Vec<(starknet::core::types::Felt, &'static str, u8)> = KNOWN_TOKENS
.iter()
.map(|(hex, name, decimals)| {
(
starknet::core::types::Felt::from_hex(hex).unwrap(),
*name,
*decimals,
)
})
.collect();
let batch_calls: Vec<_> = tokens
.iter()
.map(|(token, _, _)| (*token, balance_of_camel, vec![address]))
.collect();
let batch_results = ds.batch_call_contracts(batch_calls).await;
let mut token_balances = Vec::new();
for ((token, name, decimals), result) in tokens.iter().zip(batch_results.into_iter()) {
let balance_felt = match result {
Ok(v) => v
.first()
.copied()
.unwrap_or(starknet::core::types::Felt::ZERO),
Err(camel_err) => {
match ds
.call_contract(*token, balance_of_snake, vec![address])
.await
{
Ok(v) => v
.first()
.copied()
.unwrap_or(starknet::core::types::Felt::ZERO),
Err(snake_err) => {
warn!(
token = %name,
camel_error = %camel_err,
snake_error = %snake_err,
"balanceOf/balance_of both failed"
);
starknet::core::types::Felt::ZERO
}
}
}
};
if felt_to_u128(&balance_felt) == 0 {
continue; }
token_balances.push(crate::data::types::TokenBalance {
token_address: *token,
token_name: name.to_string(),
balance_raw: balance_felt,
decimals: *decimals,
});
}
token_balances
}
fn pf_txs_to_summaries(
pf_txs: Vec<crate::data::pathfinder::SenderTxEntry>,
) -> Vec<crate::data::types::AddressTxSummary> {
let mut summaries = Vec::new();
for pt in pf_txs {
if pt.hash.is_empty() {
continue; }
let hash = match starknet::core::types::Felt::from_hex(&pt.hash) {
Ok(h) => h,
Err(_) => continue,
};
let fee = u128::from_str_radix(pt.actual_fee.trim_start_matches("0x"), 16).unwrap_or(0);
let tx_type = match pt.tx_type.as_str() {
t if t.starts_with("INVOKE") => "INVOKE".to_string(),
t if t.starts_with("DECLARE") => "DECLARE".to_string(),
t if t.starts_with("DEPLOY_ACCOUNT") => "DEPLOY_ACCOUNT".to_string(),
t if t.starts_with("DEPLOY") => "DEPLOY".to_string(),
t if t.starts_with("L1_HANDLER") => "L1_HANDLER".to_string(),
other => other.to_string(),
};
let sender = pt
.sender_address
.as_deref()
.filter(|s| !s.is_empty())
.and_then(|s| starknet::core::types::Felt::from_hex(s).ok());
summaries.push(crate::data::types::AddressTxSummary {
hash,
nonce: pt.nonce.unwrap_or(0),
block_number: pt.block_number,
timestamp: pt.timestamp,
endpoint_names: String::new(), total_fee_fri: fee,
tip: pt.tip,
tx_type,
status: pt.status,
sender,
called_contracts: Vec::new(), });
}
summaries
}
async fn fetch_tx_summaries_from_hashes(
hashes: &[starknet::core::types::Felt],
block_map: &std::collections::HashMap<starknet::core::types::Felt, u64>,
ds: &Arc<dyn DataSource>,
pf: Option<&Arc<crate::data::pathfinder::PathfinderClient>>,
abi_reg: &Arc<AbiRegistry>,
tx: &mpsc::UnboundedSender<Action>,
progress_prefix: &str,
) -> Vec<crate::data::types::AddressTxSummary> {
let total = hashes.len();
let mut summaries = Vec::new();
for (chunk_idx, chunk) in hashes.chunks(20).enumerate() {
let fetched_so_far = chunk_idx * 20;
if !progress_prefix.is_empty() {
let _ = tx.send(Action::LoadingStatus(format!(
"{progress_prefix} {fetched_so_far}/{total}..."
)));
}
let futs: Vec<_> = chunk
.iter()
.map(|hash| {
let ds_tx = Arc::clone(ds);
let ds_rx = Arc::clone(ds);
let h = *hash;
async move {
let (tx_r, rx_r) = tokio::join!(ds_tx.get_transaction(h), ds_rx.get_receipt(h));
(h, tx_r, rx_r)
}
})
.collect();
let results = futures::future::join_all(futs).await;
for (hash, tx_result, rx_result) in results {
if let Ok(fetched_tx) = tx_result {
let receipt = rx_result.ok();
let block_num = receipt
.as_ref()
.map(|r| r.block_number)
.unwrap_or_else(|| block_map.get(&hash).copied().unwrap_or(0));
summaries.push(helpers::build_tx_summary(
hash,
&fetched_tx,
receipt.as_ref(),
block_num,
0,
abi_reg,
));
}
}
}
helpers::backfill_timestamps(&mut summaries, ds, pf).await;
summaries
}
pub(super) async fn build_tx_summaries_from_pf_rows(
tx_rows: &[crate::data::pathfinder::TxByHashData],
abi_reg: &Arc<AbiRegistry>,
) -> Vec<crate::data::types::AddressTxSummary> {
let mut targets: std::collections::HashSet<starknet::core::types::Felt> =
std::collections::HashSet::new();
for row in tx_rows {
for t in helpers::pf_tx_target_addresses(row) {
targets.insert(t);
}
}
helpers::prewarm_abis(targets, abi_reg).await;
tx_rows
.iter()
.filter_map(|row| helpers::build_tx_summary_from_pf_data(row, abi_reg))
.collect()
}
const MAX_CALL_REENRICH_ROWS: usize = 200;
fn call_row_is_incomplete(c: &crate::data::types::ContractCallSummary) -> bool {
c.sender == starknet::core::types::Felt::ZERO
}
fn persist_calls_merged(
ds: &Arc<dyn DataSource>,
address: starknet::core::types::Felt,
calls: &[crate::data::types::ContractCallSummary],
cached: Option<Vec<crate::data::types::ContractCallSummary>>,
) {
if calls.is_empty() {
return;
}
let touched: std::collections::HashSet<_> = calls.iter().map(|c| c.tx_hash).collect();
let mut merged = cached.unwrap_or_else(|| ds.load_cached_address_calls(&address));
merged.extend(calls.iter().cloned());
let rows: Vec<_> = crate::data::types::deduplicate_contract_calls(merged)
.into_iter()
.filter(|c| touched.contains(&c.tx_hash))
.collect();
ds.save_address_calls(&address, &rows);
}
pub(super) async fn build_contract_calls_from_hashes(
address: starknet::core::types::Felt,
hashes_with_blocks: &[(starknet::core::types::Felt, u64)],
ds: &Arc<dyn DataSource>,
pf: Option<&Arc<crate::data::pathfinder::PathfinderClient>>,
abi_reg: &Arc<AbiRegistry>,
) -> Vec<crate::data::types::ContractCallSummary> {
let mut calls_list = Vec::new();
for chunk in hashes_with_blocks.chunks(20) {
let futs: Vec<_> = chunk
.iter()
.map(|(hash, block_num)| {
let ds_t = Arc::clone(ds);
let ds_r = Arc::clone(ds);
let h = *hash;
let bn = *block_num;
async move {
let (mut tx_r, mut rx_r) = {
let _permit = ENRICH_RPC_SEMAPHORE.acquire().await.ok();
tokio::join!(ds_t.get_transaction(h), ds_r.get_receipt(h))
};
for backoff_ms in [400u64, 1200] {
if tx_r.is_ok() && rx_r.is_ok() {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(backoff_ms)).await;
let (retry_tx, retry_rx) = {
let _permit = ENRICH_RPC_SEMAPHORE.acquire().await.ok();
tokio::join!(ds_t.get_transaction(h), ds_r.get_receipt(h))
};
if tx_r.is_err() {
tx_r = retry_tx;
}
if rx_r.is_err() {
rx_r = retry_rx;
}
}
(h, bn, tx_r, rx_r)
}
})
.collect();
let results = futures::future::join_all(futs).await;
let prewarm_targets: std::collections::HashSet<_> = results
.iter()
.filter_map(|(_, _, tx_r, _)| tx_r.as_ref().ok())
.flat_map(|tx| match tx {
crate::data::types::SnTransaction::Invoke(i) => parse_multicall(&i.calldata),
_ => Vec::new(),
})
.map(|c| c.contract_address)
.chain(std::iter::once(address))
.collect();
helpers::prewarm_abis(prewarm_targets, abi_reg).await;
for (hash, block_num, tx_r, rx_r) in results {
if let Err(e) = &tx_r {
warn!(
tx = %format!("{:#x}", hash),
block = block_num,
error = %e,
"Call enrichment: tx fetch failed, row left unenriched"
);
} else if let Err(e) = &rx_r {
warn!(
tx = %format!("{:#x}", hash),
block = block_num,
error = %e,
"Call enrichment: receipt fetch failed, row has no fee/status"
);
}
if let Ok(fetched_tx) = tx_r {
let receipt = rx_r.ok();
let fee_fri = receipt
.as_ref()
.map(|r| felt_to_u128(&r.actual_fee))
.unwrap_or(0);
let status = receipt
.as_ref()
.map(|r| match &r.execution_status {
crate::data::types::ExecutionStatus::Succeeded => "OK",
crate::data::types::ExecutionStatus::Reverted(_) => "REV",
_ => "?",
})
.unwrap_or("?")
.to_string();
let function_name = helpers::format_endpoint_names(&fetched_tx, abi_reg);
let (nonce, tip) = helpers::extract_nonce_tip(&fetched_tx);
let inner_targets = match &fetched_tx {
crate::data::types::SnTransaction::Invoke(i) => {
let calls = parse_multicall(&i.calldata);
helpers::oe_inner_targets(&calls, abi_reg)
}
_ => Vec::new(),
};
calls_list.push(crate::data::types::ContractCallSummary {
tx_hash: hash,
sender: fetched_tx.sender(),
function_name,
block_number: block_num,
timestamp: 0,
total_fee_fri: fee_fri,
status,
nonce: Some(nonce),
tip,
inner_targets,
});
}
}
}
helpers::backfill_call_timestamps(&mut calls_list, ds, pf).await;
crate::data::types::deduplicate_contract_calls(calls_list)
}
pub(super) async fn build_contract_calls_from_pf_rows(
address: starknet::core::types::Felt,
tx_rows: &[crate::data::pathfinder::TxByHashData],
abi_reg: &Arc<AbiRegistry>,
) -> Vec<crate::data::types::ContractCallSummary> {
use starknet::core::types::Felt;
let prewarm_targets: std::collections::HashSet<Felt> = tx_rows
.iter()
.filter(|r| helpers::normalize_pf_tx_type(&r.tx_type) == "INVOKE")
.flat_map(|r| {
let calldata: Vec<Felt> = r
.calldata
.iter()
.filter_map(|h| Felt::from_hex(h).ok())
.collect();
parse_multicall(&calldata)
})
.map(|c| c.contract_address)
.chain(std::iter::once(address))
.collect();
helpers::prewarm_abis(prewarm_targets, abi_reg).await;
let mut calls_list: Vec<crate::data::types::ContractCallSummary> =
Vec::with_capacity(tx_rows.len());
for row in tx_rows {
let Ok(tx_hash) = Felt::from_hex(&row.hash) else {
continue;
};
let Ok(sender) = Felt::from_hex(&row.sender) else {
continue;
};
let fee_fri =
u128::from_str_radix(row.actual_fee.trim_start_matches("0x"), 16).unwrap_or(0);
let (function_name, inner_targets) = if helpers::normalize_pf_tx_type(&row.tx_type)
== "INVOKE"
{
let calldata: Vec<Felt> = row
.calldata
.iter()
.filter_map(|h| Felt::from_hex(h).ok())
.collect();
let calls = parse_multicall(&calldata);
let name = helpers::format_selector_names(calls.iter().map(|c| c.selector), abi_reg);
let inner = helpers::oe_inner_targets(&calls, abi_reg);
(name, inner)
} else {
(String::new(), Vec::new())
};
calls_list.push(crate::data::types::ContractCallSummary {
tx_hash,
sender,
function_name,
block_number: row.block_number,
timestamp: row.block_timestamp,
total_fee_fri: fee_fri,
status: row.status.clone(),
nonce: row.nonce,
tip: row.tip,
inner_targets,
});
}
crate::data::types::deduplicate_contract_calls(calls_list)
}
pub(super) async fn fetch_and_send_address_info(
address: starknet::core::types::Felt,
ds: &Arc<dyn DataSource>,
abi_reg: &Arc<AbiRegistry>,
dune: &Option<Arc<dune::DuneClient>>,
pf: &Option<Arc<crate::data::pathfinder::PathfinderClient>>,
voyager_c: &Option<Arc<voyager::VoyagerClient>>,
head_block: &Arc<super::HeadTracker>,
tx: &mpsc::UnboundedSender<Action>,
cancel: &CancellationToken,
) {
let start = std::time::Instant::now();
debug!(address = %format!("{:#x}", address), "Fetching address info");
{
let ds_bal = Arc::clone(ds);
let tx_bal = tx.clone();
spawn_cancellable(cancel.clone(), async move {
let balances = fetch_token_balances(address, &ds_bal).await;
let _ = tx_bal.send(Action::AddressBalancesLoaded { address, balances });
});
}
if let Some(vc) = voyager_c {
let vc = Arc::clone(vc);
let tx_v = tx.clone();
spawn_cancellable(cancel.clone(), async move {
voyager::fetch_and_send_label(address, &vc, &tx_v).await;
});
}
let cached_nonce_info = ds.load_cached_nonce(&address);
let cached_class_history = ds.load_cached_class_history(&address);
let cached_deploy = ds.load_cached_deploy_info(&address);
let cached_events = ds.load_address_events(&address);
let cached_txs = ds.load_cached_address_txs(&address);
let cached_calls = ds.load_cached_address_calls(&address);
let cached_meta_txs = ds.load_cached_meta_txs(&address);
let cached_top_class_hash = ds.load_cached_class_hash(&address).or_else(|| {
cached_class_history
.first()
.and_then(|e| starknet::core::types::Felt::from_hex(&e.class_hash).ok())
});
let cached_nonce_felt = cached_nonce_info
.map(|(n, _)| n)
.unwrap_or(starknet::core::types::Felt::ZERO);
let cached_nonce_block = cached_nonce_info.map(|(_, b)| b).unwrap_or(0);
let _ = tx.send(Action::AddressSourcesPending {
address,
sources: vec![Source::Rpc],
});
if !cached_class_history.is_empty() {
let _ = tx.send(Action::ClassHistoryLoaded {
address,
entries: cached_class_history.clone(),
});
}
if let Some((_, cached_deploy_block, _)) = cached_deploy {
let ds_c = Arc::clone(ds);
let tx_c = tx.clone();
spawn_cancellable(cancel.clone(), async move {
find_deploy_tx(address, cached_deploy_block, &ds_c, &tx_c).await;
});
} else if let Some(earliest) = cached_class_history.last() {
let deploy_block = earliest.block_number;
let ds_c = Arc::clone(ds);
let tx_c = tx.clone();
spawn_cancellable(cancel.clone(), async move {
find_deploy_tx(address, deploy_block, &ds_c, &tx_c).await;
});
}
let scanned_call_ranges = ds.load_call_scanned_ranges(&address);
let call_tracked_from = ds
.load_search_progress(&address, FilterKind::DuneCalls)
.map(|(lo, _)| lo);
if !scanned_call_ranges.is_empty() || call_tracked_from.is_some() {
let _ = tx.send(Action::AddressCallScannedRangesLoaded {
address,
ranges: scanned_call_ranges,
tracked_from: call_tracked_from,
});
}
let cached_transfers = ds.load_cached_transfers(&address);
let transfer_ranges = ds.load_transfer_scanned_ranges(&address);
if !transfer_ranges.is_empty() {
let _ = tx.send(Action::AddressTransferScannedRangesLoaded {
address,
ranges: transfer_ranges,
});
}
if !cached_transfers.is_empty() {
let _ = tx.send(Action::AddressTransfersLoaded {
address,
rows: cached_transfers,
scanned: None,
});
}
let _ = tx.send(Action::AddressInfoLoaded {
info: crate::data::types::SnAddressInfo {
address,
nonce: cached_nonce_felt,
class_hash: cached_top_class_hash,
recent_events: cached_events.clone(),
token_balances: Vec::new(),
},
decoded_events: Vec::new(),
tx_summaries: cached_txs.clone(),
contract_calls: cached_calls.clone(),
});
let incomplete: Vec<(starknet::core::types::Felt, u64)> = cached_calls
.iter()
.filter(|c| call_row_is_incomplete(c))
.take(MAX_CALL_REENRICH_ROWS)
.map(|c| (c.tx_hash, c.block_number))
.collect();
if !incomplete.is_empty() {
debug!(
addr = %format!("{:#x}", address),
rows = incomplete.len(),
"Re-enriching incomplete cached call rows"
);
let ds_c = Arc::clone(ds);
let pf_c = pf.clone();
let abi_c = Arc::clone(abi_reg);
let tx_c = tx.clone();
spawn_cancellable(cancel.clone(), async move {
let calls = build_contract_calls_from_hashes(
address,
&incomplete,
&ds_c,
pf_c.as_ref(),
&abi_c,
)
.await;
if !calls.is_empty() {
let _ = tx_c.send(Action::AddressCallsEnriched { address, calls });
}
});
}
if !cached_meta_txs.is_empty() {
let _ = tx.send(Action::AddressMetaTxsCacheLoaded {
address,
summaries: cached_meta_txs,
});
}
if !cached_events.is_empty() {
let abi_reg_c = Arc::clone(abi_reg);
let tx_c = tx.clone();
let evs = cached_events.clone();
spawn_cancellable(cancel.clone(), async move {
let unique_addrs: std::collections::HashSet<starknet::core::types::Felt> =
evs.iter().map(|e| e.from_address).collect();
helpers::prewarm_abis(unique_addrs, &abi_reg_c).await;
use futures::stream::StreamExt;
let abi_reg = &abi_reg_c;
let event_futs: Vec<_> = evs
.iter()
.map(|event| async move {
let abi = abi_reg.get_abi_for_address(&event.from_address).await;
decode_event(event, abi.as_deref())
})
.collect();
let decoded: Vec<_> = futures::stream::iter(event_futs)
.buffered(8)
.collect()
.await;
let _ = tx_c.send(Action::AddressEventsCacheLoaded {
address,
decoded_events: decoded,
});
});
}
let ds2 = Arc::clone(ds);
let (nonce_r, class_r) = tokio::join!(ds.get_nonce(address), ds2.get_class_hash(address));
let nonce = nonce_r.unwrap_or(starknet::core::types::Felt::ZERO);
let class_hash = class_r.ok();
let is_contract = nonce == starknet::core::types::Felt::ZERO && class_hash.is_some();
if class_hash.is_none() {
let _ = tx.send(Action::AddressNotDeployed { address });
return;
}
let nonce_delta = if let Some((prev_nonce, _prev_block)) = &cached_nonce_info {
let prev = crate::utils::felt_to_u64(prev_nonce);
let curr = crate::utils::felt_to_u64(&nonce);
curr.saturating_sub(prev)
} else {
u64::MAX };
const HEAD_STALENESS_SECS: u64 = 12;
let latest_block = {
let (block, age) = head_block.read();
if block > 0 && age <= HEAD_STALENESS_SECS {
block
} else {
match ds.get_latest_block_number().await {
Ok(n) => {
head_block.update(n);
n
}
Err(_) => block, }
}
};
if nonce != starknet::core::types::Felt::ZERO {
ds.save_cached_nonce(&address, &nonce, latest_block);
}
let had_cached_deploy = cached_deploy.is_some();
let kicked_deploy_lookup = had_cached_deploy || !cached_class_history.is_empty();
let cache_is_fresh = match (
cached_class_history
.first()
.and_then(|e| starknet::core::types::Felt::from_hex(&e.class_hash).ok()),
class_hash,
) {
(Some(top), Some(live)) => top == live,
_ => false,
};
if let Some(pf_client) = pf {
if cache_is_fresh {
ds.save_class_history_max_block(&address, latest_block);
} else {
let pf_c = Arc::clone(pf_client);
let ds_c = Arc::clone(ds);
let tx_c = tx.clone();
let addr = address;
let cancel_c = cancel.clone();
spawn_cancellable(cancel.clone(), async move {
match pf_c.get_class_history(addr).await {
Ok(entries) => {
ds_c.save_class_history(&addr, &entries);
ds_c.save_class_history_max_block(&addr, latest_block);
if !kicked_deploy_lookup && let Some(deploy_entry) = entries.last() {
let deploy_block = deploy_entry.block_number;
let tx_c2 = tx_c.clone();
let ds_c2 = Arc::clone(&ds_c);
spawn_cancellable(cancel_c.clone(), async move {
find_deploy_tx(addr, deploy_block, &ds_c2, &tx_c2).await;
});
}
debug!(
address = %format!("{:#x}", addr),
entries = entries.len(),
"PF class history fetched"
);
let _ = tx_c.send(Action::ClassHistoryLoaded {
address: addr,
entries,
});
}
Err(e) => {
warn!(error = %e, "PF class history fetch failed");
let _ = tx_c.send(Action::SourceUpdate {
source: Source::Pathfinder,
status: crate::app::state::SourceStatus::FetchError(e.to_string()),
});
}
}
});
}
}
let mut sources = vec![Source::Rpc];
if !is_contract && pf.is_some() {
sources.push(Source::Pathfinder);
}
if dune.is_some() {
sources.push(Source::Dune);
}
let _ = tx.send(Action::AddressSourcesPending {
address,
sources: sources.clone(),
});
let _ = tx.send(Action::AddressInfoLoaded {
info: crate::data::types::SnAddressInfo {
address,
nonce,
class_hash,
recent_events: cached_events.clone(),
token_balances: Vec::new(),
},
decoded_events: Vec::new(),
tx_summaries: cached_txs,
contract_calls: cached_calls,
});
let cached_range = ds.load_cached_activity_range(&address);
if let Some((min_b, max_b)) = cached_range {
debug!(
address = %format!("{:#x}", address),
cached_range = format!("{}..{}", min_b, max_b),
"Using cached activity range"
);
let _ = tx.send(Action::LoadingStatus(format!(
"Cached range: blocks {}..{}",
min_b, max_b
)));
}
let (probe_watch_tx, probe_watch_rx) =
tokio::sync::watch::channel::<Option<dune::AddressActivityProbe>>(None);
let cached_range_with_count = ds.load_cached_activity_range_with_count(&address);
if let Some((min_b, max_b)) = cached_range {
let event_count = cached_range_with_count
.map(|(_, _, c)| c)
.filter(|&c| c > 1) .unwrap_or_else(|| {
let span = max_b.saturating_sub(min_b).max(1);
(span / 100).max(100)
});
let probe = dune::AddressActivityProbe {
callee_min_block: min_b,
callee_max_block: max_b,
callee_call_count: event_count,
..Default::default()
};
let _ = tx.send(Action::AddressProbeLoaded {
address,
probe: probe.clone(),
});
let _ = probe_watch_tx.send(Some(probe));
} else {
let stale_cached = ds.load_cached_activity_range_any_age(&address);
let dune_probe: Option<Arc<dune::DuneClient>> = dune.as_ref().map(Arc::clone);
let ds_probe = Arc::clone(ds);
let tx_probe = tx.clone();
let probe_watch_tx_c = probe_watch_tx.clone();
spawn_cancellable(cancel.clone(), async move {
let publish = |probe: dune::AddressActivityProbe, label: &str, persist: bool| {
if probe.has_activity() {
let _ = tx_probe.send(Action::LoadingStatus(format!(
"{label}: {} events, blocks {}..{}",
probe.callee_call_count,
probe.min_block(),
probe.max_block(),
)));
if persist {
ds_probe.save_activity_range_with_count(
&address,
probe.min_block(),
probe.max_block(),
probe.callee_call_count,
);
}
} else {
let _ =
tx_probe.send(Action::LoadingStatus(format!("{label}: no activity found")));
}
let _ = tx_probe.send(Action::AddressProbeLoaded {
address,
probe: probe.clone(),
});
let _ = probe_watch_tx_c.send(Some(probe));
};
if let Some(dune_c) = dune_probe.as_ref() {
match stale_cached {
Some((cached_min, cached_max, cached_count)) => {
let stale_probe = dune::AddressActivityProbe {
callee_min_block: cached_min,
callee_max_block: cached_max,
callee_call_count: cached_count,
..Default::default()
};
let _ = tx_probe.send(Action::AddressProbeLoaded {
address,
probe: stale_probe.clone(),
});
let _ = probe_watch_tx_c.send(Some(stale_probe));
let _ = tx_probe.send(Action::LoadingStatus(format!(
"Dune: extending activity probe (>{} block)...",
cached_max
)));
let delta_min_date = block_date_floor(&ds_probe, cached_max).await;
match dune_c
.probe_address_activity_delta(address, cached_max, delta_min_date)
.await
{
Ok(delta) => {
let merged = dune::AddressActivityProbe {
callee_min_block: cached_min,
callee_max_block: delta.callee_max_block.max(cached_max),
callee_call_count: cached_count
.saturating_add(delta.callee_call_count),
..Default::default()
};
publish(merged, "Dune probe (delta)", true);
}
Err(e) => {
warn!(error = %e, "Dune delta activity probe failed");
let _ = tx_probe.send(Action::LoadingStatus(format!(
"Dune probe failed: {}",
e
)));
}
}
}
None => {
const RECENT_PROBE_DAYS: i64 = 14;
let _ = tx_probe.send(Action::LoadingStatus(
"Dune: probing activity range (events)...".into(),
));
let recent_min = chrono::Utc::now().date_naive()
- chrono::Duration::days(RECENT_PROBE_DAYS);
let recent_fut = dune_c.probe_address_activity(address, Some(recent_min));
let full_fut = dune_c.probe_address_activity(address, None);
tokio::pin!(recent_fut, full_fut);
let full_result = tokio::select! {
full = &mut full_fut => full,
recent = &mut recent_fut => {
if let Ok(recent) = recent
&& recent.has_activity()
{
publish(recent, "Dune probe (recent)", false);
}
(&mut full_fut).await
}
};
match full_result {
Ok(probe) => publish(probe, "Dune probe", true),
Err(e) => {
warn!(error = %e, "Dune activity probe failed");
let _ = tx_probe.send(Action::LoadingStatus(format!(
"Dune probe failed: {}",
e
)));
}
}
}
}
}
});
}
let pf_succeeded = Arc::new(AtomicBool::new(false));
if !is_contract && let Some(pf_client) = pf {
let pf_c = Arc::clone(pf_client);
let tx_a = tx.clone();
let ds_a = Arc::clone(ds);
let pf_ok = Arc::clone(&pf_succeeded);
spawn_cancellable(cancel.clone(), async move {
const PF_LIMIT: u32 = 200;
let _ = tx_a.send(Action::LoadingStatus("PF: fetching tx history...".into()));
match pf_c.get_sender_txs(address, PF_LIMIT, None, None).await {
Ok(pf_txs) => {
let _ = tx_a.send(Action::SourceUpdate {
source: Source::Pathfinder,
status: crate::app::state::SourceStatus::Live,
});
let real_count = pf_txs.iter().filter(|t| !t.hash.is_empty()).count();
info!(
pf_txs = pf_txs.len(),
real_txs = real_count,
"PF sender-txs returned"
);
let summaries = pf_txs_to_summaries(pf_txs);
if !summaries.is_empty() {
let min_b = summaries.iter().map(|s| s.block_number).min().unwrap_or(0);
let max_b = summaries.iter().map(|s| s.block_number).max().unwrap_or(0);
let _ = tx_a.send(Action::LoadingStatus(format!(
"PF: {} txs, blocks {}..{}",
summaries.len(),
min_b,
max_b
)));
ds_a.save_address_txs(&address, &summaries);
if min_b > 0 {
ds_a.save_activity_range(&address, min_b, max_b);
}
pf_ok.store(true, Ordering::Release);
}
let _ = tx_a.send(Action::AddressTxsStreamed {
address,
source: Source::Pathfinder,
tx_summaries: summaries,
complete: true,
});
}
Err(e) => {
warn!(error = %e, "PF sender-txs failed");
let _ = tx_a.send(Action::SourceUpdate {
source: Source::Pathfinder,
status: crate::app::state::SourceStatus::FetchError(e.to_string()),
});
let _ = tx_a.send(Action::AddressTxsStreamed {
address,
source: Source::Pathfinder,
tx_summaries: Vec::new(),
complete: true,
});
}
}
});
}
if is_contract {
let ds_b = Arc::clone(ds);
let tx_b = tx.clone();
let abi_b = Arc::clone(abi_reg);
let dune_b = dune.as_ref().map(Arc::clone);
let pf_b = pf.as_ref().map(Arc::clone);
let voyager_b = voyager_c.as_ref().map(Arc::clone);
spawn_cancellable(cancel.clone(), async move {
let _ = tx_b.send(Action::LoadingStatus("Calls: fetching from Dune...".into()));
fetch_address_contract_calls(
address,
&ds_b,
dune_b.as_ref(),
pf_b.as_ref(),
voyager_b.as_ref(),
&abi_b,
&tx_b,
nonce,
class_hash,
)
.await;
debug!(
address = %format!("{:#x}", address),
source = ?Source::Dune,
tx_summaries = 0,
"AddressTxsStreamed dispatching complete marker"
);
let _ = tx_b.send(Action::AddressTxsStreamed {
address,
source: Source::Dune,
tx_summaries: Vec::new(),
complete: true,
});
});
}
if !is_contract && let Some(dune_client) = dune {
let dune_c = Arc::clone(dune_client);
let tx_b = tx.clone();
let ds_b = Arc::clone(ds);
let mut probe_rx_b = probe_watch_rx.clone();
const DUNE_PAGE_LIMIT: u32 = 100;
const INITIAL_WINDOW: u64 = 5_000;
spawn_cancellable(cancel.clone(), async move {
let _ = tx_b.send(Action::LoadingStatus(
"Dune: fetching recent account txs...".into(),
));
let latest_block = ds_b.latest_block_hint().await.unwrap_or(0);
let from = latest_block.saturating_sub(INITIAL_WINDOW);
let from_min_date = block_date_floor(&ds_b, from).await;
let result = dune_c
.query_account_txs_windowed(
address,
from,
latest_block,
DUNE_PAGE_LIMIT,
from_min_date,
)
.await;
match result {
Ok(dune_txs) if !dune_txs.is_empty() => {
let _ = tx_b.send(dune_source_update(crate::app::state::SourceStatus::Live));
info!(
dune_txs = dune_txs.len(),
"Dune windowed account txs complete"
);
let min_b = dune_txs.iter().map(|t| t.block_number).min().unwrap_or(0);
let max_b = dune_txs.iter().map(|t| t.block_number).max().unwrap_or(0);
let _ = tx_b.send(Action::LoadingStatus(format!(
"Dune: {} txs, blocks {}..{}",
dune_txs.len(),
min_b,
max_b
)));
let _ = tx_b.send(Action::AddressTxsStreamed {
address,
source: Source::Dune,
tx_summaries: dune_txs,
complete: true,
});
}
Ok(_) | Err(_) => {
if let Err(ref e) = result {
warn!(error = %e, "Dune windowed account txs failed");
}
let probe = tokio::time::timeout(
tokio::time::Duration::from_secs(10),
probe_rx_b.wait_for(|p| p.is_some()),
)
.await
.ok()
.and_then(|r| r.ok())
.and_then(|p| p.clone());
if let Some(p) = probe {
if p.has_activity() {
let window = p.recommended_window();
let probe_from = p.max_block().saturating_sub(window);
let probe_to = p.max_block();
let _ = tx_b.send(Action::LoadingStatus(format!(
"Dune: retrying blocks {}..{} (probe-guided)...",
probe_from, probe_to
)));
let probe_min_date = block_date_floor(&ds_b, probe_from).await;
match dune_c
.query_account_txs_windowed(
address,
probe_from,
probe_to,
DUNE_PAGE_LIMIT,
probe_min_date,
)
.await
{
Ok(txs) if !txs.is_empty() => {
let _ = tx_b.send(dune_source_update(
crate::app::state::SourceStatus::Live,
));
let min_b =
txs.iter().map(|t| t.block_number).min().unwrap_or(0);
let max_b =
txs.iter().map(|t| t.block_number).max().unwrap_or(0);
let _ = tx_b.send(Action::LoadingStatus(format!(
"Dune: {} txs, blocks {}..{}",
txs.len(),
min_b,
max_b
)));
let _ = tx_b.send(Action::AddressTxsStreamed {
address,
source: Source::Dune,
tx_summaries: txs,
complete: true,
});
}
_ => {
let _ = tx_b.send(Action::AddressTxsStreamed {
address,
source: Source::Dune,
tx_summaries: Vec::new(),
complete: true,
});
}
}
} else {
let _ = tx_b.send(Action::AddressTxsStreamed {
address,
source: Source::Dune,
tx_summaries: Vec::new(),
complete: true,
});
}
} else {
match dune_c.query_account_txs(address, DUNE_PAGE_LIMIT).await {
Ok(txs) => {
let _ = tx_b.send(dune_source_update(
crate::app::state::SourceStatus::Live,
));
let _ = tx_b.send(Action::AddressTxsStreamed {
address,
source: Source::Dune,
tx_summaries: txs,
complete: true,
});
}
Err(e) => {
warn!(error = %e, "Dune account txs fallback failed");
let _ = tx_b.send(dune_source_update(
crate::app::state::SourceStatus::FetchError(e.to_string()),
));
let _ = tx_b.send(Action::AddressTxsStreamed {
address,
source: Source::Dune,
tx_summaries: Vec::new(),
complete: true,
});
}
}
}
}
}
});
}
{
let ds_c = Arc::clone(ds);
let abi_c = Arc::clone(abi_reg);
let tx_c = tx.clone();
let voyager_c2 = voyager_c.clone();
let _pf_ok = Arc::clone(&pf_succeeded);
let pf_c = pf.as_ref().map(Arc::clone);
let mut probe_rx_c = probe_watch_rx.clone();
spawn_cancellable(cancel.clone(), async move {
let latest_block = ds_c.latest_block_hint().await.unwrap_or(0);
let filter_kind = if is_contract {
crate::data::FilterKind::Unkeyed
} else {
crate::data::FilterKind::Keyed
};
let search_progress = ds_c.load_search_progress(&address, filter_kind);
let from_block = if let Some((_min_searched, max_searched)) = search_progress {
if !is_contract && nonce_delta == 0 && max_searched + 100 >= latest_block {
debug!(
address = %format!("{:#x}", address),
"Nonce unchanged & search progress up-to-date, skipping RPC event scan"
);
let _ = tx_c.send(Action::AddressTxsStreamed {
address,
source: Source::Rpc,
tx_summaries: Vec::new(),
complete: true,
});
return;
}
max_searched + 1
} else if !is_contract && cached_nonce_block > 0 && nonce_delta < 50 {
cached_nonce_block
} else {
let initial_window = if is_contract { 10_000u64 } else { 5_000u64 };
latest_block.saturating_sub(initial_window)
};
let window_size = latest_block.saturating_sub(from_block);
let _ = tx_c.send(Action::LoadingStatus(format!(
"RPC: scanning {}k blocks for events...",
window_size.div_ceil(1000)
)));
let kind = if is_contract {
EventQueryKind::Contract
} else {
EventQueryKind::Account
};
let _events_guard = QueryGuard::new(
&tx_c,
format!("events:{}", query_addr_prefix(&address)),
"Events scan".to_string(),
);
let ds_dyn: Arc<dyn crate::data::DataSource> = ds_c.clone();
let pf_page: Option<AddressActivityPage> =
match crate::network::event_window::ensure_address_events_window(
address,
kind,
crate::network::event_window::EventWindowPolicy::TopDelta,
pf_c.as_ref(),
&ds_dyn,
latest_block,
0, )
.await
{
Ok(o) => {
let _ = tx_c.send(Action::AddressEventWindowUpdated {
address,
min_searched: o.min_searched,
max_searched: o.max_searched,
deferred_gap: o.deferred_gap,
});
Some(o.page)
}
Err(e) => {
warn!(
error = %e,
address = %format!("{:#x}", address),
"event_window fetch failed in TASK C"
);
let _ =
tx_c.send(Action::LoadingStatus(format!("Event fetch failed: {}", e)));
let _ = tx_c.send(Action::SourceUpdate {
source: Source::Rpc,
status: crate::app::state::SourceStatus::FetchError(e.to_string()),
});
None
}
};
let events = pf_page
.as_ref()
.map(|p| p.events.clone())
.unwrap_or_default();
let pf_page: Option<AddressActivityPage> = pf_page.filter(|p| !p.tx_rows.is_empty());
let events_count = events.len();
debug!(
address = %format!("{:#x}", address),
events = events_count,
via_pf = pf_page.is_some(),
"Phase 1 events fetched"
);
if events_count > 0 {
let min_b = events.iter().map(|e| e.block_number).min().unwrap_or(0);
let max_b = events.iter().map(|e| e.block_number).max().unwrap_or(0);
let _ = tx_c.send(Action::LoadingStatus(format!(
"RPC: found {} events in blocks {}..{}",
events_count, min_b, max_b
)));
} else {
let _ = tx_c.send(Action::LoadingStatus(
"RPC: no recent events, waiting for probe...".into(),
));
}
let (unique_hashes, tx_block_map): (
Vec<starknet::core::types::Felt>,
std::collections::HashMap<starknet::core::types::Felt, u64>,
) = if let Some(page) = &pf_page {
(page.unique_hashes.clone(), page.tx_block_map.clone())
} else {
let mut seen = std::collections::HashSet::new();
let mut hashes: Vec<starknet::core::types::Felt> = Vec::with_capacity(events.len());
let mut map: std::collections::HashMap<starknet::core::types::Felt, u64> =
std::collections::HashMap::new();
for e in &events {
if e.transaction_hash != starknet::core::types::Felt::ZERO
&& seen.insert(e.transaction_hash)
{
hashes.push(e.transaction_hash);
}
map.entry(e.transaction_hash).or_insert(e.block_number);
}
(hashes, map)
};
let decoded_events = decode_events_dedup(&events, &abi_c).await;
if !events.is_empty() {
let min_b = events.iter().map(|e| e.block_number).min().unwrap_or(0);
let max_b = events.iter().map(|e| e.block_number).max().unwrap_or(0);
if min_b > 0 {
ds_c.save_activity_range(&address, min_b, max_b);
}
}
let can_receive_calls = class_hash.is_some();
if can_receive_calls && !unique_hashes.is_empty() {
let mut contract_calls_list = if let Some(page) = &pf_page {
build_contract_calls_from_pf_rows(address, &page.tx_rows, &abi_c).await
} else {
let call_hashes: Vec<_> = unique_hashes
.iter()
.map(|h| (*h, *tx_block_map.get(h).unwrap_or(&0)))
.collect();
build_contract_calls_from_hashes(
address,
&call_hashes,
&ds_c,
pf_c.as_ref(),
&abi_c,
)
.await
};
contract_calls_list.sort_by(|a, b| b.block_number.cmp(&a.block_number));
persist_calls_merged(&ds_c, address, &contract_calls_list, None);
{
let callers = contract_calls_list.iter().map(|c| c.sender);
spawn_voyager_prefetch(callers, &voyager_c2, &tx_c);
}
let _ = tx_c.send(Action::AddressInfoLoaded {
info: crate::data::types::SnAddressInfo {
address,
nonce,
class_hash,
recent_events: events.clone(),
token_balances: Vec::new(),
},
decoded_events,
tx_summaries: Vec::new(),
contract_calls: contract_calls_list,
});
} else if !decoded_events.is_empty() {
let _ = tx_c.send(Action::AddressInfoLoaded {
info: crate::data::types::SnAddressInfo {
address,
nonce,
class_hash,
recent_events: events.clone(),
token_balances: Vec::new(),
},
decoded_events,
tx_summaries: Vec::new(),
contract_calls: Vec::new(),
});
}
if !is_contract {
let cached_txs = ds_c.load_cached_address_txs(&address);
let cached_hashes: std::collections::HashSet<_> =
cached_txs.iter().map(|t| t.hash).collect();
let summaries = if let Some(page) = &pf_page {
let new_rows: Vec<_> = page
.tx_rows
.iter()
.filter(|r| {
starknet::core::types::Felt::from_hex(&r.hash)
.map(|h| !cached_hashes.contains(&h))
.unwrap_or(false)
})
.cloned()
.collect();
if new_rows.is_empty() {
Vec::new()
} else {
build_tx_summaries_from_pf_rows(&new_rows, &abi_c).await
}
} else {
let hashes_to_fetch: Vec<_> = unique_hashes
.iter()
.filter(|h| !cached_hashes.contains(h))
.copied()
.collect();
if hashes_to_fetch.is_empty() {
Vec::new()
} else {
fetch_tx_summaries_from_hashes(
&hashes_to_fetch,
&tx_block_map,
&ds_c,
pf_c.as_ref(),
&abi_c,
&tx_c,
"RPC: fetching txs",
)
.await
}
};
if !summaries.is_empty() {
let mut all_txs = cached_txs;
for s in &summaries {
if !all_txs.iter().any(|t| t.hash == s.hash) {
all_txs.push(s.clone());
}
}
all_txs.sort_by(|a, b| b.nonce.cmp(&a.nonce));
ds_c.save_address_txs(&address, &all_txs);
let _ = tx_c.send(Action::AddressTxsStreamed {
address,
source: Source::Rpc,
tx_summaries: summaries,
complete: false,
});
}
}
if let Some(page) = &pf_page
&& !page.tx_rows.is_empty()
{
let meta_summaries = derive_meta_txs_from_page(address, page, &abi_c).await;
if !meta_summaries.is_empty() {
ds_c.save_meta_txs(&address, &meta_summaries);
}
}
let phase1_found = !unique_hashes.is_empty();
let mut any_events_found = phase1_found;
if !phase1_found {
let probe_timeout = if cached_range.is_some() { 0 } else { 5 };
let probe = tokio::time::timeout(
tokio::time::Duration::from_secs(probe_timeout),
probe_rx_c.wait_for(|p| p.is_some()),
)
.await
.ok()
.and_then(|r| r.ok())
.and_then(|p| p.clone());
if let Some(p) = probe {
let has_activity = p.callee_call_count > 0;
if has_activity {
let window = p.recommended_window();
let search_from = p.max_block().saturating_sub(window);
let search_to = p.max_block();
let _ = tx_c.send(Action::LoadingStatus(format!(
"RPC: probe-guided search blocks {}..{}...",
search_from, search_to
)));
let kind = if is_contract {
EventQueryKind::Contract
} else {
EventQueryKind::Account
};
let deeper_events = fetch_events_routed(
kind,
pf_c.as_ref(),
&ds_c,
address,
Some(search_from),
Some(search_to),
500,
)
.await;
if let Ok(deeper_events) = deeper_events
&& !deeper_events.is_empty()
{
any_events_found = true;
let min_b = deeper_events
.iter()
.map(|e| e.block_number)
.min()
.unwrap_or(0);
let max_b = deeper_events
.iter()
.map(|e| e.block_number)
.max()
.unwrap_or(0);
if min_b > 0 {
ds_c.save_activity_range(&address, min_b, max_b);
}
let mut deep_seen = std::collections::HashSet::new();
let deep_hashes: Vec<_> = deeper_events
.iter()
.filter_map(|e| {
let h = e.transaction_hash;
if h != starknet::core::types::Felt::ZERO && deep_seen.insert(h)
{
Some(h)
} else {
None
}
})
.collect();
let deep_block_map: std::collections::HashMap<_, _> = deeper_events
.iter()
.map(|e| (e.transaction_hash, e.block_number))
.collect();
let deep_decoded = decode_events_dedup(&deeper_events, &abi_c).await;
if is_contract && !deep_hashes.is_empty() {
let call_hashes: Vec<_> = deep_hashes
.iter()
.map(|h| (*h, deep_block_map.get(h).copied().unwrap_or(0)))
.collect();
let mut contract_calls_list = build_contract_calls_from_hashes(
address,
&call_hashes,
&ds_c,
pf_c.as_ref(),
&abi_c,
)
.await;
contract_calls_list
.sort_by(|a, b| b.block_number.cmp(&a.block_number));
persist_calls_merged(&ds_c, address, &contract_calls_list, None);
{
let callers = contract_calls_list.iter().map(|c| c.sender);
spawn_voyager_prefetch(callers, &voyager_c2, &tx_c);
}
let _ = tx_c.send(Action::AddressInfoLoaded {
info: crate::data::types::SnAddressInfo {
address,
nonce,
class_hash,
recent_events: deeper_events,
token_balances: Vec::new(),
},
decoded_events: deep_decoded,
tx_summaries: Vec::new(),
contract_calls: contract_calls_list,
});
} else if !is_contract && !deep_hashes.is_empty() {
let to_fetch: Vec<_> = {
let cached_txs = ds_c.load_cached_address_txs(&address);
let cached_set: std::collections::HashSet<_> =
cached_txs.iter().map(|t| t.hash).collect();
deep_hashes
.into_iter()
.filter(|h| !cached_set.contains(h))
.collect()
};
if !to_fetch.is_empty() {
let summaries = fetch_tx_summaries_from_hashes(
&to_fetch,
&deep_block_map,
&ds_c,
pf_c.as_ref(),
&abi_c,
&tx_c,
"RPC: fetching probe-guided txs",
)
.await;
if !summaries.is_empty() {
let _ = tx_c.send(Action::AddressTxsStreamed {
address,
source: Source::Rpc,
tx_summaries: summaries,
complete: false,
});
}
}
}
}
} else {
let _ = tx_c.send(Action::LoadingStatus(
"No activity found for this address".into(),
));
}
}
}
if any_events_found {
ds_c.save_search_progress(&address, filter_kind, from_block, latest_block);
}
debug!(
address = %format!("{:#x}", address),
source = ?Source::Rpc,
tx_summaries = 0,
"AddressTxsStreamed dispatching complete marker"
);
let _ = tx_c.send(Action::AddressTxsStreamed {
address,
source: Source::Rpc,
tx_summaries: Vec::new(),
complete: true,
});
info!(
address = %format!("{:#x}", address),
elapsed_ms = start.elapsed().as_millis(),
"RPC address info task complete"
);
});
}
}
pub(super) async fn enrich_address_txs(
address: starknet::core::types::Felt,
hashes: Vec<starknet::core::types::Felt>,
ds: &Arc<dyn DataSource>,
pf: Option<&Arc<crate::data::pathfinder::PathfinderClient>>,
abi_reg: &Arc<AbiRegistry>,
action_tx: &mpsc::UnboundedSender<Action>,
) {
if hashes.is_empty() {
return;
}
debug!(
count = hashes.len(),
"Enriching address txs (endpoints/timestamps)"
);
let mut updates: Vec<crate::data::types::AddressTxSummary> = Vec::new();
let mut missing: Vec<starknet::core::types::Felt> = hashes.clone();
if let Some(pf) = pf {
match pf.get_txs_by_hash(&hashes).await {
Ok(pf_rows) => {
debug!(
requested = hashes.len(),
returned = pf_rows.len(),
"enrich_address_txs: pf-query returned"
);
let mut target_addresses: std::collections::HashSet<starknet::core::types::Felt> =
std::collections::HashSet::new();
for row in &pf_rows {
for addr in helpers::pf_tx_target_addresses(row) {
target_addresses.insert(addr);
}
}
for addr in target_addresses {
let _ = abi_reg.get_abi_for_address(&addr).await;
}
let mut returned_hashes: std::collections::HashSet<starknet::core::types::Felt> =
std::collections::HashSet::new();
for row in &pf_rows {
if let Some(summary) = helpers::build_tx_summary_from_pf_data(row, abi_reg) {
returned_hashes.insert(summary.hash);
updates.push(summary);
}
}
missing.retain(|h| !returned_hashes.contains(h));
}
Err(e) => {
warn!(error = %e, "enrich_address_txs: pf-query failed, falling back to RPC for all hashes");
}
}
}
if !missing.is_empty() {
let futs: Vec<_> = missing
.iter()
.map(|hash| {
let ds_t = Arc::clone(ds);
let ds_r = Arc::clone(ds);
let h = *hash;
async move {
let _permit = ENRICH_RPC_SEMAPHORE.acquire().await.ok();
let (tx_r, rx_r) = tokio::join!(ds_t.get_transaction(h), ds_r.get_receipt(h));
(h, tx_r, rx_r)
}
})
.collect();
let results = futures::future::join_all(futs).await;
let mut target_addresses = std::collections::HashSet::new();
for (_hash, tx_result, _rx_result) in &results {
if let Ok(SnTransaction::Invoke(invoke)) = tx_result {
for call in parse_multicall(&invoke.calldata) {
target_addresses.insert(call.contract_address);
}
}
}
for addr in target_addresses {
let _ = abi_reg.get_abi_for_address(&addr).await;
}
for (hash, tx_result, rx_result) in results {
if let Ok(fetched_tx) = tx_result {
let receipt = rx_result.ok();
let block_num = receipt.as_ref().map(|r| r.block_number).unwrap_or(0);
updates.push(helpers::build_tx_summary(
hash,
&fetched_tx,
receipt.as_ref(),
block_num,
0,
abi_reg,
));
}
}
}
helpers::backfill_timestamps(&mut updates, ds, pf).await;
if !updates.is_empty() {
let _ =
action_tx.send(crate::app::actions::Action::AddressTxsEnriched { address, updates });
}
}
pub(super) async fn run_endpoint_enrichment(
address: starknet::core::types::Felt,
current_nonce: u64,
known_txs: Vec<crate::data::types::AddressTxSummary>,
ds: &Arc<dyn DataSource>,
dune: &Option<Arc<dune::DuneClient>>,
pf: &Option<Arc<crate::data::pathfinder::PathfinderClient>>,
abi_reg: &Arc<AbiRegistry>,
action_tx: &mpsc::UnboundedSender<Action>,
) {
if known_txs.is_empty() || current_nonce == 0 {
info!(
address = %format!("{:#x}", address),
txs = known_txs.len(),
current_nonce,
"Endpoint enrich: skipping nonce gaps (empty txs or nonce=0), running endpoint enrichment only"
);
enrich_all_empty_endpoints(address, &known_txs, ds, pf.as_ref(), abi_reg, action_tx).await;
return;
}
let min_nonce = known_txs.iter().map(|t| t.nonce).min().unwrap_or(0);
let max_nonce = known_txs.iter().map(|t| t.nonce).max().unwrap_or(0);
let empty_endpoints = known_txs
.iter()
.filter(|t| t.endpoint_names.is_empty())
.count();
info!(
address = %format!("{:#x}", address),
txs = known_txs.len(),
current_nonce,
min_nonce,
max_nonce,
empty_endpoints,
"Endpoint enrich: starting (nonce range {}..{}, {} txs, {} missing endpoints)",
min_nonce, max_nonce, known_txs.len(), empty_endpoints
);
let gap_txs = fill_small_nonce_gaps_phase(
address,
current_nonce,
&known_txs,
ds,
dune,
pf,
abi_reg,
action_tx,
)
.await;
if !gap_txs.is_empty() {
let gap_nonces: Vec<u64> = gap_txs.iter().map(|t| t.nonce).collect();
info!(
found = gap_txs.len(),
nonces = ?gap_nonces,
"Endpoint enrich: filled {} small nonce gaps, sending to UI",
gap_txs.len()
);
let _ = action_tx.send(Action::AddressTxsEnriched {
address,
updates: gap_txs.clone(),
});
} else {
info!("Endpoint enrich: no small nonce gaps to fill");
}
let mut all_txs = known_txs;
for gt in &gap_txs {
if !all_txs.iter().any(|t| t.hash == gt.hash) {
all_txs.push(gt.clone());
}
}
enrich_all_empty_endpoints(address, &all_txs, ds, pf.as_ref(), abi_reg, action_tx).await;
debug!(
address = %format!("{:#x}", address),
"Endpoint enrich complete"
);
}
pub(super) async fn run_nonce_gap_fill(
address: starknet::core::types::Felt,
known_txs: Vec<crate::data::types::AddressTxSummary>,
gap: crate::app::views::address_info::UnfilledGap,
ds: &Arc<dyn DataSource>,
dune: &Option<Arc<dune::DuneClient>>,
pf: &Option<Arc<crate::data::pathfinder::PathfinderClient>>,
abi_reg: &Arc<AbiRegistry>,
action_tx: &mpsc::UnboundedSender<Action>,
) {
info!(
address = %format!("{:#x}", address),
lo_nonce = gap.lo_nonce,
hi_nonce = gap.hi_nonce,
lo_block = gap.lo_block,
hi_block = gap.hi_block,
missing = gap.missing_count,
"Gap fill: filling large nonce gap on demand"
);
let chunk = crate::app::views::address_info::LARGE_GAP_FILL_CHUNK_TXS;
let _ = action_tx.send(Action::LoadingStatus(format!(
"Filling gap (up to {} txs of {})...",
chunk, gap.missing_count
)));
let found = fill_specific_large_gap(address, &known_txs, &gap, dune, pf, action_tx).await;
if !found.is_empty() {
info!(
found = found.len(),
"Gap fill: backend returned {} new txs, enriching endpoints",
found.len()
);
let _ = action_tx.send(Action::AddressTxsEnriched {
address,
updates: found.clone(),
});
let mut combined = known_txs;
for t in &found {
if !combined.iter().any(|k| k.hash == t.hash) {
combined.push(t.clone());
}
}
enrich_all_empty_endpoints(address, &combined, ds, pf.as_ref(), abi_reg, action_tx).await;
} else {
info!("Gap fill: no txs returned for this range");
}
let _ = action_tx.send(Action::LoadingStatus(String::new()));
debug!(
address = %format!("{:#x}", address),
"Gap fill complete"
);
}
struct CallGapFillGuard {
action_tx: mpsc::UnboundedSender<Action>,
address: starknet::core::types::Felt,
lo_block: u64,
}
impl Drop for CallGapFillGuard {
fn drop(&mut self) {
let _ = self.action_tx.send(Action::AddressCallGapFillFinished {
address: self.address,
lo_block: self.lo_block,
});
}
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn run_call_gap_fill(
address: starknet::core::types::Felt,
gap: crate::app::views::address_info::UnfilledCallGap,
is_contract: bool,
ds: &Arc<dyn DataSource>,
dune: &Option<Arc<dune::DuneClient>>,
pf: &Option<Arc<crate::data::pathfinder::PathfinderClient>>,
abi_reg: &Arc<AbiRegistry>,
action_tx: &mpsc::UnboundedSender<Action>,
) {
const CALL_GAP_FILL_LIMIT: u32 = 500;
let _guard = QueryGuard::new(
action_tx,
format!("callgap:{}", query_addr_prefix(&address)),
"Calls gap fill".to_string(),
);
let _finish_guard = CallGapFillGuard {
action_tx: action_tx.clone(),
address,
lo_block: gap.lo_block,
};
let _ = action_tx.send(Action::LoadingStatus(format!(
"Filling calls gap (blocks {}..{})...",
gap.lo_block, gap.hi_block
)));
info!(
address = %format!("{:#x}", address),
lo_block = gap.lo_block,
hi_block = gap.hi_block,
is_contract,
"Calls gap fill: backfilling block range on demand"
);
let page = CallsPage {
from: Some(gap.lo_block),
to: Some(gap.hi_block),
limit: CALL_GAP_FILL_LIMIT,
track: false,
};
let (calls, covered): (
Vec<crate::data::types::ContractCallSummary>,
Option<(u64, u64)>,
) = if is_contract && let Some(dune_client) = dune {
let min_date = block_date_floor(ds, gap.hi_block).await;
match dune_client
.query_contract_calls_windowed(
address,
gap.lo_block,
gap.hi_block,
CALL_GAP_FILL_LIMIT,
min_date,
)
.await
{
Ok(rows) => {
let blocks: Vec<u64> = rows.iter().map(|c| c.block_number).collect();
let covered = calls_page_coverage(page, &blocks);
let enriched =
enrich_dune_calls(address, rows, abi_reg, ds, pf.as_ref(), action_tx).await;
(enriched, covered)
}
Err(e) => {
warn!(addr = %format!("{:#x}", address), error = %e, "Calls gap fill: Dune query failed");
let _ = action_tx.send(Action::LoadingStatus(String::new()));
return;
}
}
} else if let Some(pf_client) = pf {
let kind = if is_contract {
EventQueryKind::Contract
} else {
EventQueryKind::Account
};
let Some(latest_block) = ds.latest_block_hint().await else {
warn!(addr = %format!("{:#x}", address), "Calls gap fill: no chain head available");
let _ = action_tx.send(Action::LoadingStatus(String::new()));
return;
};
match crate::network::event_window::ensure_address_events_window(
address,
kind,
crate::network::event_window::EventWindowPolicy::FillGap {
from_block: gap.lo_block,
to_block: gap.hi_block,
},
Some(pf_client),
ds,
latest_block,
gap.lo_block,
)
.await
{
Ok(outcome) => {
let covered = outcome
.next_token
.is_none()
.then_some((gap.lo_block, gap.hi_block));
let _ = action_tx.send(Action::AddressEventWindowUpdated {
address,
min_searched: outcome.min_searched,
max_searched: outcome.max_searched,
deferred_gap: outcome.deferred_gap,
});
let calls =
build_contract_calls_from_pf_rows(address, &outcome.page.tx_rows, abi_reg)
.await;
(calls, covered)
}
Err(e) => {
warn!(addr = %format!("{:#x}", address), error = %e, "Calls gap fill: event-window scan failed");
let _ = action_tx.send(Action::LoadingStatus(String::new()));
return;
}
}
} else {
debug!(addr = %format!("{:#x}", address), "Calls gap fill: no Dune/pf backend available");
let _ = action_tx.send(Action::LoadingStatus(String::new()));
return;
};
info!(
address = %format!("{:#x}", address),
lo_block = gap.lo_block,
hi_block = gap.hi_block,
calls = calls.len(),
?covered,
"Calls gap fill: backend returned rows"
);
if !calls.is_empty() {
let _ = action_tx.send(Action::AddressCallsMerged { address, calls });
}
if let Some(range) = covered {
record_calls_coverage(ds, action_tx, address, page, range);
}
let _ = action_tx.send(Action::LoadingStatus(String::new()));
}
async fn fill_small_nonce_gaps_phase(
address: starknet::core::types::Felt,
current_nonce: u64,
known_txs: &[crate::data::types::AddressTxSummary],
ds: &Arc<dyn DataSource>,
_dune: &Option<Arc<dune::DuneClient>>,
pf: &Option<Arc<crate::data::pathfinder::PathfinderClient>>,
abi_reg: &Arc<AbiRegistry>,
action_tx: &mpsc::UnboundedSender<Action>,
) -> Vec<crate::data::types::AddressTxSummary> {
let known_nonces: std::collections::HashMap<u64, u64> = known_txs
.iter()
.filter(|t| t.block_number > 0)
.map(|t| (t.nonce, t.block_number))
.collect();
let min_known = known_txs.iter().map(|t| t.nonce).min().unwrap_or(0);
let max_known = known_txs.iter().map(|t| t.nonce).max().unwrap_or(0);
let check_up_to = current_nonce.min(max_known + 20);
info!(
min_known,
max_known,
current_nonce,
check_up_to,
known_nonce_count = known_nonces.len(),
"Sanity gap check: scanning nonces {}..{} (current_nonce={}, known={})",
min_known,
check_up_to,
current_nonce,
known_nonces.len()
);
use crate::app::views::address_info::SMALL_GAP_SPAN_BLOCKS;
let mut small_gaps: Vec<(u64, u64, u64)> = Vec::new();
for nonce in min_known..check_up_to {
if known_nonces.contains_key(&nonce) {
continue;
}
let block_before = known_nonces
.iter()
.filter(|(n, _)| **n < nonce)
.max_by_key(|(n, _)| *n)
.map(|(_, b)| *b)
.unwrap_or(0);
let block_after = known_nonces
.iter()
.filter(|(n, _)| **n > nonce)
.min_by_key(|(n, _)| *n)
.map(|(_, b)| *b)
.unwrap_or(0);
if block_before == 0 && block_after == 0 {
continue;
}
let scan_from = if block_before > 0 {
block_before
} else {
block_after.saturating_sub(10)
};
let scan_to = if block_after > 0 {
block_after
} else {
block_before + 10
};
if scan_to.saturating_sub(scan_from) <= SMALL_GAP_SPAN_BLOCKS {
small_gaps.push((nonce, scan_from, scan_to));
}
}
let small_count = small_gaps.len();
if small_count == 0 {
info!(
"Sanity gap check: no small nonce gaps in range {}..{}",
min_known, check_up_to
);
return Vec::new();
}
let small_nonces: Vec<u64> = small_gaps.iter().map(|(n, _, _)| *n).take(10).collect();
info!(
small = small_count,
first_small_nonces = ?small_nonces,
"Sanity check: filling {} small nonce gaps (wider gaps deferred to on-demand fill)",
small_count
);
let _ = action_tx.send(Action::LoadingStatus(format!(
"Filling {} small nonce gaps...",
small_count
)));
let mut found_txs = Vec::new();
if small_gaps.is_empty() {
return found_txs;
}
let bounding_from = small_gaps.iter().map(|(_, f, _)| *f).min().unwrap_or(0);
let bounding_to = small_gaps.iter().map(|(_, _, t)| *t).max().unwrap_or(0);
if let Some(pf_c) = pf.as_ref()
&& bounding_to >= bounding_from
{
info!(
small = small_count,
from = bounding_from,
to = bounding_to,
"Sanity gap-fill: querying PF for blocks {}..{}",
bounding_from,
bounding_to
);
match pf_c
.get_sender_txs(
address,
1000,
Some(bounding_to.saturating_add(1)),
Some(bounding_from),
)
.await
{
Ok(entries) => {
let summaries = pf_txs_to_summaries(entries);
let new: Vec<_> = summaries
.into_iter()
.filter(|t| !known_txs.iter().any(|k| k.hash == t.hash))
.collect();
info!(
found = new.len(),
"Sanity gap-fill: PF returned {} new txs",
new.len()
);
return new;
}
Err(e) => {
warn!(error = %e, "Sanity gap-fill: PF query failed, falling back to RPC scan");
}
}
}
let mut blocks_to_scan: std::collections::BTreeSet<u64> = std::collections::BTreeSet::new();
for (_, from, to) in &small_gaps {
for b in *from..=*to {
blocks_to_scan.insert(b);
}
}
if blocks_to_scan.len() <= 200 {
let blocks_vec: Vec<u64> = blocks_to_scan.into_iter().collect();
info!(
blocks = blocks_vec.len(),
"Sanity gap-fill: scanning {} blocks via RPC for small gaps",
blocks_vec.len()
);
let rpc_found =
fetch_txs_from_blocks(address, &blocks_vec, known_txs, ds, abi_reg, action_tx).await;
info!(
found = rpc_found.len(),
"Sanity gap-fill: RPC scan found {} txs",
rpc_found.len()
);
found_txs.extend(rpc_found);
} else {
info!(
blocks = blocks_to_scan.len(),
"Sanity gap-fill: too many small-gap blocks ({}), skipping RPC scan",
blocks_to_scan.len()
);
}
found_txs
}
async fn fill_specific_large_gap(
address: starknet::core::types::Felt,
known_txs: &[crate::data::types::AddressTxSummary],
gap: &crate::app::views::address_info::UnfilledGap,
dune: &Option<Arc<dune::DuneClient>>,
pf: &Option<Arc<crate::data::pathfinder::PathfinderClient>>,
action_tx: &mpsc::UnboundedSender<Action>,
) -> Vec<crate::data::types::AddressTxSummary> {
let from = gap.lo_block;
let to = gap.hi_block;
let chunk = crate::app::views::address_info::LARGE_GAP_FILL_CHUNK_TXS;
let known_hashes: std::collections::HashSet<_> = known_txs.iter().map(|t| t.hash).collect();
if let Some(pf_c) = pf.as_ref() {
info!(
from,
to,
span = to.saturating_sub(from),
chunk,
"Gap fill: querying Pathfinder for blocks {}..{} (limit {})",
from,
to,
chunk
);
match pf_c
.get_sender_txs(address, chunk, Some(to.saturating_add(1)), Some(from))
.await
{
Ok(entries) => {
let summaries = pf_txs_to_summaries(entries);
let total_returned = summaries.len();
let new: Vec<_> = summaries
.into_iter()
.filter(|t| !known_hashes.contains(&t.hash))
.collect();
info!(
returned = total_returned,
new = new.len(),
from,
to,
"Gap fill: PF returned {} txs, {} new for blocks {}..{}",
total_returned,
new.len(),
from,
to
);
return new;
}
Err(e) => {
warn!(error = %e, from, to, "Gap fill: PF query failed, falling back to Dune");
}
}
}
let Some(dune_c) = dune.as_ref() else {
warn!(
"Gap fill: neither Pathfinder nor Dune available, cannot fill gap {}..{}",
gap.lo_block, gap.hi_block
);
let _ = action_tx.send(Action::LoadingStatus(
"Gap fill unavailable (no backend configured)".to_string(),
));
return Vec::new();
};
info!(
from,
to,
span = to.saturating_sub(from),
chunk,
"Gap fill: querying Dune for blocks {}..{} (limit {})",
from,
to,
chunk
);
let gap_min_date = known_txs
.iter()
.filter(|t| t.block_number <= from && t.timestamp > 0)
.max_by_key(|t| t.block_number)
.and_then(|t| ts_to_min_date(t.timestamp));
match dune_c
.query_account_txs_windowed(address, from, to, chunk, gap_min_date)
.await
{
Ok(dune_txs) => {
let total_returned = dune_txs.len();
let new: Vec<_> = dune_txs
.into_iter()
.filter(|t| !known_hashes.contains(&t.hash))
.collect();
info!(
returned = total_returned,
new = new.len(),
from,
to,
"Gap fill: Dune returned {} txs, {} new for blocks {}..{}",
total_returned,
new.len(),
from,
to
);
new
}
Err(e) => {
warn!(error = %e, from, to, "Gap fill: Dune query failed for blocks {}..{}", from, to);
Vec::new()
}
}
}
async fn enrich_all_empty_endpoints(
address: starknet::core::types::Felt,
all_txs: &[crate::data::types::AddressTxSummary],
ds: &Arc<dyn DataSource>,
pf: Option<&Arc<crate::data::pathfinder::PathfinderClient>>,
abi_reg: &Arc<AbiRegistry>,
action_tx: &mpsc::UnboundedSender<Action>,
) {
const ENRICH_BUFFER: usize = 200;
let total_invoke = all_txs.iter().filter(|t| t.tx_type == "INVOKE").count();
let missing: Vec<starknet::core::types::Felt> = all_txs
.iter()
.filter(|t| t.endpoint_names.is_empty() && t.tx_type == "INVOKE")
.take(ENRICH_BUFFER)
.map(|t| t.hash)
.collect();
if missing.is_empty() {
info!(
total_invoke,
"Sanity check endpoints: all {} INVOKE txs already have endpoints", total_invoke
);
return;
}
info!(
enriching = missing.len(),
total_invoke,
buffer = ENRICH_BUFFER,
"Sanity check endpoints: enriching top {} missing (viewport buffer), remainder on-demand",
missing.len()
);
use futures::stream::StreamExt;
const MAX_PARALLEL_CHUNKS: usize = 3;
let total_batches = missing.len().div_ceil(20);
let chunks: Vec<_> = missing
.chunks(20)
.enumerate()
.map(|(i, c)| (i, c.to_vec()))
.collect();
futures::stream::iter(chunks)
.map(|(i, chunk)| async move {
info!(
batch = i + 1,
size = chunk.len(),
"Sanity check endpoints: enriching batch {}/{}",
i + 1,
total_batches
);
enrich_address_txs(address, chunk, ds, pf, abi_reg, action_tx).await;
})
.buffer_unordered(MAX_PARALLEL_CHUNKS)
.collect::<Vec<_>>()
.await;
}
pub(super) async fn fetch_txs_from_blocks(
address: starknet::core::types::Felt,
blocks: &[u64],
known_txs: &[crate::data::types::AddressTxSummary],
ds: &Arc<dyn DataSource>,
abi_reg: &Arc<AbiRegistry>,
status_tx: &mpsc::UnboundedSender<Action>,
) -> Vec<crate::data::types::AddressTxSummary> {
use futures::stream::StreamExt;
let mut found_txs: Vec<crate::data::types::AddressTxSummary> = Vec::new();
let total_chunks = blocks.chunks(10).count();
for (chunk_idx, chunk) in blocks.chunks(10).enumerate() {
if total_chunks > 1 {
let fetched = chunk_idx * 10 + 1;
let _ = status_tx.send(Action::LoadingStatus(format!(
"Fetching blocks from RPC {}/{}...",
fetched.min(blocks.len()),
blocks.len()
)));
}
let futs: Vec<_> = chunk
.iter()
.map(|bn| {
let ds_b = Arc::clone(ds);
let b = *bn;
async move { (b, ds_b.get_block_with_txs(b).await) }
})
.collect();
let results = futures::future::join_all(futs).await;
let mut pending: Vec<(
starknet::core::types::Felt,
crate::data::types::SnTransaction,
u64,
u64,
)> = Vec::new();
for (block_num, result) in results {
if let Ok((block, txs)) = result {
for btx in txs.into_iter() {
if btx.sender() != address {
continue;
}
let hash = btx.hash();
if known_txs.iter().any(|t| t.hash == hash) {
continue;
}
pending.push((hash, btx, block_num, block.timestamp));
}
}
}
if pending.is_empty() {
continue;
}
let hashes: Vec<starknet::core::types::Felt> =
pending.iter().map(|(h, _, _, _)| *h).collect();
let receipt_futs: Vec<_> = hashes
.into_iter()
.map(|h| {
let ds_r = Arc::clone(ds);
async move { (h, ds_r.get_receipt(h).await.ok()) }
})
.collect();
let receipts: std::collections::HashMap<
starknet::core::types::Felt,
Option<crate::data::types::SnReceipt>,
> = futures::stream::iter(receipt_futs)
.buffer_unordered(8)
.collect::<Vec<_>>()
.await
.into_iter()
.collect();
for (hash, btx, block_num, block_ts) in pending {
let receipt = receipts.get(&hash).and_then(|r| r.clone());
found_txs.push(helpers::build_tx_summary(
hash,
&btx,
receipt.as_ref(),
block_num,
block_ts,
abi_reg,
));
}
}
found_txs
}
pub(super) async fn fetch_more_address_txs(
address: starknet::core::types::Felt,
before_block: u64,
is_contract: bool,
ds: &Arc<dyn crate::data::DataSource>,
dune: &Option<Arc<dune::DuneClient>>,
pf: &Option<Arc<crate::data::pathfinder::PathfinderClient>>,
abi_reg: &Arc<AbiRegistry>,
tx: &mpsc::UnboundedSender<Action>,
) {
use crate::data::types::ContractCallSummary;
let start = std::time::Instant::now();
debug!(address = %format!("{:#x}", address), before_block, is_contract, "Fetching more address txs");
let cached = ds.load_cached_activity_range(&address);
let window_size = if let Some((min_b, max_b)) = cached {
let p = dune::AddressActivityProbe {
sender_min_block: min_b,
sender_max_block: max_b,
callee_min_block: min_b,
callee_max_block: max_b,
sender_tx_count: 1,
callee_call_count: 1,
};
p.recommended_window()
} else {
50_000u64
};
let deploy_block = ds
.load_cached_deploy_info(&address)
.map(|(_, block, _)| block);
if let Some(db) = deploy_block
&& before_block <= db
{
debug!(address = %format!("{:#x}", address), deploy_block = db, "Already at deploy block, no more txs to fetch");
let _ = tx.send(Action::MoreAddressTxsLoaded {
address,
tx_summaries: Vec::new(),
contract_calls: Vec::new(),
oldest_block: db,
has_more: false,
});
return;
}
let mut from_block = before_block.saturating_sub(window_size);
if let Some(db) = deploy_block {
from_block = from_block.max(db);
}
if from_block == 0 && before_block <= 1 {
let _ = tx.send(Action::MoreAddressTxsLoaded {
address,
tx_summaries: Vec::new(),
contract_calls: Vec::new(),
oldest_block: 0,
has_more: false,
});
return;
}
let _ = tx.send(Action::LoadingStatus(format!(
"Loading more: blocks {}..{}...",
from_block,
before_block.saturating_sub(1)
)));
let rpc_ds = Arc::clone(ds);
let pf_c = pf.as_ref().map(Arc::clone);
let rpc_addr = address;
let rpc_is_contract = is_contract;
let (events_from, events_to) = if rpc_is_contract {
(
before_block.saturating_sub(window_size),
before_block.saturating_sub(1),
)
} else {
(
before_block.saturating_sub(window_size.min(10_000)),
before_block.saturating_sub(1),
)
};
let rpc_fut = async move {
let kind = if rpc_is_contract {
EventQueryKind::Contract
} else {
EventQueryKind::Account
};
fetch_events_routed(
kind,
pf_c.as_ref(),
&rpc_ds,
rpc_addr,
Some(events_from),
Some(events_to),
500,
)
.await
.unwrap_or_default()
};
let pf_source = pf.as_ref().map(Arc::clone);
let dune_source = dune.as_ref().map(Arc::clone);
let txs_source_fut = async move {
let dune_to = before_block.saturating_sub(1);
if is_contract {
let Some(dune_client) = dune_source else {
return (Vec::new(), Vec::new());
};
return match dune_client
.query_contract_calls_windowed(address, from_block, dune_to, 100, None)
.await
{
Ok(calls) => (Vec::new(), calls),
Err(e) => {
warn!(error = %e, "Dune pagination contract calls failed");
(Vec::new(), Vec::new())
}
};
}
if let Some(pf_client) = pf_source {
match pf_client
.get_sender_txs(address, 200, Some(before_block), Some(from_block))
.await
{
Ok(entries) => return (pf_txs_to_summaries(entries), Vec::new()),
Err(e) => {
warn!(error = %e, "PF pagination sender-txs failed; falling back to Dune");
}
}
}
let Some(dune_client) = dune_source else {
return (Vec::new(), Vec::new());
};
let from_min_date = block_date_floor(ds, from_block).await;
match dune_client
.query_account_txs_windowed(address, from_block, dune_to, 100, from_min_date)
.await
{
Ok(txs) => (txs, Vec::new()),
Err(e) => {
warn!(error = %e, "Dune pagination account txs failed");
(Vec::new(), Vec::new())
}
}
};
let (events, (account_txs, dune_calls)) = tokio::join!(rpc_fut, txs_source_fut);
let mut seen = std::collections::HashSet::new();
let unique_hashes: Vec<starknet::core::types::Felt> = events
.iter()
.filter_map(|e| {
let h = e.transaction_hash;
if h != starknet::core::types::Felt::ZERO && seen.insert(h) {
Some(h)
} else {
None
}
})
.collect();
let tx_block_map: std::collections::HashMap<_, _> = events
.iter()
.map(|e| (e.transaction_hash, e.block_number))
.collect();
let mut summaries = Vec::new();
let mut rpc_calls: Vec<ContractCallSummary> = Vec::new();
if is_contract && !unique_hashes.is_empty() {
let call_hashes: Vec<_> = unique_hashes
.iter()
.map(|h| (*h, *tx_block_map.get(h).unwrap_or(&0)))
.collect();
rpc_calls =
build_contract_calls_from_hashes(address, &call_hashes, ds, pf.as_ref(), abi_reg).await;
} else if !unique_hashes.is_empty() {
for chunk in unique_hashes.chunks(20) {
let futs: Vec<_> = chunk
.iter()
.map(|hash| {
let ds_tx = Arc::clone(ds);
let ds_rx = Arc::clone(ds);
let h = *hash;
async move {
let (tx_r, rx_r) =
tokio::join!(ds_tx.get_transaction(h), ds_rx.get_receipt(h));
(h, tx_r, rx_r)
}
})
.collect();
let results = futures::future::join_all(futs).await;
for (hash, tx_result, rx_result) in results {
if let Ok(fetched_tx) = tx_result {
let receipt = rx_result.ok();
let block_num = receipt
.as_ref()
.map(|r| r.block_number)
.unwrap_or_else(|| tx_block_map.get(&hash).copied().unwrap_or(0));
summaries.push(helpers::build_tx_summary(
hash,
&fetched_tx,
receipt.as_ref(),
block_num,
0,
abi_reg,
));
}
}
}
helpers::backfill_timestamps(&mut summaries, ds, pf.as_ref()).await;
}
if !account_txs.is_empty() {
let existing: std::collections::HashSet<_> = summaries.iter().map(|s| s.hash).collect();
for atx in account_txs {
if !existing.contains(&atx.hash) {
summaries.push(atx);
}
}
}
summaries.sort_by(|a, b| b.nonce.cmp(&a.nonce));
let dune_calls = if !dune_calls.is_empty() {
enrich_dune_calls(address, dune_calls, abi_reg, ds, pf.as_ref(), tx).await
} else {
Vec::new()
};
let mut all_calls = rpc_calls;
if !dune_calls.is_empty() {
let existing: std::collections::HashSet<_> = all_calls.iter().map(|c| c.tx_hash).collect();
for dc in dune_calls {
if !existing.contains(&dc.tx_hash) {
all_calls.push(dc);
}
}
}
all_calls.sort_by(|a, b| b.block_number.cmp(&a.block_number));
let oldest_block = summaries
.iter()
.filter(|s| s.block_number > 0)
.map(|s| s.block_number)
.chain(all_calls.iter().map(|c| c.block_number))
.min()
.unwrap_or(from_block);
let at_deploy_floor = deploy_block.is_some_and(|db| oldest_block <= db);
let has_more = from_block > 0
&& !at_deploy_floor
&& (summaries.len() >= 50
|| all_calls.len() >= 50
|| cached.is_some_and(|(min_b, _)| min_b < from_block));
info!(
address = %format!("{:#x}", address),
new_txs = summaries.len(),
new_calls = all_calls.len(),
oldest_block,
has_more,
elapsed_ms = start.elapsed().as_millis(),
"More address txs fetched"
);
if !summaries.is_empty() {
ds.save_address_txs(&address, &summaries);
}
persist_calls_merged(ds, address, &all_calls, None);
let _ = tx.send(Action::MoreAddressTxsLoaded {
address,
tx_summaries: summaries,
contract_calls: all_calls,
oldest_block,
has_more,
});
}
pub(super) async fn enrich_dune_calls(
address: starknet::core::types::Felt,
mut dune_calls: Vec<crate::data::types::ContractCallSummary>,
abi_reg: &Arc<AbiRegistry>,
ds: &Arc<dyn DataSource>,
pf: Option<&Arc<crate::data::pathfinder::PathfinderClient>>,
tx: &mpsc::UnboundedSender<Action>,
) -> Vec<crate::data::types::ContractCallSummary> {
let mut unresolved = false;
for call in &mut dune_calls {
if call.function_name.starts_with("0x")
&& let Ok(sel) = starknet::core::types::Felt::from_hex(&call.function_name)
{
if let Some(name) = abi_reg.get_selector_name(&sel) {
call.function_name = name;
} else {
unresolved = true;
}
}
}
if unresolved && let Some(abi) = abi_reg.get_abi_for_address(&address).await {
for call in &mut dune_calls {
if call.function_name.starts_with("0x")
&& let Ok(sel) = starknet::core::types::Felt::from_hex(&call.function_name)
&& let Some(func) = abi.get_function(&sel)
{
call.function_name = func.name.clone();
}
}
}
dune_calls = crate::data::types::deduplicate_contract_calls(dune_calls);
let _ = tx.send(Action::LoadingStatus(format!(
"Fetching fees for {} calls...",
dune_calls.len()
)));
for chunk in dune_calls.chunks_mut(20) {
let futs: Vec<_> = chunk
.iter()
.map(|c| {
let ds_t = Arc::clone(ds);
let ds_r = Arc::clone(ds);
let h = c.tx_hash;
async move {
let (tx_r, rx_r) = tokio::join!(ds_t.get_transaction(h), ds_r.get_receipt(h));
(h, tx_r, rx_r)
}
})
.collect();
let results = futures::future::join_all(futs).await;
for (hash, tx_result, rx_result) in results {
if let Some(call) = chunk.iter_mut().find(|c| c.tx_hash == hash) {
if let Ok(ref fetched_tx) = tx_result {
call.sender = fetched_tx.sender();
let (nonce, tip) = helpers::extract_nonce_tip(fetched_tx);
call.nonce = Some(nonce);
call.tip = tip;
if call.inner_targets.is_empty()
&& let crate::data::types::SnTransaction::Invoke(i) = fetched_tx
{
let calls = parse_multicall(&i.calldata);
call.inner_targets = helpers::oe_inner_targets(&calls, abi_reg);
}
}
if let Ok(receipt) = rx_result {
call.total_fee_fri = felt_to_u128(&receipt.actual_fee);
call.status = match &receipt.execution_status {
crate::data::types::ExecutionStatus::Succeeded => "OK".into(),
crate::data::types::ExecutionStatus::Reverted(_) => "REV".into(),
_ => call.status.clone(),
};
}
}
}
}
helpers::backfill_call_timestamps(&mut dune_calls, ds, pf).await;
dune_calls.sort_by(|a, b| b.block_number.cmp(&a.block_number));
dune_calls
}
pub(super) async fn find_deploy_tx(
addr: starknet::core::types::Felt,
deploy_block: u64,
ds: &Arc<dyn DataSource>,
tx: &mpsc::UnboundedSender<Action>,
) {
if let Some((cached_hash, cached_block, cached_deployer)) = ds.load_cached_deploy_info(&addr) {
debug!(%addr, cached_block, "Deploy tx found in cache");
let summary = crate::data::types::AddressTxSummary {
hash: cached_hash,
nonce: 0,
block_number: cached_block,
timestamp: 0,
endpoint_names: String::new(),
total_fee_fri: 0,
tip: 0,
tx_type: "DEPLOY".into(),
status: "OK".into(),
sender: cached_deployer,
called_contracts: Vec::new(),
};
let _ = tx.send(Action::AddressTxsStreamed {
address: addr,
source: Source::Pathfinder,
tx_summaries: vec![summary],
complete: false,
});
return;
}
info!(%addr, deploy_block, "Looking for deploy tx");
let txs = match ds.get_block_with_txs(deploy_block).await {
Ok((_block, txs)) => txs,
Err(e) => {
warn!(error = %e, "Failed to fetch block for deploy tx lookup");
return;
}
};
info!(tx_count = txs.len(), "Block fetched for deploy tx lookup");
for t in &txs {
let (is_deploy, sender) = match t {
SnTransaction::DeployAccount(da) if da.contract_address == addr => {
(true, Some(da.contract_address))
}
SnTransaction::Deploy(d) if d.contract_address == addr => {
(true, Some(d.contract_address))
}
_ => (false, None),
};
if is_deploy {
ds.save_deploy_info(&addr, &t.hash(), deploy_block, sender.as_ref());
let summary = crate::data::types::AddressTxSummary {
hash: t.hash(),
nonce: 0,
block_number: deploy_block,
timestamp: 0,
endpoint_names: String::new(),
total_fee_fri: t.actual_fee().map(|f| felt_to_u128(&f)).unwrap_or(0),
tip: 0,
tx_type: t.type_name().to_string(),
status: "OK".into(),
sender,
called_contracts: Vec::new(),
};
let _ = tx.send(Action::AddressTxsStreamed {
address: addr,
source: Source::Pathfinder,
tx_summaries: vec![summary],
complete: false,
});
return;
}
}
let udc_selector = starknet::core::types::Felt::from_hex(UDC_CONTRACT_DEPLOYED).unwrap();
let invoke_count = txs
.iter()
.filter(|t| matches!(t, SnTransaction::Invoke(_)))
.count();
info!(invoke_count, "Scanning INVOKE txs for UDC event");
for t in &txs {
if !matches!(t, SnTransaction::Invoke(_)) {
continue;
}
let receipt = match ds.get_receipt(t.hash()).await {
Ok(r) => r,
Err(e) => {
warn!(error = %e, tx_hash = %t.hash(), "Failed to fetch receipt");
continue;
}
};
for event in &receipt.events {
if event.keys.first() == Some(&udc_selector) {
let deployed = event.data.first().copied().unwrap_or_default();
info!(
tx_hash = %t.hash(),
deployed_addr = %format!("{:#x}", deployed),
looking_for = %format!("{:#x}", addr),
match_result = (deployed == addr),
"Found UDC ContractDeployed event"
);
}
if event.keys.first() == Some(&udc_selector) && event.data.first() == Some(&addr) {
let deployer = event.data.get(1).copied();
ds.save_deploy_info(&addr, &t.hash(), deploy_block, deployer.as_ref());
let summary = crate::data::types::AddressTxSummary {
hash: t.hash(),
nonce: 0,
block_number: deploy_block,
timestamp: 0,
endpoint_names: String::new(),
total_fee_fri: felt_to_u128(&receipt.actual_fee),
tip: 0,
tx_type: "DEPLOY (UDC)".into(),
status: "OK".into(),
sender: deployer,
called_contracts: Vec::new(),
};
let _ = tx.send(Action::AddressTxsStreamed {
address: addr,
source: Source::Pathfinder,
tx_summaries: vec![summary],
complete: false,
});
return;
}
}
}
debug!(%addr, deploy_block, "Deploy tx not found in block (neither native nor UDC)");
}
pub(super) struct ClassifiedPage {
pub txs: Vec<crate::data::types::AddressTxSummary>,
pub calls: Vec<crate::data::types::ContractCallSummary>,
pub meta_txs: Vec<crate::data::types::MetaTxIntenderSummary>,
}
pub(super) async fn classify_activity_page(
address: starknet::core::types::Felt,
page: &AddressActivityPage,
abi_reg: &Arc<AbiRegistry>,
) -> ClassifiedPage {
use starknet::core::types::Felt;
helpers::prewarm_abis([address], abi_reg).await;
let sender_rows: Vec<_> = page
.tx_rows
.iter()
.filter(|r| {
Felt::from_hex(&r.sender)
.map(|s| s == address)
.unwrap_or(false)
})
.cloned()
.collect();
let txs = if sender_rows.is_empty() {
Vec::new()
} else {
build_tx_summaries_from_pf_rows(&sender_rows, abi_reg).await
};
let calls = build_contract_calls_from_pf_rows(address, &page.tx_rows, abi_reg).await;
let meta_txs = derive_meta_txs_from_page(address, page, abi_reg).await;
ClassifiedPage {
txs,
calls,
meta_txs,
}
}
pub(super) async fn derive_meta_txs_from_page(
address: starknet::core::types::Felt,
page: &AddressActivityPage,
abi_reg: &Arc<AbiRegistry>,
) -> Vec<crate::data::types::MetaTxIntenderSummary> {
use starknet::core::types::Felt;
use crate::data::types::MetaTxIntenderSummary;
helpers::prewarm_abis([address], abi_reg).await;
let mut tx_index_by_hash: std::collections::HashMap<Felt, u64> =
std::collections::HashMap::new();
for e in &page.events {
tx_index_by_hash
.entry(e.transaction_hash)
.and_modify(|v| {
if e.event_index < *v {
*v = e.event_index;
}
})
.or_insert(e.event_index);
}
let mut summaries: Vec<MetaTxIntenderSummary> = Vec::with_capacity(page.tx_rows.len());
for row in &page.tx_rows {
if let Some(mut s) = classify_meta_tx_candidate(address, row, abi_reg).await {
if let Some(idx) = tx_index_by_hash.get(&s.hash).copied() {
s.tx_index = idx;
}
summaries.push(s);
}
}
sort_meta_txs_recency(&mut summaries);
summaries
}
pub(super) async fn fetch_address_meta_txs(
address: starknet::core::types::Felt,
from_block: u64,
continuation_token: Option<u64>,
window_size: u64,
_limit: u32,
ds: &Arc<dyn crate::data::DataSource>,
dune: Option<&Arc<dune::DuneClient>>,
pf: &Arc<crate::data::pathfinder::PathfinderClient>,
abi_reg: &Arc<AbiRegistry>,
action_tx: &mpsc::UnboundedSender<Action>,
cancel: CancellationToken,
) {
use crate::network::event_window::{
EXTEND_DOWN_INITIAL_WINDOW, EventWindowPolicy, ensure_address_events_window,
};
let _guard = QueryGuard::new(
action_tx,
format!("meta:{}", query_addr_prefix(&address)),
"MetaTxs scan".to_string(),
);
let send_empty = || {
let _ = action_tx.send(Action::AddressMetaTxsLoaded {
address,
summaries: Vec::new(),
next_token: None,
next_window_size: None,
});
};
let Some(latest_block) = ds.latest_block_hint().await else {
warn!(addr = %format!("{:#x}", address), "MetaTxs: no chain head available");
send_empty();
return;
};
let effective_window = if window_size == 0 {
EXTEND_DOWN_INITIAL_WINDOW
} else {
window_size
};
let policy = match continuation_token {
None => EventWindowPolicy::TopDelta,
Some(_) => EventWindowPolicy::ExtendDown {
window_size: effective_window,
},
};
let ds_dyn: Arc<dyn crate::data::DataSource> = ds.clone();
let account_fut = ensure_address_events_window(
address,
EventQueryKind::Account,
policy,
Some(pf),
&ds_dyn,
latest_block,
from_block,
);
let private_fut = discover_private_meta_txs(
address,
from_block,
continuation_token,
window_size,
&ds_dyn,
pf,
abi_reg,
);
if continuation_token.is_none() {
if let Some(d) = dune {
let d = d.clone();
let pf = pf.clone();
let abi_reg = abi_reg.clone();
let ds_spawn = ds_dyn.clone();
let tx_spawn = action_tx.clone();
let cancel = cancel.clone();
tokio::spawn(async move {
tokio::select! {
_ = emit_recent_meta_txs_via_dune(
address, &d, &pf, &abi_reg, &ds_spawn, &tx_spawn,
) => {}
_ = cancel.cancelled() => {
debug!(
addr = %format!("{:#x}", address),
"MetaTxs: Dune fast-paint cancelled on navigation"
);
}
}
});
}
}
let (outcome_res, private_outcome) = tokio::join!(account_fut, private_fut);
let PrivateDiscoveryOutcome {
summaries: mut private_summaries,
pool_min_searched,
pool_suggested_window,
pool_floor,
} = private_outcome;
let pool_has_more = matches!(
(pool_min_searched, pool_floor),
(Some(m), Some(f)) if m > f
);
let outcome = match outcome_res {
Ok(o) => o,
Err(e) => {
warn!(addr = %format!("{:#x}", address), error = %e, "MetaTxs: event window fetch failed");
if !private_summaries.is_empty() {
ds.save_meta_txs(&address, &private_summaries);
sort_meta_txs_recency(&mut private_summaries);
}
let _ = action_tx.send(Action::AddressMetaTxsLoaded {
address,
summaries: private_summaries,
next_token: if pool_has_more {
pool_min_searched
} else {
None
},
next_window_size: if pool_has_more {
pool_suggested_window
} else {
None
},
});
return;
}
};
let _ = action_tx.send(Action::AddressEventWindowUpdated {
address,
min_searched: outcome.min_searched,
max_searched: outcome.max_searched,
deferred_gap: outcome.deferred_gap,
});
let account_has_more = outcome.min_searched > from_block;
let has_more_below = account_has_more || pool_has_more;
let (combined_next_token, combined_next_window) = if has_more_below {
let pool_min = pool_min_searched.unwrap_or(u64::MAX);
if !account_has_more && pool_has_more {
(Some(pool_min), pool_suggested_window)
} else if account_has_more && !pool_has_more {
(Some(outcome.min_searched), outcome.suggested_next_window)
} else {
if outcome.min_searched <= pool_min {
(Some(outcome.min_searched), outcome.suggested_next_window)
} else {
(Some(pool_min), pool_suggested_window)
}
}
} else {
(None, None)
};
if outcome.page.events.is_empty() {
debug!(
addr = %format!("{:#x}", address),
min_searched = outcome.min_searched,
max_searched = outcome.max_searched,
floor = from_block,
private_meta_txs = private_summaries.len(),
pool_min_searched = ?pool_min_searched,
pool_has_more,
account_has_more,
"MetaTxs: no new events in this window"
);
if !private_summaries.is_empty() {
ds.save_meta_txs(&address, &private_summaries);
}
sort_meta_txs_recency(&mut private_summaries);
let _ = action_tx.send(Action::AddressMetaTxsLoaded {
address,
summaries: private_summaries,
next_token: combined_next_token,
next_window_size: combined_next_window,
});
return;
}
let mut summaries = derive_meta_txs_from_page(address, &outcome.page, abi_reg).await;
let calls_from_page =
build_contract_calls_from_pf_rows(address, &outcome.page.tx_rows, abi_reg).await;
let public_count = summaries.len();
let private_count = private_summaries.len();
let existing: std::collections::HashSet<_> = summaries.iter().map(|s| s.hash).collect();
for s in private_summaries.drain(..) {
if !existing.contains(&s.hash) {
summaries.push(s);
}
}
sort_meta_txs_recency(&mut summaries);
info!(
addr = %format!("{:#x}", address),
events = outcome.page.events.len(),
candidates = outcome.page.tx_rows.len(),
meta_txs = summaries.len(),
public_meta_txs = public_count,
private_meta_txs = private_count,
supplementary_calls = calls_from_page.len(),
account_min_searched = outcome.min_searched,
account_max_searched = outcome.max_searched,
pool_min_searched = ?pool_min_searched,
floor = from_block,
next_token = ?combined_next_token,
next_window = ?combined_next_window,
deferred_gap = ?outcome.deferred_gap,
"MetaTxs: classified"
);
if !summaries.is_empty() {
ds.save_meta_txs(&address, &summaries);
}
if !calls_from_page.is_empty() {
let _ = action_tx.send(Action::AddressCallsMerged {
address,
calls: calls_from_page,
});
}
let _ = action_tx.send(Action::AddressMetaTxsLoaded {
address,
summaries,
next_token: combined_next_token,
next_window_size: combined_next_window,
});
}
async fn emit_recent_meta_txs_via_dune(
address: starknet::core::types::Felt,
dune_client: &Arc<dune::DuneClient>,
pf: &Arc<crate::data::pathfinder::PathfinderClient>,
abi_reg: &Arc<AbiRegistry>,
ds: &Arc<dyn crate::data::DataSource>,
action_tx: &mpsc::UnboundedSender<Action>,
) {
use futures::FutureExt;
use futures::stream::{FuturesUnordered, StreamExt};
use starknet::core::types::Felt;
const META_TX_LIMIT: u32 = 100;
const RECENT_DAYS: i64 = 90;
const WIDE_DAYS: i64 = 365;
const WIDE_HEDGE_DELAY: std::time::Duration = std::time::Duration::from_secs(15);
let today = chrono::Utc::now().date_naive();
let recent_min_date = today - chrono::Duration::days(RECENT_DAYS);
let wide_min_date = today - chrono::Duration::days(WIDE_DAYS);
let mut rungs = FuturesUnordered::new();
{
let d = dune_client.clone();
rungs.push(
async move {
let rows = d
.query_meta_tx_calls_windowed(
address,
0,
u64::MAX,
META_TX_LIMIT,
Some(recent_min_date),
)
.await;
(RECENT_DAYS, rows)
}
.boxed(),
);
}
{
let d = dune_client.clone();
rungs.push(
async move {
use tracing::Instrument;
let rows = hedged_query(
|| {
d.query_meta_tx_calls_windowed(
address,
0,
u64::MAX,
META_TX_LIMIT,
Some(wide_min_date),
)
},
WIDE_HEDGE_DELAY,
)
.instrument(tracing::debug_span!(
"hedged_meta_tx",
addr = %format!("{:#x}", address),
min_date = %wide_min_date,
))
.await;
(WIDE_DAYS, rows)
}
.boxed(),
);
}
let mut seen: std::collections::HashSet<Felt> = std::collections::HashSet::new();
while let Some((days, result)) = rungs.next().await {
match result {
Ok(rows) => {
let n = rows.len();
let fresh = fresh_meta_tx_hashes(&rows, &mut seen);
info!(
addr = %format!("{:#x}", address),
rung_days = days,
meta_tx_calls = n,
fresh = fresh.len(),
"MetaTxs: Dune ladder rung paint"
);
paint_meta_tx_hashes(address, &fresh, pf, abi_reg, ds, action_tx).await;
if n >= META_TX_LIMIT as usize {
break;
}
}
Err(e) => {
warn!(
addr = %format!("{:#x}", address),
rung_days = days,
error = %e,
"MetaTxs: Dune ladder rung query failed"
);
}
}
}
drop(rungs);
}
fn fresh_meta_tx_hashes(
rows: &[crate::data::types::ContractCallSummary],
seen: &mut std::collections::HashSet<starknet::core::types::Felt>,
) -> Vec<starknet::core::types::Felt> {
use starknet::core::types::Felt;
rows.iter()
.map(|c| c.tx_hash)
.filter(|h| *h != Felt::ZERO && seen.insert(*h))
.collect()
}
async fn paint_meta_tx_hashes(
address: starknet::core::types::Felt,
hashes: &[starknet::core::types::Felt],
pf: &Arc<crate::data::pathfinder::PathfinderClient>,
abi_reg: &Arc<AbiRegistry>,
ds: &Arc<dyn crate::data::DataSource>,
action_tx: &mpsc::UnboundedSender<Action>,
) {
if hashes.is_empty() {
return;
}
let rows = match pf.get_txs_by_hash(hashes).await {
Ok(r) => r,
Err(e) => {
warn!(addr = %format!("{:#x}", address), error = %e, "MetaTxs: pf txs-by-hash fetch failed");
return;
}
};
let mut summaries = Vec::new();
for row in &rows {
if let Some(s) = classify_meta_tx_candidate(address, row, abi_reg).await {
summaries.push(s);
}
}
if summaries.is_empty() {
return;
}
sort_meta_txs_recency(&mut summaries);
ds.save_meta_txs(&address, &summaries);
let _ = action_tx.send(Action::AddressMetaTxsStreamed { address, summaries });
}
#[derive(Debug, PartialEq, Eq)]
pub(super) enum CallsDuneQuery {
TopDelta {
from_block: u64,
min_date: Option<chrono::NaiveDate>,
},
DeployScoped {
from_block: u64,
min_date: chrono::NaiveDate,
},
Unwindowed,
}
pub(super) fn ts_to_min_date(ts: u64) -> Option<chrono::NaiveDate> {
if ts == 0 {
return None;
}
chrono::DateTime::from_timestamp(ts as i64, 0)
.map(|dt| dt.date_naive() - chrono::Duration::days(1))
}
pub(super) async fn block_date_floor(
ds: &Arc<dyn crate::data::DataSource>,
block: u64,
) -> Option<chrono::NaiveDate> {
let ts = ds.get_block(block).await.ok().map(|b| b.timestamp)?;
ts_to_min_date(ts)
}
#[derive(Clone, Copy, Debug)]
pub(super) struct CallsPage {
pub from: Option<u64>,
pub to: Option<u64>,
pub limit: u32,
pub track: bool,
}
pub(super) fn calls_page_coverage(page: CallsPage, blocks: &[u64]) -> Option<(u64, u64)> {
let lo = if blocks.len() >= page.limit as usize {
blocks.iter().min()? + 1
} else {
page.from?
};
let hi = match page.to {
Some(to) => to,
None => blocks.iter().max()?.checked_sub(1)?,
};
(lo <= hi).then_some((lo, hi))
}
fn record_calls_coverage(
ds: &Arc<dyn DataSource>,
action_tx: &mpsc::UnboundedSender<Action>,
address: starknet::core::types::Felt,
page: CallsPage,
(lo, hi): (u64, u64),
) {
ds.add_call_scanned_range(&address, lo, hi);
let tracked_from = page.track.then(|| page.from.unwrap_or(lo).min(lo));
if let Some(t) = tracked_from {
ds.save_search_progress(&address, FilterKind::DuneCalls, t, hi);
}
let _ = action_tx.send(Action::AddressCallRangeScanned {
address,
lo_block: lo,
hi_block: hi,
tracked_from,
});
}
pub(super) fn pick_calls_dune_query(
newest_cached_pair: Option<(u64, u64)>,
deploy_floor: Option<u64>,
deploy_floor_ts: Option<u64>,
) -> CallsDuneQuery {
if let Some((block, ts)) = newest_cached_pair {
return CallsDuneQuery::TopDelta {
from_block: block + 1,
min_date: ts_to_min_date(ts),
};
}
match (deploy_floor, deploy_floor_ts) {
(Some(from_block), Some(ts)) if ts > 0 => match ts_to_min_date(ts) {
Some(min_date) => CallsDuneQuery::DeployScoped {
from_block,
min_date,
},
None => CallsDuneQuery::Unwindowed,
},
_ => CallsDuneQuery::Unwindowed,
}
}
pub(super) async fn fetch_address_contract_calls(
address: starknet::core::types::Felt,
ds: &Arc<dyn crate::data::DataSource>,
dune: Option<&Arc<dune::DuneClient>>,
pf: Option<&Arc<crate::data::pathfinder::PathfinderClient>>,
voyager_c: Option<&Arc<voyager::VoyagerClient>>,
abi_reg: &Arc<AbiRegistry>,
action_tx: &mpsc::UnboundedSender<Action>,
nonce: starknet::core::types::Felt,
class_hash: Option<starknet::core::types::Felt>,
) {
const CONTRACT_CALL_LIMIT: u32 = 500;
const LEGACY_CALLS_RECHECK_BLOCKS: u64 = 50_000;
let Some(dune_client) = dune else {
debug!(
addr = %format!("{:#x}", address),
"Calls: Dune not configured; skipping contract calls fetch"
);
return;
};
let _guard = QueryGuard::new(
action_tx,
format!("calls:{}", query_addr_prefix(&address)),
"Calls fetch".to_string(),
);
let cached_calls = ds.load_cached_address_calls(&address);
let resume_after = match ds.load_search_progress(&address, FilterKind::DuneCalls) {
Some((_, hi)) => Some(hi),
None => cached_calls
.iter()
.map(|c| c.block_number)
.max()
.filter(|b| *b > 0)
.map(|b| b.saturating_sub(LEGACY_CALLS_RECHECK_BLOCKS)),
};
if let Some(after) = resume_after {
let ts = ds.get_block(after).await.map(|b| b.timestamp).unwrap_or(0);
let plan = pick_calls_dune_query(Some((after, ts)), None, None);
let page = CallsPage {
from: Some(after + 1),
to: None,
limit: CONTRACT_CALL_LIMIT,
track: true,
};
match run_calls_dune_plan(&plan, dune_client, address, CONTRACT_CALL_LIMIT).await {
Ok(dune_calls) => {
persist_and_emit_calls(
address,
dune_calls,
Some(cached_calls),
Some(page),
abi_reg,
ds,
pf,
action_tx,
nonce,
class_hash,
)
.await
}
Err(e) => {
warn!(addr = %format!("{:#x}", address), error = %e, "Calls: Dune contract calls fetch failed")
}
}
return;
}
use futures::FutureExt;
use futures::stream::{FuturesUnordered, StreamExt};
const RECENT_DAYS: i64 = 90;
const WIDE_DAYS: i64 = 365;
const WIDE_HEDGE_DELAY: std::time::Duration = std::time::Duration::from_secs(15);
let today = chrono::Utc::now().date_naive();
let recent_min_date = today - chrono::Duration::days(RECENT_DAYS);
let wide_min_date = today - chrono::Duration::days(WIDE_DAYS);
let mut rungs = FuturesUnordered::new();
{
let dune_client = dune_client.clone();
rungs.push(
async move {
let rows = dune_client
.query_contract_calls_windowed(
address,
0,
u64::MAX,
CONTRACT_CALL_LIMIT,
Some(recent_min_date),
)
.await;
(RECENT_DAYS, rows)
}
.boxed(),
);
}
{
let dune_client = dune_client.clone();
rungs.push(
async move {
let rows = hedged_contract_calls_window(
&dune_client,
address,
wide_min_date,
CONTRACT_CALL_LIMIT,
WIDE_HEDGE_DELAY,
)
.await;
(WIDE_DAYS, rows)
}
.boxed(),
);
}
let mut filled = false;
while let Some((days, result)) = rungs.next().await {
match result {
Ok(rows) => {
let n = rows.len();
info!(
addr = %format!("{:#x}", address),
rung_days = days,
calls = n,
"Calls: ladder rung paint"
);
let page = CallsPage {
from: None,
to: None,
limit: CONTRACT_CALL_LIMIT,
track: true,
};
persist_and_emit_calls(
address,
rows,
None,
Some(page),
abi_reg,
ds,
pf,
action_tx,
nonce,
class_hash,
)
.await;
if n >= CONTRACT_CALL_LIMIT as usize {
filled = true;
break;
}
}
Err(e) => {
warn!(
addr = %format!("{:#x}", address),
rung_days = days,
error = %e,
"Calls: ladder rung query failed"
);
}
}
}
drop(rungs);
if filled {
return;
}
let (deploy_floor, deploy_floor_ts) = resolve_deploy_floor(address, ds, pf, voyager_c).await;
let plan = pick_calls_dune_query(None, deploy_floor, deploy_floor_ts);
if let CallsDuneQuery::DeployScoped {
from_block,
min_date,
} = &plan
{
debug!(
addr = %format!("{:#x}", address),
from_block,
?min_date,
"Calls: cold-cache deploy-scoped lifetime fallback (no rung filled)"
);
}
match run_calls_dune_plan(&plan, dune_client, address, CONTRACT_CALL_LIMIT).await {
Ok(dune_calls) => {
info!(
addr = %format!("{:#x}", address),
calls = dune_calls.len(),
"Calls: deploy-scoped lifetime fallback complete"
);
let from = match plan {
CallsDuneQuery::DeployScoped { from_block, .. } => Some(from_block),
_ => None,
};
let page = CallsPage {
from,
to: None,
limit: CONTRACT_CALL_LIMIT,
track: true,
};
persist_and_emit_calls(
address,
dune_calls,
None,
Some(page),
abi_reg,
ds,
pf,
action_tx,
nonce,
class_hash,
)
.await
}
Err(e) => {
warn!(addr = %format!("{:#x}", address), error = %e, "Calls: deploy-scoped lifetime fallback failed")
}
}
}
async fn resolve_deploy_floor(
address: starknet::core::types::Felt,
ds: &Arc<dyn crate::data::DataSource>,
pf: Option<&Arc<crate::data::pathfinder::PathfinderClient>>,
voyager_c: Option<&Arc<voyager::VoyagerClient>>,
) -> (Option<u64>, Option<u64>) {
let mut deploy_floor: Option<u64> = ds
.load_cached_deploy_info(&address)
.map(|(_, block, _)| block)
.or_else(|| {
ds.load_cached_class_history(&address)
.iter()
.map(|e| e.block_number)
.min()
});
if deploy_floor.is_none() {
let pf_fut = async {
if let Some(pf_client) = pf {
Some(pf_client.get_class_history(address).await)
} else {
None
}
};
let voyager_fut = async {
if let Some(vc) = voyager_c {
Some(vc.get_label(address).await)
} else {
None
}
};
let (pf_result, voyager_result) = tokio::join!(pf_fut, voyager_fut);
if let Some(Ok(entries)) = &pf_result {
if !entries.is_empty() {
ds.save_class_history(&address, entries);
}
deploy_floor = entries.iter().map(|e| e.block_number).min();
} else if let Some(Err(e)) = &pf_result {
debug!(
addr = %format!("{:#x}", address),
error = %e,
"Calls: pf class-history fetch failed; trying Voyager"
);
}
if deploy_floor.is_none() {
match voyager_result {
Some(Ok(label)) => deploy_floor = label.deploy_block,
Some(Err(e)) => debug!(
addr = %format!("{:#x}", address),
error = %e,
"Calls: Voyager label fetch failed"
),
None => {}
}
}
}
let deploy_floor_ts = match deploy_floor {
Some(b) => ds.get_block(b).await.ok().map(|blk| blk.timestamp),
None => None,
};
(deploy_floor, deploy_floor_ts)
}
async fn run_calls_dune_plan(
plan: &CallsDuneQuery,
dune_client: &Arc<dune::DuneClient>,
address: starknet::core::types::Felt,
limit: u32,
) -> Result<Vec<crate::data::types::ContractCallSummary>, String> {
match plan {
CallsDuneQuery::TopDelta {
from_block,
min_date,
} => {
dune_client
.query_contract_calls_windowed(address, *from_block, u64::MAX, limit, *min_date)
.await
}
CallsDuneQuery::DeployScoped {
from_block,
min_date,
} => {
dune_client
.query_contract_calls_windowed(
address,
*from_block,
u64::MAX,
limit,
Some(*min_date),
)
.await
}
CallsDuneQuery::Unwindowed => dune_client.query_contract_calls(address, limit).await,
}
}
async fn hedged_contract_calls_window(
dune_client: &Arc<dune::DuneClient>,
address: starknet::core::types::Felt,
min_date: chrono::NaiveDate,
limit: u32,
hedge_delay: std::time::Duration,
) -> Result<Vec<crate::data::types::ContractCallSummary>, String> {
use tracing::Instrument;
hedged_query(
|| dune_client.query_contract_calls_windowed(address, 0, u64::MAX, limit, Some(min_date)),
hedge_delay,
)
.instrument(tracing::debug_span!("hedged_calls", addr = %format!("{:#x}", address), ?min_date))
.await
}
async fn hedged_query<F, Fut, T>(
make_attempt: F,
hedge_delay: std::time::Duration,
) -> Result<T, String>
where
F: Fn() -> Fut,
Fut: std::future::Future<Output = Result<T, String>>,
{
let first = make_attempt();
tokio::pin!(first);
tokio::select! {
r = &mut first => r,
_ = tokio::time::sleep(hedge_delay) => {
debug!(
hedge_delay_s = hedge_delay.as_secs(),
"hedged_query: first attempt slow, firing hedged second execution"
);
let second = make_attempt();
tokio::pin!(second);
tokio::select! {
r = &mut first => r,
r = &mut second => r,
}
}
}
}
#[allow(clippy::too_many_arguments)]
async fn persist_and_emit_calls(
address: starknet::core::types::Felt,
dune_calls: Vec<crate::data::types::ContractCallSummary>,
preloaded_cache: Option<Vec<crate::data::types::ContractCallSummary>>,
page: Option<CallsPage>,
abi_reg: &Arc<AbiRegistry>,
ds: &Arc<dyn crate::data::DataSource>,
pf: Option<&Arc<crate::data::pathfinder::PathfinderClient>>,
action_tx: &mpsc::UnboundedSender<Action>,
nonce: starknet::core::types::Felt,
class_hash: Option<starknet::core::types::Felt>,
) {
let blocks: Vec<u64> = dune_calls.iter().map(|c| c.block_number).collect();
let coverage = page.and_then(|p| Some((p, calls_page_coverage(p, &blocks)?)));
let calls = enrich_dune_calls(address, dune_calls, abi_reg, ds, pf, action_tx).await;
persist_calls_merged(ds, address, &calls, preloaded_cache);
let _ = action_tx.send(Action::AddressInfoLoaded {
info: crate::data::types::SnAddressInfo {
address,
nonce,
class_hash,
recent_events: Vec::new(),
token_balances: Vec::new(),
},
decoded_events: Vec::new(),
tx_summaries: Vec::new(),
contract_calls: calls,
});
if let Some((p, range)) = coverage {
record_calls_coverage(ds, action_tx, address, p, range);
}
}
pub(super) async fn classify_meta_tx_candidate(
address: starknet::core::types::Felt,
row: &crate::data::pathfinder::TxByHashData,
abi_reg: &Arc<AbiRegistry>,
) -> Option<crate::data::types::MetaTxIntenderSummary> {
use starknet::core::types::Felt;
use crate::data::types::MetaTxIntenderSummary;
use crate::decode::outside_execution::{DetectionMethod, detect_outside_execution};
let hash = Felt::from_hex(&row.hash).ok()?;
let sender = Felt::from_hex(&row.sender).ok()?;
if sender == address {
return None; }
if helpers::normalize_pf_tx_type(&row.tx_type) != "INVOKE" {
return None;
}
let calldata: Vec<Felt> = row
.calldata
.iter()
.filter_map(|h| Felt::from_hex(h).ok())
.collect();
let calls = parse_multicall(&calldata);
helpers::prewarm_abis([address], abi_reg).await;
let mut found: Option<(
crate::decode::outside_execution::OutsideExecutionInfo,
&'static str,
)> = None;
for c in &calls {
let name = abi_reg.get_selector_name(&c.selector);
let Some((oe, method)) = detect_outside_execution(c, name.as_deref()) else {
continue;
};
if oe.intender != address {
continue;
}
let label: &'static str = match method {
DetectionMethod::AvnuForwarder => "avnu",
DetectionMethod::Name => oe.version.short(),
DetectionMethod::Heuristic => "v?",
};
found = Some((oe, label));
break;
}
let (oe, version_label) = found?;
let targets: Vec<Felt> = oe
.inner_calls
.iter()
.map(|ic| ic.contract_address)
.collect();
helpers::prewarm_abis(targets.iter().copied(), abi_reg).await;
let inner_endpoints =
helpers::format_selector_names(oe.inner_calls.iter().map(|ic| ic.selector), abi_reg);
let fee_fri = u128::from_str_radix(row.actual_fee.trim_start_matches("0x"), 16).unwrap_or(0);
Some(MetaTxIntenderSummary {
hash,
block_number: row.block_number,
tx_index: row.tx_index,
timestamp: row.block_timestamp,
paymaster: sender,
version: version_label.to_string(),
oe_nonce: oe.nonce,
total_fee_fri: fee_fri,
status: row.status.clone(),
inner_targets: targets,
inner_endpoints,
caller: oe.caller,
})
}
pub(super) fn sort_meta_txs_recency(summaries: &mut [crate::data::types::MetaTxIntenderSummary]) {
summaries.sort_by(|a, b| {
b.block_number
.cmp(&a.block_number)
.then(b.tx_index.cmp(&a.tx_index))
});
}
pub(super) struct PrivateDiscoveryOutcome {
pub summaries: Vec<crate::data::types::MetaTxIntenderSummary>,
pub pool_min_searched: Option<u64>,
pub pool_suggested_window: Option<u64>,
pub pool_floor: Option<u64>,
}
pub(super) async fn discover_private_meta_txs(
user: starknet::core::types::Felt,
from_block: u64,
continuation_token: Option<u64>,
window_size: u64,
ds: &Arc<dyn DataSource>,
pf: &Arc<crate::data::pathfinder::PathfinderClient>,
abi_reg: &Arc<AbiRegistry>,
) -> PrivateDiscoveryOutcome {
use std::collections::{HashMap, HashSet};
use starknet::core::types::Felt;
use crate::decode::privacy::{
POOL_ADDRESS, POOL_DEPLOY_BLOCK, PoolEventMatch, match_pool_event,
};
use crate::network::event_window::{
EXTEND_DOWN_INITIAL_WINDOW, EventWindowPolicy, ensure_address_events_window,
};
let empty_outcome = || PrivateDiscoveryOutcome {
summaries: Vec::new(),
pool_min_searched: None,
pool_suggested_window: None,
pool_floor: None,
};
let (notes, nullifier_pairs) = ds.load_private_notes_for_user(&user);
if notes.is_empty() && nullifier_pairs.is_empty() {
return empty_outcome();
}
let note_ids: HashSet<Felt> = notes.iter().map(|n| n.note_id).collect();
let nullifiers: HashSet<Felt> = nullifier_pairs.iter().map(|(n, _)| *n).collect();
let Some(latest_block) = ds.latest_block_hint().await else {
debug!(
user = %format!("{:#x}", user),
"discover_private_meta_txs: no chain head available"
);
return empty_outcome();
};
let effective_window = if window_size == 0 {
EXTEND_DOWN_INITIAL_WINDOW
} else {
window_size
};
let policy = match continuation_token {
None => EventWindowPolicy::TopDelta,
Some(_) => EventWindowPolicy::ExtendDown {
window_size: effective_window,
},
};
let pool = *POOL_ADDRESS;
let pool_floor = from_block.max(POOL_DEPLOY_BLOCK);
let outcome = match ensure_address_events_window(
pool,
EventQueryKind::Contract,
policy,
Some(pf),
ds,
latest_block,
pool_floor,
)
.await
{
Ok(o) => o,
Err(e) => {
warn!(
user = %format!("{:#x}", user),
error = %e,
"discover_private_meta_txs: pool event scan failed"
);
return empty_outcome();
}
};
let pool_min_searched = Some(outcome.min_searched);
let pool_suggested_window = outcome.suggested_next_window;
let mut matched: HashSet<Felt> = HashSet::new();
for ev in &outcome.merged {
match match_pool_event(ev) {
Some(PoolEventMatch::Note(nid)) if note_ids.contains(&nid) => {
matched.insert(ev.transaction_hash);
}
Some(PoolEventMatch::Nullifier(nul)) if nullifiers.contains(&nul) => {
matched.insert(ev.transaction_hash);
}
_ => {}
}
}
debug!(
user = %format!("{:#x}", user),
cached_pool_events = outcome.merged.len(),
fresh_pool_events = outcome.page.events.len(),
matched_txs = matched.len(),
min_searched = outcome.min_searched,
max_searched = outcome.max_searched,
"discover_private_meta_txs: matched"
);
if matched.is_empty() {
return PrivateDiscoveryOutcome {
summaries: Vec::new(),
pool_min_searched,
pool_suggested_window,
pool_floor: Some(pool_floor),
};
}
let already_classified: HashSet<Felt> = ds
.load_cached_meta_txs(&user)
.into_iter()
.map(|s| s.hash)
.collect();
let new_matches: HashSet<Felt> = matched.difference(&already_classified).copied().collect();
let already_seen = matched.len() - new_matches.len();
if new_matches.is_empty() {
debug!(
user = %format!("{:#x}", user),
matched = matched.len(),
already_seen,
"discover_private_meta_txs: all matches already classified, skipping"
);
return PrivateDiscoveryOutcome {
summaries: Vec::new(),
pool_min_searched,
pool_suggested_window,
pool_floor: Some(pool_floor),
};
}
let matched = new_matches;
let mut row_by_hash: HashMap<Felt, crate::data::pathfinder::TxByHashData> = HashMap::new();
for row in &outcome.page.tx_rows {
if let Ok(h) = Felt::from_hex(&row.hash) {
row_by_hash.insert(h, row.clone());
}
}
let missing: Vec<Felt> = matched
.iter()
.copied()
.filter(|h| !row_by_hash.contains_key(h))
.collect();
let mut fetched_rows = 0usize;
if !missing.is_empty() {
match pf.get_txs_by_hash(&missing).await {
Ok(rows) => {
fetched_rows = rows.len();
for row in rows {
if let Ok(h) = Felt::from_hex(&row.hash) {
row_by_hash.insert(h, row);
}
}
}
Err(e) => {
warn!(
user = %format!("{:#x}", user),
error = %e,
missing = missing.len(),
"discover_private_meta_txs: tx body batch fetch failed"
);
}
}
}
let mut summaries: Vec<crate::data::types::MetaTxIntenderSummary> =
Vec::with_capacity(matched.len());
let mut dropped_no_body = 0usize;
let mut dropped_no_oe = 0usize;
for tx_hash in &matched {
let Some(row) = row_by_hash.get(tx_hash) else {
dropped_no_body += 1;
continue;
};
match build_private_meta_tx_summary(row, abi_reg).await {
Some(s) => summaries.push(s),
None => {
dropped_no_oe += 1;
debug!(
user = %format!("{:#x}", user),
tx = %row.hash,
sender = %row.sender,
tx_type = %row.tx_type,
"discover_private_meta_txs: matched tx is not a meta-tx (no OE wrapping the pool call)"
);
}
}
}
sort_meta_txs_recency(&mut summaries);
debug!(
user = %format!("{:#x}", user),
matched = matched.len(),
fetched_rows,
dropped_no_body,
dropped_no_oe,
summaries = summaries.len(),
"discover_private_meta_txs: built"
);
PrivateDiscoveryOutcome {
summaries,
pool_min_searched,
pool_suggested_window,
pool_floor: Some(pool_floor),
}
}
async fn build_private_meta_tx_summary(
row: &crate::data::pathfinder::TxByHashData,
abi_reg: &Arc<AbiRegistry>,
) -> Option<crate::data::types::MetaTxIntenderSummary> {
use starknet::core::types::Felt;
use crate::data::types::MetaTxIntenderSummary;
use crate::decode::outside_execution::{DetectionMethod, detect_outside_execution};
use crate::decode::privacy::POOL_ADDRESS;
let hash = Felt::from_hex(&row.hash).ok()?;
let sender = Felt::from_hex(&row.sender).ok()?;
if helpers::normalize_pf_tx_type(&row.tx_type) != "INVOKE" {
return None;
}
let calldata: Vec<Felt> = row
.calldata
.iter()
.filter_map(|h| Felt::from_hex(h).ok())
.collect();
let calls = parse_multicall(&calldata);
let pool = *POOL_ADDRESS;
let mut found: Option<(
crate::decode::outside_execution::OutsideExecutionInfo,
DetectionMethod,
)> = None;
for c in &calls {
let name = abi_reg.get_selector_name(&c.selector);
let Some((oe, method)) = detect_outside_execution(c, name.as_deref()) else {
continue;
};
if oe.inner_calls.iter().any(|ic| ic.contract_address == pool) {
found = Some((oe, method));
break;
}
}
let (oe, method) = found?;
let label: &'static str = match method {
DetectionMethod::AvnuForwarder => "avnu",
DetectionMethod::Name => oe.version.short(),
DetectionMethod::Heuristic => "v?",
};
let targets: Vec<Felt> = oe
.inner_calls
.iter()
.map(|ic| ic.contract_address)
.collect();
helpers::prewarm_abis(targets.iter().copied(), abi_reg).await;
let inner_endpoints =
helpers::format_selector_names(oe.inner_calls.iter().map(|ic| ic.selector), abi_reg);
let fee_fri = u128::from_str_radix(row.actual_fee.trim_start_matches("0x"), 16).unwrap_or(0);
Some(MetaTxIntenderSummary {
hash,
block_number: row.block_number,
tx_index: row.tx_index,
timestamp: row.block_timestamp,
paymaster: sender,
version: label.to_string(),
oe_nonce: oe.nonce,
total_fee_fri: fee_fri,
status: row.status.clone(),
inner_targets: targets,
inner_endpoints,
caller: oe.caller,
})
}
pub(super) async fn refresh_address_rpc(
address: starknet::core::types::Felt,
ds: &Arc<dyn DataSource>,
pf: &Option<Arc<crate::data::pathfinder::PathfinderClient>>,
abi_reg: &Arc<AbiRegistry>,
action_tx: &mpsc::UnboundedSender<Action>,
) {
let nonce = match ds.get_nonce(address).await {
Ok(n) => n,
Err(e) => {
debug!(addr = %format!("{:#x}", address), error = %e, "refresh_address_rpc: get_nonce failed");
return;
}
};
let Some(latest_block) = ds.latest_block_hint().await else {
debug!(addr = %format!("{:#x}", address), "refresh_address_rpc: no chain head available");
return;
};
if nonce != starknet::core::types::Felt::ZERO {
ds.save_cached_nonce(&address, &nonce, latest_block);
}
let ds_dyn: Arc<dyn DataSource> = Arc::clone(ds);
let page = match crate::network::event_window::ensure_address_events_window(
address,
EventQueryKind::Account,
crate::network::event_window::EventWindowPolicy::TopDelta,
pf.as_ref(),
&ds_dyn,
latest_block,
0, )
.await
{
Ok(o) => {
let _ = action_tx.send(Action::AddressEventWindowUpdated {
address,
min_searched: o.min_searched,
max_searched: o.max_searched,
deferred_gap: o.deferred_gap,
});
o.page
}
Err(e) => {
debug!(addr = %format!("{:#x}", address), error = %e, "refresh_address_rpc: event_window fetch failed");
return;
}
};
let (unique_hashes, tx_block_map): (
Vec<starknet::core::types::Felt>,
std::collections::HashMap<starknet::core::types::Felt, u64>,
) = if !page.tx_rows.is_empty() {
(page.unique_hashes.clone(), page.tx_block_map.clone())
} else {
let mut seen = std::collections::HashSet::new();
let mut hashes: Vec<starknet::core::types::Felt> = Vec::with_capacity(page.events.len());
let mut map: std::collections::HashMap<starknet::core::types::Felt, u64> =
std::collections::HashMap::new();
for e in &page.events {
if e.transaction_hash != starknet::core::types::Felt::ZERO
&& seen.insert(e.transaction_hash)
{
hashes.push(e.transaction_hash);
}
map.entry(e.transaction_hash).or_insert(e.block_number);
}
(hashes, map)
};
let cached_hashes: std::collections::HashSet<_> = ds
.load_cached_address_txs(&address)
.iter()
.map(|t| t.hash)
.collect();
let summaries: Vec<crate::data::types::AddressTxSummary> = if !page.tx_rows.is_empty() {
let new_rows: Vec<_> = page
.tx_rows
.iter()
.filter(|r| {
starknet::core::types::Felt::from_hex(&r.hash)
.map(|h| !cached_hashes.contains(&h))
.unwrap_or(false)
})
.cloned()
.collect();
if new_rows.is_empty() {
Vec::new()
} else {
build_tx_summaries_from_pf_rows(&new_rows, abi_reg).await
}
} else {
let hashes_to_fetch: Vec<_> = unique_hashes
.iter()
.filter(|h| !cached_hashes.contains(h))
.copied()
.collect();
if hashes_to_fetch.is_empty() {
Vec::new()
} else {
fetch_tx_summaries_from_hashes(
&hashes_to_fetch,
&tx_block_map,
ds,
pf.as_ref(),
abi_reg,
action_tx,
"RPC refresh: fetching txs",
)
.await
}
};
if !summaries.is_empty() {
let _ = action_tx.send(Action::AddressTxsStreamed {
address,
source: Source::Rpc,
tx_summaries: summaries,
complete: false,
});
}
}
#[cfg(test)]
mod meta_tx_tests {
use super::*;
use crate::data::types::MetaTxIntenderSummary;
use starknet::core::types::Felt;
fn s(block: u64, idx: u64) -> MetaTxIntenderSummary {
MetaTxIntenderSummary {
hash: Felt::from(block * 1000 + idx),
block_number: block,
tx_index: idx,
timestamp: 0,
paymaster: Felt::ZERO,
version: "v3".into(),
oe_nonce: Felt::ZERO,
total_fee_fri: 0,
status: "OK".into(),
inner_targets: vec![],
inner_endpoints: String::new(),
caller: Felt::ZERO,
}
}
#[test]
fn sort_orders_by_block_then_tx_index_desc() {
let mut v = vec![s(100, 5), s(200, 1), s(100, 7)];
sort_meta_txs_recency(&mut v);
let coords: Vec<_> = v.iter().map(|m| (m.block_number, m.tx_index)).collect();
assert_eq!(coords, vec![(200, 1), (100, 7), (100, 5)]);
}
}
#[cfg(test)]
mod shared_pipeline_tests {
use super::*;
use crate::data::pathfinder::TxByHashData;
use crate::data::rpc::RpcDataSource;
use crate::data::types::SnEvent;
use crate::decode::AbiRegistry;
use crate::decode::class_cache::ClassCache;
use starknet::core::types::Felt;
fn mk_abi_reg() -> Arc<AbiRegistry> {
let ds: Arc<dyn DataSource> = Arc::new(RpcDataSource::new("http://127.0.0.1:1/"));
let db = rusqlite::Connection::open_in_memory().expect("in-memory sqlite");
db.execute_batch(
"CREATE TABLE IF NOT EXISTS parsed_abis (class_hash TEXT PRIMARY KEY, data TEXT NOT NULL);"
).unwrap();
let class_cache = ClassCache::new(db, 16);
Arc::new(AbiRegistry::new(ds, class_cache))
}
fn tx_row(
hash: Felt,
sender: Felt,
block_number: u64,
tx_index: u64,
calldata: Vec<Felt>,
) -> TxByHashData {
TxByHashData {
hash: format!("{:#x}", hash),
block_number,
block_timestamp: 0,
tx_index,
sender: format!("{:#x}", sender),
nonce: Some(0),
tx_type: "INVOKE".to_string(),
calldata: calldata.iter().map(|c| format!("{:#x}", c)).collect(),
actual_fee: "0x0".to_string(),
tip: 0,
status: "OK".to_string(),
revert_reason: None,
}
}
fn ev(tx_hash: Felt, block_number: u64, event_index: u64) -> SnEvent {
SnEvent {
from_address: Felt::ZERO,
keys: vec![],
data: vec![],
transaction_hash: tx_hash,
block_number,
event_index,
}
}
#[tokio::test]
async fn derive_meta_txs_skips_self_relayed() {
let addr =
Felt::from_hex("0x3a496b92d292386ad70dab94ae181a06d289440e3b632a2435721b4280874c4")
.unwrap();
let tx_hash = Felt::from(1u64);
let page = AddressActivityPage {
events: vec![ev(tx_hash, 100, 0)],
tx_rows: vec![tx_row(tx_hash, addr, 100, 3, vec![])],
unique_hashes: vec![tx_hash],
tx_block_map: std::collections::HashMap::from([(tx_hash, 100u64)]),
next_token: None,
};
let abi = mk_abi_reg();
let metas = derive_meta_txs_from_page(addr, &page, &abi).await;
assert!(
metas.is_empty(),
"self-relayed tx must not be classified as meta-tx"
);
}
#[tokio::test]
async fn derive_meta_txs_filters_non_meta_tx_rows() {
let addr =
Felt::from_hex("0x3a496b92d292386ad70dab94ae181a06d289440e3b632a2435721b4280874c4")
.unwrap();
let other_target = Felt::from(0xC0FFEEu64);
let paymaster = Felt::from(0xDEADu64);
let tx_hash = Felt::from(0xBEEFu64);
let calldata = vec![
Felt::from(1u64),
other_target,
Felt::from(0x99u64),
Felt::ZERO,
];
let page = AddressActivityPage {
events: vec![ev(tx_hash, 100, 0)],
tx_rows: vec![tx_row(tx_hash, paymaster, 100, 0, calldata)],
unique_hashes: vec![tx_hash],
tx_block_map: std::collections::HashMap::from([(tx_hash, 100u64)]),
next_token: None,
};
let abi = mk_abi_reg();
let metas = derive_meta_txs_from_page(addr, &page, &abi).await;
assert!(
metas.is_empty(),
"non-meta-tx invoke must not classify, got: {:?}",
metas
);
}
#[tokio::test]
async fn build_contract_calls_lists_all_multicall_endpoints() {
let target =
Felt::from_hex("0x3a496b92d292386ad70dab94ae181a06d289440e3b632a2435721b4280874c4")
.unwrap();
let other_a = Felt::from(0xAAu64);
let other_b = Felt::from(0xBBu64);
let sel_a = Felt::from(0x11u64);
let sel_t = Felt::from(0x22u64); let sel_b = Felt::from(0x33u64);
let calldata = vec![
Felt::from(3u64),
other_a,
sel_a,
Felt::ZERO,
target,
sel_t,
Felt::ZERO,
other_b,
sel_b,
Felt::ZERO,
];
let sender = Felt::from(0xCAFEu64);
let tx_hash = Felt::from(0xD00Du64);
let tx_rows = vec![tx_row(tx_hash, sender, 42, 7, calldata)];
let abi = mk_abi_reg();
let calls = build_contract_calls_from_pf_rows(target, &tx_rows, &abi).await;
assert_eq!(calls.len(), 1, "one row -> one ContractCallSummary");
let c = &calls[0];
assert_eq!(c.tx_hash, tx_hash);
assert_eq!(c.sender, sender);
assert_eq!(c.block_number, 42);
assert_eq!(c.status, "OK");
assert!(
!c.function_name.is_empty(),
"function_name must reflect the full top-level multicall"
);
assert_eq!(
c.function_name.matches(',').count(),
2,
"all three top-level calls should be listed, got: {:?}",
c.function_name
);
}
#[cfg(test)]
const HYBRID_TEST_ADDR: &str =
"0x3a496b92d292386ad70dab94ae181a06d289440e3b632a2435721b4280874c4";
#[tokio::test]
#[ignore = "requires APP_RPC_URL + APP_PATHFINDER_SERVICE_URL + Dune"]
async fn hybrid_account_shared_pipeline_derives_consistent_tabs() {
dotenvy::dotenv().ok();
let rpc_url = std::env::var("APP_RPC_URL").expect("APP_RPC_URL");
let pf_url =
std::env::var("APP_PATHFINDER_SERVICE_URL").expect("APP_PATHFINDER_SERVICE_URL");
let ds: Arc<dyn DataSource> = Arc::new(RpcDataSource::new(&rpc_url));
let pf = Arc::new(crate::data::pathfinder::PathfinderClient::new(pf_url));
let db = rusqlite::Connection::open_in_memory().expect("in-memory sqlite");
db.execute_batch(
"CREATE TABLE IF NOT EXISTS parsed_abis (class_hash TEXT PRIMARY KEY, data TEXT NOT NULL);",
)
.unwrap();
let abi = Arc::new(AbiRegistry::new(Arc::clone(&ds), ClassCache::new(db, 256)));
let address = Felt::from_hex(HYBRID_TEST_ADDR).unwrap();
let page =
fetch_address_activity(address, EventQueryKind::Account, 0, None, None, 100, &pf)
.await
.expect("fetch_address_activity");
println!(
"Pipeline page: {} events, {} tx_rows, next_token={:?}",
page.events.len(),
page.tx_rows.len(),
page.next_token
);
assert!(!page.events.is_empty(), "hybrid account must have events");
assert!(!page.tx_rows.is_empty(), "hybrid account must have tx_rows");
for e in &page.events {
assert!(
page.tx_block_map.contains_key(&e.transaction_hash),
"tx_block_map missing entry for event tx_hash {:#x}",
e.transaction_hash
);
}
let unique_event_hashes: std::collections::HashSet<Felt> =
page.events.iter().map(|e| e.transaction_hash).collect();
let row_hashes: std::collections::HashSet<Felt> = page
.tx_rows
.iter()
.filter_map(|r| Felt::from_hex(&r.hash).ok())
.collect();
let covered = unique_event_hashes.intersection(&row_hashes).count();
assert!(
covered * 10 >= unique_event_hashes.len() * 9,
"tx_rows must cover ≥90% of unique event tx_hashes \
(got {}/{})",
covered,
unique_event_hashes.len()
);
let meta_txs = derive_meta_txs_from_page(address, &page, &abi).await;
let calls = build_contract_calls_from_pf_rows(address, &page.tx_rows, &abi).await;
let tx_summaries = build_tx_summaries_from_pf_rows(&page.tx_rows, &abi).await;
println!(
"Derivations: {} meta-txs, {} calls, {} tx_summaries",
meta_txs.len(),
calls.len(),
tx_summaries.len()
);
let tx_row_hashes: std::collections::HashSet<Felt> = row_hashes.clone();
for m in &meta_txs {
assert!(
tx_row_hashes.contains(&m.hash),
"meta-tx {:#x} not in tx_rows — classified from thin air",
m.hash
);
}
let call_hashes: std::collections::HashSet<Felt> =
calls.iter().map(|c| c.tx_hash).collect();
for h in &call_hashes {
assert!(
tx_row_hashes.contains(h),
"call hash {:#x} not derived from tx_rows",
h
);
}
let meta_hash_set: std::collections::HashSet<Felt> =
meta_txs.iter().map(|m| m.hash).collect();
let missing_from_calls: Vec<Felt> =
meta_hash_set.difference(&call_hashes).copied().collect();
assert!(
missing_from_calls.is_empty(),
"{} meta-tx(es) missing from Calls derivation (Bug 3): {:?}",
missing_from_calls.len(),
missing_from_calls.iter().take(3).collect::<Vec<_>>()
);
if meta_txs.is_empty() {
println!(
"WARN: no meta-txs found in this page — pick a fresher page \
or a different test address if this becomes common."
);
}
}
#[tokio::test]
async fn build_tx_summaries_preserves_rows_without_abi() {
let sender = Felt::from(0xAB01u64);
let rows = vec![
tx_row(Felt::from(1u64), sender, 100, 0, vec![Felt::ZERO]),
tx_row(Felt::from(2u64), sender, 101, 1, vec![Felt::ZERO]),
];
let abi = mk_abi_reg();
let out = build_tx_summaries_from_pf_rows(&rows, &abi).await;
assert_eq!(out.len(), 2, "each parseable row produces one summary");
assert_eq!(out[0].hash, Felt::from(1u64));
assert_eq!(out[0].block_number, 100);
assert_eq!(out[1].hash, Felt::from(2u64));
assert_eq!(out[1].block_number, 101);
assert_eq!(out[0].sender, Some(sender));
}
#[tokio::test]
async fn classify_activity_page_fans_out_sender_and_call() {
let addr =
Felt::from_hex("0x3a496b92d292386ad70dab94ae181a06d289440e3b632a2435721b4280874c4")
.unwrap();
let tx_hash = Felt::from(0xABCDu64);
let calldata = vec![Felt::from(1u64), addr, Felt::from(0x22u64), Felt::ZERO];
let page = AddressActivityPage {
events: vec![ev(tx_hash, 100, 0)],
tx_rows: vec![tx_row(tx_hash, addr, 100, 5, calldata)],
unique_hashes: vec![tx_hash],
tx_block_map: std::collections::HashMap::from([(tx_hash, 100u64)]),
next_token: None,
};
let abi = mk_abi_reg();
let classified = classify_activity_page(addr, &page, &abi).await;
assert_eq!(
classified.txs.len(),
1,
"sender == address ⇒ one tx summary"
);
assert_eq!(classified.txs[0].hash, tx_hash);
assert_eq!(classified.txs[0].sender, Some(addr));
assert_eq!(
classified.calls.len(),
1,
"inner call to address ⇒ one call summary"
);
assert_eq!(classified.calls[0].tx_hash, tx_hash);
assert_eq!(classified.calls[0].sender, addr);
assert!(
classified.meta_txs.is_empty(),
"no outside_execution shape ⇒ no meta-tx classification, got: {:?}",
classified.meta_txs
);
}
#[tokio::test]
async fn classify_activity_page_empty_page_yields_empty_projections() {
let addr =
Felt::from_hex("0x3a496b92d292386ad70dab94ae181a06d289440e3b632a2435721b4280874c4")
.unwrap();
let page = AddressActivityPage {
events: vec![],
tx_rows: vec![],
unique_hashes: vec![],
tx_block_map: std::collections::HashMap::new(),
next_token: None,
};
let abi = mk_abi_reg();
let classified = classify_activity_page(addr, &page, &abi).await;
assert!(classified.txs.is_empty());
assert!(classified.calls.is_empty());
assert!(classified.meta_txs.is_empty());
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::data::rpc::RpcDataSource;
use starknet::core::types::Felt;
#[test]
fn incomplete_call_rows_are_only_unfetched_stubs() {
fn row(sender: Felt, fee: u128, name: &str) -> crate::data::types::ContractCallSummary {
crate::data::types::ContractCallSummary {
tx_hash: Felt::from(1u64),
sender,
function_name: name.to_string(),
block_number: 100,
timestamp: 1000,
total_fee_fri: fee,
status: "OK".to_string(),
nonce: Some(1),
tip: 0,
inner_targets: Vec::new(),
}
}
assert!(call_row_is_incomplete(&row(Felt::ZERO, 0, "")));
assert!(!call_row_is_incomplete(&row(
Felt::from(0x1234u64),
5000,
"execute_from_outside_v2"
)));
assert!(!call_row_is_incomplete(&row(
Felt::from(0x1234u64),
0,
"handle_token_deposit"
)));
}
#[test]
fn fresh_meta_tx_hashes_dedups_across_rungs() {
fn row(h: Felt) -> crate::data::types::ContractCallSummary {
crate::data::types::ContractCallSummary {
tx_hash: h,
sender: Felt::ZERO,
function_name: String::new(),
block_number: 0,
timestamp: 0,
total_fee_fri: 0,
status: "OK".into(),
nonce: None,
tip: 0,
inner_targets: Vec::new(),
}
}
let (a, b, c) = (Felt::from(1u32), Felt::from(2u32), Felt::from(3u32));
let mut seen = std::collections::HashSet::new();
let narrow = vec![row(a), row(b), row(Felt::ZERO), row(a)];
assert_eq!(fresh_meta_tx_hashes(&narrow, &mut seen), vec![a, b]);
let wide = vec![row(a), row(b), row(c)];
assert_eq!(fresh_meta_tx_hashes(&wide, &mut seen), vec![c]);
assert_eq!(seen.len(), 3);
}
fn rpc_ds() -> Arc<dyn DataSource> {
dotenvy::dotenv().ok();
let rpc_url =
std::env::var("APP_RPC_URL").expect("APP_RPC_URL must be set for integration tests");
Arc::new(RpcDataSource::new(&rpc_url))
}
const WELL_KNOWN_ADDRESSES: &[(&str, &str)] = &[(
"0x00000005dd3d2f4429af886cd1a3b08289dbcea99a294197e9eb43b0e0325b4b",
"Ekubo Core",
)];
#[tokio::test]
#[ignore = "requires APP_RPC_URL"]
async fn fetch_balances_for_well_known_addresses() {
let ds = rpc_ds();
for (hex, label) in WELL_KNOWN_ADDRESSES {
let address = Felt::from_hex(hex).unwrap();
let balances = fetch_token_balances(address, &ds).await;
println!("{} ({}): {} non-zero balances", label, hex, balances.len());
for b in &balances {
println!(" {} = {:#x}", b.token_name, b.balance_raw);
}
assert!(
!balances.is_empty(),
"{} should have at least one non-zero token balance",
label
);
for b in &balances {
assert_ne!(
crate::utils::felt_to_u128(&b.balance_raw),
0,
"{}: {} had zero balance but was returned",
label,
b.token_name
);
}
}
}
const DEPLOY_TS: u64 = 1_745_193_600;
fn deploy_min_date() -> chrono::NaiveDate {
chrono::NaiveDate::from_ymd_opt(2025, 4, 20).unwrap()
}
#[test]
fn warm_cache_uses_top_delta_with_date_hint() {
let plan = pick_calls_dune_query(Some((9_148_000, DEPLOY_TS)), None, None);
assert_eq!(
plan,
CallsDuneQuery::TopDelta {
from_block: 9_148_001,
min_date: Some(deploy_min_date()),
}
);
}
#[test]
fn warm_cache_with_zero_timestamp_drops_date_hint() {
let plan = pick_calls_dune_query(Some((9_148_000, 0)), Some(9_013_975), Some(DEPLOY_TS));
assert_eq!(
plan,
CallsDuneQuery::TopDelta {
from_block: 9_148_001,
min_date: None,
}
);
}
#[test]
fn warm_cache_ignores_deploy_floor() {
let plan = pick_calls_dune_query(
Some((9_148_000, DEPLOY_TS)),
Some(9_013_975),
Some(DEPLOY_TS),
);
match plan {
CallsDuneQuery::TopDelta { from_block, .. } => assert_eq!(from_block, 9_148_001),
other => panic!("expected TopDelta, got {:?}", other),
}
}
#[test]
fn cold_cache_with_deploy_info_uses_deploy_scoped() {
let plan = pick_calls_dune_query(None, Some(9_013_975), Some(DEPLOY_TS));
assert_eq!(
plan,
CallsDuneQuery::DeployScoped {
from_block: 9_013_975,
min_date: deploy_min_date(),
}
);
}
#[test]
fn cold_cache_without_deploy_floor_falls_back_to_unwindowed() {
let plan = pick_calls_dune_query(None, None, None);
assert_eq!(plan, CallsDuneQuery::Unwindowed);
}
#[test]
fn calls_page_coverage_claims_only_what_rows_prove() {
let page = |from, to, limit| CallsPage {
from,
to,
limit,
track: true,
};
assert_eq!(
calls_page_coverage(page(Some(100), None, 500), &[300, 150]),
Some((100, 299))
);
assert_eq!(
calls_page_coverage(page(Some(100), None, 3), &[300, 200, 150]),
Some((151, 299))
);
assert_eq!(calls_page_coverage(page(Some(100), None, 500), &[]), None);
assert_eq!(
calls_page_coverage(page(Some(100), None, 500), &[100]),
None
);
assert_eq!(
calls_page_coverage(page(Some(100), Some(200), 500), &[]),
Some((100, 200))
);
assert_eq!(
calls_page_coverage(page(Some(100), Some(200), 2), &[180, 150]),
Some((151, 200))
);
assert_eq!(
calls_page_coverage(page(None, None, 500), &[300, 150]),
None
);
assert_eq!(
calls_page_coverage(page(None, None, 2), &[300, 150]),
Some((151, 299))
);
}
#[test]
fn cold_cache_with_floor_but_no_timestamp_falls_back() {
let plan = pick_calls_dune_query(None, Some(9_013_975), None);
assert_eq!(plan, CallsDuneQuery::Unwindowed);
let plan_zero_ts = pick_calls_dune_query(None, Some(9_013_975), Some(0));
assert_eq!(plan_zero_ts, CallsDuneQuery::Unwindowed);
}
#[tokio::test]
#[ignore = "requires APP_RPC_URL"]
async fn batch_call_matches_sequential() {
let ds = rpc_ds();
let balance_of = starknet::core::utils::get_selector_from_name("balanceOf").unwrap();
let address = Felt::from_hex(WELL_KNOWN_ADDRESSES[0].0).unwrap();
let calls: Vec<(Felt, Felt, Vec<Felt>)> = KNOWN_TOKENS
.iter()
.map(|(hex, _, _)| (Felt::from_hex(hex).unwrap(), balance_of, vec![address]))
.collect();
let batched = ds.batch_call_contracts(calls.clone()).await;
assert_eq!(batched.len(), calls.len(), "batch returned wrong count");
for ((contract, selector, calldata), batch_result) in calls.iter().zip(batched.iter()) {
let seq = ds
.call_contract(*contract, *selector, calldata.clone())
.await;
match (batch_result, &seq) {
(Ok(a), Ok(b)) => assert_eq!(a, b, "batched and sequential results diverged"),
(Err(_), Err(_)) => {}
(Ok(_), Err(e)) | (Err(e), Ok(_)) => {
panic!("batched/sequential disagreed on Ok/Err: {}", e);
}
}
}
}
}