Skip to content

Commit

Permalink
chore: Improving config types
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Dec 8, 2023
1 parent e5dea9c commit d20068d
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 65 deletions.
2 changes: 2 additions & 0 deletions packages/protocol-utils-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"devDependencies": {
"@ethersproject/abstract-provider": "^5.7.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.7.0",
"@layerzerolabs/lz-definitions": "~1.5.69",
Expand All @@ -59,6 +60,7 @@
"peerDependencies": {
"@ethersproject/abstract-provider": "^5.7.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.7.0",
"@layerzerolabs/lz-definitions": "~1.5.69",
Expand Down
1 change: 1 addition & 0 deletions packages/protocol-utils-evm/src/uln302/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './sdk'
export * from './types'
8 changes: 5 additions & 3 deletions packages/protocol-utils-evm/src/uln302/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { z } from 'zod'
import { AddressSchema } from '@layerzerolabs/utils'
import { BigNumberishNumberSchema } from '@layerzerolabs/utils-evm'
import { BigNumberishBigintSchema } from '@layerzerolabs/utils-evm'
import { z } from 'zod'
import type { Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-utils'
import type { Uln302ExecutorConfigInput, Uln302UlnConfigInput } from './types'

/**
* Schema for parsing an ethers-specific UlnConfig into a common format
Expand All @@ -11,15 +13,15 @@ export const Uln302UlnConfigSchema = z.object({
requiredDVNs: z.array(AddressSchema),
optionalDVNs: z.array(AddressSchema),
optionalDVNThreshold: z.coerce.number().int().nonnegative(),
})
}) satisfies z.ZodSchema<Uln302UlnConfig, z.ZodTypeDef, Uln302UlnConfigInput>

/**
* Schema for parsing an ethers-specific ExecutorConfig into a common format
*/
export const Uln302ExecutorConfigSchema = z.object({
maxMessageSize: BigNumberishNumberSchema,
executor: AddressSchema,
})
}) satisfies z.ZodSchema<Uln302ExecutorConfig, z.ZodTypeDef, Uln302ExecutorConfigInput>

/**
* Schema for parsing a common UlnConfig into a ethers-specific format
Expand Down
10 changes: 10 additions & 0 deletions packages/protocol-utils-evm/src/uln302/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { BigNumberish } from '@ethersproject/bignumber'
import { Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-utils'

export interface Uln302UlnConfigInput extends Omit<Uln302UlnConfig, 'confirmations'> {
confirmations: BigNumberish
}

export interface Uln302ExecutorConfigInput extends Omit<Uln302ExecutorConfig, 'maxMessageSize'> {
maxMessageSize: BigNumberish
}
2 changes: 0 additions & 2 deletions packages/ua-utils-evm-hardhat-test/test/__utils__/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ export const setupDefaultEndpoint = async (): Promise<void> => {
contracts: [
{
contract: ethEndpoint,
config: undefined,
},
{
contract: avaxEndpoint,
config: undefined,
},
],
connections: [
Expand Down
4 changes: 0 additions & 4 deletions packages/ua-utils-evm-hardhat-test/test/oapp/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,19 @@ describe('oapp/config', () => {
contracts: [
{
contract: ethContract,
config: undefined,
},
{
contract: avaxContract,
config: undefined,
},
],
connections: [
{
from: ethContract,
to: avaxContract,
config: undefined,
},
{
from: avaxContract,
to: ethContract,
config: undefined,
},
],
}
Expand Down
1 change: 1 addition & 0 deletions packages/ua-utils-evm-hardhat/src/oapp/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './schema'
export * from './types'
18 changes: 18 additions & 0 deletions packages/ua-utils-evm-hardhat/src/oapp/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
createOmniEdgeHardhatSchema,
createOmniGraphHardhatSchema,
createOmniNodeHardhatSchema,
} from '@layerzerolabs/utils-evm-hardhat'
import { z } from 'zod'
import type { OAppOmniGraphHardhat } from './types'

/**
* Validation schema for OApp configs in hardhat environment.
*
* Produces an `OAppOmniGraphHardhat` after successful parsing
* the user input.
*/
export const OAppOmniGraphHardhatSchema = createOmniGraphHardhatSchema(
createOmniNodeHardhatSchema(z.unknown()),
createOmniEdgeHardhatSchema(z.unknown())
) satisfies z.ZodSchema<OAppOmniGraphHardhat>
16 changes: 6 additions & 10 deletions packages/utils-evm-hardhat/src/omnigraph/coordinates.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import type { OmniPoint } from '@layerzerolabs/utils'
import type { Deployment } from 'hardhat-deploy/dist/types'
import type { HardhatRuntimeEnvironment } from 'hardhat/types'
import pMemoize from 'p-memoize'
import { OmniContract } from '@layerzerolabs/utils-evm'
import { Contract } from '@ethersproject/contracts'
import assert from 'assert'
import { OmniContractFactoryHardhat } from './types'
import { OmniContractFactoryHardhat, OmniDeployment } from './types'
import { createNetworkEnvironmentFactory, getDefaultRuntimeEnvironment } from '@/runtime'
import { assertHardhatDeploy } from '@/internal/assertions'

export interface OmniDeployment {
eid: EndpointId
deployment: Deployment
}

export const omniDeploymentToPoint = ({ eid, deployment }): OmniPoint => ({ eid, address: deployment.address })
export const omniDeploymentToPoint = ({ eid, deployment }: OmniDeployment): OmniPoint => ({
eid,
address: deployment.address,
})

export const omniDeploymentToContract = ({ eid, deployment }): OmniContract => ({
export const omniDeploymentToContract = ({ eid, deployment }: OmniDeployment): OmniContract => ({
eid,
contract: new Contract(deployment.address, deployment.abi),
})
Expand Down
18 changes: 9 additions & 9 deletions packages/utils-evm-hardhat/src/omnigraph/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ const OmniPointOrOmniPointHardhatSchema = z.union([OmniPointHardhatSchema, OmniP
* @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> =>
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef>
): z.ZodSchema<OmniNodeHardhat<TConfig>, z.ZodTypeDef> =>
z.object({
contract: OmniPointOrOmniPointHardhatSchema,
config: configSchema,
}) as z.ZodSchema<OmniNodeHardhat<TConfig>, z.ZodTypeDef, unknown>
}) as z.ZodSchema<OmniNodeHardhat<TConfig>, z.ZodTypeDef>

/**
* Factory for OmniEdgeHardhat schemas
Expand All @@ -33,13 +33,13 @@ export const createOmniNodeHardhatSchema = <TConfig = unknown>(
* @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> =>
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef>
): z.ZodSchema<OmniEdgeHardhat<TConfig>, z.ZodTypeDef> =>
z.object({
from: OmniPointOrOmniPointHardhatSchema,
to: OmniPointOrOmniPointHardhatSchema,
config: configSchema,
}) as z.ZodSchema<OmniEdgeHardhat<TConfig>, z.ZodTypeDef, unknown>
}) as z.ZodSchema<OmniEdgeHardhat<TConfig>, z.ZodTypeDef>

/**
* Factory for OmniGraphHardhat schemas
Expand All @@ -50,9 +50,9 @@ export const createOmniEdgeHardhatSchema = <TConfig = unknown>(
* @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> =>
nodeSchema: z.ZodSchema<OmniNodeHardhat<TNodeConfig>, z.ZodTypeDef>,
edgeSchema: z.ZodSchema<OmniEdgeHardhat<TEdgeConfig>, z.ZodTypeDef>
): z.ZodSchema<OmniGraphHardhat<TNodeConfig, TEdgeConfig>, z.ZodTypeDef> =>
z.object({
contracts: z.array(nodeSchema),
connections: z.array(edgeSchema),
Expand Down
13 changes: 5 additions & 8 deletions packages/utils-evm-hardhat/src/omnigraph/transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,31 @@ import type { OmniContractFactoryHardhat, OmniEdgeHardhat, OmniGraphHardhat, Omn

export const createOmniNodeHardhatTransformer =
(contractFactory: OmniContractFactoryHardhat = createContractFactory()) =>
async <TNodeConfig = unknown>({
contract,
config,
}: OmniNodeHardhat<TNodeConfig>): Promise<OmniNode<TNodeConfig>> => {
async <TNodeConfig>({ contract, config }: OmniNodeHardhat<TNodeConfig>): Promise<OmniNode<TNodeConfig>> => {
const point = isOmniPoint(contract) ? contract : omniContractToPoint(await contractFactory(contract))

return { point, config }
return { point, config: config as TNodeConfig }
}

export const createOmniEdgeHardhatTransformer =
(contractFactory: OmniContractFactoryHardhat = createContractFactory()) =>
async <TEdgeConfig = unknown>({
async <TEdgeConfig>({
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 }
return { vector: { from, to }, config: config as TEdgeConfig }
}

export type OmniGraphHardhatTransformer<TNodeConfig = unknown, TEdgeConfig = unknown> = (
graph: OmniGraphHardhat<TNodeConfig, TEdgeConfig>
) => Promise<OmniGraph<TNodeConfig, TEdgeConfig>>

export const createOmniGraphHardhatTransformer =
<TNodeConfig = unknown, TEdgeConfig = unknown>(
<TNodeConfig, TEdgeConfig>(
nodeTransformer = createOmniNodeHardhatTransformer(),
edgeTransformer = createOmniEdgeHardhatTransformer()
): OmniGraphHardhatTransformer<TNodeConfig, TEdgeConfig> =>
Expand Down
43 changes: 34 additions & 9 deletions packages/utils-evm-hardhat/src/omnigraph/types.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import type { OmniPoint } from '@layerzerolabs/utils'
import type { OmniPoint, WithEid, WithOptionals } from '@layerzerolabs/utils'
import type { OmniContract } from '@layerzerolabs/utils-evm'
import type { Deployment } from 'hardhat-deploy/dist/types'

export interface OmniPointHardhat {
eid: EndpointId
/**
* Omniverse wrapper around a hardhat-deploy deployment
*/
export type OmniDeployment = WithEid<{
deployment: Deployment
}>

/**
* Hardhat-specific variation of an `OmniPoint`
*
* Since in hardhat we have access to artifacts/deployments,
* we can use contract name to find an address or ABIs of a particular contract
* and transform `OmniPointHardhat` to `OmniPoint`
*/
export type OmniPointHardhat = WithEid<{
contractName?: string | null
address?: string | null
}
}>

export interface OmniNodeHardhat<TNodeConfig> {
/**
* Hardhat-specific variation of `OmniNode` that uses `OmniPointHardhat`
* instead of `OmniPoint` to specify the contract coordinates
*/
export type OmniNodeHardhat<TNodeConfig> = WithOptionals<{
contract: OmniPointHardhat | OmniPoint
config: TNodeConfig
}
}>

export interface OmniEdgeHardhat<TEdgeConfig> {
/**
* Hardhat-specific variation of `OmniEdge` that uses `OmniPointHardhat`
* instead of `OmniPoint` to specify the contracts' coordinates
*/
export type OmniEdgeHardhat<TEdgeConfig> = WithOptionals<{
from: OmniPointHardhat | OmniPoint
to: OmniPointHardhat | OmniPoint
config: TEdgeConfig
}
}>

/**
* Hardhat-specific variation of `OmniGraph` that uses `OmniNodeHardhat`
* and `OmniEdgeHardhat` to specify the contracts and connections
*/
export interface OmniGraphHardhat<TNodeConfig = unknown, TEdgeConfig = unknown> {
contracts: OmniNodeHardhat<TNodeConfig>[]
connections: OmniEdgeHardhat<TEdgeConfig>[]
Expand Down
8 changes: 3 additions & 5 deletions packages/utils-evm/src/omnigraph/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { Contract } from '@ethersproject/contracts'
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import type { OmniPoint } from '@layerzerolabs/utils'
import type { OmniPoint, WithEid } from '@layerzerolabs/utils'

export interface OmniContract<TContract extends Contract = Contract> {
eid: EndpointId
export type OmniContract<TContract extends Contract = Contract> = WithEid<{
contract: TContract
}
}>

export type OmniContractFactory = (point: OmniPoint) => OmniContract | Promise<OmniContract>
24 changes: 17 additions & 7 deletions packages/utils/src/omnigraph/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { OmniPoint, OmniNode, OmniVector, OmniEdge, OmniGraph } from './typ

export const AddressSchema = z.string()

export const EndpointIdSchema: z.ZodSchema<EndpointId, z.ZodTypeDef, unknown> = z
export const EndpointIdSchema: z.ZodSchema<EndpointId, z.ZodTypeDef, string | number> = z
.nativeEnum(EndpointId)
.pipe(z.number())

Expand All @@ -20,12 +20,12 @@ export const OmniVectorSchema: z.ZodSchema<OmniVector, z.ZodTypeDef, unknown> =

export const EmptyOmniNodeSchema = z.object({
point: OmniPointSchema,
config: z.unknown(),
config: z.unknown().optional(),
})

export const EmptyOmniEdgeSchema = z.object({
vector: OmniVectorSchema,
config: z.unknown(),
config: z.unknown().optional(),
})

/**
Expand All @@ -36,6 +36,16 @@ export const EmptyOmniEdgeSchema = z.object({
*/
export const isOmniPoint = (value: unknown): value is OmniPoint => OmniPointSchema.safeParse(value).success

/**
* Helper assertion utility that checks whether an `OmniGraph`
* has at least any contracts or connections defined
*
* @param {OmniGraph} graph
* @returns {boolean}
*/
export const isOmniGraphEmpty = ({ contracts, connections }: OmniGraph): boolean =>
contracts.length === 0 && connections.length === 0

/**
* Factory for OmniNode schemas
*
Expand All @@ -45,10 +55,10 @@ export const isOmniPoint = (value: unknown): value is OmniPoint => OmniPointSche
*/
export const createOmniNodeSchema = <TConfig = unknown>(
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef, unknown>
): z.ZodSchema<OmniNode<TConfig>, z.ZodTypeDef, unknown> =>
): z.ZodSchema<OmniNode<TConfig>, z.ZodTypeDef> =>
EmptyOmniNodeSchema.extend({
config: configSchema,
}) as z.ZodSchema<OmniNode<TConfig>, z.ZodTypeDef, unknown>
}) as z.ZodSchema<OmniNode<TConfig>, z.ZodTypeDef>

/**
* Factory for OmniEdge schemas
Expand All @@ -59,10 +69,10 @@ export const createOmniNodeSchema = <TConfig = unknown>(
*/
export const createOmniEdgeSchema = <TConfig = unknown>(
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef, unknown>
): z.ZodSchema<OmniEdge<TConfig>, z.ZodTypeDef, unknown> =>
): z.ZodSchema<OmniEdge<TConfig>, z.ZodTypeDef> =>
EmptyOmniEdgeSchema.extend({
config: configSchema,
}) as z.ZodSchema<OmniEdge<TConfig>, z.ZodTypeDef, unknown>
}) as z.ZodSchema<OmniEdge<TConfig>, z.ZodTypeDef>

/**
* Factory for OmniGraph schemas
Expand Down
Loading

0 comments on commit d20068d

Please sign in to comment.