tx.go

Overview

The `tx.go` file within the Thorchain coinstack is responsible for **parsing, synthesizing, and formatting Thorchain-specific transactions** from raw blockchain data. It bridges the gap between low-level blockchain events and high-level transaction representations consumable by the API layer and clients.

Thorchain's blockchain emits unique event types and message structures that require custom decoding beyond generic Cosmos SDK processing. This file implements functions to:

This file interacts closely with:


Detailed Explanation of Functions

1. GetTxHistory

func GetTxHistory(handler *cosmos.Handler, pubkey string, cursor string, pageSize int) (api.TxHistory, error)

Purpose

Retrieves a paginated list of transactions involving a specific public key (`pubkey`). It searches blocks for events associated with the pubkey, parses those events into transactions, and returns a structured transaction history.

Parameters

Returns

Implementation Details

Usage Example

txHistory, err := GetTxHistory(handler, "thor1xyz...", "", 25)
if err != nil {
    log.Fatal(err)
}
for _, tx := range txHistory.Txs {
    fmt.Println(tx.TxID, tx.BlockHeight)
}

2. ParseMessages

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

Purpose

Parses arrays of Cosmos SDK and Thorchain-specific messages, combining them with associated blockchain events to produce a list of enriched, structured messages. Handles special Thorchain message types and synthesizes additional messages based on event data.

Parameters

Returns

Implementation Details

Usage Example

messages := ParseMessages(txMsgs, txEvents)
for _, msg := range messages {
    fmt.Printf("Type: %s, From: %s, To: %s, Value: %s\n", msg.Type, msg.From, msg.To, msg.Value.Amount)
}

3. GetTxFromBlockEvents

func GetTxFromBlockEvents(eventCache map[string]interface{}, blockHeader types.Header, blockEvents []cosmos.ABCIEvent, eventIndex int, latestHeight int, denom string, nativeFee int) (*ResultTx, error)

Purpose

Creates a **synthetic transaction** from Thorchain block-end events, especially handling outbound transfers that do not correspond to explicit on-chain transactions.

Parameters

Returns

Implementation Details

Usage Example

tx, err := GetTxFromBlockEvents(eventCache, header, events, 0, latestHeight, "rune", 37500)
if err != nil {
    log.Fatal(err)
}
if tx != nil {
    // process synthetic tx
}

4. formatTx

func formatTx(tx *ResultTx) (*cosmos.Tx, error)

Purpose

Formats a `ResultTx` synthetic transaction into a standard `cosmos.Tx` structure compatible with the Cosmos SDK API framework.

Parameters

Returns

Implementation Details

Usage Example

cosmosTx, err := formatTx(resultTx)
if err != nil {
    log.Fatal(err)
}
fmt.Println(cosmosTx.TxID)

Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram: Function Interaction Flowchart

flowchart TD
    A[GetTxHistory] --> B[Search Blocks for Pubkey Events]
    B --> C[Fetch Block Results per Block]
    C --> D[For Each Block Event Index]
    D --> E[GetTxFromBlockEvents]
    E --> F[ParseBlockEvents (cache)]
    E --> G[Match Fee Event or Use Native Fee]
    E --> H[Create ResultTx (Synthetic Tx)]
    D --> I[Filter Tx by Pubkey]
    I --> J[Collect Transactions]
    J --> K[Return TxHistory]

    subgraph ParseMessages Flow
        L[ParseMessages]
        L --> M[Handle MsgSend]
        L --> N[Handle MsgDeposit]
        N --> O[Detect Withdraw/Refund/Outbound Events]
        N --> P[Create Synthetic Messages]
        L --> Q[Handle MsgObservedTxQuorum]
        L --> R[Handle Unhandled Messages (Cosmos Parser)]
    end

Summary

The `tx.go` file provides critical Thorchain-specific transaction parsing capabilities:

This file embodies the core logic that transforms raw Thorchain blockchain data into rich, structured transaction data, enabling client applications to present accurate and comprehensive transaction histories and details.


Appendix: Example Usage Scenario

// Initialize handler and required parameters
handler := cosmos.NewHandler(...)
pubkey := "thor1xyz..."
pageSize := 50

// Fetch transaction history for pubkey
txHistory, err := GetTxHistory(handler, pubkey, "", pageSize)
if err != nil {
    log.Fatalf("failed to get tx history: %v", err)
}

// Iterate and print transaction IDs
for _, tx := range txHistory.Txs {
    fmt.Printf("TxID: %s, Height: %d\n", tx.TxID, tx.BlockHeight)
}

// Parse messages from a transaction
for _, tx := range txHistory.Txs {
    messages := ParseMessages(tx.Messages, tx.Events)
    for _, msg := range messages {
        fmt.Printf("Message Type: %s, From: %s, To: %s, Value: %s\n",
            msg.Type, msg.From, msg.To, msg.Value.Amount)
    }
}

This example demonstrates retrieving and parsing transaction history for a given Thorchain address.


*End of documentation for `tx.go`.*