Classes Realized on Deploying Good Contracts, Half 2

HomeCrypto News

Classes Realized on Deploying Good Contracts, Half 2

That is the second article in our collection on integrating fee channels on Telegram Open Community. Within the first half, we introduced the comm


That is the second article in our collection on integrating fee channels on Telegram Open Community. Within the first half, we introduced the community, detailed our expertise of the competition, and defined how synchronous and asynchronous sensible contracts work. As the subsequent addition to the collection, this text particulars how we constructed a synchronous fee channel on the community throughout TON’s contest again in September. Right here, we can be speaking solely about Fift (TON’s general-purpose programming language) and FunC (TON’s programming language for writing sensible contracts).

The TON white paper provides extra in-depth details about fee channels, however we’ll briefly clarify them once more.

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

A synchronous fee channel permits sending transactions between two customers off-chain utilizing on-chain property. In our case — GRAMs. It’s unattainable for one occasion to cheat the opposite off-chain, and transactions are made a lot sooner than executing layer-one blockchain transactions, as solely person units are used to finish them with out having to jot down to the blockchain. There are two fundamental operations: deposit and withdraw. The withdrawal is essentially the most difficult one to implement.

To make an accurate withdrawal, customers want to offer the newest details about the state of their channel. The state consists of the steps and digital signatures of every participant, which implies it’s not doable to offer an accurate state with knowledge that has not been permitted by each events.

To deploy a sensible contract, you want to write a deploy script in Fift and compile it to a .boc (bag of cells) file. Doing this makes a number of cells that can be linked to one another. GRAMs then should be despatched to the handle that was obtained throughout deploy script execution. As soon as GRAMs are on the handle, ship the .boc file to the community and the contract can be deployed.

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

Mainly, something on TON is a cell with some references. A bag of cells is a knowledge construction that was designed by the Telegram group. It’s an actor mannequin. Extra particulars are at TON whitepaper: “all the pieces is a bag of cells.” You’re constructing a cell that can work together with one other cell when it’s deployed.

Every peer-to-peer fee channel is a single sensible contract. Let’s check out the segments of a sensible contract.

Associated: What to Expect From the Telegram Open Network: A Developer’s Perspective

Deployment half

A serialized Fift script is used to deploy a contract. It’s saved to a .boc file and despatched to the community through TON Cli, the community’s mild shopper.

The most recent cell on the stack is the results of executing the above Fift script.

The same old segments of a Fift deploy script embody (however will not be restricted to):

  1. Code of the sensible contract as a single cell (often written in FunC, then compiled into Fift ASM code and included in the principle .fif file utilizing path-to-compiled-asm.fif).
  2. Preliminary storage of the sensible contract (see under).
  3. New sensible contract handle (the hash from the preliminary state of the sensible contract that additionally consists of the sensible contract code cell and the preliminary storage cell).
  4. Arguments of the primary name of the recv_external operate (the quantity of arguments and kind is determined by the contract).
  5. An exterior message cell for initialization, which can be serialized into bytes and packed to the .boc file, which consists of all the info from factors 1–four and a few extra ones which are nonetheless missing documentation.

When the .boc is compiled, a certain amount of GRAMs should be despatched to the sensible contract handle. The .boc file should be despatched to the community to initialize the sensible contract. The quantity of GRAMs is determined by the dimensions and quantity of calculations of the deployed sensible contract’s exterior message cell (not solely the code of it). Fuel × gasoline worth is taken from the deployed sensible contract stability. This quantity is the minimal wanted to pay for gasoline through the deployment.

A illustration of the storage:

  1. seqno 32 bits
  2. contract_status four bits
  3. first_user_pubkey. The primary occasion’s public key 256 bits
  4. second_user_pubkey. The second occasion’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 two individuals as much as 121 bits
  7. state_num 64 bits. The present quantity of states that occurred

A cell comprises as much as 1023 bits and 4 references to different cells. We had been capable of match all the storage onto one cell with no single reference. Our storage can take up a most of 765 bits.

All sensible contract states

0x0 — Deployment state

0x1 — Channel opened and prepared for deposit

0x2 — Deposit by person 1

0x3 — Deposit by person 2

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

0x5 — Person 1 has supplied the state

0x6 — Person 2 has supplied the state

0x7 — The channel is closed

Depositing

The…



nasdaq.com