All files / src/Program/programs blockfrostWorker.ts

65.78% Statements 25/38
20% Branches 2/10
50% Functions 1/2
64.86% Lines 24/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 7835x 35x   35x   35x 35x 35x   35x 35x 35x 35x 35x 35x   35x     35x 35x 35x 35x 35x 35x 35x 35x 35x                       35x   35x                                                                        
import { BlockfrostWorker, BlockfrostWorkerConfig, getPool } from '../services';
import { CommonProgramOptions, PosgresProgramOptions, PostgresOptionDescriptions } from '../options';
import { Logger } from 'ts-log';
import { MissingProgramOption } from '../errors/MissingProgramOption';
import { SrvRecord } from 'dns';
import { createDnsResolver } from '../utils';
import { createLogger } from 'bunyan';
import { readFile } from 'fs/promises';
 
export const BLOCKFROST_WORKER_API_URL_DEFAULT = new URL('http://localhost:3000');
export const CACHE_TTL_DEFAULT = 4 * 60; // Four hours
export const CREATE_SCHEMA_DEFAULT = false;
export const DROP_SCHEMA_DEFAULT = false;
export const DRY_RUN_DEFAULT = false;
export const SCAN_INTERVAL_DEFAULT = 60; // One hour
 
export const availableNetworks = ['mainnet', 'preprod', 'preview', 'sanchonet'] as const;
export type AvailableNetworks = typeof availableNetworks[number];
 
export enum BlockfrostWorkerOptionDescriptions {
  BlockfrostApiFile = 'Blockfrost API Key file path',
  BlockfrostApiKey = 'Blockfrost API Key',
  CacheTTL = 'TTL of blockfrost cached metrics in minutes',
  CreateSchema = 'create the schema; useful for development',
  DropSchema = 'drop the schema; useful for development',
  DryRun = 'dry run; useful for tests',
  Network = 'network to run against',
  ScanInterval = 'interval between a scan and the next one in minutes'
}
 
export type BlockfrostWorkerArgs = CommonProgramOptions &
  PosgresProgramOptions<'DbSync'> &
  BlockfrostWorkerConfig & { blockfrostApiFile?: string };
 
export interface LoadBlockfrostWorkerDependencies {
  dnsResolver?: (serviceName: string) => Promise<SrvRecord>;
  logger?: Logger;
}
 
const blockfrostWorker = 'Blockfrost worker';
 
export const loadBlockfrostWorker = async (args: BlockfrostWorkerArgs, deps: LoadBlockfrostWorkerDependencies = {}) => {
  const logger = deps?.logger || createLogger({ level: args.loggerMinSeverity, name: 'blockfrost-worker' });
  const dnsResolver =
    deps?.dnsResolver ||
    createDnsResolver(
      {
        factor: args.serviceDiscoveryBackoffFactor,
        maxRetryTime: args.serviceDiscoveryTimeout
      },
      logger
    );
  const db = await getPool(dnsResolver, logger, args);
 
  Iif (args.blockfrostApiFile)
    try {
      args.blockfrostApiKey = (await readFile(args.blockfrostApiFile)).toString('utf-8').replace(/[\n\r]/g, '');
    } catch (error) {
      logger.error(error);
 
      throw error;
    }
 
  Iif (!args.blockfrostApiKey)
    throw new MissingProgramOption(blockfrostWorker, [
      BlockfrostWorkerOptionDescriptions.BlockfrostApiFile,
      BlockfrostWorkerOptionDescriptions.BlockfrostApiKey
    ]);
 
  Iif (!db)
    throw new MissingProgramOption(blockfrostWorker, [
      PostgresOptionDescriptions.ConnectionString,
      PostgresOptionDescriptions.ServiceDiscoveryArgs
    ]);
 
  return new BlockfrostWorker(args, { db, logger });
};