Stay in Tact with TON
This article is a script of a talk we gave at TON Korea meetup telling the community about the newest tools for development on TON
First, what’s so special about TON compared to other blockchains? There are a couple of cool features:
- Easy integration with Telegram
- Everything on-chain is a smart contract(even accounts and wallets)
- Calls between contracts are asynchronous
- Contract sharding is the main consensus
These provide infinite scaling, predictable gas prices, and cheap execution. But my personal favorite is that there is no need for a proxy, you can make your contracts mutable to fix mistakes and easily update them. However, there’s one big problem with TON.
FunC development is not fun at all!!
FunC is a very low-level language: you need to deal with TL-B schemas, TVM opcodes, and lots of hashing algorithms and ideally understand all of them. Furthermore, it has its own variable types: Cells and slices with which you have to deal regularly. Then it has the authentic syntax: functions and methods declaration vary significantly from the ones we are used to seeing, and there is a lot of syntax sugar. And my personal pain is the fact that there is only one method to deal with all the incoming messages.
That makes contracts very messy. All this sets a very high entry threshold for devs. But recently, a way to loosen the burden appeared.
Tact — the savior of all FunC developers
A team of devs from TonWhales, Tonkeeper, and Ton core teams created a new programming language: Tact. The first version was released in March and is regularly updated. TACT is expected to become the main language for TON smart contract development. The main features are these:
- it introduces Object-oriented programming elements giving the opportunity to recycle code structures
- resembles Kotlin, Rust, Typescript, and Solidity in a way
- has built-in methods like initializer
- you don’t have to deal with cells and slices
- has ABI support, so you don’t need to use the client to access smart contracts
TACT is all about real necessities for devs
Awesome tools for Tact development
I use it together with the blueprint framework, which provides easy creation, compilation, and deployment for smart contracts. The blueprint framework has a testing suite — Sandbox, which makes testing comfortable and smooth. Another cool tool is Ton connect, which supports wallets like Tonkeeper, OpenMask, TonHub, etc., and makes payment processing a piece of cake.
Getting to the code
We also recorded a video walkthrough of the code
To quickly get started let’s use the blueprint framework, it’ll scaffold the project with npm.
npm create ton@latest
After this command it'll prompt you to specify your project name, first contract name, and a template - choose "An empty contract (TACT)" here.
In the project directory, under contracts/imports
is our first smart contract - balance.tact
. Let's write some code now
import "@stdlib/deploy";
import "@stdlib/ownable";
message Withdraw{
amount : Int as coins;
}
contract Balance with Deployable, Ownable {
balance: Int as uint256 = 0;
owner: Address;
init() {
// sender() is a built-in function which reads the sender of the message
self.owner = sender();
}
// Deposit TON to our smart contract
receive("Deposit"){
// context() returns data of the incoming message: sender, value, type(bounced, not bounced) and its raw representation
self.balance = self.balance + context().value;
}
receive(msg: Withdraw){
// A built-in method to check that the message is sent by the owner of the contract
self.requireOwner();
require(msg.amount < self.balance , "Too much!");
self.balance = self.balance - msg.amount;
send(SendParameters{
to: sender(),
value: msg.amount,
mode: SendIgnoreErrors,
body: "Get your tokens!".asComment()
});
}
get fun balance(): Int{
return self.balance;
}
}
Build your smart contract with this command
npx blueprint build
You should see a couple of files appear under build/Balance
directory. The most interesting is the compiled FunC code - tact_Balance.code.fc
.
Looking at it further, you might notice that it’s not that Gas efficient. In other words, if you were to write the same thing in FunC you’d probably save 20% on gas costs, but because Gas is so cheap on TON, it’s not an issue.
Also, Tact plans to compile code straight to fift — an extremely low-level language for the TON Virtual Machine and TON Blockchain. This might lead to Tact being on par, if not more gas-efficient than FunC. Exciting times are ahead for Tact.
Special thanks
Thanks to the lead Blockchain Developer at MiKi Digital — Vladislav Lenskiy, for preparing this awesome talk.
Thanks to Lucy Kang for hosting the event and helping drive the adoption of TON in Korea.
And a very warm thank you to the team behind Tact language — Steve Korshakov, Tal Kol, Oleg Andreev, Kirill Emelyanenko, Kirill Maev, and others from Tonkeeper, TON Whales, TON core, etc.
If you need any web3 help feel free to reach out to us at at our website or book a call