|
| 1 | +import { task } from "hardhat/config"; |
| 2 | +import { NomicLabsHardhatPluginError } from "hardhat/plugins"; |
| 3 | +import { |
| 4 | + HardhatConfig, |
| 5 | + HardhatRuntimeEnvironment, |
| 6 | + TaskArguments, |
| 7 | +} from "hardhat/types"; |
| 8 | +import fetch from "node-fetch"; |
| 9 | +import path from "path"; |
| 10 | + |
| 11 | +task("blockscout-verify") |
| 12 | + .addParam("filePath", "File path to the contract") |
| 13 | + .addParam("address", "Deployed contract address") |
| 14 | + .setAction(async function ( |
| 15 | + args: TaskArguments, |
| 16 | + hre: HardhatRuntimeEnvironment |
| 17 | + ) { |
| 18 | + if (!validateArgs(args)) { |
| 19 | + throw new NomicLabsHardhatPluginError( |
| 20 | + "hardhat-blockscout-verify", |
| 21 | + "Missing args for this task" |
| 22 | + ); |
| 23 | + } |
| 24 | + const fileName = args.fileName; |
| 25 | + const address = args.address; |
| 26 | + const contractName = path.basename(fileName, ".sol"); |
| 27 | + if (!validateContractName(hre.config, contractName)) { |
| 28 | + throw new NomicLabsHardhatPluginError( |
| 29 | + "hardhat-blockscout-verify", |
| 30 | + "Contracts is not defined in Hardhat config" |
| 31 | + ); |
| 32 | + } |
| 33 | + if (!validateBlockscoutURL(hre.config)) { |
| 34 | + throw new NomicLabsHardhatPluginError( |
| 35 | + "hardhat-blockscout-verify", |
| 36 | + "Blockscout URL is not defined in Hardhat config" |
| 37 | + ); |
| 38 | + } |
| 39 | + console.log(`Task will process ${contractName} in ${fileName}`); |
| 40 | + const flattenContent = await hre.run("smart-flatten", { |
| 41 | + files: [fileName], |
| 42 | + }); |
| 43 | + console.log("File flatten has completed"); |
| 44 | + const verifyConfig = hre.config.blockscoutVerify!.contracts[contractName]; |
| 45 | + const params: any = { |
| 46 | + addressHash: address, |
| 47 | + name: contractName, |
| 48 | + compilerVersion: verifyConfig!.compilerVersion, |
| 49 | + optimization: verifyConfig!.optimization, |
| 50 | + contractSourceCode: flattenContent, |
| 51 | + autodetectConstructorArguments: "true", |
| 52 | + evmVersion: verifyConfig!.evmVersion, |
| 53 | + optimizationRuns: verifyConfig!.optimizationRuns, |
| 54 | + }; |
| 55 | + const blockscoutURL = hre.config.blockscoutVerify.blockscoutURL; |
| 56 | + try { |
| 57 | + console.log(`Sending file for verification to ${blockscoutURL}`); |
| 58 | + console.log(`Contract address is ${address}`); |
| 59 | + const verifyRes = await fetch( |
| 60 | + `${blockscoutURL}/api?module=contract&action=verify`, |
| 61 | + { |
| 62 | + method: "POST", |
| 63 | + body: JSON.stringify(params), |
| 64 | + headers: { |
| 65 | + "Content-Type": "application/json", |
| 66 | + }, |
| 67 | + } |
| 68 | + ); |
| 69 | + if (verifyRes.status === 200) { |
| 70 | + console.log(`${contractName} is verified`); |
| 71 | + } else { |
| 72 | + throw new NomicLabsHardhatPluginError( |
| 73 | + "hardhat-blockscout-verify", |
| 74 | + "Fail to verify contract" |
| 75 | + ); |
| 76 | + } |
| 77 | + } catch (e) { |
| 78 | + throw new NomicLabsHardhatPluginError( |
| 79 | + "hardhat-blockscout-verify", |
| 80 | + "Fail to verify contract" |
| 81 | + ); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | +function validateArgs(args: TaskArguments): boolean { |
| 86 | + return args.fileName !== null && args.address !== null; |
| 87 | +} |
| 88 | + |
| 89 | +function validateBlockscoutURL(hreConfig: HardhatConfig) { |
| 90 | + if (hreConfig.blockscoutVerify.blockscoutURL === null) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + let url; |
| 94 | + try { |
| 95 | + url = new URL(hreConfig.blockscoutVerify.blockscoutURL); |
| 96 | + } catch (_) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + return true; |
| 100 | +} |
| 101 | + |
| 102 | +function validateContractName(hreConfig: HardhatConfig, contractName: string) { |
| 103 | + return ( |
| 104 | + hreConfig.blockscoutVerify !== undefined && |
| 105 | + hreConfig.blockscoutVerify.contracts !== undefined && |
| 106 | + hreConfig.blockscoutVerify.contracts[contractName] !== undefined |
| 107 | + ); |
| 108 | +} |
0 commit comments