dist_agent_lang 1.0.5

A hybrid programming language for decentralized and centralized network integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use crate::runtime::values::Value;
use serde_json;
use std::collections::HashMap;

/// Add Solidity — utilities for integrating with Solidity contracts (ABI parse, register, call, events).

#[derive(Debug, Clone)]
pub struct SolidityContract {
    pub name: String,
    pub address: String,
    pub chain_id: i64,
    pub abi: Option<String>, // JSON ABI string
}

#[derive(Debug, Clone)]
pub struct ContractFunction {
    pub name: String,
    pub inputs: Vec<FunctionInput>,
    pub outputs: Vec<FunctionOutput>,
    pub state_mutability: String, // "view", "nonpayable", "payable"
}

#[derive(Debug, Clone)]
pub struct FunctionInput {
    pub name: String,
    pub param_type: String, // "uint256", "address", "string", etc.
    pub indexed: bool,
}

#[derive(Debug, Clone)]
pub struct FunctionOutput {
    pub name: String,
    pub param_type: String,
}

#[derive(Debug, Clone)]
pub struct ContractEvent {
    pub name: String,
    pub inputs: Vec<FunctionInput>,
    pub anonymous: bool,
}

/// Parse Solidity ABI JSON string
pub fn parse_abi(abi_json: String) -> Result<Vec<ContractFunction>, String> {
    let abi: Vec<serde_json::Value> =
        serde_json::from_str(&abi_json).map_err(|e| format!("Failed to parse ABI: {}", e))?;

    let mut functions = Vec::new();

    for item in abi {
        if let Some(item_type) = item.get("type").and_then(|v| v.as_str()) {
            if item_type == "function" {
                if let Some(name) = item.get("name").and_then(|v| v.as_str()) {
                    let inputs = parse_inputs(item.get("inputs"));
                    let outputs = parse_outputs(item.get("outputs"));
                    let state_mutability = item
                        .get("stateMutability")
                        .and_then(|v| v.as_str())
                        .unwrap_or("nonpayable")
                        .to_string();

                    functions.push(ContractFunction {
                        name: name.to_string(),
                        inputs,
                        outputs,
                        state_mutability: state_mutability.to_string(),
                    });
                }
            }
        }
    }

    Ok(functions)
}

/// Parse event definitions from ABI
pub fn parse_events(abi_json: String) -> Result<Vec<ContractEvent>, String> {
    let abi: Vec<serde_json::Value> =
        serde_json::from_str(&abi_json).map_err(|e| format!("Failed to parse ABI: {}", e))?;

    let mut events = Vec::new();

    for item in abi {
        if let Some(item_type) = item.get("type").and_then(|v| v.as_str()) {
            if item_type == "event" {
                if let Some(name) = item.get("name").and_then(|v| v.as_str()) {
                    let inputs = parse_inputs(item.get("inputs"));
                    let anonymous = item
                        .get("anonymous")
                        .and_then(|v| v.as_bool())
                        .unwrap_or(false);

                    events.push(ContractEvent {
                        name: name.to_string(),
                        inputs,
                        anonymous,
                    });
                }
            }
        }
    }

    Ok(events)
}

fn parse_inputs(inputs_value: Option<&serde_json::Value>) -> Vec<FunctionInput> {
    let mut inputs = Vec::new();

    if let Some(inputs_array) = inputs_value.and_then(|v| v.as_array()) {
        for input in inputs_array {
            if let (Some(name), Some(param_type)) = (
                input.get("name").and_then(|v| v.as_str()),
                input.get("type").and_then(|v| v.as_str()),
            ) {
                let indexed = input
                    .get("indexed")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);

                inputs.push(FunctionInput {
                    name: name.to_string(),
                    param_type: param_type.to_string(),
                    indexed,
                });
            }
        }
    }

    inputs
}

