"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hardhat_1 = require("hardhat");
const deploymentConfig_1 = require("../helpers/deploymentConfig");
var PendleRateKind;
(function (PendleRateKind) {
    PendleRateKind[PendleRateKind["PT_TO_ASSET"] = 0] = "PT_TO_ASSET";
    PendleRateKind[PendleRateKind["PT_TO_SY"] = 1] = "PT_TO_SY";
})(PendleRateKind || (PendleRateKind = {}));
const deployPendleOracle = async (deployments, deployer, name, args, proxyOwnerAddress, defaultProxyAdmin) => {
    const { deploy } = deployments;
    return deploy(name, {
        contract: "PendleOracle",
        from: deployer,
        log: true,
        deterministicDeployment: false,
        args,
        proxy: {
            owner: proxyOwnerAddress,
            proxyContract: "OptimizedTransparentUpgradeableProxy",
            viaAdminContract: {
                name: "DefaultProxyAdmin",
                artifact: defaultProxyAdmin,
            },
        },
        skipIfAlreadyDeployed: true,
    });
};
const func = async ({ getNamedAccounts, deployments, network, artifacts, }) => {
    const { deployer } = await getNamedAccounts();
    const oracle = await hardhat_1.ethers.getContract("ResilientOracle");
    const proxyOwnerAddress = network.live ? deploymentConfig_1.ADDRESSES[network.name].timelock : deployer;
    const defaultProxyAdmin = await artifacts.readArtifact("hardhat-deploy/solc_0.8/openzeppelin/proxy/transparent/ProxyAdmin.sol:ProxyAdmin");
    const addresses = deploymentConfig_1.ADDRESSES[network.name];
    const fallbackAddress = "0x0000000000000000000000000000000000000001";
    const deployOracles = async (oracleConfig) => {
        await Promise.all(oracleConfig.map(async ({ name, market, ptToken, underlyingToken, ptOracle, TWAPDuration, pendleRateKind }) => {
            const ptOracleAddress = ptOracle || (await hardhat_1.ethers.getContract("MockPendlePtOracle")).address;
            await deployPendleOracle(deployments, deployer, name, [
                market || fallbackAddress,
                ptOracleAddress,
                pendleRateKind,
                ptToken,
                underlyingToken,
                oracle.address,
                TWAPDuration,
            ], proxyOwnerAddress, defaultProxyAdmin);
        }));
    };
    const oracleConfig = [
        {
            name: "PendleOracle-PT-weETH-26DEC2024",
            market: addresses.PTweETH_26DEC2024_Market,
            ptToken: addresses.PTweETH_26DEC2024,
            underlyingToken: addresses.WETH,
            ptOracle: addresses.PTOracle || (await hardhat_1.ethers.getContract("MockPendleOracle")).address,
            TWAPDuration: 1800,
            pendleRateKind: PendleRateKind.PT_TO_ASSET,
        },
        {
            name: "PendleOracle_PT_USDe_27MAR2025",
            market: addresses.PTUSDe_27MAR2025_Market,
            ptToken: addresses.PTUSDe_27MAR2025,
            underlyingToken: addresses.USDe,
            ptOracle: network.name === "sepolia"
                ? (await hardhat_1.ethers.getContract("MockPendleOracle_PT_USDe_27MAR2025")).address
                : addresses.PTOracle,
            TWAPDuration: 1800,
            pendleRateKind: PendleRateKind.PT_TO_ASSET,
        },
        {
            name: "PendleOracle_PT_sUSDe_27MAR2025",
            market: addresses.PTsUSDe_27MAR2025_Market,
            ptToken: addresses.PTsUSDe_27MAR2025,
            underlyingToken: addresses.USDe,
            ptOracle: network.name === "sepolia"
                ? (await hardhat_1.ethers.getContract("MockPendleOracle_PT_sUSDe_27MAR2025")).address
                : addresses.PTOracle,
            TWAPDuration: 1800,
            pendleRateKind: PendleRateKind.PT_TO_ASSET,
        },
    ];
    await deployOracles(oracleConfig);
    if (network.name === "sepolia") {
        const NormalTimelock = "0xc332F7D8D5eA72cf760ED0E1c0485c8891C6E0cF";
        const mockContracts = ["MockPendleOracle_PT_USDe_27MAR2025", "MockPendleOracle_PT_sUSDe_27MAR2025"];
        await Promise.all(mockContracts.map(async (mock) => {
            const contract = await hardhat_1.ethers.getContract(mock);
            if ((await contract.owner()) === deployer) {
                console.log(`Transferring ownership of ${contract.address} to ${NormalTimelock}`);
                const tx = await contract.transferOwnership(NormalTimelock);
                await tx.wait();
                console.log(`Ownership transferred to ${NormalTimelock}`);
            }
        }));
    }
};
exports.default = func;
func.tags = ["pendle"];
func.skip = async (hre) => hre.network.name !== "ethereum" && hre.network.name !== "sepolia";
//# sourceMappingURL=7-deploy-pendle-oracle.js.map