fees.go
Overview
The [fees.go](/projects/291/69231) file is part of the `cosmos` package and provides functionality for retrieving minimum gas price information from a Cosmos SDK-based blockchain network. It interacts with the blockchain's REST API (LCD - Light Client Daemon) to fetch two types of gas prices:
Global Minimum Gas Prices: Obtained from the feemarket module, representing the globally suggested minimum gas prices for transactions.
Local Minimum Gas Prices: Obtained from the node's local configuration, representing the node-specific configured minimum gas prices.
This information is crucial for clients constructing transactions to ensure their fees meet the minimum network requirements to be accepted and processed.
Detailed Description of Functions
1. GetGlobalMinimumGasPrices
func (c *HTTPClient) GetGlobalMinimumGasPrices() (map[string]sdkmath.LegacyDec, error)
Purpose
Fetches the global minimum gas prices from the feemarket module of the blockchain by querying the LCD endpoint `/feemarket/v1/gas_price/{denom}`.
Parameters
No explicit parameters; it uses the
denomproperty of theHTTPClientreceiver to specify the token denomination.
Returns
map[string]sdkmath.LegacyDec: A map where keys are coin denominations (e.g.,"uatom") and values are their corresponding gas prices represented asLegacyDecdecimal values.error: Non-nil if an error occurs during the HTTP request, response parsing, or decimal conversion.
Description
Constructs the URL endpoint using the denomination (
c.denom).Sends a GET request to the fee market gas price endpoint.
Parses the JSON response into an internal struct.
Converts the amount string to a
LegacyDecdecimal type.Returns a map associating denominations with their gas prices.
Usage Example
client := &cosmos.HTTPClient{denom: "uatom", /* ... other fields ... */}
gasPrices, err := client.GetGlobalMinimumGasPrices()
if err != nil {
log.Fatalf("Error fetching global gas prices: %v", err)
}
fmt.Println("Global Minimum Gas Prices:", gasPrices)
2. GetLocalMinimumGasPrices
func (c *HTTPClient) GetLocalMinimumGasPrices() (map[string]sdkmath.LegacyDec, error)
Purpose
Retrieves the local node's configured minimum gas prices from the node's base configuration endpoint `/cosmos/base/node/v1beta1/config`.
Parameters
None explicitly; uses the
HTTPClientreceiver for making HTTP requests.
Returns
map[string]sdkmath.LegacyDec: Map of coin denominations to their respective minimum gas prices.error: Non-nil if there is a failure in HTTP request, parsing the response, or decoding coin strings.
Description
Sends a GET request to the node configuration endpoint.
Extracts the
minimum_gas_pricestring from the response.Parses the string (which can represent multiple coins, e.g.,
"0.025uatom,0.001stake") into a slice ofDecCoin.Converts the parsed coins into a map for easy access by denomination.
Usage Example
client := &cosmos.HTTPClient{ /* ... */ }
localGasPrices, err := client.GetLocalMinimumGasPrices()
if err != nil {
log.Fatalf("Error fetching local gas prices: %v", err)
}
fmt.Println("Local Minimum Gas Prices:", localGasPrices)
Important Implementation Details
Error Handling: Both functions use structured error handling to wrap and return errors encountered during HTTP requests, JSON unmarshalling, or parsing operations. They also handle API-level errors by checking the error response (
ErrorResponse) returned by the LCD.Use of
sdkmath.LegacyDec: Gas prices are represented usingLegacyDecfrom the Cosmos SDK's math package, a fixed-point decimal type designed for precision in financial calculations.HTTP Client (
HTTPClient): The methods are bound toHTTPClient, which internally usesc.LCD(likely a REST client) to interact with the blockchain's LCD endpoints.Parsing Coins: The local gas price string is parsed using
sdk.ParseDecCoins, which supports multiple coin denominations in a single string.
Interaction with Other System Components
HTTPClient and LCD: The
HTTPClientstruct acts as a wrapper around a REST client (c.LCD) that communicates with the blockchain's LCD endpoints. This file depends on this client to fetch real-time blockchain configuration data.Cosmos SDK Types: The file leverages Cosmos SDK types such as
sdkmath.LegacyDecandsdk.DecCoinfor precise and standardized representation of coin amounts and denominations.ErrorResponse: Uses a custom
ErrorResponsestruct (definition not included here) to capture and handle error messages returned by the LCD.Modules Queried:
feemarket: Provides global network fee parameters.base node config: Provides node-specific configuration, including local fee settings.
This file is likely used by higher-level modules responsible for transaction creation, fee estimation, and validation to ensure transactions include appropriate fees.
Mermaid Diagram: Flowchart of Main Functions and Their Relationships
flowchart TD
A[HTTPClient] --> B[GetGlobalMinimumGasPrices]
A --> C[GetLocalMinimumGasPrices]
B --> D{HTTP GET /feemarket/v1/gas_price/{denom}}
D --> E[Parse JSON response]
E --> F[Convert amount to LegacyDec]
F --> G[Return map[denom]LegacyDec]
C --> H{HTTP GET /cosmos/base/node/v1beta1/config}
H --> I[Parse JSON response]
I --> J[Parse minimum_gas_price string to DecCoins]
J --> K[Build map[denom]LegacyDec]
K --> L[Return gas prices map]
subgraph ErrorHandling
D -- On failure --> M[Return error]
H -- On failure --> M
E -- On failure --> M
I -- On failure --> M
F -- On failure --> M
J -- On failure --> M
end
Summary
The [fees.go](/projects/291/69231) file provides essential utilities for querying minimum gas price information from a Cosmos SDK blockchain's REST API. It exposes two primary methods for retrieving global and local gas price configurations, handling all necessary API interactions, JSON unmarshalling, and error handling internally. This functionality supports fee management in transaction processing workflows, ensuring clients can dynamically adjust to network fee requirements.
If you require further details about the `HTTPClient` struct or `ErrorResponse` type, please refer to their respective source files in the `cosmos` package.