index.ts


Overview

The `index.ts` file serves as the central entry point and interface definition for the Unified API Layer that standardizes blockchain interactions across multiple coinstacks. It exposes common models, utilities, middleware, and protocol-specific namespaces (`evm` and `utxo`) to provide a unified development experience.

Most importantly, it defines the `BaseAPI` interface—an abstract contract that all blockchain coinstack API implementations must conform to. This interface ensures consistency by prescribing key methods for retrieving chain info, account data, transaction history, and submitting transactions.

Additionally, the file defines a generic `ApiError` class for uniform error handling across all API implementations.


Exports and Imports

Imports

Re-exports

This structure provides a modular, organized API surface for different blockchain types and shared utilities.


Classes

ApiError

export class ApiError extends Error {
  statusText: string
  statusCode: number

  constructor(statusText: string, statusCode: number, message?: string) {
    super(message)
    this.name = this.constructor.name
    this.statusText = statusText
    this.statusCode = statusCode
  }
}

Purpose

`ApiError` extends the native JavaScript `Error` class to provide a consistent way to represent HTTP API errors. It captures:

Usage Example

throw new ApiError('Not Found', 404, 'The requested account does not exist');

This class can be used by API controllers and services to throw errors that middleware can catch and format into standardized HTTP responses.


Interfaces

BaseAPI

export interface BaseAPI {
  getInfo(): Promise<BaseInfo>
  getAccount(pubkey: string): Promise<BaseAccount>
  getTxHistory(pubkey: string, cursor?: string, pageSize?: number): Promise<BaseTxHistory>
  sendTx(body: SendTxBody): Promise<string>
}

Purpose

`BaseAPI` defines the minimal set of asynchronous methods that all blockchain coinstack API implementations must provide. It abstracts the specifics of different blockchain protocols behind a standardized interface.

Methods

Method

Parameters

Returns

Description

`getInfo`

None

`Promise`

Retrieves metadata about the running coinstack (e.g., network info).

`getAccount`

`pubkey: string` - account address or extended public key

`Promise`

Fetches detailed account information by address or xpub.

`getTxHistory`

`pubkey: string` - account address or extended public key
`cursor?: string` - optional pagination cursor
`pageSize?: number` - optional page size

`Promise`

Retrieves paginated transaction history for an account.

`sendTx`

`body: SendTxBody` - serialized raw transaction hex

`Promise`

Submits a raw transaction to be broadcast; returns transaction ID.

Implementation Notes

Usage Example

class EvmApi implements BaseAPI {
  async getInfo(): Promise<BaseInfo> {
    // Implementation specific to EVM chain
  }

  async getAccount(pubkey: string): Promise<BaseAccount> {
    // Fetch account details
  }

  async getTxHistory(pubkey: string, cursor?: string, pageSize?: number): Promise<BaseTxHistory> {
    // Fetch transaction history with pagination
  }

  async sendTx(body: SendTxBody): Promise<string> {
    // Broadcast raw transaction hex
  }
}

Important Implementation Details


Interaction with Other System Components


Visual Diagram

flowchart TD
    A[BaseAPI Interface] -->|Implements| B[EVM API Implementation]
    A -->|Implements| C[UTXO API Implementation]

    subgraph Exports
        D[Models]
        E[Middleware]
        F[WebSocket]
        G[Registry]
        H[Prometheus]
        I[Utils]
        J[EVM Namespace]
        K[UTXO Namespace]
    end

    L[ApiError Class] -->|Used by| B
    L -->|Used by| C

    B -->|Uses| D
    B -->|Uses| E
    B -->|Uses| I

    C -->|Uses| D
    C -->|Uses| E
    C -->|Uses| I

    index_ts["index.ts"] --> Exports
    index_ts --> A
    index_ts --> L

Summary

The `index.ts` file is a foundational module in the Unified API Layer, establishing:

This design enforces uniformity and modularity across diverse blockchain protocols, enabling scalable development and maintenance of the multi-blockchain Unified API Layer.


References


End of Documentation for index.ts