Skip to content

test: verify rewards distribution#107

Merged
MicBun merged 2 commits intomainfrom
testRewards
Mar 9, 2026
Merged

test: verify rewards distribution#107
MicBun merged 2 commits intomainfrom
testRewards

Conversation

@MicBun
Copy link
Member

@MicBun MicBun commented Mar 9, 2026

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

Summary by CodeRabbit

  • Documentation
    • Added an example demonstrating a full market P&L verification workflow: market creation, order placement, settlement, and P&L/collateral checks with progress reporting.
    • Added an example for verifying LP reward distribution and viewing reward history for market participants, with summary outputs for distributed fees and per-participant rewards.

@MicBun MicBun requested a review from pr-time-tracker March 9, 2026 22:01
@MicBun MicBun self-assigned this Mar 9, 2026
@holdex
Copy link

holdex bot commented Mar 9, 2026

Time Submission Status

Member Status Time Action Last Update
MicBun ✅ Submitted 3h 30min Update time Mar 9, 2026, 10:32 PM

You can submit time with the command. Example:

@holdex pr submit-time 15m

See available commands to help comply with our Guidelines.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 9, 2026

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

Adds two new example scripts demonstrating market lifecycle orchestration and LP reward/P&L verification via TNClient and indexer HTTP endpoints, including exponential-backoff retry helpers and CLI entry for reward checks.

Changes

Cohort / File(s) Summary
P&L Verification Example
examples/order_book/05_verify_pnl.py
New script that creates a market, places maker/taker orders, waits for settlement, then queries indexer endpoints to verify P&L summaries, chart data, and rewards history. Adds with_retry and main.
LP Rewards Verification Example
examples/order_book/06_check_lp_rewards.py
New script that fetches market distribution summary and per-participant reward history (CLI main(query_id)), with an exponential-backoff with_retry helper and printed verification output.

Sequence Diagram(s)

sequenceDiagram
    participant Script
    participant TNClient
    participant Node
    participant Indexer
    participant MM_Wallet
    participant Buyer_Wallet

    Script->>TNClient: create_market(settle_time, params)
    TNClient->>Node: submit tx (market creation)
    Node-->>TNClient: tx result
    Script->>TNClient: place_split_limit_order(MM_Wallet)
    TNClient->>Node: submit tx (maker order)
    Node-->>TNClient: tx result
    Script->>TNClient: place_buy_order(Buyer_Wallet)
    TNClient->>Node: submit tx (taker order)
    Node-->>TNClient: tx result
    Script->>Indexer: GET /distribution?query_id=...
    Indexer-->>Script: distribution summary
    Script->>Indexer: GET /rewards?query_id=...&wallet=MM_Wallet
    Indexer-->>Script: MM rewards history
    Script->>Indexer: GET /rewards?query_id=...&wallet=Buyer_Wallet
    Indexer-->>Script: Buyer rewards history
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

documentation

Suggested reviewers

  • pr-time-tracker

Poem

🐰 I hopped to run a market test,
Orders placed and ledgers pressed,
P&L sings, rewards appear,
Indexer answers, loud and clear,
A tiny rabbit cheers the quest! 🎉

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'test: verify rewards distribution' is specific and directly related to the main changes, which add two new verification scripts (05_verify_pnl.py and 06_check_lp_rewards.py) for P&L and rewards distribution verification.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch testRewards

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.

Copy link
Contributor

@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: 4

🧹 Nitpick comments (5)
examples/order_book/05_verify_pnl.py (2)

43-46: Prefer bare raise to preserve full traceback.

Using raise e instead of raise can truncate the traceback in some edge cases.

🧹 Proposed fix
             if retries >= max_retries:
