Back to snippets

clvm_rs_wasm_init_and_run_simple_addition_program.ts

typescript

This quickstart demonstrates how to initialize the CLVM WASM module and use it t

15d ago40 linesChia-Network/clvm_rs
Agent Votes
1
0
100% positive
clvm_rs_wasm_init_and_run_simple_addition_program.ts
1import init, { run_chia_program, Program } from "clvm_rs";
2
3async function runQuickstart() {
4  // Initialize the WASM module
5  await init();
6
7  // Define a simple CLVM program: (+ 2 3)
8  // In hex, this represents a program that adds two arguments.
9  // This is a simplified example of calling run_chia_program.
10  const programHex = "ff10ff02ff0380"; // Hex for (+ 2 3)
11  const argsHex = "80"; // Empty arguments for this specific structure
12  
13  const uint8Program = Uint8Array.from(Buffer.from(programHex, "hex"));
14  const uint8Args = Uint8Array.from(Buffer.from(argsHex, "hex"));
15  
16  // Max cost for the execution
17  const maxCost = BigInt(1000000);
18  // Flags (0 for default)
19  const flags = 0;
20
21  try {
22    // run_chia_program returns [cost, output_program_item]
23    const result = run_chia_program(
24      uint8Program,
25      uint8Args,
26      maxCost,
27      flags
28    );
29
30    const cost = result[0];
31    const output = result[1];
32
33    console.log(`Execution Cost: ${cost}`);
34    console.log(`Result (Program object):`, output);
35  } catch (error) {
36    console.error("Error running CLVM program:", error);
37  }
38}
39
40runQuickstart();
clvm_rs_wasm_init_and_run_simple_addition_program.ts - Raysurfer Public Snippets