11import { factory , mergeAbis } from "ponder" ;
22import { type Abi , getAbiItem } from "viem" ;
33import fullRegistry from "../generated" ;
4- import { skipBlocks , type RegistryVersions , type Registry , type NetworkNames } from "./chains" ;
4+ import { type RegistryVersions , type Registry , type NetworkNames } from "./chains" ;
55import { networkNames } from "./chains" ;
66
77// ============================================================================
@@ -270,7 +270,7 @@ export function decorateDeploymentContracts<
270270 registryVersion : V ,
271271 selectedAbiNames : A ,
272272 additionalMappings : AM ,
273- overlappingHours ?: number
273+ endblocks ?: Partial < Record < keyof typeof networkNames , number > >
274274) : ContractsWithAdditionalMappings < V , A [ number ] , AM , keyof AM & string > {
275275 // Validate registry version exists
276276 const abis = Abis [ registryVersion ] ;
@@ -297,7 +297,7 @@ export function decorateDeploymentContracts<
297297 `${ toContractCase ( abiName ) } ${ registryVersion . toUpperCase ( ) } ` ,
298298 {
299299 abi,
300- chain : getContractChain ( registryVersion , abiName ) ,
300+ chain : getContractChain ( registryVersion , abiName , endblocks ) ,
301301 } ,
302302 ] ;
303303 } ) ;
@@ -398,7 +398,7 @@ export function decorateDeploymentContracts<
398398 mappingName ,
399399 {
400400 abi : resolvedAbi ,
401- chain : getContractChain ( registryVersion , m . factory . abi , overlappingHours , {
401+ chain : getContractChain ( registryVersion , m . factory . abi , endblocks , {
402402 // Type assertion: event name is validated above to exist in factory ABI
403403 // Cast through unknown to satisfy type system while maintaining runtime safety
404404 event : m . factory . eventName ,
@@ -427,7 +427,7 @@ export function decorateDeploymentContracts<
427427function getContractChain < V extends RegistryVersions , N extends AbiName < V > > (
428428 registryVersion : V ,
429429 abiName : N ,
430- overlappingHours ?: number ,
430+ endBlocks ?: Partial < Record < keyof typeof networkNames , number | undefined > > ,
431431 factoryConfig ?: {
432432 event : AbiEventName < V , N > ;
433433 parameter : AbiEventParameter < V , N , AbiEventName < V , N > > ;
@@ -465,12 +465,7 @@ function getContractChain<V extends RegistryVersions, N extends AbiName<V>>(
465465 }
466466
467467 const startBlock = chainValue . deployment . startBlock as number ;
468- const endBlock = computeEndBlock (
469- chainId ,
470- toContractCase ( abiName ) ,
471- registryVersion ,
472- overlappingHours
473- ) ;
468+ const endBlock = computeEndBlock ( chainId , toContractCase ( abiName ) , registryVersion , endBlocks ) ;
474469 return [ chainName , { address, startBlock, endBlock } ] ;
475470 } ) ;
476471
@@ -496,62 +491,50 @@ function computeEndBlock(
496491 chainId : keyof typeof networkNames ,
497492 contractName : string ,
498493 startVersion : RegistryVersions ,
499- overlappingHours ?: number
494+ endBlocks ?: Partial < Record < keyof typeof networkNames , number | undefined > >
500495) : number | undefined {
496+ if ( endBlocks ) return endBlocks [ chainId ] ;
497+
501498 // Get the versions array
502499 const versions = Object . keys ( fullRegistry ) as RegistryVersions [ ] ;
503500
504501 // Find the index of startVersion in the versions array
505- const startVersionIndex = versions . indexOf ( startVersion ) ;
502+ const currentVersionIndex = versions . indexOf ( startVersion ) ;
506503
507- if ( startVersionIndex === - 1 ) {
504+ if ( currentVersionIndex === - 1 )
508505 throw new Error ( `Start version "${ startVersion } " not found in registry versions` ) ;
509- }
510506
511- // Get the next version after startVersion
512- const endVersionIndex = startVersionIndex + 1 ;
507+ const nextVersionIndex = currentVersionIndex + 1 ;
513508
514- if ( endVersionIndex >= versions . length ) {
515- return undefined ;
516- }
509+ if ( nextVersionIndex >= versions . length ) return undefined ;
517510
518- const endVersion = versions [ endVersionIndex ] ;
519- if ( ! endVersion ) {
520- return undefined ;
521- }
511+ const nextVersion = versions [ nextVersionIndex ] ;
512+ if ( ! nextVersion ) return undefined ;
522513
523514 // Get the registry for the next version
524- const endRegistry = fullRegistry [ endVersion ] as Registry < RegistryVersions > ;
515+ const nextRegistry = fullRegistry [ nextVersion ] as Registry < RegistryVersions > ;
525516
526517 // Access the chain for the given chainId
527518 // Use Object.entries to safely access chains
528- const chainEntries = Object . entries ( endRegistry . chains ) as Entries < typeof endRegistry . chains > ;
529- const chainEntry = chainEntries . find ( ( [ id ] ) => id === chainId ) ;
530- if ( ! chainEntry ) {
531- return undefined ;
532- }
533- const chain = chainEntry [ 1 ] ;
519+ const nextChainEntries = Object . entries ( nextRegistry . chains ) as Entries <
520+ typeof nextRegistry . chains
521+ > ;
522+ const nextChainEntry = nextChainEntries . find ( ( [ id ] ) => id === chainId ) ;
523+ if ( ! nextChainEntry ) return undefined ;
524+
525+ const nextChain = nextChainEntry [ 1 ] ;
534526
535527 // Check if the contract exists in this version
536- const contract = chain . contracts [ contractName as keyof typeof chain . contracts ] ;
537- if ( ! contract ) {
538- return undefined ;
539- }
528+ const newContract = nextChain . contracts [ contractName as keyof typeof nextChain . contracts ] ;
529+ if ( ! newContract ) return undefined ;
540530
541531 // Get the block number: use contract.blockNumber if available, otherwise fallback to chain.deployment.startBlock
542- const contractData = contract as { blockNumber ?: number | null } ;
543- const startBlock = contractData . blockNumber ?? chain . deployment . startBlock ;
544-
545- if ( startBlock === null || startBlock === undefined ) {
546- return undefined ;
547- }
532+ const nextContractData = newContract as { blockNumber ?: number | null } ;
533+ const nextContractStartBlock = nextContractData . blockNumber ?? nextChain . deployment . startBlock ;
548534
549- const endBlock = overlappingHours
550- ? startBlock + overlappingHours * skipBlocks [ chainId as keyof typeof skipBlocks ]
551- : startBlock - 1 ;
535+ if ( ! nextContractStartBlock ) return undefined ;
552536
553- // Return the end block (start block of next version minus 1)
554- return endBlock ;
537+ return nextContractStartBlock - 1 ;
555538}
556539
557540/** Ordered registry version keys (e.g. ["v3", "v3_1"]) for use as message/payload version index. */
0 commit comments