"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const markets_1 = require("../helpers/markets");
const rateModelHelpers_1 = require("../helpers/rateModelHelpers");
const writeFile_1 = require("../helpers/writeFile");
const checkpoints = {
    bsctestnet: {
        at: 1744097580,
        fromBlocksPerYear: 10512000,
        toBlocksPerYear: 21024000,
    },
    bscmainnet: {
        at: 1745903100,
        fromBlocksPerYear: 10512000,
        toBlocksPerYear: 21024000,
    },
};
const capitalize = (s) => `${s.charAt(0).toUpperCase()}${s.substring(1)}`;
const getSetterContractName = (networkName) => `SetCheckpoint${capitalize(networkName)}`;
const generateSetterContract = (networkName, admin, setterCode) => {
    const body = setterCode.map(line => `        ${line}`).join("\n");
    return `
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.8.25;

interface VBNBAdminInterface {
    function setInterestRateModel(address) external;
}

interface VTokenInterface {
    function _setInterestRateModel(address) external;
}

contract ${getSetterContractName(networkName)} {
    error Unauthorized();

    function run() external {
        if (msg.sender != ${admin}) {
            revert Unauthorized();
        }
${body}
    }
}`;
};
const func = async function (hre) {
    const { deployments, getNamedAccounts } = hre;
    const { deploy } = deployments;
    const { deployer } = await getNamedAccounts();
    const chain = hre.getNetworkName(); // ok since we skip the others
    const timelock = await deployments.get("NormalTimelock");
    const { at, fromBlocksPerYear, toBlocksPerYear } = checkpoints[chain];
    const marketsConfig = markets_1.markets[chain];
    const setterCode = [];
    for (const market of marketsConfig) {
        const { symbol, interestRateModel } = market;
        console.log(`Deploying checkpoint view for ${symbol}`);
        const oldRateModelName = (0, rateModelHelpers_1.getRateModelName)(interestRateModel, fromBlocksPerYear);
        const newRateModelName = (0, rateModelHelpers_1.getRateModelName)(interestRateModel, toBlocksPerYear);
        console.log("  from", oldRateModelName);
        console.log("    to", newRateModelName);
        const oldRateModel = await deployments.get(oldRateModelName);
        const newRateModel = await deployments.get(newRateModelName);
        const checkpointViewName = `CheckpointView_From_${oldRateModelName}_To_bpy${toBlocksPerYear}_At_${at}`;
        const checkpointView = await deploy(checkpointViewName, {
            contract: "CheckpointView",
            from: deployer,
            args: [oldRateModel.address, newRateModel.address, at],
            log: true,
            skipIfAlreadyDeployed: true,
            waitConfirmations: 3,
        });
        const vToken = await deployments.get(`${symbol}`);
        setterCode.push("");
        setterCode.push(`// ${symbol} -> ${checkpointViewName}`);
        if (symbol === "vBNB") {
            const vBNBAdmin = await deployments.get("VBNBAdmin");
            setterCode.push(`VBNBAdminInterface(${vBNBAdmin.address}).setInterestRateModel(${checkpointView.address});`);
        }
        else {
            setterCode.push(`VTokenInterface(${vToken.address})._setInterestRateModel(${checkpointView.address});`);
        }
        console.log(`-----------------------------------------`);
    }
    const setterContractCode = generateSetterContract(chain, timelock.address, setterCode);
    (0, writeFile_1.writeGeneratedContract)(`${getSetterContractName(chain)}.sol`, setterContractCode);
};
func.tags = ["RateModelCheckpoint"];
func.skip = async (hre) => !Object.keys(checkpoints).includes(hre.network.name);
exports.default = func;
