All files / src/api/smartcontracts fetchers.ts

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

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                                      10x               10x         1x 1x                 10x             1x         1x                                 10x           1x           1x               10x                               1x 1x       1x                 10x                       1x             1x    
import type {
  AbstractTransaction,
  ContractInterfaceResponse,
  TransactionEventSmartContractLog,
  TransactionEventStxLock,
  TransactionEventStxAsset,
  TransactionEventFungibleAsset,
  TransactionEventNonFungibleAsset,
  ContractSourceResponse,
  MapEntryResponse,
} from '@stacks/stacks-blockchain-api-types';
import { BaseListParams } from '../types';
import {
  fetchJson,
  fetchJsonPost,
  generateUrl,
  contractEndpoint,
  contractsEndpoint,
  v2Endpoint,
} from '../utils';
 
/**
 * Get contract info using the contract ID
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/smart-contracts#fetchcontractbyid
 */
 
export async function fetchContractById({
  url,
  contract_id,
  unanchored,
}: BaseListParams & { contract_id: string; unanchored: boolean }) {
  const path = generateUrl(`${contractEndpoint(url)}/${contract_id}`, { unanchored });
  return fetchJson<AbstractTransaction>(path);
}
 
/**
 * Get contract events using a contract ID
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/smart-contracts#fetchcontracteventsbyid
 */
 
export async function fetchContractEventsById({
  url,
  contract_id,
  limit,
  offset,
  unanchored,
}: BaseListParams & { contract_id: string; unanchored: boolean }) {
  const path = generateUrl(`${contractEndpoint(url)}/${contract_id}/events`, {
    limit,
    offset,
    unanchored,
  });
  return fetchJson<
    (
      | TransactionEventSmartContractLog
      | TransactionEventStxLock
      | TransactionEventStxAsset
      | TransactionEventFungibleAsset
      | TransactionEventNonFungibleAsset
    )[]
  >(path);
}
 
/**
 * Get contract interface using a contract_address and contract name
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/smart-contracts#fetchcontractinterface
 */
 
export async function fetchContractInterface({
  url,
  contract_address,
  contract_name,
  tip,
}: BaseListParams & { contract_address: string; contract_name: string; tip: string }) {
  const path = generateUrl(
    `${contractsEndpoint(url)}/interface/${contract_address}/${contract_name}`,
    {
      tip,
    }
  );
  return fetchJson<ContractInterfaceResponse>(path);
}
 
/**
 * Get specific data-map inside a contract
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/smart-contracts#fetchcontractdatamapentry
 */
export async function fetchContractDataMapEntry({
  url,
  contract_name,
  contract_address,
  map_name,
  proof,
  tip,
  lookup_key,
}: BaseListParams & {
  contract_name: string;
  contract_address: string;
  map_name: string;
  proof: number;
  tip: string;
  lookup_key: string;
}) {
  const body = lookup_key;
  const path = generateUrl(
    `${v2Endpoint(url)}/map_entry/${contract_address}/${contract_name}/${map_name}`,
    { proof: proof, tip: tip }
  );
  return fetchJsonPost<MapEntryResponse>(path, { body });
}
 
/**
 * Get contract source
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/smart-contracts#fetchcontractsource
 */
 
export async function fetchContractSource({
  url,
  contract_address,
  contract_name,
  proof,
  tip,
}: BaseListParams & {
  contract_address: string;
  contract_name: string;
  proof: number;
  tip: string;
}) {
  const path = generateUrl(
    `${contractsEndpoint(url)}/source/${contract_address}/${contract_name}`,
    {
      proof,
      tip,
    }
  );
  return fetchJson<ContractSourceResponse>(path);
}