diff --git a/tasks/misc/verify-sc.ts b/tasks/misc/verify-sc.ts index ee1ce0e..cd4834c 100644 --- a/tasks/misc/verify-sc.ts +++ b/tasks/misc/verify-sc.ts @@ -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; } );