All files / src/api/tx fetchers.ts

36.36% Statements 8/22
0% Branches 0/2
0% Functions 0/7
36.36% Lines 8/22

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                9x               9x                                       9x                                               9x                         9x                                   9x                   9x                                 9x                      
import {
  MempoolTransaction,
  MempoolTransactionListResponse,
  Transaction,
  TransactionResults,
  TransactionType,
} from '@stacks/stacks-blockchain-api-types';
import { BaseListParams, EventListParams } from '../types';
import { fetchJson, generateUrl, txEndpoint, txMempoolEndpoint, validateTxTypes } from '../utils';
 
/**
 * Get transactions list
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/transactions#fetchtransactionslist
 */
 
export async function fetchTransactionsList({
  limit,
  offset,
  type,
  url,
}: BaseListParams & { type?: TransactionType | TransactionType[] }) {
  const path = generateUrl(txEndpoint(url), {
    limit,
    offset,
    type: type ? (validateTxTypes(type) as any) : undefined,
  });
  return fetchJson<TransactionResults>(path);
}
 
/**
 * Get mempool transactions list
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/transactions#fetchmempooltransactionslist
 */
 
export async function fetchMempoolTransactionsList({
  limit,
  offset,
  sender_address,
  recipient_address,
  address,
  url,
}: BaseListParams & { sender_address?: string; recipient_address?: string; address?: string }) {
  const path = generateUrl(txMempoolEndpoint(url), {
    limit,
    offset,
    sender_address,
    recipient_address,
    address,
  });
  return fetchJson<MempoolTransactionListResponse>(path);
}
 
/**
 * Get dropped mempool transactions list
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/transactions#fetchdroppedmempooltransactionslist
 */
 
export async function fetchDroppedMempoolTransactionsList({ limit, offset, url }: BaseListParams) {
  const path = generateUrl(txMempoolEndpoint(url), {
    limit,
    offset,
  });
  return fetchJson<MempoolTransactionListResponse>(path);
}
 
/**
 * Get transaction by txid
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/transactions#fetchtransaction
 */
export async function fetchTransaction({
  txid,
  event_offset,
  event_limit,
  url,
}: EventListParams & { txid: string }) {
  const path = generateUrl(`${txEndpoint(url)}/${txid}`, {
    event_offset,
    event_limit,
  });
  return fetchJson<Transaction | MempoolTransaction>(path);
}
 
/**
 * Get raw transaction by id
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/transactions#fetchrawtransaction
 */
export async function fetchRawTransaction({ txid, url }: { txid: string; url: string }) {
  const path = `${txEndpoint(url)}/${txid}/raw`;
  return fetchJson<string>(path);
}
 
/**
 * Get transactions in a block by hash
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/transactions#fetchtransactionsbyblockhash
 */
export async function fetchTransactionsByBlockHash({
  block_hash,
  url,
  limit,
  offset,
}: BaseListParams & {
  block_hash: string;
}) {
  const path = `${txEndpoint(url)}/block/${block_hash}`;
  return fetchJson<TransactionResults>(generateUrl(path, { limit, offset }));
}
 
/**
 * Get transactions in a block by height
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/transactions#fetchtransactionsbyblockheight
 */
export async function fetchTransactionsByBlockHeight({
  block_height,
  url,
  limit,
  offset,
}: BaseListParams & {
  block_height: number;
}) {
  const path = `${txEndpoint(url)}/block_height/${block_height.toString(10)}`;
  return fetchJson<TransactionResults>(generateUrl(path, { limit, offset }));
}