Unified API Layer

The Unified API Layer serves as a coherent and consistent interface that abstracts the underlying complexities of interacting with multiple blockchain networks. It provides standardized RESTful and WebSocket APIs that enable clients to efficiently retrieve blockchain data, submit transactions, and subscribe to real-time blockchain events across different blockchain types such as EVM-compatible chains and UTXO-based chains.


Overview and Purpose

Different blockchains have diverse data models, transaction formats, and node interaction protocols. The Unified API Layer exists to mask these differences behind a common set of API endpoints and data structures, enabling clients to interact with any supported blockchain with minimal changes to their code.

The core problems it solves include:


Core Concepts


How It Works: Key Functionalities and Workflows

BaseAPI Interface (node/coinstacks/common/api/src/index.ts)

The `BaseAPI` interface defines the minimal set of methods that each blockchain API must provide:

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>;
}

This interface ensures a baseline uniformity of API capabilities across blockchains.


EVM API Implementation


UTXO API Implementation


Interactions with Other System Components


Design Patterns and Approaches


Summary of File Relationships


Mermaid Sequence Diagram: Unified API Layer Request Flow

sequenceDiagram
  participant Client
  participant APIController
  participant APIService
  participant Blockbook
  participant NodeRPC
  participant GasOracle

  Client->>APIController: Send REST API request (e.g., getAccount)
  APIController->>APIService: Delegate request
  alt EVM Chain
    APIService->>Blockbook: Query token balances & tx history
    APIService->>GasOracle: Get gas fee estimates (if needed)
    APIService->>NodeRPC: Send transaction / trace calls (if needed)
  else UTXO Chain
    APIService->>Blockbook: Query address info, tx history, UTXOs
    APIService->>NodeRPC: Send raw transaction
  end
  APIService-->>APIController: Return formatted data
  APIController-->>Client: Send JSON response

This documentation details the Unified API Layer’s role as a foundational abstraction enabling consistent blockchain interaction through RESTful APIs, supporting both account-based and UTXO-based chains. Its modular design, interface conformity, and comprehensive service implementations facilitate scalable multi-blockchain support.