Behind the Scenes of TON: Lessons Learned on Deploying Smart Contracts, Part 1

Published at: Dec. 15, 2019

Amid news of its recent spat with the United States Securities and Exchange Commission, relatively few people have likely heard about Telegram’s Open Network competition that occurred some weeks ago. It was a milestone event that transformed the once-miniscule TON developer community writing in Fift (TON’s general-purpose programming language), which differs greatly from common languages due to its low-level approach.

The contest attracted new developers, further built a community around the new platform, and solved existing issues concerning the lack of documentation on smart contracts by providing examples of them and how they are deployed on TON. My team, Button Wallet, and I also participated, and have summed up everything that happened during and after the TON contest.

Lack of documentation

FunC — TON’s other language for writing smart contracts is FunC, a high-level language similar to C with functions and variables that is much easier to read and write in than the general-purpose Fift. A similar comparison would be C# and CIL. However, prior to the contest, there was barely any documentation of FunC. This posed an issue, as most of the TON contest tasks required contestants to write a smart contract.

Due to the lack of documentation on writing for TON using FunC, contest participants needed to analyze and learn from existing examples, which had been uploaded to a small GitHub repository and the TON test website along with some theoretical details. While this wasn’t a difficult task overall, understanding the basic necessities of a language based on nothing but examples can prove challenging. Nevertheless, most contestants were able to start writing freely in FunC after the first few days.

Basics — When writing a smart contract using FunC, we needed to understand how to deploy and compile the smart contract, as well as how to call functions using arguments. The complete lack of detailed information on this fundamental aspect of the language — in addition to there not being any examples showing the full steps — was almost laughable for its irony. TON’s brief guideline was extremely helpful to those who participated in the competition, but it was only an introduction to writing smart contracts on TON that conveniently left out examples or detailed information on deploying and running function calls on smart contracts. Eventually, everyone figured out how to do all of this, but it took considerable time and effort.

Contest tasks

There were two tasks out of the five total that I want to highlight: the asynchronous payment channel and the synchronous one. But first, what is a payment channel?

A payment channel is a way to send transactions between two parties off-chain (i.e., outside of the blockchain) to make it faster, less expensive and more customized. Users have their own accounts on the blockchain and can send transactions between themselves using their deposited sums. For this, a special smart contract stores the deposited funds of the two parties while the payment channel is open. Withdrawals require a smart contract containing certain data, which will be discussed below.

Parties A and B send coins to the smart contract, making deposits to open a payment channel between them

To open the payment channel, funds need to be deposited to the smart contract from both sides.

Party A sends a transaction to Party B, changing the state of payment channel from (a, b) to a new one

If the payment channel is open, transactions can be sent between parties with a speed of more than 100,000 transactions per second. It is important to understand that this all happens off-chain and that at some point, the parties will need to reach an agreement and withdraw their funds from the smart contract.

An off-chain transaction from A to B through their synchronous payment channel

It is assumed that the parties might try to unfairly withdraw all funds within the pool. Because of this, each party needs to prove that the sum they are going to withdraw belongs to them. To prove this, each party needs to send their signature, which correctly proves the state (sum A, sum B, and some other information).

The synchronous payment channel has a state number that does not change unless specific requirements, such as receiving Party B’s confirmation and signature, are met. Therefore, Party A cannot send several transactions to B in a row, as each new state requires a signature from both parties.

When sending a transaction to Party B, Party A needs to create a state that will change the amount that will belong to both A and B, sign this state using its own private key, and then send the new state and signature to B. Party B then signs this state and sends its signature back to Party A, thereby confirming the transaction status. Party A cannot send another transaction to Party B until B creates a new state. Due to this, it is called a synchronous channel.

An off-chain transaction from Party A to Party B through an asynchronous payment channel

In an asynchronous payment channel, each counterparty has its own group of states. Each state consists of the amount that Party A received from Party B, the number of transactions that Party A has sent to Party B, the amount that B sent to A, and the number of transactions that B has sent to A. Unlike in a synchronous payment channel, A and B do not need to wait for confirmation from one another, they just need to send a signed state.

The most difficult aspect for both channels is the withdrawal process. The smart contract needs to check that each party has provided the correct data before funds can be withdrawn. We need to check the signatures of state and also that this state is the latest. If there is a conflict between the parties, the smart contract needs to resolve it according to the latest state.

There must be protection against sending the same data to different payment channels, as well as a resolution for when one party does not provide any information. All of this must be written in FunC and thoroughly tested in order to be secure.

Solutions and competitors

According to the contest’s official channel, there are 68 submissions.

Most of the submissions were multisignature wallets and domain name system (or DNS) resolvers. However, several submissions are for payment channels. Writing payment channels was the competition’s most complex task, so the few strong teams (such as 363, 375, 381) that were able to produce such solutions were more prominent than the others.

