All files / src/util/BlockfrostProvider blockfrostUtil.ts

92.85% Statements 39/42
89.65% Branches 26/29
85.71% Functions 6/7
94.44% Lines 34/36

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 11750x 50x 50x                     50x 7x   50x 17x       11x 6x           6x 3x     3x     50x                           10x 10x 10x 8x 8x 8x 8x 4x   4x   2x 1x   1x                 50x           4x 6x 6x 6x 6x     50x                   5x                     50x                    
import { BlockfrostClientError, isBlockfrostErrorResponse } from '@blockfrost/blockfrost-js/lib/utils/errors';
import { BlockfrostServerError } from '@blockfrost/blockfrost-js';
import {
  Cardano,
  EraSummary,
  Milliseconds,
  ProviderError,
  ProviderFailure,
  ProviderUtil,
  statusCodeMapToProviderFailure
} from '@cardano-sdk/core';
import { PaginationOptions } from '@blockfrost/blockfrost-js/lib/types';
 
export const isBlockfrostNotFoundError = (error: unknown) =>
  (error instanceof BlockfrostServerError || isBlockfrostErrorResponse(error)) && error.status_code === 404;
 
export const blockfrostToProviderError = (error: BlockfrostServerError | BlockfrostClientError | unknown) => {
  if (
    (error instanceof BlockfrostServerError || isBlockfrostErrorResponse(error)) &&
    statusCodeMapToProviderFailure.has(error.status_code)
  )
    return new ProviderError(statusCodeMapToProviderFailure.get(error.status_code)!, error, `${error.message}`);
  else Iif (
    isBlockfrostErrorResponse(error) ||
    error instanceof BlockfrostClientError ||
    error instanceof BlockfrostServerError
  )
    return new ProviderError(ProviderFailure.Unknown, error, `${error.message}`);
  else if (error instanceof ProviderError) {
    return error;
  }
 
  return new ProviderError(ProviderFailure.Unknown, error);
};
 
export const fetchSequentially = async <Item, Arg, Response>(
  props: {
    arg: Arg;
    request: (arg: Arg, pagination: PaginationOptions) => Promise<Response[]>;
    responseTranslator?: (response: Response[], arg: Arg) => Item[];
    /**
     * @returns true to indicate that current result set should be returned
     */
    haveEnoughItems?: (items: Item[]) => boolean;
    paginationOptions?: PaginationOptions;
  },
  page = 1,
  accumulated: Item[] = []
): Promise<Item[]> => {
  props.paginationOptions = props.paginationOptions || { count: 100 };
  try {
    const response = await props.request(props.arg, { ...props.paginationOptions, page });
    const maybeTranslatedResponse = props.responseTranslator ? props.responseTranslator(response, props.arg) : response;
    const newAccumulatedItems = [...accumulated, ...maybeTranslatedResponse] as Item[];
    const haveEnoughItems = props.haveEnoughItems?.(newAccumulatedItems);
    if (response.length === props.paginationOptions.count && !haveEnoughItems) {
      return fetchSequentially<Item, Arg, Response>(props, page + 1, newAccumulatedItems);
    }
    return newAccumulatedItems;
  } catch (error) {
    if (isBlockfrostNotFoundError(error)) {
      return [];
    }
    throw error;
  }
};
 
/**
 * Maps txs metadata from blockfrost into to a TxMetadata
 *
 * @returns {Cardano.TxMetadata} map with bigint as key and Metadatum as value
 */
export const blockfrostMetadataToTxMetadata = (
  metadata: {
    label: string;
    json_metadata: unknown;
  }[]
): Cardano.TxMetadata =>
  metadata.reduce((map, metadatum) => {
    const { json_metadata, label } = metadatum;
    Iif (!json_metadata || !label) return map;
    map.set(BigInt(label), ProviderUtil.jsonToMetadatum(json_metadata));
    return map;
  }, new Map<bigint, Cardano.Metadatum>());
 
export const fetchByAddressSequentially = async <Item, Response>(props: {
  address: Cardano.PaymentAddress;
  request: (address: Cardano.PaymentAddress, pagination: PaginationOptions) => Promise<Response[]>;
  responseTranslator?: (address: Cardano.PaymentAddress, response: Response[]) => Item[];
  /**
   * @returns true to indicate that current result set should be returned
   */
  haveEnoughItems?: (items: Item[]) => boolean;
  paginationOptions?: PaginationOptions;
}): Promise<Item[]> =>
  fetchSequentially({
    arg: props.address,
    haveEnoughItems: props.haveEnoughItems,
    paginationOptions: props.paginationOptions,
    request: props.request,
    responseTranslator: props.responseTranslator
      ? (response, arg) => props.responseTranslator!(arg, response)
      : undefined
  });
 
// copied from util-dev
export const testnetEraSummaries: EraSummary[] = [
  {
    parameters: { epochLength: 21_600, slotLength: Milliseconds(20_000) },
    start: { slot: 0, time: new Date(1_563_999_616_000) }
  },
  {
    parameters: { epochLength: 432_000, slotLength: Milliseconds(1000) },
    start: { slot: 1_598_400, time: new Date(1_595_967_616_000) }
  }
];