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

That is the second one article in our sequence 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 sensible contracts paintings. As the following addition to the sequence, this newsletter main points how we constructed a synchronous cost channel at the community all over TON’s contest again in September. Right here, we will be able to be speaking simplest about Fift (TON’s general-purpose programming language) and FunC (TON’s programming language for writing sensible contracts).

The TON white paper supplies extra in-depth details about cost channels, however we will be able to in brief provide an explanation for them once more.

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

A synchronous cost channel permits sending transactions between two customers off-chain the use of on-chain property. 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 simplest consumer gadgets are used to finish them with no need to write down to the blockchain. There are two fundamental operations: deposit and withdraw. The withdrawal is essentially the most difficult one to enforce.

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

To deploy a sensible contract, you want to write down a deploy script in Fift and bring together it to a .boc (bag of cells) document. Doing this makes a couple of cells that will likely be connected to one another. GRAMs then wish to be despatched to the deal with that used to be won all over deploy script execution. As soon as GRAMs are at the deal with, ship the .boc document to the community and the contract will likely be deployed.

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

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

Every peer-to-peer cost channel is a unmarried sensible contract. Let’s check out the segments of a sensible 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 mild consumer.

The newest cellular 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 sensible contract as a unmarried cellular (typically written in FunC, then compiled into Fift ASM code and integrated in the principle .fif document the use of path-to-compiled-asm.fif).
  2. Preliminary garage of the sensible contract (see beneath).
  3. New sensible contract deal with (the hash from the preliminary state of the sensible contract that still comprises the sensible contract code cellular and the preliminary garage cellular).
  4. Arguments of the primary name of the recv_external serve as (the quantity of arguments and sort relies on the contract).
  5. An exterior message cellular for initialization, which will likely 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 are nonetheless missing documentation.

When the .boc is compiled, a certain amount of GRAMs wish to be despatched to the sensible contract deal with. The .boc document should be despatched to the community to initialize the sensible contract. The quantity of GRAMs relies on the scale and quantity of calculations of the deployed sensible contract’s exterior message cellular (now not simplest the code of it). Fuel × fuel value is taken from the deployed sensible contract steadiness. This quantity is the minimal had to pay for fuel all over 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 happened

A cellular comprises as much as 1023 bits and 4 references to different cells. We had been ready to suit all the garage onto one cellular with out a unmarried reference. Our garage can absorb a most of 765 bits.

All sensible 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 supply a state to the sensible contract

0x5 — Person 1 has equipped the state

0x6 — Person 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 (for instance, 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 “shifting” reference (the .fif is accountable for shifting GRAMs from the pockets).
  4. The recv_external serve as is named with arguments (the deposit quantity and the vacation spot deal with of the channel) when the compiled .fif document is shipped to the community.
  5. The send_raw_message serve as is finished. Deposited GRAMs and further physique payload is shipped to a P2P channel sensible contract vacation spot deal with.
  6. The recv_internal serve as of the P2P channel sensible contract is named. GRAMs are won by way of channel contracts. 

The deposit serve as can also be referred to as if the state of the P2P channel sensible contract is 0x1 or 0x2 or 0x3.

FunC code that tests the state:


Most effective the house owners of the general public keys (written within the preliminary garage) are allowed to make a deposit. The sensible contract tests the signature of each and every inner message that will likely be won throughout 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 accountable for converting contract standing:



Price range can also be returned if a counterparty has now not made a deposit on time.

To do this, a consumer wishes to supply their deal with and signature by means of exterior message. The price range will likely 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 accountable for verifying the refund software:



Every individual must supply an go out state, the signature of this state, and signature of the physique message.

State main points:

  1. Sensible contract deal with (to exclude the opportunity of coming into the proper 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 cellular overflow.

Withdrawal steps:

  1. Test the physique message signature and resolve the player.


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

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


We then wish to check that the sensible contract deal with written to the state is the real contract deal with:


Subsequent, we wish to check signatures below the state:


After that, there are two assertions:

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


In case of new_state_num > state_num we wish 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 choice, another way 0x6).

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

If the signatures are proper, GRAMs are withdrawn from one deal with and put into the landlord’s deal with.


Every time a a success name occurs, we wish to retailer all garage knowledge despite the fact that it doesn’t trade.

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 achieving off-chain.

We now have now not but found out tips on how to calculate the full fee, making an allowance for the truth that avid gamers can write an beside the point state and report precise states after that. Needless to say we wish to pay charges from the P2P channel sensible contract each and every time we effectively name recv_internal or recv_external purposes.

As discussed previous, we wish to upload some quantity of GRAMs to a non-bounceable long run sensible contract deal with with a view to initialize it.

At the closing 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 sensible contract steadiness.


Tips for conceivable answers to this drawback are welcome!


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

The asynchronous means of sensible contract verbal exchange is your best option to make it scalable, and TON has those choices. Our answer ended up being harder to enforce than Solidity, however there may be all the time a trade-off. It’s indubitably conceivable to construct a complicated sensible contract on TON, and the best way that TON’s workforce treated it is extremely spectacular. We’re taking a look ahead to seeing extra libraries and gear that can assist to deploy and construct FunC contracts.

We totally loved the entire duties and want that we’d had extra time to enforce they all. Nonetheless, we received two prizes at TON Contest: first position for highest synchronous cost channel in addition to 3rd position for highest asynchronous cost channel.

We will be able to percentage our personal private comments partially 3.

The perspectives, ideas and reviews expressed listed here are the authors’ on my own and don’t essentially replicate or constitute the perspectives and reviews 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 tool 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, identification) (record, ‘script’, ‘facebook-jssdk’)); !serve as (f, b, e, v, n, t, s) if (f.fbq) go back; n = f.fbq = serve as () ; if (!f._fbq) f._fbq = n; n.push = n; n.loaded = !zero; n.model = ‘’; n.queue = []; t = b.createElement(e); t.async = !zero; t.src = v; s = b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t, s) (window, record, ‘script’, ‘https://attach.fb.internet/en_US/fbevents.js’); fbq(‘init’, ‘1922752334671725’); fbq(‘observe’, ‘PageView’);

Leave a Reply

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