diff --git a/tasks/misc/verify-sc.ts b/tasks/misc/verify-sc.ts index ee1ce0e..198d21f 100644 --- a/tasks/misc/verify-sc.ts +++ b/tasks/misc/verify-sc.ts @@ -1,35 +1,67 @@ -import {task} from '@nomiclabs/buidler/config'; -import {verifyContract, checkVerification} from '../../helpers/etherscan-verification'; +// The 'task' utility is imported from the modern Hardhat package. +import { task } from 'hardhat/config'; +import { HardhatRuntimeEnvironment } from 'hardhat/types'; +import { verifyContract, checkVerification } from '../../helpers/etherscan-verification'; +// Define the interface for the parsed parameters with better types for libraries. interface VerifyParams { contractName: string; address: string; constructorArguments: string[]; + // Libraries are expected to be a JSON string, which we will parse into an object. libraries: 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 to automatically verify a deployed smart contract on Etherscan. + * It handles the required arguments including the complex libraries object and constructor arguments. + */ +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"}' + 'Stringified JSON object of linked libraries (e.g., {"Library1": "0xAddr..."})', + '{}' // Default to an empty object string to simplify parsing ) .addOptionalVariadicPositionalParam( 'constructorArguments', - 'arguments for contract constructor', + 'Arguments for the contract constructor.', [] ) .setAction( async ( - {contractName, address, constructorArguments = [], libraries}: VerifyParams, - localBRE + { contractName, address, constructorArguments, libraries }: VerifyParams, + hre: HardhatRuntimeEnvironment // Use the modern 'hre' (Hardhat Runtime Environment) ) => { - await localBRE.run('set-bre'); - + // 1. Check if Etherscan verification setup is complete (as per helper logic). checkVerification(); - const result = await verifyContract(contractName, address, constructorArguments, libraries); + // 2. Parse the libraries string into an object if it's not empty. + let parsedLibraries: Record = {}; + try { + if (libraries && libraries !== '{}') { + parsedLibraries = JSON.parse(libraries); + } + } catch (e) { + throw new Error(`Invalid JSON provided for 'libraries' parameter: ${libraries}. Error: ${e}`); + } + + console.log(`Verifying contract: ${contractName} at ${address}`); + console.log(`Constructor Arguments: ${constructorArguments.join(', ')}`); + console.log(`Linked Libraries: ${JSON.stringify(parsedLibraries)}`); + + // NOTE: The original `await localBRE.run('set-bre')` is removed as the HRE + // is already fully initialized when the task runs. + + // 3. Execute the core verification logic using the external helper function. + const result = await verifyContract( + contractName, + address, + constructorArguments, + parsedLibraries // Pass the parsed object + ); + return result; } );