staking.go

Overview

The `staking.go` file is part of the `cosmos` package and provides client-side functionality for interacting with the Cosmos blockchain's staking module via HTTP REST API calls. Specifically, it enables querying validator information, including fetching lists of validators with pagination support and retrieving details of a specific validator by address.

The core purpose of this file is to encapsulate the logic to:

This file is designed to be used within a larger Cosmos SDK client context (`HTTPClient`), likely working alongside other modules querying different parts of the blockchain state.


Detailed Breakdown

Types and Structures

**Note:** The file references several types (`HTTPClient`, `ValidatorsResponse`, `ValidatorResponse`, etc.) assumed defined elsewhere in the package or project. This documentation focuses on the functions and logic within this file.


Function: GetValidators

func (c *HTTPClient) GetValidators(apr *big.Float, cursor string, pageSize int) (*ValidatorsResponse, error)

Function: GetValidator

func (c *HTTPClient) GetValidator(addr string, apr *big.Float) (*Validator, error)

Helper Function: httpValidator

func httpValidator(validator ValidatorResponse, apr *big.Float) *Validator

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

Below is a Mermaid class diagram representing the main structures and methods in `staking.go`:

classDiagram
    class HTTPClient {
        +GetValidators(apr *big.Float, cursor string, pageSize int) *ValidatorsResponse
        +GetValidator(addr string, apr *big.Float) *Validator
    }

    class Validator {
        +Address: string
        +Moniker: string
        +Jailed: bool
        +Status: int
        +Tokens: string
        +Shares: string
        +Website: string
        +Description: string
        +APR: string
        +Unbonding: ValidatorUnbonding
        +Commission: ValidatorCommission
    }

    class ValidatorUnbonding {
        +Height: int64
        +Timestamp: int64
    }

    class ValidatorCommission {
        +Rate: string
        +MaxRate: string
        +MaxChangeRate: string
    }

    class ValidatorsResponse {
        +Validators: []Validator
        +Pagination: Pagination
    }

    HTTPClient --> ValidatorsResponse : returns
    HTTPClient --> Validator : returns
    HTTPClient ..> Validator : uses httpValidator()
    Validator o-- ValidatorUnbonding
    Validator o-- ValidatorCommission

Summary

The `staking.go` file provides essential staking-related query functionality by wrapping Cosmos SDK REST API endpoints. It supports retrieving validator lists with pagination and individual validator details while adjusting APR values to reflect commission fees. It implements precise financial calculations and error handling, making it a critical component of Cosmos blockchain clients needing staking data.

This file interacts closely with HTTP client infrastructure and domain models in the broader `cosmos` package, serving as a clean, reusable interface for staking queries.