Tolk language: overview
Tolk is a new language for writing smart contracts in TON. Tolk compiler is a fork of FunC compiler, introducing familiar syntax similar to TypeScript but leaving all low-level optimizations untouched. Think of Tolk as the next‑generation FunC.
import "storage.tolk"
fun loadData() {
ctxCounter = getContractData().beginParse().loadUint(32);
}
fun onInternalMessage(msgValue: int, msgFull: cell, msgBody: slice) {
var cs = msgFull.beginParse();
var flags = cs.loadMessageFlags();
if (isMessageBounced(flags)) {
return;
}
...
}
get currentCounter(): int {
loadData(); // fills global variables
return ctxCounter;
}
See same logic implemented with FunC
#include "storage.fc";
() load_data() impure {
slice cs = get_data().begin_parse();
ctx_counter = cs~load_uint(32);
}
() recv_internal(int msg_value, cell msg_full, slice msg_body) impure {
slice cs = msg_full.begin_parse();
int flags = cs.load_uint(4);
if (flags & 1) {
return ();
}
...
}
int currentCounter() method_id {
load_data(); ;; fills global variables
return ctx_counter;
}
Try a FunC → Tolk converter
Tolk vs FunC differences
Motivation behind Tolk
FunC is awesome. It is low-level and encourages a programmer to think about compiler internals. It gives complete control over TVM assembler, allowing a programmer to make his contract as effective as possible. If you get used to it, you love it.
But there is a problem. FunC is "functional C", and it's for ninja. If you are keen on Lisp and Haskell, you'll be happy. But if you are a JavaScript / Go / Kotlin developer, its syntax is peculiar for you, leading to occasional mistakes. A struggle with syntax may decrease your motivation for digging into TON.
Imagine what if there was a language that was smart and low-level but not functional and not like C? Leaving all beauty and complexity inside, what if it would be more similar to popular languages at first glance?
That's what Tolk is about.
Migrating from FunC to Tolk
If you know FunC and want to try a new syntax, your way is:
- Read Tolk vs FunC: in short.
- With a blueprint, create a new Tolk contract (for example, a counter) and experiment around it. Remember that almost all stdlib functions are renamed to
verboseclear names. Here is a mapping. - Try a converter for your existing contracts or one from FunC Contracts. Remember that contracts written in Tolk from scratch look nicer than auto-converted ones. For instance, using logical operators instead of bitwise tremendously increases code readability.
How to try Tolk if you don't know FunC
This section describes the Tolk vs FunC differences. Later, it will be adapted to land newcomers. Moreover, FunC will eventually become deprecated for onboarding, and all code snippets throughout the documentation will be rewritten for Tolk.
If you are new to TON, your way is:
- Dig into this documentation to acquire basic on development in TON. No matter your language, you must be aware of cells, slices, and TON asynchronous nature.
- Facing FunC snippets, you can still use FunC or try to express the same in Tolk. If FunC syntax is peculiar to you, don't worry: the goal of Tolk is to fix this issue precisely.
- Once you understand what's happening, try using Tolk with blueprint.
Tooling around Tolk language
Sources of the Tolk compiler are a part of the ton-blockchain
repo.
Besides the compiler, we have:
- tolk-js — a WASM wrapper for Tolk compiler.
- JetBrains IDE plugin supports Tolk besides FunC, Fift, TL/B, and Tact.
- VS Code Extension enabling Tolk Language support.
- Converter from FunC to Tolk — convert a
.fc
file to a.tolk
file with a singlenpx
command. - Tolk Language is available in blueprint.
Is Tolk production-ready?
The Tolk compiler, a fork of the FunC compiler, is deemed production-ready, albeit somewhat experimental at the moment.
Undiscovered bugs may exist, potentially inherited from FunC or attributable to TVM characteristics. Anyway, no matter your language, you should cover your contracts with tests to reach high reliability.
Roadmap
The first released version of Tolk is v0.6, emphasizing missing FunC v0.5.
Here are some points to investigate:
- type system improvements
- structures and generics
- auto-pack structures to/from cells, probably integrated with message handlers
- methods for structures, generalized to cover built-in types
- easier message sending
- better experience for everyday use cases: jettons, NFT, etc.
- gas and stack optimizations, AST inlining
- extending and maintaining stdlib
- some ABI (how explorers "see" bytecode)
- gas and fee management in general
The following strategic milestone for Tolk v1.0 is structures with auto-serialization into cells.
This milestone eliminates manual manipulations with builders and slices, allowing data and messages to be described declaratively. Closely related to this is the ABI (interface) of contracts. Well-designed structures make up the majority of an ABI.
Issues and contacts
If you face an issue, connect to developer society on TON Dev Chats or create GitHub issues.