How to compile Teal to Uint8Array

I am trying to create an application using the JavaScript SDK js-algorand-sdk/makeTxn.js at 0640240ba167a52c3f05a8d7df1e425df4f9c616 · algorand/js-algorand-sdk · GitHub.

How would one actually compile Teal to Uint8Array? The .teal files are stored locally and I have tried using goal clerk compile.

You can:

  • either run goal clerk compile myprogram.teal and then in the JS read the file myprogram.teal.tok,
  • or use the .compile function of an algod V2 client (for that you need your algod endpoint to have the flag EnableDeveloperAPI set to true - Algorand Developer Docs).

You can see examples for the latter in Algorand Developer Docs

// helper function to compile program source  
async function compileProgram(client, programSource) {
    let encoder = new TextEncoder();
    let programBytes = encoder.encode(programSource);
    let compileResponse = await client.compile(programBytes).do();
    let compiledBytes = new Uint8Array(Buffer.from(compileResponse.result, "base64"));
    return compiledBytes;
}
1 Like

Thanks for the answer!

What is the programSource argument? Is it a string representation of the teal program?

Yes, programSource is the string representation of the TEAL program.