From 0c55124e7e3a46a37cf6a9453201c117baf397e3 Mon Sep 17 00:00:00 2001 From: Jan Nanista Date: Fri, 8 Dec 2023 12:06:28 -0800 Subject: [PATCH] chore: Moving on with OApp wire --- .../__data__/configs/invalid.config.001.js | 15 +++++++++- .../task/oapp/__snapshots__/wire.test.ts.snap | 22 ++++++++++++++ .../test/task/oapp/wire.test.ts | 13 +++++++-- .../src/tasks/oapp/wire.ts | 29 +++++++++++++++++-- 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 packages/ua-utils-evm-hardhat-test/test/task/oapp/__snapshots__/wire.test.ts.snap diff --git a/packages/ua-utils-evm-hardhat-test/test/task/oapp/__data__/configs/invalid.config.001.js b/packages/ua-utils-evm-hardhat-test/test/task/oapp/__data__/configs/invalid.config.001.js index d6d1738de..56f925b50 100644 --- a/packages/ua-utils-evm-hardhat-test/test/task/oapp/__data__/configs/invalid.config.001.js +++ b/packages/ua-utils-evm-hardhat-test/test/task/oapp/__data__/configs/invalid.config.001.js @@ -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', + }, + ], +}; diff --git a/packages/ua-utils-evm-hardhat-test/test/task/oapp/__snapshots__/wire.test.ts.snap b/packages/ua-utils-evm-hardhat-test/test/task/oapp/__snapshots__/wire.test.ts.snap new file mode 100644 index 000000000..b5f010fc7 --- /dev/null +++ b/packages/ua-utils-evm-hardhat-test/test/task/oapp/__snapshots__/wire.test.ts.snap @@ -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] +`; diff --git a/packages/ua-utils-evm-hardhat-test/test/task/oapp/wire.test.ts b/packages/ua-utils-evm-hardhat-test/test/task/oapp/wire.test.ts index 41af6e7ff..9d93990ac 100644 --- a/packages/ua-utils-evm-hardhat-test/test/task/oapp/wire.test.ts +++ b/packages/ua-utils-evm-hardhat-test/test/task/oapp/wire.test.ts @@ -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() }) }) diff --git a/packages/ua-utils-evm-hardhat/src/tasks/oapp/wire.ts b/packages/ua-utils-evm-hardhat/src/tasks/oapp/wire.ts index 6db752be9..c24e9791d 100644 --- a/packages/ua-utils-evm-hardhat/src/tasks/oapp/wire.ts +++ b/packages/ua-utils-evm-hardhat/src/tasks/oapp/wire.ts @@ -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 @@ -17,13 +18,37 @@ const action: ActionType = 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')