Coder Social home page Coder Social logo

vuvoth / aqua-compiler Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 1.0 2.3 MB

An expressive high level language for the Algorand block chain that compiles to TEAL code.

License: MIT License

PEG.js 5.52% JavaScript 0.27% Batchfile 0.01% TypeScript 94.21%

aqua-compiler's Introduction

Aqua-compiler

An expressive high level language for the Algorand blockchain smart contracts that compiles to TEAL code.

This is a work in progress. Please report issues and help set the direction for this project.

Using the Aqua command

Download the latest executable for your platform from the releases page.

Add the executable to your path. If you are on MacOS or Linux you should rename the executable from aqua-mac or aqua-linux to just be called aqua (so the rest of the instructions make sense).

You can also install Aqua using npm:

npm install -g aqua-compiler

REPL

Running the executable with no arguments starts the REPL:

aqua

You can type Aqua expressions and statements at the REPL and see the TEAL code that is generated.

Trying entering expressions at the REPL prompt:

  • txn.Amount >= 1000;
  • 15 + txn.Amount >= 1000;
  • txn.Amount <= arg[0];
  • txn.Amount + arg[0] > 1000 && arg[1] > 30;
  • txn.Receiver == addr ABC123;
  • "a string" == txn.Something;
  • return 1+2;

Compiling an Aqua file

To compile an Aqua file to TEAL code, input the Aqua filename:

aqua my-smart-contract.aqua

That prints the generated TEAL code to standard output.

Typically you'll want to capture the TEAL code to a file (so you can run it against the blockchain):

aqua my-smart-contact.aqua > my-smart-contract.teal

Examples of Aqua code

See the examples subdirectory for various examples of Aqua code.

Using the Aqua API

You can compile Aqua to TEAL code using Aqua's JavaScript/TypesScript API.

First install Aqua in your Node.js project:

npm install --save aqua-compiler

Then import Aqua's compile function:

const { compile } = require("aqua-compiler");

Or in TypeScript:

import { compile } from "aqua-compiler";

Now use compile to compile Aqua to TEAL:

const aquaCode = "return 1 + 2;"
const tealCode = compiler(aquaCode);
console.log(tealCode);

Testing Aqua code with Jest

One reason why you might want to use Aqua's API is to enable automated testing.

For example, here's a Jest test that compiles Aqua to TEAL:

import { compile } from "aqua-compiler";
import { readFile } from "fs/promises";

describe("My smart contract test suite", () => {

    test("My first test", async () => {

        const tealCode = await compileAquaFile("my-smart-contract.aqua");

        // ... test that you can execute teal code against your sandbox blockchain ...
    });

    // ... other tests go here ...

});

//
// Loads and compiles an Aqua file.
//
async function compileAquaFile(fileName) {
    const fileContent = await readFile(join(tealPath, tealFileName), { encoding: "utf8" });
    return compile(fileContent);
}

After compiling an Aqua file to TEAL code you can then deploy that code against your sandbox Algorand blockchain.

Another way of testing that is faster and doesn't require having an actual Algorand node instance is to use the TEAL interpreter to simulate the Algorand virtual machine.

A Jest test that runs Aqua code against the TEAL interpreter might look like this:

import { compile } from "aqua-compiler";
import { readFile } from "fs/promises";
import { execute } from "teal-interpreter";

describe("My smart contract test suite", () => {

    test("My first test", async () => {

        const config = {
            // ... configure the initial state of the TEAL interpreter ...
        }
        const result = await executeAqua("my-smart-contract.aqua", config);

        // ... run expectations against the result to check that execution of the aqua code has expected results ...
        
    });

    // ... other tests go here ...

});

//
// Loads and compiles an Aqua file.
//
async function compileAquaFile(fileName) {
    const fileContent = await readFile(join(tealPath, tealFileName), { encoding: "utf8" });
    return compile(fileContent);
}

//
// Executes Aqua code against the TEAL interpreter.
//
async function executeAqua(fileName, config) {
    const tealCode = await compileAquaFile(fileName);
    return await execute(tealCode, config);
}

Development

See the development guide for instructions on development of Aqua.

aqua-compiler's People

Contributors

aetherplex avatar ashleydavis avatar

Forkers

ashleydavis

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.