{"size": 1809, "ext": "js", "max_stars_repo_path": "base-service/lib/blockchainexplorer.js", "max_stars_repo_name": "owstack/wallet-service", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "base-service/lib/blockchainexplorer.js", "max_issues_repo_name": "owstack/wallet-service", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "base-service/lib/blockchainexplorer.js", "max_forks_repo_name": "owstack/wallet-service", "max_forks_repo_forks_event_min_datetime": "2019-03-30T20:35:37.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-04T07:41:41.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 3, "avg_line_length": 38.4893617021, "max_line_length": 150, "alphanum_fraction": 0.64013267}
const owsCommon = require('@owstack/ows-common');
const baseConfig = require('config');
const log = require('npmlog');
const lodash = owsCommon.deps.lodash;
const $ = require('preconditions').singleton();

log.debug = log.verbose;

class BlockchainExplorer {
    constructor(context, opts, config) {
    // Context defines the coin network and is set by the implementing service in
    // order to instance this base service; e.g., btc-service.
        context.inject(this);

        // Set some frequently used contant values based on context.
        this.LIVENET = this.ctx.Networks.livenet;

        this.config = config || baseConfig;

        const networkName = opts.networkName || this.LIVENET.name;
        const network = this.ctx.Networks.get(networkName);

        const providers = this.config[network.currency].blockchainExplorerOpts;
        const provider = opts.provider || providers.defaultProvider;

        $.checkState(providers[provider], `Provider ${  provider  } not supported`);
        $.checkState(lodash.includes(lodash.keys(providers[provider]), network.alias), `Network ${  network.alias  } not supported by this provider`);

        const url = this.config[network.currency].url || providers[provider][network.alias].url;
        const apiPrefix = this.config[network.currency].apiPrefix || providers[provider][network.alias].apiPrefix;

        switch (provider) {
            case 'explorer':
                return new this.ctx.Explorer({
                    networkName: networkName,
                    url: url,
                    apiPrefix: apiPrefix,
                    userAgent: opts.userAgent
                });
            default:
                throw new Error(`Provider ${  provider  } not supported`);
        }
    }
}

module.exports = BlockchainExplorer;
