All files / test/Utxo/fixtures FixtureBuilder.ts

93.54% Statements 29/31
76.92% Branches 10/13
100% Functions 4/4
93.33% Lines 28/30

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 601x         1x 1x 1x 1x 1x     1x 1x 1x     1x 1x             8x       8x 3x 3x 5x 1x   4x   4x 2x   2x 1x     2x   4x     8x 8x   8x       12x      
import * as Queries from './queries';
import { Cardano } from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import { Pool, QueryResult } from 'pg';
 
export enum AddressWith {
  MultiAsset = 'multiAsset',
  AssetWithoutName = 'AssetWithoutName',
  ReferenceScript = 'ReferenceScript',
  InlineDatum = 'InlineDatum'
}
 
export class UtxoFixtureBuilder {
  #db: Pool;
  #logger: Logger;
 
  constructor(db: Pool, logger: Logger) {
    this.#db = db;
    this.#logger = logger;
  }
 
  public async getAddresses(
    desiredQty: number,
    options?: { with?: AddressWith[]; scriptType?: string }
  ): Promise<Cardano.PaymentAddress[]> {
    this.#logger.debug(`About to fetch up to ${desiredQty} addresses`);
 
    let result: QueryResult<{ address: string }>;
 
    if (options?.with?.includes(AddressWith.ReferenceScript)) {
      const type = options.scriptType ? options.scriptType : 'timelock';
      result = await this.#db.query(Queries.findAddressWithScriptRefUtxo, [desiredQty, type]);
    } else if (options?.with?.includes(AddressWith.InlineDatum)) {
      result = await this.#db.query(Queries.findAddressWithInlineDatumUtxo, [desiredQty]);
    } else {
      let query = Queries.findAddresses;
 
      if (options?.with?.includes(AddressWith.MultiAsset)) {
        query = Queries.beingMultiAssetAddresses;
 
        if (options?.with?.includes(AddressWith.AssetWithoutName)) {
          query += Queries.withMultiAssetWithoutName;
        }
 
        query += Queries.endMultiAssetAddresses;
      }
      result = await this.#db.query(query, [desiredQty]);
    }
 
    const resultsQty = result!.rows.length;
    Iif (result!.rows.length === 0) {
      throw new Error('No addresses found');
    } else Iif (resultsQty < desiredQty) {
      this.#logger.warn(`${desiredQty} distinct addresses desired, only ${resultsQty} results found`);
    }
 
    return result!.rows.map(({ address }) => address as unknown as Cardano.PaymentAddress);
  }
}