From 07094a1123d3b7ed73d2b31b7b3c38c7e07a5458 Mon Sep 17 00:00:00 2001 From: J M Rossy Date: Tue, 19 Dec 2023 11:51:21 -0500 Subject: [PATCH] Fix chain metadata parsing --- .vscode/settings.json | 4 ++-- src/features/chains/chainConfig.ts | 11 +++++----- src/features/chains/chainconfig.test.ts | 29 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 src/features/chains/chainconfig.test.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index b9bca4f..91ea6ca 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -16,14 +16,14 @@ "[typescript]": { "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.organizeImports": true + "source.organizeImports": "explicit" }, "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[typescriptreact]": { "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.organizeImports": true + "source.organizeImports": "explicit" }, "editor.defaultFormatter": "esbenp.prettier-vscode" }, diff --git a/src/features/chains/chainConfig.ts b/src/features/chains/chainConfig.ts index 3e4184a..7b69896 100644 --- a/src/features/chains/chainConfig.ts +++ b/src/features/chains/chainConfig.ts @@ -1,14 +1,13 @@ import { z } from 'zod'; -import { ChainMetadata, ChainMetadataSchema, MultiProvider } from '@hyperlane-xyz/sdk'; +import { ChainMetadata, ChainMetadataSchemaObject, MultiProvider } from '@hyperlane-xyz/sdk'; import { logger } from '../../utils/logger'; -export const ChainConfigSchema = z.record( - ChainMetadataSchema.and( - z.object({ mailbox: z.string().optional(), interchainGasPaymaster: z.string().optional() }), - ), -); +export const ChainConfigSchema = ChainMetadataSchemaObject.extend({ + mailbox: z.string().optional(), + interchainGasPaymaster: z.string().optional(), +}); export type ChainConfig = ChainMetadata & { mailbox?: Address; interchainGasPaymaster?: Address }; diff --git a/src/features/chains/chainconfig.test.ts b/src/features/chains/chainconfig.test.ts new file mode 100644 index 0000000..043c19b --- /dev/null +++ b/src/features/chains/chainconfig.test.ts @@ -0,0 +1,29 @@ +import { tryParseChainConfig } from './chainConfig'; + +const validConfig = { + chainId: 12345, + name: 'mytestnet', + protocol: 'ethereum', + rpcUrls: [{ http: 'https://fakerpc.com' }], + blockExplorers: [ + { + name: 'FakeScan', + family: 'other', + url: 'https://fakeexplorer.com', + apiUrl: 'https://fakeexplorer.com', + }, + ], + blocks: { confirmations: 1, estimateBlockTime: 10 }, + mailbox: '0x14999bccB37118713891DAAA1D5959a02E206C1f', +}; + +describe('chain configs', () => { + it('parses valid config', async () => { + const result = tryParseChainConfig(JSON.stringify(validConfig)); + expect(result.success).toBe(true); + }); + it('rejects invalid config', async () => { + const result = tryParseChainConfig(JSON.stringify({ ...validConfig, chainId: undefined })); + expect(result.success).toBe(false); + }); +});