Cells, Builders and Slices
Cell
is a low-level primitive that represents data in TON Blockchain. Cells consist of bits of data with up to references to another cells. They are read-only and immutable, and cannot have cyclic references.
Builder
is an immutable primitive to construct cells, and Slice
is a mutable primitive to parse them.
beginCell
Creates a new empty Builder
.
Usage example:
emptyCell
Creates and returns an empty Cell
(without data and references). Alias to beginCell().endCell()
.
Usage example:
emptySlice
Creates and returns an empty Slice
(without data and references). Alias to emptyCell().asSlice()
.
Usage example:
Cell.beginParse
Extension function for the Cell
.
Opens the Cell
for parsing and returns it as a Slice
.
Usage example:
Cell.hash
Extension function for the Cell
.
Calculates and returns an Int
value of the SHA-256 hash of the standard Cell
representation of the given Cell
.
Usage example:
Cell.asSlice
Extension function for the Cell
.
Converts the Cell to a Slice
and returns it. Alias to self.beginParse()
.
Usage example:
Builder.endCell
Extension function for the Builder
.
Converts a Builder
into an ordinary Cell
.
Usage example:
Builder.storeUint
Extension function for the Builder
.
Stores an unsigned bits
-bit value
into the copy of the Builder
for bits
. Returns that copy.
Attempts to store a negative value
or provide an insufficient or out-of-bounds bits
number throw an exception with exit code 5: Integer out of expected range
.
Usage example:
Builder.storeInt
Extension function for the Builder
.
Stores a signed bits
-bit value
into the copy of the Builder
for bits
. Returns that copy.
Attempts to provide an insufficient or out-of-bounds bits
number throw an exception with exit code 5: Integer out of expected range
.
Usage example:
Builder.storeBool
Extension function for the Builder
.
Stores a Bool
value
into the copy of the Builder
. Writes as a single bit if value
is true
, and writes otherwise. Returns that copy of the Builder
.
Usage example:
Builder.storeBit
Available since Tact 1.5Extension function for the Builder
. Alias to Builder.storeBool()
.
Usage example:
Builder.storeBuilder
Extension function for the Builder
.
Appends all data from a Builder
cell
to the copy of the Builder
. Returns that copy.
Usage example:
Builder.storeSlice
Extension function for the Builder
.
Stores a Slice
cell
into the copy of the Builder
. Returns that copy.
Usage example:
Builder.storeCoins
Extension function for the Builder
.
Stores (serializes) an unsigned Int
value
in the range into the copy of the Builder
. The serialization of value
consists of a -bit unsigned big-endian integer , which is the smallest integer , such that value
, followed by an -bit unsigned big-endian representation of value
. Returns that copy of the Builder
.
Attempts to store an out-of-bounds value
throw an exception with exit code 5: Integer out of expected range
.
This is the most common way of storing nanoToncoins.
Usage example:
Builder.storeAddress
Extension function for the Builder
.
Stores the address
in the copy of the Builder
. Returns that copy.
Usage example:
Builder.storeRef
Extension function for the Builder
.
Stores a reference cell
into the copy of the Builder
. Returns that copy.
As a single Cell
can store up to references, attempts to store more throw an exception with exit code 8: Cell overflow
.
Usage example:
Builder.storeMaybeRef
Available since Tact 1.5Extension function for the Builder
.
If the cell
is not null
, stores as a single bit and then reference cell
into the copy of the Builder
. Returns that copy.
If the cell
is null
, only stores as a single bit into the copy of the Builder
. Returns that copy.
As a single Cell
can store up to references, attempts to store more throw an exception with exit code 8: Cell overflow
.
Usage example:
Builder.refs
Extension function for the Builder
.
Returns the number of cell references already stored in the Builder
as an Int
.
Usage example:
Builder.bits
Extension function for the Builder
.
Returns the number of data bits already stored in the Builder
as an Int
.
Usage example:
Builder.asSlice
Extension function for the Builder
.
Converts the Builder
to a Slice
and returns it. Alias to self.endCell().beginParse()
.
Usage example:
Builder.asCell
Extension function for the Builder
.
Converts the Builder
to a Cell
and returns it. Alias to self.endCell()
.
Usage example:
Slice.loadUint
Extension mutation function for the Slice
.
Loads and returns an unsigned l
-bit Int
from the Slice
for l
.
Attempts to specify an out-of-bounds l
value throw an exception with exit code 5: Integer out of expected range
.
Attempts to load more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage example:
Slice.preloadUint
Extension function for the Slice
.
Preloads and returns an unsigned l
-bit Int
from the Slice
for l
. Doesn’t modify the Slice
.
Attempts to specify an out-of-bounds l
value throw an exception with exit code 5: Integer out of expected range
.
Attempts to preload more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage example:
Slice.loadInt
Extension mutation function for the Slice
.
Loads and returns a signed l
-bit Int
from the Slice
for l
.
Attempts to specify an out-of-bounds l
value throw an exception with exit code 5: Integer out of expected range
.
Attempts to load more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage example:
Slice.preloadInt
Extension function for the Slice
.
Preloads and returns a signed l
-bit Int
from the Slice
for l
. Doesn’t modify the Slice
.
Attempts to specify an out-of-bounds l
value throw an exception with exit code 5: Integer out of expected range
.
Attempts to preload more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage example:
Slice.loadBits
Extension mutation function for the Slice
.
Loads l
bits from the Slice
, and returns them as a separate Slice
.
Attempts to specify an out-of-bounds l
value throw an exception with exit code 5: Integer out of expected range
.
Attempts to load more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage example:
Slice.preloadBits
Extension function for the Slice
.
Preloads l
bits from the Slice
, and returns them as a separate Slice
. Doesn’t modify the original Slice
.
Attempts to specify an out-of-bounds l
value throw an exception with exit code 5: Integer out of expected range
.
Attempts to preload more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage example:
Slice.skipBits
Extension mutation function for the Slice
.
Loads all but the first l
bits from the Slice
.
Attempts to specify an out-of-bounds l
value throw an exception with exit code 5: Integer out of expected range
.
Attempts to load more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage example:
Slice.loadBool
Extension mutation function for the Slice
.
Loads a single bit and returns a Bool
value from the Slice
. Reads true
if the loaded bit is equal to , and reads false
otherwise.
Attempts to load such Bool
when Slice
doesn’t contain it throw an exception with exit code 8: Cell overflow
.
Attempts to load more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage example:
Slice.loadBit
Available since Tact 1.5Extension mutation function for the Slice
. Alias to Slice.loadBool()
.
Usage example:
Slice.loadCoins
Extension mutation function for the Slice
.
Loads and returns serialized an unsigned Int
value in the range from the Slice
. This value usually represents the amount in nanoToncoins.
Attempts to load such Int
when Slice
doesn’t contain it throw an exception with exit code 8: Cell overflow
.
Attempts to load more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage example:
Slice.loadAddress
Extension mutation function for the Slice
.
Loads and returns an Address
from the Slice
.
Attempts to load such Address
when Slice
doesn’t contain it throw an exception with exit code 8: Cell overflow
.
Attempts to load more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage example:
Slice.loadRef
Extension mutation function for the Slice
.
Loads the next reference from the Slice
as a Cell
.
Attempts to load such reference Cell
when Slice
doesn’t contain it throw an exception with exit code 8: Cell overflow
.
Attempts to load more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage examples:
Slice.preloadRef
Extension function for the Slice
.
Preloads the next reference from the Slice
as a Cell
. Doesn’t modify the original Slice
.
Attempts to preload such reference Cell
when Slice
doesn’t contain it throw an exception with exit code 8: Cell overflow
.
Attempts to preload more data than Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage examples:
Slice.refs
Extension function for the Slice
.
Returns the number of references in the Slice
as an Int
.
Usage example:
Slice.bits
Extension function for the Slice
.
Returns the number of data bits in the Slice
as an Int
.
Usage example:
Slice.empty
Extension function for the Slice
.
Checks whether the Slice
is empty (i.e., contains no bits of data and no cell references). Returns true
if it’s empty, false
otherwise.
Usage example:
Slice.dataEmpty
Extension function for the Slice
.
Checks whether the Slice
has no bits of data. Returns true
if it has no data, false
otherwise.
Usage example:
Slice.refsEmpty
Extension function for the Slice
.
Checks whether the Slice
has no references. Returns true
if it has no references, false
otherwise.
Usage example:
Slice.endParse
Extension function for the Slice
.
Checks whether the Slice
is empty (i.e., contains no bits of data and no cell references). If it’s not, throws an exception with exit code 9: Cell underflow
.
Usage examples:
Slice.hash
Extension function for the Slice
.
Calculates and returns an Int
value of the SHA-256 hash of the standard Cell
representation of the given Slice
.
Usage example:
Slice.asCell
Extension function for the Slice
.
Converts the Slice
to a Cell
and returns it. Alias to beginCell().storeSlice(self).endCell()
.
Usage example:
Address.asSlice
Extension function for the Address
.
Converts the Address
to a Slice
and returns it. Alias to beginCell().storeAddress(self).asSlice()
.
Usage example:
Struct.toCell
Extension function for any structure type Struct.
Converts the Struct to a Cell
and returns it.
Usage example:
Struct.toSlice
Available since Tact 1.5Extension function for any structure type Struct.
Converts the Struct to a Slice
and returns it. Alias to self.toCell().asSlice()
.
Usage example:
Struct.fromCell
Extension function for any structure type Struct.
Converts a Cell
into the specified Struct and returns that Struct.
Attempts to pass a Cell
with layout different from the specified Struct or to load more data than a Cell
contains throw an exception with exit code 9: Cell underflow
.
Usage examples:
Struct.fromSlice
Extension function for any structure type Struct.
Converts a Slice
into the specified Struct and returns that Struct.
Attempts to pass a Slice
with layout different from the specified Struct or to load more data than a Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage examples:
Message.toCell
Extension function for any message type Message.
Converts the Message to a Cell
and returns it.
Usage example:
Message.toSlice
Available since Tact 1.5Extension function for any message type Message.
Converts the Message to a Slice
and returns it. Alias to self.toCell().asSlice()
.
Usage example:
Message.fromCell
Extension function for any message type Message.
Converts a Cell
into the specified Message and returns that Message.
Attempts to pass a Cell
with layout different from the specified Message or to load more data than a Cell
contains throw an exception with exit code 9: Cell underflow
.
Usage examples:
Message.fromSlice
Extension function for any message type Message.
Converts a Slice
into the specified Message and returns that Message.
Attempts to pass a Slice
with layout different from the specified Message or to load more data than a Slice
contains throw an exception with exit code 9: Cell underflow
.
Usage examples: