feat: add block tag enum for validating reorgPeriod (#4739)

### Description

- feat: add `EthJsonRpcBlockParameterTag` enum for validating
reorgPeriod
- cli will check that reorgPeriod is either a number or one of the
appropriate tags, but the underlying schema is still left loose to be
forwards-compatible
- exporting the enum will also let us use this in unit tests on the
registry side

### Drive-by changes

na

### Related issues

na

### Backward compatibility

ye

### Testing

ci

---------

Signed-off-by: pbio <10051819+paulbalaji@users.noreply.github.com>
pull/4740/head
Paul Balaji 4 weeks ago committed by GitHub
parent 5fc442b97f
commit d41aa69285
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      .changeset/quiet-cooks-join.md
  2. 21
      typescript/cli/src/config/chain.ts
  3. 1
      typescript/sdk/src/index.ts
  4. 8
      typescript/sdk/src/metadata/chainMetadata.test.ts
  5. 8
      typescript/sdk/src/metadata/chainMetadataTypes.ts

@ -0,0 +1,6 @@
---
'@hyperlane-xyz/cli': minor
'@hyperlane-xyz/sdk': minor
---
Add `EthJsonRpcBlockParameterTag` enum for validating reorgPeriod

@ -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<void> {
}
async function addBlockConfig(metadata: ChainMetadata): Promise<void> {
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<void> {
});
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):',

@ -196,6 +196,7 @@ export {
export {
BlockExplorer,
BlockExplorerSchema,
EthJsonRpcBlockParameterTag,
ChainMetadata,
ChainMetadataSchema,
ChainMetadataSchemaObject,

@ -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);

@ -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',

Loading…
Cancel
Save