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
import "@stdlib/deploy";contract Example with Deployable { receive() { self.reply("Hello, World!".asComment()); // asComment converts a String to a Cell with a comment }}
How to send a simple message
import "@stdlib/deploy";contract Example with Deployable { 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) }); }}
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.
import "@stdlib/deploy";contract Example with Deployable { 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) }); }}
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.
import "@stdlib/deploy";contract Example with Deployable { 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) }); }}
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
import "@stdlib/deploy";contract Example with Deployable { 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) }); }}
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 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.
import "@stdlib/deploy";contract Example with Deployable { 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(), }); }}