thorchain.go
Overview
The `thorchain.go` file provides Thorchain-specific fee parsing functionality within the ShapeShift Unchained platform. It extends the generic Cosmos SDK fee parsing by incorporating Thorchain’s native fee model, which automatically deducts a native fee from every transaction but does not explicitly represent it in the transaction fee fields.
The core purpose of this file is to accurately calculate and return the total fee paid in a Thorchain transaction by combining the standard transaction fee with the native fee amount. This ensures that downstream components and clients receive an accurate representation of transaction costs on Thorchain.
Contents
Function:
ParseFee— Parses and calculates the total fee for a Thorchain transaction by adding the native fee to the standard fee extracted from the transaction.
Detailed Explanation
Function: ParseFee
func ParseFee(tx cosmos.SigningTx, txid string, denom string, nativeFee int) cosmos.Value
Purpose
Parses the transaction fee from a Cosmos SDK transaction (`tx`) for Thorchain by:
Extracting the standard fee using the generic Cosmos fee parser.
Adding the Thorchain native fee (which is automatically deducted but not tracked in the fee field) to the parsed fee amount.
Returning the combined fee as a
cosmos.Valuerepresenting the total fee paid in the specified denomination.
Parameters
tx cosmos.SigningTx:
The transaction object that conforms to the Cosmos SDK signing transaction interface. It contains the raw transaction data including fee information.txid string:
The transaction ID (hash) as a string, used for logging or tracing purposes in the underlying Cosmos fee parser.denom string:
The denomination (currency unit) of the fee token, e.g.,runefor Thorchain native token.nativeFee int:
The native fee amount automatically deducted by Thorchain for every transaction. This is not present explicitly in the transaction fee but must be added to reflect the true cost.
Returns
cosmos.Value:
A struct representing the total fee paid, including the original fee plus the native fee, with fields for the denomination and amount as a string.
Behavior and Implementation Details
The function calls the base Cosmos fee parser
cosmos.Fee(tx, txid, denom)to get the initial fee value.It converts the fee amount string to a big integer (
big.Int) to safely perform arithmetic operations on potentially large numbers.It adds the native fee (converted to
big.Int) to the parsed fee amount.The sum is converted back to a string and assigned to the
Amountfield of the returnedcosmos.Value.This approach ensures precision and correctness when dealing with large fee amounts typical in blockchain systems.
Usage Example
import (
"fmt"
"github.com/shapeshift/unchained/pkg/cosmos"
"github.com/shapeshift/unchained/pkg/thorchain"
)
// Assume tx is a cosmos.SigningTx obtained from a Thorchain transaction
var tx cosmos.SigningTx
txid := "ABC123..."
denom := "rune"
nativeFee := 100000 // 0.001 rune represented as integer units
fee := thorchain.ParseFee(tx, txid, denom, nativeFee)
fmt.Printf("Total fee paid: %s %s\n", fee.Amount, fee.Denom)
Important Implementation Details
Big Integer Arithmetic:
The fee amount is handled usingmath/big.Intto avoid integer overflow and preserve exact fee values during addition.Native Fee Addition:
Thorchain deducts a small, fixed native fee from every transaction at the protocol level. This fee is not visible in the standard transaction fee fields, so it must be manually added to report the correct total fee.Integration with Cosmos SDK:
The function builds upon the existing Cosmos SDK fee parsing logic (cosmos.Fee) by augmenting it with Thorchain-specific adjustments.
Interaction with Other Components
cosmos.Feefunction:
TheParseFeefunction relies on the generic Cosmos SDK fee parser to extract the base fee before adjustment.Thorchain API and Handler modules:
This fee parser is used when preparing transaction data to be returned by Thorchain API endpoints, ensuring that fee information is accurate and complete.Affiliate Fee Indexer and Revenue Tracking:
Accurate fee parsing is essential for components that calculate affiliate revenues or other fee-based metrics, as the native fee affects the total fees collected.Transaction Parsing Components:
Other Thorchain-specific transaction parsing modules may callParseFeeto obtain truthful fee information when constructing transaction summaries or synthetic transactions.
Visual Diagram: Function Flow in thorchain.go
flowchart TD
A[Start: Call ParseFee with tx, txid, denom, nativeFee]
B[Call cosmos.Fee(tx, txid, denom) to get base fee]
C[Convert fee.Amount (string) to big.Int]
D[Convert nativeFee (int) to big.Int]
E[Add nativeFee to base fee]
F[Convert sum back to string]
G[Assign sum string to fee.Amount]
H[Return fee (cosmos.Value) with updated amount]
A --> B --> C --> D --> E --> F --> G --> H
Summary
The `thorchain.go` file implements a single, critical function `ParseFee` that accurately computes the total fee for Thorchain transactions by combining the explicit transaction fee with the implicit native fee. This function is a key building block for Thorchain-specific transaction processing and ensures that all components relying on fee data have a consistent and correct view of transaction costs.
By leveraging big integer arithmetic and extending the Cosmos SDK fee parsing, this file encapsulates Thorchain’s unique fee model in a clean, reusable function, enabling accurate fee representation throughout the Thorchain integration within the ShapeShift Unchained platform.