"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hardhat_1 = require("hardhat");
const deploymentConfig_1 = require("../helpers/deploymentConfig");
const utils_1 = require("../helpers/utils");
const utils_2 = require("../helpers/utils");
const configureAccessControls = async (methods, accessControlManagerAddress, caller, target, hre) => {
    const commands = await Promise.all(methods.map(async (method) => {
        const callerAddress = await (0, utils_1.toAddress)(caller, hre);
        const targetAddress = await (0, utils_1.toAddress)(target, hre);
        return [
            {
                contract: accessControlManagerAddress,
                signature: "giveCallPermission(address,string,address)",
                argTypes: ["address", "string", "address"],
                parameters: [targetAddress, method, callerAddress],
                value: 0,
            },
        ];
    }));
    return commands.flat();
};
const configureBridgeCommands = async (target, hre) => {
    const commands = await Promise.all(deploymentConfig_1.bridgeConfig[hre.network.name].methods.map(async (entry) => {
        const { method, args } = entry;
        return {
            contract: target,
            signature: method,
            argTypes: (0, utils_2.getArgTypesFromSignature)(method),
            parameters: args,
            value: 0,
        };
    }));
    return commands.flat();
};
const func = async function (hre) {
    const { deployments, getNamedAccounts } = hre;
    const { deploy } = deployments;
    const { deployer } = await getNamedAccounts();
    const preconfiguredAddresses = await (0, deploymentConfig_1.getPreConfiguredAddresses)(hre.network.name);
    const accessControlManager = await hardhat_1.ethers.getContract("AccessControlManager");
    const normalTimelock = await hardhat_1.ethers.getContract("NormalTimelock");
    const resilientOracle = await hardhat_1.ethers.getContract("ResilientOracle");
    const XVS = await hardhat_1.ethers.getContract("XVS");
    const XVSProxyOFTSrc = await deploy("XVSProxyOFTSrc", {
        from: deployer,
        contract: "XVSProxyOFTSrc",
        args: [XVS.address, 8, preconfiguredAddresses.LzEndpoint, resilientOracle.address],
        autoMine: true,
        log: true,
    });
    const XVSBridgeAdmin = await deploy("XVSBridgeAdmin", {
        from: deployer,
        args: [XVSProxyOFTSrc.address],
        contract: "XVSBridgeAdmin",
        proxy: {
            owner: normalTimelock.address,
            proxyContract: "OpenZeppelinTransparentProxy",
            execute: {
                methodName: "initialize",
                args: [accessControlManager.address],
            },
            upgradeIndex: 0,
        },
        log: true,
        autoMine: true,
    });
    const bridge = await hardhat_1.ethers.getContract("XVSProxyOFTSrc");
    const bridgeAdmin = await hardhat_1.ethers.getContract("XVSBridgeAdmin");
    const removeArray = new Array(deploymentConfig_1.xvsBridgeMethodsSrc.length).fill(true);
    let tx = await bridgeAdmin.upsertSignature(deploymentConfig_1.xvsBridgeMethodsSrc, removeArray);
    await tx.wait();
    tx = await bridge.transferOwnership(XVSBridgeAdmin.address);
    await tx.wait();
    tx = await bridgeAdmin.transferOwnership(normalTimelock.address);
    await tx.wait();
    console.log(`Bridge Admin owner ${deployer} sucessfully changed to ${normalTimelock.address}. Please accept the ownership.`);
    const commands = [
        ...(await configureAccessControls(deploymentConfig_1.xvsBridgeMethodsSrc, accessControlManager.address, normalTimelock.address, XVSBridgeAdmin.address, hre)),
        ...(await configureAccessControls(deploymentConfig_1.XVSBridgeAdminMethods, accessControlManager.address, normalTimelock.address, XVSBridgeAdmin.address, hre)),
        {
            contract: XVSBridgeAdmin.address,
            signature: "acceptOwnership()",
            parameters: [],
            value: 0,
        },
        {
            contract: XVSBridgeAdmin.address,
            signature: "setTrustedRemoteAddress(uint16,bytes)",
            parameters: [preconfiguredAddresses.LzVirtualChainIdL, "0xDestAddress"],
            value: 0,
        },
        ...(await configureBridgeCommands(XVSBridgeAdmin.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 = ["XVSBridgeSrc"];
func.skip = async (hre) => !(hre.network.name === "bsctestnet" || hre.network.name === "bscmainnet");
exports.default = func;
