Search code examples
blockchainsoliditytlbeverscale

What is the TL-B scheme in ton-solidity and how to use it?


This question is about ton-solidity language of Everscale blockchain smart contracts.

Sometimes, I see the code relative to TL-B scheme. For example, like this:

function getInvokeMessage(address sender, address recipient, uint128 amount, bool bounce, TvmCell payload) public pure
        returns(TvmCell message) {
        TvmCell body = tvm.encodeBody(MsigDebot.invokeTransaction, sender, recipient, amount, bounce, payload);
        TvmBuilder message_;
        message_.store(false, true, true, false, address(0), address(this));
        message_.storeTons(0);
        message_.storeUnsigned(0, 1);
        message_.storeTons(0);
        message_.storeTons(0);
        message_.store(uint64(0));
        message_.store(uint32(0));
        message_.storeUnsigned(0, 1); //init: nothing$0
        message_.storeUnsigned(1, 1); //body: right$1
        message_.store(body);
        message = message_.toCell();
    }

I need explanations step by step, what does this code doing? Or give me the refers to documentation that explain it.


Solution

  • The messages construct accordingly to TL-B scheme.

    In general, the message has type that called "Message X"

    This type has 3 parts.

    The first part is the "CommonMsgInfo"

               /* int_msg_info$0
               ihr_disabled:Bool
               bounce:Bool
               bounced:Bool
               src:MsgAddressInt
               dest:MsgAddressInt */
            message_.store(false, true, true, false, address(0), address(this));
            message_.storeTons(0);//value:CurrencyCollection
            message_.storeUnsigned(0, 1);//value:CurrencyCollection (for other currencies)
            // In the next 4 fields we store zeroes, because blockchain software will replace them
            // with the correct values after this function finishes execution.         
            message_.storeTons(0);//ihr_fee:Grams
            message_.storeTons(0);//fwd_fee:Grams
            message_.store(uint64(0));//created_lt:uint64
            message_.store(uint32(0));//created_at:uint32
    

    The second part has type "init"

            message_.storeUnsigned(0, 1); //no initial state
    

    The third part has type "body"

            message_.storeUnsigned(1, 1); //1 because, we don't store body in current cell
            message_.store(body);