All files / src/api/info fetchers.ts

100% Statements 28/28
100% Branches 0/0
100% Functions 9/9
100% Lines 28/28

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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                                                            10x               10x 1x 1x                 10x 1x 1x                 10x 1x 1x                 10x       1x 1x                 10x 1x 1x                 10x 1x 1x                 10x 1x 1x                 10x 1x 1x                 10x 1x 1x    
import {
  CoreNodeInfoResponse,
  ServerStatusResponse,
  NetworkBlockTimesResponse,
  NetworkBlockTimeResponse,
  GetStxSupplyResponse,
  GetStxTotalSupplyPlainResponse,
  GetStxCirculatingSupplyPlainResponse,
  GetStxSupplyLegacyFormatResponse,
  CoreNodePoxResponse,
} from '@stacks/stacks-blockchain-api-types';
import { BaseListParams } from '../types';
 
type WithHeight<T> = T & {
  height?: number;
};
 
import {
  fetchJson,
  fetchText,
  generateUrl,
  infoEndpoint,
  statusEndpoint,
  networkBlockTimesEndpoint,
  networkBlockTimeEndpoint,
  stxSupplyEndpoint,
  stxSupplyPlainEndpoint,
  stxSupplyCirculatingPlainEndpoint,
  stxSupplyLegacyFormatEndpoint,
  poxEndpoint,
} from '../utils';
 
/**
 * Get Core API information
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/info#fetchcoreapiinfo
 */
 
export async function fetchCoreApiInfo({ url }: BaseListParams) {
  const path = generateUrl(infoEndpoint(url), {});
  return fetchJson<CoreNodeInfoResponse>(path);
}
 
/**
 * Get Blockchain API status
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/info#fetchstatus
 */
 
export async function fetchStatus({ url }: BaseListParams) {
  const path = generateUrl(statusEndpoint(url), {});
  return fetchJson<ServerStatusResponse>(path);
}
 
/**
 * Get the network target block time
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/info#fetchnetworkblocktimes
 */
 
export async function fetchNetworkBlockTimes({ url }: BaseListParams) {
  const path = generateUrl(networkBlockTimesEndpoint(url), {});
  return fetchJson<NetworkBlockTimesResponse>(path);
}
 
/**
 * Get a given network's target block time
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/info#fetchnetworkblocktime
 */
 
export async function fetchNetworkBlockTime({
  url,
  network,
}: BaseListParams & { network: 'mainnet' | 'testnet' }) {
  const path = generateUrl(`${networkBlockTimeEndpoint(url)}/${network}`, {});
  return fetchJson<NetworkBlockTimeResponse>(path);
}
 
/**
 * Get total and unlocked STX supply
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/info#fetchstxsupply
 */
 
export async function fetchStxSupply({ url, height }: WithHeight<BaseListParams>) {
  const path = generateUrl(stxSupplyEndpoint(url), {});
  return fetchJson<GetStxSupplyResponse>(path);
}
 
/**
 * Get total STX supply in plain text format
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/info#fetchstxsupplyplain
 */
 
export async function fetchStxSupplyPlain({ url }: BaseListParams) {
  const path = generateUrl(stxSupplyPlainEndpoint(url), {});
  return fetchText<GetStxTotalSupplyPlainResponse>(path);
}
 
/**
 * Get circulating STX supply in plain text format
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/info#fetchstxsupplycirculatingplain
 */
 
export async function fetchStxSupplyCirculatingPlain({ url }: BaseListParams) {
  const path = generateUrl(stxSupplyCirculatingPlainEndpoint(url), {});
  return fetchText<GetStxCirculatingSupplyPlainResponse>(path);
}
 
/**
 * Get total and unlocked STX supply (results formatted the same as the legacy 1.0 API)
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/info#fetchstxsupplylegacyformat
 */
 
export async function fetchStxSupplyLegacyFormat({ url, height }: WithHeight<BaseListParams>) {
  const path = generateUrl(stxSupplyLegacyFormatEndpoint(url), {});
  return fetchJson<GetStxSupplyLegacyFormatResponse>(path);
}
 
/**
 * Get PoX details
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/info#fetchpox
 */
 
export async function fetchPox({ url }: BaseListParams) {
  const path = generateUrl(poxEndpoint(url), {});
  return fetchJson<CoreNodePoxResponse>(path);
}