api.go

Overview

The [api.go](/projects/291/69232) file defines the HTTP API layer for the Cosmos blockchain service within the application. It provides RESTful endpoints and WebSocket support to interact with Cosmos blockchain data such as account information, transaction history, transaction submission, and gas estimation.

The core component is the `API` struct, which acts as an HTTP handler integrating with a `RouteHandler` (business logic interface) and a WebSocket connection manager. The file manages incoming HTTP requests, upgrades connections to WebSockets when required, validates query parameters, and formats responses or errors consistently.

Key features include:

This file is a crucial bridge between the client-facing API and the internal blockchain processing logic.


Detailed Descriptions

Constants and Variables

Name

Description

`GRACEFUL_SHUTDOWN`

Duration (15s) to wait for graceful server shutdown.

DEFAULT_PAGE_SIZE_VALIDATORS

Default page size for validators endpoint (not used in this file).

DEFAULT_PAGE_SIZE_TX_HISTORY

Default page size when fetching transaction history (10).

MAX_PAGE_SIZE_TX_HISTORY

Maximum allowed page size for transaction history queries (100).

`upgrader`

WebSocket upgrader from Gorilla WebSocket with buffer size and origin check.


Type: API

The `API` struct encapsulates the HTTP API handler for Cosmos blockchain endpoints.

Fields:

Field

Type

Description

`handler`

`RouteHandler`

Interface to blockchain business logic and data access.

`manager`

`*websocket.Manager`

Manages WebSocket connections and message routing.

`server`

`*http.Server`

HTTP server serving incoming API requests.


Functions and Methods

New(handler RouteHandler, manager *websocket.Manager, server *http.Server) *API

Constructor function to create a new `API` instance.


(a *API) Serve(errChan chan<- error)

Starts serving the API by launching the WebSocket and HTTP server.


(a *API) Shutdown()

Gracefully shuts down the HTTP server and stops WebSocket connections.


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

Root endpoint handler.


(a *API) ValidatePagingParams(w http.ResponseWriter, r *http.Request, defaultPageSize int, maxPageSize *int) (string, int, error)

Validates and parses paging query parameters `cursor` and `pageSize`.


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

Handles upgrading an HTTP connection to WebSocket and managing the new connection.


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

Returns general blockchain node information.


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

Returns account details for a given public key.


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

Returns a paginated list of transactions for a given public key.


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

Returns details for a specific transaction by transaction ID.


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

Submits a raw transaction to the blockchain network.


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

Estimates the gas fee required for a raw transaction.


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class API {
        -handler: RouteHandler
        -manager: *websocket.Manager
        -server: *http.Server
        +New(handler, manager, server) *API
        +Serve(errChan chan<- error)
        +Shutdown()
        +Root(w http.ResponseWriter, r *http.Request)
        +ValidatePagingParams(w http.ResponseWriter, r *http.Request, defaultPageSize int, maxPageSize *int) (string, int, error)
        +Websocket(w http.ResponseWriter, r *http.Request)
        +Info(w http.ResponseWriter, r *http.Request)
        +Account(w http.ResponseWriter, r *http.Request)
        +TxHistory(w http.ResponseWriter, r *http.Request)
        +Tx(w http.ResponseWriter, r *http.Request)
        +SendTx(w http.ResponseWriter, r *http.Request)
        +EstimateGas(w http.ResponseWriter, r *http.Request)
    }

    API --> RouteHandler : uses
    API --> websocket.Manager : manages
    API --> http.Server : serves
    API ..> api.HandleError : uses utility
    API ..> api.HandleResponse : uses utility

Summary

The [api.go](/projects/291/69232) file implements the HTTP API for Cosmos blockchain interactions, providing endpoints for querying blockchain data, managing transactions, and supporting WebSocket connections. It is designed with clean separation of concerns, delegating business logic to a `RouteHandler` interface, and maintaining consistent request validation, error handling, and graceful server lifecycle management. This file integrates tightly with the WebSocket manager and HTTP server to deliver a responsive, scalable API service.