Sending messages
TON blockchain is a message-based one to communicate with other contracts you need to send messages.
The message consists of:
value
in TON - the amount of TON you want to send with the message. This value is used to cover gas fees on the receiver side.bounce
- if set totrue
(default) then the message will be bounced back to the sender if the receiver contract doesn't exist or wasn't able to process the message.code
anddata
- init package, useful for deploymentsbody
- message body asCell
mode
- an 8-bit flag that configures how to send a message
Send simple reply
The simplest message is a reply to the incoming message returning all excess value of a message:
self.reply("Hello, World!".asComment()); // asComment converts string to a Cell with a comment
Send message
If you need more advanced logic you can use the send
function directly.
This example sends a message to the to
address with a value
of 1 TON and the body
of a comment with a string "Hello, World!".
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.
let to: Address = ...;
let value: Int = ton("1");
send(SendParameters{
to: to,
value: value,
mode: SendIgnoreErrors,
bounce: true,
body: "Hello, World!".asComment()
});
Send typed message
To send a binary typed message you can use the following code:
let to: Address = ...;
let value: Int = ton("1");
send(SendParameters{
to: to,
value: value,
mode: SendIgnoreErrors,
bounce: true,
body: SomeMessage{arg1: 123, arg2: 1234}.toCell()
});
Deploy contract
To deploy a contract you need to calculate its address and init package, then send it with an initial message. You can always send an init package with a message and it would be ignored, but would cost more than the message without an init package.
let init: StateInit = initOf SecondContract(arg1, arg2);
let address: Address = contractAddress(init);
let value: Int = ton("1");
send(SendParameters{
to: address,
value: value,
mode: SendIgnoreErrors,
bounce: true,
code: init.code,
data: init.data,
body: "Hello, World!".asComment()
});