Skip to main content
In TVM, the Cell type contains only metadata of the cell: level, hashes, and depths. To read actual data, TVM need to load cell from celldb, key-value storage of the node, which stores cells by their representation hashes. CTOS instruction loads a cell by its metadata from Cell type, and provides a Slice, a read-only wrapper for the cell’s content. To simplify coding cell deserializers in smart contracts, instead of behaving like a simple bit/ref array, Slice is a “read cursor”: it allows loading a piece of data from the beginning of the slice, returning that data and the slice that contains remaining data. On the other hand, there is a Builder type, which provides a convenient way to serialize data to a cell. Only the Cell type can be used outside of TVM: in output actions and in persistent storage.

Builder

Builder provides a way to construct a cell from a sequence of values of the following TVM types: integers, cells (as references), slices, and builders. Tuples, continuations, and null are not serializable. For example, serialize the following numbers to a cell:
First, create an empty builder using NEWC:
Fift
Then, put a number on the stack:
Fift
And call STU (“STore Unsigned integer”) to store integer into builder (swapping builder and value to meet STU input order):
Fift
Then, store the other two numbers:
Fift
And, finally, ENDC instruction finalizes the builder to a cell.

Slice

Slice allows reading data back from a cell, field by field. For example, deserialize bitstring x{0001001011111111} created above. CTOS (“Cell TO Slice”) loads a Cell to a Slice.
Fift
Then, call LDU (“LoaD Unsigned integer”) to read first value (uint4).
Fift
LDU takes the first 4 bits from a slice and creates a new slice without these 4 bits: x{0001|001011111111} slices into a number 0001 and a slice x{001011111111}. Similarly, read another two numbers:
Fift
A common way to ensure there is no data left inside the slice is to call ENDS.