RAWRESERVE instruction that takes from the stack an amount of Toncoin (in nanotoncoins) to reserve and an integer mode specifying a way of the reservation. The reservation action is queued to the output action list of a smart contract, which contains other actions such as message sends.
The RAWRESERVE instruction is equivalent to creating an outbound message carrying the specified amount of nanotoncoin to oneself. But unlike regular sending of a message, the reservation action does not create a new message and does not incur any forward fees. Its primary goal is to limit the amount of Toncoin that can be spent by subsequent actions.
Modes
Themode parameter is a bitmask that specifies how a reserved amount is calculated. The resulting mode value can have the following base modes:
| Mode value | Convenient name | Description |
|---|---|---|
0 | ReserveExact | Reserves exactly the specified amount of nanotoncoin. |
1 | ReserveAllExcept | Reserves all but the specified amount of nanotoncoin. |
2 | ReserveAtMost | Reserves at most the specified amount of nanotoncoin. |
mode can have the following optional flags added:
| Flag value | Convenient name | Description |
|---|---|---|
+4 | ReserveAddOriginalBalance | Increases the amount by the original balance (i.e. without incoming message value) of the current account before the compute phase. |
+8 | ReserveInvertSign | Negates the amount value before performing the reservation. |
+16 | ReserveBounceIfActionFail | Bounces the transaction if the reservation fails. |
Behavior
Notation:amount– the amount of Toncoin passed to theRAWRESERVEinstruction.mode– the integer mode passed to theRAWRESERVEinstruction.original_balance:- if after the storage phase, account’s balance is less than the value of incoming message with bounce flag set to
false, thenoriginal_balanceis set to ; - otherwise,
original_balanceequals account’s balance before the compute phase minus incoming message value.
- if after the storage phase, account’s balance is less than the value of incoming message with bounce flag set to
remaining_balance– account’s balance before the reservation action.reserve– the final amount to be reserved.
- Check that
modehas flagReserveBounceIfActionFail:- if so, then in case of any failure the action phase will be interrupted and the bounce phase will be initiated;
- if not, then in case of any failure the reservation action will be skipped.
- Set
reservetoamount. - Check that
modehas flagReserveAddOriginalBalance:- If so, then check that
modehas flagReserveInvertSign:- if so, then set
reservetooriginal_balance - reserve; - otherwise, increase
reservebyoriginal_balance.
- if so, then set
- Otherwise, if
modehas flagReserveInvertSign, throw the error “invalid reserve mode”.
- If so, then check that
- Check that
modehas flagReserveAtMost:- if so, then set
reservetomin(reserve, remaining_balance).
- if so, then set
- Check that
modehas flagReserveAllExcept:- if so, then set
reservetoremaining_balance - reserve.
- if so, then set
- Set
remaining_balancetoremaining_balance - reserve.
remaining_balance.
For example, suppose that:
amount=0.1Ton;mode=1 + 4 + 8(ReserveAllExcept,ReserveAddOriginalBalance,ReserveInvertSign);original_balance=2Ton;remaining_balance=3Ton.
modehas flagReserveBounceIfActionFail? No.reserve=0.1Ton.modehas flagReserveAddOriginalBalance? Yes.modehas flagReserveInvertSign? Yes.- Thus,
reserve=original_balance - reserve=2 - 0.1=1.9Ton.
modehas flagReserveAtMost? No.modehas flagReserveAllExcept? Yes.- Thus,
reserve=remaining_balance - reserve=3 - 1.9=1.1Ton.
- Thus,
remaining_balance=remaining_balance - reserve=3 - 1.1=1.9Ton.
Errors
The following errors can occur during the reservation flow:- If the
modebitmask has more then first five ones positive bits, then the error34is thrown. - If
modehas flag8but not flag4, then the the error34is thrown. - If after step 3,
reserveis negative, then the the error34is thrown. - Passing a negative
amountalso results in the same error. - If after step 4,
reserveis greater thanremaining_balance, then the error37(“Not enough Toncoin”) is thrown. - Some problems with unpacking the reserve action cell.
- A problem related to extra-currency.
16, then in case of any of the above errors a bounce message is sent back to a sender.