"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable no-console */
const config_1 = require("hardhat/config");
const logger_1 = require("../../ts/logger");
const ContractStorage_1 = require("../helpers/ContractStorage");
const ContractVerifier_1 = require("../helpers/ContractVerifier");
/**
 * Main verification task which runs hardhat-etherscan task for all the deployed contract.
 */
(0, config_1.task)("verify-full", "Verify contracts listed in storage")
    .addFlag("force", "Ignore verified status")
    .setAction(async ({ force = false }, hre) => {
    const storage = ContractStorage_1.ContractStorage.getInstance();
    const verifier = new ContractVerifier_1.ContractVerifier(hre);
    const addressList = [];
    const entryList = [];
    let index = 0;
    const addEntry = (address, entry) => {
        if (!entry.verify) {
            return;
        }
        addressList.push(address);
        entryList.push(entry);
        index += 1;
    };
    const instances = storage.getInstances(hre.network.name);
    instances.forEach(([key, entry]) => {
        if (entry.id.includes("Poseidon")) {
            return;
        }
        addEntry(key, entry);
    });
    (0, logger_1.logMagenta)({ text: "======================================================================" });
    (0, logger_1.logMagenta)({ text: "======================================================================" });
    (0, logger_1.logMagenta)({ text: `Verification batch with ${addressList.length} entries of ${index} total.` });
    (0, logger_1.logMagenta)({ text: "======================================================================" });
    const summary = [];
    for (let i = 0; i < addressList.length; i += 1) {
        const address = addressList[i];
        const entry = entryList[i];
        const params = entry.verify;
        (0, logger_1.logMagenta)({ text: "\n======================================================================" });
        (0, logger_1.logMagenta)({ text: `[${i}/${addressList.length}] Verify contract: ${entry.id} ${address}` });
        (0, logger_1.logMagenta)({ text: `\tArgs: ${params?.args}` });
        const verifiedEntity = storage.getVerified(address, hre.network.name);
        if (!force && verifiedEntity) {
            (0, logger_1.logYellow)({ text: (0, logger_1.warning)("Already verified") });
        }
        else {
            if (params?.impl) {
                // eslint-disable-next-line no-await-in-loop
                const [ok, err] = await verifier.verify(params.impl, "[]", params.name, params.libraries);
                if (ok) {
                    storage.setVerified(params.impl, hre.network.name, true);
                }
                else {
                    summary.push(`${params.impl} ${entry.id} implementation ${params.impl}: ${err}`);
                }
            }
            // eslint-disable-next-line no-await-in-loop
            const [ok, err] = await verifier.verify(address, params?.args ?? "[]", params?.name, params?.libraries, params?.impl);
            if (ok) {
                storage.setVerified(address, hre.network.name, true);
            }
            else {
                summary.push(`${address} ${entry.id}: ${err}`);
            }
        }
    }
    (0, logger_1.logGreen)({ text: "\n======================================================================" });
    (0, logger_1.logGreen)({ text: `Verification batch has finished with ${summary.length} issue(s).` });
    (0, logger_1.logGreen)({ text: "======================================================================" });
    (0, logger_1.logGreen)({ text: summary.join("\n") });
    (0, logger_1.logGreen)({ text: "======================================================================" });
});
//# sourceMappingURL=verifyFull.js.map