"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpeedProvider = void 0;
const ethers_1 = require("ethers");
const cachedProvider_1 = require("./cachedProvider");
const utils_1 = require("./utils");
const constants_1 = require("./constants");
class SpeedProvider extends ethers_1.ethers.providers.StaticJsonRpcProvider {
    maxConcurrencySpeed;
    maxConcurrencyRateLimit;
    providers;
    constructor(params, chainId, maxConcurrencySpeed, maxConcurrencyRateLimit, providerCacheNamespace, pctRpcCallsLogged, redisClient, standardTtlBlockDistance, noTtlBlockDistance, providerCacheTtl = constants_1.PROVIDER_CACHE_TTL, logger) {
        super(undefined, chainId);
        this.maxConcurrencySpeed = maxConcurrencySpeed;
        this.maxConcurrencyRateLimit = maxConcurrencyRateLimit;
        this.providers = params.map((inputs) => new cachedProvider_1.CacheProvider(providerCacheNamespace, redisClient, standardTtlBlockDistance, noTtlBlockDistance, providerCacheTtl, maxConcurrencyRateLimit, pctRpcCallsLogged, logger, ...inputs));
    }
    async send(method, params) {
        try {
            const providersToUse = this.providers.slice(0, this.maxConcurrencySpeed);
            const result = await Promise.any(providersToUse.map((provider) => provider.send(method, params)));
            return result;
        }
        catch (error) {
            if (error instanceof AggregateError) {
                const errors = error.errors.map((error, index) => {
                    const provider = this.providers[index];
                    return (0, utils_1.formatProviderError)(provider, error.message);
                });
                throw new Error("All providers errored:\n" + errors.join("\n"));
            }
            throw error;
        }
    }
}
exports.SpeedProvider = SpeedProvider;
//# sourceMappingURL=speedProvider.js.map