All files / src/api/blocks fetchers.ts

100% Statements 16/16
100% Branches 0/0
100% Functions 5/5
100% Lines 16/16

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    10x               10x 1x       1x                 10x 2x 2x                 10x 2x 2x                 10x       2x 2x                 10x       2x 2x    
import { BlockListResponse, Block } from '@stacks/stacks-blockchain-api-types';
import { BaseListParams } from '../types';
import { fetchJson, generateUrl, blockEndpoint } from '../utils';
 
/**
 * Get recent blocks
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/blocks#fetchblocks
 */
 
export async function fetchBlocks({ url, limit, offset }: BaseListParams) {
  const path = generateUrl(blockEndpoint(url), {
    limit,
    offset,
  });
  return fetchJson<BlockListResponse>(path);
}
 
/**
 * Get a specific block by hash
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/blocks#fetchblock
 */
 
export async function fetchBlock({ url, hash }: BaseListParams & { hash: string }) {
  const path = generateUrl(`${blockEndpoint(url)}/${hash}`, {});
  return fetchJson<Block>(path);
}
 
/**
 * Get a specific block by height
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/blocks#fetchblockbyheight
 */
 
export async function fetchBlockByHeight({ url, height }: BaseListParams & { height: number }) {
  const path = generateUrl(`${blockEndpoint(url)}/by_height/${height}`, {});
  return fetchJson<Block>(path);
}
 
/**
 * Get a specific block by burnchain block hash
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/blocks#fetchblockbyburnblockhash
 */
 
export async function fetchBlockByBurnBlockHash({
  url,
  burn_block_hash,
}: BaseListParams & { burn_block_hash: string }) {
  const path = generateUrl(`${blockEndpoint(url)}/by_burn_block_hash/${burn_block_hash}`, {});
  return fetchJson<Block>(path);
}
 
/**
 * Get a specific block by burn chain height
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/blocks#fetchblockbyburnblockheight
 */
 
export async function fetchBlockByBurnBlockHeight({
  url,
  burn_block_height,
}: BaseListParams & { burn_block_height: number }) {
  const path = generateUrl(`${blockEndpoint(url)}/by_burn_block_height/${burn_block_height}`, {});
  return fetchJson<Block>(path);
}