Skip to content

Commit

Permalink
feat: add chain-specific finalization thresholds
Browse files Browse the repository at this point in the history
- Add chainConfig with default and chain-specific finalization thresholds
- Add getFinalizationThreshold utility function
- Add tests for configuration and threshold retrieval
  • Loading branch information
0age committed Dec 4, 2024
1 parent 131bf5e commit f0b1bb7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/__tests__/chain-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getFinalizationThreshold, chainConfig } from '../chain-config';

describe('Chain Configuration', () => {
it('should return correct finalization threshold for known chains', () => {
expect(getFinalizationThreshold('1')).toBe(25); // Ethereum Mainnet
expect(getFinalizationThreshold('10')).toBe(2); // Optimism
expect(getFinalizationThreshold('8453')).toBe(4); // Base
});

it('should return default finalization threshold for unknown chains', () => {
expect(getFinalizationThreshold('123')).toBe(
chainConfig.defaultFinalizationThreshold
);
expect(getFinalizationThreshold('999')).toBe(
chainConfig.defaultFinalizationThreshold
);
});

it('should have valid configuration values', () => {
// All thresholds should be positive numbers
expect(chainConfig.defaultFinalizationThreshold).toBeGreaterThan(0);

Object.entries(chainConfig.finalizationThresholds).forEach(
([_chainId, threshold]) => {
expect(Number.isInteger(threshold)).toBe(true);
expect(threshold).toBeGreaterThan(0);
}
);
});
});
35 changes: 35 additions & 0 deletions src/chain-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Configuration for chain-specific settings
*/

export interface ChainConfig {
defaultFinalizationThreshold: number;
finalizationThresholds: Record<string, number>;
}

export const chainConfig: ChainConfig = {
// Default finalization threshold in seconds
defaultFinalizationThreshold: 3,

// Chain-specific finalization thresholds in seconds
finalizationThresholds: {
// Ethereum Mainnet
'1': 25,
// Optimism
'10': 2,
// Base
'8453': 4,
},
};

/**
* Get the finalization threshold for a specific chain ID
* @param chainId - The chain ID to get the threshold for
* @returns The finalization threshold in seconds
*/
export function getFinalizationThreshold(chainId: string): number {
return (
chainConfig.finalizationThresholds[chainId] ??
chainConfig.defaultFinalizationThreshold
);
}

0 comments on commit f0b1bb7

Please sign in to comment.