Context and state
Contextual and state-related functions and structs.
Time
now
fun now(): Int;
Returns the current Unix time.
Usage example:
let timeOffset: Int = now() + 1000; // thousand seconds from now()
curLt
Available since Tact 1.6fun curLt(): Int;
Returns the Int
value of the logical time of the current transaction.
Usage example:
let lt: Int = curLt();nativeRandomize(lt); // Equivalent to calling nativeRandomizeLt()
blockLt
Available since Tact 1.6fun blockLt(): Int;
Returns the Int
value of the starting logical time of the current block.
Usage example:
let time: Int = blockLt();
Incoming message
sender
fun sender(): Address;
Returns the Address
of the sender of the current message.
Usage example:
contract MeSee { receive() { let whoSentMeMessage: Address = sender(); }}
context
fun context(): Context;
Returns Context
struct, which consists of:
Field | Type | Description |
---|---|---|
bounceable | Bool | Indicates whether the received message can bounce back. |
sender | Address | Internal address of the sender on the TON Blockchain. |
value | Int | Amount of nanoToncoin in the received message. |
raw | Slice | The remainder of the received message as a Slice . It follows the internal message layout of TON, starting from the destination Address (MsgAddressInt in TL-B notation). |
Usage example:
let ctx: Context = context();require(ctx.value != 68 + 1, "Invalid amount of nanoToncoins, bye!");
Context.readForwardFee
extends fun readForwardFee(self: Context): Int;
Extension function for the Context
struct.
Reads the forward fee and returns it as an Int
amount of nanoToncoin.
Usage example:
let fwdFee: Int = context().readForwardFee();
Contract and transaction state
myAddress
fun myAddress(): Address;
Returns the address of the current smart contract as an Address
.
Usage example:
let meMyselfAndI: Address = myAddress();
myCode
Available since Tact 1.6fun myCode(): Cell;
Returns the smart contract code Cell
obtained from the c7
register.
Usage example:
let code: Cell = myCode();
myStorageDue
Available since Tact 1.5fun myStorageDue(): Int;
Returns the nanoToncoin Int
amount of the accumulated storage fee debt. Storage fees are deducted from the incoming message value before the new contract balance is calculated.
Usage example:
let debt: Int = myStorageDue();
myBalance
fun myBalance(): Int;
Returns the nanoToncoin Int
balance of the smart contract as it was at the start of the compute phase of the current transaction.
Usage example:
let iNeedADolla: Int = myBalance();
gasConsumed
Available since Tact 1.5fun gasConsumed(): Int;
Returns the nanoToncoin Int
amount of gas consumed by TVM in the current transaction so far. The resulting value includes the cost of calling this function.
Usage example:
let gas: Int = gasConsumed();
nativeReserve
Gas-expensivefun nativeReserve(amount: Int, mode: Int);
Executes the native RAWRESERVE
instruction with the specified amount and mode. The RAWRESERVE
instruction creates an output action to reserve a specific amount of nanoToncoin from the remaining balance of the account.
The RAWRESERVE
instruction takes two arguments:
amount
: The number of nanoToncoin to reserve.mode
: Determines the reservation behavior.
The RAWRESERVE
instruction is roughly equivalent to creating an outbound message carrying the specified amount
of nanoToncoin (or b - amount
nanoToncoin, where b
is the remaining balance) to oneself. This ensures that subsequent output actions cannot spend more money than the remainder.
It is possible to use raw Int
values and manually provide them for the mode
, but for your convenience, there’s a set of constants you may use to construct the compound mode
with ease. Take a look at the following tables for more information on base modes and optional flags.
Base modes
The resulting mode
value can have the following base modes:
Mode value | Constant name | Description |
---|---|---|
ReserveExact | Reserves exactly the specified amount of nanoToncoin. | |
ReserveAllExcept | Reserves all but the specified amount of nanoToncoin. | |
ReserveAtMost | Reserves at most the specified amount of nanoToncoin. |
Optional flags
Additionally, the resulting mode
can have the following optional flags added:
Flag value | Constant name | Description |
---|---|---|
ReserveAddOriginalBalance | Increases the amount by the original balance of the current account (before the compute phase), including all extra currencies. | |
ReserveInvertSign | Negates the amount value before performing the reservation. | |
ReserveBounceIfActionFail | Bounces the transaction if the reservation fails. |
Combining modes with flags
To construct the Int
value for the mode
parameter, combine base modes with optional flags by applying the bitwise OR operation:
nativeReserve(ton("0.1"), ReserveExact | ReserveBounceIfActionFail);// ---------- ----------------------------------------// ↑ ↑// | mode, which would bounce the transaction if exact reservation fails// amount of nanoToncoins to reserve
commit
fun commit();
Commits the current state of registers c4
(persistent data) and c5
(actions), so that the current execution is considered “successful” with the saved values even if an exception in the compute phase is thrown later.
Usage example:
commit(); // now, transaction is considered "successful"throw(42); // and this won't fail it
Blockchain state
getConfigParam
fun getConfigParam(id: Int): Cell?;
Loads a configuration parameter of TON Blockchain by its id
number.
Usage examples:
// Parameter 0, address of a special smart contract that stores the blockchain's configurationlet configAddrAsCell: Cell = getConfigParam(0)!!;
// Parameter 18, configuration for determining the prices for data storagelet dataStorageFeeConfig: Cell = getConfigParam(18)!!;