Skip to main content

data

Pebble 0.3.1 · all symbols on this page are stable.

The Plutus Core Data type: a serialization format that every on-chain type can be encoded into and decoded from. Datums, redeemers, and the script context all flow through data at the system boundary.

You rarely write data directly; the compiler does it for you whenever you use .toData() or pattern-match a datum. Reach for raw data only when you need to construct or inspect the CBOR shape yourself.

data has five constructors (CBOR major types in parentheses):

ConstructorShapeBuiltin builder
Constr(index: int, fields: List<data>)std.builtins.constrData
MapLinearMap<data, data>std.builtins.mapData
ListList<data>std.builtins.listData
I (integer)intstd.builtins.iData
B (bytes)bytesstd.builtins.bData

Operators

CategoryOperators
Equality==, !=

Equality is structural CBOR equality, implemented by equalsData.

Working with data

using { constrData, unConstrData, iData, bData, listData } = std.builtins;
using { strToData } = std.data;

// Build:
const d: data = constrData(0, [iData(42), bData(#cafe)]);

// Destructure:
const RawConstr{ index, fields } = unConstrData(d);
match index {
0: trace("Some"),
_: fail "unknown constructor"
}

For most use cases prefer the .toData() method (via the ToData interface) and pattern-matching on the typed value, rather than touching raw data.

See also