"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDiamondUpgradeProposal = exports.optionDefinitions = void 0;
const fs_1 = require("fs");
const dotenv_1 = require("dotenv");
const ethers_1 = require("ethers");
const command_line_args_1 = __importDefault(require("command-line-args"));
const getProposedFacetCuts_1 = require("../../getProposedFacetCuts");
const config_1 = require("../../config");
const helpers_1 = require("./helpers");
(0, dotenv_1.config)();
exports.optionDefinitions = [
    { name: "env", type: String },
    { name: "network", type: String },
    { name: "chains", type: Number, multiple: true },
];
const getDiamondUpgradeProposal = async () => {
    let cmdArgs;
    try {
        cmdArgs = (0, command_line_args_1.default)(exports.optionDefinitions);
    }
    catch (err) {
        throw new Error(`Parsing arguments failed, cmdArgs: ${process.argv}`);
    }
    // Validate command line arguments
    // const chains = [1, 10, 56, 100, 137, 42161];
    const { env: _env, chains: _chains, network: _network } = cmdArgs;
    const network = (_network ?? process.env.NETWORK ?? "testnet");
    const env = _env ?? process.env.ENV ?? "staging";
    const chains = _chains ?? helpers_1.SUPPORTED_CHAINS[network];
    if (!["testnet", "mainnet"].includes(network)) {
        throw new Error(`Environment should be either staging or production, env: ${env}`);
    }
    if (!["staging", "production"].includes(env)) {
        throw new Error(`Environment should be either staging or production, env: ${env}`);
    }
    const chainCuts = { chains };
    for (const chain of chains) {
        // get the hardhat config
        const config = Object.values(config_1.hardhatNetworks).find((c) => c.chainId === chain);
        if (!config?.url) {
            throw new Error(`Could not find url for chainId: ${chain}`);
        }
        // get provider
        const provider = new ethers_1.providers.JsonRpcProvider(config.url);
        // get all the deployments
        const deployments = (0, helpers_1.getDeployments)(`${chain}`, env);
        // generate the facet options
        const { Connext, ...facets } = deployments;
        const facetOptions = Object.entries(facets).map(([name, deployment]) => {
            return {
                name,
                contract: new ethers_1.Contract(deployment.address, deployment.abi, provider),
            };
        });
        const connext = new ethers_1.Contract(Connext.address, Connext.abi, provider);
        // get the proposed cut
        const namedCuts = await (0, getProposedFacetCuts_1.getProposedFacetCuts)(facetOptions, connext);
        // write to file without `name` field (matching contract call)
        chainCuts[chain] = {
            // default 1000 blocks into cut
            forkBlock: helpers_1.FORK_BLOCKS[chain],
            numberOfCuts: namedCuts.length,
            connext: Connext.address,
            proposal: namedCuts.map((cut) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { name, ...rest } = cut;
                return rest;
            }),
        };
    }
    // write cuts output to json file
    (0, fs_1.writeFileSync)("cuts.json", JSON.stringify(chainCuts), { encoding: "utf-8" });
};
exports.getDiamondUpgradeProposal = getDiamondUpgradeProposal;
//# sourceMappingURL=propose.js.map