Skip to main content

Program

Overview

WebAssembly representation of an Aleo program. The Program class provides methods for parsing, inspecting, and analyzing Aleo programs including their functions, records, structs, and mappings.

Methods

fromString

Create a program from a program string

fromString(program)Program
ParamTypeDescription
programstringAleo program source code
returnProgramProgram object

toString

Get a string representation of the program

toString() ► string
ParamType
returnstring

hasFunction

Determine if a function is present in the program

hasFunction(functionName) ► boolean
ParamTypeDescription
functionNamestringName of the function to check for
returnbooleanTrue if the program has the function, false otherwise

getFunctions

Get javascript array of function names in the program

getFunctions()Array
ParamType
returnArray

Example

const expected_functions = [
"mint",
"transfer_private",
"transfer_private_to_public",
"transfer_public",
"transfer_public_to_private",
"join",
"split",
"fee"
];

const credits_program = aleo_wasm.Program.getCreditsProgram();
const credits_functions = credits_program.getFunctions();
console.log(credits_functions === expected_functions); // Output should be "true"

getFunctionInputs

Get a javascript object representation of the function inputs and types. This can be used to generate a web form to capture user inputs for an execution of a function.

getFunctionInputs(function_name)Array
ParamTypeDescription
function_namestringName of the function to get inputs for
returnArrayArray of function inputs

Example

const expected_inputs = [
{
type:"record",
visibility:"private",
record:"credits",
members:[
{
name:"microcredits",
type:"u64",
visibility:"private"
}
],
register:"r0"
},
{
type:"address",
visibility:"private",
register:"r1"
},
{
type:"u64",
visibility:"private",
register:"r2"
}
];

const credits_program = aleo_wasm.Program.getCreditsProgram();
const transfer_function_inputs = credits_program.getFunctionInputs("transfer_private");
console.log(transfer_function_inputs === expected_inputs); // Output should be "true"

getMappings

Get a list of a program's mappings and the names/types of their keys and values.

getMappings()Array
ParamType
returnArray

Example

const expected_mappings = [
{
name: "account",
key_name: "owner",
key_type: "address",
value_name: "microcredits",
value_type: "u64"
}
];

const credits_program = aleo_wasm.Program.getCreditsProgram();
const credits_mappings = credits_program.getMappings();
console.log(credits_mappings === expected_mappings); // Output should be "true"

getRecordMembers

Get a javascript object representation of a program record and its types

getRecordMembers(record_name)Object
ParamTypeDescription
record_namestringName of the record to get members for
returnObjectObject containing the record name, type, and members

Example

const expected_record = {
type: "record",
record: "Credits",
members: [
{
name: "owner",
type: "address",
visibility: "private"
},
{
name: "microcredits",
type: "u64",
visibility: "private"
}
]
};

const credits_program = aleo_wasm.Program.getCreditsProgram();
const credits_record = credits_program.getRecordMembers("Credits");
console.log(credits_record === expected_record); // Output should be "true"

getStructMembers

Get a javascript object representation of a program struct and its types

getStructMembers(struct_name)Array
ParamTypeDescription
struct_namestringName of the struct to get members for
returnArrayArray containing the struct members

Example

const STRUCT_PROGRAM = `program token_issue.aleo;

struct token_metadata:
network as u32;
version as u32;

struct token:
token_id as u32;
metadata as token_metadata;

function no_op:
input r0 as u64;
output r0 as u64;`;

const expected_struct_members = [
{
name: "token_id",
type: "u32",
},
{
name: "metadata",
type: "struct",
struct_id: "token_metadata",
members: [
{
name: "network",
type: "u32",
},
{
name: "version",
type: "u32",
}
]
}
];

const program = aleo_wasm.Program.fromString(STRUCT_PROGRAM);
const struct_members = program.getStructMembers("token");
console.log(struct_members === expected_struct_members); // Output should be "true"

getCreditsProgram

Get the credits.aleo program

getCreditsProgram()Program
ParamType
returnProgram

id

Get the id of the program

id() ► string
ParamType
returnstring

address

Get a unique address of the program

address()Address
ParamType
returnAddress

isEqual

Determine equality with another program

isEqual(other) ► boolean
ParamTypeDescription
otherProgramThe other program to compare
returnbooleanTrue if the programs are equal, false otherwise

getImports

Get program imports

getImports()Array
ParamType
returnArray

Example

const DOUBLE_TEST = `import multiply_test.aleo;

program double_test.aleo;

function double_it:
input r0 as u32.private;
call multiply_test.aleo/multiply 2u32 r0 into r1;
output r1 as u32.private;`;

const expected_imports = [
"multiply_test.aleo"
];

const program = aleo_wasm.Program.fromString(DOUBLE_TEST);
const imports = program.getImports();
console.log(imports === expected_imports); // Output should be "true"