Skip to content

feat: view fee distribution audit trail for LP rewards#1281

Merged
MicBun merged 1 commit into
mainfrom
LpAudit
Dec 10, 2025
Merged

feat: view fee distribution audit trail for LP rewards#1281
MicBun merged 1 commit into
mainfrom
LpAudit

Conversation

@MicBun
Copy link
Copy Markdown
Contributor

@MicBun MicBun commented Dec 10, 2025

resolves: https://github.com/truflation/website/issues/2923

Summary by CodeRabbit

Release Notes

  • New Features

    • Added immutable audit trail for fee distributions, capturing detailed records of all fee distributions and per-participant rewards
    • Added new APIs to query distribution summaries, detailed reward records, and participant reward history
  • Chores

    • Cleaned up internal documentation and comments

✏️ Tip: You can customize this high-level summary in your review settings.

@MicBun MicBun self-assigned this Dec 10, 2025
@MicBun MicBun added the type: feat New feature or request label Dec 10, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 10, 2025

Walkthrough

This PR introduces an immutable audit trail system for fee distributions in the order book. It adds new migration files that create audit tables (ob_fee_distributions and ob_fee_distribution_details), public query actions, performance indexes, and a comprehensive test suite. Several existing migrations receive documentation cleanups removing historical Issue references.

Changes

Cohort / File(s) Summary
Comment & Documentation Cleanup
internal/migrations/032-order-book-actions.sql, internal/migrations/034-order-book-rewards.sql
Removed references to specific Issue numbers and historical stub/implementation notes from headers and comments; simplified descriptive sections for brevity without altering logic or schema.
Fee Distribution Audit Trail
internal/migrations/033-order-book-settlement.sql, internal/migrations/036-order-book-audit.sql
Extended distribute_fees flow with immutable audit records (Step 5.5); created two new audit tables (ob_fee_distributions, ob_fee_distribution_details) with foreign keys, CHECK constraints, and four performance indexes; added three public query actions (get_distribution_summary, get_distribution_details, get_participant_reward_history) to retrieve audit data.
Fee Distribution Audit Tests
tests/streams/order_book/fee_distribution_audit_test.go
New comprehensive test suite covering audit record creation, multi-block scenarios, empty LP cases, zero-fee distributions, and data integrity validation; includes helper functions for LP setup and vault funding.

Sequence Diagram

sequenceDiagram
    participant Client
    participant DistributeFees as distribute_fees<br/>(Migration 033)
    participant Vault
    participant AuditTables as Audit Tables<br/>(Migration 036)
    participant QueryAPI as Public Query<br/>Actions

    Client->>DistributeFees: Trigger fee distribution<br/>(with LP reward samples)
    DistributeFees->>Vault: Execute vault transfers<br/>(Step 1-5)
    Vault-->>DistributeFees: Transfers complete
    
    alt Wallets to distribute to
        DistributeFees->>AuditTables: Step 5.5: Create distribution<br/>record in ob_fee_distributions
        AuditTables-->>DistributeFees: distribution_id generated
        DistributeFees->>AuditTables: Insert per-LP details<br/>in ob_fee_distribution_details
        AuditTables-->>DistributeFees: Detail records inserted
        DistributeFees->>DistributeFees: Delete ob_rewards
    else No wallets
        DistributeFees->>DistributeFees: Skip audit record creation
    end
    
    Client->>QueryAPI: Query distribution history
    QueryAPI->>AuditTables: Select from audit tables
    AuditTables-->>QueryAPI: Return audit records
    QueryAPI-->>Client: Distribution summary &<br/>per-LP reward details
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Areas requiring extra attention:

  • Verify foreign key constraints between ob_fee_distributions and ob_fee_distribution_details are correctly defined and reference migrations 033 and 036 occur in proper order
  • Confirm audit record creation in Migration 033 Step 5.5 correctly handles numeric precision (NUMERIC(78,0)) when encoding per-LP rewards
  • Review test file assertions for balance comparisons and ensure big.Int and Decimal type conversions are accurate
  • Validate that the four new performance indexes align with actual query patterns in the three public actions

Possibly related PRs

Suggested reviewers

  • pr-time-tracker
  • outerlook

Poem

🐰✨ A trail of fees, now carved in stone,
Each reward and block, forever known,
Audits leap and queries shine bright,
Liquidity providers sleep at night! 🌙

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title accurately describes the main change: adding a fee distribution audit trail for LP rewards, which is the primary objective across all modified migrations (032, 033, 034, 036) and new test file.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch LpAudit

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@holdex
Copy link
Copy Markdown

holdex Bot commented Dec 10, 2025

Time Submission Status

Member Status Time Action Last Update
MicBun ✅ Submitted 4h Update time Dec 10, 2025, 4:47 PM

@MicBun MicBun changed the title feat: add fee distribution audit trail for LP rewards feat: view fee distribution audit trail for LP rewards Dec 10, 2025
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
internal/migrations/036-order-book-audit.sql (1)

104-126: Consider adding a composite index for market timeline queries.

The current indexes cover single-column queries well. For queries like "show distribution history for market X over time", a composite index would be more efficient:

+/**
+ * Composite index for market timeline queries
+ * Query pattern: "Show distribution history for Market #123 ordered by time"
+ */
+CREATE INDEX IF NOT EXISTS idx_fee_dist_query_time
+    ON ob_fee_distributions(query_id, distributed_at DESC);
internal/migrations/033-order-book-settlement.sql (1)