fn parse_outputs(outputs_value: Option<&serde_json::Value>) -> Vec<FunctionOutput> {
    let mut outputs = Vec::new();

    if let Some(outputs_array) = outputs_value.and_then(|v| v.as_array()) {
        for output in outputs_array {
            if let (Some(name), Some(param_type)) = (
                output.get("name").and_then(|v| v.as_str()),
                output.get("type").and_then(|v| v.as_str()),
            ) {
                outputs.push(FunctionOutput {
                    name: name.to_string(),
                    param_type: param_type.to_string(),
                });
            }
        }
    }

    outputs
}

/// Convert Solidity type to dist_agent_lang type
pub fn solidity_to_dal_type(solidity_type: &str) -> String {
    match solidity_type {
        "uint256" | "uint128" | "uint64" | "uint32" | "uint8" | "uint" => "int".to_string(),
        "int256" | "int128" | "int64" | "int32" | "int8" | "int" => "int".to_string(),
        "address" => "string".to_string(),
        "bool" => "bool".to_string(),
        "string" | "bytes" | "bytes32" => "string".to_string(),
        _ => {
            // Handle arrays
            if solidity_type.ends_with("[]") {
                "vector<".to_string()
                    + &solidity_to_dal_type(&solidity_type[..solidity_type.len() - 2])
                    + ">"
            } else {
                "any".to_string()
            }
        }
    }
}

/// Generate type-safe wrapper function signature
pub fn generate_wrapper_signature(function: &ContractFunction) -> String {
    let mut signature = format!("fn {}(&self", function.name);

    for input in &function.inputs {
        signature.push_str(&format!(
            ", {}: {}",
            input.name,
            solidity_to_dal_type(&input.param_type)
        ));
    }

    signature.push_str(") -> ");

    if function.outputs.is_empty() {
        signature.push_str("bool");
    } else if function.outputs.len() == 1 {
        signature.push_str(&solidity_to_dal_type(&function.outputs[0].param_type));
    } else {
        signature.push_str("map<string, any>");
    }

    signature.push_str(";");
    signature
}

/// Register a Solidity contract with ABI
pub fn register_contract(
    name: String,
    address: String,
    chain_id: i64,
    abi_json: Option<String>,
) -> SolidityContract {
    SolidityContract {
        name,
        address,
        chain_id,
        abi: abi_json,
    }
}

/// Call a Solidity contract function with type checking
pub fn call_with_abi(
    contract: &SolidityContract,
    function_name: String,
    args: HashMap<String, Value>,
) -> Result<String, String> {
    // Parse ABI if available
    if let Some(ref abi_json) = contract.abi {
        let functions = parse_abi(abi_json.clone())?;

        // Find the function
        let function = functions
            .iter()
            .find(|f| f.name == function_name)
            .ok_or_else(|| format!("Function {} not found in ABI", function_name))?;

        // Validate arguments
        validate_args(function, &args)?;

        // Call the contract
        return Ok(crate::stdlib::chain::call(
            contract.chain_id,
            contract.address.clone(),
            function_name,
            convert_args_to_string_map(args),
        ));
    }

    // Fallback to direct call without ABI validation
    Ok(crate::stdlib::chain::call(
        contract.chain_id,
        contract.address.clone(),
        function_name,
        convert_args_to_string_map(args),
    ))
}

fn validate_args(function: &ContractFunction, args: &HashMap<String, Value>) -> Result<(), String> {
    if function.inputs.len() != args.len() {
        return Err(format!(
            "Argument count mismatch: expected {}, got {}",
            function.inputs.len(),
            args.len()
        ));
    }

    for input in &function.inputs {
        if !args.contains_key(&input.name) {
            return Err(format!("Missing argument: {}", input.name));
        }
    }

    Ok(())
}

fn convert_args_to_string_map(args: HashMap<String, Value>) -> HashMap<String, String> {
    args.iter()
        .map(|(k, v)| (k.clone(), value_to_string(v)))
        .collect()
}

