API Server

Purpose

The API Server subcomponent serves as the primary interface for clients to interact with the Thorchain blockchain data and related services. It addresses the need for:

This subtopic focuses on the Thorchain API server's unique responsibilities and capabilities that go beyond generic Cosmos SDK or blockchain API services, notably integrating affiliate revenue data and customized transaction handling specific to Thorchain.

Functionality

The API Server is implemented in Go and leverages several packages, including the Cosmos SDK abstraction and a custom affiliate fee indexer. Its key responsibilities and workflows include:

REST API Endpoints

WebSocket Endpoint

Internal Components and Flow

Example: Affiliate Revenue Query Flow

When a client requests affiliate revenue via [/api/v1/affiliate/revenue](/projects/291/69281), the server:

  1. Parses optional start and end query parameters, defaulting to epoch start and current time if omitted.

  2. Calls the handler's GetAffiliateRevenue method with these timestamps.

  3. Returns the aggregated revenue data or an error if the retrieval fails.

func (a *API) AffiliateRevenue(w http.ResponseWriter, r *http.Request) {
    start, err := strconv.Atoi(r.URL.Query().Get("start"))
    if err != nil {
        start = 0
    }

    end, err := strconv.Atoi(r.URL.Query().Get("end"))
    if err != nil {
        end = int(time.Now().UnixMilli())
    }

    affiliateRevenue, err := a.handler.GetAffiliateRevenue(start, end)
    if err != nil {
        api.HandleError(w, http.StatusInternalServerError, err.Error())
        return
    }

    api.HandleResponse(w, http.StatusOK, affiliateRevenue)
}

This integration of affiliate revenue data is unique to this subtopic and extends the parent topic's general Thorchain functionality.

Integration

The API Server acts as the central point of interaction for Thorchain blockchain data and services, tightly integrating with other subtopics:

This subtopic complements the parent topic by providing the API layer that exposes Thorchain blockchain data and affiliate revenue in a consistent, extensible, and developer-friendly manner.

Diagram

flowchart TD
    Client[Client Application]
    subgraph API Server
        REST[REST API Endpoints]
        WS[WebSocket Manager]
        Handler[Thorchain Handler]
        Indexer[Affiliate Fee Indexer]
        CosmosAPI[Cosmos SDK API Layer]
        Metrics[Prometheus Metrics Endpoint]
    end

    Client -->|HTTP Requests| REST
    Client -->|WebSocket| WS
    REST --> Handler
    WS --> Handler
    Handler --> CosmosAPI
    Handler --> Indexer
    REST --> Metrics

*Diagram Explanation:* Clients interact with the API Server via REST or WebSocket. The server's handler processes requests, leveraging the Cosmos SDK layer for generic blockchain data and the affiliate fee indexer for Thorchain-specific revenue data. Metrics are exposed for monitoring.