Affiliate Fee Indexer

Purpose

The Affiliate Fee Indexer addresses the need to accurately track and aggregate outbound affiliate fee transactions within the Thorchain blockchain ecosystem. Thorchain distributes affiliate fees to specific affiliate addresses whenever transactions pass through the network. This subtopic is dedicated to indexing these outbound affiliate fee transactions by scanning blockchain events and maintaining a comprehensive ledger of all affiliate revenue. This enables transparent reporting and auditing of affiliate fees, which is critical for stakeholders relying on affiliate revenue data.

Functionality

At its core, the Affiliate Fee Indexer continuously monitors new blocks and searches historic blocks for outbound transactions sent to a predefined list of affiliate addresses. Its main workflows include:

This design ensures both historic and live affiliate fee transactions are indexed accurately and efficiently.

Key Data Flow

  1. Block Search: Query blockchain nodes for transactions outbound to affiliate addresses.

  2. Block Results Fetch: Retrieve detailed block results including events.

  3. Event Parsing: Identify outbound affiliate fee events.

  4. Record Creation: Construct AffiliateFee records with transaction and block metadata.

  5. Record Storage: Append records safely to the indexer's fee list.

Critical Snippet Illustrating Event Handling

func (i *AffiliateFeeIndexer) processAffiliateFees(block thorchain.Block, blockEvents []cosmos.ABCIEvent, affiliateAddresses []string) {
  // Parse events from block
  _, typedEvents, err := thorchain.ParseBlockEvents(blockEvents)
  if err != nil {
    logger.Panicf("failed to parse block events for block: %d: %+v", block.Height(), err)
  }

  // Iterate over events to find outbound affiliate fees
  for _, event := range typedEvents {
    switch v := event.(type) {
    case *thorchain.EventOutbound:
      // Check if recipient is an affiliate address
      for _, affiliateAddress := range affiliateAddresses {
        if v.To == affiliateAddress {
          // Create and store affiliate fee record
          affiliateFee := &AffiliateFee{
            TxID:        v.InTxID,
            Address:     v.To,
            Amount:      strings.Fields(v.Coin)[0],
            Asset:       strings.Fields(v.Coin)[1],
            BlockHeight: block.Height(),
            BlockHash:   block.Hash(),
            Timestamp:   block.Timestamp(),
          }
          i.mu.Lock()
          i.AffiliateFees = append(i.AffiliateFees, affiliateFee)
          i.mu.Unlock()
          break
        }
      }
    }
  }
}

Relationship to Parent Topic and Other Subtopics

The Affiliate Fee Indexer is a specialized component within the broader Thorchain-specific functionality, complementing other subtopics such as:

Together, these components form a cohesive system for processing, indexing, and exposing detailed Thorchain blockchain data with a focus on affiliate revenue transparency.

Unlike the generic transaction parsing subtopic, this indexer introduces concurrency and pagination in historic block searching and maintains an internal store of affiliate fees, providing optimized, real-time, and historical affiliate fee tracking not available in other subtopics.


Diagram: Affiliate Fee Indexer 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

This flowchart illustrates both the live block processing triggered by new blocks and the historic synchronization process that paginates through past blocks to index all affiliate fee transactions.


The Affiliate Fee Indexer ensures Thorchain’s affiliate revenue data is kept current and comprehensive, enabling transparent tracking and supporting analytics and reporting services built on top of this indexed data.