Skip to main content

arete_interpreter/
lib.rs

1//! # arete-interpreter
2//!
3//! AST transformation runtime and VM for Arete streaming pipelines.
4//!
5//! This crate provides the core components for processing Solana blockchain
6//! events into typed state projections:
7//!
8//! - **AST Definition** - Type-safe schemas for state and event handlers
9//! - **Bytecode Compiler** - Compiles specs into optimized bytecode  
10//! - **Virtual Machine** - Executes bytecode to process events
11//! - **TypeScript Generation** - Generate client SDKs automatically
12//!
13//! ## Example
14//!
15//! ```rust,ignore
16//! use arete_interpreter::{TypeScriptCompiler, TypeScriptConfig};
17//!
18//! let config = TypeScriptConfig::default();
19//! let compiler = TypeScriptCompiler::new(config);
20//! let typescript = compiler.compile(&spec)?;
21//! ```
22//!
23//! ## Feature Flags
24//!
25//! - `otel` - OpenTelemetry integration for distributed tracing and metrics
26
27pub mod ast;
28pub mod canonical_log;
29pub mod compiler;
30pub mod debugger;
31pub mod event_type_helpers;
32pub mod metrics_context;
33pub mod program_sdk;
34pub mod proto_router;
35pub mod resolvers;
36pub mod runtime_resolvers;
37pub mod runtime_resolvers_factory;
38pub mod rust;
39pub mod scheduler;
40pub mod slot_hash_cache;
41pub mod spec_trait;
42pub mod typescript;
43pub mod typescript_instructions;
44pub mod versioned;
45pub mod vm;
46pub mod vm_metrics;
47
48// Re-export slot hash cache functions
49pub use slot_hash_cache::{get_slot_hash, record_slot_hash};
50
51pub use canonical_log::{CanonicalLog, LogLevel};
52pub use debugger::{VmDebugEvent, VmDebugger, VmLookupHop};
53pub use metrics_context::{FieldAccessor, FieldRef, MetricsContext};
54pub use resolvers::{
55    InstructionContext, KeyResolution, ResolveContext, ReverseLookupUpdater, TokenMetadata,
56};
57pub use runtime_resolvers::{
58    InProcessResolver, ResolverApplyFuture, ResolverBatchFuture, ResolverBatchResult,
59    RuntimeResolver, RuntimeResolverBatchRequest, RuntimeResolverBatchResponse,
60    RuntimeResolverRequest, RuntimeResolverResponse, SharedRuntimeResolver,
61};
62pub use typescript::{write_typescript_to_file, TypeScriptCompiler, TypeScriptConfig};
63pub use vm::{
64    CapacityWarning, CleanupResult, DirtyTracker, FieldChange, PendingAccountUpdate,
65    PendingQueueStats, QueuedAccountUpdate, ResolverRequest, ResolverTarget, ScheduledCallback,
66    StateTableConfig, UpdateContext, VmMemoryStats,
67};
68
69// Re-export macros for convenient use
70// The field! macro is the new recommended way to create field references
71// The field_accessor! macro is kept for backward compatibility
72
73use serde::{Deserialize, Serialize};
74use serde_json::Value;
75
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct Mutation {
78    pub export: String,
79    pub key: Value,
80    pub patch: Value,
81    #[serde(skip_serializing_if = "Vec::is_empty", default)]
82    pub append: Vec<String>,
83}
84
85/// Generic wrapper for event data that includes context metadata
86/// This ensures type safety for events captured in entity specs
87///
88/// # Runtime Structure
89/// Events captured with `#[event]` are automatically wrapped in this structure:
90/// ```json
91/// {
92///   "timestamp": 1234567890,
93///   "data": { /* event-specific data */ },
94///   "slot": 381471241,
95///   "signature": "4xNEYTVL8DB28W87..."
96/// }
97/// ```
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct EventWrapper<T = Value> {
100    /// Unix timestamp when the event was processed
101    pub timestamp: i64,
102    /// The event-specific data
103    pub data: T,
104    /// Optional slot number from UpdateContext
105    #[serde(skip_serializing_if = "Option::is_none")]
106    pub slot: Option<u64>,
107    /// Optional transaction signature from UpdateContext
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub signature: Option<String>,
110}
111
112/// Generic wrapper for account capture data that includes context metadata
113/// This ensures type safety for accounts captured with `#[capture]` in entity specs
114///
115/// # Runtime Structure
116/// Accounts captured with `#[capture]` are automatically wrapped in this structure:
117/// ```json
118/// {
119///   "timestamp": 1234567890,
120///   "account_address": "C6P5CpJnYHgpGvCGuXYAWL6guKH5LApn3QwTAZmNUPCj",
121///   "data": { /* account-specific data (filtered, no __ fields) */ },
122///   "slot": 381471241,
123///   "signature": "4xNEYTVL8DB28W87..."
124/// }
125/// ```
126#[derive(Debug, Clone, Serialize, Deserialize)]
127pub struct CaptureWrapper<T = Value> {
128    /// Unix timestamp when the account was captured
129    pub timestamp: i64,
130    /// The account address (base58 encoded public key)
131    pub account_address: String,
132    /// The account data (already filtered to remove internal __ fields)
133    pub data: T,
134    /// Optional slot number from UpdateContext
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub slot: Option<u64>,
137    /// Optional transaction signature from UpdateContext
138    #[serde(skip_serializing_if = "Option::is_none")]
139    pub signature: Option<String>,
140}