"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGasPriceEstimate = getGasPriceEstimate;
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
const constants_1 = require("../constants");
const utils_1 = require("../utils");
const util_1 = require("./util");
const arbitrum = tslib_1.__importStar(require("./adapters/arbitrum"));
const ethereum = tslib_1.__importStar(require("./adapters/ethereum"));
const polygon = tslib_1.__importStar(require("./adapters/polygon"));
const lineaViem = tslib_1.__importStar(require("./adapters/linea-viem"));
const solana = tslib_1.__importStar(require("./adapters/solana"));
const GAS_PRICE_ESTIMATE_DEFAULTS = {
    legacyFallback: true,
};
const VIEM_CHAINS = [constants_1.CHAIN_IDs.LINEA];
async function getGasPriceEstimate(provider, opts = {}) {
    const baseFeeMultiplier = opts.baseFeeMultiplier ?? (0, utils_1.toBNWei)("1");
    const priorityFeeMultiplier = opts.priorityFeeMultiplier ?? (0, utils_1.toBNWei)("1");
    if (!(0, utils_1.isEvmProvider)(provider)) {
        const optsWithDefaults = {
            ...GAS_PRICE_ESTIMATE_DEFAULTS,
            baseFeeMultiplier,
            priorityFeeMultiplier,
            ...opts,
            chainId: opts.chainId ?? constants_1.CHAIN_IDs.SOLANA,
        };
        return solana.messageFee(provider, optsWithDefaults);
    }
    const chainId = opts.chainId ?? (await provider.getNetwork()).chainId;
    const optsWithDefaults = {
        ...GAS_PRICE_ESTIMATE_DEFAULTS,
        baseFeeMultiplier,
        priorityFeeMultiplier,
        ...opts,
        chainId,
    };
    const useViem = VIEM_CHAINS.includes(chainId);
    return useViem ? getGasPriceViem(chainId, optsWithDefaults) : getGasPriceEthers(provider, optsWithDefaults);
}
function getGasPriceEthers(provider, opts) {
    const { chainId, legacyFallback } = opts;
    (0, assert_1.default)(!VIEM_CHAINS.includes(chainId), `Chain ID ${chainId} will use Viem gas price estimation`);
    const gasPriceFeeds = {
        [constants_1.CHAIN_IDs.ARBITRUM]: arbitrum.eip1559,
        [constants_1.CHAIN_IDs.POLYGON]: polygon.gasStation,
        [constants_1.CHAIN_IDs.TEMPO]: ethereum.eip1559,
        [constants_1.CHAIN_IDs.TRON]: ethereum.legacy,
        [constants_1.CHAIN_IDs.ZK_SYNC]: ethereum.legacy,
        [constants_1.CHAIN_IDs.POLYGON_AMOY]: polygon.gasStation,
    };
    let gasPriceFeed = (0, utils_1.isDefined)(opts.feeHistoryOptions) ? ethereum.feeHistory : gasPriceFeeds[chainId];
    (0, assert_1.default)(gasPriceFeed || legacyFallback, `No suitable gas price oracle for Chain ID ${chainId}`);
    gasPriceFeed ??= ethereum.eip1559;
    return gasPriceFeed(provider, opts);
}
async function getGasPriceViem(chainId, opts) {
    const { baseFeeMultiplier, transport } = opts;
    const viemProvider = (0, util_1.getPublicClient)(chainId, transport);
    const gasPriceFeeds = {
        [constants_1.CHAIN_IDs.LINEA]: lineaViem.eip1559,
    };
    let maxFeePerGas;
    let maxPriorityFeePerGas;
    if (gasPriceFeeds[chainId]) {
        ({ maxFeePerGas, maxPriorityFeePerGas } = await gasPriceFeeds[chainId](viemProvider, opts));
    }
    else {
        let gasPrice;
        ({ maxFeePerGas, maxPriorityFeePerGas, gasPrice } = await viemProvider.estimateFeesPerGas());
        maxFeePerGas ??= (gasPrice * BigInt(baseFeeMultiplier.toString())) / BigInt(utils_1.fixedPointAdjustment.toString());
        maxPriorityFeePerGas ??= BigInt(0);
    }
    return {
        maxFeePerGas: utils_1.BigNumber.from(maxFeePerGas.toString()),
        maxPriorityFeePerGas: utils_1.BigNumber.from(maxPriorityFeePerGas.toString()),
    };
}
//# sourceMappingURL=oracle.js.map