api.go


Overview

`api.go` implements the Thorchain Unchained API server, providing REST and WebSocket endpoints to expose Thorchain blockchain data and related services. It builds upon a generic Cosmos SDK-based API framework by embedding a customized Thorchain-specific handler that extends Cosmos functionality with Thorchain-specific transaction parsing, synthetic transaction creation, and affiliate fee revenue tracking.

This file orchestrates:

By combining generic Cosmos API features with Thorchain-specific extensions, this file enables clients to interact with Thorchain blockchain data through a consistent and enriched API interface.


Detailed Explanation

Constants

Constant

Type

Description

PORT

`int`

HTTP server port (default: 3000)

`PPROF_PORT`

`int`

Profiling server port (default: 3001)

`GRACEFUL_SHUTDOWN`

`time.Duration`

Timeout for graceful server shutdown (15 seconds)

`WRITE_TIMEOUT`

`time.Duration`

HTTP server write timeout (15 seconds)

`READ_TIMEOUT`

`time.Duration`

HTTP server read timeout (15 seconds)

`IDLE_TIMEOUT`

`time.Duration`

HTTP server idle timeout (60 seconds)


Type: API

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

Function: New

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

Purpose

Constructs and initializes a new Thorchain API server instance.

Parameters

Parameter

Type

Description

`cfg`

`cosmos.Config`

Configuration specifying chain-specific parameters such as denomination and native fees.

`httpClient`

`*cosmos.HTTPClient`

HTTP client for querying blockchain data and indexer services.

`wsClient`

`*cosmos.WSClient`

WebSocket client for subscribing to blockchain events.

`blockService`

`*cosmos.BlockService`

Service for fetching block data.

`indexer`

`*AffiliateFeeIndexer`

Indexer responsible for tracking affiliate fee events.

`swaggerPath`

`string`

File system path to the Swagger JSON specification.

`prometheus`

`*metrics.Prometheus`

Prometheus metrics collector instance.

Returns

Usage

api := api.New(cfg, httpClient, wsClient, blockService, affiliateIndexer, "/path/to/swagger.json", prometheus)

Description


HTTP Handler Methods

All these methods delegate to the embedded `cosmos.API` methods, except affiliate revenue which is custom.

Websocket

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

Info

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

Account

func (a *API) Account(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)

SendTx

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

EstimateGas

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

AffiliateRevenue

func (a *API) AffiliateRevenue(w http.ResponseWriter, r *http.Request)
Example Request
GET /api/v1/affiliate/revenue?start=1654041600000&end=1656633600000
Example Response
{
  "total_revenue": "123456789000",
  "affiliate_addresses": ["addr1...", "addr2..."]
}

Important Implementation Details


Interaction with Other Components


Visual 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:**


Summary

`api.go` is the entry point and main server setup file for the Thorchain Unchained API. It configures and exposes REST and WebSocket endpoints to serve Thorchain blockchain information, transaction submission, gas estimation, and affiliate revenue statistics. This file extends a generic Cosmos SDK API framework with Thorchain-specific logic and integrates critical components like the affiliate fee indexer and WebSocket event manager. Its modular and middleware-rich design ensures scalability, observability, and developer usability.


Example Usage

package main

import (
    "github.com/shapeshift/unchained/go/coinstacks/thorchain/api"
    "github.com/shapeshift/unchained/pkg/cosmos"
    "github.com/shapeshift/unchained/pkg/metrics"
)

func main() {
    cfg := cosmos.Config{Denom: "rune", NativeFee: 5000}
    httpClient := cosmos.NewHTTPClient(cfg)
    wsClient := cosmos.NewWSClient(cfg)
    blockService := cosmos.NewBlockService(httpClient)
    affiliateIndexer := api.NewAffiliateFeeIndexer()
    prometheus := metrics.NewPrometheus()

    server := api.New(cfg, httpClient, wsClient, blockService, affiliateIndexer, "./swagger.json", prometheus)

    // Start server (blocking call)
    server.ListenAndServe()
}

End of Documentation for api.go