[ad_1]
That is the second article in our sequence on integrating fee channels on Telegram Open Community. Within the first half, we launched the community, detailed our expertise of the competition, and defined how synchronous and asynchronous good contracts work. As the subsequent addition to the sequence, this text particulars how we constructed a synchronous fee channel on the community throughout TON’s contest again in September. Right here, we shall be speaking solely about Fift (TON’s general-purpose programming language) and FunC (TON’s programming language for writing good contracts).
The TON white paper gives extra in-depth details about fee channels, however we’ll briefly clarify them once more.
Associated: Behind the Scenes of TON: Classes Discovered on Deploying Sensible Contracts, Half 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 social gathering 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 write down to the blockchain. There are two fundamental operations: deposit and withdraw. The withdrawal is probably the most difficult one to implement.
To make an accurate withdrawal, customers want to offer the most recent details about the state of their channel. The state consists of the steps and digital signatures of every participant, which implies it’s not potential to offer an accurate state with knowledge that has not been accepted by each events.
To deploy a wise contract, you might 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 shall be linked to one another. GRAMs then should be despatched to the tackle that was obtained throughout deploy script execution. As soon as GRAMs are on the tackle, ship the .boc file to the community and the contract shall be deployed.
To make a operate name, write a script that can ship an exterior message to the deployed good contract.
Principally, something on TON is a cell with some references. A bag of cells is an information construction that was designed by the Telegram crew. It’s an actor mannequin. Extra particulars are at TON whitepaper: “every part is a bag of cells.” You might be constructing a cell that can work together with one other cell when it’s deployed.
Every peer-to-peer fee channel is a single good contract. Let’s check out the segments of a wise contract.
Associated: What to Anticipate From the Telegram Open Community: 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 gentle shopper.
The newest cell on the stack is the results of executing the above Fift script.
The same old segments of a Fift deploy script embody (however should not restricted to):
- Code of the good contract as a single cell (normally written in FunC, then compiled into Fift ASM code and included in the primary .fif file utilizing path-to-compiled-asm.fif).
- Preliminary storage of the good contract (see beneath).
- New good contract tackle (the hash from the preliminary state of the good contract that additionally consists of the good contract code cell and the preliminary storage cell).
- Arguments of the primary name of the recv_external operate (the quantity of arguments and sort relies on the contract).
- An exterior message cell for initialization, which shall be serialized into bytes and packed to the .boc file, which consists of all the information from factors 1–four and a few further ones which are nonetheless missing documentation.
When the .boc is compiled, a certain amount of GRAMs should be despatched to the good contract tackle. The .boc file should be despatched to the community to initialize the good contract. The quantity of GRAMs relies on the scale and quantity of calculations of the deployed good contract’s exterior message cell (not solely the code of it). Fuel × fuel worth is taken from the deployed good contract stability. This quantity is the minimal wanted to pay for fuel in the course of the deployment.
A illustration of the storage:
- seqno 32 bits
- contract_status four bits
- first_user_pubkey. The primary social gathering’s public key 256 bits
- second_user_pubkey. The second social gathering’s public key 256 bits
- time_to_send. Time to ship after the very first state being submitted 32 bits (legitimate till 2038)
- depositSum. The deposited sum of two individuals as much as 121 bits
- state_num 64 bits. The present quantity of states that occurred
A cell accommodates as much as 1023 bits and 4 references to different cells. We had been in a position to match your complete storage onto one cell and not using a single reference. Our storage can take up a most of 765 bits.
All good 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 potential to offer a state to the good contract
0x5 — Person 1 has supplied the state
0x6 — Person 2 has supplied the state
0x7 — The channel is closed
Depositing
The deposit operate receives a message from a easy pockets (switch) with a further physique payload.
Depositing GRAMs to the channel:
- The person generates a further physique payload that features a message (for instance, 1 bit) and its signature in a separate .fif file.
- Physique payload is compiled to a .boc file.
- Physique payload is loaded from this .boc file right into a .fif file as a body-cell “transferring” reference (the .fif is accountable for transferring GRAMs from the pockets).
- The recv_external operate is named with arguments (the deposit quantity and the vacation spot tackle of the channel) when the compiled .fif file is shipped to the community.
- The send_raw_message operate is executed. Deposited GRAMs and extra physique payload is shipped to a P2P channel good contract vacation spot tackle.
- The recv_internal operate of the P2P channel good contract is named. GRAMs are obtained by channel contracts.
The deposit operate may be known as if the state of the P2P channel good contract is 0x1 or 0x2 or 0x3.
FunC code that checks the state:
Solely the house owners of the general public keys (written within the preliminary storage) are allowed to make a deposit. The good contract checks the signature of every inner message that shall be obtained by the recv_internal operate. If the message is signed by one of many public key house owners, the contract standing modifications 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 modifications to 0x4 on the identical operate name.
The FunC code accountable for altering contract standing:
Refund
Funds may be returned if a counterparty has not made a deposit on time.
To try this, a person wants to offer their tackle and signature through exterior message. The funds shall be refunded if the supplied 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 that’s accountable for verifying the refund software:
Withdrawal
Every individual ought to present an exit state, the signature of this state, and signature of the physique message.
State particulars:
- Sensible contract tackle (to exclude the potential of coming into the right state from the earlier P2P channel with the identical individuals).
- The ultimate stability of the primary participant.
- The ultimate stability of the second participant.
- State quantity.
The physique message signature is saved in the primary slice, the state is saved in a separate reference, and state signatures are saved as references to a “signatures” reference to keep away from cell overflow.
Withdrawal steps:
-
Examine the physique message signature and decide the participant.
-
Examine that it’s the flip of the participant or 24 hours have handed for the reason that final entered state. Write the flip of the present participant (0x5 or 0x6) to the contract standing.
An instance of an accurate signature of the physique message for the proprietor of first_user_pubkey:
We then must confirm that the good contract tackle written to the state is the precise contract tackle:
Subsequent, we have to confirm signatures underneath the state:
After that, there are two assertions:
- The deposited quantity from the storage needs to be equal to the sum of the entire balances of the individuals.
- The brand new entered state quantity should be better than or equal to the earlier one.
In case of new_state_num > state_num we have 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 precise contract standing (0x5 if first participant made a name, in any other case 0x6).
In one other case, if new_state_num == state_num we have to put a further two references to the “signatures” reference with addresses of every participant and signatures underneath their addresses.
If the signatures are right, GRAMs are withdrawn from one tackle and put into the proprietor’s tackle.
Every time a profitable name occurs, we have to retailer all storage knowledge even when it doesn’t change.
Unsolved points
The belief is that the primary person deployed the contract and the individuals agreed on commissions. The settlement on commissions in our case is reaching off-chain.
Now we have not but found out find out how to calculate the entire fee, making an allowance for the truth that gamers can write an irrelevant state and file precise states after that. Remember the fact that we have to pay charges from the P2P channel good contract every time we efficiently name recv_internal or recv_external features.
As talked about earlier, we have to add some quantity of GRAMs to a non-bounceable future good contract tackle with a purpose to initialize it.
On the final day of the competitors, TON’s builders made a decide to the stdlib.fc library with a brand new operate that permits getting the precise good contract stability.
Options for potential options to this downside are welcome!
Conclusion
FunC and Fift enable any developer entry to the low-level world of software program engineering, opening new alternatives and options for blockchain builders who’ve already gotten used to Ethereum or every other good contract platform. It will be important that TON is a sharded blockchain, so implementing good contracts on it is tougher. For instance, Ethereum’s contracts run synchronously and don’t require dealing with conditions resembling ready for a solution from one other contract.
The asynchronous method of good contract communication is the one choice to make it scalable, and TON has these choices. Our answer ended up being harder to implement than Solidity, however there’s all the time a trade-off. It’s positively potential to construct a complicated good contract on TON, and the way in which that TON’s crew dealt with it is extremely spectacular. We’re trying ahead to seeing extra libraries and instruments that can assist to deploy and construct FunC contracts.
We completely loved all of the duties and need that we’d had extra time to implement all of them. Nonetheless, we received two prizes at TON Contest: first place for greatest synchronous fee channel in addition to third place for greatest asynchronous fee channel.
We’ll share our personal private suggestions partly three.
The views, ideas and opinions expressed listed here are the authors’ alone and don’t essentially replicate or characterize the views and opinions of Cointelegraph.
This text was co-authored by Nick Kozlov and Kirill Kuznetsov.
Nick Kozlov is the CTO and co-founder of Button Pockets, a software program developer and researcher, in addition to one of many winners of the TON contest.
Kirill Kuznetsov is the co-founder of Button Pockets, in addition to one of many winners of the TON contest.
[ad_2]
Source link