Skip to content

Commit ca6bbfc

Browse files
committed
chore: Simplify chainId validation logic
1 parent 2736f9b commit ca6bbfc

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

src/config/config.service.ts

+11-19
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class ConfigService {
3737
// is executed across services (i.e. there is no guarantee that the config service will be the
3838
// first to initialize). The `isReady` promise must be awaited on Relayer initialization.
3939
private async initialize(): Promise<void> {
40-
await this.validateChains(this.chainsConfig);
40+
await this.validateChainIds(this.chainsConfig);
4141
}
4242

4343
private loadNodeEnv(): string {
@@ -185,28 +185,20 @@ export class ConfigService {
185185
return this.ambsConfig.get(amb)?.globalProperties[key];
186186
}
187187

188-
private async validateChains(chainsConfig: Map<string, ChainConfig>): Promise<void> {
189-
const validateChainIdFromRPC = async (rpc: string, expectedChainId: string): Promise<boolean> => {
190-
const provider = new JsonRpcProvider(rpc, undefined, { staticNetwork: true });
191-
try {
192-
const network = await provider.getNetwork();
193-
const actualChainId = network.chainId.toString();
194-
return actualChainId === expectedChainId;
195-
} catch (error) {
196-
return false;
197-
}
198-
};
188+
private async validateChainIds(chainsConfig: Map<string, ChainConfig>): Promise<void> {
199189

200190
const validationPromises = [];
201-
202191
for (const [chainId, config] of chainsConfig) {
203-
const validationPromise = async () => {
204-
const chainIdValidate = await validateChainIdFromRPC(config.rpc, chainId);
205-
if (!chainIdValidate) {
206-
throw new Error(`Error validating the Chain ID for chain ${chainId}`);
192+
const provider = new JsonRpcProvider(config.rpc, undefined, { staticNetwork: true });
193+
const validationPromise = provider.getNetwork().then(
194+
(network) => {
195+
const rpcChainId = network.chainId.toString();
196+
if (rpcChainId !== chainId) {
197+
throw new Error(`Error validating the chain ID of chain ${chainId}: the RPC chain ID is ${rpcChainId}.`)
198+
}
207199
}
208-
};
209-
validationPromises.push(validationPromise());
200+
)
201+
validationPromises.push(validationPromise);
210202
}
211203

212204
await Promise.all(validationPromises);

0 commit comments

Comments
 (0)