From e316d450fcad8f44f19ce28435291d6270a48e36 Mon Sep 17 00:00:00 2001 From: dule-git <61541725+dule-git@users.noreply.github.com> Date: Thu, 21 Nov 2024 14:10:57 +0100 Subject: [PATCH] Improve chainId resolution and error handling (#248) ### TL;DR Improved chain ID resolution and error handling in the Tenderly Hardhat plugin. ### What changed? - Created a new `UndefinedChainIdError` class for better error messaging - Extracted chain ID resolution logic into a separate `getChainId` function - Improved error handling when chain ID cannot be determined - Standardized the chain ID resolution process across the plugin ### How to test? 1. Try verifying contracts on a network without a defined chain ID 2. Verify the new error message appears with the network name 3. Test contract verification on networks with valid chain IDs 4. Verify chain ID resolution works for both custom and predefined networks ### Why make this change? To provide clearer error messages and better handle cases where chain IDs are undefined. This improves the developer experience by making it immediately clear when and why chain ID resolution fails, rather than silently failing or providing unclear error messages. --- packages/tenderly-hardhat/src/errors.ts | 5 +++ packages/tenderly-hardhat/src/utils/util.ts | 37 +++++++++++++-------- 2 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 packages/tenderly-hardhat/src/errors.ts diff --git a/packages/tenderly-hardhat/src/errors.ts b/packages/tenderly-hardhat/src/errors.ts new file mode 100644 index 00000000..cd08646c --- /dev/null +++ b/packages/tenderly-hardhat/src/errors.ts @@ -0,0 +1,5 @@ +export class UndefinedChainIdError extends Error { + constructor(networkName: string) { + super(`Couldn't find chainId for the network: ${networkName}. \nPlease provide the chainId in the network config object`); + } +} diff --git a/packages/tenderly-hardhat/src/utils/util.ts b/packages/tenderly-hardhat/src/utils/util.ts index 1af69ce0..296e3adc 100644 --- a/packages/tenderly-hardhat/src/utils/util.ts +++ b/packages/tenderly-hardhat/src/utils/util.ts @@ -28,6 +28,7 @@ import { CONTRACT_NAME_PLACEHOLDER, PLUGIN_NAME } from "../constants"; import { CONTRACTS_NOT_DETECTED } from "../tenderly/errors"; import { ContractByName, Metadata } from "../tenderly/types"; import { logger } from "./logger"; +import { UndefinedChainIdError } from "../errors"; export const makeVerifyContractsRequest = async ( hre: HardhatRuntimeEnvironment, @@ -66,22 +67,11 @@ export const makeVerifyContractsRequest = async ( platformID !== undefined ) { chainId = platformID; - } else if (hre.network?.config?.chainId !== undefined) { - chainId = hre.network.config.chainId.toString(); - } else if ( - NETWORK_NAME_CHAIN_ID_MAP[networkName.toLowerCase()] !== undefined - ) { - chainId = NETWORK_NAME_CHAIN_ID_MAP[networkName.toLowerCase()].toString(); + } else { + chainId = (await getChainId(hre)).toString(); } logger.trace(`ChainId for network '${networkName}' is ${chainId}`); - if (chainId === undefined) { - logger.error( - `Error in ${PLUGIN_NAME}: Couldn't identify network. Please provide a chainId in the network config object`, - ); - return null; - } - const compiler = await insertLibraries( hre, job.getSolcConfig(), @@ -108,6 +98,27 @@ export const makeVerifyContractsRequest = async ( }; }; +export async function getChainId( + hre: HardhatRuntimeEnvironment, +): Promise { + let chainId; + const networkName = hre.network.name; + + if (hre.network?.config?.chainId !== undefined) { + chainId = hre.network.config.chainId.toString(); + } else if ( + NETWORK_NAME_CHAIN_ID_MAP[networkName.toLowerCase()] !== undefined + ) { + chainId = NETWORK_NAME_CHAIN_ID_MAP[networkName.toLowerCase()].toString(); + } + + if (!chainId) { + throw new UndefinedChainIdError(networkName); + } + + return parseInt(chainId); +} + async function extractSources( hre: HardhatRuntimeEnvironment, contractToVerify: string,