Cookbook
Single-contract communication

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

receive() {
    self.reply("Hello, World!".asComment()); // asComment converts a String to a Cell with a comment
}

How to send a simple message

send(SendParameters{
    bounce: true, // default
    to: destinationAddress,
    value: ton("0.01"), // attached amount of Tons to send
    body: "Hello from Tact!".asComment(), // comment (optional)
});

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.

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)
});

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.

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)
});

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

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)
});

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.

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(),
});
🤔

Didn't find your favorite example of a single-contract communication? Have cool implementations in mind? Contributions are welcome! (opens in a new tab)