@@ -567,52 +567,60 @@ export class Common {
567567 }
568568
569569 /**
570- * Returns the change block for the next hardfork after the hardfork provided or set
570+ * Returns the block number or timestamp at which the next hardfork will occur.
571+ * For pre-merge hardforks, returns the block number.
572+ * For post-merge hardforks, returns the timestamp.
573+ * Returns null if there is no next hardfork.
571574 * @param hardfork Hardfork name, optional if HF set
572- * @returns Block timestamp, number or null if not available
575+ * @returns Block number or timestamp, or null if not available
573576 */
574577 nextHardforkBlockOrTimestamp ( hardfork ?: string | Hardfork ) : bigint | null {
575- hardfork = hardfork ?? this . _hardfork
578+ const targetHardfork = hardfork ?? this . _hardfork
576579 const hfs = this . hardforks ( )
577- let hfIndex = hfs . findIndex ( ( hf ) => hf . name === hardfork )
578- // If the current hardfork is merge, go one behind as merge hf is not part of these
579- // calcs even if the merge hf block is set
580- if ( hardfork === Hardfork . Paris ) {
581- hfIndex -= 1
582- }
583- // Hardfork not found
584- if ( hfIndex < 0 ) {
580+
581+ // Find the index of the target hardfork
582+ let targetHfIndex = hfs . findIndex ( ( hf ) => hf . name === targetHardfork )
583+
584+ // Special handling for The Merge (Paris) hardfork
585+ if ( targetHardfork === Hardfork . Paris ) {
586+ // The Merge is determined by total difficulty, not block number
587+ // So we look at the previous hardfork's parameters instead
588+ targetHfIndex -= 1
589+ }
590+
591+ // If we couldn't find a valid hardfork index, return null
592+ if ( targetHfIndex < 0 ) {
585593 return null
586594 }
587595
588- let currHfTimeOrBlock = hfs [ hfIndex ] . timestamp ?? hfs [ hfIndex ] . block
589- currHfTimeOrBlock =
590- currHfTimeOrBlock !== null && currHfTimeOrBlock !== undefined
591- ? Number ( currHfTimeOrBlock )
592- : null
596+ // Get the current hardfork's block/timestamp
597+ const currentHf = hfs [ targetHfIndex ]
598+ const currentBlockOrTimestamp = currentHf . timestamp ?? currentHf . block
599+ if ( currentBlockOrTimestamp === null || currentBlockOrTimestamp === undefined ) {
600+ return null
601+ }
593602
594- const nextHf = hfs . slice ( hfIndex + 1 ) . find ( ( hf ) => {
595- let hfTimeOrBlock = hf . timestamp ?? hf . block
596- hfTimeOrBlock =
597- hfTimeOrBlock !== null && hfTimeOrBlock !== undefined ? Number ( hfTimeOrBlock ) : null
603+ // Find the next hardfork that has a different block/timestamp
604+ const nextHf = hfs . slice ( targetHfIndex + 1 ) . find ( ( hf ) => {
605+ const nextBlockOrTimestamp = hf . timestamp ?? hf . block
598606 return (
599- hf . name !== Hardfork . Paris &&
600- hfTimeOrBlock !== null &&
601- hfTimeOrBlock !== undefined &&
602- hfTimeOrBlock !== currHfTimeOrBlock
607+ nextBlockOrTimestamp !== null &&
608+ nextBlockOrTimestamp !== undefined &&
609+ nextBlockOrTimestamp !== currentBlockOrTimestamp
603610 )
604611 } )
605612 // If no next hf found with valid block or timestamp return null
606613 if ( nextHf === undefined ) {
607614 return null
608615 }
609616
610- const nextHfBlock = nextHf . timestamp ?? nextHf . block
611- if ( nextHfBlock === null || nextHfBlock === undefined ) {
617+ // Get the block/timestamp for the next hardfork
618+ const nextBlockOrTimestamp = nextHf . timestamp ?? nextHf . block
619+ if ( nextBlockOrTimestamp === null || nextBlockOrTimestamp === undefined ) {
612620 return null
613621 }
614622
615- return BigInt ( nextHfBlock )
623+ return BigInt ( nextBlockOrTimestamp )
616624 }
617625
618626 /**
0 commit comments