Skip to main content

graph_native/
helpers.rs

1use crate::types::Address;
2
3/// Concatenate bytes with an i32 suffix — used for entity ID generation.
4/// Mirrors graph-ts Bytes.concatI32(): appends 4 little-endian bytes of the
5/// i32 to the byte array, returns 0x-prefixed hex string.
6pub fn concat_i32(left: &[u8], right: i32) -> String {
7    let mut result = left.to_vec();
8    result.extend_from_slice(&right.to_le_bytes());
9    format!("0x{}", hex::encode(&result))
10}
11
12/// Concatenate two byte arrays — used for entity ID generation.
13/// Mirrors graph-ts Bytes.concat()
14pub fn concat_bytes(left: &[u8], right: &[u8]) -> Vec<u8> {
15    let mut result = left.to_vec();
16    result.extend_from_slice(right);
17    result
18}
19
20/// Bytes type alias
21pub type Bytes = Vec<u8>;
22
23/// Logging macros matching graph-ts log levels
24#[macro_export]
25macro_rules! info {
26    ($($arg:tt)*) => {
27        // TODO: wire to graph-node logging
28        eprintln!("[INFO] {}", format!($($arg)*));
29    };
30}
31
32#[macro_export]
33macro_rules! warning {
34    ($($arg:tt)*) => {
35        eprintln!("[WARN] {}", format!($($arg)*));
36    };
37}
38
39#[macro_export]
40macro_rules! error {
41    ($($arg:tt)*) => {
42        eprintln!("[ERROR] {}", format!($($arg)*));
43    };
44}
45
46#[macro_export]
47macro_rules! debug {
48    ($($arg:tt)*) => {
49        eprintln!("[DEBUG] {}", format!($($arg)*));
50    };
51}
52
53/// data_source module — mirrors graph-ts dataSource
54///
55/// PHASE C BLOCKER: Both functions below are stubs. They need runtime context
56/// from graph-node that is only available when the plugin is loaded by
57/// RuntimeHost::process_mapping_trigger:
58/// - network() needs the deployment's network string from DataSourceDetails
59/// - create() needs to register a dynamic data source with the SubgraphRunner
60///   via BlockState::created_data_sources
61///
62/// Until Phase C wiring, any handler using these will get wrong results.
63/// The transpiler should mark handlers using data_source::create() as
64/// unsupported (WASM fallback).
65///
66/// Tests for these functions are also blocked on Phase C — they require
67/// mocking graph-node's deployment context.
68pub mod data_source {
69    /// STUB: Returns hardcoded "mainnet". Phase C will read from DataSourceDetails.
70    pub fn network() -> String {
71        "mainnet".to_string()
72    }
73
74    /// STUB: No-op. Phase C will wire to BlockState::created_data_sources.
75    pub fn create(_template: &str, _address: &super::Address) {}
76}