"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 constants_1 = require("../helpers/deploy/constants");
const deploymentUtils_1 = require("../helpers/deploy/deploymentUtils");
const func = function (hre) {
    return __awaiter(this, void 0, void 0, function* () {
        const { deployments, getNamedAccounts } = hre;
        const { deploy } = deployments;
        const { deployer } = yield getNamedAccounts();
        const networkName = hre.network.name;
        const normalTimelockAddress = (yield hre.ethers.getContract("NormalTimelock")).address;
        const accessControlManager = yield hre.ethers.getContract("AccessControlManager");
        const corePoolComptroller = (yield hre.ethers.getContractOrNull("Unitroller")) || {
            address: "0x0000000000000000000000000000000000000001",
        };
        const defaultProxyAdmin = yield hre.artifacts.readArtifact("hardhat-deploy/solc_0.8/openzeppelin/proxy/transparent/ProxyAdmin.sol:ProxyAdmin");
        yield deploy("RiskOracle", {
            from: deployer,
            log: true,
            args: [],
            proxy: {
                owner: networkName === "hardhat" ? deployer : normalTimelockAddress,
                proxyContract: "OptimizedTransparentUpgradeableProxy",
                execute: {
                    methodName: "initialize",
                    args: [accessControlManager.address],
                },
                viaAdminContract: {
                    name: "DefaultProxyAdmin",
                    artifact: defaultProxyAdmin,
                },
            },
            skipIfAlreadyDeployed: true,
        });
        const riskOracle = yield hre.ethers.getContract("RiskOracle");
        if ((yield riskOracle.owner()) === deployer) {
            yield riskOracle.transferOwnership(normalTimelockAddress);
        }
        const lzEndpoint = yield (0, deploymentUtils_1.getLzV2Endpoint)(networkName);
        const sourceChainId = constants_1.LZ_V2_EID[networkName];
        yield deploy("RiskStewardReceiver", {
            from: deployer,
            log: true,
            args: [riskOracle.address, lzEndpoint, sourceChainId],
            proxy: {
                owner: networkName === "hardhat" ? deployer : normalTimelockAddress,
                proxyContract: "OptimizedTransparentUpgradeableProxy",
                execute: {
                    methodName: "initialize",
                    args: [accessControlManager.address, normalTimelockAddress],
                },
                viaAdminContract: {
                    name: "DefaultProxyAdmin",
                    artifact: defaultProxyAdmin,
                },
            },
            skipIfAlreadyDeployed: true,
        });
        const riskStewardReceiver = yield hre.ethers.getContract("RiskStewardReceiver");
        if ((yield riskStewardReceiver.owner()) === deployer) {
            yield riskStewardReceiver.transferOwnership(normalTimelockAddress);
        }
        yield deploy("MarketCapsRiskSteward", {
            from: deployer,
            log: true,
            args: [riskStewardReceiver.address],
            proxy: {
                owner: networkName === "hardhat" ? deployer : normalTimelockAddress,
                proxyContract: "OptimizedTransparentUpgradeableProxy",
                execute: {
                    methodName: "initialize",
                    args: [accessControlManager.address],
                },
                viaAdminContract: {
                    name: "DefaultProxyAdmin",
                    artifact: defaultProxyAdmin,
                },
            },
            skipIfAlreadyDeployed: true,
        });
        const marketCapsSteward = yield hre.ethers.getContract("MarketCapsRiskSteward");
        if ((yield marketCapsSteward.owner()) === deployer) {
            yield marketCapsSteward.transferOwnership(normalTimelockAddress);
        }
        yield deploy("CollateralFactorsRiskSteward", {
            from: deployer,
            log: true,
            args: [corePoolComptroller.address, riskStewardReceiver.address],
            proxy: {
                owner: networkName === "hardhat" ? deployer : normalTimelockAddress,
                proxyContract: "OptimizedTransparentUpgradeableProxy",
                execute: {
                    methodName: "initialize",
                    args: [accessControlManager.address],
                },
                viaAdminContract: {
                    name: "DefaultProxyAdmin",
                    artifact: defaultProxyAdmin,
                },
            },
            skipIfAlreadyDeployed: true,
        });
        const collateralFactorsSteward = yield hre.ethers.getContract("CollateralFactorsRiskSteward");
        if ((yield collateralFactorsSteward.owner()) === deployer) {
            yield collateralFactorsSteward.transferOwnership(normalTimelockAddress);
        }
        yield deploy("IRMRiskSteward", {
            from: deployer,
            log: true,
            args: [corePoolComptroller.address, riskStewardReceiver.address],
            proxy: {
                owner: networkName === "hardhat" ? deployer : normalTimelockAddress,
                proxyContract: "OptimizedTransparentUpgradeableProxy",
                execute: {
                    methodName: "initialize",
                    args: [accessControlManager.address],
                },
                viaAdminContract: {
                    name: "DefaultProxyAdmin",
                    artifact: defaultProxyAdmin,
                },
            },
            skipIfAlreadyDeployed: true,
        });
        const irmRiskSteward = yield hre.ethers.getContract("IRMRiskSteward");
        if ((yield irmRiskSteward.owner()) === deployer) {
            yield irmRiskSteward.transferOwnership(normalTimelockAddress);
        }
    });
};
func.tags = ["risk-stewards", "bsc"];
func.skip = (hre) => __awaiter(void 0, void 0, void 0, function* () { return !(hre.network.name === "bsctestnet" || hre.network.name === "bscmainnet" || hre.network.name == "hardhat"); });
exports.default = func;
//# sourceMappingURL=012-risk-stewards.js.map