Skip to content

Single-contract communication

This page lists examples of communication of a single deployed contract with other contracts on blockchain.

For examples of communication between multiple deployed contracts see: Multi-contract communication.

How to make a basic reply

contract Example {
receive() {
self.reply("Hello, World!".asComment()); // asComment converts a String to a Cell with a comment
}
}
▶️ Open in Web IDE

How to send a simple message

contract Example {
receive() {
send(SendParameters{
bounce: true, // default
to: sender(), // or another destination address
value: ton("0.01"), // attached amount of Tons to send
body: "Hello from Tact!".asComment(), // comment (optional)
});
}
}
▶️ Open in Web IDE

How to send a message with the entire balance

If we need to send the whole balance of the smart contract, then we should use the SendRemainingBalance send mode. Alternatively, we can use mode: 128, which has the same meaning.

contract Example {
receive() {
send(SendParameters{
// bounce = true by default
to: sender(), // send the message back to the original sender
value: 0,
mode: SendRemainingBalance, // or mode: 128
body: "Hello from Tact!".asComment(), // comment (optional)
});
}
}
▶️ Open in Web IDE

How to send a message with the remaining value

If we want to make a reply to the same sender, we can use the mode SendRemainingValue (i.e. mode: 64), which carries all the remaining value of the inbound message in addition to the value initially indicated in the new message.

contract Example {
receive() {
send(SendParameters{
// bounce = true by default
to: sender(), // send the message back to the original sender
value: 0,
mode: SendRemainingValue,
body: "Hello from Tact!".asComment(), // comment (optional)
});
}
}
▶️ Open in Web IDE

It’s often useful to add the SendIgnoreErrors flag too, in order to ignore any errors arising while processing this message during the action phaseL

contract Example {
receive() {
send(SendParameters{
// bounce = true by default
to: sender(), // send the message back to the original sender
value: 0,
mode: SendRemainingValue | SendIgnoreErrors, // prefer using | over + for the mode
body: "Hello from Tact!".asComment(), // comment (optional)
});
}
}
▶️ Open in Web IDE

The latter example is identical to using a .reply() function.

How to send a message with a long text comment

If we need to send a message with a lengthy text comment, we should create a String that consists of more than 127127 characters. To do this, we can utilize the StringBuilder primitive type and its methods called beginComment() and append(). Prior to sending, we should convert this string into a cell using the toCell() method.

contract Example {
receive() {
let comment: StringBuilder = beginComment();
let longString = "..."; // Some string with more than 127 characters.
comment.append(longString);
send(SendParameters{
// bounce = true by default
to: sender(),
value: 0,
mode: SendIgnoreErrors,
body: comment.toCell(),
});
}
}
▶️ Open in Web IDE