Sending messages
TON blockchain is message-based — to communicate with other contracts and to deploy new ones you need to send messages.
Messages in Tact are commonly composed using a built-in Struct SendParameters
, which consists of:
Field | Type | Description |
---|---|---|
bounce | Bool | When set to true (default) message bounces back to the sender if the receiver contract doesn’t exist or wasn’t able to process the message. |
to | Address | Receiver internal Address in TON blockchain. |
value | Int | The amount of nanoToncoins you want to send with the message. This value is usually used to cover forward fees, unless the optional flag SendPayGasSeparately is used. |
mode | Int | An 8-bit value that configures how to send a message, defaults to . See: Message mode . |
body | Cell? | Optional message body as a Cell |
code | Cell? | Optional initial code of the contract (the compiled bytecode) |
data | Cell? | Optional initial data of the contract (arguments of init() function of the contract) |
Fields code
and data
are what’s called an init package, which is used in deployments of new contracts.
Send simple reply
The simplest message is a reply to the incoming message returning all excess value of a message:
Send message
If you need more advanced logic you can use the send()
function and SendParameters
Struct directly.
In fact, the previous example with .reply()
can be made using the following call to send()
function:
Another example sends a message to the specified Address
with a value
of TON and the body
of a comment with a String
"Hello, World!"
:
The optional flag SendIgnoreErrors
means that even when an error occurs during message sending next messages would be sent anyway. No error during the sending phase would revert a transaction.
Send typed message
To send a binary typed message you can use the following code:
Deploy contract
To deploy a contract you need to calculate its address and initial state with initOf
, then send them in the initialization message:
Outbound message processing
Each transaction on TON Blockchain consists of multiple phases. Outbound messages are evaluated in compute phase, but are not sent in that phase. Instead, they’re queued in order of appearance for the action phase, where all actions listed in compute phase, like outbound messages or reserve requests, are executed.
As all the values are computed in compute phase, all the fees computed by the end of it, and exceptions do not revert the transaction during action phase, outbound message sends can fail without bounce due to unsufficient action fees or forward fees.
Consider the following example:
There, the second message won’t actually be sent:
-
After finishing the compute phase, the remaining value of the contract is computed.
-
During the outbound message processing and assuming that there was enough value provided in the inbound message, the first message leaves nanoToncoins on the balance.
-
When the second message is processed, contract tries to send nanoToncoins, but fails to do so because there is already a smaller amount left.