-
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.
Merge branch 'main' into getOAppConfig
- Loading branch information
Showing
10 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/ua-utils-evm-hardhat-test/test/task/oapp/__data__/configs/invalid.config.001.js
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,14 @@ | ||
const { EndpointId } = require('@layerzerolabs/lz-definitions'); | ||
|
||
module.exports = { | ||
contracts: [ | ||
{ | ||
eid: EndpointId.EON_MAINNET, | ||
contractName: 'DefaultOApp', | ||
}, | ||
{ | ||
eid: 'Invalid EndpointId', | ||
contractName: 'DefaultOApp', | ||
}, | ||
], | ||
}; |
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions
4
packages/ua-utils-evm-hardhat-test/test/task/oapp/__data__/configs/valid.config.empty.js
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,4 @@ | ||
module.exports = { | ||
contracts: [], | ||
connections: [], | ||
}; |
30 changes: 30 additions & 0 deletions
30
packages/ua-utils-evm-hardhat-test/test/task/oapp/__snapshots__/wire.test.ts.snap
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,30 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`task/oapp/wire with invalid configs should fail if the config file does not exist 1`] = `[Error: Unable to read config file './does-not-exist.js'. Check that the file exists and is readable to your terminal user]`; | ||
|
||
exports[`task/oapp/wire with invalid configs should fail if the config file is not a file 1`] = `[Error: Unable to read config file '/app/packages/ua-utils-evm-hardhat-test/test/task/oapp'. Check that the file exists and is readable to your terminal user]`; | ||
|
||
exports[`task/oapp/wire with invalid configs should fail if the config file is not a valid JSON or JS file 1`] = `[Error: Unable to read config file '/app/packages/ua-utils-evm-hardhat-test/README.md': SyntaxError: Unexpected token '<']`; | ||
|
||
exports[`task/oapp/wire with invalid configs 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: | ||
- Property 'contracts.0.contract': Invalid input | ||
- Property 'contracts.1.contract': Invalid input, | ||
connections: | ||
- Property 'connections': Required] | ||
`; | ||
|
||
exports[`task/oapp/wire with invalid configs should fail with an empty JS file 1`] = ` | ||
[Error: Config from file '/app/packages/ua-utils-evm-hardhat-test/test/task/oapp/__data__/configs/invalid.config.empty.js' is malformed. Please fix the following errors: | ||
contracts: | ||
- Property 'contracts': Required, | ||
connections: | ||
- Property 'connections': Required] | ||
`; | ||
|
||
exports[`task/oapp/wire with invalid configs should fail with an empty JSON file 1`] = `[Error: Unable to read config file '/app/packages/ua-utils-evm-hardhat-test/test/task/oapp/__data__/configs/invalid.config.empty.json': SyntaxError: Unexpected end of JSON input]`; |
101 changes: 101 additions & 0 deletions
101
packages/ua-utils-evm-hardhat-test/test/task/oapp/wire.test.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,101 @@ | ||
import { setupDefaultEndpoint } from '../../__utils__/endpoint' | ||
import hre from 'hardhat' | ||
import { isFile, promptToContinue } from '@layerzerolabs/io-utils' | ||
import { resolve } from 'path' | ||
import { TASK_LZ_WIRE_OAPP } from '@layerzerolabs/ua-utils-evm-hardhat' | ||
|
||
jest.mock('@layerzerolabs/io-utils', () => { | ||
const original = jest.requireActual('@layerzerolabs/io-utils') | ||
|
||
return { | ||
...original, | ||
promptToContinue: jest.fn().mockRejectedValue('Not mocked'), | ||
} | ||
}) | ||
|
||
const promptToContinueMock = promptToContinue as jest.Mock | ||
|
||
describe('task/oapp/wire', () => { | ||
const CONFIGS_BASE_DIR = resolve(__dirname, '__data__', 'configs') | ||
const configPathFixture = (fileName: string): string => { | ||
const path = resolve(CONFIGS_BASE_DIR, fileName) | ||
|
||
expect(isFile(path)).toBeTruthy() | ||
|
||
return path | ||
} | ||
|
||
beforeEach(async () => { | ||
promptToContinueMock.mockReset() | ||
|
||
await setupDefaultEndpoint() | ||
}) | ||
|
||
describe('with invalid configs', () => { | ||
it('should fail if the config file does not exist', async () => { | ||
await expect(hre.run(TASK_LZ_WIRE_OAPP, { oappConfig: './does-not-exist.js' })).rejects.toMatchSnapshot() | ||
}) | ||
|
||
it('should fail if the config file is not a file', async () => { | ||
await expect(hre.run(TASK_LZ_WIRE_OAPP, { oappConfig: __dirname })).rejects.toMatchSnapshot() | ||
}) | ||
|
||
it('should fail if the config file is not a valid JSON or JS file', async () => { | ||
const readme = resolve(__dirname, '..', '..', '..', 'README.md') | ||
|
||
expect(isFile(readme)).toBeTruthy() | ||
|
||
await expect(hre.run(TASK_LZ_WIRE_OAPP, { oappConfig: readme })).rejects.toMatchSnapshot() | ||
}) | ||
|
||
it('should fail with an empty JSON file', async () => { | ||
const oappConfig = configPathFixture('invalid.config.empty.json') | ||
|
||
await expect(hre.run(TASK_LZ_WIRE_OAPP, { oappConfig })).rejects.toMatchSnapshot() | ||
}) | ||
|
||
it('should fail with an empty JS file', async () => { | ||
const oappConfig = configPathFixture('invalid.config.empty.js') | ||
|
||
await expect(hre.run(TASK_LZ_WIRE_OAPP, { oappConfig })).rejects.toMatchSnapshot() | ||
}) | ||
|
||
it('should fail with a malformed JS file (001)', async () => { | ||
const oappConfig = configPathFixture('invalid.config.001.js') | ||
|
||
await expect(hre.run(TASK_LZ_WIRE_OAPP, { oappConfig })).rejects.toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
describe('with valid configs', () => { | ||
it('should ask the user whether they want to continue', async () => { | ||
const oappConfig = configPathFixture('valid.config.empty.js') | ||
|
||
promptToContinueMock.mockResolvedValue(true) | ||
|
||
await hre.run(TASK_LZ_WIRE_OAPP, { oappConfig }) | ||
|
||
expect(promptToContinueMock).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should return undefined if the user decides not to continue', async () => { | ||
const oappConfig = configPathFixture('valid.config.empty.js') | ||
|
||
promptToContinueMock.mockResolvedValue(false) | ||
|
||
const result = await hre.run(TASK_LZ_WIRE_OAPP, { oappConfig }) | ||
|
||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should return a list of transactions if the user decides to continue', async () => { | ||
const oappConfig = configPathFixture('valid.config.empty.js') | ||
|
||
promptToContinueMock.mockResolvedValue(true) | ||
|
||
const result = await hre.run(TASK_LZ_WIRE_OAPP, { oappConfig }) | ||
|
||
expect(result).toEqual([]) | ||
}) | ||
}) | ||
}) |
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,2 +1,3 @@ | ||
import './oapp' | ||
import './getDefaultConfig' | ||
import './getOAppConfig' |
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 @@ | ||
import './wire' |
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,62 @@ | ||
import { task, types } from 'hardhat/config' | ||
import type { ActionType } from 'hardhat/types' | ||
import { TASK_LZ_WIRE_OAPP } from '@/constants/tasks' | ||
import { isFile, isReadable, promptToContinue } from '@layerzerolabs/io-utils' | ||
import { OAppOmniGraphHardhat, OAppOmniGraphHardhatSchema } from '@/oapp' | ||
|
||
interface TaskArgs { | ||
oappConfig: string | ||
} | ||
|
||
const action: ActionType<TaskArgs> = async ({ oappConfig: oappConfigPath }) => { | ||
// First we check that the config file is indeed there and we can read it | ||
const isConfigReadable = isFile(oappConfigPath) && isReadable(oappConfigPath) | ||
if (!isConfigReadable) { | ||
throw new Error( | ||
`Unable to read config file '${oappConfigPath}'. Check that the file exists and is readable to your terminal user` | ||
) | ||
} | ||
|
||
// Now let's see if we can load the config file | ||
let rawConfig: unknown | ||
try { | ||
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 | ||
|
||
const go = await promptToContinue() | ||
if (!go) { | ||
return undefined | ||
} | ||
|
||
return [] | ||
} | ||
task(TASK_LZ_WIRE_OAPP, 'Wire LayerZero OApp') | ||
.addParam('oappConfig', 'Path to your LayerZero OApp config', './layerzero.config.js', types.string) | ||
.setAction(action) |