Skip to content

Commit

Permalink
Extract getVerificationType from Tenderly.ts (#251)
Browse files Browse the repository at this point in the history
### TL;DR
Extracted verification type logic into separate utility functions to improve code organization and testability.

### What changed?
- Created new utility functions `getVerificationType` and `isVerificationOnVnet`
- Moved verification type logic from private methods in the Tenderly class to standalone functions
- Updated all verification-related method calls to use the new utility functions
- Created new utility files to house the extracted functions

### How to test?
1. Run existing verification tests to ensure they pass with the new utility functions
2. Test verification on different network types (devnet, fork, private, public)
3. Verify that contract verification still works as expected across all scenarios

### Why make this change?
The verification type logic was previously embedded within the Tenderly class as private methods, making it difficult to test and reuse. By extracting these functions, we improve:
- Code modularity and reusability
- Testing capabilities
- Separation of concerns
- Maintainability of verification-related logic
  • Loading branch information
dule-git authored Nov 21, 2024
1 parent 31fd155 commit d1f07b0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
40 changes: 9 additions & 31 deletions packages/tenderly-hardhat/src/Tenderly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { TenderlyNetwork } from "./TenderlyNetwork";
import { ProxyPlaceholderName } from "./index";
import { VerificationService } from "./verification";
import { throwIfUsernameOrProjectNotSet, UndefinedChainIdError } from "./errors";
import { getVerificationType, isVerificationOnVnet } from "./utils";

export class Tenderly {
public env: HardhatRuntimeEnvironment;
Expand Down Expand Up @@ -90,7 +91,7 @@ export class Tenderly {
return;
}

const verificationType = this._getVerificationType();
const verificationType = await getVerificationType(this.env, this.tenderlyNetwork);
const platformID =
verificationType === VERIFICATION_TYPES.FORK
? this.tenderlyNetwork.forkID
Expand All @@ -107,7 +108,7 @@ export class Tenderly {
);
return;
}
if (this._isVerificationOnPlatform(verificationType)) {
if (await isVerificationOnVnet(verificationType)) {
logger.info(
`Network parameter is set to '${this.getNetworkName()}', redirecting to ${verificationType} verification.`,
);
Expand Down Expand Up @@ -139,7 +140,8 @@ export class Tenderly {
logger.info("Invoked verification (multi compiler version) through API.");
logger.trace("Request data:", request);

switch (this._getVerificationType()) {
const verificationType = await getVerificationType(this.env, this.tenderlyNetwork);
switch (verificationType) {
case VERIFICATION_TYPES.FORK:
logger.error(
`Error in ${PLUGIN_NAME}: Network parameter is set to 'tenderly' and verifyMultiCompilerAPI() is not available for fork deployments, please use verifyForkAPI().`,
Expand Down Expand Up @@ -257,25 +259,6 @@ export class Tenderly {
return this.tenderlyNetwork;
}

private _getVerificationType(): string {
if (isTenderlyNetworkConfig(this.env.network.config)) {
return this.tenderlyNetwork.devnetID !== undefined
? VERIFICATION_TYPES.DEVNET
: VERIFICATION_TYPES.FORK;
}

const priv = this.env.config.tenderly?.privateVerification;
if (
priv !== undefined &&
priv &&
!isTenderlyNetworkConfig(this.env.network.config)
) {
return VERIFICATION_TYPES.PRIVATE;
}

return VERIFICATION_TYPES.PUBLIC;
}

public async push(...contracts: any[]): Promise<void> {
return this.verify(...contracts);
}
Expand All @@ -285,8 +268,9 @@ export class Tenderly {
): Promise<void> {
logger.info("Invoked public verification through API request.");

const verificationType = await getVerificationType(this.env, this.tenderlyNetwork);
if (isTenderlyNetworkConfig(this.env.network.config)) {
if (this._getVerificationType() === VERIFICATION_TYPES.DEVNET) {
if (verificationType === VERIFICATION_TYPES.DEVNET) {
logger.error(
`Error in ${PLUGIN_NAME}: Network parameter is set to '${this.getNetworkName()}' and verifyAPI() is not available for devnet deployments.`,
);
Expand Down Expand Up @@ -330,8 +314,9 @@ export class Tenderly {
): Promise<void> {
logger.info("Invoked pushing contracts through API.");

const verificationType = await getVerificationType(this.env, this.tenderlyNetwork);
if (isTenderlyNetworkConfig(this.env.network.config)) {
if (this._getVerificationType() === VERIFICATION_TYPES.DEVNET) {
if (verificationType === VERIFICATION_TYPES.DEVNET) {
logger.error(
`Error in ${PLUGIN_NAME}: Network parameter is set to '${this.getNetworkName()}' and pushAPI() is not available for devnet deployments.`,
);
Expand Down Expand Up @@ -529,13 +514,6 @@ export class Tenderly {
};
}

private _isVerificationOnPlatform(verificationType: string): boolean {
return (
verificationType === VERIFICATION_TYPES.DEVNET ||
verificationType === VERIFICATION_TYPES.FORK
);
}

private _existsProxyVerification(contracts: any[]): boolean {
return contracts.some((contract) => contract.name === ProxyPlaceholderName);
}
Expand Down
34 changes: 34 additions & 0 deletions packages/tenderly-hardhat/src/utils/get-verification-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { isTenderlyNetworkConfig } from "./util";
import { VERIFICATION_TYPES } from "../constants";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { TenderlyNetwork } from "../TenderlyNetwork";

export async function getVerificationType(
hre: HardhatRuntimeEnvironment,
tenderlyNetwork: TenderlyNetwork
): Promise<string> {
if (isTenderlyNetworkConfig(hre.network.config)) {
return tenderlyNetwork.devnetID !== undefined
? VERIFICATION_TYPES.DEVNET
: VERIFICATION_TYPES.FORK;
}

const priv = hre.config.tenderly?.privateVerification;
if (
priv !== undefined &&
priv &&
!isTenderlyNetworkConfig(hre.network.config)
) {
return VERIFICATION_TYPES.PRIVATE;
}

return VERIFICATION_TYPES.PUBLIC;
}

export async function isVerificationOnVnet(verificationType: string): Promise<boolean> {
return (
verificationType === VERIFICATION_TYPES.DEVNET ||
verificationType === VERIFICATION_TYPES.FORK
);
}

4 changes: 4 additions & 0 deletions packages/tenderly-hardhat/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
getVerificationType,
isVerificationOnVnet
} from "./get-verification-type";

0 comments on commit d1f07b0

Please sign in to comment.