Skip to main content

hyperstack_interpreter/
resolvers.rs

1use std::collections::{BTreeMap, HashMap, HashSet};
2use std::sync::OnceLock;
3
4use futures::future::join_all;
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8
9/// Context provided to primary key resolver functions
10pub struct ResolveContext<'a> {
11    #[allow(dead_code)]
12    pub(crate) state_id: u32,
13    pub(crate) slot: u64,
14    pub(crate) signature: String,
15    pub(crate) reverse_lookups:
16        &'a mut std::collections::HashMap<String, crate::vm::PdaReverseLookup>,
17}
18
19impl<'a> ResolveContext<'a> {
20    /// Create a new ResolveContext (primarily for use by generated code)
21    pub fn new(
22        state_id: u32,
23        slot: u64,
24        signature: String,
25        reverse_lookups: &'a mut std::collections::HashMap<String, crate::vm::PdaReverseLookup>,
26    ) -> Self {
27        Self {
28            state_id,
29            slot,
30            signature,
31            reverse_lookups,
32        }
33    }
34
35    /// Try to reverse lookup a PDA address to find the seed value
36    /// This is typically used to find the primary key from a PDA account address
37    pub fn pda_reverse_lookup(&mut self, pda_address: &str) -> Option<String> {
38        let lookup_name = "default_pda_lookup";
39        self.reverse_lookups
40            .get_mut(lookup_name)
41            .and_then(|t| t.lookup(pda_address))
42    }
43
44    pub fn slot(&self) -> u64 {
45        self.slot
46    }
47
48    pub fn signature(&self) -> &str {
49        &self.signature
50    }
51}
52
53/// Result of attempting to resolve a primary key
54pub enum KeyResolution {
55    /// Primary key successfully resolved
56    Found(String),
57
58    /// Queue this update until we see one of these instruction discriminators
59    /// The discriminators identify which instructions can populate the reverse lookup
60    QueueUntil(&'static [u8]),
61
62    /// Skip this update entirely (don't queue)
63    Skip,
64}
65
66/// Context provided to instruction hook functions
67pub struct InstructionContext<'a> {
68    pub(crate) accounts: HashMap<String, String>,
69    #[allow(dead_code)]
70    pub(crate) state_id: u32,
71    pub(crate) reverse_lookup_tx: &'a mut dyn ReverseLookupUpdater,
72    pub(crate) pending_updates: Vec<crate::vm::PendingAccountUpdate>,
73    pub(crate) registers: Option<&'a mut Vec<crate::vm::RegisterValue>>,
74    pub(crate) state_reg: Option<crate::vm::Register>,
75    #[allow(dead_code)]
76    pub(crate) compiled_paths: Option<&'a HashMap<String, crate::metrics_context::CompiledPath>>,
77    pub(crate) instruction_data: Option<&'a serde_json::Value>,
78    pub(crate) slot: Option<u64>,
79    pub(crate) signature: Option<String>,
80    pub(crate) timestamp: Option<i64>,
81    pub(crate) dirty_tracker: crate::vm::DirtyTracker,
82}
83
84pub trait ReverseLookupUpdater {
85    fn update(
86        &mut self,
87        pda_address: String,
88        seed_value: String,
89    ) -> Vec<crate::vm::PendingAccountUpdate>;
90    fn flush_pending(&mut self, pda_address: &str) -> Vec<crate::vm::PendingAccountUpdate>;
91}
92
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct TokenMetadata {
95    pub mint: String,
96    pub name: Option<String>,
97    pub symbol: Option<String>,
98    pub decimals: Option<u8>,
99    pub logo_uri: Option<String>,
100}
101
102#[derive(Debug, Clone, Copy)]
103pub struct ResolverTypeScriptSchema {
104    pub name: &'static str,
105    pub definition: &'static str,
106}
107
108#[derive(Debug, Clone, Copy)]
109pub struct ResolverComputedMethod {
110    pub name: &'static str,
111    pub arg_count: usize,
112}
113
114pub trait ResolverDefinition: Send + Sync {
115    fn name(&self) -> &'static str;
116    fn output_type(&self) -> &'static str;
117    fn computed_methods(&self) -> &'static [ResolverComputedMethod];
118    fn evaluate_computed(
119        &self,
120        method: &str,
121        args: &[Value],
122    ) -> std::result::Result<Value, Box<dyn std::error::Error>>;
123    fn typescript_interface(&self) -> Option<&'static str> {
124        None
125    }
126    fn typescript_schema(&self) -> Option<ResolverTypeScriptSchema> {
127        None
128    }
129    fn extra_output_types(&self) -> &'static [&'static str] {
130        &[]
131    }
132}
133
134pub struct ResolverRegistry {
135    resolvers: BTreeMap<String, Box<dyn ResolverDefinition>>,
136}
137
138impl Default for ResolverRegistry {
139    fn default() -> Self {
140        Self::new()
141    }
142}
143
144impl ResolverRegistry {
145    pub fn new() -> Self {
146        Self {
147            resolvers: BTreeMap::new(),
148        }
149    }
150
151    pub fn register(&mut self, resolver: Box<dyn ResolverDefinition>) {
152        self.resolvers.insert(resolver.name().to_string(), resolver);
153    }
154
155    pub fn resolver(&self, name: &str) -> Option<&dyn ResolverDefinition> {
156        self.resolvers.get(name).map(|resolver| resolver.as_ref())
157    }
158
159    pub fn definitions(&self) -> impl Iterator<Item = &dyn ResolverDefinition> {
160        self.resolvers.values().map(|resolver| resolver.as_ref())
161    }
162
163    pub fn is_output_type(&self, type_name: &str) -> bool {
164        self.resolvers.values().any(|resolver| {
165            resolver.output_type() == type_name
166                || resolver.extra_output_types().contains(&type_name)
167        })
168    }
169
170    pub fn evaluate_computed(
171        &self,
172        resolver: &str,
173        method: &str,
174        args: &[Value],
175    ) -> std::result::Result<Value, Box<dyn std::error::Error>> {
176        let resolver_impl = self
177            .resolver(resolver)
178            .ok_or_else(|| format!("Unknown resolver '{}'", resolver))?;
179
180        let method_spec = resolver_impl
181            .computed_methods()
182            .iter()
183            .find(|spec| spec.name == method)
184            .ok_or_else(|| {
185                format!(
186                    "Resolver '{}' does not provide method '{}'",
187                    resolver, method
188                )
189            })?;
190
191        if method_spec.arg_count != args.len() {
192            return Err(format!(
193                "Resolver '{}' method '{}' expects {} args, got {}",
194                resolver,
195                method,
196                method_spec.arg_count,
197                args.len()
198            )
199            .into());
200        }
201
202        resolver_impl.evaluate_computed(method, args)
203    }
204
205    pub fn validate_computed_expr(
206        &self,
207        expr: &crate::ast::ComputedExpr,
208        errors: &mut Vec<String>,
209    ) {
210        match expr {
211            crate::ast::ComputedExpr::ResolverComputed {
212                resolver,
213                method,
214                args,
215            } => {
216                let resolver_impl = self.resolver(resolver);
217                if resolver_impl.is_none() {
218                    errors.push(format!("Unknown resolver '{}'", resolver));
219                } else if let Some(resolver_impl) = resolver_impl {
220                    let method_spec = resolver_impl
221                        .computed_methods()
222                        .iter()
223                        .find(|spec| spec.name == method);
224                    if let Some(method_spec) = method_spec {
225                        if method_spec.arg_count != args.len() {
226                            errors.push(format!(
227                                "Resolver '{}' method '{}' expects {} args, got {}",
228                                resolver,
229                                method,
230                                method_spec.arg_count,
231                                args.len()
232                            ));
233                        }
234                    } else {
235                        errors.push(format!(
236                            "Resolver '{}' does not provide method '{}'",
237                            resolver, method
238                        ));
239                    }
240                }
241
242                for arg in args {
243                    self.validate_computed_expr(arg, errors);
244                }
245            }
246            crate::ast::ComputedExpr::FieldRef { .. }
247            | crate::ast::ComputedExpr::Literal { .. }
248            | crate::ast::ComputedExpr::None
249            | crate::ast::ComputedExpr::Var { .. }
250            | crate::ast::ComputedExpr::ByteArray { .. }
251            | crate::ast::ComputedExpr::ContextSlot
252            | crate::ast::ComputedExpr::ContextTimestamp => {}
253            crate::ast::ComputedExpr::UnwrapOr { expr, .. }
254            | crate::ast::ComputedExpr::Cast { expr, .. }
255            | crate::ast::ComputedExpr::Paren { expr }
256            | crate::ast::ComputedExpr::Some { value: expr }
257            | crate::ast::ComputedExpr::Slice { expr, .. }
258            | crate::ast::ComputedExpr::Index { expr, .. }
259            |             crate::ast::ComputedExpr::U64FromLeBytes { bytes: expr }
260            | crate::ast::ComputedExpr::U64FromBeBytes { bytes: expr }
261            | crate::ast::ComputedExpr::JsonToBytes { expr }
262            | crate::ast::ComputedExpr::Keccak256 { expr }
263            | crate::ast::ComputedExpr::Unary { expr, .. } => {
264                self.validate_computed_expr(expr, errors);
265            }
266            crate::ast::ComputedExpr::Binary { left, right, .. } => {
267                self.validate_computed_expr(left, errors);
268                self.validate_computed_expr(right, errors);
269            }
270            crate::ast::ComputedExpr::MethodCall { expr, args, .. } => {
271                self.validate_computed_expr(expr, errors);
272                for arg in args {
273                    self.validate_computed_expr(arg, errors);
274                }
275            }
276            crate::ast::ComputedExpr::Let { value, body, .. } => {
277                self.validate_computed_expr(value, errors);
278                self.validate_computed_expr(body, errors);
279            }
280            crate::ast::ComputedExpr::If {
281                condition,
282                then_branch,
283                else_branch,
284            } => {
285                self.validate_computed_expr(condition, errors);
286                self.validate_computed_expr(then_branch, errors);
287                self.validate_computed_expr(else_branch, errors);
288            }
289            crate::ast::ComputedExpr::Closure { body, .. } => {
290                self.validate_computed_expr(body, errors);
291            }
292        }
293    }
294}
295
296static BUILTIN_RESOLVER_REGISTRY: OnceLock<ResolverRegistry> = OnceLock::new();
297
298pub fn register_builtin_resolvers(registry: &mut ResolverRegistry) {
299    registry.register(Box::new(SlotHashResolver));
300    registry.register(Box::new(TokenMetadataResolver));
301}
302
303pub fn builtin_resolver_registry() -> &'static ResolverRegistry {
304    BUILTIN_RESOLVER_REGISTRY.get_or_init(|| {
305        let mut registry = ResolverRegistry::new();
306        register_builtin_resolvers(&mut registry);
307        registry
308    })
309}
310
311pub fn evaluate_resolver_computed(
312    resolver: &str,
313    method: &str,
314    args: &[Value],
315) -> std::result::Result<Value, Box<dyn std::error::Error>> {
316    builtin_resolver_registry().evaluate_computed(resolver, method, args)
317}
318
319pub fn validate_resolver_computed_specs(
320    specs: &[crate::ast::ComputedFieldSpec],
321) -> std::result::Result<(), Box<dyn std::error::Error>> {
322    let registry = builtin_resolver_registry();
323    let mut errors = Vec::new();
324
325    for spec in specs {
326        registry.validate_computed_expr(&spec.expression, &mut errors);
327    }
328
329    if errors.is_empty() {
330        Ok(())
331    } else {
332        Err(errors.join("\n").into())
333    }
334}
335
336pub fn is_resolver_output_type(type_name: &str) -> bool {
337    builtin_resolver_registry().is_output_type(type_name)
338}
339
340const DEFAULT_DAS_BATCH_SIZE: usize = 100;
341const DEFAULT_DAS_TIMEOUT_SECS: u64 = 10;
342const DAS_API_ENDPOINT_ENV: &str = "DAS_API_ENDPOINT";
343const DAS_API_BATCH_ENV: &str = "DAS_API_BATCH_SIZE";
344
345pub struct TokenMetadataResolverClient {
346    endpoint: String,
347    client: reqwest::Client,
348    batch_size: usize,
349}
350
351impl TokenMetadataResolverClient {
352    pub fn new(
353        endpoint: String,
354        batch_size: usize,
355    ) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
356        let client = reqwest::Client::builder()
357            .timeout(std::time::Duration::from_secs(DEFAULT_DAS_TIMEOUT_SECS))
358            .build()?;
359
360        Ok(Self {
361            endpoint,
362            client,
363            batch_size: batch_size.max(1),
364        })
365    }
366
367    pub fn from_env(
368    ) -> Result<Option<Self>, Box<dyn std::error::Error + Send + Sync>> {
369        let Some(endpoint) = std::env::var(DAS_API_ENDPOINT_ENV).ok() else {
370            return Ok(None);
371        };
372
373        let batch_size = std::env::var(DAS_API_BATCH_ENV)
374            .ok()
375            .and_then(|value| value.parse::<usize>().ok())
376            .unwrap_or(DEFAULT_DAS_BATCH_SIZE);
377
378        Ok(Some(Self::new(endpoint, batch_size)?))
379    }
380
381    pub async fn resolve_token_metadata(
382        &self,
383        mints: &[String],
384    ) -> Result<HashMap<String, Value>, Box<dyn std::error::Error + Send + Sync>> {
385        let mut unique = HashSet::new();
386        let mut deduped = Vec::new();
387
388        for mint in mints {
389            if mint.is_empty() {
390                continue;
391            }
392            if unique.insert(mint.clone()) {
393                deduped.push(mint.clone());
394            }
395        }
396
397        let mut results = HashMap::new();
398        if deduped.is_empty() {
399            return Ok(results);
400        }
401
402        for chunk in deduped.chunks(self.batch_size) {
403            let assets = self.fetch_assets(chunk).await?;
404            for asset in assets {
405                if let Some((mint, value)) = Self::build_token_metadata(&asset) {
406                    results.insert(mint, value);
407                }
408            }
409        }
410
411        Ok(results)
412    }
413
414    async fn fetch_assets(
415        &self,
416        ids: &[String],
417    ) -> Result<Vec<Value>, Box<dyn std::error::Error + Send + Sync>> {
418        let payload = serde_json::json!({
419            "jsonrpc": "2.0",
420            "id": "1",
421            "method": "getAssetBatch",
422            "params": {
423                "ids": ids,
424                "options": {
425                    "showFungible": true,
426                },
427            },
428        });
429
430        let response = self.client.post(&self.endpoint).json(&payload).send().await?;
431        let response = response.error_for_status()?;
432        let value = response.json::<Value>().await?;
433
434        if let Some(error) = value.get("error") {
435            return Err(format!("Resolver response error: {}", error).into());
436        }
437
438        let assets = value
439            .get("result")
440            .and_then(|result| match result {
441                Value::Array(items) => Some(items.clone()),
442                Value::Object(obj) => obj
443                    .get("items")
444                    .and_then(|items| items.as_array())
445                    .cloned(),
446                _ => None,
447            })
448            .ok_or_else(|| "Resolver response missing result".to_string())?;
449
450        let assets = assets.into_iter().filter(|a| !a.is_null()).collect();
451        Ok(assets)
452    }
453
454    fn build_token_metadata(asset: &Value) -> Option<(String, Value)> {
455        let mint = asset.get("id").and_then(|value| value.as_str())?.to_string();
456
457        let name = asset
458            .pointer("/content/metadata/name")
459            .and_then(|value| value.as_str());
460
461        let symbol = asset
462            .pointer("/content/metadata/symbol")
463            .and_then(|value| value.as_str());
464
465        let token_info = asset.get("token_info").or_else(|| asset.pointer("/content/token_info"));
466
467        let decimals = token_info
468            .and_then(|info| info.get("decimals"))
469            .and_then(|value| value.as_u64());
470
471        let logo_uri = asset
472            .pointer("/content/links/image")
473            .and_then(|value| value.as_str())
474            .or_else(|| asset.pointer("/content/links/image_uri").and_then(|value| value.as_str()));
475
476        let mut obj = serde_json::Map::new();
477        obj.insert("mint".to_string(), serde_json::json!(mint));
478        obj.insert(
479            "name".to_string(),
480            name.map(|value| serde_json::json!(value))
481                .unwrap_or(Value::Null),
482        );
483        obj.insert(
484            "symbol".to_string(),
485            symbol.map(|value| serde_json::json!(value))
486                .unwrap_or(Value::Null),
487        );
488        obj.insert(
489            "decimals".to_string(),
490            decimals
491                .map(|value| serde_json::json!(value))
492                .unwrap_or(Value::Null),
493        );
494        obj.insert(
495            "logo_uri".to_string(),
496            logo_uri
497                .map(|value| serde_json::json!(value))
498                .unwrap_or(Value::Null),
499        );
500
501        Some((mint, Value::Object(obj)))
502    }
503}
504
505// ============================================================================
506// URL Resolver Client - Fetch and parse data from external URLs
507// ============================================================================
508
509const DEFAULT_URL_TIMEOUT_SECS: u64 = 30;
510
511pub struct UrlResolverClient {
512    client: reqwest::Client,
513}
514
515impl Default for UrlResolverClient {
516    fn default() -> Self {
517        Self::new()
518    }
519}
520
521impl UrlResolverClient {
522    pub fn new() -> Self {
523        let client = reqwest::Client::builder()
524            .timeout(std::time::Duration::from_secs(DEFAULT_URL_TIMEOUT_SECS))
525            .build()
526            .expect("Failed to create HTTP client for URL resolver");
527
528        Self { client }
529    }
530
531    pub fn with_timeout(timeout_secs: u64) -> Self {
532        let client = reqwest::Client::builder()
533            .timeout(std::time::Duration::from_secs(timeout_secs))
534            .build()
535            .expect("Failed to create HTTP client for URL resolver");
536
537        Self { client }
538    }
539
540    /// Resolve a URL and return the parsed JSON response
541    pub async fn resolve(
542        &self,
543        url: &str,
544        method: &crate::ast::HttpMethod,
545    ) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
546        if url.is_empty() {
547            return Err("URL is empty".into());
548        }
549
550        let response = match method {
551            crate::ast::HttpMethod::Get => self.client.get(url).send().await?,
552            crate::ast::HttpMethod::Post => self.client.post(url).send().await?,
553        };
554
555        let response = response.error_for_status()?;
556        let value = response.json::<Value>().await?;
557
558        Ok(value)
559    }
560
561    /// Resolve a URL and extract a specific JSON path from the response
562    pub async fn resolve_with_extract(
563        &self,
564        url: &str,
565        method: &crate::ast::HttpMethod,
566        extract_path: Option<&str>,
567    ) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
568        let response = self.resolve(url, method).await?;
569
570        if let Some(path) = extract_path {
571            Self::extract_json_path(&response, path)
572        } else {
573            Ok(response)
574        }
575    }
576
577    /// Extract a value from a JSON object using dot-notation path
578    /// e.g., "data.image" extracts response["data"]["image"]
579    pub fn extract_json_path(
580        value: &Value,
581        path: &str,
582    ) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
583        if path.is_empty() {
584            return Ok(value.clone());
585        }
586
587        let mut current = value;
588        for segment in path.split('.') {
589            // Try as object key first
590            if let Some(next) = current.get(segment) {
591                current = next;
592            } else if let Ok(index) = segment.parse::<usize>() {
593                // Try as array index
594                if let Some(next) = current.get(index) {
595                    current = next;
596                } else {
597                    return Err(format!("Index '{}' out of bounds in path '{}'", index, path).into());
598                }
599            } else {
600                return Err(format!("Key '{}' not found in path '{}'", segment, path).into());
601            }
602        }
603
604        Ok(current.clone())
605    }
606
607    /// Batch resolve multiple URLs in parallel with deduplication.
608    /// Returns raw JSON keyed by URL. Identical URLs are only fetched once.
609    pub async fn resolve_batch(
610        &self,
611        urls: &[(String, crate::ast::HttpMethod)],
612    ) -> HashMap<String, Value> {
613        let mut unique: HashMap<String, crate::ast::HttpMethod> = HashMap::new();
614        for (url, method) in urls {
615            if !url.is_empty() {
616                unique.entry(url.clone()).or_insert_with(|| method.clone());
617            }
618        }
619
620        let futures = unique
621            .into_iter()
622            .map(|(url, method)| async move {
623                let result = self.resolve(&url, &method).await;
624                (url, result)
625            });
626
627        join_all(futures)
628            .await
629            .into_iter()
630            .filter_map(|(url, result)| match result {
631                Ok(value) => Some((url, value)),
632                Err(e) => {
633                    tracing::warn!(url = %url, error = %e, "Failed to resolve URL");
634                    None
635                }
636            })
637            .collect()
638    }
639}
640
641/// Intermediate type for validated URL resolver requests,
642/// avoiding redundant pattern matching after partition.
643struct ValidUrlRequest {
644    url: String,
645    method: crate::ast::HttpMethod,
646    request: crate::vm::ResolverRequest,
647}
648
649/// Resolve a batch of URL resolver requests in parallel with deduplication,
650/// apply results to VM state, and requeue failures.
651///
652/// This is the single entry point for URL resolution at runtime —
653/// it owns the full lifecycle: validate, fetch, apply, requeue.
654pub async fn resolve_url_batch(
655    vm: &std::sync::Mutex<crate::vm::VmContext>,
656    bytecode: &crate::compiler::MultiEntityBytecode,
657    url_client: &UrlResolverClient,
658    requests: Vec<crate::vm::ResolverRequest>,
659) -> Vec<crate::Mutation> {
660    if requests.is_empty() {
661        return Vec::new();
662    }
663
664    // Partition into valid and invalid in a single pass
665    let mut valid = Vec::with_capacity(requests.len());
666    let mut invalid = Vec::new();
667
668    for request in requests {
669        if let crate::ast::ResolverType::Url(ref config) = request.resolver {
670            match &request.input {
671                serde_json::Value::String(s) if !s.is_empty() => {
672                    valid.push(ValidUrlRequest {
673                        url: s.clone(),
674                        method: config.method.clone(),
675                        request,
676                    });
677                }
678                _ => {
679                    tracing::warn!(
680                        "URL resolver input is not a non-empty string: {:?}",
681                        request.input
682                    );
683                    invalid.push(request);
684                }
685            }
686        }
687    }
688
689    if !invalid.is_empty() {
690        let mut vm = vm.lock().unwrap_or_else(|e| e.into_inner());
691        vm.restore_resolver_requests(invalid);
692    }
693
694    if valid.is_empty() {
695        return Vec::new();
696    }
697
698    // Build deduplicated batch input
699    let batch_input: Vec<_> = valid
700        .iter()
701        .map(|v| (v.url.clone(), v.method.clone()))
702        .collect();
703
704    let results = url_client.resolve_batch(&batch_input).await;
705
706    // Apply results to VM state, requeue anything that didn't resolve
707    let mut vm = vm.lock().unwrap_or_else(|e| e.into_inner());
708    let mut mutations = Vec::new();
709    let mut failed = Vec::new();
710
711    for entry in valid {
712        match results.get(&entry.url) {
713            Some(resolved_value) => {
714                match vm.apply_resolver_result(bytecode, &entry.request.cache_key, resolved_value.clone()) {
715                    Ok(mut new_mutations) => {
716                        mutations.append(&mut new_mutations)
717                    }
718                    Err(err) => {
719                        tracing::warn!(url = %entry.url, "Failed to apply URL resolver result: {}", err);
720                    }
721                }
722            }
723            None => {
724                tracing::warn!(url = %entry.url, "URL resolver request failed, re-queuing");
725                failed.push(entry.request);
726            }
727        }
728    }
729
730    if !failed.is_empty() {
731        vm.restore_resolver_requests(failed);
732    }
733
734    mutations
735}
736
737/// Resolver for looking up slot hashes by slot number
738/// Uses the global slot hash cache populated from gRPC stream
739struct SlotHashResolver;
740
741const SLOT_HASH_METHODS: &[ResolverComputedMethod] = &[
742    ResolverComputedMethod {
743        name: "slot_hash",
744        arg_count: 1,
745    },
746    ResolverComputedMethod {
747        name: "keccak_rng",
748        arg_count: 3,
749    },
750];
751
752impl SlotHashResolver {
753    /// Compute keccak256(slot_hash || seed || samples_le_bytes) and XOR-fold into a u64.
754    /// args[0] = slot_hash bytes (JSON array of 32 bytes)
755    /// args[1] = seed bytes (JSON array of 32 bytes)
756    /// args[2] = samples (u64 number)
757    fn evaluate_keccak_rng(args: &[Value]) -> Result<Value, Box<dyn std::error::Error>> {
758        if args.len() != 3 {
759            return Ok(Value::Null);
760        }
761
762        let slot_hash = Self::json_array_to_bytes(&args[0], 32);
763        let seed = Self::json_array_to_bytes(&args[1], 32);
764        let samples = match &args[2] {
765            Value::Number(n) => n.as_u64(),
766            _ => None,
767        };
768
769        let (slot_hash, seed, samples) = match (slot_hash, seed, samples) {
770            (Some(s), Some(sd), Some(sm)) => (s, sd, sm),
771            _ => return Ok(Value::Null),
772        };
773
774        // Build input: slot_hash[32] || seed[32] || samples_le_bytes[8]
775        let mut input = Vec::with_capacity(72);
776        input.extend_from_slice(&slot_hash);
777        input.extend_from_slice(&seed);
778        input.extend_from_slice(&samples.to_le_bytes());
779
780        // keccak256
781        use sha3::{Digest, Keccak256};
782        let hash = Keccak256::digest(&input);
783
784        // XOR-fold four u64 chunks
785        let r1 = u64::from_le_bytes(hash[0..8].try_into()?);
786        let r2 = u64::from_le_bytes(hash[8..16].try_into()?);
787        let r3 = u64::from_le_bytes(hash[16..24].try_into()?);
788        let r4 = u64::from_le_bytes(hash[24..32].try_into()?);
789        let rng = r1 ^ r2 ^ r3 ^ r4;
790
791        Ok(Value::String(rng.to_string()))
792    }
793
794    /// Extract a byte array of expected length from a JSON array value.
795    fn json_array_to_bytes(value: &Value, expected_len: usize) -> Option<Vec<u8>> {
796        let arr = value.as_array()?;
797        let bytes: Vec<u8> = arr
798            .iter()
799            .filter_map(|v| v.as_u64().and_then(|n| u8::try_from(n).ok()))
800            .collect();
801        if bytes.len() == expected_len {
802            Some(bytes)
803        } else {
804            tracing::debug!(
805                got = bytes.len(),
806                expected = expected_len,
807                "json_array_to_bytes: length mismatch or out-of-range element"
808            );
809            None
810        }
811    }
812
813    fn evaluate_slot_hash(args: &[Value]) -> Result<Value, Box<dyn std::error::Error>> {
814        if args.len() != 1 {
815            return Ok(Value::Null);
816        }
817
818        let slot = match &args[0] {
819            Value::Number(n) => n.as_u64().unwrap_or(0),
820            _ => return Ok(Value::Null),
821        };
822
823        if slot == 0 {
824            return Ok(Value::Null);
825        }
826
827        // Try to get the slot hash from the global cache
828        let slot_hash = crate::slot_hash_cache::get_slot_hash(slot);
829
830        match slot_hash {
831            Some(hash) => {
832                // Convert the base58 encoded slot hash to bytes
833                // The slot hash is a 32-byte value base58 encoded
834                match bs58::decode(&hash).into_vec() {
835                    Ok(bytes) if bytes.len() == 32 => {
836                        // Return as { bytes: [...] } to match the SlotHashBytes TypeScript interface
837                        let json_bytes: Vec<Value> = bytes.into_iter().map(|b| Value::Number(b.into())).collect();
838                        let mut obj = serde_json::Map::new();
839                        obj.insert("bytes".to_string(), Value::Array(json_bytes));
840                        Ok(Value::Object(obj))
841                    }
842                    _ => {
843                        tracing::warn!(slot = slot, hash = hash, "Failed to decode slot hash");
844                        Ok(Value::Null)
845                    }
846                }
847            }
848            None => {
849                tracing::debug!(slot = slot, "Slot hash not found in cache");
850                Ok(Value::Null)
851            }
852        }
853    }
854}
855
856impl ResolverDefinition for SlotHashResolver {
857    fn name(&self) -> &'static str {
858        "SlotHash"
859    }
860
861    fn output_type(&self) -> &'static str {
862        "SlotHash"
863    }
864
865    fn computed_methods(&self) -> &'static [ResolverComputedMethod] {
866        SLOT_HASH_METHODS
867    }
868
869    fn evaluate_computed(
870        &self,
871        method: &str,
872        args: &[Value],
873    ) -> std::result::Result<Value, Box<dyn std::error::Error>> {
874        match method {
875            "slot_hash" => Self::evaluate_slot_hash(args),
876            "keccak_rng" => Self::evaluate_keccak_rng(args),
877            _ => Err(format!("Unknown SlotHash method '{}'", method).into()),
878        }
879    }
880
881    fn typescript_interface(&self) -> Option<&'static str> {
882        Some(
883            r#"export interface SlotHashBytes {
884  /** 32-byte slot hash as array of numbers (0-255) */
885  bytes: number[];
886}
887
888export type KeccakRngValue = string;"#,
889        )
890    }
891
892    fn extra_output_types(&self) -> &'static [&'static str] {
893        &["SlotHashBytes", "KeccakRngValue"]
894    }
895
896    fn typescript_schema(&self) -> Option<ResolverTypeScriptSchema> {
897        Some(ResolverTypeScriptSchema {
898            name: "SlotHashTypes",
899            definition: r#"export const SlotHashBytesSchema = z.object({
900  bytes: z.array(z.number().int().min(0).max(255)).length(32),
901});
902
903export const KeccakRngValueSchema = z.string();"#,
904        })
905    }
906}
907
908struct TokenMetadataResolver;
909
910const TOKEN_METADATA_METHODS: &[ResolverComputedMethod] = &[
911    ResolverComputedMethod {
912        name: "ui_amount",
913        arg_count: 2,
914    },
915    ResolverComputedMethod {
916        name: "raw_amount",
917        arg_count: 2,
918    },
919];
920
921impl TokenMetadataResolver {
922    fn optional_f64(value: &Value) -> Option<f64> {
923        if value.is_null() {
924            return None;
925        }
926        match value {
927            Value::Number(number) => number.as_f64(),
928            Value::String(text) => text.parse::<f64>().ok(),
929            _ => None,
930        }
931    }
932
933    fn optional_u8(value: &Value) -> Option<u8> {
934        if value.is_null() {
935            return None;
936        }
937        match value {
938            Value::Number(number) => number
939                .as_u64()
940                .or_else(|| {
941                    number
942                        .as_i64()
943                        .and_then(|v| if v >= 0 { Some(v as u64) } else { None })
944                })
945                .and_then(|v| u8::try_from(v).ok()),
946            Value::String(text) => text.parse::<u8>().ok(),
947            _ => None,
948        }
949    }
950
951    fn evaluate_ui_amount(
952        args: &[Value],
953    ) -> std::result::Result<Value, Box<dyn std::error::Error>> {
954        let raw_value = Self::optional_f64(&args[0]);
955        let decimals = Self::optional_u8(&args[1]);
956
957        match (raw_value, decimals) {
958            (Some(value), Some(decimals)) => {
959                let factor = 10_f64.powi(decimals as i32);
960                let result = value / factor;
961                if result.is_finite() {
962                    serde_json::Number::from_f64(result)
963                        .map(Value::Number)
964                        .ok_or_else(|| "Failed to serialize ui_amount".into())
965                } else {
966                    Err("ui_amount result is not finite".into())
967                }
968            }
969            _ => Ok(Value::Null),
970        }
971    }
972
973    fn evaluate_raw_amount(
974        args: &[Value],
975    ) -> std::result::Result<Value, Box<dyn std::error::Error>> {
976        let ui_value = Self::optional_f64(&args[0]);
977        let decimals = Self::optional_u8(&args[1]);
978
979        match (ui_value, decimals) {
980            (Some(value), Some(decimals)) => {
981                let factor = 10_f64.powi(decimals as i32);
982                let result = value * factor;
983                if !result.is_finite() || result < 0.0 {
984                    return Err("raw_amount result is not finite".into());
985                }
986                let rounded = result.round();
987                if rounded > u64::MAX as f64 {
988                    return Err("raw_amount result exceeds u64".into());
989                }
990                Ok(Value::Number(serde_json::Number::from(rounded as u64)))
991            }
992            _ => Ok(Value::Null),
993        }
994    }
995}
996
997impl ResolverDefinition for TokenMetadataResolver {
998    fn name(&self) -> &'static str {
999        "TokenMetadata"
1000    }
1001
1002    fn output_type(&self) -> &'static str {
1003        "TokenMetadata"
1004    }
1005
1006    fn computed_methods(&self) -> &'static [ResolverComputedMethod] {
1007        TOKEN_METADATA_METHODS
1008    }
1009
1010    fn evaluate_computed(
1011        &self,
1012        method: &str,
1013        args: &[Value],
1014    ) -> std::result::Result<Value, Box<dyn std::error::Error>> {
1015        match method {
1016            "ui_amount" => Self::evaluate_ui_amount(args),
1017            "raw_amount" => Self::evaluate_raw_amount(args),
1018            _ => Err(format!("Unknown TokenMetadata method '{}'", method).into()),
1019        }
1020    }
1021
1022    fn typescript_interface(&self) -> Option<&'static str> {
1023        Some(
1024            r#"export interface TokenMetadata {
1025  mint: string;
1026  name?: string | null;
1027  symbol?: string | null;
1028  decimals?: number | null;
1029  logo_uri?: string | null;
1030}"#,
1031        )
1032    }
1033
1034    fn typescript_schema(&self) -> Option<ResolverTypeScriptSchema> {
1035        Some(ResolverTypeScriptSchema {
1036            name: "TokenMetadataSchema",
1037            definition: r#"export const TokenMetadataSchema = z.object({
1038  mint: z.string(),
1039  name: z.string().nullable().optional(),
1040  symbol: z.string().nullable().optional(),
1041  decimals: z.number().nullable().optional(),
1042  logo_uri: z.string().nullable().optional(),
1043});"#,
1044        })
1045    }
1046}
1047
1048impl<'a> InstructionContext<'a> {
1049    pub fn new(
1050        accounts: HashMap<String, String>,
1051        state_id: u32,
1052        reverse_lookup_tx: &'a mut dyn ReverseLookupUpdater,
1053    ) -> Self {
1054        Self {
1055            accounts,
1056            state_id,
1057            reverse_lookup_tx,
1058            pending_updates: Vec::new(),
1059            registers: None,
1060            state_reg: None,
1061            compiled_paths: None,
1062            instruction_data: None,
1063            slot: None,
1064            signature: None,
1065            timestamp: None,
1066            dirty_tracker: crate::vm::DirtyTracker::new(),
1067        }
1068    }
1069
1070    #[allow(clippy::too_many_arguments)]
1071    pub fn with_metrics(
1072        accounts: HashMap<String, String>,
1073        state_id: u32,
1074        reverse_lookup_tx: &'a mut dyn ReverseLookupUpdater,
1075        registers: &'a mut Vec<crate::vm::RegisterValue>,
1076        state_reg: crate::vm::Register,
1077        compiled_paths: &'a HashMap<String, crate::metrics_context::CompiledPath>,
1078        instruction_data: &'a serde_json::Value,
1079        slot: Option<u64>,
1080        signature: Option<String>,
1081        timestamp: i64,
1082    ) -> Self {
1083        Self {
1084            accounts,
1085            state_id,
1086            reverse_lookup_tx,
1087            pending_updates: Vec::new(),
1088            registers: Some(registers),
1089            state_reg: Some(state_reg),
1090            compiled_paths: Some(compiled_paths),
1091            instruction_data: Some(instruction_data),
1092            slot,
1093            signature,
1094            timestamp: Some(timestamp),
1095            dirty_tracker: crate::vm::DirtyTracker::new(),
1096        }
1097    }
1098
1099    /// Get an account address by its name from the instruction
1100    pub fn account(&self, name: &str) -> Option<String> {
1101        self.accounts.get(name).cloned()
1102    }
1103
1104    /// Register a reverse lookup: PDA address -> seed value
1105    /// This also flushes any pending account updates for this PDA
1106    ///
1107    /// The pending account updates are accumulated internally and can be retrieved
1108    /// via `take_pending_updates()` after all hooks have executed.
1109    pub fn register_pda_reverse_lookup(&mut self, pda_address: &str, seed_value: &str) {
1110        let pending = self
1111            .reverse_lookup_tx
1112            .update(pda_address.to_string(), seed_value.to_string());
1113        self.pending_updates.extend(pending);
1114    }
1115
1116    /// Take all accumulated pending updates
1117    ///
1118    /// This should be called after all instruction hooks have executed to retrieve
1119    /// any pending account updates that need to be reprocessed.
1120    pub fn take_pending_updates(&mut self) -> Vec<crate::vm::PendingAccountUpdate> {
1121        std::mem::take(&mut self.pending_updates)
1122    }
1123
1124    pub fn dirty_tracker(&self) -> &crate::vm::DirtyTracker {
1125        &self.dirty_tracker
1126    }
1127
1128    pub fn dirty_tracker_mut(&mut self) -> &mut crate::vm::DirtyTracker {
1129        &mut self.dirty_tracker
1130    }
1131
1132    /// Get the current state register value (for generating mutations)
1133    pub fn state_value(&self) -> Option<&serde_json::Value> {
1134        if let (Some(registers), Some(state_reg)) = (self.registers.as_ref(), self.state_reg) {
1135            Some(&registers[state_reg])
1136        } else {
1137            None
1138        }
1139    }
1140
1141    /// Get a field value from the entity state
1142    /// This allows reading aggregated values or other entity fields
1143    pub fn get<T: serde::de::DeserializeOwned>(&self, field_path: &str) -> Option<T> {
1144        if let (Some(registers), Some(state_reg)) = (self.registers.as_ref(), self.state_reg) {
1145            let state = &registers[state_reg];
1146            self.get_nested_value(state, field_path)
1147                .and_then(|v| serde_json::from_value(v.clone()).ok())
1148        } else {
1149            None
1150        }
1151    }
1152
1153    pub fn set<T: serde::Serialize>(&mut self, field_path: &str, value: T) {
1154        if let (Some(registers), Some(state_reg)) = (self.registers.as_mut(), self.state_reg) {
1155            let serialized = serde_json::to_value(value).ok();
1156            if let Some(val) = serialized {
1157                Self::set_nested_value_static(&mut registers[state_reg], field_path, val);
1158                self.dirty_tracker.mark_replaced(field_path);
1159                println!("      ✓ Set field '{}' and marked as dirty", field_path);
1160            }
1161        } else {
1162            println!("      ⚠️  Cannot set field '{}': metrics not configured (registers={}, state_reg={:?})", 
1163                field_path, self.registers.is_some(), self.state_reg);
1164        }
1165    }
1166
1167    pub fn increment(&mut self, field_path: &str, amount: i64) {
1168        let current = self.get::<i64>(field_path).unwrap_or(0);
1169        self.set(field_path, current + amount);
1170    }
1171
1172    pub fn append<T: serde::Serialize>(&mut self, field_path: &str, value: T) {
1173        if let (Some(registers), Some(state_reg)) = (self.registers.as_mut(), self.state_reg) {
1174            let serialized = serde_json::to_value(&value).ok();
1175            if let Some(val) = serialized {
1176                Self::append_to_array_static(&mut registers[state_reg], field_path, val.clone());
1177                self.dirty_tracker.mark_appended(field_path, val);
1178                println!(
1179                    "      ✓ Appended to '{}' and marked as appended",
1180                    field_path
1181                );
1182            }
1183        } else {
1184            println!(
1185                "      ⚠️  Cannot append to '{}': metrics not configured",
1186                field_path
1187            );
1188        }
1189    }
1190
1191    fn append_to_array_static(
1192        value: &mut serde_json::Value,
1193        path: &str,
1194        new_value: serde_json::Value,
1195    ) {
1196        let segments: Vec<&str> = path.split('.').collect();
1197        if segments.is_empty() {
1198            return;
1199        }
1200
1201        let mut current = value;
1202        for segment in &segments[..segments.len() - 1] {
1203            if !current.is_object() {
1204                *current = serde_json::json!({});
1205            }
1206            let obj = current.as_object_mut().unwrap();
1207            current = obj
1208                .entry(segment.to_string())
1209                .or_insert(serde_json::json!({}));
1210        }
1211
1212        let last_segment = segments[segments.len() - 1];
1213        if !current.is_object() {
1214            *current = serde_json::json!({});
1215        }
1216        let obj = current.as_object_mut().unwrap();
1217        let arr = obj
1218            .entry(last_segment.to_string())
1219            .or_insert_with(|| serde_json::json!([]));
1220        if let Some(arr) = arr.as_array_mut() {
1221            arr.push(new_value);
1222        }
1223    }
1224
1225    fn get_nested_value<'b>(
1226        &self,
1227        value: &'b serde_json::Value,
1228        path: &str,
1229    ) -> Option<&'b serde_json::Value> {
1230        let mut current = value;
1231        for segment in path.split('.') {
1232            current = current.get(segment)?;
1233        }
1234        Some(current)
1235    }
1236
1237    fn set_nested_value_static(
1238        value: &mut serde_json::Value,
1239        path: &str,
1240        new_value: serde_json::Value,
1241    ) {
1242        let segments: Vec<&str> = path.split('.').collect();
1243        if segments.is_empty() {
1244            return;
1245        }
1246
1247        let mut current = value;
1248        for segment in &segments[..segments.len() - 1] {
1249            if !current.is_object() {
1250                *current = serde_json::json!({});
1251            }
1252            let obj = current.as_object_mut().unwrap();
1253            current = obj
1254                .entry(segment.to_string())
1255                .or_insert(serde_json::json!({}));
1256        }
1257
1258        if !current.is_object() {
1259            *current = serde_json::json!({});
1260        }
1261        if let Some(obj) = current.as_object_mut() {
1262            obj.insert(segments[segments.len() - 1].to_string(), new_value);
1263        }
1264    }
1265
1266    /// Access instruction data field
1267    pub fn data<T: serde::de::DeserializeOwned>(&self, field: &str) -> Option<T> {
1268        self.instruction_data
1269            .and_then(|data| data.get(field))
1270            .and_then(|v| serde_json::from_value(v.clone()).ok())
1271    }
1272
1273    /// Get the current timestamp
1274    pub fn timestamp(&self) -> i64 {
1275        self.timestamp.unwrap_or(0)
1276    }
1277
1278    /// Get the current slot
1279    pub fn slot(&self) -> Option<u64> {
1280        self.slot
1281    }
1282
1283    /// Get the current signature
1284    pub fn signature(&self) -> Option<&str> {
1285        self.signature.as_deref()
1286    }
1287}