Skip to content

Commit

Permalink
Create zod schemas for StackItem
Browse files Browse the repository at this point in the history
  • Loading branch information
d0rich committed Jun 23, 2024
1 parent 69c6de9 commit db72c8c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { TupleItem, TupleReader } from '@ton/core';
import type { StackItem } from './types';
import { stackItemSchema } from './types';
import { parseStackItem } from './parseStackItem';

export function parseStack(src: unknown[]) {
let stack: TupleItem[] = [];

for (let s of src) {
stack.push(parseStackItem(s as StackItem));
stack.push(parseStackItem(stackItemSchema.parse(s)));
}

return new TupleReader(stack);
Expand Down
48 changes: 37 additions & 11 deletions src/parser/types/stackItems.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
import { z } from 'zod';
import type { TvmTuple, TvmList } from './tvmValues';

type SerializedCell = { bytes: string };
const serializedCellSchema = z.object({
bytes: z.string(),
});
type SerializedCell = z.infer<typeof serializedCellSchema>;

type NullStackItem = ['null'];
type NumStackItem = ['num', string];
type CellStackItem = ['cell', SerializedCell];
type SliceStackItem = ['slice', SerializedCell];
type BuilderStackItem = ['builder', SerializedCell];
const nullSchema = z.union([
z.tuple([z.literal('null')]),
z.tuple([z.literal('null'), z.null().optional()]),
]);
type NullStackItem = z.infer<typeof nullSchema>;

type TupleStackItem = ['tuple', TvmTuple];
type ListStackItem = ['list', TvmList];
const numSchema = z.tuple([z.literal('num'), z.string()]);
type NumStackItem = z.infer<typeof numSchema>;

export type StackItem = NullStackItem | NumStackItem
| CellStackItem | SliceStackItem | BuilderStackItem
| TupleStackItem | ListStackItem;
const cellSchema = z.tuple([z.literal('cell'), serializedCellSchema]);
type CellStackItem = z.infer<typeof cellSchema>;

const sliceSchema = z.tuple([z.literal('slice'), serializedCellSchema]);
type SliceStackItem = z.infer<typeof sliceSchema>;

const builderSchema = z.tuple([z.literal('builder'), serializedCellSchema]);
type BuilderStackItem = z.infer<typeof builderSchema>;

const tupleSchema = z.tuple([z.literal('tuple'), z.unknown() as z.ZodType<TvmTuple>]);
type TupleStackItem = z.infer<typeof tupleSchema>;

const listSchema = z.tuple([z.literal('list'), z.unknown() as z.ZodType<TvmList>]);
type ListStackItem = z.infer<typeof listSchema>;

export const stackItemSchema = z.union([
nullSchema,
numSchema,
cellSchema,
sliceSchema,
builderSchema,
tupleSchema,
listSchema,
]);
export type StackItem = NullStackItem | NumStackItem | CellStackItem | SliceStackItem | BuilderStackItem | TupleStackItem | ListStackItem;
1 change: 1 addition & 0 deletions src/parser/types/tvmValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type TvmCell = { '@type': 'tvm.cell', bytes: string };
type TvmStackEntryCell = { '@type': 'tvm.stackEntryCell', cell: TvmCell };

// Structured types for TON Virtual Machine values
// TODO: Check how list parsing works
export type TvmList = { '@type': 'tvm.list', elements: TvmValue[] };
type TvmStackEntryList = { '@type': 'tvm.stackEntryList', list: TvmList };

Expand Down

0 comments on commit db72c8c

Please sign in to comment.