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


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:

  1. Extracting the standard fee using the generic Cosmos fee parser.

  2. Adding the Thorchain native fee (which is automatically deducted but not tracked in the fee field) to the parsed fee amount.

  3. Returning the combined fee as a cosmos.Value representing the total fee paid in the specified denomination.

Parameters

Returns

Behavior and Implementation Details

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


Interaction with Other Components


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.