"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RpcProviderSource = void 0;
const ethers_1 = require("ethers");
const tsyringe_1 = require("tsyringe");
const Config_1 = require("./Config");
const LoggingJsonRpcProvider_1 = require("./utils/LoggingJsonRpcProvider");
const config_1 = require("@streamr/config");
function isDevChain(config) {
    return config.contracts.ethereumNetwork.chainId === config_1.config.dev2.id;
}
const formJsonRpcApiProviderOptions = (config) => {
    return {
        staticNetwork: true,
        batchStallTime: isDevChain(config) ? 0 : undefined, // Don't batch requests, send them immediately
        cacheTimeout: isDevChain(config) ? -1 : undefined // Do not employ result caching
    };
};
let RpcProviderSource = class RpcProviderSource {
    config;
    provider;
    subProviders;
    constructor(config) {
        this.config = config;
    }
    getProvider() {
        if (this.provider === undefined) {
            const providers = this.getSubProviders();
            this.provider = new ethers_1.FallbackProvider(providers, this.config.contracts.ethereumNetwork.chainId, {
                quorum: Math.min(this.config.contracts.rpcQuorum, this.config.contracts.rpcs.length),
                cacheTimeout: formJsonRpcApiProviderOptions(this.config).cacheTimeout
            });
        }
        return this.provider;
    }
    /**
     * Use this method only if you need access each provider separately. In most cases it is better to use
     * the `getProvider` method as it provides better fail-safety.
     */
    getSubProviders() {
        if (this.subProviders === undefined) {
            this.subProviders = this.config.contracts.rpcs.map((c) => {
                const f = new ethers_1.FetchRequest(c.url);
                f.retryFunc = async () => false;
                // eslint-disable-next-line no-underscore-dangle
                f.timeout = this.config._timeouts.jsonRpcTimeout;
                const opts = formJsonRpcApiProviderOptions(this.config);
                return new LoggingJsonRpcProvider_1.LoggingJsonRpcProvider(f, this.config.contracts.ethereumNetwork.chainId, opts);
            });
        }
        return this.subProviders;
    }
};
exports.RpcProviderSource = RpcProviderSource;
exports.RpcProviderSource = RpcProviderSource = __decorate([
    (0, tsyringe_1.scoped)(tsyringe_1.Lifecycle.ContainerScoped),
    __param(0, (0, tsyringe_1.inject)(Config_1.ConfigInjectionToken)),
    __metadata("design:paramtypes", [Object])
], RpcProviderSource);
//# sourceMappingURL=RpcProviderSource.js.map