All files / src/transactions/fetchers get-abi.ts

100% Statements 11/11
100% Branches 1/1
100% Functions 1/1
100% Lines 11/11

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    9x                     9x         2x       2x   2x 2x 1x 1x 1x   1x         1x    
import { StacksNetwork } from 'micro-stacks/network';
import { ClarityAbi } from 'micro-stacks/clarity';
import { fetchPrivate } from 'micro-stacks/common';
 
/**
 * Fetch a contract's ABI
 *
 * @param {string} address - the contracts address
 * @param {string} contractName - the contracts name
 * @param {StacksNetwork} network - the Stacks network to broadcast transaction to
 *
 * @returns {Promise} that resolves to a ClarityAbi if the operation succeeds
 */
export async function getAbi(
  address: string,
  contractName: string,
  network: StacksNetwork
): Promise<ClarityAbi> {
  const options = {
    method: 'GET',
  };
 
  const url = network.getAbiApiUrl(address, contractName);
 
  const response = await fetchPrivate(url, options);
  if (!response.ok) {
    let msg = '';
    try {
      msg = await response.text();
    } catch (error) {}
    throw new Error(
      `Error fetching contract ABI for contract "${contractName}" at address ${address}. Response ${response.status}: ${response.statusText}. Attempted to fetch ${url} and failed with the message: "${msg}"`
    );
  }
 
  return JSON.parse(await response.text()) as ClarityAbi;
}