Bug: Streaming swap events emit incorrect streaming_swap_quantity and streaming_swap_count attributes

Problem Summary

Streaming swap transactions emit incorrect streaming_swap_quantity and streaming_swap_count values in Tendermint event attributes. Approximately 70% of streaming swaps (2,478,753 transactions since August 2023) show both attributes set to "1" for all sub-swaps instead of the correct values.

Expected Behavior:

  • streaming_swap_quantity: Should be constant total (e.g., 747 for all 747 sub-swaps)
  • streaming_swap_count: Should increment per sub-swap (1, 2, 3...)

Actual Behavior:

  • Both attributes are set to "1" for all sub-swaps in affected transactions

Bug Location

File: x/thorchain/swap_current.go
Lines: 115-122
Function: SwapperVCUR.Swap()

Introduced By: Son of Odin
Commit: 6a2cb0886cf33e5530425b104e413b35e5954ddd (July 29, 2023)
Commit Message: [bug] include streaming swap quantity and count into a swap event

The Bug

// Lines 115-122 in swap_current.go
for _, evt := range swapEvents {
    if swp.Quantity > evt.StreamingSwapQuantity {
        evt.StreamingSwapQuantity = swp.Quantity
        evt.StreamingSwapCount = swp.Count + 1 // first swap count is "zero"
    } else {
        evt.StreamingSwapQuantity = 1  // ❌ BUG: Sets to 1 for non-streaming swaps
        evt.StreamingSwapCount = 1     // ❌ BUG: Sets to 1 for non-streaming swaps
    }
    if err := mgr.EventMgr().EmitEvent(ctx, evt); err != nil {
        ctx.Logger().Error("fail to emit swap event", "error", err)
    }
    // ...
}

Root Cause

The else branch incorrectly sets both values to 1 when:

  1. swp.Quantity is 0 (not a streaming swap) - condition 0 > 0 is false, falls into else
  2. swp.Quantity is valid but swp.Quantity <= evt.StreamingSwapQuantity (shouldn't happen for valid streaming swaps)

For non-streaming swaps, swp.Quantity = 0, so the condition fails and both attributes are incorrectly set to 1 instead of remaining at 0 (initial state).

Proposed Fix

Only set these values if it's actually a streaming swap:

// CORRECT FIX:
for _, evt := range swapEvents {
    if swp.Quantity > 0 {  // Only set if it's actually a streaming swap
        evt.StreamingSwapQuantity = swp.Quantity
        evt.StreamingSwapCount = swp.Count + 1 // first swap count is "zero"
    }
    // If swp.Quantity == 0, leave evt.StreamingSwapQuantity and evt.StreamingSwapCount at 0
    if err := mgr.EventMgr().EmitEvent(ctx, evt); err != nil {
        ctx.Logger().Error("fail to emit swap event", "error", err)
    }
    // ...
}

Alternative using the existing Valid() method:

// ALTERNATIVE FIX using Valid() method:
for _, evt := range swapEvents {
    if swp.Valid() == nil {  // Valid streaming swap
        evt.StreamingSwapQuantity = swp.Quantity
        evt.StreamingSwapCount = swp.Count + 1
    }
    // If invalid/empty, leave at 0 (initial state)
    if err := mgr.EventMgr().EmitEvent(ctx, evt); err != nil {
        ctx.Logger().Error("fail to emit swap event", "error", err)
    }
    // ...
}

Evidence

Broken Transaction

  • TX: A961DD61516074E7E44D1FD722234FE93B5E219A4E9FD58A245B9DF046ECEB66
  • Date: 2025-11-26
  • Total Events: 747 sub-swaps
  • Emits: streaming_swap_count: "1", streaming_swap_quantity: "1" for ALL 747 events
  • Expected: streaming_swap_count should increment 1..747, streaming_swap_quantity should be "747" for all

Working Transaction (for comparison)

  • TX: 3C820AA2E3C46B8CAF3C0B8ACDAA0695CC6CB0C89DF5955E6AA7EB273ECACE28
  • Date: 2025-10-18
  • Total Events: 371 streaming swaps
  • Emits: streaming_swap_count: "1", "2", "3"... (incrementing), streaming_swap_quantity: "371" (constant) ✅

Impact

  • Affected Transactions: 2,478,753 broken streaming swaps
  • Working Transactions: 1,055,577 correct streaming swaps
  • Affected Percentage: ~70% of streaming swaps
  • Since: August 2023 (introduced July 29, 2023)
  • Data Impact: All broken transactions show streaming_quantity = 1 and streaming_count = 1 instead of correct values, preventing accurate volume reconciliation

Testing

After fix, verify with:

  1. Broken TX: A961DD61516074E7E44D1FD722234FE93B5E219A4E9FD58A245B9DF046ECEB66

    • Should emit: streaming_swap_count incrementing 1..747, streaming_swap_quantity: "747" constant
  2. Working TX: 3C820AA2E3C46B8CAF3C0B8ACDAA0695CC6CB0C89DF5955E6AA7EB273ECACE28

    • Should continue to work correctly
  3. Verify via Tendermint RPC: Query block_results and check finalize_block_events for correct attribute values

Additional Context

  • Midgard (indexer) is a passive parser that inserts these values directly from Tendermint event attributes
  • The issue originates in Thorchain's event emission logic, not in downstream systems
  • This affects data consumers who rely on these attributes for volume reconciliation and analytics