"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpeedProvider = void 0;
var tslib_1 = require("tslib");
var ethers_1 = require("ethers");
var cachedProvider_1 = require("./cachedProvider");
var utils_1 = require("./utils");
var constants_1 = require("./constants");
var SpeedProvider = (function (_super) {
    tslib_1.__extends(SpeedProvider, _super);
    function SpeedProvider(params, chainId, maxConcurrencySpeed, maxConcurrencyRateLimit, providerCacheNamespace, pctRpcCallsLogged, redisClient, standardTtlBlockDistance, noTtlBlockDistance, providerCacheTtl, logger) {
        if (providerCacheTtl === void 0) { providerCacheTtl = constants_1.PROVIDER_CACHE_TTL; }
        var _this = _super.call(this, undefined, chainId) || this;
        _this.maxConcurrencySpeed = maxConcurrencySpeed;
        _this.maxConcurrencyRateLimit = maxConcurrencyRateLimit;
        _this.providers = params.map(function (inputs) {
            return new (cachedProvider_1.CacheProvider.bind.apply(cachedProvider_1.CacheProvider, tslib_1.__spreadArray([void 0, providerCacheNamespace,
                redisClient,
                standardTtlBlockDistance,
                noTtlBlockDistance,
                providerCacheTtl,
                maxConcurrencyRateLimit,
                pctRpcCallsLogged,
                logger], inputs, false)))();
        });
        return _this;
    }
    SpeedProvider.prototype.send = function (method, params) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var providersToUse, result, error_1, errors;
            var _this = this;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 2, , 3]);
                        providersToUse = this.providers.slice(0, this.maxConcurrencySpeed);
                        return [4, Promise.any(providersToUse.map(function (provider) { return provider.send(method, params); }))];
                    case 1:
                        result = _a.sent();
                        return [2, result];
                    case 2:
                        error_1 = _a.sent();
                        if (error_1 instanceof AggregateError) {
                            errors = error_1.errors.map(function (error, index) {
                                var provider = _this.providers[index];
                                return (0, utils_1.formatProviderError)(provider, error.message);
                            });
                            throw new Error("All providers errored:\n" + errors.join("\n"));
                        }
                        throw error_1;
                    case 3: return [2];
                }
            });
        });
    };
    return SpeedProvider;
}(ethers_1.ethers.providers.StaticJsonRpcProvider));
exports.SpeedProvider = SpeedProvider;
//# sourceMappingURL=speedProvider.js.map