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

That is the second one article in our collection on integrating cost channels on Telegram Open Community. Within the first phase, we presented the community, detailed our enjoy of the competition, and defined how synchronous and asynchronous good contracts paintings. As the following addition to the collection, this newsletter main points how we constructed a synchronous cost channel at the community all through TON’s contest again in September. Right here, we can be speaking most effective about Fift (TON’s general-purpose programming language) and FunC (TON’s programming language for writing good contracts).

The TON white paper supplies extra in-depth details about cost channels, however we can in brief give an explanation for them once more.

Comparable: In the back of the Scenes of TON: Courses Realized on Deploying Sensible Contracts, Section 1

A synchronous cost channel permits sending transactions between two customers off-chain the use of on-chain belongings. In our case — GRAMs. It’s inconceivable for one birthday party to cheat the opposite off-chain, and transactions are made a lot quicker than executing layer-one blockchain transactions, as most effective consumer gadgets are used to finish them with no need to jot down to the blockchain. There are two elementary operations: deposit and withdraw. The withdrawal is probably the most difficult one to put into effect.

To make a right kind withdrawal, customers want to give you the newest details about the state in their channel. The state is composed of the stairs and virtual signatures of every player, which means that it’s now not conceivable to offer a right kind state with knowledge that has now not been licensed by way of each events.

To deploy a wise contract, you wish to have to jot down a deploy script in Fift and collect it to a .boc (bag of cells) document. Doing this makes more than one cells that might be related to one another. GRAMs then want to be despatched to the cope with that used to be won all through deploy script execution. As soon as GRAMs are at the cope with, ship the .boc document to the community and the contract might be deployed.

To make a serve as name, write a script that may ship an exterior message to the deployed good contract.

Mainly, anything else on TON is a mobile with some references. A bag of cells is an information construction that used to be designed by way of the Telegram group. It’s an actor style. Extra main points are at TON whitepaper: “the whole lot is a bag of cells.” You’re construction a mobile that may have interaction with some other mobile when it’s deployed.

Each and every peer-to-peer cost channel is a unmarried good contract. Let’s check out the segments of a wise contract.

Comparable: What to Be expecting From the Telegram Open Community: A Developer’s Viewpoint

Deployment phase

A serialized Fift script is used to deploy a freelance. It’s stored to a .boc document and despatched to the community by means of TON Cli, the community’s gentle consumer.

The newest mobile at the stack is the results of executing the above Fift script.

The standard segments of a Fift deploy script come with (however aren’t restricted to):

  1. Code of the good contract as a unmarried mobile (most often written in FunC, then compiled into Fift ASM code and incorporated in the principle .fif document the use of path-to-compiled-asm.fif).
  2. Preliminary garage of the good contract (see under).
  3. New good contract cope with (the hash from the preliminary state of the good contract that still comprises the good contract code mobile and the preliminary garage mobile).
  4. Arguments of the primary name of the recv_external serve as (the quantity of arguments and sort is dependent upon the contract).
  5. An exterior message mobile for initialization, which might be serialized into bytes and packed to the .boc document, which is composed of the entire knowledge from issues 1–four and a few further ones which might be nonetheless missing documentation.

When the .boc is compiled, a certain amount of GRAMs want to be despatched to the good contract cope with. The .boc document will have to be despatched to the community to initialize the good contract. The volume of GRAMs is dependent upon the scale and quantity of calculations of the deployed good contract’s exterior message mobile (now not most effective the code of it). Fuel × gasoline worth is taken from the deployed good contract steadiness. This quantity is the minimal had to pay for gasoline all through the deployment.

A illustration of the garage:

  1. seqno 32 bits
  2. contract_status four bits
  3. first_user_pubkey. The primary birthday party’s public key 256 bits
  4. second_user_pubkey. The second one birthday party’s public key 256 bits
  5. time_to_send. Time to ship after the very first state being submitted 32 bits (legitimate till 2038)
  6. depositSum. The deposited sum of 2 members as much as 121 bits
  7. state_num 64 bits. The present quantity of states that befell

A mobile incorporates as much as 1023 bits and 4 references to different cells. We have been in a position to suit all the garage onto one mobile with out a unmarried reference. Our garage can take in a most of 765 bits.

All good contract states

0x0 — Deployment state

0x1 — Channel opened and able for deposit

0x2 — Deposit by way of consumer 1

0x3 — Deposit by way of consumer 2

0x4 — The deposit is blocked. It’s conceivable to offer a state to the good contract

0x5 — Consumer 1 has equipped the state

0x6 — Consumer 2 has equipped the state

0x7 — The channel is closed


The deposit serve as receives a message from a easy pockets (switch) with an extra physique payload.

Depositing GRAMs to the channel:

  1. The consumer generates an extra physique payload that features a message (as an example, 1 bit) and its signature in a separate .fif document.
  2. Frame payload is compiled to a .boc document.
  3. Frame payload is loaded from this .boc document right into a .fif document as a body-cell “moving” reference (the .fif is liable for moving GRAMs from the pockets).
  4. The recv_external serve as is known as with arguments (the deposit quantity and the vacation spot cope with of the channel) when the compiled .fif document is distributed to the community.
  5. The send_raw_message serve as is completed. Deposited GRAMs and further physique payload is distributed to a P2P channel good contract vacation spot cope with.
  6. The recv_internal serve as of the P2P channel good contract is known as. GRAMs are won by way of channel contracts. 

