handler.go

Overview

The [handler.go](/projects/291/69233) file defines the core request handling mechanism for interacting with the Cosmos blockchain network within the application. It provides the primary interface and implementation for processing both WebSocket and REST API requests related to blockchain data such as accounts, transactions, staking, and fees.

The file defines the `RouteHandler` interface which outlines the essential methods for WebSocket lifecycle management and REST endpoints. The main concrete implementation is the `Handler` struct, which integrates coin-specific logic alongside common Cosmos SDK interactions.

This handler encapsulates complex blockchain operations such as transaction decoding, event parsing, fee calculation, and staking queries, providing a clean and reusable abstraction layer between the application and the underlying Cosmos SDK and Tendermint RPC clients.


Classes and Interfaces

RouteHandler Interface

Defines the contract for handling blockchain-related requests:

Method

Description

`StartWebsocket() error`

Starts the WebSocket client to listen for new blockchain events and transactions.

`StopWebsocket()`

Stops the WebSocket client and closes connections.

`NewWebsocketConnection(conn *ws.Conn, manager *websocket.Manager)`

Registers and starts a new WebSocket connection.

`GetInfo() (api.Info, error)`

Retrieves basic blockchain info (e.g., network name).

`GetAccount(pubkey string) (api.Account, error)`

Fetches account details by public key/address.

`GetTxHistory(pubkey string, cursor string, pageSize int) (api.TxHistory, error)`

Retrieves transaction history for an account.

`GetTx(txid string) (api.Tx, error)`

Retrieves a specific transaction by its ID.

`SendTx(hex string) (string, error)`

Broadcasts a raw transaction hex to the network.

`EstimateGas(rawTx string) (string, error)`

Estimates gas usage of a raw transaction.


CoinSpecificHandler Interface

Defines coin-specific parsing methods that must be implemented by handlers tailored to particular Cosmos-based chains:

Method

Description

`ParseMessages([]sdk.Msg, EventsByMsgIndex) []Message`

Parses blockchain messages from a transaction with event data.

`ParseFee(tx SigningTx, txid string) Value`

Parses and calculates the transaction fee.


Handler Struct

The primary implementation of `RouteHandler`, combining generic Cosmos SDK logic with coin-specific behavior.

Field

Type

Description

`ParseMessages`

`func([]sdk.Msg, EventsByMsgIndex) []Message`

Coin-specific function for parsing messages.

`ParseFee`

`func(tx SigningTx, txid string) Value`

Coin-specific function for parsing fees.

`HTTPClient`

`APIClient`

Client for making HTTP API calls to the Cosmos node.

`WSClient`

`*WSClient`

WebSocket client for real-time blockchain event subscriptions.

`BlockService`

`*BlockService`

Service to fetch block data by height.

`Denom`

`string`

The denomination (token symbol) used in the blockchain.

`NativeFee`

`int`

Native fee unit value for transactions.


Functions and Methods

ValidateCoinSpecific(handler interface{}) error


NewWebsocketConnection(conn *ws.Conn, manager *websocket.Manager)


StartWebsocket() error


StopWebsocket()


GetInfo() (api.Info, error)


GetAccount(pubkey string) (api.Account, error)


GetTxHistory(pubkey string, cursor string, pageSize int) (api.TxHistory, error)


GetValidatorTxHistory(pubkey string, cursor string, pageSize int) (api.TxHistory, error)


GetTx(txid string) (api.Tx, error)


SendTx(hex string) (string, error)


EstimateGas(rawTx string) (string, error)


GetStaking(pubkey string, apr *big.Float) (*Staking, error)


FormatTx(tx *coretypes.ResultTx) (*Tx, error)


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
    class RouteHandler {
        +StartWebsocket() error
        +StopWebsocket()
        +NewWebsocketConnection(conn *ws.Conn, manager *websocket.Manager)
        +GetInfo() (api.Info, error)
        +GetAccount(pubkey string) (api.Account, error)
        +GetTxHistory(pubkey string, cursor string, pageSize int) (api.TxHistory, error)
        +GetTx(txid string) (api.Tx, error)
        +SendTx(hex string) (string, error)
        +EstimateGas(rawTx string) (string, error)
    }

    class CoinSpecificHandler {
        +ParseMessages([]sdk.Msg, EventsByMsgIndex) []Message
        +ParseFee(tx SigningTx, txid string) Value
    }

    class Handler {
        +ParseMessages func([]sdk.Msg, EventsByMsgIndex) []Message
        +ParseFee func(tx SigningTx, txid string) Value
        +HTTPClient APIClient
        +WSClient *WSClient
        +BlockService *BlockService
        +Denom string
        +NativeFee int

        +ValidateCoinSpecific(handler interface{}) error
        +NewWebsocketConnection(conn *ws.Conn, manager *websocket.Manager)
        +StartWebsocket() error
        +StopWebsocket()
        +GetInfo() (api.Info, error)
        +GetAccount(pubkey string) (api.Account, error)
        +GetTxHistory(pubkey string, cursor string, pageSize int) (api.TxHistory, error)
        +GetValidatorTxHistory(pubkey string, cursor string, pageSize int) (api.TxHistory, error)
        +GetTx(txid string) (api.Tx, error)
        +SendTx(hex string) (string, error)
        +EstimateGas(rawTx string) (string, error)
        +GetStaking(pubkey string, apr *big.Float) (*Staking, error)
        +FormatTx(tx *coretypes.ResultTx) (*Tx, error)
    }

    RouteHandler <|.. Handler
    CoinSpecificHandler <|.. Handler

Summary

The [handler.go](/projects/291/69233) file provides a robust, extensible framework for interacting with the Cosmos blockchain in both real-time and RESTful manners. It cleanly separates coin-specific logic from general blockchain operations, leverages concurrent calls for efficient data retrieval, and integrates tightly with WebSocket and HTTP clients to offer a comprehensive blockchain API surface.

This modular design supports easy adaptation to different Cosmos SDK-based blockchains by injecting coin-specific parsing logic while maintaining a unified handler interface for the application.