Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 47 additions & 14 deletions tasks/misc/verify-sc.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,68 @@
import {task} from '@nomiclabs/buidler/config';
import {verifyContract, checkVerification} from '../../helpers/etherscan-verification';
import { task, HardhatRuntimeEnvironment } from '@nomiclabs/buidler/config';
import { verifyContract, checkVerification } from '../../helpers/etherscan-verification';

// Constants for improved readability and maintenance
const LIBRARIES_PARAM = 'libraries';
const CONSTRUCTOR_ARGS_PARAM = 'constructorArguments';

/**
* Interface defining the parameters passed to the Etherscan verification task.
* Note: The 'libraries' string must be parsed into an object before use.
*/
interface VerifyParams {
contractName: string;
address: string;
constructorArguments: string[];
libraries: string;
libraries?: string; // Optional raw JSON string
}

task('verify-sc', 'Inits the BRE, to have access to all the plugins')
.addParam('contractName', 'Name of the Solidity smart contract')
.addParam('address', 'Ethereum address of the smart contract')
task('verify-sc', 'Verifies a deployed smart contract on Etherscan.')
.addParam('contractName', 'Name of the Solidity smart contract.')
.addParam('address', 'Ethereum address of the smart contract.')
.addOptionalParam(
'libraries',
'Stringified JSON object in format of {library1: "0x2956356cd2a2bf3202f771f50d3d14a367b48071"}'
LIBRARIES_PARAM,
'Stringified JSON object defining library addresses: e.g., \'{"Library1": "0xAddr", "Library2": "0xAddr"}\''
)
.addOptionalVariadicPositionalParam(
'constructorArguments',
'arguments for contract constructor',
CONSTRUCTOR_ARGS_PARAM,
'Arguments for the contract constructor.',
[]
)
.setAction(
async (
{contractName, address, constructorArguments = [], libraries}: VerifyParams,
localBRE
{ contractName, address, constructorArguments = [], libraries }: VerifyParams,
localBRE: HardhatRuntimeEnvironment // Using the framework's internal type if available
) => {
await localBRE.run('set-bre');
// 1. Run internal setup task (if required by the environment)
await localBRE.run('set-bre');

// 2. Perform external checks (e.g., ensuring API key is set)
checkVerification();

const result = await verifyContract(contractName, address, constructorArguments, libraries);
let parsedLibraries = {};

// CRITICAL FIX: Parse the stringified JSON object for libraries if provided
if (libraries) {
try {
parsedLibraries = JSON.parse(libraries);
} catch (e) {
console.error(`Error parsing ${LIBRARIES_PARAM}: Expected stringified JSON. Received: ${libraries}`);
throw e; // Stop execution if parsing fails
}
}

console.log(`Verifying contract ${contractName} at address ${address}...`);
console.log(`Constructor arguments: ${constructorArguments.length > 0 ? constructorArguments.join(', ') : 'None'}`);

// 3. Call the external verification helper with the parsed libraries object
const result = await verifyContract(
contractName,
address,
constructorArguments,
parsedLibraries
);

console.log('Verification result received.');
return result;
}
);