diff --git a/.changeset/quiet-cooks-join.md b/.changeset/quiet-cooks-join.md new file mode 100644 index 0000000000..a5f39f2ab4 --- /dev/null +++ b/.changeset/quiet-cooks-join.md @@ -0,0 +1,6 @@ +--- +'@hyperlane-xyz/cli': minor +'@hyperlane-xyz/sdk': minor +--- + +Add `EthJsonRpcBlockParameterTag` enum for validating reorgPeriod diff --git a/typescript/cli/src/config/chain.ts b/typescript/cli/src/config/chain.ts index d3279a2029..e710b6cf00 100644 --- a/typescript/cli/src/config/chain.ts +++ b/typescript/cli/src/config/chain.ts @@ -5,6 +5,7 @@ import { stringify as yamlStringify } from 'yaml'; import { ChainMetadata, ChainMetadataSchema, + EthJsonRpcBlockParameterTag, ExplorerFamily, ZChainName, } from '@hyperlane-xyz/sdk'; @@ -168,9 +169,11 @@ async function addBlockOrGasConfig(metadata: ChainMetadata): Promise { } async function addBlockConfig(metadata: ChainMetadata): Promise { - const parseReorgPeriod = (value: string) => { + const parseReorgPeriod = ( + value: string, + ): number | EthJsonRpcBlockParameterTag => { const parsed = parseInt(value, 10); - return isNaN(parsed) ? value : parsed; + return isNaN(parsed) ? (value as EthJsonRpcBlockParameterTag) : parsed; }; const wantBlockConfig = await confirm({ @@ -184,10 +187,16 @@ async function addBlockConfig(metadata: ChainMetadata): Promise { }); const blockReorgPeriod = await input({ message: - 'Enter no. of blocks before a transaction has a near-zero chance of reverting (0-500) or block tag:', - validate: (value) => - isNaN(parseInt(value)) || - (parseInt(value) >= 0 && parseInt(value) <= 500), + 'Enter no. of blocks before a transaction has a near-zero chance of reverting (0-500) or block tag (earliest, latest, safe, finalized, pending):', + validate: (value) => { + const parsedInt = parseInt(value, 10); + return ( + Object.values(EthJsonRpcBlockParameterTag).includes( + value as EthJsonRpcBlockParameterTag, + ) || + (!isNaN(parsedInt) && parsedInt >= 0 && parsedInt <= 500) + ); + }, }); const blockTimeEstimate = await input({ message: 'Enter the rough estimate of time per block in seconds (0-20):', diff --git a/typescript/sdk/src/index.ts b/typescript/sdk/src/index.ts index 415a11d269..e9bd31bb42 100644 --- a/typescript/sdk/src/index.ts +++ b/typescript/sdk/src/index.ts @@ -196,6 +196,7 @@ export { export { BlockExplorer, BlockExplorerSchema, + EthJsonRpcBlockParameterTag, ChainMetadata, ChainMetadataSchema, ChainMetadataSchemaObject, diff --git a/typescript/sdk/src/metadata/chainMetadata.test.ts b/typescript/sdk/src/metadata/chainMetadata.test.ts index 65a980f834..f38a59e46d 100644 --- a/typescript/sdk/src/metadata/chainMetadata.test.ts +++ b/typescript/sdk/src/metadata/chainMetadata.test.ts @@ -2,7 +2,11 @@ import { expect } from 'chai'; import { ProtocolType } from '@hyperlane-xyz/utils'; -import { ChainMetadata, isValidChainMetadata } from './chainMetadataTypes.js'; +import { + ChainMetadata, + EthJsonRpcBlockParameterTag, + isValidChainMetadata, +} from './chainMetadataTypes.js'; const minimalSchema: ChainMetadata = { chainId: 5, @@ -68,7 +72,7 @@ describe('ChainMetadataSchema', () => { ...minimalSchema, blocks: { confirmations: 1, - reorgPeriod: 'finalized', + reorgPeriod: EthJsonRpcBlockParameterTag.Finalized, }, }), ).to.eq(true); diff --git a/typescript/sdk/src/metadata/chainMetadataTypes.ts b/typescript/sdk/src/metadata/chainMetadataTypes.ts index cf6c9bb54b..bf41cd4ac2 100644 --- a/typescript/sdk/src/metadata/chainMetadataTypes.ts +++ b/typescript/sdk/src/metadata/chainMetadataTypes.ts @@ -10,6 +10,14 @@ import { ChainMap } from '../types.js'; import { ZChainName, ZNzUint, ZUint } from './customZodTypes.js'; +export enum EthJsonRpcBlockParameterTag { + Earliest = 'earliest', + Latest = 'latest', + Safe = 'safe', + Finalized = 'finalized', + Pending = 'pending', +} + export enum ExplorerFamily { Etherscan = 'etherscan', Blockscout = 'blockscout',