Skip to content

Commit

Permalink
chore: Schema & types for OApp configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Nov 22, 2023
1 parent 5fb1838 commit 3c873ee
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/ua-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"directory": "packages/ua-utils"
},
"devDependencies": {
"@layerzerolabs/lz-definitions": "~1.5.58",
"@types/mocha": "^10.0.1",
"@types/sinon": "^17.0.2",
"chai": "^4.3.10",
Expand All @@ -37,5 +38,11 @@
"ts-node": "^10.9.1",
"tsup": "~7.2.0",
"typescript": "^5.2.2"
},
"peerDependencies": {
"@layerzerolabs/lz-definitions": "~1.5.58"
},
"dependencies": {
"zod": "^3.22.4"
}
}
File renamed without changes.
8 changes: 8 additions & 0 deletions packages/ua-utils/src/common/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { EndpointId } from "@layerzerolabs/lz-definitions"
import { z } from "zod"

export const NetworkNameSchema = z.string()

export const EndpointIdSchema = z.nativeEnum(EndpointId)

export const CoerceEndpointIdSchema = z.coerce.number(EndpointIdSchema)
5 changes: 4 additions & 1 deletion packages/ua-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from "./property"
export * from "./common/property"
export * from "./common/schema"
export * from "./oapp/schema"
export * from "./oapp/types"
31 changes: 31 additions & 0 deletions packages/ua-utils/src/oapp/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { z } from "zod"
import { CoerceEndpointIdSchema } from "@/common/schema"
import { endpointIdToStage } from "@layerzerolabs/lz-definitions"

export const EnforcedOptionsSchema = z.object({
msgType: z.number().int().nonnegative().max(0xffff),
options: z.string(),
})

export const OAppConfigConnectionSchema = z.object({
enforcedOptions: EnforcedOptionsSchema,
})

export const OAppConfigContractSchema = z.record(CoerceEndpointIdSchema, OAppConfigConnectionSchema)

export const OAppConfigSchema = z.record(CoerceEndpointIdSchema, OAppConfigContractSchema).superRefine((config, ctx) => {
Object.entries(config).forEach(([srcEndpointId, endpointConfig]) => {
const srcStage = endpointIdToStage(CoerceEndpointIdSchema.parse(srcEndpointId))

Object.entries(endpointConfig).forEach(([dstEndpointId]) => {
const dstStage = endpointIdToStage(CoerceEndpointIdSchema.parse(dstEndpointId))

if (srcStage === dstStage) return

ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Invalid peer: endpoint ${srcEndpointId} on stage ${srcStage} cannot be connected to endpoint ${dstEndpointId} on stage ${dstStage}`,
})
})
})
})
6 changes: 6 additions & 0 deletions packages/ua-utils/src/oapp/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { z } from "zod"
import { OAppConfigSchema, OAppConfigContractSchema } from "./schema"

export type OAppConfig = z.TypeOf<typeof OAppConfigSchema>

export type OAppConfigContract = z.TypeOf<typeof OAppConfigContractSchema>
64 changes: 64 additions & 0 deletions packages/ua-utils/test/oapp.schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { expect } from "chai"
import { describe } from "mocha"
import { EndpointId } from "@layerzerolabs/lz-definitions"
import { OAppConfigSchema } from "@/oapp/schema"

describe("oapp/schema", () => {
describe("OAppConfigSchema", () => {
it("should pass with an empty object", () => {
const config = {}

expect(OAppConfigSchema.parse(config)).to.eql({})
})

it("should pass with a single network with no connections", () => {
const config = {
[EndpointId.AVALANCHE_MAINNET]: {},
}

expect(OAppConfigSchema.parse(config)).to.eql(config)
})

it("should fail with a single network with one misconfigured connection on the same stage", () => {
const config = {
[EndpointId.AVALANCHE_MAINNET]: {
[EndpointId.ETHEREUM_MAINNET]: {},
},
}

expect(() => OAppConfigSchema.parse(config)).to.throw()
})

it("should pass with a single network with one connection on the same stage", () => {
const config = {
[EndpointId.AVALANCHE_MAINNET]: {
[EndpointId.ETHEREUM_MAINNET]: {
enforcedOptions: {
msgType: 1,
options: "",
},
},
},
}

expect(OAppConfigSchema.parse(config)).to.eql(config)
})

it("should fail with a single network with one connection on a different stage", () => {
const config = {
[EndpointId.AVALANCHE_MAINNET]: {
[EndpointId.AVALANCHE_TESTNET]: {
enforcedOptions: {
msgType: 1,
options: "",
},
},
},
}

expect(() => OAppConfigSchema.parse(config)).to.throw(
"Invalid peer: endpoint 106 on stage mainnet cannot be connected to endpoint 10106 on stage testnet"
)
})
})
})
2 changes: 1 addition & 1 deletion packages/ua-utils/test/property.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai"
import { describe } from "mocha"
import sinon from "sinon"
import { createProperty, isConfigured, isMisconfigured } from "../src/property"
import { createProperty, isConfigured, isMisconfigured } from "@/common/property"

describe("property", () => {
describe("isMisconfigured", () => {
Expand Down

0 comments on commit 3c873ee

Please sign in to comment.