api.go

Overview

The [api.go](/projects/291/69232) file implements the HTTP API server for accessing the Thorchain-V1 blockchain data through the unchained backend service. It exposes RESTful endpoints that provide blockchain information, transaction history, and transaction details using JSON over HTTP. This file sets up routing, middleware, server configuration, and integration with other system components such as metrics, websockets, and CORS handling.

Key features include:

This file acts as the entry point for the Thorchain-V1 API server and wraps underlying cosmos API functionality with Thorchain-specific logic.


Package and Imports


Constants

Constant

Type

Description

PORT

`int`

HTTP server port (3000)

`PPROF_PORT`

`int`

Profiling server port (3001)

`GRACEFUL_SHUTDOWN`

`time.Duration`

Duration for graceful shutdown (15s)

`WRITE_TIMEOUT`

`time.Duration`

HTTP write timeout (15s)

`READ_TIMEOUT`

`time.Duration`

HTTP read timeout (15s)

`IDLE_TIMEOUT`

`time.Duration`

HTTP idle timeout (60s)


Types

API struct

type API struct {
    *cosmos.API
    handler *Handler
}

Functions

New

func New(
    cfg cosmos.Config,
    httpClient *cosmos.HTTPClient,
    wsClient *cosmos.WSClient,
    blockService *cosmos.BlockService,
    swaggerPath string,
    prometheus *metrics.Prometheus,
) *API

Description

Creates and configures a new Thorchain-V1 API server instance. Sets up HTTP routing, middleware, metrics, websocket management, profiling, and server timeouts.

Parameters

Parameter

Type

Description

`cfg`

`cosmos.Config`

Configuration including denomination and fees

`httpClient`

`*cosmos.HTTPClient`

HTTP client for blockchain node communication

`wsClient`

`*cosmos.WSClient`

Websocket client for real-time blockchain data

`blockService`

`*cosmos.BlockService`

Service to fetch block data

`swaggerPath`

`string`

File path to OpenAPI Swagger specification

`prometheus`

`*metrics.Prometheus`

Prometheus metrics collector

Returns

Usage Example

cfg := cosmos.Config{
    Denom: "rune",
    NativeFee: 0.0001,
}
httpClient := cosmos.NewHTTPClient(...)
wsClient := cosmos.NewWSClient(...)
blockService := cosmos.NewBlockService(...)
prom := metrics.NewPrometheus()

apiServer := api.New(cfg, httpClient, wsClient, blockService, "./swagger.json", prom)

Implementation Highlights


API Methods

These methods implement the handlers for various API endpoints, essentially forwarding calls to the embedded `cosmos.API`.

Info

func (a *API) Info(w http.ResponseWriter, r *http.Request)

TxHistory

func (a *API) TxHistory(w http.ResponseWriter, r *http.Request)

Tx

func (a *API) Tx(w http.ResponseWriter, r *http.Request)

Implementation Details and Algorithms


Interaction with Other System Components


API Endpoint Summary

HTTP Method

Path

Handler

Description

GET

`/`

`api.DocsRedirect`

Redirects to API docs

GET

`/health`

Inline handler

Health check returning status

GET

`/metrics`

`promhttp.Handler`

Exposes Prometheus metrics

GET

`/swagger`

Inline handler

Serves OpenAPI Swagger JSON

GET

`/docs/`

Static FileServer

Serves Swagger UI static assets

GET

`/api/v1/info`

`API.Info`

Returns coinstack info

GET

`/api/v1/account/{pubkey}/txs`

`API.TxHistory`

Gets paginated tx history

GET

`/api/v1/tx/{txid}`

`API.Tx`

Gets transaction details


Visual Diagram

flowchart TD
    subgraph API Server Initialization
        A[New(cfg, httpClient, wsClient, blockService, swaggerPath, prometheus)] --> B[Create Gorilla Mux Router]
        B --> C[Create Thorchain Handler]
        C --> D[Create Websocket Manager]
        D --> E[Create HTTP Server with CORS and Timeouts]
        E --> F[Setup pprof Server (Goroutine)]
        F --> G[Register Middleware (Scheme, Logger)]
        G --> H[Register Routes (/health, /metrics, /swagger, /docs, /api/v1/*)]
        H --> I[Return API Instance]
    end

    subgraph Request Handling
        I --> J[Incoming HTTP Request]
        J --> K{Route Match}
        K -->|/api/v1/info| L[API.Info -> cosmos.API.Info]
        K -->|/api/v1/account/{pubkey}/txs| M[API.TxHistory -> cosmos.API.TxHistory]
        K -->|/api/v1/tx/{txid}| N[API.Tx -> cosmos.API.Tx]
        K -->|Other| O[Static or Redirect Handlers]
    end

Summary

The [api.go](/projects/291/69232) file is the backbone of the Thorchain-V1 unchained API server. It configures and exposes a set of RESTful endpoints backed by a cosmos-based blockchain API, enhanced with Thorchain-specific logic. Its modular design leverages middleware, websocket management, metrics, and profiling to provide a robust, observable, and maintainable API service. This file fits into the larger system as the HTTP interface layer that clients and services interact with to query blockchain data and transactions.