|
| 1 | +# Message Modes Cookbook |
| 2 | + |
| 3 | +Understanding the different modes and flags available for sending messages is important to ensure that your smart contracts behave correctly. |
| 4 | +While [Message-modes](/develop/smart-contracts/messages#message-modes) section provided detailed descriptions of these modes and flags, in this section we will illustrate their practical application with concrete examples. |
| 5 | + |
| 6 | +:::info IMPORTANT |
| 7 | +The result of the error cases is described when the error occurred. |
| 8 | +::: |
| 9 | + |
| 10 | +## 1. Send a regular message |
| 11 | + |
| 12 | +We currently have 100 Toncoin in the balance of our smart contract. After receiving an internal message with 50 Toncoin, we send a regular message with 20 Toncoin, the transaction fee is 3 Toncoin and will be deducted from the message. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +| Mode and Flags | Code | |
| 17 | +|:-|:-| |
| 18 | +| `mode` = 0, no `flag` | `send_raw_message(msg, 0)` | |
| 19 | + |
| 20 | +## 2. Send a regular message, no bounce the message on error and ignore it |
| 21 | + |
| 22 | +We currently have 100 Toncoin in the balance of our smart contract. After receiving an internal message with 50 Toncoin, we send a regular message with 20 Toncoin, the transaction fee is 3 Toncoin and will be deducted from the message. |
| 23 | +In case of an error during transaction processing, the message will not bounce and will be ignored. |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +| Mode and Flags | Code | |
| 28 | +|:-|:-| |
| 29 | +| `mode` = 0, `flag` = 2 | `send_raw_message(msg, 2)` | |
| 30 | + |
| 31 | +## 3. Send a regular message and bounce the message on error |
| 32 | + |
| 33 | +We currently have 100 Toncoin in the balance of our smart contract. After receiving an internal message with 50 Toncoin, we send a regular message with 20 Toncoin, the transaction fee is 3 Toncoin and will be deducted from the message, |
| 34 | +if an error occurs during action processing - bounce the message in addition to rolling back the transaction. |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +| Mode and Flags | Code | |
| 39 | +|:-|:-| |
| 40 | +| `mode` = 0, `flag` = 16 | `send_raw_message(msg, 16)` | |
| 41 | + |
| 42 | +## 4. Send a regular message with separate fees |
| 43 | + |
| 44 | +We have 100 Toncoin on our smart contract balance, we receive an internal message with 50 Toncoin and send a regular message with 20 Toncoin, the total fee is 3 Toncoin and we pay the transfer fee separately (from the contract balance). |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +| Mode and Flags | Code | |
| 49 | +|:-|:-| |
| 50 | +| `mode` = 0, `flag` = 1 | `send_raw_message(msg, 1)` | |
| 51 | + |
| 52 | +## 5. Send a regular message with separate fees and bounce the message on error |
| 53 | + |
| 54 | +We have 100 Toncoin on our smart contract balance and we receive an internal message with 50 Toncoin and send a regular message with 20 Toncoin, the total fee is 3 Toncoin and we pay the transfer fee separately (from the contract balance), |
| 55 | +if an error occurs during action processing - bounce the message in addition to rolling back the transaction. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +| Mode and Flags | Code | |
| 60 | +|:-|:-| |
| 61 | +| `mode` = 0, `flag` = 1 + 16 = 17 | `send_raw_message(msg, 17)` | |
| 62 | + |
| 63 | +## 6. Carry remaining value with new message |
| 64 | + |
| 65 | +We currently have 100 Toncoin in the balance of our smart contract. After receiving an internal message with 50 Toncoin we carry all the remaining value of the inbound message in addition to the value initially indicated in the new message, |
| 66 | +the transaction fee is 3 Toncoin and will be deducted from the message. |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +| Mode and Flags | Code | |
| 71 | +|:-|:-| |
| 72 | +| `mode` = 64, no `flag` | `send_raw_message(msg, 64)` | |
| 73 | + |
| 74 | +## 7. Carry remaining value with new message with separate fees |
| 75 | + |
| 76 | +We currently have 100 Toncoin in the balance of our smart contract. After receiving an internal message with 50 Toncoin we carry all the remaining value of the inbound message in addition to the value initially indicated in the new message, |
| 77 | +the transaction fee is 3 Toncoin and will be paid separately (from the smart contract balance). |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +| Mode and Flags | Code | |
| 82 | +|:-|:-| |
| 83 | +| `mode` = 64, `flag` = 1 | `send_raw_message(msg, 65)` | |
| 84 | + |
| 85 | +## 8. Carry remaining value and bounce the message on error |
| 86 | + |
| 87 | +We currently have 100 Toncoin in the balance of our smart contract. After receiving an internal message with 50 Toncoin we carry all the remaining value of the inbound message in addition to the value initially indicated in the new message, |
| 88 | +the transaction fee is 3 Toncoin and will be deducted from the message, if an error occurs during action processing - bounce the message in addition to rolling back the transaction. |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +| Mode and Flags | Code | |
| 93 | +|:-|:-| |
| 94 | +| `mode` = 64, `flag` = 16 | `send_raw_message(msg, 80)` | |
| 95 | + |
| 96 | +## 9. Carry remaining value with new message with separate fees and bounce the message on error |
| 97 | + |
| 98 | +We currently have 100 Toncoin in the balance of our smart contract. After receiving an internal message with 50 Toncoin we send a message, we transfer the entire contract balance in addition to the original amount received, the transaction fee is 3 Toncoin and will be paid separately (from the smart contract balance), |
| 99 | +if an error occurs during action processing - bounce the message in addition to rolling back the transaction. |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +| Mode and Flags | Code | |
| 104 | +|:-|:-| |
| 105 | +| `mode` = 64, `flag` = 16 + 1 | `send_raw_message(msg, 81)` | |
| 106 | + |
| 107 | +## 10. Send all received tokens together with the contract balance |
| 108 | + |
| 109 | +We currently have 100 Toncoin in the balance of our smart contract. After receiving an internal message with 50 Toncoin we send a message, we transfer the entire contract balance in addition to the original amount received, |
| 110 | +the transaction fee is 3 Toncoin and will be deducted from the message. |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +| Mode and Flags | Code | |
| 115 | +|:-|:-| |
| 116 | +| `mode` = 128, no `flag` | `send_raw_message(msg, 128)` | |
| 117 | + |
| 118 | +## 11. Send all received tokens together with the contract balance and bounce the message on error |
| 119 | + |
| 120 | +We currently have 100 Toncoin in the balance of our smart contract. After receiving an internal message with 50 Toncoin we send a message, we transfer the entire contract balance in addition to the original amount received, the transaction fee is 3 Toncoin and will be deducted from the message, |
| 121 | +if there was an error in processing the action - bounce the message in addition to rolling back the transaction. |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +| Mode and Flags | Code | |
| 126 | +|:-|:-| |
| 127 | +| `mode` = 128, `flag` = 16 | `send_raw_message(msg, 144)` | |
| 128 | + |
| 129 | +## 12. Send all received tokens together with the contract balance and destroy smart-contract |
| 130 | + |
| 131 | +We currently have 100 Toncoin in the balance of our smart contract. After receiving an internal message with 50 Toncoin, we send a message to transfer the entire contract balance in addition to the original amount received and destroy the contract, |
| 132 | +the transaction fee is 3 Toncoin and will be deducted from the message. |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +| Mode and Flags | Code | |
| 137 | +|:-|:-| |
| 138 | +| `mode` = 128, `flag` = 32 | `send_raw_message(msg, 160)` | |
0 commit comments