Skip to content

Commit

Permalink
chore: Moving on with OApp wire
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Dec 8, 2023
1 parent cd4e55a commit 114cde0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export default [];
const { EndpointId } = require('@layerzerolabs/lz-definitions');

module.exports = {
contracts: [
{
eid: EndpointId.EON_MAINNET,
contractName: 'DefaultOApp',
},
{
eid: 'Invalid EndpointId',
contractName: 'DefaultOApp',
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`task/oapp/wire should fail with a malformed JS file (001) 1`] = `
[Error: Config from file '/app/packages/ua-utils-evm-hardhat-test/test/task/oapp/__data__/configs/invalid.config.001.js' is malformed. Please fix the following errors:
contracts:
- Invalid input
- Invalid input,
connections:
- Required]
`;

exports[`task/oapp/wire should fail with an empty JS file 1`] = `
[Error: Config from file '/app/packages/ua-utils-evm-hardhat-test/test/task/oapp/__data__/configs/empty.config.js' is malformed. Please fix the following errors:
contracts:
- Required,
connections:
- Required]
`;
13 changes: 10 additions & 3 deletions packages/ua-utils-evm-hardhat-test/test/task/oapp/wire.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ describe('task/oapp/wire', () => {
await expect(hre.run(TASK_LZ_WIRE_OAPP, { oappConfig: readme })).rejects.toThrow(/Unable to read config file/)
})

// TODO This should fail once the task is complete
it('should succeed with an empty JS file', async () => {
it('should fail with an empty JS file', async () => {
const oappConfig = resolve(CONFIGS_BASE_DIR, 'empty.config.js')

expect(isFile(oappConfig)).toBeTruthy()

await hre.run(TASK_LZ_WIRE_OAPP, { oappConfig })
await expect(hre.run(TASK_LZ_WIRE_OAPP, { oappConfig })).rejects.toMatchSnapshot()
})

it('should fail with a malformed JS file (001)', async () => {
const oappConfig = resolve(CONFIGS_BASE_DIR, 'invalid.config.001.js')

expect(isFile(oappConfig)).toBeTruthy()

await expect(hre.run(TASK_LZ_WIRE_OAPP, { oappConfig })).rejects.toMatchSnapshot()
})
})
29 changes: 27 additions & 2 deletions packages/ua-utils-evm-hardhat/src/tasks/oapp/wire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { task, types } from 'hardhat/config'
import type { ActionType } from 'hardhat/types'
import { TASK_LZ_WIRE_OAPP } from '@/constants/tasks'
import { isFile, isReadable } from '@layerzerolabs/io-utils'
import { OAppOmniGraphHardhat, OAppOmniGraphHardhatSchema } from '@/oapp'

interface TaskArgs {
oappConfig: string
Expand All @@ -17,13 +18,37 @@ const action: ActionType<TaskArgs> = async ({ oappConfig: oappConfigPath }) => {
}

// Now let's see if we can load the config file
let config: unknown
let rawConfig: unknown
try {
config = require(oappConfigPath)
rawConfig = require(oappConfigPath)
} catch (error) {
throw new Error(`Unable to read config file '${oappConfigPath}': ${error}`)
}

// It's time to make sure that the config is not malformed
//
// At this stage we are only interested in the shape of the data,
// we are not checking whether the information makes sense (e.g.
// whether there are no missing nodes etc)
const configParseResult = OAppOmniGraphHardhatSchema.safeParse(rawConfig)
if (configParseResult.success === false) {
// FIXME Error formatting
const errors = configParseResult.error.flatten(
(issue) => `Property '${issue.path.join('.') ?? '[root]'}': ${issue.message}`
)
const formErrors = errors.formErrors.map((error) => `- ${error}`).join(`\n`)
const fieldErrors = Object.entries(errors.fieldErrors).map(
([field, errors]) => `\n${field}:\n${errors.map((error) => `- ${error}`).join(`\n`)}`
)
const allErrors = [...formErrors, fieldErrors]

throw new Error(
`Config from file '${oappConfigPath}' is malformed. Please fix the following errors:\n\n${allErrors}`
)
}

// At this point we have a correctly typed config
const config: OAppOmniGraphHardhat = configParseResult.data
console.log({ config })
}
task(TASK_LZ_WIRE_OAPP, 'Wire LayerZero OApp')
Expand Down

0 comments on commit 114cde0

Please sign in to comment.