diff --git a/packages/ua-utils-evm-hardhat/src/utils/coordinates.ts b/packages/ua-utils-evm-hardhat/src/utils/coordinates.ts new file mode 100644 index 0000000000..46e2dd7e46 --- /dev/null +++ b/packages/ua-utils-evm-hardhat/src/utils/coordinates.ts @@ -0,0 +1 @@ +export const getCoordinate = (contractName: string): OmniNodeCoordinate | undefined => {} diff --git a/packages/ua-utils-evm/src/schema.ts b/packages/ua-utils-evm/src/schema.ts new file mode 100644 index 0000000000..99e2abca02 --- /dev/null +++ b/packages/ua-utils-evm/src/schema.ts @@ -0,0 +1,12 @@ +import type { OmniGraph } from "@layerzerolabs/ua-utils" +import { z } from "zod" + +export type OAppContractConfig = z.TypeOf + +export type OAppConnectionConfig = z.TypeOf + +export type OAppOmnichainConfig = OmniGraph + +export const OAppContractConfigSchema = z.object({}) + +export const OAppConnectionConfigSchema = z.object({}) diff --git a/packages/ua-utils/src/coordinates.ts b/packages/ua-utils/src/coordinates.ts deleted file mode 100644 index 05b806537f..0000000000 --- a/packages/ua-utils/src/coordinates.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { OmnichainEdgeCoordinates, OmnichainNodeCoordinate } from "./types" - -/** - * Compares two coordinates by value - * - * @param a `OmnichainNodeCoordinate` - * @param b `OmnichainNodeCoordinate` - * - * @returns `true` if the coordinates point to the same point in omniverse - */ -export const isCoordinateEqual = (a: OmnichainNodeCoordinate, b: OmnichainNodeCoordinate): boolean => a.address === b.address && a.eid === b.eid - -/** - * Compares two coordinate vectors - * - * @param a `OmnichainEdgeCoordinates` - * @param b `OmnichainEdgeCoordinates` - * - * @returns `true` if the coordinates point from and to the same point in omniverse - */ -export const areCoordinatesEqual = (a: OmnichainEdgeCoordinates, b: OmnichainEdgeCoordinates): boolean => - isCoordinateEqual(a.from, b.from) && isCoordinateEqual(a.to, b.to) - -/** - * Serializes a coordinate. Useful for when coordinates need to be used in Map - * where we cannot adjust the default behavior of using a reference equality - * - * @param coordinate `OmnichainNodeCoordinate` - * - * @returns `string` - */ -export const serializeCoordinate = ({ address, eid }: OmnichainNodeCoordinate): string => `${eid}|${address}` - -/** - * Serializes coordinate vector. Useful for when coordinates need to be used in Map - * where we cannot adjust the default behavior of using a reference equality - * - * @param coordinate `OmnichainEdgeCoordinates` - * - * @returns `string` - */ -export const serializeCoordinates = ({ from, to }: OmnichainEdgeCoordinates): string => - `${serializeCoordinate(from)} → ${serializeCoordinate(to)}` diff --git a/packages/ua-utils/src/index.ts b/packages/ua-utils/src/index.ts index 3dec059935..de37f954b7 100644 --- a/packages/ua-utils/src/index.ts +++ b/packages/ua-utils/src/index.ts @@ -1,3 +1 @@ -export * from "./coordinates" -export * from "./schema" -export * from "./types" +export * from "./omnigraph" diff --git a/packages/ua-utils/src/omnigraph/coordinates.ts b/packages/ua-utils/src/omnigraph/coordinates.ts new file mode 100644 index 0000000000..639defec75 --- /dev/null +++ b/packages/ua-utils/src/omnigraph/coordinates.ts @@ -0,0 +1,42 @@ +import { OmniEdgeCoordinates, OmniNodeCoordinate } from "./types" + +/** + * Compares two coordinates by value + * + * @param a `OmniNodeCoordinate` + * @param b `OmniNodeCoordinate` + * + * @returns `true` if the coordinates point to the same point in omniverse + */ +export const isCoordinateEqual = (a: OmniNodeCoordinate, b: OmniNodeCoordinate): boolean => a.address === b.address && a.eid === b.eid + +/** + * Compares two coordinate vectors + * + * @param a `OmniEdgeCoordinates` + * @param b `OmniEdgeCoordinates` + * + * @returns `true` if the coordinates point from and to the same point in omniverse + */ +export const areCoordinatesEqual = (a: OmniEdgeCoordinates, b: OmniEdgeCoordinates): boolean => + isCoordinateEqual(a.from, b.from) && isCoordinateEqual(a.to, b.to) + +/** + * Serializes a coordinate. Useful for when coordinates need to be used in Map + * where we cannot adjust the default behavior of using a reference equality + * + * @param coordinate `OmniNodeCoordinate` + * + * @returns `string` + */ +export const serializeCoordinate = ({ address, eid }: OmniNodeCoordinate): string => `${eid}|${address}` + +/** + * Serializes coordinate vector. Useful for when coordinates need to be used in Map + * where we cannot adjust the default behavior of using a reference equality + * + * @param coordinate `OmniEdgeCoordinates` + * + * @returns `string` + */ +export const serializeCoordinates = ({ from, to }: OmniEdgeCoordinates): string => `${serializeCoordinate(from)} → ${serializeCoordinate(to)}` diff --git a/packages/ua-utils/src/omnigraph/index.ts b/packages/ua-utils/src/omnigraph/index.ts new file mode 100644 index 0000000000..3dec059935 --- /dev/null +++ b/packages/ua-utils/src/omnigraph/index.ts @@ -0,0 +1,3 @@ +export * from "./coordinates" +export * from "./schema" +export * from "./types" diff --git a/packages/ua-utils/src/omnigraph/schema.ts b/packages/ua-utils/src/omnigraph/schema.ts new file mode 100644 index 0000000000..36912e410f --- /dev/null +++ b/packages/ua-utils/src/omnigraph/schema.ts @@ -0,0 +1,47 @@ +import { EndpointId } from "@layerzerolabs/lz-definitions" +import { z } from "zod" +import type { OmniNodeCoordinate, OmniNode, OmniEdgeCoordinates, OmniEdge } from "./types" + +export const AddressSchema = z.string() + +export const EndpointIdSchema: z.ZodSchema = z.nativeEnum(EndpointId).pipe(z.number()) + +export const OmniNodeCoordinateSchema: z.ZodSchema = z.object({ + address: AddressSchema, + eid: EndpointIdSchema, +}) + +export const OmniEdgeCoordinatesSchema: z.ZodSchema = z.object({ + from: OmniNodeCoordinateSchema, + to: OmniNodeCoordinateSchema, +}) + +/** + * Factory for OmniNode schemas + * + * @param configSchema Schema of the config contained in the node + * + * @returns `z.ZodSchema>` schema for a node with the particular config type + */ +export const createOmniNodeSchema = ( + configSchema: z.ZodSchema +): z.ZodSchema, z.ZodTypeDef, unknown> => + z.object({ + coordinate: OmniNodeCoordinateSchema, + config: configSchema, + }) as z.ZodSchema, z.ZodTypeDef, unknown> + +/** + * Factory for OmniEdge schemas + * + * @param configSchema `z.ZodSchema` Schema of the config contained in the edge + * + * @returns `z.ZodSchema>` schema for an edge with the particular config type + */ +export const createOmniEdgeSchema = ( + configSchema: z.ZodSchema +): z.ZodSchema, z.ZodTypeDef, unknown> => + z.object({ + coordinates: OmniEdgeCoordinatesSchema, + config: configSchema, + }) as z.ZodSchema, z.ZodTypeDef, unknown> diff --git a/packages/ua-utils/src/omnigraph/types.ts b/packages/ua-utils/src/omnigraph/types.ts new file mode 100644 index 0000000000..7a1fe39f5b --- /dev/null +++ b/packages/ua-utils/src/omnigraph/types.ts @@ -0,0 +1,53 @@ +import type { EndpointId } from "@layerzerolabs/lz-definitions" + +export type Address = string + +/** + * OmniNodeCoordinate identifies a point in omniverse, an omnichain universe. + * + * In layman terms this is a contract deployed on a particular network (represented by an endpoint). + */ +export interface OmniNodeCoordinate { + eid: EndpointId + address: Address +} + +/** + * OmniEdgeCoordinates identify a line in omniverse, an omnichain universe. + * + * In layman terms this is a directional connection between two contracts + */ +export interface OmniEdgeCoordinates { + from: OmniNodeCoordinate + to: OmniNodeCoordinate +} + +/** + * OmniNode represents a point in omniverse + * with an additional piece of information attached + */ +export interface OmniNode { + coordinate: OmniNodeCoordinate + config: TConfig +} + +/** + * OmniEdge represents a connection between two points in omniverse + * with an additional piece of information attached + */ +export interface OmniEdge { + coordinates: OmniEdgeCoordinates + config: TConfig +} + +/** + * OmniGraph is a collection of nodes and edges of omniverse + * that together represent an omnichain app a.k.a. OApp. + * + * For purposes of readability and to avoid overabstraction on the user end, + * the names are set to be `contracts` rather than `nodes` and `connections` rather than `edges` + */ +export interface OmniGraph { + contracts: OmniNode[] + connections: OmniEdge[] +} diff --git a/packages/ua-utils/src/schema.ts b/packages/ua-utils/src/schema.ts deleted file mode 100644 index 40b568b076..0000000000 --- a/packages/ua-utils/src/schema.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { EndpointId } from "@layerzerolabs/lz-definitions" -import { z } from "zod" -import type { OmnichainNodeCoordinate, OmnichainNode, OmnichainEdgeCoordinates, OmnichainEdge } from "./types" - -export const AddressSchema = z.string() - -export const EndpointIdSchema: z.ZodSchema = z.nativeEnum(EndpointId).pipe(z.number()) - -export const OmnichainNodeCoordinateSchema: z.ZodSchema = z.object({ - address: AddressSchema, - eid: EndpointIdSchema, -}) - -export const OmnichainEdgeCoordinatesSchema: z.ZodSchema = z.object({ - from: OmnichainNodeCoordinateSchema, - to: OmnichainNodeCoordinateSchema, -}) - -/** - * Factory for OmnichainNode schemas - * - * @param configSchema Schema of the config contained in the node - * - * @returns `z.ZodSchema>` schema for a node with the particular config type - */ -export const createOmnichainNodeSchema = ( - configSchema: z.ZodSchema -): z.ZodSchema, z.ZodTypeDef, unknown> => - z.object({ - coordinate: OmnichainNodeCoordinateSchema, - config: configSchema, - }) as z.ZodSchema, z.ZodTypeDef, unknown> - -/** - * Factory for OmnichainEdge schemas - * - * @param configSchema `z.ZodSchema` Schema of the config contained in the edge - * - * @returns `z.ZodSchema>` schema for an edge with the particular config type - */ -export const createOmnichainEdgeSchema = ( - configSchema: z.ZodSchema -): z.ZodSchema, z.ZodTypeDef, unknown> => - z.object({ - coordinates: OmnichainEdgeCoordinatesSchema, - config: configSchema, - }) as z.ZodSchema, z.ZodTypeDef, unknown> diff --git a/packages/ua-utils/src/types.ts b/packages/ua-utils/src/types.ts deleted file mode 100644 index 1734a70ef1..0000000000 --- a/packages/ua-utils/src/types.ts +++ /dev/null @@ -1,53 +0,0 @@ -import type { EndpointId } from "@layerzerolabs/lz-definitions" - -export type Address = string - -/** - * OmnichainNodeCoordinate identifies a point in omniverse, an omnichain universe. - * - * In layman terms this is a contract deployed on a particular network (represented by an endpoint). - */ -export interface OmnichainNodeCoordinate { - eid: EndpointId - address: Address -} - -/** - * OmnichainEdgeCoordinates identify a line in omniverse, an omnichain universe. - * - * In layman terms this is a directional connection between two contracts - */ -export interface OmnichainEdgeCoordinates { - from: OmnichainNodeCoordinate - to: OmnichainNodeCoordinate -} - -/** - * OmnichainNode represents a point in omniverse - * with an additional piece of information attached - */ -export interface OmnichainNode { - coordinate: OmnichainNodeCoordinate - config: TConfig -} - -/** - * OmnichainEdge represents a connection between two points in omniverse - * with an additional piece of information attached - */ -export interface OmnichainEdge { - coordinates: OmnichainEdgeCoordinates - config: TConfig -} - -/** - * OmnichainGraph is a collection of nodes and edges of omniverse - * that together represent an omnichain app a.k.a. OApp. - * - * For purposes of readability and to avoid overabstraction on the user end, - * the names are set to be `contracts` rather than `nodes` and `connections` rather than `edges` - */ -export interface OmnichainGraph { - contracts: OmnichainNode[] - connections: OmnichainEdge[] -} diff --git a/packages/ua-utils/test/__utils__/arbitraries.ts b/packages/ua-utils/test/__utils__/arbitraries.ts index f04bf4b015..328c25d79b 100644 --- a/packages/ua-utils/test/__utils__/arbitraries.ts +++ b/packages/ua-utils/test/__utils__/arbitraries.ts @@ -1,18 +1,18 @@ import fc from "fast-check" import { EndpointId } from "@layerzerolabs/lz-definitions" import { ENDPOINT_IDS } from "./constants" -import { OmnichainNodeCoordinate, OmnichainEdgeCoordinates } from "@/types" +import { OmniNodeCoordinate, OmniEdgeCoordinates } from "@/omnigraph/types" export const addressArbitrary = fc.string() export const endpointArbitrary: fc.Arbitrary = fc.constantFrom(...ENDPOINT_IDS) -export const coordinateArbitrary: fc.Arbitrary = fc.record({ +export const coordinateArbitrary: fc.Arbitrary = fc.record({ eid: endpointArbitrary, address: addressArbitrary, }) -export const coordinatesArbitrary: fc.Arbitrary = fc.record({ +export const coordinatesArbitrary: fc.Arbitrary = fc.record({ from: coordinateArbitrary, to: coordinateArbitrary, }) diff --git a/packages/ua-utils/test/__utils__/constants.ts b/packages/ua-utils/test/__utils__/constants.ts index 8f45d26153..415310e290 100644 --- a/packages/ua-utils/test/__utils__/constants.ts +++ b/packages/ua-utils/test/__utils__/constants.ts @@ -1,4 +1,4 @@ import { EndpointId } from "@layerzerolabs/lz-definitions" -import { EndpointIdSchema } from "../../src/schema" +import { EndpointIdSchema } from "../../src/omnigraph/schema" export const ENDPOINT_IDS = Object.values(EndpointId).filter((value): value is EndpointId => EndpointIdSchema.safeParse(value).success) diff --git a/packages/ua-utils/test/coordinates.test.ts b/packages/ua-utils/test/omnigraph/coordinates.test.ts similarity index 96% rename from packages/ua-utils/test/coordinates.test.ts rename to packages/ua-utils/test/omnigraph/coordinates.test.ts index 6836ac11c3..761a05b69e 100644 --- a/packages/ua-utils/test/coordinates.test.ts +++ b/packages/ua-utils/test/omnigraph/coordinates.test.ts @@ -1,8 +1,8 @@ import fc from "fast-check" -import { areCoordinatesEqual, isCoordinateEqual, serializeCoordinate, serializeCoordinates } from "@/coordinates" -import { coordinateArbitrary, addressArbitrary, endpointArbitrary, coordinatesArbitrary } from "./__utils__/arbitraries" +import { areCoordinatesEqual, isCoordinateEqual, serializeCoordinate, serializeCoordinates } from "@/omnigraph/coordinates" +import { coordinateArbitrary, addressArbitrary, endpointArbitrary, coordinatesArbitrary } from "../__utils__/arbitraries" -describe("coordinates", () => { +describe("omnigraph/coordinates", () => { describe("assertions", () => { describe("isCoordinateEqual", () => { it("should be true for referentially equal coordinates", () => { diff --git a/packages/ua-utils/test/schema.test.ts b/packages/ua-utils/test/omnigraph/schema.test.ts similarity index 83% rename from packages/ua-utils/test/schema.test.ts rename to packages/ua-utils/test/omnigraph/schema.test.ts index 1bde6feec4..44bced5e3e 100644 --- a/packages/ua-utils/test/schema.test.ts +++ b/packages/ua-utils/test/omnigraph/schema.test.ts @@ -1,9 +1,9 @@ import fc from "fast-check" -import { createOmnichainEdgeSchema, createOmnichainNodeSchema } from "@/schema" +import { createOmniEdgeSchema, createOmniNodeSchema } from "@/omnigraph/schema" import { z } from "zod" -import { coordinateArbitrary, coordinatesArbitrary } from "./__utils__/arbitraries" +import { coordinateArbitrary, coordinatesArbitrary } from "../__utils__/arbitraries" -describe("schema", () => { +describe("omnigraph/schema", () => { interface TestCase { configSchema: z.ZodSchema good: fc.Arbitrary @@ -23,9 +23,9 @@ describe("schema", () => { }, ] - describe("createOmnichainNodeSchema", () => { + describe("createOmniNodeSchema", () => { describe.each(TEST_CASES)(`schema`, ({ configSchema, good, bad }) => { - const schema = createOmnichainNodeSchema(configSchema) + const schema = createOmniNodeSchema(configSchema) it("should parse successfully", () => { fc.assert( @@ -45,9 +45,9 @@ describe("schema", () => { }) }) - describe("createOmnichainEdgeSchema", () => { + describe("createOmniEdgeSchema", () => { describe.each(TEST_CASES)(`schema`, ({ configSchema, good, bad }) => { - const schema = createOmnichainEdgeSchema(configSchema) + const schema = createOmniEdgeSchema(configSchema) it("should parse successfully", () => { fc.assert(