diff --git a/pages/book/message-mode.mdx b/pages/book/message-mode.mdx index 6827a768..c41e3ec7 100644 --- a/pages/book/message-mode.mdx +++ b/pages/book/message-mode.mdx @@ -54,6 +54,8 @@ send(SendParameters{ Also note, that there can be only one [base mode](#base-modes), but number of [optional flags](#optional-flags) may vary: you can use them all, none or just some. + See more examples: [Single-contract communication in the Cookbook](/cookbook/single-communication). + [int]: /book/integers diff --git a/pages/book/send.mdx b/pages/book/send.mdx index 92b3f21d..b3d0c04f 100644 --- a/pages/book/send.mdx +++ b/pages/book/send.mdx @@ -1,5 +1,7 @@ # Sending messages +import { Callout } from 'nextra/components' + TON blockchain is message-based — to communicate with other contracts and to deploy new ones you need to send messages. Messages in Tact are composed using a built-in [Struct](/book/structs-and-messages#structs) `SendParameters{:tact}`, which consists of: @@ -95,6 +97,12 @@ send(SendParameters{ }); ``` + + + See more examples of sending messages: [Single-contract communication in the Cookbook](/cookbook/single-communication). + + + [p]: /book/types#primitive-types [int]: /book/integers [opt]: /book/optionals diff --git a/pages/cookbook/single-communication.mdx b/pages/cookbook/single-communication.mdx index 6755b1c9..51a86878 100644 --- a/pages/cookbook/single-communication.mdx +++ b/pages/cookbook/single-communication.mdx @@ -10,21 +10,158 @@ For examples of communication between multiple deployed contracts see: [Multi-co ```tact receive() { - self.reply("Hello, World!".asComment()); // asComment converts a String to a Cell with a comment + self.reply("Hello, World!".asComment()); } ``` -## How to send a simple message + + + **Useful links:**\ + [`self.reply(){:tact}` in Core library](/ref/core-base#self-reply)\ + [`String.asComment(){:tact}` in Core library](/ref/core-strings#stringascomment) + + + +## How to send a regular message + +### With a default mode and no optional flags [#regular-default] ```tact send(SendParameters{ bounce: true, // default - to: destinationAddress, - value: ton("0.01"), // attached amount of Tons to send - body: "Hello from Tact!".asComment(), // comment (optional) + to: someDestinationAddress, + value: ton("0.1"), // attached amount of Tons to send + body: "Hello from Tact!".asComment(), // comment as a body (optional) +}); +``` + +### With `SendPayGasSeparately` [#regular-paygasseparately] + +Paying [forward fees](https://docs.ton.org/develop/smart-contracts/guidelines/processing) separately from the message value. + +```tact +send(SendParameters{ + // bounce: true by default + to: groundControl, + value: ton("0.1"), // attached amount of Tons to send + mode: SendPayGasSeparately, +}); +``` + +### With `SendIgnoreErrors` [#regular-sendignoreerrors] + +Ignoring any errors arising while processing this message during the [action phase](https://docs.ton.org/learn/tvm-instructions/tvm-overview#transactions-and-phases). + +```tact +send(SendParameters{ + // bounce: true by default + to: majorTom, + value: ton("0.1"), // attached amount of Tons to send + mode: SendIgnoreErrors, +}); +``` + +### With `SendBounceIfActionFail` [#regular-sendbounceifactionfail] + +Bouncing the transaction in case of any errors during action phase. Has no effect if flag [`SendIgnoreErrors{:tact}`](#regular-sendignoreerrors) is used. + +```tact +send(SendParameters{ + // bounce: true by default + to: deepThought, + value: ton("0.1"), // attached amount of Tons to send + mode: SendBounceIfActionFail, +}); +``` + +### With `SendDestroyIfZero` [#regular-senddestroyifzero] + +Destroing current contract if its resulting balance is zero (often used with mode [`SendRemainingBalance{:tact}`](#remainingbalance-default)). + +```tact +send(SendParameters{ + // bounce: true by default + to: zaphodBeeblebrox, + value: ton("0.1"), // attached amount of Tons to send + mode: SendDestroyIfZero, +}); +``` + +## How to send a message carrying all the remaining value + +### Without optional flags, `SendRemainingValue` [#remainingvalue-default] + +Carrying all the remaining value of the inbound message (the one we've just received) in addition to the value initially indicated in the message (the one we're about to send). + +```tact +send(SendParameters{ + // bounce: true by default + to: jamesBond, + value: ton("0.1"), // attached amount of Tons to send + mode: SendRemainingValue, +}); +``` + +### With `SendPayGasSeparately` [#remainingvalue-paygasseparately] + +```tact +send(SendParameters{ + // bounce: true by default + to: groundControl, + value: ton("0.1"), // attached amount of Tons to send + mode: SendRemainingValue | SendPayGasSeparately, +}); +``` + +### With `SendIgnoreErrors` [#remainingvalue-sendignoreerrors] + +```tact +send(SendParameters{ + // bounce: true by default + to: majorTom, + value: ton("0.1"), // attached amount of Tons to send + mode: SendRemainingValue | SendIgnoreErrors, +}); +``` + +### With `SendBounceIfActionFail` [#remainingvalue-sendbounceifactionfail] + +```tact +send(SendParameters{ + // bounce: true by default + to: deepThought, + value: ton("0.1"), // attached amount of Tons to send + mode: SendRemainingValue | SendBounceIfActionFail, }); ``` +### With `SendDestroyIfZero` [#remainingvalue-senddestroyifzero] + +```tact +send(SendParameters{ + // bounce: true by default + to: zaphodBeeblebrox, + value: ton("0.1"), // attached amount of Tons to send + mode: SendRemainingValue | SendDestroyIfZero, +}); +``` + +## How to send a message carrying all the remaining balance + +### Without optional flags, `SendRemainingBalance` [#remainingbalance-default] + +TODO + +and x4 from prev thing + +"X" +"Like X, but also" +"Like X, but also" +"Like X, but also" +"Like X, but also" + +TODO: re-do other things below + ## 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{:tact}` send mode. Alternatively, we can use `mode: 128{:tact}`, which has the same meaning. @@ -89,7 +226,7 @@ send(SendParameters{ **Useful links:**\ ["Sending messages" in the Book](/book/send#send-message)\ - ["Message `mode`" in the Book](/book/message-mode) + ["Message `mode`" in the Book](/book/message-mode)\ [`StringBuilder{:tact}` in the Book](/book/types#primitive-types)\ [`Cell{:tact}` in Core library](/ref/core-cells)