-                raise e
+                raise
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/order_book/05_verify_pnl.py` around lines 43 - 46, In the except
block that catches Exception as e (the retry loop using retries and
max_retries), replace the explicit "raise e" with a bare "raise" to preserve the
original traceback; keep the same logic that increments retries and re-raises
only when retries >= max_retries (i.e., in the except Exception as e handler,
use "raise" instead of re-raising the exception object).

120-131: Inconsistent with earlier examples: use SDK methods for distribution queries.

The preceding order_book examples (01-04) consistently use SDK client methods. File 05 breaks this pattern by using direct HTTP requests. The SDK provides get_distribution_summary(query_id) to fetch distribution data with type safety and consistent error handling. Examples 01-04 demonstrate that SDK methods are the standard approach for this example series; using them here would maintain consistency.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/order_book/05_verify_pnl.py` around lines 120 - 131, Replace the
direct HTTP call using with_retry(requests.get, dist_url) with the SDK method
get_distribution_summary(query_id) on the existing SDK client (e.g., call
client.get_distribution_summary(query_id)); check the SDK response for
success/error using the SDK's return structure, extract the distribution data
(total_fees_distributed, distributed_at) from the returned object instead of
resp.json(), and print the same fields; remove reliance on with_retry/requests
in this block so the example matches earlier files that use the SDK's type-safe
method.
examples/order_book/06_check_lp_rewards.py (3)

5-7: Remove unused imports.

timedelta and Web3 are imported but never used in this script.

🧹 Proposed fix
-from datetime import datetime, timezone, timedelta
+from datetime import datetime, timezone
 import requests
-from web3 import Web3
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/order_book/06_check_lp_rewards.py` around lines 5 - 7, The imports
include unused symbols timedelta and Web3; remove them from the import
statements so only used symbols remain (keep datetime, timezone and requests).
Specifically edit the top import line that currently references timedelta and
the Web3 import (the Web3 class) and delete those unused imports to eliminate
dead code and lint warnings.

10-13: Remove unused constants.

NODE_URL and TEST_CHAIN_ID are defined but never used in this script.

🧹 Proposed fix
 # --- Configuration ---
-# Kwil/Node Configuration
-NODE_URL = "http://ec2-3-141-77-16.us-east-2.compute.amazonaws.com:8484"
 INDEXER_URL = "http://ec2-52-15-66-172.us-east-2.compute.amazonaws.com:8080"
-TEST_CHAIN_ID = "testnet-v1"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/order_book/06_check_lp_rewards.py` around lines 10 - 13, Remove the
unused constants NODE_URL and TEST_CHAIN_ID from the top of the script; keep
only INDEXER_URL (or any other constants that are actually referenced later) to
avoid dead code and confusion, and run a quick grep/IDE search to ensure no
remaining references to NODE_URL or TEST_CHAIN_ID before committing.

20-31: Inconsistent with_retry implementation between example scripts.

This implementation differs significantly from 05_verify_pnl.py:

  • 3 retries vs 5 retries with exponential backoff
  • Returns empty Response() vs raises exception on failure
  • Fixed 2s sleep vs exponential backoff

