Skip to content

Commit

Permalink
chore: Shorten names and move stuff under omnigraph directory
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Nov 23, 2023
1 parent 6ee0709 commit 692b7ae
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 160 deletions.
1 change: 1 addition & 0 deletions packages/ua-utils-evm-hardhat/src/utils/coordinates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getCoordinate = (contractName: string): OmniNodeCoordinate | undefined => {}
12 changes: 12 additions & 0 deletions packages/ua-utils-evm/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { OmniGraph } from "@layerzerolabs/ua-utils"
import { z } from "zod"

export type OAppContractConfig = z.TypeOf<typeof OAppContractConfigSchema>

export type OAppConnectionConfig = z.TypeOf<typeof OAppConnectionConfigSchema>

export type OAppOmnichainConfig = OmniGraph<OAppContractConfig, OAppConnectionConfig>

export const OAppContractConfigSchema = z.object({})

export const OAppConnectionConfigSchema = z.object({})
43 changes: 0 additions & 43 deletions packages/ua-utils/src/coordinates.ts

This file was deleted.

4 changes: 1 addition & 3 deletions packages/ua-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from "./coordinates"
export * from "./schema"
export * from "./types"
export * from "./omnigraph"
42 changes: 42 additions & 0 deletions packages/ua-utils/src/omnigraph/coordinates.ts
Original file line number Diff line number Diff line change
@@ -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)}`
3 changes: 3 additions & 0 deletions packages/ua-utils/src/omnigraph/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./coordinates"
export * from "./schema"
export * from "./types"
47 changes: 47 additions & 0 deletions packages/ua-utils/src/omnigraph/schema.ts
Original file line number Diff line number Diff line change
@@ -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<EndpointId, z.ZodTypeDef, unknown> = z.nativeEnum(EndpointId).pipe(z.number())

export const OmniNodeCoordinateSchema: z.ZodSchema<OmniNodeCoordinate, z.ZodTypeDef, unknown> = z.object({
address: AddressSchema,
eid: EndpointIdSchema,
})

export const OmniEdgeCoordinatesSchema: z.ZodSchema<OmniEdgeCoordinates, z.ZodTypeDef, unknown> = z.object({
from: OmniNodeCoordinateSchema,
to: OmniNodeCoordinateSchema,
})

/**
* Factory for OmniNode schemas
*
* @param configSchema Schema of the config contained in the node
*
* @returns `z.ZodSchema<OmniNode<TConfig>>` schema for a node with the particular config type
*/
export const createOmniNodeSchema = <TConfig = unknown>(
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef, unknown>
): z.ZodSchema<OmniNode<TConfig>, z.ZodTypeDef, unknown> =>
z.object({
coordinate: OmniNodeCoordinateSchema,
config: configSchema,
}) as z.ZodSchema<OmniNode<TConfig>, z.ZodTypeDef, unknown>

/**
* Factory for OmniEdge schemas
*
* @param configSchema `z.ZodSchema<TConfig>` Schema of the config contained in the edge
*
* @returns `z.ZodSchema<OmniEdge<TConfig>>` schema for an edge with the particular config type
*/
export const createOmniEdgeSchema = <TConfig = unknown>(
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef, unknown>
): z.ZodSchema<OmniEdge<TConfig>, z.ZodTypeDef, unknown> =>
z.object({
coordinates: OmniEdgeCoordinatesSchema,
config: configSchema,
}) as z.ZodSchema<OmniEdge<TConfig>, z.ZodTypeDef, unknown>
53 changes: 53 additions & 0 deletions packages/ua-utils/src/omnigraph/types.ts
Original file line number Diff line number Diff line change
@@ -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<TConfig = unknown> {
coordinate: OmniNodeCoordinate
config: TConfig
}

/**
* OmniEdge represents a connection between two points in omniverse
* with an additional piece of information attached
*/
export interface OmniEdge<TConfig = unknown> {
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<TNodeConfig = unknown, TEdgeConfig = unknown> {
contracts: OmniNode<TNodeConfig>[]
connections: OmniEdge<TEdgeConfig>[]
}
47 changes: 0 additions & 47 deletions packages/ua-utils/src/schema.ts

This file was deleted.

53 changes: 0 additions & 53 deletions packages/ua-utils/src/types.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/ua-utils/test/__utils__/arbitraries.ts
Original file line number Diff line number Diff line change
@@ -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<EndpointId> = fc.constantFrom(...ENDPOINT_IDS)

export const coordinateArbitrary: fc.Arbitrary<OmnichainNodeCoordinate> = fc.record({
export const coordinateArbitrary: fc.Arbitrary<OmniNodeCoordinate> = fc.record({
eid: endpointArbitrary,
address: addressArbitrary,
})

export const coordinatesArbitrary: fc.Arbitrary<OmnichainEdgeCoordinates> = fc.record({
export const coordinatesArbitrary: fc.Arbitrary<OmniEdgeCoordinates> = fc.record({
from: coordinateArbitrary,
to: coordinateArbitrary,
})
2 changes: 1 addition & 1 deletion packages/ua-utils/test/__utils__/constants.ts
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<unknown>
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 692b7ae

Please sign in to comment.