Type casting
Cast plutus data to custom data types.
Type casting is currently an experimental feature. To convert Plutus data to JavaScript data structures, you'll need information about the type's shape, which is stored in a JSON schema. To make this process more convenient, we use a tool called Typebox.
import { Data } from "https://deno.land/x/lucid/mod.ts";
const ListingSchema = Data.Object({
owner: Data.Bytes(),
amount: Data.Integer(),
private: Data.Boolean(),
});
Furthermore, we can generate a TypeScript type definition from the data structure:
type Listing = Data.Static<typeof ListingSchema>;
const Listing = ListingSchema as unknown as Listing;
Cast to plutus data:
const listing = Data.to(
{ owner: "31313131313131", amount: 5252352323n, private: false },
Listing,
);
Cast from plutus data:
const listing: Listing = Data.from(
"d8799f47313131313131311b0000000139108943d87980ff",
Listing,
);