The empty Response() return is subtle—its status_code attribute is None, which happens to work with the == 200 checks but could cause confusion. Consider aligning implementations or extracting a shared utility.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/order_book/06_check_lp_rewards.py` around lines 20 - 31, The
with_retry function currently retries 3 times with a fixed 2s sleep and returns
an empty requests.Response() on failure; change it to match the behavior in
05_verify_pnl.py by performing 5 attempts with exponential backoff (e.g., sleep
grows each retry), propagate/raise an exception when all retries fail instead of
returning an empty Response(), and preserve checking resp.status_code == 200
before returning resp; update error handling for
requests.exceptions.RequestException to include the exception when raising so
callers of with_retry (calling code that checks resp.status_code) get a clear
failure instead of a bogus Response object.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@examples/order_book/05_verify_pnl.py`:
- Around line 142-143: The print statement currently uses an unnecessary
f-string: change the line that prints "  Summary P&L:" (the print after
assigning data = resp.json().get("data", {})) to use a normal string literal
(remove the leading "f") so it is print("  Summary P&L:") instead of print(f" 
Summary P&L:"); no other behavior changes required.
- Around line 164-173: The loop that prints reward entries uses direct dict
access (r['query_id'], r['reward_amount'], r['distributed_at']) which can raise
KeyError if the API omits fields; update the printing logic in the block that
builds rewards_url/resp/data to use r.get('query_id'), r.get('reward_amount')
and r.get('distributed_at') (providing sensible defaults like None or 'N/A') or
guard each field before formatting so missing keys don't crash the script; keep
the rest of the rewards_url/resp handling the same.

In `@examples/order_book/06_check_lp_rewards.py`:
- Around line 54-55: The print statement in the else branch (the line containing
print(f"  - Distributed At:         Not yet distributed")) uses an unnecessary
f-string prefix even though there are no placeholders; change that print call to
a normal string literal by removing the leading "f" so it reads print("  -
Distributed At:         Not yet distributed") to eliminate the extraneous f
prefix.
- Around line 72-74: The loop over rewards uses direct dict indexing
(r['reward_amount'] and r['distributed_at']) which can raise KeyError; update
the block that iterates the rewards list to access r.get('reward_amount',
<default>) and r.get('distributed_at') safely (e.g., reward_amount =
r.get('reward_amount', 0) and ts = r.get('distributed_at')), then only call
datetime.fromtimestamp(ts, timezone.utc).strftime(...) if ts is not None and is
a number, otherwise use a fallback string like "unknown time"; apply this change
where the rewards iteration/print occurs (variables: rewards, r, reward_amount,
distributed_at/ts).

---

Nitpick comments:
In `@examples/order_book/05_verify_pnl.py`:
- Around line 43-46: In the except block that catches Exception as e (the retry
loop using retries and max_retries), replace the explicit "raise e" with a bare
"raise" to preserve the original traceback; keep the same logic that increments
retries and re-raises only when retries >= max_retries (i.e., in the except
Exception as e handler, use "raise" instead of re-raising the exception object).
- Around line 120-131: Replace the direct HTTP call using
with_retry(requests.get, dist_url) with the SDK method
get_distribution_summary(query_id) on the existing SDK client (e.g., call
client.get_distribution_summary(query_id)); check the SDK response for
success/error using the SDK's return structure, extract the distribution data
(total_fees_distributed, distributed_at) from the returned object instead of
resp.json(), and print the same fields; remove reliance on with_retry/requests
in this block so the example matches earlier files that use the SDK's type-safe
method.

In `@examples/order_book/06_check_lp_rewards.py`:
- Around line 5-7: The imports include unused symbols timedelta and Web3; remove
them from the import statements so only used symbols remain (keep datetime,
timezone and requests). Specifically edit the top import line that currently
references timedelta and the Web3 import (the Web3 class) and delete those
unused imports to eliminate dead code and lint warnings.
- Around line 10-13: Remove the unused constants NODE_URL and TEST_CHAIN_ID from
the top of the script; keep only INDEXER_URL (or any other constants that are
actually referenced later) to avoid dead code and confusion, and run a quick
grep/IDE search to ensure no remaining references to NODE_URL or TEST_CHAIN_ID
before committing.
- Around line 20-31: The with_retry function currently retries 3 times with a
fixed 2s sleep and returns an empty requests.Response() on failure; change it to
match the behavior in 05_verify_pnl.py by performing 5 attempts with exponential
backoff (e.g., sleep grows each retry), propagate/raise an exception when all
retries fail instead of returning an empty Response(), and preserve checking
resp.status_code == 200 before returning resp; update error handling for
requests.exceptions.RequestException to include the exception when raising so
callers of with_retry (calling code that checks resp.status_code) get a clear
failure instead of a bogus Response object.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 688d191d-b8cb-4ea4-ba63-b10c11aafdcc

📥 Commits

Reviewing files that changed from the base of the PR and between b0ba15f and 2c1347a.

📒 Files selected for processing (2)
  • examples/order_book/05_verify_pnl.py
  • examples/order_book/06_check_lp_rewards.py

@MicBun MicBun merged commit 05841b6 into main Mar 9, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant