account.go

Overview

This file, [account.go](/projects/291/69316), is part of the `cosmos` package and provides a set of functions implemented as methods on the `HTTPClient` type to interact with the Cosmos blockchain via its REST LCD (Light Client Daemon) API. The primary purpose of this file is to query various account-related data such as account details, balances, staking delegations, redelegations, unbondings, and rewards.

Each function sends HTTP GET requests to specific Cosmos SDK REST endpoints, deserializes the JSON responses into Go structs, and processes the data to return a structured and usable response. The file also includes a helper function for processing balances.

The functionality in this file serves as a client-side interface layer, abstracting the REST API details and enabling other parts of the system to easily fetch and work with Cosmos account data.


Types and Structures

The file references several domain-specific types (assumed defined elsewhere in the package or project):


Functions and Methods

(c *HTTPClient) GetAccount(address string) (*AccountResponse, error)

Fetches the Cosmos account information for the given account address.


(c *HTTPClient) GetBalance(address string, baseDenom string) (*BalanceResponse, error)

Retrieves the balances for the given account address.


(c *HTTPClient) GetDelegations(address string, apr *big.Float) ([]Delegation, error)

Fetches all staking delegations for the account.


(c *HTTPClient) GetRedelegations(address string, apr *big.Float) ([]Redelegation, error)

Retrieves redelegations for the account.


(c *HTTPClient) GetUnbondings(address string, baseDenom string, apr *big.Float) ([]Unbonding, error)

Fetches unbonding delegations for the account.


(c *HTTPClient) GetRewards(address string, apr *big.Float) ([]Reward, error)

Retrieves staking rewards for the delegator.


balance(balances sdk.Coins, baseDenom string) (*BalanceResponse, error)

Helper function to process balances, separating base denomination from other assets.


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class HTTPClient {
        +GetAccount(address string) *AccountResponse, error
        +GetBalance(address string, baseDenom string) *BalanceResponse, error
        +GetDelegations(address string, apr *big.Float) []Delegation, error
        +GetRedelegations(address string, apr *big.Float) []Redelegation, error
        +GetUnbondings(address string, baseDenom string, apr *big.Float) []Unbonding, error
        +GetRewards(address string, apr *big.Float) []Reward, error
    }

    class AccountResponse {
        +Address string
        +AccountNumber int
        +Sequence int
    }

    class BalanceResponse {
        +Amount string
        +Assets []Value
    }

    class Delegation {
        +Validator Validator
        +Shares string
        +Balance Value
    }

    class Redelegation {
        +SourceValidator Validator
        +DestinationValidator Validator
        +Entries []RedelegationEntry
    }

    class RedelegationEntry {
        +CompletionTime string
        +Shares string
        +Balance string
    }

    class Unbonding {
        +Validator Validator
        +Entries []UnbondingEntry
    }

    class UnbondingEntry {
        +CompletionTime string
        +Balance Value
    }

    class Reward {
        +Validator Validator
        +Rewards []Value
    }

    class Value {
        +Amount string
        +Denom string
    }

    class Validator {
        +Address string
        // Other validator properties...
    }

    HTTPClient --> AccountResponse
    HTTPClient --> BalanceResponse
    HTTPClient --> Delegation
    HTTPClient --> Redelegation
    HTTPClient --> Unbonding
    HTTPClient --> Reward
    Delegation --> Validator
    Delegation --> Value
    Redelegation --> Validator : SourceValidator
    Redelegation --> Validator : DestinationValidator
    Redelegation --> RedelegationEntry
    RedelegationEntry --> Value : Balance (string here)
    Unbonding --> Validator
    Unbonding --> UnbondingEntry
    UnbondingEntry --> Value
    Reward --> Validator
    Reward --> Value

Summary

The [account.go](/projects/291/69316) file provides a comprehensive client interface for retrieving Cosmos blockchain account and staking-related data through REST API calls. It handles JSON deserialization, error management, and data structuring to present clean, usable Go types. It is a crucial part of the Cosmos blockchain integration in this system, abstracting low-level API details and enabling other modules to work with account, balance, delegation, redelegation, unbonding, and rewards information efficiently.