Sat. Sep 25th, 2021
    smart contract, Free TON, RSquad

    RSquad company, involved in Free TON technology development since the launch of the project, has enough experience in Free TON development, which will be useful to everyone who is interested in blockchain and plans to develop professionally in this direction. The author of the article, Alexey Polyakov, Blockchain Practice Leader at RSquad, shares his experience.

    Differences From Other Blockchains

    Free TON has features that need to be considered when implementing business logic. First, it is an asynchronous blockchain, which immediately changes the approach to development, and second, everything here is a smart contract.

    For example, to store tokens, not only a key pair (public and private) is needed but also a deployed smart contract of the wallet.

    The point is that each address contains some number of crystals, but to change the balance at this address, there must be a code that can handle this task. It is this code that is called a smart contract.

    It should be noted that the address itself is computed from the smart contract code and some additional data, and this combination is unique. It is not possible to deploy a contract twice at the same address. The uniqueness of the addresses allows us to think of the Free TON blockchain as a distributed ledger that stores information about smart contracts.

    Smart Contracts can be developed in C++ or Solidity. The example for the article is written in Solidity.

    In this article, we will look at Free TON smart contracts, describe how they can interact with each other, and demonstrate it with an example.

    On The Free TON Blockchain, A Basis Is A Smart Contract

    It is a computer algorithm executed by a blockchain virtual machine. A smart contract monitors and ensures the fulfillment of obligations by the parties to the contract.

    Basically, each smart contract describes a class. If we consider this situation from an OOP point of view, when the contract is deployed to the blockchain, a class instance is created.

    The contract stores data and also contains business logic. Contracts can be interacted with outside the blockchain via the SDK.

    On the other hand, smart contracts interact with each other. Any interaction between them is done using a message.

    Interaction Of Smart Contracts

    When an incoming message that did not cause processing errors is received, a transaction is created. This generates outgoing messages to other contracts. Accordingly, it is possible to create large projects with a large number of contracts and complex interactions between them.

    Message

    Consists of a header and a payload.

    There are three types of header, according to which the message will be processed:

    internal message
    external in message — external incoming message;
    external outmessage — external outgoing message.

    All of them contain the source address — src and the destination address — dest, have the logical creation time created_lt and unixtime created_at.

    An internal message also contains some value in TON Crystal or other currencies, a bounce parameter, and flags that set the order of payment for actions in the blockchain.

    Payload is arbitrary but must match the smart contract to which the message is sent. Otherwise, the smart contract cannot process it and will generate an error.

    Compiling a smart contract creates two files: .abi and .tvc. All functions that are implemented in a smart contract are described in the .abi file.

    Transaction

    This results from executing a contract. It is usually generated by a single incoming message (external or internal) and may generate multiple outgoing messages (external or internal) as a result. The transaction can be successful or aborted.

    On The Free TON Blockchain, Sending And Processing Messages Are Paid For

    We also pay for data storage within the blockchain. To control the payment of internal messages, you need flags when sending them. Basically, it is a bitmask.

    Possible values:

    0 — carries funds equal to parameter value to destination. Forward fee is subtracted from parameter value.
    128 — carries all the remaining balance of the current smart contract. Parameter value is ignored. The contract’s balance will be equal to zero.
    64 — carries funds equal to parameter value and all the remaining value of the inbound message.

    Parameter flag can also be changed:

    flag + 1 — means that the sender wants to pay transfer fees separately from contract’s balance.
    flag + 2 — means that any errors arising while processing this message during the action phase should be ignored.
    flag + 32 — means that the current account must be destroyed if its resulting balance is zero.

    For example, flag: 128 + 32 used to send all balance and destroy the contract.

    Let’s Consider A Simple Example

    Let’s take two contracts that implement the same interface.

    This example intentionally implements the interface in two variants to make it obvious how smart contract method calls work. It is the interface, i.e. the name of the function, its parameters, and return values that are important for a function call, not the implementation.

    First ClassA contract can increase the accumulator _accum, according to the type of the variable, but cannot add more than the _limit value to itself at one time.

    Second ClassB contract, in addition, has a _capacity limit of the battery. For simplicity, the _limit and _capacity values are set in the constructor.

    Send() method can be called any way as well as the accept() method, since they are both public, but the implementation is such that when the send() method is called, an internal message is sent with parameters, including flags that handle how the call is paid for.

    Diagram classes
    Sequence diagram

    IClass — common interface

    ClassA — first implementation

    ClassB — second implementation

    Errors error constants

    Good Luck With The Development

    This example can help anyone new to smart contract development in the Free TON blockchain. We wrote a detailed walkthrough to understand the basics. We hope that with the help of this material, beginners will take their first steps in developing Free TON projects faster and join the community.

    More information can be found here.

    6
    0