"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGasPriceEstimate = getGasPriceEstimate;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importDefault(require("assert"));
var constants_1 = require("../constants");
var utils_1 = require("../utils");
var util_1 = require("./util");
var arbitrum = tslib_1.__importStar(require("./adapters/arbitrum"));
var ethereum = tslib_1.__importStar(require("./adapters/ethereum"));
var polygon = tslib_1.__importStar(require("./adapters/polygon"));
var lineaViem = tslib_1.__importStar(require("./adapters/linea-viem"));
var solana = tslib_1.__importStar(require("./adapters/solana"));
var GAS_PRICE_ESTIMATE_DEFAULTS = {
    legacyFallback: true,
};
var VIEM_CHAINS = [constants_1.CHAIN_IDs.LINEA];
function getGasPriceEstimate(provider_1) {
    return tslib_1.__awaiter(this, arguments, void 0, function (provider, opts) {
        var baseFeeMultiplier, priorityFeeMultiplier, optsWithDefaults_1, chainId, _a, optsWithDefaults, useViem;
        var _b, _c, _d, _e;
        if (opts === void 0) { opts = {}; }
        return tslib_1.__generator(this, function (_f) {
            switch (_f.label) {
                case 0:
                    baseFeeMultiplier = (_b = opts.baseFeeMultiplier) !== null && _b !== void 0 ? _b : (0, utils_1.toBNWei)("1");
                    priorityFeeMultiplier = (_c = opts.priorityFeeMultiplier) !== null && _c !== void 0 ? _c : (0, utils_1.toBNWei)("1");
                    if (!(0, utils_1.isEvmProvider)(provider)) {
                        optsWithDefaults_1 = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, GAS_PRICE_ESTIMATE_DEFAULTS), { baseFeeMultiplier: baseFeeMultiplier, priorityFeeMultiplier: priorityFeeMultiplier }), opts), { chainId: (_d = opts.chainId) !== null && _d !== void 0 ? _d : constants_1.CHAIN_IDs.SOLANA });
                        return [2, solana.messageFee(provider, optsWithDefaults_1)];
                    }
                    if (!((_e = opts.chainId) !== null && _e !== void 0)) return [3, 1];
                    _a = _e;
                    return [3, 3];
                case 1: return [4, provider.getNetwork()];
                case 2:
                    _a = (_f.sent()).chainId;
                    _f.label = 3;
                case 3:
                    chainId = _a;
                    optsWithDefaults = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, GAS_PRICE_ESTIMATE_DEFAULTS), { baseFeeMultiplier: baseFeeMultiplier, priorityFeeMultiplier: priorityFeeMultiplier }), opts), { chainId: chainId });
                    useViem = VIEM_CHAINS.includes(chainId);
                    return [2, useViem ? getGasPriceViem(chainId, optsWithDefaults) : getGasPriceEthers(provider, optsWithDefaults)];
            }
        });
    });
}
function getGasPriceEthers(provider, opts) {
    var _a;
    var chainId = opts.chainId, legacyFallback = opts.legacyFallback;
    (0, assert_1.default)(!VIEM_CHAINS.includes(chainId), "Chain ID ".concat(chainId, " will use Viem gas price estimation"));
    var gasPriceFeeds = (_a = {},
        _a[constants_1.CHAIN_IDs.ARBITRUM] = arbitrum.eip1559,
        _a[constants_1.CHAIN_IDs.POLYGON] = polygon.gasStation,
        _a[constants_1.CHAIN_IDs.SCROLL] = ethereum.legacy,
        _a[constants_1.CHAIN_IDs.TEMPO] = ethereum.eip1559,
        _a[constants_1.CHAIN_IDs.ZK_SYNC] = ethereum.legacy,
        _a[constants_1.CHAIN_IDs.POLYGON_AMOY] = polygon.gasStation,
        _a);
    var 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 ".concat(chainId));
    gasPriceFeed !== null && gasPriceFeed !== void 0 ? gasPriceFeed : (gasPriceFeed = ethereum.eip1559);
    return gasPriceFeed(provider, opts);
}
function getGasPriceViem(chainId, opts) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var baseFeeMultiplier, transport, viemProvider, gasPriceFeeds, maxFeePerGas, maxPriorityFeePerGas, gasPrice;
        var _a, _b, _c;
        return tslib_1.__generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    baseFeeMultiplier = opts.baseFeeMultiplier, transport = opts.transport;
                    viemProvider = (0, util_1.getPublicClient)(chainId, transport);
                    gasPriceFeeds = (_a = {},
                        _a[constants_1.CHAIN_IDs.LINEA] = lineaViem.eip1559,
                        _a);
                    if (!gasPriceFeeds[chainId]) return [3, 2];
                    return [4, gasPriceFeeds[chainId](viemProvider, opts)];
                case 1:
                    (_b = _d.sent(), maxFeePerGas = _b.maxFeePerGas, maxPriorityFeePerGas = _b.maxPriorityFeePerGas);
                    return [3, 4];
                case 2:
                    gasPrice = void 0;
                    return [4, viemProvider.estimateFeesPerGas()];
                case 3:
                    (_c = _d.sent(), maxFeePerGas = _c.maxFeePerGas, maxPriorityFeePerGas = _c.maxPriorityFeePerGas, gasPrice = _c.gasPrice);
                    maxFeePerGas !== null && maxFeePerGas !== void 0 ? maxFeePerGas : (maxFeePerGas = (gasPrice * BigInt(baseFeeMultiplier.toString())) / BigInt(utils_1.fixedPointAdjustment.toString()));
                    maxPriorityFeePerGas !== null && maxPriorityFeePerGas !== void 0 ? maxPriorityFeePerGas : (maxPriorityFeePerGas = BigInt(0));
                    _d.label = 4;
                case 4: return [2, {
                        maxFeePerGas: utils_1.BigNumber.from(maxFeePerGas.toString()),
                        maxPriorityFeePerGas: utils_1.BigNumber.from(maxPriorityFeePerGas.toString()),
                    }];
            }
        });
    });
}
//# sourceMappingURL=oracle.js.map