"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ethers_1 = require("ethers");
const utils_1 = require("ethers/lib/utils");
const hardhat_1 = require("hardhat");
const hardhat_2 = require("hardhat");
const deploymentConfig_1 = require("../helpers/deploymentConfig");
const mantissaToBps = (num) => {
    return ethers_1.BigNumber.from(num).div((0, utils_1.parseUnits)("1", 14)).toString();
};
const VTOKEN_DECIMALS = 8;
const EMPTY_BYTES_ARRAY = "0x";
const func = async function (hre) {
    const { deployments, getNamedAccounts } = hre;
    const { deploy } = deployments;
    const { deployer } = await getNamedAccounts();
    const { tokensConfig, marketsConfig } = await (0, deploymentConfig_1.getConfig)(hre.network.name);
    const comptrollerDeployment = await deployments.get("Unitroller");
    console.log(`Got deployment of Unitroller with address: ${comptrollerDeployment.address}`);
    for (const market of marketsConfig) {
        const { name, asset, symbol, rateModel, baseRatePerYear, multiplierPerYear, jumpMultiplierPerYear, kink_ } = market;
        const token = (0, deploymentConfig_1.getTokenConfig)(asset, tokensConfig);
        let tokenContract;
        if (token.isMock) {
            tokenContract = await hardhat_1.ethers.getContract(`Mock${token.symbol}`);
        }
        else {
            tokenContract = await hardhat_1.ethers.getContractAt("@openzeppelin/contracts/token/ERC20/ERC20.sol:ERC20", token.tokenAddress);
        }
        let rateModelAddress;
        if (rateModel === deploymentConfig_1.InterestRateModels.JumpRate.toString()) {
            const [b, m, j, k] = [baseRatePerYear, multiplierPerYear, jumpMultiplierPerYear, kink_].map(mantissaToBps);
            const rateModelName = `JumpRateModel_base${b}bps_slope${m}bps_jump${j}bps_kink${k}bps`;
            console.log(`Deploying interest rate model ${rateModelName}`);
            const result = await deploy(rateModelName, {
                from: deployer,
                contract: "JumpRateModel",
                args: [baseRatePerYear, multiplierPerYear, jumpMultiplierPerYear, kink_],
                log: true,
            });
            rateModelAddress = result.address;
        }
        else {
            const [b, m] = [baseRatePerYear, multiplierPerYear].map(mantissaToBps);
            const rateModelName = `WhitePaperInterestRateModel_base${b}bps_slope${m}bps`;
            console.log(`Deploying interest rate model ${rateModelName}`);
            const result = await deploy(rateModelName, {
                from: deployer,
                contract: "WhitePaperInterestRateModel",
                args: [baseRatePerYear, multiplierPerYear],
                log: true,
            });
            rateModelAddress = result.address;
        }
        const underlyingDecimals = Number(await tokenContract.decimals());
        const normalTimelock = await hardhat_1.ethers.getContract("NormalTimelock");
        const vBep20DelegateDeployment = await deploy("VBep20Delegate", { from: deployer, skipIfAlreadyDeployed: true });
        console.log(`Deploying VBep20 Proxy for ${symbol} with Implementation ${vBep20DelegateDeployment.address}`);
        await deploy(`${symbol}`, {
            contract: "VBep20Delegator",
            from: deployer,
            args: [
                tokenContract.address,
                comptrollerDeployment.address,
                rateModelAddress,
                (0, utils_1.parseUnits)("1", underlyingDecimals + 18 - VTOKEN_DECIMALS),
                name,
                symbol,
                VTOKEN_DECIMALS,
                hardhat_2.network.live ? normalTimelock.address : deployer,
                vBep20DelegateDeployment.address,
                EMPTY_BYTES_ARRAY,
            ],
            log: true,
            skipIfAlreadyDeployed: true,
        });
    }
};
func.tags = ["Markets"];
func.skip = (0, deploymentConfig_1.skipRemoteNetworks)();
exports.default = func;
