From e47194d6c2e7a072975678700a1d6d45060a8467 Mon Sep 17 00:00:00 2001 From: sashass1315 Date: Thu, 4 Sep 2025 14:50:14 +0300 Subject: [PATCH] Add defensive checks in getDeployedContract for missing data --- packages/utils/src/networks/index.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/networks/index.ts b/packages/utils/src/networks/index.ts index c98f734fe..ed4ca6076 100644 --- a/packages/utils/src/networks/index.ts +++ b/packages/utils/src/networks/index.ts @@ -54,9 +54,25 @@ export function getDeployedContract(supportedNetwork: SupportedNetwork) { throw new Error(`Semaphore has not been deployed on '${supportedNetwork}' yet`) } - const deployedContract = deployedContracts.find(({ network }) => network === supportedNetwork) + const networkDeployedContracts = deployedContracts.find(({ network }) => network === supportedNetwork) - return deployedContract!.contracts.find(({ name }) => name === "Semaphore") as { + if (!networkDeployedContracts) { + throw new Error( + `No deployed contracts found for network '${supportedNetwork}'. ` + + "Please ensure 'deployed-contracts.json' contains an entry for this network." + ) + } + + const semaphoreContract = networkDeployedContracts.contracts.find(({ name }) => name === "Semaphore") + + if (!semaphoreContract) { + throw new Error( + `Contract 'Semaphore' not found in deployed contracts for network '${supportedNetwork}'. ` + + "Make sure deployments include 'Semaphore' for this network." + ) + } + + return semaphoreContract as { name: string address: string startBlock: number