use starknet::core::types::Felt;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SnBlock {
pub number: u64,
pub hash: Felt,
pub parent_hash: Felt,
pub timestamp: u64,
pub sequencer_address: Felt,
pub transaction_count: usize,
pub l1_gas_price_fri: u128,
pub l1_gas_price_wei: u128,
pub l2_gas_price_fri: u128,
pub l1_data_gas_price_fri: u128,
pub starknet_version: String,
#[serde(default)]
pub status: String,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum SnTransaction {
Invoke(InvokeTx),
Declare(DeclareTx),
DeployAccount(DeployAccountTx),
L1Handler(L1HandlerTx),
Deploy(DeployTx),
}
impl SnTransaction {
pub fn hash(&self) -> Felt {
match self {
SnTransaction::Invoke(tx) => tx.hash,
SnTransaction::Declare(tx) => tx.hash,
SnTransaction::DeployAccount(tx) => tx.hash,
SnTransaction::L1Handler(tx) => tx.hash,
SnTransaction::Deploy(tx) => tx.hash,
}
}
pub fn sender(&self) -> Felt {
match self {
SnTransaction::Invoke(tx) => tx.sender_address,
SnTransaction::Declare(tx) => tx.sender_address,
SnTransaction::DeployAccount(tx) => tx.contract_address,
SnTransaction::L1Handler(tx) => tx.contract_address,
SnTransaction::Deploy(tx) => tx.contract_address,
}
}
pub fn type_name(&self) -> &'static str {
match self {
SnTransaction::Invoke(_) => "INVOKE",
SnTransaction::Declare(_) => "DECLARE",
SnTransaction::DeployAccount(_) => "DEPLOY_ACCOUNT",
SnTransaction::L1Handler(_) => "L1_HANDLER",
SnTransaction::Deploy(_) => "DEPLOY",
}
}
pub fn actual_fee(&self) -> Option<Felt> {
match self {
SnTransaction::Invoke(tx) => tx.actual_fee,
SnTransaction::Declare(tx) => tx.actual_fee,
SnTransaction::DeployAccount(tx) => tx.actual_fee,
SnTransaction::L1Handler(tx) => tx.actual_fee,
SnTransaction::Deploy(tx) => tx.actual_fee,
}
}
pub fn set_actual_fee(&mut self, fee: Felt) {
match self {
SnTransaction::Invoke(tx) => tx.actual_fee = Some(fee),
SnTransaction::Declare(tx) => tx.actual_fee = Some(fee),
SnTransaction::DeployAccount(tx) => tx.actual_fee = Some(fee),
SnTransaction::L1Handler(tx) => tx.actual_fee = Some(fee),
SnTransaction::Deploy(tx) => tx.actual_fee = Some(fee),
}
}
pub fn tip(&self) -> u64 {
match self {
SnTransaction::Invoke(tx) => tx.tip,
SnTransaction::Declare(tx) => tx.tip,
SnTransaction::DeployAccount(tx) => tx.tip,
SnTransaction::L1Handler(_) | SnTransaction::Deploy(_) => 0,
}
}
pub fn block_number(&self) -> u64 {
match self {
SnTransaction::Invoke(tx) => tx.block_number,
SnTransaction::Declare(tx) => tx.block_number,
SnTransaction::DeployAccount(tx) => tx.block_number,
SnTransaction::L1Handler(tx) => tx.block_number,
SnTransaction::Deploy(tx) => tx.block_number,
}
}
pub fn index(&self) -> u64 {
match self {
SnTransaction::Invoke(tx) => tx.index,
SnTransaction::Declare(tx) => tx.index,
SnTransaction::DeployAccount(tx) => tx.index,
SnTransaction::L1Handler(tx) => tx.index,
SnTransaction::Deploy(tx) => tx.index,
}
}
pub fn nonce(&self) -> Option<Felt> {
match self {
SnTransaction::Invoke(tx) => tx.nonce,
SnTransaction::Declare(_) => None,
SnTransaction::DeployAccount(tx) => tx.nonce,
SnTransaction::L1Handler(tx) => tx.nonce,
SnTransaction::Deploy(_) => None,
}
}
pub fn first_selector(&self) -> Option<Felt> {
match self {
SnTransaction::Invoke(tx) => {
if tx.calldata.len() >= 3 {
Some(tx.calldata[2])
} else {
None
}
}
SnTransaction::L1Handler(tx) => Some(tx.entry_point_selector),
_ => None,
}
}
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct SnResourceBounds {
pub l1_gas_max_amount: u64,
pub l1_gas_max_price: u128,
pub l2_gas_max_amount: u64,
pub l2_gas_max_price: u128,
pub l1_data_gas_max_amount: u64,
pub l1_data_gas_max_price: u128,
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct SnExecutionResources {
pub l1_gas: u64,
pub l2_gas: u64,
pub l1_data_gas: u64,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct InvokeTx {
pub hash: Felt,
pub sender_address: Felt,
pub calldata: Vec<Felt>,
pub nonce: Option<Felt>,
pub version: Felt,
pub actual_fee: Option<Felt>,
pub execution_status: ExecutionStatus,
pub block_number: u64,
pub index: u64,
pub tip: u64,
pub resource_bounds: Option<SnResourceBounds>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DeclareTx {
pub hash: Felt,
pub sender_address: Felt,
pub class_hash: Felt,
pub version: Felt,
pub actual_fee: Option<Felt>,
pub execution_status: ExecutionStatus,
pub block_number: u64,
pub index: u64,
pub tip: u64,
pub resource_bounds: Option<SnResourceBounds>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DeployAccountTx {
pub hash: Felt,
pub contract_address: Felt,
pub class_hash: Felt,
pub constructor_calldata: Vec<Felt>,
#[serde(default)]
pub contract_address_salt: Felt,
pub nonce: Option<Felt>,
pub version: Felt,
pub actual_fee: Option<Felt>,
pub execution_status: ExecutionStatus,
pub block_number: u64,
pub index: u64,
pub tip: u64,
pub resource_bounds: Option<SnResourceBounds>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct L1HandlerTx {
pub hash: Felt,
pub contract_address: Felt,
pub entry_point_selector: Felt,
pub calldata: Vec<Felt>,
pub nonce: Option<Felt>,
pub actual_fee: Option<Felt>,
pub execution_status: ExecutionStatus,
pub block_number: u64,
pub index: u64,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DeployTx {
pub hash: Felt,
pub contract_address: Felt,
pub class_hash: Felt,
pub constructor_calldata: Vec<Felt>,
pub version: Felt,
pub actual_fee: Option<Felt>,
pub execution_status: ExecutionStatus,
pub block_number: u64,
pub index: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ExecutionStatus {
Succeeded,
Reverted(String),
Unknown,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SnReceipt {
pub transaction_hash: Felt,
pub block_number: u64,
pub block_hash: Option<Felt>,
pub actual_fee: Felt,
pub fee_unit: String,
pub execution_status: ExecutionStatus,
pub execution_resources: SnExecutionResources,
pub events: Vec<SnEvent>,
pub revert_reason: Option<String>,
pub finality: String, }
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SnEvent {
pub from_address: Felt,
pub keys: Vec<Felt>,
pub data: Vec<Felt>,
pub transaction_hash: Felt,
pub block_number: u64,
pub event_index: u64,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AddressTxSummary {
pub hash: Felt,
pub nonce: u64,
pub block_number: u64,
pub timestamp: u64,
pub endpoint_names: String, pub total_fee_fri: u128,
pub tip: u64,
pub tx_type: String,
pub status: String, #[serde(default)]
pub sender: Option<Felt>,
#[serde(default)]
pub called_contracts: Vec<Felt>,
}
impl AddressTxSummary {
pub fn needs_enrichment(&self) -> bool {
self.timestamp == 0 || (self.tx_type == "INVOKE" && self.endpoint_names.is_empty())
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MetaTxIntenderSummary {
pub hash: Felt,
pub block_number: u64,
pub tx_index: u64,
pub timestamp: u64,
pub paymaster: Felt,
pub version: String,
pub oe_nonce: Felt,
pub total_fee_fri: u128,
pub status: String,
pub inner_targets: Vec<Felt>,
pub inner_endpoints: String,
pub caller: Felt,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ContractCallSummary {
pub tx_hash: Felt,
pub sender: Felt,
pub function_name: String,
pub block_number: u64,
pub timestamp: u64,
pub total_fee_fri: u128,
pub status: String,
#[serde(default)]
pub nonce: Option<u64>,
#[serde(default)]
pub tip: u64,
#[serde(default)]
pub inner_targets: Vec<Felt>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct VoyagerLabelInfo {
pub name: Option<String>,
pub class_alias: Option<String>,
#[serde(default)]
pub deploy_block: Option<u64>,
}
pub fn merge_call_into(existing: &mut ContractCallSummary, incoming: ContractCallSummary) -> bool {
let mut changed = false;
for name in incoming.function_name.split(", ").filter(|s| !s.is_empty()) {
if !existing.function_name.split(", ").any(|n| n == name) {
if !existing.function_name.is_empty() {
existing.function_name.push_str(", ");
}
existing.function_name.push_str(name);
changed = true;
}
}
if existing.sender == Felt::ZERO && incoming.sender != Felt::ZERO {
existing.sender = incoming.sender;
changed = true;
}
if existing.total_fee_fri == 0 && incoming.total_fee_fri > 0 {
existing.total_fee_fri = incoming.total_fee_fri;
changed = true;
}
if existing.timestamp == 0 && incoming.timestamp > 0 {
existing.timestamp = incoming.timestamp;
changed = true;
}
if existing.status == "?" && incoming.status != "?" {
existing.status = incoming.status;
changed = true;
}
if existing.nonce.is_none() && incoming.nonce.is_some() {
existing.nonce = incoming.nonce;
changed = true;
}
if existing.tip == 0 && incoming.tip > 0 {
existing.tip = incoming.tip;
changed = true;
}
if existing.inner_targets.is_empty() && !incoming.inner_targets.is_empty() {
existing.inner_targets = incoming.inner_targets;
changed = true;
}
changed
}
pub fn deduplicate_contract_calls(calls: Vec<ContractCallSummary>) -> Vec<ContractCallSummary> {
use std::collections::HashMap;
let mut seen: HashMap<Felt, usize> = HashMap::new();
let mut result: Vec<ContractCallSummary> = Vec::new();
for call in calls {
if let Some(&idx) = seen.get(&call.tx_hash) {
merge_call_into(&mut result[idx], call);
} else {
seen.insert(call.tx_hash, result.len());
result.push(call);
}
}
result
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SnAddressInfo {
pub address: Felt,
pub nonce: Felt,
pub class_hash: Option<Felt>,
pub recent_events: Vec<SnEvent>,
pub token_balances: Vec<TokenBalance>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TokenBalance {
pub token_address: Felt,
pub token_name: String,
pub balance_raw: Felt,
pub decimals: u8,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TokenTransfer {
pub token: Felt,
pub from: Felt,
pub to: Felt,
pub amount_low: u128,
pub amount_high: u128,
pub tx_hash: Felt,
pub block_number: u64,
#[serde(default)]
pub tx_index: u64,
pub event_index: u64,
#[serde(default)]
pub timestamp: u64,
#[serde(default)]
pub symbol: Option<String>,
#[serde(default)]
pub decimals: Option<u8>,
#[serde(default)]
pub value_usd: Option<f64>,
}
impl TokenTransfer {
pub fn key(&self) -> (Felt, u64) {
(self.tx_hash, self.event_index)
}
pub fn from_event(event: &SnEvent, tx_index: u64) -> Option<Self> {
let selector = Felt::from_hex(crate::data::pathfinder::TRANSFER_SELECTOR).ok()?;
if event.keys.first() != Some(&selector) {
return None;
}
let (from, to, low, high) = match event.keys.len() {
n if n >= 3 => (
event.keys[1],
event.keys[2],
*event.data.first()?,
event.data.get(1).copied().unwrap_or(Felt::ZERO),
),
1 if event.data.len() >= 3 => (
event.data[0],
event.data[1],
event.data[2],
event.data.get(3).copied().unwrap_or(Felt::ZERO),
),
_ => return None,
};
Some(Self {
token: event.from_address,
from,
to,
amount_low: crate::utils::felt_to_u128(&low),
amount_high: crate::utils::felt_to_u128(&high),
tx_hash: event.transaction_hash,
block_number: event.block_number,
tx_index,
event_index: event.event_index,
timestamp: 0,
symbol: None,
decimals: None,
value_usd: None,
})
}
pub fn cmp_newest_first(a: &Self, b: &Self) -> std::cmp::Ordering {
b.block_number
.cmp(&a.block_number)
.then(b.tx_index.cmp(&a.tx_index))
.then(b.event_index.cmp(&a.event_index))
}
}
#[derive(Debug, Clone)]
pub struct ClassContractEntry {
pub address: Felt,
pub block_number: u64,
}
#[derive(Debug, Clone)]
pub struct ClassDeclareInfo {
pub tx_hash: Felt,
pub sender: Felt,
pub block_number: u64,
pub timestamp: u64,
}