tx.go


Overview

The [tx.go](/projects/291/69258) file is part of the Cosmos SDK client package and provides functionalities related to transaction (tx) handling via HTTP requests and transaction processing utilities. It primarily facilitates:

This file acts as a bridge between the Cosmos SDK transaction data structures and external access via REST or RPC, making it essential for wallet services, blockchain explorers, or any service interacting with Cosmos-based blockchains.


Detailed Documentation

HTTPClient Methods

These methods are defined on the `HTTPClient` type (not shown in this file but assumed to exist) and are responsible for interacting with CometBFT RPC and Cosmos LCD endpoints.


func (c *HTTPClient) GetTxHistory(address string, cursor string, pageSize int, sources map[string]*TxState) (*TxHistoryResponse, error)

Fetches paginated transaction history for a specific blockchain address. It supports multiple "sources" to track pagination state per source.

history, err := client.GetTxHistory("cosmos1...", "", 20, initialSources)
if err != nil {
    log.Fatal(err)
}
// Process history.Txs ...

func (c *HTTPClient) GetTx(txid string) (*coretypes.ResultTx, error)

Retrieves a transaction by its transaction hash.

tx, err := client.GetTx("ABC123...")
if err != nil {
    log.Fatal(err)
}
fmt.Println(tx.TxResult)

func (c *HTTPClient) TxSearch(query string, page int, pageSize int) (*coretypes.ResultTxSearch, error)

Searches transactions matching a given query with pagination.

result, err := client.TxSearch("tx.height > 1000", 1, 10)
if err != nil {
    log.Fatal(err)
}
for _, tx := range result.Txs {
    fmt.Println(tx.TxHash)
}

func (c *HTTPClient) BroadcastTx(rawTx string) (string, error)

Broadcasts a raw base64 encoded transaction to the blockchain synchronously.

txHash, err := client.BroadcastTx(base64Tx)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Broadcasted with hash: %s\n", txHash)

Utility Functions and Types


func ParseEvents(txResult abcitypes.ExecTxResult) EventsByMsgIndex

Parses ABCI transaction execution results to extract structured events indexed by message index.


func ParseMessages(msgs []sdk.Msg, events EventsByMsgIndex) []Message

Converts raw Cosmos SDK messages and their associated events into a slice of simplified `Message` structs for easier interpretation.


func Fee(tx SigningTx, txid string, denom string) Value

Extracts the fee information from a transaction.


func DecodeTx(encoding params.EncodingConfig, rawTx interface{}) (sdk.Tx, SigningTx, error)

Decodes a raw transaction input into Cosmos SDK transaction interfaces.

tx, signingTx, err := DecodeTx(encodingConfig, base64RawTx)
if err != nil {
    log.Fatal(err)
}

func GetTxAddrs(events EventsByMsgIndex, messages []Message) []string

Extracts all unique addresses involved in a transaction from both events and messages.


Important Implementation Details and Algorithms


Interactions with Other Parts of the System


Visual Diagram

flowchart TD
    A[HTTPClient] -->|GetTxHistory| B[History]
    B -->|decode cursor| C[Cursor]
    C --> D[CursorState (per source)]
    A -->|GetTx| E[/tx RPC endpoint/]
    A -->|TxSearch| F[/tx_search RPC endpoint/]
    A -->|BroadcastTx| G[/cosmos/tx/v1beta1/txs LCD endpoint/]

    subgraph Parsing
        H[ParseEvents] --> I[EventsByMsgIndex]
        J[ParseMessages] --> K[[]Message]
        L[GetTxAddrs] --> M[[]string (addresses)]
        N[DecodeTx] --> O[sdk.Tx, SigningTx]
        P[Fee] --> Q[Value (fee info)]
    end

Summary

[tx.go](/projects/291/69258) is a core utility file that enables querying, decoding, parsing, and broadcasting Cosmos SDK transactions via HTTP clients. It abstracts complex transaction handling logic, event parsing, and message interpretation, facilitating integration with Cosmos-based blockchains. The file's careful design ensures robustness against protocol variations and provides clear, structured data for downstream applications.