All files / src/StakePool/HttpStakePoolMetadata util.ts

100% Statements 17/17
100% Branches 6/6
100% Functions 4/4
100% Lines 12/12

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    36x 36x 36x 36x                   36x                   36x 10x     36x 10x 10x       36x 8x  
import { Cardano } from '@cardano-sdk/core';
import { Cip6ExtMetadataResponse } from './types';
import { ExtMetadataFormat, StakePoolExtMetadataResponse } from '../types';
import { isNotNil } from '@cardano-sdk/util';
import fs from 'fs';
import path from 'path';
 
/**
 * Extracts the extended metadata url, takes CIP-6 with priority if both extended properties exist
 *
 * CIP-6 standard: https://github.com/cardano-foundation/CIPs/blob/master/CIP-0006/README.md
 * AdaPools standard: https://raw.githubusercontent.com/cardanians/adapools.org/master/example-meta.json
 *
 * @returns url string
 */
export const getExtMetadataUrl = (metadata: Cardano.StakePoolMetadata) => metadata.extDataUrl ?? metadata.extended!;
 
/**
 * A check to determine the schema format
 *
 * CIP-6 schema: https://raw.githubusercontent.com/cardano-foundation/CIPs/b93e77119e15d8763d7548a8a00c4cb7591714e4/CIP-0006/schema.json
 * AdaPools schema: https://a.adapools.org/extended-example
 *
 * @returns extended metadata format
 */
export const getSchemaFormat = (value: Cardano.StakePoolMetadata): ExtMetadataFormat =>
  isNotNil(value.extDataUrl) ? ExtMetadataFormat.CIP6 : ExtMetadataFormat.AdaPools;
 
/** Loads JSON schema based on extended metadata format */
export const loadJsonSchema = (format: ExtMetadataFormat): JSON => {
  const schemaPath = path.join(__dirname, 'schemas', `${format}.json`);
  return JSON.parse(fs.readFileSync(schemaPath, 'utf8'));
};
 
/** Type guard to determine if the consumed ext metadata response is in CIP-6 format */
export const isCip6Format = (response: StakePoolExtMetadataResponse): response is Cip6ExtMetadataResponse =>
  isNotNil(response.pool) || isNotNil(response.serial);