Message-driven execution
This section covers how messages trigger transactions in TON. You'll learn how accounts communicate through messages, how message types differ, and how to construct and send messages to initiate on-chain execution.
Messages
In the TON blockchain, a message is the fundamental unit of interaction between accounts (smart contracts). All actions, state changes, and logic execution within accounts are triggered by messages.
Transaction and message real-life examples
Let’s explore how transactions and messages work in TON through an analogy. Imagine the TON blockchain as a unique city in the global world — the Internet. In this city, the rules are strict: residents never meet in person, and the only way to communicate is via a postal service. People receive messages asking them to perform a task, complete it privately in their homes, and then send the results back to others.
Not every message makes it to the mailbox — if it fails to arrive due to insufficient postage, it’s discarded. But when a message does arrive, the resident picks up exactly one incoming message, locks themselves inside, and doesn’t come out until the task is completed. While they’re working, new letters may arrive — but they ignore them completely.
If they realize mid-task that some necessary information is missing, they cannot pause to ask for clarification. In that case, they must declare the task as failed and, at best, return leftover resources along with scraps of the original instructions.
If the task is successful, the person, as specified in the original message:
- send new messages to other residents, or
- store the resulting items safely in their home.
After finishing, they return to their mailbox and retrieve the next incoming message.
Now imagine that for each work session, the person keeps a detailed journal. To keep their work organized and separate, each journal entry is called a transaction. Each transaction contains:
- the full content of the incoming message (the task),
- a note on what was produced and where it was stored,
- and, optionally, information about any messages sent to others.
Messages delivered via the postal system between residents are called internal messages. Messages from the outside world also reach the city. These have a specified recipient, but their sender is unknown — they are known as external incoming or external-in messages.
While external-in and internal messages fulfil a similar role from the actor’s perspective, each actor may handle them differently. Some may process them normally, while others might completely ignore messages from the outside world — like someone who refuses to open letters from strangers.
Message structure: TL-B
Now, let’s look at the data contained in the messages. TON distinguishes between external and internal messages, both of which are capable of carrying data.
In TON, all data is represented using cells. To serialize data into a cell, TON relies on a well-established standard called TL-B (Type Language – Binary).
Native message structure
In TON, all messages adhere to a unified schema that defines their structure and serialization.
message$_ {X:Type} info:CommonMsgInfo
init:(Maybe (Either StateInit ^StateInit))
body:(Either X ^X) = Message X;
info: CommonMsgInfo
— contains metadata about the message.init: (Maybe (Either StateInit ^StateInit))
— optional field used to initialize a new account or update the existing one.body: (Either X ^X)
— the main payload of the message; can be embedded directly or stored as a reference.
Message types
There are three types of messages in TON:
- External incoming: sent from outside the blockchain → received by a smart contract
- Internal: sent from a smart contract → received by a smart contract
- External outgoing: sent from a smart contract → received outside the blockchain (unknown actor)