What’s next?

Currently, there are about 10 to 20 teams with enough skills and knowledge to start building TON’s infrastructure, and they will likely transfer most of the successful Ethereum solutions to TON. The TON contest and its $200,000 prize pool incentive greatly boosted the number of teams that can work with the platform by providing an opportunity to gain experience with it first-hand. Now, the participating teams are all set to build their own projects or startups on TON.

Key takeaways for the Button Wallet team 

As early TON adopters and lovers of Telegram, my team and I decided to participate in the TON contest. We had a large team of eight people. For our solution, we decided to build synchronous and asynchronous payment channels. Although we made some mistakes during the contest, we did manage to make a synchronous payment channel with all the wrappers that allow deposits, withdrawals, and the transfer of transactions using our command-line interface. So, we made not only a smart contract but a lot of wrappers over it that allowed working with TON much easier. After we finished the synchronous channel, we leveraged knowledge from our Plasma developing experience and built another smart contract that implements asynchronous off-chain payments. However, we were unable to write all the wrappers to it before the deadline.

We thoroughly enjoyed all the tasks and wish that we’d had more time to implement all of them. Nevertheless, we won the first place for best synchronous payment channel as well as third place for best asynchronous payment channel.

The views, thoughts and opinions expressed here are the author’s alone and do not necessarily reflect or represent the views and opinions of Cointelegraph.

Nick Kozlov is the CTO of Button Wallet, a software developer and researcher, as well as one of the winners of the TON contest.

Tags
Ton
Related Posts
Crypto project brings apps and smart contracts to Bitcoin
A blockchain project says it makes Bitcoin programmable in a way that it hasn’t been before — enabling apps and smart contracts to be brought to the network. Stacks (formerly known as Blockstack) aims to position BTC as the foundation for a better internet that’s owned by users. Those behind this ecosystem argue that the internet is broken, with tech giants dominating the landscape. Stacks is a Layer 1 blockchain that uses Bitcoin’s infrastructure as a secure base layer. Smart contracts and apps can be enabled with little modification, with “minimal transaction load” on the network. Underpinning how scalable Stacks …
Blockchain / Jan. 14, 2021
The cryptocurrency sector is overflowing with dead projects
In 2017, when everything concerned with cryptocurrency and blockchain still looked fresh and interesting, it seemed that there would be no day without a new “revolutionary” project or idea. Decentralized financial system, decentralized torrent tracker, decentralized office documentation system. Decentralized, decentralized, decentralized. The overuse of terms like “transparent,” “distributed” and “blockchain-based” soon made most press papers look generic. The closer we got to the peak of the Bitcoin price at the end of 2017, the more absurd the names of new projects became: “Ethereum-based payment system for slaughterhouse industry workers,” “decentralized blockchain-based dwarf horse breeding platform,” “peer-to-peer personal banking service …
Technology / Nov. 8, 2020
Report: Telegram’s TON Blockchain to Be Compatible with Ethereum DApps
Telegram Open Network’s (TON) blockchain will allow developers to port their Ethereum (ETH) smart contracts. As industry news outlet CoinDesk reported on Aug. 30, software startup TON Labs managed by Telegram’s token offering investors is building a Solidity compiler for its blockchain. This programming language is primarily used to develop smart contracts in the Ethereum network. Assembly might be required Per the report, the tool would allow developers to port their Ethereum smart contracts over to TON, but it is not specified whether some changes in their code would be required. TON Labs CEO and managing partner Alexander Filatov told …
Blockchain / Sept. 1, 2019
‘Free Transactions Cleared in Five Seconds’: Platform to Help Businesses Use Blockchain
A startup is aiming to attract social networks, gaming companies and mobile payment providers through a “full-service platform” that eliminates the headaches associated with using blockchain technology to reach customers. Aerum says its offering is geared towards entrepreneurs and businesses that create DApps (decentralized apps) based on the Ethereum protocol. According to the company, its infrastructure tackles the high transaction costs, lack of scalability and long processing times which have made blockchain an impractical choice for many firms – hampering its progress to become a mainstream force in the global economy. Through its platform, Aerum claims it can offer transactions …
Blockchain / Nov. 7, 2018
Universal Wallet Becomes the ‘First’ to Support Cardano’s ADA on Mobile
The company behind a multi-asset, universal wallet that can store major crypto and smart contract tokens in one place says it has become the first mobile wallet to support the ADA cryptocurrency. Infinito Wallet says the milestone coincides with an update to version 1.15.0 of its platform – enabling users to “securely send, receive and check transaction history of ADA.” According to the company, this development is significant because very few wallets on the market support ADA, and there is no such app for mobile devices. Infinito developers have worked with the development team behind Cardano and IOHK, and they …
Blockchain / Sept. 27, 2018