affiliateFees.go


Overview

The [affiliateFees.go](/projects/291/69279) file implements an **Affiliate Fee Indexer** for the Thorchain blockchain ecosystem. Its primary purpose is to **index and aggregate outbound affiliate fee transactions** by scanning blockchain events both historically and in real-time. Affiliate fees in Thorchain are paid to specific affiliate addresses when transactions occur, and this module tracks all such payments to enable transparent reporting and auditing.

Key features include:

This indexer is a specialized component within the Thorchain-specific API layer, providing authoritative affiliate fee data that can be consumed by other services or APIs.


Detailed Documentation

Constants

Name

Description

`blockWorkers`

Number of concurrent workers fetching blocks during historic sync (10).

pageSize

Number of results per page when querying blocks (50).

`resultWorkers`

Number of workers processing fetched block results concurrently (100).


Types

AffiliateFeeIndexer

type AffiliateFeeIndexer struct {
    AffiliateAddresses []string          // List of affiliate addresses to track
    AffiliateFees      []*AffiliateFee   // Collected affiliate fee records
    httpClients        []*cosmos.HTTPClient // HTTP clients for querying blockchain nodes
    mu                 sync.Mutex        // Mutex for thread-safe access to AffiliateFees
}

AffiliateFee

type AffiliateFee struct {
    Amount      string  // Amount of asset paid as affiliate fee (string to preserve precision)
    Asset       string  // Asset symbol or denomination (e.g., "THOR.RUNE")
    BlockHeight int64   // Height of the block containing the transaction
    BlockHash   string  // Hash of the block
    Timestamp   int64   // Unix timestamp of the block time
    Address     string  // Affiliate address receiving the fee
    TxID        string  // Transaction ID from which the affiliate fee originated
}

Functions and Methods


NewAffiliateFeeIndexer

func NewAffiliateFeeIndexer(httpClients []*cosmos.HTTPClient, wsClient *cosmos.WSClient) *AffiliateFeeIndexer
httpClients := []*cosmos.HTTPClient{client1, client2}
wsClient := cosmos.NewWSClient(...)
indexer := NewAffiliateFeeIndexer(httpClients, wsClient)

Sync

func (i *AffiliateFeeIndexer) Sync() error
if err := indexer.Sync(); err != nil {
    log.Fatalf("Sync failed: %v", err)
}

fetchBlocks

func (i *AffiliateFeeIndexer) fetchBlocks(
    httpClient *cosmos.HTTPClient,
    affiliateAddress string,
    pageCh <-chan int,
    resultCh chan<- *coretypes.ResultBlockSearch)

handleBlocks

func (i *AffiliateFeeIndexer) handleBlocks(
    httpClient *cosmos.HTTPClient,
    affiliateAddress string,
    resultCh <-chan *coretypes.ResultBlockSearch) *sync.WaitGroup

processAffiliateFees

func (i *AffiliateFeeIndexer) processAffiliateFees(
    block thorchain.Block,
    blockEvents []cosmos.ABCIEvent,
    affiliateAddresses []string)
// Called on new block events or during historic sync
indexer.processAffiliateFees(block, events, indexer.AffiliateAddresses)

Implementation Details and Algorithms


Interaction with Other System Components


Usage Example

httpClients := []*cosmos.HTTPClient{client1, client2}
wsClient := cosmos.NewWSClient("ws://thorchain-node:26657/websocket")

indexer := NewAffiliateFeeIndexer(httpClients, wsClient)

// Sync historic affiliate fee data
if err := indexer.Sync(); err != nil {
    log.Fatalf("Failed to sync affiliate fees: %v", err)
}

// indexer listens and processes new blocks automatically
// Access affiliate fees:
for _, fee := range indexer.AffiliateFees {
    fmt.Printf("Affiliate %s received %s %s at block %d\n",
        fee.Address, fee.Amount, fee.Asset, fee.BlockHeight)
}

Visual Diagram: AffiliateFeeIndexer Workflow

flowchart TD
    Start[Start Indexer / On New Block] --> CheckNewBlock{New Block Received?}
    CheckNewBlock -- Yes --> FetchBlockEvents[Fetch Block Events]
    FetchBlockEvents --> ParseEvents[Parse Block Events]
    ParseEvents --> IdentifyAffiliateFees{Outbound To Affiliate?}
    IdentifyAffiliateFees -- Yes --> CreateFeeRecord[Create Affiliate Fee Record]
    CreateFeeRecord --> StoreRecord[Store Record Thread-Safely]
    IdentifyAffiliateFees -- No --> EndProcess[End Processing]
    StoreRecord --> EndProcess

    StartHistoricSync[Start Historic Sync] --> ForEachAffiliate[For Each Affiliate Address]
    ForEachAffiliate --> PaginatedBlockSearch[Paginate Blocks with Outbound Tx to Affiliate]
    PaginatedBlockSearch --> FetchBlockResults[Fetch Block Results]
    FetchBlockResults --> ParseEventsHistoric[Parse Events in Blocks]
    ParseEventsHistoric --> IdentifyAffiliateFeesHistoric{Outbound To Affiliate?}
    IdentifyAffiliateFeesHistoric -- Yes --> CreateFeeRecordHistoric[Create Fee Record]
    CreateFeeRecordHistoric --> StoreRecord
    IdentifyAffiliateFeesHistoric -- No --> Continue
    Continue --> EndHistoricSync[End Historic Sync]

    EndProcess -.-> WaitForNextBlock[Wait for Next Block]
    WaitForNextBlock --> CheckNewBlock

Summary

The [affiliateFees.go](/projects/291/69279) file provides a robust, concurrent indexing mechanism for tracking and recording outbound affiliate fee transactions on Thorchain. It balances real-time event listening with historic backfill capabilities, ensuring comprehensive and accurate affiliate fee data aggregation. This module is critical for supporting transparency and accountability in affiliate revenue reporting within the Thorchain ecosystem.