The deposit serve as may also be known as if the state of the P2P channel good contract is 0x1 or 0x2 or 0x3.

FunC code that tests the state:


Simplest the house owners of the general public keys (written within the preliminary garage) are allowed to make a deposit. The good contract tests the signature of every interior message that might be won in the course of the recv_internal serve as. If the message is signed by way of probably the most public key house owners, the contract standing adjustments to 0x2 or 0x3 (0x2 whether it is public key 1 and 0x3 whether it is public key 2). If all customers have made a deposit, the contract standing adjustments to 0x4 at the similar serve as name.

The FunC code liable for converting contract standing:



Finances may also be returned if a counterparty has now not made a deposit on time.

To do this, a consumer wishes to offer their cope with and signature by means of exterior message. The finances might be refunded if the equipped signature belongs to public key 1 or public key 2 (individuals who made a deposit) and the contract standing is 0x2 or 0x3.

FunC code this is liable for verifying the refund utility:



Each and every individual will have to supply an go out state, the signature of this state, and signature of the physique message.

State main points:

  1. Sensible contract cope with (to exclude the potential for getting into the right kind state from the former P2P channel with the similar members).
  2. The general steadiness of the primary player.
  3. The general steadiness of the second one player.
  4. State quantity.

The physique message signature is saved in the principle slice, the state is saved in a separate reference, and state signatures are saved as references to a “signatures” connection with keep away from mobile overflow.

Withdrawal steps:

  1. Take a look at the physique message signature and resolve the player.


  1. Take a look at that it’s the flip of the player or 24 hours have handed for the reason that final entered state. Write the flip of the present player (0x5 or 0x6) to the contract standing.

An instance of a right kind signature of the physique message for the landlord of first_user_pubkey:


We then want to test that the good contract cope with written to the state is the real contract cope with:


Subsequent, we want to test signatures below the state:


After that, there are two assertions:

  1. The deposited quantity from the garage will have to be equivalent to the sum of the entire balances of the members.
  2. The brand new entered state quantity will have to be more than or equivalent to the former one.


In case of new_state_num > state_num we want to retailer new_state_num with the brand new time_to_send equaling to now() + 86401 (24 hours from the present time), and in addition write the real contract standing (0x5 if first player made a decision, differently 0x6).

In some other case, if new_state_num == state_num we want to put an extra two references to the “signatures” reference with addresses of every player and signatures below their addresses.

If the signatures are right kind, GRAMs are withdrawn from one cope with and put into the landlord’s cope with.


Each and every time a a success name occurs, we want to retailer all garage knowledge although it doesn’t alternate.

Unsolved problems

The belief is that the primary consumer deployed the contract and the members agreed on commissions. The settlement on commissions in our case is attaining off-chain.

We’ve now not but found out how one can calculate the entire fee, allowing for the truth that avid gamers can write an beside the point state and file exact states after that. Remember that we want to pay charges from the P2P channel good contract every time we effectively name recv_internal or recv_external purposes.

As discussed previous, we want to upload some quantity of GRAMs to a non-bounceable long run good contract cope with with a purpose to initialize it.

At the final day of the contest, TON’s builders made a decide to the stdlib.fc library with a brand new serve as that permits getting the real good contract steadiness.


Ideas for conceivable answers to this downside are welcome!


FunC and Fift permit any developer get right of entry to to the low-level international of instrument engineering, opening new alternatives and lines for blockchain builders who’ve already gotten used to Ethereum or every other good contract platform. It is vital that TON is a sharded blockchain, so imposing good contracts on it is tougher. As an example, Ethereum’s contracts run synchronously and don’t require dealing with eventualities akin to looking ahead to a solution from some other contract.

The asynchronous method of good contract communique is your best option to make it scalable, and TON has those choices. Our resolution ended up being harder to put into effect than Solidity, however there’s at all times a trade-off. It’s certainly conceivable to construct a sophisticated good contract on TON, and the best way that TON’s group treated it is extremely spectacular. We’re having a look ahead to seeing extra libraries and gear that may assist to deploy and construct FunC contracts.

We totally loved the entire duties and need that we’d had extra time to put into effect they all. However, we received two prizes at TON Contest: first position for perfect synchronous cost channel in addition to 3rd position for perfect asynchronous cost channel.

We will be able to proportion our personal non-public comments partially 3.

The perspectives, ideas and critiques expressed listed below are the authors’ on my own and don’t essentially replicate or constitute the perspectives and critiques of Cointelegraph.

This newsletter used to be co-authored by way of Nick Kozlov and Kirill Kuznetsov.

Nick Kozlov is the CTO and co-founder of Button Pockets, a instrument developer and researcher, in addition to probably the most winners of the TON contest.

Kirill Kuznetsov is the co-founder of Button Pockets, in addition to probably the most winners of the TON contest.


window.fbAsyncInit = serve as () FB.init(); FB.AppEvents.logPageView(); ; (serve as (d, s, identity) var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(identity)) go back; js = d.createElement(s); js.identity = identity; js.src = “http://attach.fb.internet/en_US/sdk.js”; js.async = true; fjs.parentNode.insertBefore(js, fjs); (record, ‘script’, ‘facebook-jssdk’)); !serve as (f, b, e, v, n, t, s) (window, record, ‘script’, ‘https://attach.fb.internet/en_US/fbevents.js’); fbq(‘init’, ‘1922752334671725’); fbq(‘monitor’, ‘PageView’);

Leave a Reply

Your email address will not be published. Required fields are marked *