utils.ts


Overview

The [utils.ts](/projects/291/69086) file provides utility functions and configured HTTP clients for interacting with a blockchain indexer API. Its primary purpose is to fetch enriched transaction data for a given transaction ID (`txid`) from an external indexer service, handling retries and error conditions gracefully.

This file abstracts the complexity of making HTTP requests to the indexer, including retry logic and exponential backoff, and returns transactions in a structured format compatible with the system’s `Tx` model.


Detailed Explanations

Environment Variables

Both environment variables are mandatory, and the file throws an error on initialization if either is missing.


Constants / Instances

axiosNoRetry

axiosWithRetry


Functions

getTransaction

async function getTransaction(
  txid: string,
  shouldRetry?: boolean,
  retryCount = 0
): Promise<Tx>
import { getTransaction } from './utils'

async function example() {
  try {
    const tx = await getTransaction('some-transaction-id', true)
    console.log('Transaction:', tx)
  } catch (error) {
    console.error('Failed to fetch transaction:', error)
  }
}

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[getTransaction(txid, shouldRetry, retryCount)]
    A -->|shouldRetry = true| B[axiosWithRetry POST /transactions]
    A -->|shouldRetry = false| C[axiosNoRetry POST /transactions]
    B --> D{Response Data}
    C --> D
    D -->|tx found| E[Map EnrichedTransaction -> Tx]
    D -->|tx not found| F{retryCount < 5?}
    F -->|Yes| G[exponentialDelay]
    G --> A
    F -->|No| H[Throw 'Transaction not found' error]
    A -->|Catch error| I[handleError]

Summary

The [utils.ts](/projects/291/69086) file is a specialized utility module focused on retrieving enriched blockchain transaction data with robust retry and error handling mechanisms. It abstracts HTTP client configuration and retry logic, providing a clean interface (`getTransaction`) for other parts of the application to reliably fetch transaction details from an external indexer service. This file is a critical integration point ensuring the system can handle eventual consistency and delays inherent in blockchain data finalization.