All files / test/StakePool/fixtures FixtureBuilder.ts

90.9% Statements 50/55
80.76% Branches 21/26
100% Functions 8/8
90.38% Lines 47/52

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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118  2x         2x 2x 2x 2x 2x 2x 2x 2x 2x     2x               2x 2x 2x     2x 2x         17x   17x   17x       17x 8x   9x     17x 7x 10x 3x 7x 5x 2x     2x     17x             17x   17x 17x   17x       22x                   1x       1x   1x       2x   2x 2x       2x   2x 2x   2x       13x      
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as Queries from './queries';
import { Cardano } from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import { Pool, QueryResult } from 'pg';
 
export enum PoolWith {
  Metadata = 'Metadata',
  NoMetadata = 'NoMetadata',
  RetiredState = 'RetiredState',
  RetiringState = 'RetiringState',
  ActiveState = 'ActiveState',
  ActivatingState = 'ActivatingState',
  PledgeMet = 'PledgeMet',
  PledgeNotMet = 'PledgeNotMet'
}
 
export class PoolInfo {
  name: string;
  ticker: string;
  id: Cardano.PoolId;
  hashId: number;
  updateId: number;
}
 
export class DbSyncStakePoolFixtureBuilder {
  #db: Pool;
  #logger: Logger;
 
  constructor(db: Pool, logger: Logger) {
    this.#db = db;
    this.#logger = logger;
  }
 
  // eslint-disable-next-line complexity,sonarjs/cognitive-complexity
  public async getPools(desiredQty: number, options?: { with?: PoolWith[] }): Promise<PoolInfo[]> {
    this.#logger.debug(`About to fetch ${desiredQty} pools`);
 
    let query = Queries.subQueries;
 
    query += options?.with?.includes(PoolWith.Metadata)
      ? Queries.beginFindPoolsWithMetadata
      : Queries.beginFindPoolsWithoutMetadata;
 
    if (options?.with?.includes(PoolWith.PledgeMet) || options?.with?.includes(PoolWith.PledgeNotMet)) {
      query += options?.with?.includes(PoolWith.PledgeMet) ? Queries.withPledgeMet : Queries.withPledgeNotMet;
    } else {
      query += Queries.withNoPledgeFilter;
    }
 
    if (options?.with?.includes(PoolWith.ActiveState)) {
      query += Queries.withPoolActive;
    } else if (options?.with?.includes(PoolWith.ActivatingState)) {
      query += Queries.withPoolActivating;
    } else if (options?.with?.includes(PoolWith.RetiredState)) {
      query += Queries.withPoolRetired;
    } else Iif (options?.with?.includes(PoolWith.RetiringState)) {
      query += Queries.withPoolRetiring;
    } else {
      query += Queries.withNoStateFilter;
    }
 
    query += Queries.endFindPools;
 
    const result: QueryResult<{
      pool_id: string;
      metadata: any;
      hash_id: bigint;
      update_id: bigint;
    }> = await this.#db.query(query, [desiredQty]);
 
    const resultsQty = result.rows.length;
    Iif (result.rows.length === 0) {
      throw new Error('No pools found');
    } else Iif (resultsQty < desiredQty) {
      this.#logger.warn(`${desiredQty} pools desired, only ${resultsQty} results found`);
    }
 
    return result.rows.map(({ pool_id, metadata, hash_id, update_id }) => ({
      hashId: Number(hash_id.toString()),
      id: pool_id as unknown as Cardano.PoolId,
      name: metadata?.name,
      ticker: metadata?.ticker,
      updateId: Number(update_id.toString())
    }));
  }
 
  public async getLasKnownEpoch(): Promise<number> {
    this.#logger.debug('About to fetch las known epoch');
 
    const result: QueryResult<{
      no: number;
    }> = await this.#db.query(Queries.lastKnownEpoch);
 
    return result.rows.map(({ no }) => no)[0];
  }
 
  public async getDistinctPoolIds(desiredQty: number, withMetadata: boolean): Promise<Cardano.PoolId[]> {
    this.#logger.debug('About to fetch las known epoch');
 
    let query = Queries.beginPoolIds;
    query += withMetadata ? Queries.withMetadata : Queries.withoutMetadata;
 
    const result: QueryResult<{
      pool_id: string;
    }> = await this.#db.query(query);
 
    const resultsQty = result.rows.length;
    Iif (result.rows.length === 0) {
      throw new Error('No pool ids found');
    } else Iif (resultsQty < desiredQty) {
      this.#logger.warn(`${desiredQty} pools desired, only ${resultsQty} results found`);
    }
 
    return result.rows.map(({ pool_id }) => pool_id as unknown as Cardano.PoolId);
  }
}