Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add block tag enum for validating reorgPeriod #4739

Merged
merged 7 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/quiet-cooks-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperlane-xyz/cli': minor
'@hyperlane-xyz/sdk': minor
---

Add `EthJsonRpcBlockParameterTag` enum for validating reorgPeriod
21 changes: 15 additions & 6 deletions typescript/cli/src/config/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { stringify as yamlStringify } from 'yaml';
import {
ChainMetadata,
ChainMetadataSchema,
EthJsonRpcBlockParameterTag,
ExplorerFamily,
ZChainName,
} from '@hyperlane-xyz/sdk';
Expand Down Expand Up @@ -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({
Expand All @@ -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):',
Expand Down
1 change: 1 addition & 0 deletions typescript/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export {
export {
BlockExplorer,
BlockExplorerSchema,
EthJsonRpcBlockParameterTag,
ChainMetadata,
ChainMetadataSchema,
ChainMetadataSchemaObject,
Expand Down
8 changes: 6 additions & 2 deletions typescript/sdk/src/metadata/chainMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -68,7 +72,7 @@ describe('ChainMetadataSchema', () => {
...minimalSchema,
blocks: {
confirmations: 1,
reorgPeriod: 'finalized',
reorgPeriod: EthJsonRpcBlockParameterTag.Finalized,
},
}),
).to.eq(true);
Expand Down
8 changes: 8 additions & 0 deletions typescript/sdk/src/metadata/chainMetadataTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import { ChainMap } from '../types.js';

import { ZChainName, ZNzUint, ZUint } from './customZodTypes.js';

export enum EthJsonRpcBlockParameterTag {
paulbalaji marked this conversation as resolved.
Show resolved Hide resolved
Earliest = 'earliest',
Latest = 'latest',
Safe = 'safe',
Finalized = 'finalized',
Pending = 'pending',
}

export enum ExplorerFamily {
Etherscan = 'etherscan',
Blockscout = 'blockscout',
Expand Down
Loading