Skip to content

Commit 320e174

Browse files
codybornclaude
andcommitted
refactor: replace ! assertions in factories with LazyProviderMap.getOrThrow
The factory closures all used providerMap.get(chainId)! to coerce the optional return into a non-null provider. That's safe today because every validator map's isSupported predicate is a subset of LazyProviderMap's supported set, but it's a brittle invariant — narrowing the supported set in a future change would silently produce undefined providers wrapped inside validator instances. Add LazyProviderMap.getOrThrow(chainId): StaticJsonRpcProvider that throws a clear error if the chainId isn't supported, and use it from the four factory closures. The structural ProviderMap interface (Map-compatible .get returning T | undefined) is unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 6909e8c commit 320e174

4 files changed

Lines changed: 17 additions & 6 deletions

File tree

lib/handlers/check-order-status/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const providerMap = new LazyProviderMap()
2424

2525
const relayOrderValidator = new OffChainRelayOrderValidator(() => new Date().getTime() / 1000)
2626
const relayOrderValidatorMap = new OnChainValidatorMap<OnChainRelayOrderValidator>([], {
27-
factory: (chainId) => new OnChainRelayOrderValidator(providerMap.get(chainId)!, chainId),
27+
factory: (chainId) => new OnChainRelayOrderValidator(providerMap.getOrThrow(chainId), chainId),
2828
isSupported: (chainId) => supportedChainSet.has(chainId),
2929
})
3030

lib/handlers/post-limit-order/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ const isSupportedChain = (chainId: ChainId) => supportedChainSet.has(chainId)
3030
const providerMap = new LazyProviderMap()
3131

3232
const onChainValidatorMap = new OnChainValidatorMap<OnChainOrderValidator>([], {
33-
factory: (chainId) => new OnChainOrderValidator(providerMap.get(chainId)!, chainId),
33+
factory: (chainId) => new OnChainOrderValidator(providerMap.getOrThrow(chainId), chainId),
3434
isSupported: isSupportedChain,
3535
})
3636

3737
const relayOrderValidatorMap = new OnChainValidatorMap<OnChainRelayOrderValidator>([], {
38-
factory: (chainId) => new OnChainRelayOrderValidator(providerMap.get(chainId)!, chainId),
38+
factory: (chainId) => new OnChainRelayOrderValidator(providerMap.getOrThrow(chainId), chainId),
3939
isSupported: isSupportedChain,
4040
})
4141

lib/handlers/post-order/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ const isSupportedChain = (chainId: ChainId) => supportedChainSet.has(chainId)
3737
const providerMap = new LazyProviderMap()
3838

3939
const onChainValidatorMap = new OnChainValidatorMap<OnChainOrderValidator>([], {
40-
factory: (chainId) => new OnChainOrderValidator(providerMap.get(chainId)!, chainId),
40+
factory: (chainId) => new OnChainOrderValidator(providerMap.getOrThrow(chainId), chainId),
4141
isSupported: isSupportedChain,
4242
})
4343

4444
const onChainV4ValidatorMap = new OnChainValidatorMap<OnChainV4OrderValidator>([], {
45-
factory: (chainId) => new OnChainV4OrderValidator(providerMap.get(chainId)!, chainId),
45+
factory: (chainId) => new OnChainV4OrderValidator(providerMap.getOrThrow(chainId), chainId),
4646
isSupported: (chainId) => isSupportedChain(chainId) && !!OnChainV4QuoterMapping[chainId],
4747
})
4848

4949
const relayOrderValidatorMap = new OnChainValidatorMap<OnChainRelayOrderValidator>([], {
50-
factory: (chainId) => new OnChainRelayOrderValidator(providerMap.get(chainId)!, chainId),
50+
factory: (chainId) => new OnChainRelayOrderValidator(providerMap.getOrThrow(chainId), chainId),
5151
isSupported: isSupportedChain,
5252
})
5353

lib/handlers/shared/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@ export class LazyProviderMap implements ProviderMap {
2828
}
2929
return provider
3030
}
31+
32+
// Strict variant for callers (e.g. validator factories) that must receive a
33+
// provider. Throws if the chainId is outside this map's supported set
34+
// instead of returning undefined.
35+
getOrThrow(chainId: ChainId): StaticJsonRpcProvider {
36+
const provider = this.get(chainId)
37+
if (!provider) {
38+
throw new Error(`No RPC provider configured for chain ${chainId}`)
39+
}
40+
return provider
41+
}
3142
}

0 commit comments

Comments
 (0)