Compile-time
This page lists all the built-in global functions that are evaluated at the time of building the Tact project and cannot work with non-constant runtime data. These functions are commonly referred to as “compile-time functions” or comptime functions for short.
address
fun address(s: String): Address;A compile-time function that converts a String containing an address into the Address type and embeds it into the contract.
Usage example:
contract Example { // Persistent state variables addr: Address = address("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N"); // works at compile-time!}cell
fun cell(bocBase64: String): Cell;A compile-time function that embeds a base64-encoded BoC bocBase64 as a Cell into the contract.
Usage example:
contract Example { // Persistent state variables stored: Cell = // Init package for Wallet V3R1 as a base64-encoded BoC cell("te6cckEBAQEAYgAAwP8AIN0gggFMl7qXMO1E0NcLH+Ck8mCDCNcYINMf0x/TH/gjE7vyY+1E0NMf0x/T/9FRMrryoVFEuvKiBPkBVBBV+RDyo/gAkyDXSpbTB9QC+wDo0QGkyMsfyx/L/8ntVD++buA="); // works at compile-time!}slice
Available since Tact 1.5fun slice(bocBase64: String): Slice;A compile-time function that embeds a base64-encoded BoC bocBase64 as a Slice into the contract.
Usage example:
contract Example { // Persistent state variables stored: Slice = // Works at compile-time! slice("te6cckEBAQEADgAAGEhlbGxvIHdvcmxkIXgtxbw="); // Hello world!}rawSlice
Available since Tact 1.5fun rawSlice(hex: String): Slice;A compile-time function that converts the hex String, containing hex-encoded and optionally bit-padded contents, into a Slice and embeds it into the contract.
Contents are bit-padded if there is an underscore _ at the very end of the String. Padding removes all trailing zeros and the last bit before them:
// Not bit-paddedrawSlice("4a").loadUint(8); // 74, or 1001010 in binary
// Bit-paddedrawSlice("4a_").loadUint(6); // 18, or 10010 in binaryNote that this function is limited and only allows specifying up to bits.
Usage example:
contract Example { // Persistent state variables stored: Slice = rawSlice("000DEADBEEF000"); // CS{Cell{03f...430} bits: 588..644; refs: 1..1} bitPadded: Slice = rawSlice("000DEADBEEF000_"); // CS{Cell{03f...e14} bits: 36..79; refs: 0..0}}ascii
Available since Tact 1.5fun ascii(str: String): Int;A compile-time function that concatenates the numerical values of the characters in the non-empty string str into one value of the Int type. This function works only for strings occupying up to 32 bytes, allowing the representation of up to 32 ASCII codes or up to 8 Unicode code points of 4 bytes each, so the resulting non-negative integer fits into 256 bits.
To understand how the ascii function works, consider the following pseudo-Tact example involving hexadecimal escape sequences:
ascii("NstK") == ascii("\x4e\x73\x74\x4b") == 0x4e73744b == 1316189259Each ASCII character in the string "NstK" is represented by its hexadecimal escape sequence, which is then converted to the corresponding integer value by concatenating all the bytes.
The ascii builtin assumes the string is encoded in UTF-8. For example, the ⚡ character’s UTF-8 encoding is 0xE2 0x9A 0xA1, so 0xE2 will be the most significant byte.
The builtin can be used to create human-readable encodings for some actions or operations.
Usage example:
message(42) Action { action: Int as uint256;}
contract Example { receive(msg: Action) { if (msg.action == ascii("start")) { // Do something } else if (msg.action == ascii("stop")) { // Do something else } }}crc32
Available since Tact 1.5fun crc32(str: String): Int;A compile-time function that computes a checksum using the CRC-32 algorithm and embeds the resulting Int value into the contract.
Usage example:
contract Example { // Persistent state variables checksum: Int = crc32("000DEADBEEF000"); // 1821923098}ton
fun ton(value: String): Int;A compile-time function that converts the given Toncoin value from a human-readable format String to the nanoToncoin Int format.
Usage example:
contract Example { // Persistent state variables one: Int = ton("1"); // One Toncoin, which is equivalent to 10^9 nanoToncoins pointOne: Int = ton("0.1"); // 0.1 Toncoin, which is equivalent to 10^8 nanoToncoins nano: Int = ton("0.000000001"); // 10^-9 Toncoin, which is equivalent to 1 nanoToncoin // Works at compile-time!}