-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🪚 OmniGraph™ Transformations & schemas (#76)
- Loading branch information
1 parent
e95222a
commit a665df2
Showing
13 changed files
with
374 additions
and
37 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
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
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
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 @@ | ||
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,3 @@ | ||
import { OmniGraphHardhat } from '@layerzerolabs/utils-evm-hardhat' | ||
|
||
export type OAppOmniGraphHardhat = OmniGraphHardhat<unknown, 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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from './builder' | ||
export * from './contracts' | ||
export * from './coordinates' | ||
export * from './schema' | ||
export * from './transformations' | ||
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,59 @@ | ||
import { z } from 'zod' | ||
import { EndpointIdSchema, OmniPointSchema } from '@layerzerolabs/utils' | ||
import type { OmniEdgeHardhat, OmniGraphHardhat, OmniNodeHardhat, OmniPointHardhat } from './types' | ||
|
||
export const OmniPointHardhatSchema: z.ZodSchema<OmniPointHardhat, z.ZodTypeDef, unknown> = z.object({ | ||
eid: EndpointIdSchema, | ||
contractName: z.string().nullish(), | ||
address: z.string().nullish(), | ||
}) | ||
|
||
const OmniPointOrOmniPointHardhatSchema = z.union([OmniPointHardhatSchema, OmniPointSchema]) | ||
|
||
/** | ||
* Factory for OmniNodeHardhat schemas | ||
* | ||
* @param configSchema Schema of the config contained in the node | ||
* | ||
* @returns {z.ZodSchema<OmniNodeHardhat<TConfig>>} schema for a node with the particular config type | ||
*/ | ||
export const createOmniNodeHardhatSchema = <TConfig = unknown>( | ||
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef, unknown> | ||
): z.ZodSchema<OmniNodeHardhat<TConfig>, z.ZodTypeDef, unknown> => | ||
z.object({ | ||
contract: OmniPointOrOmniPointHardhatSchema, | ||
config: configSchema, | ||
}) as z.ZodSchema<OmniNodeHardhat<TConfig>, z.ZodTypeDef, unknown> | ||
|
||
/** | ||
* Factory for OmniEdgeHardhat schemas | ||
* | ||
* @param {z.ZodSchema<TConfig>} configSchema Schema of the config contained in the edge | ||
* | ||
* @returns {z.ZodSchema<OmniEdgeHardhat<TConfig>>} Schema for an edge with the particular config type | ||
*/ | ||
export const createOmniEdgeHardhatSchema = <TConfig = unknown>( | ||
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef, unknown> | ||
): z.ZodSchema<OmniEdgeHardhat<TConfig>, z.ZodTypeDef, unknown> => | ||
z.object({ | ||
from: OmniPointOrOmniPointHardhatSchema, | ||
to: OmniPointOrOmniPointHardhatSchema, | ||
config: configSchema, | ||
}) as z.ZodSchema<OmniEdgeHardhat<TConfig>, z.ZodTypeDef, unknown> | ||
|
||
/** | ||
* Factory for OmniGraphHardhat schemas | ||
* | ||
* @param {z.ZodSchema<OmniNodeHardhat<TNodeConfig>>} nodeSchema | ||
* @param {z.ZodSchema<OmniEdgeHardhat<TEdgeConfig>>} edgeSchema | ||
* | ||
* @returns {z.ZodSchema<OmniGraphHardhat<TNodeConfig, TEdgeConfig>>} | ||
*/ | ||
export const createOmniGraphHardhatSchema = <TNodeConfig = unknown, TEdgeConfig = unknown>( | ||
nodeSchema: z.ZodSchema<OmniNodeHardhat<TNodeConfig>, z.ZodTypeDef, unknown>, | ||
edgeSchema: z.ZodSchema<OmniEdgeHardhat<TEdgeConfig>, z.ZodTypeDef, unknown> | ||
): z.ZodSchema<OmniGraphHardhat<TNodeConfig, TEdgeConfig>, z.ZodTypeDef, unknown> => | ||
z.object({ | ||
contracts: z.array(nodeSchema), | ||
connections: z.array(edgeSchema), | ||
}) |
43 changes: 43 additions & 0 deletions
43
packages/utils-evm-hardhat/src/omnigraph/transformations.ts
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,43 @@ | ||
import type { OmniEdge, OmniGraph, OmniNode } from '@layerzerolabs/utils' | ||
import { isOmniPoint } from '@layerzerolabs/utils' | ||
import { omniContractToPoint } from '@layerzerolabs/utils-evm' | ||
import { createContractFactory } from './coordinates' | ||
import type { OmniContractFactoryHardhat, OmniEdgeHardhat, OmniGraphHardhat, OmniNodeHardhat } from './types' | ||
|
||
export const createOmniNodeHardhatTransformer = | ||
(contractFactory: OmniContractFactoryHardhat = createContractFactory()) => | ||
async <TNodeConfig = unknown>({ | ||
contract, | ||
config, | ||
}: OmniNodeHardhat<TNodeConfig>): Promise<OmniNode<TNodeConfig>> => { | ||
const point = isOmniPoint(contract) ? contract : omniContractToPoint(await contractFactory(contract)) | ||
|
||
return { point, config } | ||
} | ||
|
||
export const createOmniEdgeHardhatTransformer = | ||
(contractFactory: OmniContractFactoryHardhat = createContractFactory()) => | ||
async <TEdgeConfig = unknown>({ | ||
from: fromContract, | ||
to: toContract, | ||
config, | ||
}: OmniEdgeHardhat<TEdgeConfig>): Promise<OmniEdge<TEdgeConfig>> => { | ||
const from = isOmniPoint(fromContract) ? fromContract : omniContractToPoint(await contractFactory(fromContract)) | ||
const to = isOmniPoint(toContract) ? toContract : omniContractToPoint(await contractFactory(toContract)) | ||
|
||
return { vector: { from, to }, config } | ||
} | ||
|
||
export type OmniGraphHardhatTransformer<TNodeConfig = unknown, TEdgeConfig = unknown> = ( | ||
graph: OmniGraphHardhat<TNodeConfig, TEdgeConfig> | ||
) => Promise<OmniGraph<TNodeConfig, TEdgeConfig>> | ||
|
||
export const createOmniGraphHardhatTransformer = | ||
<TNodeConfig = unknown, TEdgeConfig = unknown>( | ||
nodeTransformer = createOmniNodeHardhatTransformer(), | ||
edgeTransformer = createOmniEdgeHardhatTransformer() | ||
): OmniGraphHardhatTransformer<TNodeConfig, TEdgeConfig> => | ||
async (graph) => ({ | ||
contracts: await Promise.all(graph.contracts.map(nodeTransformer)), | ||
connections: await Promise.all(graph.connections.map(edgeTransformer)), | ||
}) |
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
Oops, something went wrong.