"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("../../../ts/logger");
const constants_1 = require("../../helpers/constants");
const ContractStorage_1 = require("../../helpers/ContractStorage");
const Deployment_1 = require("../../helpers/Deployment");
const types_1 = require("../../helpers/types");
const DEFAULT_INITIAL_VOICE_CREDITS = 99;
const deployment = Deployment_1.Deployment.getInstance();
const storage = ContractStorage_1.ContractStorage.getInstance();
/**
 * Deploy step registration and task itself
 */
deployment.deployTask(constants_1.EDeploySteps.InitialVoiceCreditProxy, "Deploy initial voice credit proxy").then((task) => task.setAction(async ({ incremental }, hre) => {
    deployment.setHre(hre);
    const deployer = await deployment.getDeployer();
    const { deployConstantInitialVoiceCreditProxy, deployERC20VotesInitialVoiceCreditProxy } = await Promise.resolve().then(() => __importStar(require("../../../ts/deploy")));
    const voiceCreditProxyType = deployment.getDeployConfigField(types_1.EContracts.Poll, "initialVoiceCreditProxy");
    const needDeploy = deployment.getDeployConfigField(voiceCreditProxyType, "deploy");
    if (needDeploy === false) {
        return;
    }
    const skipDeployConstantInitialVoiceCreditProxy = voiceCreditProxyType !== types_1.EInitialVoiceCreditProxies.Constant;
    const skipDeployERC20VotesInitialVoiceCreditProxy = voiceCreditProxyType !== types_1.EInitialVoiceCreditProxies.ERC20Votes;
    if (!skipDeployConstantInitialVoiceCreditProxy) {
        const constantInitialVoiceCreditProxyContractAddress = storage.getAddress(types_1.EInitialVoiceCreditProxies.Constant, hre.network.name);
        if (incremental && constantInitialVoiceCreditProxyContractAddress) {
            // eslint-disable-next-line no-console
            (0, logger_1.logGreen)({ text: (0, logger_1.info)(`Skipping deployment of the ${types_1.EContracts.ConstantInitialVoiceCreditProxy} contract`) });
            return;
        }
        const amount = deployment.getDeployConfigField(types_1.EContracts.ConstantInitialVoiceCreditProxy, "amount") ??
            DEFAULT_INITIAL_VOICE_CREDITS;
        const constantProxyFactoryAddress = storage.getAddress(types_1.EInitialVoiceCreditProxiesFactories.Constant, hre.network.name);
        const constantProxyFactory = constantProxyFactoryAddress
            ? await deployment.getContract({
                name: types_1.EContracts.ConstantInitialVoiceCreditProxyFactory,
                address: constantProxyFactoryAddress,
                signer: deployer,
            })
            : undefined;
        const [constantInitialVoiceCreditProxyContract, constantInitialVoiceCreditProxyContractFactory] = await deployConstantInitialVoiceCreditProxy({ amount }, deployer, constantProxyFactory, true);
        const constantImplementation = await constantInitialVoiceCreditProxyContractFactory.IMPLEMENTATION();
        await Promise.all([
            storage.register({
                id: types_1.EInitialVoiceCreditProxies.Constant,
                contract: constantInitialVoiceCreditProxyContract,
                implementation: constantImplementation,
                args: [],
                network: hre.network.name,
            }),
            storage.register({
                id: types_1.EInitialVoiceCreditProxiesFactories.Constant,
                contract: constantInitialVoiceCreditProxyContractFactory,
                args: [],
                network: hre.network.name,
            }),
        ]);
    }
    if (!skipDeployERC20VotesInitialVoiceCreditProxy) {
        const erc20VotesInitialVoiceCreditProxyContractAddress = storage.getAddress(types_1.EInitialVoiceCreditProxies.ERC20Votes, hre.network.name);
        if (incremental && erc20VotesInitialVoiceCreditProxyContractAddress) {
            // eslint-disable-next-line no-console
            (0, logger_1.logGreen)({ text: (0, logger_1.info)(`Skipping deployment of the ${types_1.EInitialVoiceCreditProxies.ERC20Votes} contract`) });
            return;
        }
        // get args
        const tokenAddress = deployment.getDeployConfigField(types_1.EContracts.ERC20VotesInitialVoiceCreditProxy, "token");
        const snapshotBlock = deployment.getDeployConfigField(types_1.EContracts.ERC20VotesInitialVoiceCreditProxy, "snapshotBlock");
        const factor = deployment.getDeployConfigField(types_1.EContracts.ERC20VotesInitialVoiceCreditProxy, "factor");
        const erc20VotesProxyFactoryAddress = storage.getAddress(types_1.EInitialVoiceCreditProxiesFactories.ERC20Votes, hre.network.name);
        const erc20VotesProxyFactory = erc20VotesProxyFactoryAddress
            ? await deployment.getContract({
                name: types_1.EContracts.ERC20VotesInitialVoiceCreditProxyFactory,
                address: erc20VotesProxyFactoryAddress,
                signer: deployer,
            })
            : undefined;
        const [erc20VotesInitialVoiceCreditProxyContract, erc20VotesInitialVoiceCreditProxyContractFactory] = await deployERC20VotesInitialVoiceCreditProxy({
            token: tokenAddress,
            snapshotBlock: BigInt(snapshotBlock),
            factor: BigInt(factor),
        }, deployer, erc20VotesProxyFactory, true);
        const erc20VotesImplementation = await erc20VotesInitialVoiceCreditProxyContractFactory.IMPLEMENTATION();
        await Promise.all([
            storage.register({
                id: types_1.EInitialVoiceCreditProxies.ERC20Votes,
                contract: erc20VotesInitialVoiceCreditProxyContract,
                implementation: erc20VotesImplementation,
                args: [],
                network: hre.network.name,
            }),
            storage.register({
                id: types_1.EInitialVoiceCreditProxiesFactories.ERC20Votes,
                contract: erc20VotesInitialVoiceCreditProxyContractFactory,
                args: [],
                network: hre.network.name,
            }),
        ]);
    }
}));
//# sourceMappingURL=01-voiceCreditProxy.js.map