Acton
Scripting

Overview

Learn how to run Tolk files as standalone scripts for local experiments, contract deployment, and blockchain interactions

Acton provides the ability to run individual Tolk files as standalone scripts through the script command. Such scripts can be used both for learning the Tolk language, local experiments, and for real tasks like contract deployment or sending messages to the real blockchain.

Scripts can use all the functions that are used in tests, which means that within scripts it is possible to deploy a contract to a local blockchain, send messages, and call get methods.

The main difference between scripts and tests is that scripts define a main() function which becomes the entry point. The main() function can optionally accept arguments passed from the command line, and those arguments are parsed against the ABI for main().

Each script is a regular smart contract, so, for example, sending a message through msg.send() will update the c5 register.

Scripts terminate with an exit code equal to the exit_code of the main get method execution. The maximum exit code is 255 due to the standard Unix/Linux operating system limitation, where exit codes are stored in an 8-bit unsigned integer. Any exit_code value larger than 255 will be wrapped using modulo 256 (x % 256).

The state is not preserved between script runs.

The following script shows the simplest Hello World written in Tolk:

hello.tolk
import "@acton/io"

fun main() {
    println("Hello World")
}

To run it, execute the following command:

acton script hello.tolk

# Hello World

And the following script calculates the storage fee for a library code of a given duration in seconds:

storage_fee_calculator.tolk
import "@stdlib/gas-payments"

fun main(libraryCode: cell, duration: int) {
    val gasConsumedBeforeCalculation = getGasConsumedAtTheMoment();
    val (libraryRefs, libraryBits, _) = libraryCode.calculateSizeStrict(2048);
    val gasConsumedForCalculation = getGasConsumedAtTheMoment() - gasConsumedBeforeCalculation;

    val toReserve = calculateGasFeeWithoutFlatPrice(MASTERCHAIN, gasConsumedForCalculation)
                    + calculateStorageFee(MASTERCHAIN, duration, libraryBits, libraryRefs);
    println("Storage fee: {:ton}", toReserve);
}

To run it, you need to pass the library code as a cell and the duration in seconds:

acton script storage_fee_calculator.tolk b5ee9c72010101010006000008000003e7 3600

# Storage fee: 0.002140812 TON

Next Steps

Last updated on

On this page