"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("ethers/lib/utils");
const hardhat_1 = require("hardhat");
const hardhat_2 = require("hardhat");
const chains_1 = require("../helpers/chains");
const deploymentConfig_1 = require("../helpers/deploymentConfig");
const markets_1 = require("../helpers/markets");
const rateModelHelpers_1 = require("../helpers/rateModelHelpers");
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 chain = (0, chains_1.assertBlockBasedChain)(hre.network.name);
    const marketsConfig = markets_1.markets[chain];
    const blocksPerYear = chains_1.blocksPerYear[chain];
    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, interestRateModel } = market;
        // Short-circuit to avoid extra requests to the node if vToken already exists
        const deployment = await deployments.getOrNull(symbol);
        if (deployment !== null && deployment !== undefined) {
            console.log(`Skipping ${symbol} deployment: found at ${deployment.address}`);
            continue;
        }
        let tokenContract;
        if (asset.isMock) {
            tokenContract = await hardhat_1.ethers.getContract(`Mock${asset.symbol}`);
        }
        else {
            tokenContract = await hardhat_1.ethers.getContractAt("@openzeppelin/contracts/token/ERC20/ERC20.sol:ERC20", asset.tokenAddress);
        }
        const rateModelName = (0, rateModelHelpers_1.getRateModelName)(interestRateModel, blocksPerYear);
        const rateModelAddress = (await deployments.get(rateModelName)).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;