187-277: Consider optimizing audit record creation for better performance.

The current implementation uses a nested loop pattern with LIMIT/OFFSET (line 230) to pair wallet addresses with reward amounts, resulting in O(n²) complexity. While correct and acceptable for typical LP counts (<100), this could be optimized.

Current pattern:

for $w_row in SELECT wallet FROM UNNEST($wallet_addresses) AS w(wallet) {
    -- Use LIMIT 1 OFFSET ($idx - 1) to get corresponding amount
    for $a_row in SELECT amount FROM UNNEST($amounts) LIMIT 1 OFFSET ($idx - 1) {
        $reward_amount := $a_row.amount;
    }
    $idx := $idx + 1;
}

Suggested refactor using UNNEST with multiple arrays:

for $lp_data in 
    SELECT wallet, amount, ROW_NUMBER() OVER () as row_num
    FROM UNNEST($wallet_addresses, $amounts) AS u(wallet, amount)
{
    -- Direct access to paired values, no LIMIT/OFFSET needed
    -- Fetch participant info once per LP
    ...
}

This eliminates the O(n²) array access pattern and simplifies the code. However, given typical LP counts are small, the current implementation is acceptable.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 93c5a12 and 18f7d19.

📒 Files selected for processing (5)
  • internal/migrations/032-order-book-actions.sql (5 hunks)
  • internal/migrations/033-order-book-settlement.sql (5 hunks)
  • internal/migrations/034-order-book-rewards.sql (1 hunks)
  • internal/migrations/036-order-book-audit.sql (1 hunks)
  • tests/streams/order_book/fee_distribution_audit_test.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/streams/order_book/fee_distribution_audit_test.go (1)
internal/migrations/migration.go (1)
  • GetSeedScriptStatements (60-109)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: acceptance-test
🔇 Additional comments (8)
internal/migrations/032-order-book-actions.sql (1)

314-314: LGTM! Documentation cleanup.

The removal of historical issue references from comments improves readability and removes internal tracking details from the codebase.

Also applies to: 1274-1294, 1868-1870, 2117-2118, 2143-2143

internal/migrations/036-order-book-audit.sql (2)

150-261: LGTM! Well-designed query actions with consistent wallet handling.

The three public actions provide comprehensive audit trail querying capabilities:

  • Market-based summaries
  • Distribution-level details
  • Participant reward history

The wallet address normalization (lines 243-245) and case-insensitive comparison (line 256) correctly handle variations in input format.


49-94: The CASCADE DELETE behavior is explicitly documented as intentional in the migration file.

The migration header states the purpose is to "Create immutable audit tables to track fee distribution history. Ensures compliance, user transparency, and data preservation." The comment in lines 51-53 explicitly documents: "CASCADE deleted when market (ob_queries) is deleted" as part of the intended lifecycle. This design ensures audit records remain permanently for each market's lifetime but are cleaned up when the market itself is deleted, preventing orphaned records. The CASCADE behavior is correct and aligns with the documented design.

internal/migrations/034-order-book-rewards.sql (1)

2-2: LGTM! Documentation cleanup.

Consistent with the cleanup in other migration files, removing internal issue tracking references.

internal/migrations/033-order-book-settlement.sql (1)

9-9: LGTM! Clear documentation of audit trail integration.

The updated documentation clearly explains:

  • The audit trail creation process
  • Edge cases (no samples → no audit record)
  • Dependencies on new migration 036
  • Data preservation guarantees

The comment on line 280 correctly notes that audit records now preserve distribution history before cleanup.

Also applies to: 58-102, 280-280

tests/streams/order_book/fee_distribution_audit_test.go (3)

1-43: LGTM! Well-structured test suite.

The test suite follows kwil-db testing conventions:

  • Proper build tag isolation (//go:build kwiltest)
  • Uses platform testing framework
  • Seeds with migration scripts for consistency
  • Organized into focused test scenarios

480-551: LGTM! Well-designed helper functions.

Both helper functions follow good practices:

setupLPScenario (lines 480-503):

  • Creates realistic LP positions with paired orders
  • Proper error checking with require.NoError

fundVaultAndDistributeFees (lines 505-551):

  • Correctly converts big.Int to NUMERIC(78,0) using ParseDecimalExplicit (line 523)
  • Proper TxContext construction with test overrides
  • Comprehensive error handling (checks both err and res.Error)

45-478: Test structure uses sequential execution; global state reset pattern is intentional.

The lastBalancePoint global variable is intentionally reset at the start of each test function. These tests run sequentially within RunSchemaTest (no t.Parallel() calls), so the explicit resets prevent cross-test contamination while relying on sequential execution order. This pattern is acceptable for the test framework being used, though it would be problematic if tests were ever parallelized.

Test coverage is comprehensive:

  • ✅ Basic audit record creation with zero-loss verification
  • ✅ Multi-block sampling (3 blocks)
  • ✅ Edge case: no LPs → no audit
  • ✅ Edge case: zero fees → no audit
  • ✅ Data integrity: audit matches actual balance changes

Callback-based result handling correctly uses *kwilTypes.Decimal for NUMERIC(78,0) fields.

@MicBun MicBun merged commit fc9f04e into main Dec 10, 2025
7 checks passed
@MicBun MicBun deleted the LpAudit branch December 10, 2025 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feat New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant