"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports._getViemGasPriceEstimate = exports.getGasPriceEstimate = void 0;
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, opts) {
    var _a, _b, _c, _d;
    if (opts === void 0) { opts = {}; }
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var baseFeeMultiplier, priorityFeeMultiplier, optsWithDefaults_1, chainId, _e, optsWithDefaults, useViem;
        return tslib_1.__generator(this, function (_f) {
            switch (_f.label) {
                case 0:
                    baseFeeMultiplier = (_a = opts.baseFeeMultiplier) !== null && _a !== void 0 ? _a : (0, utils_1.toBNWei)("1");
                    priorityFeeMultiplier = (_b = opts.priorityFeeMultiplier) !== null && _b !== void 0 ? _b : (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: (_c = opts.chainId) !== null && _c !== void 0 ? _c : constants_1.CHAIN_IDs.SOLANA });
                        return [2, solana.messageFee(provider, optsWithDefaults_1)];
                    }
                    if (!((_d = opts.chainId) !== null && _d !== void 0)) return [3, 1];
                    _e = _d;
                    return [3, 3];
                case 1: return [4, provider.getNetwork()];
                case 2:
                    _e = (_f.sent()).chainId;
                    _f.label = 3;
                case 3:
                    chainId = _e;
                    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
                            ? _getViemGasPriceEstimate(chainId, optsWithDefaults)
                            : _getEthersGasPriceEstimate(provider, optsWithDefaults)];
            }
        });
    });
}
exports.getGasPriceEstimate = getGasPriceEstimate;
function _getEthersGasPriceEstimate(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.ALEPH_ZERO] = arbitrum.eip1559,
        _a[constants_1.CHAIN_IDs.ARBITRUM] = arbitrum.eip1559,
        _a[constants_1.CHAIN_IDs.BSC] = ethereum.legacy,
        _a[constants_1.CHAIN_IDs.MAINNET] = ethereum.eip1559,
        _a[constants_1.CHAIN_IDs.POLYGON] = polygon.gasStation,
        _a[constants_1.CHAIN_IDs.SCROLL] = ethereum.legacy,
        _a[constants_1.CHAIN_IDs.ZK_SYNC] = ethereum.legacy,
        _a[constants_1.CHAIN_IDs.ARBITRUM_SEPOLIA] = arbitrum.eip1559,
        _a[constants_1.CHAIN_IDs.POLYGON_AMOY] = polygon.gasStation,
        _a[constants_1.CHAIN_IDs.SEPOLIA] = ethereum.eip1559,
        _a[constants_1.CHAIN_IDs.TATARA] = ethereum.eip1559,
        _a);
    var gasPriceFeed = 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 = (0, utils_1.chainIsOPStack)(chainId) ? ethereum.eip1559 : ethereum.legacy);
    return gasPriceFeed(provider, opts);
}
function _getViemGasPriceEstimate(providerOrChainId, opts) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var baseFeeMultiplier, transport, chainId, _a, viemProvider, gasPriceFeeds, maxFeePerGas, maxPriorityFeePerGas, gasPrice;
        var _b, _c, _d;
        return tslib_1.__generator(this, function (_e) {
            switch (_e.label) {
                case 0:
                    baseFeeMultiplier = opts.baseFeeMultiplier, transport = opts.transport;
                    if (!(typeof providerOrChainId === "number")) return [3, 1];
                    _a = providerOrChainId;
                    return [3, 3];
                case 1: return [4, providerOrChainId.getNetwork()];
                case 2:
                    _a = (_e.sent()).chainId;
                    _e.label = 3;
                case 3:
                    chainId = _a;
                    viemProvider = (0, util_1.getPublicClient)(chainId, transport);
                    gasPriceFeeds = (_b = {},
                        _b[constants_1.CHAIN_IDs.LINEA] = lineaViem.eip1559,
                        _b);
                    if (!gasPriceFeeds[chainId]) return [3, 5];
                    return [4, gasPriceFeeds[chainId](viemProvider, opts)];
                case 4:
                    (_c = _e.sent(), maxFeePerGas = _c.maxFeePerGas, maxPriorityFeePerGas = _c.maxPriorityFeePerGas);
                    return [3, 7];
                case 5:
                    gasPrice = void 0;
                    return [4, viemProvider.estimateFeesPerGas()];
                case 6:
                    (_d = _e.sent(), maxFeePerGas = _d.maxFeePerGas, maxPriorityFeePerGas = _d.maxPriorityFeePerGas, gasPrice = _d.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));
                    _e.label = 7;
                case 7: return [2, {
                        maxFeePerGas: utils_1.BigNumber.from(maxFeePerGas.toString()),
                        maxPriorityFeePerGas: utils_1.BigNumber.from(maxPriorityFeePerGas.toString()),
                    }];
            }
        });
    });
}
exports._getViemGasPriceEstimate = _getViemGasPriceEstimate;
//# sourceMappingURL=oracle.js.map