fn value_to_string(value: &Value) -> String {
    match value {
        Value::Int(i) => i.to_string(),
        Value::Float(f) => f.to_string(),
        Value::String(s) => s.clone(),
        Value::Bool(b) => b.to_string(),
        Value::Null => "null".to_string(),
        Value::Array(arr) => format!("{:?}", arr),
        Value::Map(map) => format!("{:?}", map),
        Value::Set(set) => format!("{:?}", set),
        Value::Struct(name, fields) => format!("{} {:?}", name, fields),
        Value::List(list) => format!("{:?}", list),
        Value::Result(ok_val, err_val) => {
            // Check if it's Ok or Err by checking if err_val is Null
            if matches!(err_val.as_ref(), Value::Null) {
                format!("Ok({})", value_to_string(ok_val))
            } else {
                format!("Err({})", value_to_string(err_val))
            }
        }
        Value::Option(opt_val) => match opt_val {
            Some(val) => value_to_string(val),
            None => "null".to_string(),
        },
        Value::Closure(id) => format!("<closure {}>", id),
    }
}

/// Listen to Solidity contract events
pub fn listen_to_event(
    contract: &SolidityContract,
    event_name: String,
    callback: String, // Function name to call
    from_block: Option<i64>,
    to_block: Option<i64>,
) -> String {
    // Log event subscription
    crate::stdlib::log::info(
        "event_listener",
        {
            let mut data = std::collections::HashMap::new();
            data.insert(
                "contract".to_string(),
                Value::String(contract.address.clone()),
            );
            data.insert("event".to_string(), Value::String(event_name.clone()));
            data.insert("callback".to_string(), Value::String(callback.clone()));
            if let Some(from) = from_block {
                data.insert("from_block".to_string(), Value::Int(from));
            }
            if let Some(to) = to_block {
                data.insert("to_block".to_string(), Value::Int(to));
            }
            data
        },
        Some("add_sol"),
    );

    format!(
        "Event listener registered for {} on contract {}",
        event_name, contract.address
    )
}

/// Generate dist_agent_lang wrapper code from Solidity ABI
pub fn generate_wrapper_code(contract: &SolidityContract) -> Result<String, String> {
    let abi_json = contract
        .abi
        .as_ref()
        .ok_or_else(|| "ABI not available".to_string())?;

    let functions = parse_abi(abi_json.clone())?;
    let events = parse_events(abi_json.clone())?;

    let mut code = format!(
        "// Auto-generated wrapper for Solidity contract: {}\n",
        contract.name
    );
    code.push_str(&format!("// Address: {}\n", contract.address));
    code.push_str(&format!("// Chain ID: {}\n\n", contract.chain_id));

    code.push_str("@trust(\"hybrid\")\n");
    code.push_str(&format!("service {}Wrapper {{\n", contract.name));
    code.push_str(&format!(
        "    contract_address: string = \"{}\";\n",
        contract.address
    ));
    code.push_str(&format!("    chain_id: int = {};\n\n", contract.chain_id));

    // Generate wrapper functions
    for function in &functions {
        code.push_str("    ");
        code.push_str(&generate_wrapper_signature(function));
        code.push_str("\n");
        code.push_str("    ");
        code.push_str(&format!("        return chain::call(\n"));
        code.push_str(&format!("            self.chain_id,\n"));
        code.push_str(&format!("            self.contract_address,\n"));
        code.push_str(&format!("            \"{}\",\n", function.name));
        code.push_str("            {\n");

        for input in &function.inputs {
            code.push_str(&format!(
                "                \"{}\": {},\n",
                input.name, input.name
            ));
        }

        code.push_str("            }\n");
        code.push_str("        );\n");
        code.push_str("    }\n\n");
    }

    // Generate event handlers
    if !events.is_empty() {
        code.push_str("    // Event handlers\n");
        for event in &events {
            code.push_str(&format!(
                "    fn on_{}(callback: function) {{\n",
                event.name
            ));
            code.push_str(&format!("        add_sol::listen_to_event(\n"));
            code.push_str(&format!("            self.contract_address,\n"));
            code.push_str(&format!("            \"{}\",\n", event.name));
            code.push_str("            callback\n");
            code.push_str("        );\n");
            code.push_str("    }\n\n");
        }
    }

    code.push_str("}\n");

    Ok(code)
}