Skip to content

Commit

Permalink
Improve chainId resolution and error handling (#248)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
dule-git authored Nov 21, 2024
1 parent 42b5ea8 commit e316d45
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
5 changes: 5 additions & 0 deletions packages/tenderly-hardhat/src/errors.ts
Original file line number Diff line number Diff line change
@@ -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`);
}
}
37 changes: 24 additions & 13 deletions packages/tenderly-hardhat/src/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -108,6 +98,27 @@ export const makeVerifyContractsRequest = async (
};
};

export async function getChainId(
hre: HardhatRuntimeEnvironment,
): Promise<number> {
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,
Expand Down

0 comments on commit e316d45

Please sign in to comment.