"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const hardhat_1 = require("hardhat");
const deploymentConfig_1 = require("../helpers/deploy/deploymentConfig");
const deploymentUtils_1 = require("../helpers/deploy/deploymentUtils");
const utils_1 = require("../helpers/utils");
const BNB_MAINNET_GUARDIAN = "0x1C2CAc6ec528c20800B2fe734820D87b581eAA6B";
const BNB_TESTNET_GUARDIAN = "0x2Ce1d0ffD7E869D9DF33e28552b12DdDed326706";
const configureCommands = (target, hre) => __awaiter(void 0, void 0, void 0, function* () {
    const networkName = hre.network.name;
    const commands = yield Promise.all(deploymentConfig_1.config[networkName].methods.map((entry) => __awaiter(void 0, void 0, void 0, function* () {
        const { method, args } = entry;
        return {
            contract: target,
            signature: method,
            argTypes: (0, utils_1.getArgTypesFromSignature)(method),
            parameters: args,
            value: 0,
        };
    })));
    return commands.flat();
});
const configureAccessControls = (methods, accessControlManagerAddress, callerAddress, targetAddress) => __awaiter(void 0, void 0, void 0, function* () {
    const commands = yield Promise.all(methods.map((method) => __awaiter(void 0, void 0, void 0, function* () {
        return [
            {
                contract: accessControlManagerAddress,
                signature: "giveCallPermission(address,string,address)",
                argTypes: ["address", "string", "address"],
                parameters: [targetAddress, method, callerAddress],
                value: 0,
            },
        ];
    })));
    return commands.flat();
});
const func = function (hre) {
    return __awaiter(this, void 0, void 0, function* () {
        const { deployments, getNamedAccounts } = hre;
        const { deploy } = deployments;
        const { deployer } = yield getNamedAccounts();
        const acmAddress = (yield hardhat_1.ethers.getContract("AccessControlManager")).address;
        const normalTimelockAddress = (yield hardhat_1.ethers.getContract("NormalTimelock")).address;
        const fastTrackTimelockAddress = (yield hardhat_1.ethers.getContract("FastTrackTimelock")).address;
        const criticalTimelockAddress = (yield hardhat_1.ethers.getContract("CriticalTimelock")).address;
        const BNB_GUARDIAN = hre.network.name === "bscmainnet" ? BNB_MAINNET_GUARDIAN : BNB_TESTNET_GUARDIAN;
        const OmnichainProposalSender = yield deploy("OmnichainProposalSender", {
            from: deployer,
            args: [yield (0, deploymentUtils_1.getLzEndpoint)(hre.network.name), acmAddress],
            log: true,
            autoMine: true,
        });
        const omnichainProposalSender = yield hardhat_1.ethers.getContractAt("OmnichainProposalSender", OmnichainProposalSender.address, hardhat_1.ethers.provider.getSigner(deployer));
        if ((yield omnichainProposalSender.owner()) === deployer) {
            const tx = yield omnichainProposalSender.transferOwnership(normalTimelockAddress);
            yield tx.wait();
            console.log(`Omnichain Proposal Sender ${deployer} sucessfully changed to ${normalTimelockAddress}.`);
        }
        const commands = [
            ...(yield configureAccessControls(deploymentConfig_1.OmnichainProposalSenderNormalMethods, acmAddress, normalTimelockAddress, OmnichainProposalSender.address)),
            ...(yield configureAccessControls(deploymentConfig_1.OmnichainProposalSenderFasttrackMethods, acmAddress, fastTrackTimelockAddress, OmnichainProposalSender.address)),
            ...(yield configureAccessControls(deploymentConfig_1.OmnichainProposalSenderCriticalMethods, acmAddress, criticalTimelockAddress, OmnichainProposalSender.address)),
            ...(yield configureAccessControls(deploymentConfig_1.OmnichainProposalSenderGuardianMethods, acmAddress, BNB_GUARDIAN, OmnichainProposalSender.address)),
            ...(yield configureCommands(OmnichainProposalSender.address, hre)),
        ];
        console.log("Please propose a VIP with the following commands:");
        console.log(JSON.stringify(commands.map(c => ({ target: c.contract, signature: c.signature, params: c.parameters, value: c.value }))));
    });
};
func.tags = ["OmnichainProposalSender"];
func.skip = (hre) => __awaiter(void 0, void 0, void 0, function* () { return !(hre.network.name === "bsctestnet" || hre.network.name === "bscmainnet") && hre.network.name !== "hardhat"; });
func.dependencies = ["mocks"];
exports.default = func;
//# sourceMappingURL=003-omnichain-source.js.map