"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.estimateGas = exports.getGasCost = void 0;
const ethers_1 = require("ethers");
const constants_1 = require("./constants");
const contract_getters_1 = require("./contract-getters");
const utils_1 = require("./utils");
async function getGasCostAlchemy(networkAlias) {
    return fetch(`https://${networkAlias}-mainnet.g.alchemy.com/v2/${process.env.REACT_APP_ALCHEMY_KEY}`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify({
            jsonrpc: "2.0",
            method: "eth_gasPrice",
            id: 1,
        }),
    })
        .then((res) => res.json())
        .then((res) => {
        // return the amount of wei per gas
        return ethers_1.BigNumber.from(res.result);
    });
}
async function getGasCost(network) {
    switch (network) {
        case "localhost":
        case "mainnet":
            return getGasCostAlchemy("eth");
        case "sepolia":
        case "goerli":
            return getGasCostAlchemy("eth");
        case "optimism_localhost":
        case "optimism":
            return getGasCostAlchemy("opt");
    }
    console.error("Could not get gas cost of network: ", network);
    return 0;
}
exports.getGasCost = getGasCost;
// TODO: continue implementing every function in this
async function estimateGas(params) {
    try {
        const client = await params.signer.getAddress();
        let tokenAddress = "";
        let amount;
        if (params.asset || params.underlying) {
            if (!params.amount || params.amount === "N/A")
                return ethers_1.BigNumber.from("0");
            tokenAddress = utils_1.convertSymbolToAddress(params.asset || params.underlying, params.network);
            amount = await utils_1.convertToCurrencyDecimals(tokenAddress, params.amount.toString(), params.test, params.providerRpc);
        }
        const lendingPool = await contract_getters_1.getLendingPool({
            signer: params.signer,
            network: params.network,
            test: params.test,
            providerRpc: params.providerRpc,
        });
        const incentivesController = await contract_getters_1.getIncentivesController({
            signer: params.signer,
            network: params.network,
            test: params.test,
            providerRpc: params.providerRpc,
        });
        let gasAmount = ethers_1.BigNumber.from("0");
        const optionalParams = params.test ? { gasLimit: "8000000" } : {};
        switch (params.function) {
            case "supply":
                gasAmount = await lendingPool
                    .connect(params.signer)
                    .estimateGas.deposit(tokenAddress, params.trancheId, amount, client, params.referrer || 0, optionalParams);
                break;
            case "borrow":
                gasAmount = await lendingPool
                    .connect(params.signer)
                    .estimateGas.borrow(tokenAddress, params.trancheId, amount, params.referrer || 0, client, optionalParams);
                break;
            case "withdraw":
                gasAmount = await lendingPool
                    .connect(params.signer)
                    .estimateGas.withdraw(tokenAddress, params.trancheId, params.isMax ? constants_1.MAX_UINT_AMOUNT : amount, client, optionalParams);
                break;
            case "repay":
                gasAmount = await lendingPool
                    .connect(params.signer)
                    .estimateGas.repay(tokenAddress, params.trancheId, params.isMax ? constants_1.MAX_UINT_AMOUNT : amount, client, optionalParams);
                break;
            case "claimRewards":
                gasAmount = await incentivesController
                    .connect(params.signer)
                    .estimateGas.claimAllRewards(params.incentivizedAssets, params.to);
                break;
        }
        console.log("function", params.function, "has gasamount", gasAmount.toHexString());
        return gasAmount.mul(await getGasCost(params.network));
    }
    catch (err) {
        console.error(err);
    }
}
exports.estimateGas = estimateGas;
//# sourceMappingURL=estimate-gas.js.map