types.go

Overview

The [types.go](/projects/291/69257) file defines core data structures, interfaces, and implementations used within the Cosmos blockchain ecosystem integration layer. It primarily provides typed responses for blockchain queries (e.g., account info, balances, blocks, validators), transaction history handling, and event abstraction. The file bridges types from the CometBFT RPC layer and Cosmos SDK types, enabling uniform handling of blockchain data and events.

This file plays a foundational role in representing blockchain-related data structures and their behaviors, facilitating communication between blockchain nodes and higher-level application logic or client interfaces.


Detailed Documentation

Structs

AccountResponse

Represents the essential account information retrieved from the blockchain.

Field

Type

Description

Address

string

The Bech32 encoded account address.

AccountNumber

int

The unique account number (nonce).

Sequence

int

The transaction sequence number for replay protection.

**Usage Example:**

acc := AccountResponse{
    Address: "cosmos1abcd...",
    AccountNumber: 12345,
    Sequence: 10,
}

BalanceResponse

Represents the balance details of an account or address.

Field

Type

Description

Amount

string

Total amount in string format (e.g., "1000uatom").

Assets

[]Value

Slice of `Value` representing detailed asset balances (likely defined elsewhere).

*Note:* The `json` tags indicate serialization keys when marshaling/unmarshaling to JSON.


BlockResponse

Represents basic block metadata.

Field

Type

Description

Height

int

Block height (block number).

Hash

string

Block hash string.

Timestamp

int

Unix timestamp of block creation.


ErrorResponse

Standardized error response structure.

Field

Type

Description

Code

int

Numeric error code.

Msg

string

Human-readable error message.

Detail

[]interface{}

Additional error details (can vary).


Pagination

Represents pagination information for paginated queries.

Field

Type

Description

NextKey

*[]byte

Pointer to next page key (optional).

Total

uint64

Total number of items as string in JSON.


ABCIEventAttribute

Represents a key-value attribute in an ABCI event.

Field

Type

Description

Key

string

Attribute key.

Value

string

Attribute value.


ABCIEvent

Represents an ABCI event emitted during block or transaction processing.

Field

Type

Description

Type

string

Event type identifier.

Attributes

[]ABCIEventAttribute

List of key-value attributes for the event.


ResultBlockResults

Wraps CometBFT's `ResultBlockResults` to implement the `BlockResults` interface.

type ResultBlockResults struct {
    *coretypes.ResultBlockResults
}

**Example:**

var results ResultBlockResults
events := results.GetBlockEvents()

ResultTx

Wraps CometBFT's `ResultTx` to implement the `HistoryTx` interface and provides transaction formatting.

type ResultTx struct {
    *coretypes.ResultTx
    formatTx func(tx *coretypes.ResultTx) (*Tx, error)
}

**Usage:**

tx := ResultTx{ResultTx: someResultTx, formatTx: formatFunc}
height := tx.GetHeight()
txID := tx.GetTxID()
formattedTx, err := tx.FormatTx()

TxHistoryResponse

Represents paginated transaction history response.

Field

Type

Description

Cursor

string

Cursor string for fetching next page.

Txs

[]Tx

List of transactions in the page.


ValidatorsResponse

Represents the response for validators query.

Field

Type

Description

Validators

[]Validator

List of validator info structs.

Pagination

PageResponse

Pagination metadata.

*Note:* `Validator` and `PageResponse` types are assumed to be defined elsewhere.


signingTx (implements SigningTx)

Represents a simple transaction signing structure with memo and fee.

Field

Type

Description

memo

string

Memo string attached to the transaction.

fee

sdk.Coins

Fee amount in Cosmos SDK coin format.


Interfaces

BlockResults

Defines behavior for retrieving block events.

type BlockResults interface {
    GetBlockEvents() []ABCIEvent
}

HistoryTx

Defines an interface for historical transaction data access.

type HistoryTx interface {
    GetHeight() int64
    GetIndex() int
    GetTxID() string
    FormatTx() (*Tx, error)
}

SigningTx

Defines accessors for transaction signing metadata.

type SigningTx interface {
    GetMemo() string
    GetFee() sdk.Coins
}

Important Implementation Details


Interactions with Other Parts of the System


Visual Diagram

classDiagram
    class AccountResponse {
        +string Address
        +int AccountNumber
        +int Sequence
    }
    class BalanceResponse {
        +string Amount
        +[]Value Assets
    }
    class BlockResponse {
        +int Height
        +string Hash
        +int Timestamp
    }
    class ErrorResponse {
        +int Code
        +string Msg
        +[]interface{} Detail
    }
    class Pagination {
        +*[]byte NextKey
        +uint64 Total
    }
    class ABCIEventAttribute {
        +string Key
        +string Value
    }
    class ABCIEvent {
        +string Type
        +[]ABCIEventAttribute Attributes
    }
    class ResultBlockResults {
        +GetBlockEvents() []ABCIEvent
    }
    class ResultTx {
        +GetHeight() int64
        +GetIndex() int
        +GetTxID() string
        +FormatTx() (*Tx, error)
    }
    class TxHistoryResponse {
        +string Cursor
        +[]Tx Txs
    }
    class ValidatorsResponse {
        +[]Validator Validators
        +PageResponse Pagination
    }
    class signingTx {
        -string memo
        -sdk.Coins fee
        +GetMemo() string
        +GetFee() sdk.Coins
    }
    
    ResultBlockResults --|> coretypes.ResultBlockResults
    ResultTx --|> coretypes.ResultTx
    signingTx ..|> SigningTx
    ResultBlockResults ..|> BlockResults
    ResultTx ..|> HistoryTx

Summary

The [types.go](/projects/291/69257) file is a core utility file that defines foundational data types, interfaces, and wrapper implementations necessary for interacting with the Cosmos blockchain via CometBFT RPC and Cosmos SDK. It abstracts raw blockchain data into usable Go structures, supports transaction history and event processing, and defines consistent interfaces for blockchain-related operations. This facilitates clean integration with other system components such as API layers, transaction processors, and user interfaces.