"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());
    });
};
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 = function ({ getChainId, getNamedAccounts, deployments, network, }) {
    return __awaiter(this, void 0, void 0, function* () {
        const { deploy } = deployments;
        const { deployer } = yield getNamedAccounts();
        let accessControlManager;
        if (!network.live) {
            yield deploy("AccessControlManagerScenario", {
                from: deployer,
                args: [],
                log: true,
                autoMine: true,
            });
            accessControlManager = yield hardhat_1.default.ethers.getContract("AccessControlManagerScenario");
        }
        let vai;
        if (!network.live) {
            yield deploy("VAIScenario", {
                from: deployer,
                log: true,
                autoMine: true,
                args: [yield getChainId()],
            });
            vai = yield hardhat_1.default.ethers.getContract("VAIScenario");
        }
        const accessControlManagerAddress = network.live ? deploymentConfig_1.ADDRESSES[network.name].acm : accessControlManager === null || accessControlManager === void 0 ? void 0 : accessControlManager.address;
        const proxyOwnerAddress = network.live ? deploymentConfig_1.ADDRESSES[network.name].timelock : deployer;
        const vaiAddress = network.live ? deploymentConfig_1.ADDRESSES[network.name].VAIAddress : vai === null || vai === void 0 ? void 0 : 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 = yield hardhat_1.default.artifacts.readArtifact("hardhat-deploy/solc_0.8/openzeppelin/proxy/transparent/ProxyAdmin.sol:ProxyAdmin");
        yield 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 = yield hardhat_1.default.ethers.getContract("BoundValidator");
        yield 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";
        yield 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 = yield hardhat_1.default.ethers.getContract("ResilientOracle");
        const chainlinkOracle = yield hardhat_1.default.ethers.getContract(contractName);
        yield (accessControlManager === null || accessControlManager === void 0 ? void 0 : accessControlManager.giveCallPermission(chainlinkOracle.address, "setTokenConfig(TokenConfig)", deployer));
        yield (accessControlManager === null || accessControlManager === void 0 ? void 0 : accessControlManager.giveCallPermission(resilientOracle.address, "setTokenConfig(TokenConfig)", deployer));
        const resilientOracleOwner = yield resilientOracle.owner();
        const chainlinkOracleOwner = yield chainlinkOracle.owner();
        const boundValidatorOwner = yield boundValidator.owner();
        if (resilientOracleOwner === deployer) {
            yield resilientOracle.transferOwnership(timelock);
            console.log(`Ownership of ResilientOracle transfered from deployer to Timelock (${timelock})`);
        }
        if (chainlinkOracleOwner === deployer) {
            yield chainlinkOracle.transferOwnership(timelock);
            console.log(`Ownership of ChainlinkOracle transfered from deployer to Timelock (${timelock})`);
        }
        if (boundValidatorOwner === deployer) {
            yield 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