-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ec7803
commit 2b6e0e7
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./schema" | ||
export * from "./types" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |