All files / src/api/accounts fetchers.ts

50% Statements 23/46
9.09% Branches 1/11
35.71% Functions 5/14
50% Lines 23/46

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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                              10x             10x           10x           10x                                     10x                 1x 1x           1x       10x                                     10x 1x 1x       10x                                 10x 1x 1x       10x 1x 1x       10x 1x 1x         10x                                                                              
import { AccountBase, AccountListOptions } from './types';
import {
  AddressBalanceResponse,
  AddressStxBalanceResponse,
  AddressTransactionsListResponse,
  AddressTransactionsWithTransfersListResponse,
  AddressAssetsListResponse,
  MempoolTransactionListResponse,
  MempoolTransaction,
  AddressTransactionWithTransfers,
  AddressNonces,
  AddressStxInboundListResponse,
  AddressNftListResponse,
  AccountDataResponse,
} from '@stacks/stacks-blockchain-api-types';
import { addressEndpoint, fetchJson, generateUrl, txMempoolEndpoint, txEndpoint } from '../utils';
 
type WithHeight<T> = T & {
  height?: number;
};
 
// @see https://docs.micro-stacks.dev/modules/core/api/accounts#fetchaccountbalances
export async function fetchAccountBalances({ url, principal }: AccountBase) {
  const path = `${addressEndpoint(url)}/${principal}/balances`;
  return fetchJson<AddressBalanceResponse>(path);
}
 
// @see https://docs.micro-stacks.dev/modules/core/api/accounts#fetchaccountstxbalance
export async function fetchAccountStxBalance({ url, principal }: AccountBase) {
  const path = `${addressEndpoint(url)}/${principal}/stx`;
  return fetchJson<AddressStxBalanceResponse>(path);
}
 
// @see https://docs.micro-stacks.dev/modules/core/api/accounts#fetchaccounttransactions
export async function fetchAccountTransactions({
  url,
  principal,
  limit,
  offset = 0,
  height,
  unanchored,
}: WithHeight<AccountListOptions>) {
  const basePath = `${addressEndpoint(url)}/${principal}/transactions`;
  const path = generateUrl(basePath, {
    limit,
    offset,
    height,
    unanchored,
  });
  return fetchJson<AddressTransactionsListResponse>(path);
}
 
//See https://docs.hiro.so/api#operation/get_single_transaction_with_transfers
export async function fetchAccountTransactionWithTransfers({
  url,
  principal,
  tx_id,
  limit,
  offset = 0,
  height,
  unanchored,
}: { tx_id: string } & WithHeight<AccountListOptions>) {
  const basePath = `${addressEndpoint(url)}/${principal}/${tx_id}/transactions_with_transfers`;
  const path = generateUrl(basePath, {
    limit,
    offset,
    height,
    unanchored,
  });
  return fetchJson<AddressTransactionWithTransfers>(path);
}
 
// @see https://docs.micro-stacks.dev/modules/core/api/accounts#fetchaccounttransactionswithtransfers
export async function fetchAccountTransactionsWithTransfers({
  url,
  principal,
  limit,
  offset = 0,
  height,
  unanchored,
}: WithHeight<AccountListOptions>) {
  const basePath = `${addressEndpoint(url)}/${principal}/transactions_with_transfers`;
  const path = generateUrl(basePath, {
    limit,
    offset,
    height,
    unanchored,
  });
  return fetchJson<AddressTransactionsWithTransfersListResponse>(path);
}
 
// https://docs.hiro.so/api#operation/get_account_nonces
export async function fetchAccountNonces({ url, principal }: AccountBase) {
  const path = `${addressEndpoint(url)}/${principal}/nonces`;
  return fetchJson<AddressNonces>(path);
}
 
// @see https://docs.micro-stacks.dev/modules/core/api/accounts#fetchaccountassets
export async function fetchAccountAssets({
  url,
  principal,
  limit,
  offset = 0,
  unanchored,
}: AccountListOptions) {
  const basePath = `${addressEndpoint(url)}/${principal}/assets`;
  const path = generateUrl(basePath, {
    limit,
    offset,
    unanchored,
  });
  return fetchJson<AddressAssetsListResponse>(path);
}
 
// https://docs.hiro.so/api#operation/get_account_inbound (does this replaces the fetchaccountmempooltransactions endpoint ? )
export async function fetchAccountStxInbound({ url, principal }: AccountBase) {
  const path = `${addressEndpoint(url)}/${principal}/stx_inbound`;
  return fetchJson<AddressStxInboundListResponse>(path);
}
 
// https://docs.hiro.so/api#operation/get_account_nft
export async function fetchAccountNftEvents({ url, principal }: AccountBase) {
  const path = `${addressEndpoint(url)}/${principal}/nft_events`;
  return fetchJson<AddressNftListResponse>(path);
}
 
// https://docs.hiro.so/api#operation/get_account_info
export async function fetchAccountInfo({ url, principal }: AccountBase) {
  const path = `${addressEndpoint(url)}/${principal}`;
  return fetchJson<AccountDataResponse>(path);
}
 
// DEPRECATED?
// @see https://docs.micro-stacks.dev/modules/core/api/accounts#fetchaccountmempooltransactions
export async function fetchAccountMempoolTransactions({
  url,
  principal,
  limit,
  offset = 0,
}: AccountListOptions) {
  const basePath = `${txMempoolEndpoint(url)}`;
  const isContract = principal?.includes('.');
  if (!isContract) {
    const path = generateUrl(basePath, {
      limit,
      offset,
      address: principal,
    });
    return fetchJson<MempoolTransactionListResponse>(path);
  } else {
    const path = generateUrl(basePath, {
      limit,
      offset,
    });
    const data = await fetchJson<MempoolTransactionListResponse>(path);
    const results = await Promise.all(
      data.results
        .filter(tx => JSON.stringify(tx).includes(principal))
        .map(async tx =>
          fetchTx<MempoolTransaction>({
            url,
            txid: tx.tx_id,
          })
        )
    );
 
    return { ...data, results };
  }
}
 
async function fetchTx<T>({ url, txid }: { url: string; txid: string }) {
  return fetchJson<T>(`${txEndpoint(url)}/${txid}`);
}