From 2b6e0e7f74c2e1a5784d75407b3a4819d9c97ac3 Mon Sep 17 00:00:00 2001 From: Jan Nanista Date: Wed, 22 Nov 2023 16:36:40 -0800 Subject: [PATCH] chore: Stub of OmnichainConfig --- packages/ua-utils/src/index.ts | 2 + packages/ua-utils/src/schema.ts | 16 ++++++++ packages/ua-utils/src/types.ts | 20 ++++++++++ packages/ua-utils/test/schema.test.ts | 57 +++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 packages/ua-utils/src/index.ts create mode 100644 packages/ua-utils/src/schema.ts create mode 100644 packages/ua-utils/src/types.ts create mode 100644 packages/ua-utils/test/schema.test.ts diff --git a/packages/ua-utils/src/index.ts b/packages/ua-utils/src/index.ts new file mode 100644 index 000000000..8827fed6e --- /dev/null +++ b/packages/ua-utils/src/index.ts @@ -0,0 +1,2 @@ +export * from "./schema" +export * from "./types" diff --git a/packages/ua-utils/src/schema.ts b/packages/ua-utils/src/schema.ts new file mode 100644 index 000000000..84fa47845 --- /dev/null +++ b/packages/ua-utils/src/schema.ts @@ -0,0 +1,16 @@ +import { EndpointId } from "@layerzerolabs/lz-definitions" +import { z } from "zod" +import type { ContractSpec } from "./types" + +export const AddressSchema = z.string() + +export const EndpointIdSchema = z.nativeEnum(EndpointId).pipe(z.number()) + +export const createContractSpecSchema = ( + configSchema: z.ZodSchema +): z.ZodSchema, z.ZodTypeDef, unknown> => + z.object({ + address: AddressSchema, + endpointId: EndpointIdSchema, + config: configSchema, + }) as z.ZodSchema, z.ZodTypeDef, unknown> diff --git a/packages/ua-utils/src/types.ts b/packages/ua-utils/src/types.ts new file mode 100644 index 000000000..2cf3862f8 --- /dev/null +++ b/packages/ua-utils/src/types.ts @@ -0,0 +1,20 @@ +import type { EndpointId } from "@layerzerolabs/lz-definitions" + +export type Address = string + +export interface ContractSpec { + endpointId: EndpointId + address: Address + config: TConfig +} + +export interface ContractConnectionSpec { + from: EndpointId + to: EndpointId + config: TConfig +} + +export interface OmnichainSpec { + contracts: ContractSpec[] + connections: ContractConnectionSpec[] +} diff --git a/packages/ua-utils/test/schema.test.ts b/packages/ua-utils/test/schema.test.ts new file mode 100644 index 000000000..edc60d965 --- /dev/null +++ b/packages/ua-utils/test/schema.test.ts @@ -0,0 +1,57 @@ +import { createContractSpecSchema } from "@/schema" +import { EndpointId } from "@layerzerolabs/lz-definitions" +import { expect } from "chai" +import { z } from "zod" + +describe("schema", () => { + describe("createContractSpecSchema", () => { + interface TestCase { + schema: z.ZodSchema + good: unknown[] + bad: unknown[] + } + + const TEST_CASES: TestCase[] = [ + { + schema: z.string(), + good: ["config", ""], + bad: [false, null, undefined, 1, {}], + }, + { + schema: z.object({ a: z.number().nonnegative() }), + good: [{ a: 0 }, { a: 1 }], + bad: [{ a: -1 }, false, "", []], + }, + ] + + TEST_CASES.forEach(({ schema, good, bad }, index) => { + const contractSpecSchema = createContractSpecSchema(schema) + + describe(`case ${index}`, () => { + good.forEach((config) => { + it(`should work for ${JSON.stringify(config)}`, () => { + const spec = { + endpointId: EndpointId.APTOS_MAINNET, + address: "0x0", + config, + } + + expect(contractSpecSchema.parse(spec)).to.eql(spec) + }) + }) + + bad.forEach((config) => { + it(`should not work for ${JSON.stringify(config)}`, () => { + const spec = { + endpointId: EndpointId.APTOS_MAINNET, + address: "0x0", + config, + } + + expect(() => contractSpecSchema.parse(spec)).to.throw() + }) + }) + }) + }) + }) +})