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
fun beginCell(): Builder;
Creates a new empty Builder
.
Usage example:
let fizz: Builder = beginCell();
emptyCell
Gas-expensivefun emptyCell(): Cell;
Creates and returns an empty Cell
(without data and references). Alias to beginCell().endCell()
.
Usage example:
let fizz: Cell = emptyCell();let buzz: Cell = beginCell().endCell();
fizz == buzz; // true
emptySlice
Gas-expensivefun emptySlice(): Slice;
Creates and returns an empty Slice
(without data and references). Alias to emptyCell().asSlice()
.
Usage example:
let fizz: Slice = emptySlice();let buzz: Slice = emptyCell().asSlice();
fizz == buzz; // true
Cell
A section to group all extension and extension mutation functions for the Cell
type.
Cell.beginParse
extends fun beginParse(self: Cell): Slice;
Extension function for the Cell
.
Opens the Cell
for parsing and returns it as a Slice
.
Usage example:
let c: Cell = emptyCell();let fizz: Slice = c.beginParse();
Cell.depth
Available since Tact 1.6 (not released yet)extends fun depth(self: Cell?): Int;
Extension function for the Cell
.
Computes and returns the Int
depth of the Cell
. Produces if the Cell
has no references, otherwise plus the maximum of the depths of the referenced cells. If self
is null
, returns .
Usage example:
let c: Cell = beginCell().storeInt(42, 7).endCell();let depth: Int = c.depth(); // 0
Cell.computeDataSize
Gas-expensive Available since Tact 1.6 (not released yet)extends fun computeDataSize(self: Cell?, maxCells: Int): DataSize;
Extension function for the Cell
.
Computes and returns the number of distinct cells, bits and refs in the Cell
by using a depth-first search (DFS) algorithm, recursively traversing each referenced cell. This function is computationally expensive and can consume a lot of gas. If self
is null
, returns a DataSize
with all fields set to .
The results are packed into a DataSize
Struct consisting of:
Field | Type | Description |
---|---|---|
cells | Int | The total number of nested cells, including the starting one |
bits | Int | The total number of bits in all nested cells, including the starting one |
refs | Int | The total number of refs in all nested cells, including the starting one |
If the specified maxCells
value isn’t enough to traverse all cells including the starting one, an exception with exit code 8 is thrown: Cell overflow
.
Attempts to specify a negative value of maxCells
throw an exception with exit code 5: Integer out of expected range
.
Usage example:
let c: Cell = beginCell().storeInt(42, 7).storeRef(emptyCell()).endCell();try { let dataSize: DataSize = c.computeDataSize(2); dataSize.cells; // 2 dataSize.bits; // 7 dataSize.refs; // 1} catch (exitCode) { // if maxCells was insufficient to traverse the cell // and all of its references, the exitCode here would be 8}
Cell.hash
extends fun hash(self: Cell): Int;
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:
let c: Cell = emptyCell();let fizz: Int = c.hash();
Cell.asSlice
extends fun asSlice(self: Cell): Slice;
Extension function for the Cell
.
Converts the Cell to a Slice
and returns it. Alias to self.beginParse()
.
Usage example:
let c: Cell = emptyCell();let fizz: Slice = c.asSlice();
Builder
A section to group all extension and extension mutation functions for the Builder
type.
Builder.endCell
Gas-expensiveextends fun endCell(self: Builder): Cell;
Extension function for the Builder
.
Converts a Builder
into an ordinary Cell
.
Usage example:
let b: Builder = beginCell();let fizz: Cell = b.endCell();
Builder.storeUint
extends fun storeUint(self: Builder, value: Int, bits: Int): Builder;
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:
let b: Builder = beginCell();let fizz: Builder = b.storeUint(42, 6);
Builder.storeInt
extends fun storeInt(self: Builder, value: Int, bits: Int): Builder;
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:
let b: Builder = beginCell();let fizz: Builder = b.storeInt(42, 7);
Builder.storeBool
extends fun storeBool(self: Builder, value: Bool): Builder;
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:
let b: Builder = beginCell();let fizz: Builder = b.storeBool(true); // writes 1let buzz: Builder = b.storeBool(false); // writes 0
Builder.storeBit
Available since Tact 1.5extends fun storeBit(self: Builder, value: Bool): Builder;
Extension function for the Builder
. Alias to Builder.storeBool()
.
Usage example:
let b: Builder = beginCell();let fizz: Builder = b.storeBit(true); // writes 1let buzz: Builder = b.storeBit(false); // writes 0
Builder.storeBuilder
extends fun storeBuilder(self: Builder, cell: Builder): Builder;
Extension function for the Builder
.
Appends all data from a Builder
cell
to the copy of the Builder
. Returns that copy.
Usage example:
let b: Builder = beginCell().storeCoins(42);let fizz: Builder = beginCell().storeBuilder(b);b.endCell() == fizz.endCell(); // true
Builder.storeSlice
extends fun storeSlice(self: Builder, cell: Slice): Builder;
Extension function for the Builder
.
Stores a Slice
cell
into the copy of the Builder
. Returns that copy.
Usage example:
let b: Builder = beginCell();let s: Slice = emptyCell().asSlice();let fizz: Builder = b.storeSlice(s);
Builder.storeCoins
extends fun storeCoins(self: Builder, value: Int): Builder;
Extension function for the Builder
.
Stores (serializes) an unsigned Int
value
in the range from to inclusive 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:
let b: Builder = beginCell();let fizz: Builder = b.storeCoins(42);
Builder.storeVarUint16
Available since Tact 1.6 (not released yet)extends fun storeVarUint16(self: Builder, value: Int): Builder;
Extension function for the Builder
. Alias to Builder.storeCoins()
.
Usage example:
let b: Builder = beginCell();let fizz: Builder = b.storeVarUint16(42);
Builder.storeVarInt16
Available since Tact 1.6 (not released yet)extends fun storeVarInt16(self: Builder, value: Int): Builder;
Extension function for the Builder
.
Similar to Builder.storeCoins()
, but with a different value
range: from to inclusive.
Attempts to store an out-of-bounds value
throw an exception with exit code 5: Integer out of expected range
.
Usage example:
let b: Builder = beginCell();let fizz: Builder = b.storeVarInt16(-42);
Builder.storeVarUint32
Available since Tact 1.6 (not released yet)extends fun storeVarUint32(self: Builder, value: Int): Builder;
Extension function for the Builder
.
Stores (serializes) an unsigned Int
value
in the range from to inclusive 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
.
Usage example:
let b: Builder = beginCell();let fizz: Builder = b.storeVarUint32(420000);
Builder.storeVarInt32
Available since Tact 1.6 (not released yet)extends fun storeVarInt32(self: Builder, value: Int): Builder;
Extension function for the Builder
.
Similar to Builder.storeVarUint32()
, but with a different value
range: from to inclusive.
Attempts to store an out-of-bounds value
throw an exception with exit code 5: Integer out of expected range
.
Usage example:
let b: Builder = beginCell();let fizz: Builder = b.storeVarInt32(-420000);
Builder.storeAddress
extends fun storeAddress(self: Builder, address: Address): Builder;
Extension function for the Builder
.
Stores the address
in the copy of the Builder
. Returns that copy.
Usage example:
let b: Builder = beginCell();let fizz: Builder = b.storeAddress(myAddress());
Builder.storeRef
extends fun storeRef(self: Builder, cell: Cell): Builder;
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:
let b: Builder = beginCell();let fizz: Builder = b.storeRef(emptyCell());
Builder.storeMaybeRef
Available since Tact 1.5extends fun storeMaybeRef(self: Builder, cell: Cell?): Builder;
Extension 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:
let b: Builder = beginCell();let fizz: Builder = b .storeMaybeRef(emptyCell()) // stores a single 1 bit, then an empty cell .storeMaybeRef(null); // stores only a single 0 bit
Builder.refs
extends fun refs(self: Builder): Int;
Extension function for the Builder
.
Returns the number of cell references already stored in the Builder
as an Int
.
Usage example:
let b: Builder = beginCell();let fizz: Int = b.refs(); // 0
Builder.bits
extends fun bits(self: Builder): Int;
Extension function for the Builder
.
Returns the number of data bits already stored in the Builder
as an Int
.
Usage example:
let b: Builder = beginCell();let fizz: Int = b.bits(); // 0
Builder.depth
Available since Tact 1.6 (not released yet)extends fun depth(self: Builder): Int;
Extension function for the Builder
.
Computes and returns the Int
depth of the Builder
. Produces if the Builder
has no references stored so far, otherwise plus the maximum of the depths of the referenced cells.
Usage example:
let b: Builder = beginCell().storeInt(42, 7);let depth: Int = b.depth(); // 0
Builder.asSlice
extends fun asSlice(self: Builder): Slice;
Extension function for the Builder
.
Converts the Builder
to a Slice
and returns it. Alias to self.endCell().beginParse()
.
Usage example:
let b: Builder = beginCell();let fizz: Slice = b.asSlice();
Builder.asCell
extends fun asCell(self: Builder): Cell;
Extension function for the Builder
.
Converts the Builder
to a Cell
and returns it. Alias to self.endCell()
.
Usage example:
let b: Builder = beginCell();let fizz: Cell = b.asCell();
Slice
A section to group all extension and extension mutation functions for the Slice
type.
Slice.loadUint
extends mutates fun loadUint(self: Slice, l: Int): Int;
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:
let s: Slice = beginCell().storeUint(42, 7).asSlice();let fizz: Int = s.loadUint(7);
Slice.preloadUint
extends fun preloadUint(self: Slice, l: Int): Int;
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:
let s: Slice = beginCell().storeUint(42, 7).asSlice();let fizz: Int = s.preloadUint(7);
Slice.loadInt
extends mutates fun loadInt(self: Slice, l: Int): Int;
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:
let s: Slice = beginCell().storeInt(42, 7).asSlice();let fizz: Int = s.loadInt(7);
Slice.preloadInt
extends fun preloadInt(self: Slice, l: Int): Int;
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:
let s: Slice = beginCell().storeInt(42, 7).asSlice();let fizz: Int = s.preloadInt(7);
Slice.loadBits
extends mutates fun loadBits(self: Slice, l: Int): Slice;
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:
let s: Slice = beginCell().storeInt(42, 7).asSlice();let fizz: Slice = s.loadBits(7);
Slice.preloadBits
extends fun preloadBits(self: Slice, l: Int): Slice;
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:
let s: Slice = beginCell().storeInt(42, 7).asSlice();let fizz: Slice = s.preloadBits(7);
Slice.skipBits
extends mutates fun skipBits(self: Slice, l: Int);
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:
let s: Slice = beginCell().storeInt(42, 7).asSlice();s.skipBits(5); // all but first 5 bitslet fizz: Slice = s.loadBits(1); // load only 1 bit
Slice.skipLastBits
Available since Tact 1.6 (not released yet)extends fun skipLastBits(self: Slice, len: Int);
Extension function for the Slice
.
Preloads all but the last len
bits from the Slice
.
Attempts to specify an out-of-bounds len
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:
let s: Slice = beginCell().storeInt(42, 7).asSlice();let allButLastFive: Slice = s.skipLastBits(5); // all but last 5 bits, // i.e. only first 2
Slice.loadBool
extends mutates fun loadBool(self: Slice): Bool;
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:
let s: Slice = beginCell().storeBool(true).asSlice();let fizz: Bool = s.loadBool(); // true
Slice.loadBit
Available since Tact 1.5extends mutates fun loadBit(self: Slice): Bool;
Extension mutation function for the Slice
. Alias to Slice.loadBool()
.
Usage example:
let s: Slice = beginCell().storeBit(true).asSlice();let fizz: Bool = s.loadBit(); // true
Slice.loadCoins
extends mutates fun loadCoins(self: Slice): Int;
Extension mutation function for the Slice
.
Loads and returns a serialized unsigned Int
value in the range from to inclusive 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:
let s: Slice = beginCell().storeCoins(42).asSlice();let fizz: Int = s.loadCoins(); // 42
Slice.loadVarUint16
Available since Tact 1.6 (not released yet)extends mutates fun loadVarUint16(self: Slice): Int;
Extension mutation function for the Slice
. Alias to Slice.loadCoins()
.
Usage example:
let s: Slice = beginCell().storeVarUint16(42).asSlice();let fizz: Int = s.loadVarUint16(); // 42
Slice.loadVarInt16
Available since Tact 1.6 (not released yet)extends mutates fun loadVarInt16(self: Slice): Int;
Extension mutation function for the Slice
.
Similar to Slice.loadCoins()
, but with a different value
range: from to inclusive.
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:
let s: Slice = beginCell().storeVarInt16(-42).asSlice();let fizz: Int = s.loadVarInt16(); // -42
Slice.loadVarUint32
Available since Tact 1.6 (not released yet)extends mutates fun loadVarUint32(self: Slice): Int;
Loads and returns a serialized unsigned Int
value in the range from to inclusive from the Slice
.
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:
let s: Slice = beginCell().storeVarUint32(420000).asSlice();let fizz: Int = s.loadVarUint32(); // 420000
Slice.loadVarInt32
Available since Tact 1.6 (not released yet)extends mutates fun loadVarInt32(self: Slice): Int;
Similar to Slice.loadVarUint32()
, but with a different value
range: from to inclusive.
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:
let s: Slice = beginCell().storeVarInt32(-420000).asSlice();let fizz: Int = s.loadVarInt32(); // -420000
Slice.loadAddress
extends mutates fun loadAddress(self: Slice): Address;
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:
let s: Slice = beginCell().storeAddress(myAddress()).asSlice();let fizz: Address = s.loadAddress();
Slice.loadRef
extends mutates fun loadRef(self: Slice): Cell;
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:
let s1: Slice = beginCell().storeRef(emptyCell()).asSlice();let fizz: Cell = s1.loadRef();
let s2: Slice = beginCell() .storeRef(emptyCell()) .storeRef(s1.asCell()) .asSlice();let ref1: Cell = s2.loadRef();let ref2: Cell = s2.loadRef();ref1 == ref2; // false
Slice.preloadRef
extends fun preloadRef(self: Slice): Cell;
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:
let s1: Slice = beginCell().storeRef(emptyCell()).asSlice();let fizz: Cell = s1.preloadRef(); // didn't modify s1
let s2: Slice = beginCell() .storeRef(emptyCell()) .storeRef(s1.asCell()) .asSlice();let ref1: Cell = s2.preloadRef();let ref2: Cell = s2.preloadRef();ref1 == ref2; // true
Slice.refs
extends fun refs(self: Slice): Int;
Extension function for the Slice
.
Returns the number of references in the Slice
as an Int
.
Usage example:
let s: Slice = beginCell().storeRef(emptyCell()).asSlice();let fizz: Int = s.refs();
Slice.bits
extends fun bits(self: Slice): Int;
Extension function for the Slice
.
Returns the number of data bits in the Slice
as an Int
.
Usage example:
let s: Slice = beginCell().storeRef(emptyCell()).asSlice();let fizz: Int = s.bits();
Slice.firstBits
Available since Tact 1.6 (not released yet)extends fun firstBits(self: Slice, len: Int): Slice;
Extension function for the Slice
.
Preloads the first len
bits from the Slice
.
Attempts to specify an out-of-bounds len
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:
let s: Slice = beginCell().storeInt(42, 7).asSlice();let firstFive: Slice = s.firstBits(5); // first 5 bits
Slice.lastBits
Available since Tact 1.6 (not released yet)extends fun lastBits(self: Slice, len: Int): Slice;
Extension function for the Slice
.
Preloads the last len
bits from the Slice
.
Attempts to specify an out-of-bounds len
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:
let s: Slice = beginCell().storeInt(42, 7).asSlice();let lastFive: Slice = s.lastBits(5); // last 5 bits
Slice.depth
Available since Tact 1.6 (not released yet)extends fun depth(self: Slice): Int;
Extension function for the Slice
.
Computes and returns the Int
depth of the Slice
. Produces if the Slice
has no references, otherwise plus the maximum of the depths of the referenced cells.
Usage example:
let s: Slice = beginCell().storeInt(42, 7).asSlice();let depth: Int = s.depth(); // 0
Slice.computeDataSize
Gas-expensive Available since Tact 1.6 (not released yet)extends fun computeDataSize(self: Slice, maxCells: Int): DataSize;
Extension function for the Slice
.
Similar to Cell.computeDataSize()
, but doesn’t take into account the cell that contains the Slice
itself. However, accounts for its bits and refs.
The results are packed into a DataSize
Struct consisting of:
Field | Type | Description |
---|---|---|
cells | Int | The total number of nested cells, including the starting one |
bits | Int | The total number of bits in all nested cells, including the starting one |
refs | Int | The total number of refs in all nested cells, including the starting one |
If the specified maxCells
value isn’t enough to traverse all cells not including the starting one, an exception with exit code 8 is thrown: Cell overflow
.
Attempts to specify a negative value of maxCells
throw an exception with exit code 5: Integer out of expected range
.
Usage example:
let s: Slice = beginCell().storeInt(42, 7).storeRef(emptyCell()).asSlice();try { let dataSize: DataSize = s.computeDataSize(1); dataSize.cells; // 1 dataSize.bits; // 7 dataSize.refs; // 1} catch (exitCode) { // if maxCells was insufficient to traverse the cell // and all of its references, the exitCode here would be 8}
Slice.empty
extends fun empty(self: Slice): Bool;
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:
let s: Slice = beginCell().storeRef(emptyCell()).asSlice();let fizz: Bool = s.empty(); // falselet buzz: Bool = beginCell().asSlice().empty(); // true
Slice.dataEmpty
extends fun dataEmpty(slice: Slice): Bool;
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:
let s: Slice = beginCell().storeRef(emptyCell()).asSlice();let fizz: Bool = s.dataEmpty(); // true
let s2: Slice = beginCell().storeInt(42, 7).asSlice();let buzz: Bool = s2.dataEmpty(); // false
Slice.refsEmpty
extends fun refsEmpty(slice: Slice): Bool;
Extension function for the Slice
.
Checks whether the Slice
has no references. Returns true
if it has no references, false
otherwise.
Usage example:
let s: Slice = beginCell().storeRef(emptyCell()).asSlice();let fizz: Bool = s.refsEmpty(); // falselet buzz: Bool = beginCell().asSlice().refsEmpty(); // true
Slice.endParse
extends fun endParse(self: Slice);
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:
let emptyOne: Slice = emptySlice();emptyOne.endParse(); // nothing, as it's empty
let paul: Slice = "Fear is the mind-killer".asSlice();try { paul.endParse(); // throws exit code 9}
Slice.hash
Gas-expensiveextends fun hash(self: Slice): Int;
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:
let s: Slice = beginCell().asSlice();let fizz: Int = s.hash();
Slice.asCell
Gas-expensiveextends fun asCell(self: Slice): Cell;
Extension function for the Slice
.
Converts the Slice
to a Cell
and returns it. Alias to beginCell().storeSlice(self).endCell()
.
Usage example:
let s: Slice = beginCell().asSlice();let fizz: Cell = s.asCell();let buzz: Cell = beginCell().storeSlice(s).endCell();
fizz == buzz; // true
Slice.asAddress
Available since Tact 1.6 (not released yet)extends fun asAddress(self: Slice, chain: Int): Address;
Extension function for the Slice
.
Casts the Slice
to an Address
in a given chain
ID and returns it. The inverse of Address.asSlice()
and a safe but more gas-expensive version of Slice.asAddressUnsafe()
.
Attempts to specify a Slice
with an invalid account ID length throw an error with exit code 136: Invalid standard address
.
Attempts to specify a Slice
with an invalid tag prefix (not 0b100
) or with an invalid account ID length (not bits) throw an error with exit code 136: Invalid standard address
.
Usage example:
let a: Address = myAddress(); // let's assume we're in a basechainlet a2: Address = a.asSlice().asAddress(0); // so the chain ID is 0
a == a2; // true
Slice.asAddressUnsafe
Available since Tact 1.6 (not released yet)extends fun asAddressUnsafe(self: Slice): Address;
Extension function for the Slice
.
Unsafely casts the Slice
to an Address
and returns it. The inverse of Address.asSlice()
.
This function does not perform any checks on the contents of the Slice
.
Usage example:
let a: Address = myAddress();let a2: Address = a.asSlice().asAddressUnsafe();
a == a2; // true
Address.asSlice
extends fun asSlice(self: Address): Slice;
Extension function for the Address
.
Casts the Address
back to the underlying Slice
and returns it. The inverse of Slice.asAddressUnsafe()
.
Usage example:
let a: Address = myAddress();let fizz: Slice = beginCell().storeAddress(a).asSlice();let buzz: Slice = a.asSlice(); // cheap, unlike the previous statement
fizz == buzz; // true
Struct.toCell
extends fun toCell(self: Struct): Cell;
Extension function for any structure type Struct.
Converts the Struct to a Cell
and returns it.
Usage example:
struct GuessCoin { probably: Int as coins; nothing: Int as coins;}
fun coinCell(): Cell { let s: GuessCoin = GuessCoin{ probably: 42, nothing: 27 }; let fizz: Cell = s.toCell();
return fizz; // "x{12A11B}"}
Struct.toSlice
Available since Tact 1.5extends fun toSlice(self: Struct): Slice;
Extension function for any structure type Struct.
Converts the Struct to a Slice
and returns it. Alias to self.toCell().asSlice()
.
Usage example:
struct GuessCoin { probably: Int as coins; nothing: Int as coins;}
fun coinSlice(): Slice { let s: GuessCoin = GuessCoin{ probably: 42, nothing: 27 }; let fizz: Slice = s.toSlice();
return fizz; // "CS{Cell{000612a11b} bits: 0..24; refs: 0..0}"}
Struct.fromCell
extends fun fromCell(self: Struct, cell: Cell): Struct;
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 GuessCoin { probably: Int as coins; nothing: Int as coins;}
fun directParse(payload: Cell): GuessCoin { return GuessCoin.fromCell(payload);}
fun cautiousParse(payload: Cell): GuessCoin? { let coin: GuessCoin? = null; try { coin = GuessCoin.fromCell(payload); } catch (e) { dump("Cell payload doesn't match GuessCoin Struct!"); } return coin;}
Struct.fromSlice
extends fun fromSlice(self: Struct, slice: Slice): Struct;
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:
struct GuessCoin { probably: Int as coins; nothing: Int as coins;}
fun directParse(payload: Slice): GuessCoin { return GuessCoin.fromSlice(payload);}
fun cautiousParse(payload: Slice): GuessCoin? { let coin: GuessCoin? = null; try { coin = GuessCoin.fromSlice(payload); } catch (e) { dump("Slice payload doesn't match GuessCoin Struct!"); } return coin;}
Message.toCell
extends fun toCell(self: Message): Cell;
Extension function for any message type Message.
Converts the Message to a Cell
and returns it.
Usage example:
message GuessCoin { probably: Int as coins; nothing: Int as coins;}
fun coinCell(): Cell { let s: GuessCoin = GuessCoin{ probably: 42, nothing: 27 }; let fizz: Cell = s.toCell();
return fizz; // "x{AB37107712A11B}"}
Message.toSlice
Available since Tact 1.5extends fun toSlice(self: Message): Slice;
Extension function for any message type Message.
Converts the Message to a Slice
and returns it. Alias to self.toCell().asSlice()
.
Usage example:
message GuessCoin { probably: Int as coins; nothing: Int as coins;}
fun coinSlice(): Slice { let s: GuessCoin = GuessCoin{ probably: 42, nothing: 27 }; let fizz: Slice = s.toSlice();
return fizz; // "CS{Cell{000eab37107712a11b} bits: 0..56; refs: 0..0}"}
Message.fromCell
extends fun fromCell(self: Message, cell: Cell): Message;
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(0x777) TripleAxe { prize: Int as uint32;}
fun directParse(payload: Cell): TripleAxe { return TripleAxe.fromCell(payload);}
fun cautiousParse(payload: Cell): TripleAxe? { let coin: TripleAxe? = null; try { coin = TripleAxe.fromCell(payload); } catch (e) { dump("Cell payload doesn't match TripleAxe Message!"); } return coin;}
Message.fromSlice
extends fun fromSlice(self: Message, slice: Slice): Message;
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:
message(0x777) TripleAxe { prize: Int as uint32;}
fun directParse(payload: Slice): TripleAxe { return TripleAxe.fromSlice(payload);}
fun cautiousParse(payload: Slice): TripleAxe? { let coin: TripleAxe? = null; try { coin = TripleAxe.fromSlice(payload); } catch (e) { dump("Slice payload doesn't match TripleAxe Message!"); } return coin;}