Skip to content

Commit

Permalink
chore: Stub of OmnichainConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Nov 23, 2023
1 parent 9ec7803 commit 2b6e0e7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/ua-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./schema"
export * from "./types"
16 changes: 16 additions & 0 deletions packages/ua-utils/src/schema.ts
Original file line number Diff line number Diff line change
@@ -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 = <TConfig = unknown>(
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef, unknown>
): z.ZodSchema<ContractSpec<TConfig>, z.ZodTypeDef, unknown> =>
z.object({
address: AddressSchema,
endpointId: EndpointIdSchema,
config: configSchema,
}) as z.ZodSchema<ContractSpec<TConfig>, z.ZodTypeDef, unknown>
20 changes: 20 additions & 0 deletions packages/ua-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { EndpointId } from "@layerzerolabs/lz-definitions"

export type Address = string

export interface ContractSpec<TConfig = unknown> {
endpointId: EndpointId
address: Address
config: TConfig
}

export interface ContractConnectionSpec<TConfig = unknown> {
from: EndpointId
to: EndpointId
config: TConfig
}

export interface OmnichainSpec<TContractConfig = unknown, TConnectionConfig = unknown> {
contracts: ContractSpec<TContractConfig>[]
connections: ContractConnectionSpec<TConnectionConfig>[]
}
57 changes: 57 additions & 0 deletions packages/ua-utils/test/schema.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
})
})
})

0 comments on commit 2b6e0e7

Please sign in to comment.