"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const hardhat_1 = __importDefault(require("hardhat"));
const deploymentConfig_1 = require("../helpers/deploymentConfig");
const func = async function ({ getChainId, getNamedAccounts, deployments, network, }) {
    const { deploy } = deployments;
    const { deployer } = await getNamedAccounts();
    let accessControlManager;
    if (!network.live) {
        await deploy("AccessControlManager", {
            from: deployer,
            args: [],
            log: true,
            autoMine: true,
        });
        accessControlManager = await hardhat_1.default.ethers.getContract("AccessControlManager");
    }
    let vai;
    if (!network.live) {
        await deploy("VAI", {
            from: deployer,
            log: true,
            autoMine: true,
            args: [await getChainId()],
        });
        vai = await hardhat_1.default.ethers.getContract("VAI");
    }
    const accessControlManagerAddress = network.live ? deploymentConfig_1.ADDRESSES[network.name].acm : accessControlManager?.address;
    const proxyOwnerAddress = network.live ? deploymentConfig_1.ADDRESSES[network.name].timelock : deployer;
    const vaiAddress = network.live ? deploymentConfig_1.ADDRESSES[network.name].VAIAddress : vai?.address;
    const vbnbAddress = network.live ? deploymentConfig_1.ADDRESSES[network.name].vBNBAddress : deployer;
    const timelock = network.live ? deploymentConfig_1.ADDRESSES[network.name].timelock : deployer;
    const defaultProxyAdmin = await hardhat_1.default.artifacts.readArtifact("hardhat-deploy/solc_0.8/openzeppelin/proxy/transparent/ProxyAdmin.sol:ProxyAdmin");
    await deploy("BoundValidator", {
        from: deployer,
        log: true,
        deterministicDeployment: false,
        skipIfAlreadyDeployed: true,
        args: [],
        proxy: {
            owner: proxyOwnerAddress,
            proxyContract: "OptimizedTransparentUpgradeableProxy",
            execute: {
                methodName: "initialize",
                args: [accessControlManagerAddress],
            },
            viaAdminContract: {
                name: "DefaultProxyAdmin",
                artifact: defaultProxyAdmin,
            },
        },
    });
    const boundValidator = await hardhat_1.default.ethers.getContract("BoundValidator");
    await deploy("ResilientOracle", {
        from: deployer,
        log: true,
        deterministicDeployment: false,
        skipIfAlreadyDeployed: true,
        args: [vbnbAddress, vaiAddress, boundValidator.address],
        proxy: {
            owner: proxyOwnerAddress,
            proxyContract: "OptimizedTransparentUpgradeableProxy",
            execute: {
                methodName: "initialize",
                args: [accessControlManagerAddress],
            },
            viaAdminContract: {
                name: "DefaultProxyAdmin",
                artifact: defaultProxyAdmin,
            },
        },
    });
    const sequencer = deploymentConfig_1.SEQUENCER[network.name];
    let contractName = "ChainlinkOracle";
    if (sequencer !== undefined)
        contractName = "SequencerChainlinkOracle";
    await deploy(contractName, {
        contract: network.live ? contractName : "MockChainlinkOracle",
        from: deployer,
        log: true,
        deterministicDeployment: false,
        skipIfAlreadyDeployed: true,
        args: sequencer ? [sequencer] : [],
        proxy: {
            owner: proxyOwnerAddress,
            proxyContract: "OptimizedTransparentUpgradeableProxy",
            execute: {
                methodName: "initialize",
                args: network.live ? [accessControlManagerAddress] : [],
            },
            viaAdminContract: {
                name: "DefaultProxyAdmin",
                artifact: defaultProxyAdmin,
            },
        },
    });
    const resilientOracle = await hardhat_1.default.ethers.getContract("ResilientOracle");
    const chainlinkOracle = await hardhat_1.default.ethers.getContract(contractName);
    await accessControlManager?.giveCallPermission(chainlinkOracle.address, "setTokenConfig(TokenConfig)", deployer);
    await accessControlManager?.giveCallPermission(resilientOracle.address, "setTokenConfig(TokenConfig)", deployer);
    const resilientOracleOwner = await resilientOracle.owner();
    const chainlinkOracleOwner = await chainlinkOracle.owner();
    const boundValidatorOwner = await boundValidator.owner();
    if (resilientOracleOwner === deployer) {
        await resilientOracle.transferOwnership(timelock);
        console.log(`Ownership of ResilientOracle transfered from deployer to Timelock (${timelock})`);
    }
    if (chainlinkOracleOwner === deployer) {
        await chainlinkOracle.transferOwnership(timelock);
        console.log(`Ownership of ChainlinkOracle transfered from deployer to Timelock (${timelock})`);
    }
    if (boundValidatorOwner === deployer) {
        await boundValidator.transferOwnership(timelock);
        console.log(`Ownership of BoundValidator transfered from deployer to Timelock (${timelock})`);
    }
};
exports.default = func;
func.tags = ["deploy"];
//# sourceMappingURL=1-deploy-oracles.js.map