pub mod abi;
pub mod calldata;
pub mod class_cache;
pub mod events;
pub mod functions;
pub mod outside_execution;
pub mod privacy;
pub mod privacy_crypto;
pub mod privacy_sync;
pub mod trace;
use std::sync::Arc;
use starknet::core::types::Felt;
use tracing::{debug, warn};
use crate::data::DataSource;
use abi::{ABI_SCHEMA_VERSION, ParsedAbi};
use class_cache::ClassCache;
pub struct AbiRegistry {
data_source: Arc<dyn DataSource>,
cache: ClassCache,
}
impl AbiRegistry {
pub fn new(data_source: Arc<dyn DataSource>, cache: ClassCache) -> Self {
Self { data_source, cache }
}
pub async fn get_abi_for_address(&self, address: &Felt) -> Option<Arc<ParsedAbi>> {
let class_hash = match self.data_source.get_class_hash(*address).await {
Ok(h) => h,
Err(e) => {
debug!(address = %format!("{:#x}", address), error = %e, "Failed to get class hash");
return None;
}
};
self.get_abi_for_class(&class_hash).await
}
pub async fn get_abi_for_class(&self, class_hash: &Felt) -> Option<Arc<ParsedAbi>> {
let cached = self.cache.get(class_hash);
if let Some(ref abi) = cached {
if abi.schema_version >= ABI_SCHEMA_VERSION {
return cached;
}
debug!(class_hash = %format!("{:#x}", class_hash), "Stale ABI cache entry, re-fetching");
}
let class = match self.data_source.get_class(*class_hash).await {
Ok(c) => c,
Err(e) => {
warn!(class_hash = %format!("{:#x}", class_hash), error = %e, "Failed to fetch class");
return cached;
}
};
let parsed = abi::parse_contract_class(&class);
if parsed.is_empty() {
debug!(class_hash = %format!("{:#x}", class_hash), "ABI is empty");
}
self.cache.index_abi_selectors(&parsed);
let arc = Arc::new(parsed);
self.cache.put(*class_hash, Arc::clone(&arc));
Some(arc)
}
pub fn get_selector_name(&self, selector: &Felt) -> Option<String> {
self.cache.get_selector_name(selector)
}
}