Skip to content

Conversation

@jusbar23
Copy link
Contributor

@jusbar23 jusbar23 commented Nov 19, 2025

Changelist

  • Fix issue with 30d volume not being decremented
  • Fix issue with commissions about to go over the cap

Test Plan

  • Test cases

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • Bug Fixes

    • Affiliate fees now respect per-user 30‑day revenue caps (fees clamped when caps near/are reached).
  • New Features

    • Per-fill affiliate attributions for taker and maker are recorded and propagated through matching and stats.
    • Matching computes attributable volumes and enforces per-user 30‑day attribution caps.
  • Stats

    • New fields and APIs expose per-user and per-epoch attributed volume and revenue; expirations decrement attributed/referred volumes.
    • Queries now return attributed 30‑day rolling volume.
  • Tests

    • Extensive attribution and cap tests added; one legacy aggregation test removed.
  • Chores

    • CI workflows expanded to include additional branch patterns.
    • Deprecated an old affiliate tier stake field and removed block-level aggregation logic.

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

@jusbar23 jusbar23 requested a review from a team as a code owner November 19, 2025 19:12
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 19, 2025

Walkthrough

Adds per-fill affiliate attributions, persists and tracks per-user and per-epoch referred and attributed 30‑day volumes in stats, enforces per-user 30‑day affiliate revenue caps when computing affiliate revshare payouts, removes in-block affiliate aggregation/EndBlocker, and updates tests, protos, generated TS, and CI triggers.

Changes

Cohort / File(s) Summary
Revshare keeper
protocol/x/revshare/keeper/revshare.go
Clamp computed affiliate fees to the remaining per-user 30‑day affiliate revenue cap when user stats exist; no exported signature changes.
Revshare tests
protocol/x/revshare/keeper/revshare_test.go
Add "Affiliates has about to go over the edge" scenario and reorder setup to validate cap behavior.
Stats proto, keeper & generated TS
proto/dydxprotocol/stats/stats.proto, protocol/x/stats/keeper/keeper.go, protocol/x/stats/keeper/keeper_test.go, indexer/packages/v4-protos/src/codegen/dydxprotocol/stats/stats.ts
Add AffiliateAttribution message; extend BlockStats.Fill with repeated affiliate_attributions; add affiliate_30d_attributed_volume_quote_quantums to UserStats; extend RecordFill to accept/persist affiliate attributions and update per-user/per-epoch referred & attributed volumes; update tests and generated TS.
Affiliates keeper, types & queries
protocol/x/affiliates/keeper/keeper.go, protocol/x/affiliates/keeper/keeper_test.go, protocol/x/affiliates/types/expected_keepers.go, proto/dydxprotocol/affiliates/query.proto, protocol/x/affiliates/keeper/grpc_query.go, protocol/x/affiliates/keeper/grpc_query_test.go
Remove in-block aggregation methods and related test; add StatsKeeper epoch accessors (GetEpochStatsOrNil, SetEpochStats); expose attributed 30d rolling volume in AffiliateInfoResponse and tests; relax/deprecate staked-field checks.
CLOB keeper integration & tests
protocol/x/clob/keeper/process_single_match.go, protocol/x/clob/keeper/process_single_match_affiliate_stats_test.go, protocol/x/clob/types/expected_keepers.go
Compute cap-aware attributable volumes, build taker/maker AffiliateAttribution entries, pass attributions to Stats.RecordFill; add comprehensive tests for attributions, caps, and expiration.
Affiliates EndBlocker
protocol/x/affiliates/abci.go
Remove EndBlocker aggregation call and logging; EndBlocker becomes a no-op.
Indexer & generated query artifacts
indexer/packages/v4-protos/..., indexer/packages/v4-protos/src/codegen/dydxprotocol/affiliates/query.ts, indexer/packages/v4-protos/src/codegen/dydxprotocol/stats/stats.ts
Add AffiliateAttribution types and fields in generated TS; extend BlockStats.Fill and UserStats generated types to include attribution fields; add attributed volume to AffiliateInfoResponse.
CI/workflows
.github/workflows/*
Add justin/* and justin/bug_fix_95 branch patterns to push triggers for several workflows.

Sequence Diagram(s)

sequenceDiagram
    participant CLOB as CLOB Keeper
    participant AFF as AffiliatesKeeper
    participant STATS as StatsKeeper
    participant REV as Revshare Keeper
    participant U as UserStats Store

    CLOB->>AFF: GetReferredBy(referee)
    CLOB->>CLOB: compute attributableVolume (cap-aware 30d)
    alt referrer(s) present
        CLOB->>STATS: RecordFill(..., affiliateAttributions)
    else no referrer
        CLOB->>STATS: RecordFill(..., nil)
    end
    Note right of STATS `#DDEBF7`: BlockStats.Fill now contains AffiliateAttribution entries\nSTATS updates per-user & per-epoch referred/attributed volumes and persists them
    REV->>U: Read UserStats (Affiliate_30DRevenueGeneratedQuantums)
    alt userStats present and cap > 0
        REV->>REV: remaining = cap - revenueGenerated
        REV->>REV: feesShared = min(calculatedFees, remaining)
    else
        REV->>REV: feesShared = calculatedFees
    end
    REV-->>CLOB: return rev-share amounts (affiliate portion possibly capped)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • Areas needing extra attention:
    • Big-int arithmetic and cap-clamping in protocol/x/revshare/keeper/revshare.go.
    • RecordFill signature changes and all call-sites (CLOB → Stats).
    • Per-epoch snapshot updates and ExpireOldStats adjustments in protocol/x/stats/keeper/keeper.go.
    • Removal of aggregation methods and EndBlocker—verify no dangling callers or tests.

Possibly related PRs

Suggested labels

feature:x/affiliates

Suggested reviewers

  • teddyding
  • anmolagrawal345
  • northstar456

Poem

🐇 I count the hops across thirty-day fields,
I trim each share so payout stays sealed,
I tag referrers in fills that I keep,
I shelve the old EndBlocker and tidy the heap,
A rabbit nods — the caps make the math healed.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 30.77% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main changes: fixing 30d volume removal and commission capping issues mentioned in the changelist.
Description check ✅ Passed The description follows the template structure with Changelist, Test Plan, and Author/Reviewer Checklist sections, though Test Plan is minimal ('Test cases') and no specific labels were manually added as indicated by unchecked checkboxes.
✨ 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 justin/bug_fix_95

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: 0

🧹 Nitpick comments (1)
protocol/x/affiliates/keeper/keeper_test.go (1)

320-352: New affiliate tier validation tests look correct; consider avoiding magic cap values

The added cases for:

  • "Taker fee share ppm greater than cap" (expecting types.ErrRevShareSafetyViolation), and
  • the two “valid tiers” scenarios around req_staked_whole_coins being zero and mixed with non‑zero values

all align well with the proto types and expected validation behavior (monotonic volumes, reasonable ppm ranges, and explicit handling of the deprecated stake field). The scenarios meaningfully guard against misconfigured tiers and regressions in the new cap logic.

If there is a single canonical constant for the maximum allowed taker fee share ppm (e.g., in revshare or affiliates types), it would be slightly more robust to reference that here (and in the whitelist tests below) rather than hard‑coding 550_000, to keep tests in sync with any future cap changes.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 37686ee and ed69c1d.

📒 Files selected for processing (1)
  • protocol/x/affiliates/keeper/keeper_test.go (1 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: hwray
Repo: dydxprotocol/v4-chain PR: 2597
File: indexer/services/ender/src/scripts/handlers/dydx_update_perpetual_v1_handler.sql:16-20
Timestamp: 2024-11-22T18:12:04.606Z
Learning: Avoid suggesting changes to deprecated functions such as `dydx_update_perpetual_v1_handler` in `indexer/services/ender/src/scripts/handlers/dydx_update_perpetual_v1_handler.sql` if they are unchanged in the PR.
📚 Learning: 2024-11-15T16:17:29.092Z
Learnt from: hwray
Repo: dydxprotocol/v4-chain PR: 2551
File: protocol/x/clob/types/expected_keepers.go:86-90
Timestamp: 2024-11-15T16:17:29.092Z
Learning: The function `GetCrossInsuranceFundBalance` in `protocol/x/clob/types/expected_keepers.go` already existed and was just moved in this PR; changes to its error handling may be out of scope.

Applied to files:

  • protocol/x/affiliates/keeper/keeper_test.go
📚 Learning: 2024-11-15T16:00:11.304Z
Learnt from: hwray
Repo: dydxprotocol/v4-chain PR: 2551
File: protocol/x/subaccounts/keeper/subaccount.go:852-865
Timestamp: 2024-11-15T16:00:11.304Z
Learning: The function `GetCrossInsuranceFundBalance` in `protocol/x/subaccounts/keeper/subaccount.go` already existed and was just moved in this PR; changes to its error handling may be out of scope.

Applied to files:

  • protocol/x/affiliates/keeper/keeper_test.go
📚 Learning: 2024-11-15T15:59:28.095Z
Learnt from: hwray
Repo: dydxprotocol/v4-chain PR: 2551
File: protocol/x/subaccounts/keeper/subaccount.go:833-850
Timestamp: 2024-11-15T15:59:28.095Z
Learning: The function `GetInsuranceFundBalance` in `protocol/x/subaccounts/keeper/subaccount.go` already existed and was just moved in this PR; changes to its error handling may be out of scope.

Applied to files:

  • protocol/x/affiliates/keeper/keeper_test.go
🧬 Code graph analysis (1)
protocol/x/affiliates/keeper/keeper_test.go (2)
protocol/x/affiliates/types/affiliates.pb.go (6)
  • AffiliateTiers (28-31)
  • AffiliateTiers (35-35)
  • AffiliateTiers (36-38)
  • AffiliateTiers_Tier (74-82)
  • AffiliateTiers_Tier (86-86)
  • AffiliateTiers_Tier (87-89)
protocol/x/affiliates/types/errors.go (1)
  • ErrRevShareSafetyViolation (13-14)
⏰ 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). (60)
  • GitHub Check: build
  • GitHub Check: test-race
  • GitHub Check: unit-end-to-end-and-integration
  • GitHub Check: test / run_command
  • GitHub Check: call-build-ecs-service-comlink / (comlink) Check docker image build
  • GitHub Check: call-build-ecs-service-roundtable / (roundtable) Check docker image build
  • GitHub Check: check-build-auxo
  • GitHub Check: call-build-ecs-service-ender / (ender) Check docker image build
  • GitHub Check: check-build-bazooka
  • GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-auxo-lambda / (auxo) Build and Push Lambda
  • GitHub Check: (Mainnet) Build and Push ECS Services / call-build-and-push-bazooka-lambda / (bazooka) Build and Push Lambda
  • GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-bazooka-lambda / (bazooka) Build and Push Lambda
  • GitHub Check: (Mainnet) Build and Push ECS Services / call-build-and-push-ecs-service-socks / (socks) Build and Push
  • GitHub Check: (Mainnet) Build and Push ECS Services / call-build-and-push-ecs-service-comlink / (comlink) Build and Push
  • GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-ecs-service-roundtable / (roundtable) Build and Push
  • GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-ecs-service-ender / (ender) Build and Push
  • GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-ecs-service-comlink / (comlink) Build and Push
  • GitHub Check: run_command
  • GitHub Check: benchmark
  • GitHub Check: lint
  • GitHub Check: build-and-push-mainnet
  • GitHub Check: check-sample-pregenesis-up-to-date
  • GitHub Check: golangci-lint
  • GitHub Check: build-and-push-testnet
  • GitHub Check: (Dev4) Build and Push ECS Services / call-build-and-push-ecs-service-ender / (ender) Build and Push
  • GitHub Check: (Dev4) Build and Push ECS Services / call-build-and-push-auxo-lambda / (auxo) Build and Push Lambda
  • GitHub Check: (Staging) Build and Push ECS Services / call-build-and-push-vulcan / (vulcan) Build and Push
  • GitHub Check: (Dev4) Build and Push ECS Services / call-build-and-push-bazooka-lambda / (bazooka) Build and Push Lambda
  • GitHub Check: (Dev4) Build and Push ECS Services / call-build-and-push-ecs-service-socks / (socks) Build and Push
  • GitHub Check: (Staging) Build and Push ECS Services / call-build-and-push-auxo-lambda / (auxo) Build and Push Lambda
  • GitHub Check: (Staging) Build and Push ECS Services / call-build-and-push-bazooka-lambda / (bazooka) Build and Push Lambda
  • GitHub Check: Analyze (go)
  • GitHub Check: (Staging) Build and Push ECS Services / call-build-and-push-ecs-service-roundtable / (roundtable) Build and Push
  • GitHub Check: (Staging) Build and Push ECS Services / call-build-and-push-ecs-service-socks / (socks) Build and Push
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: (Staging) Build and Push ECS Services / call-build-and-push-ecs-service-comlink / (comlink) Build and Push
  • GitHub Check: (Staging) Build and Push ECS Services / call-build-and-push-ecs-service-ender / (ender) Build and Push
  • GitHub Check: (Dev2) Build and Push ECS Services / call-build-and-push-ecs-service-comlink / (comlink) Build and Push
  • GitHub Check: (Dev2) Build and Push ECS Services / call-build-and-push-ecs-service-socks / (socks) Build and Push
  • GitHub Check: (Dev2) Build and Push ECS Services / call-build-and-push-ecs-service-ender / (ender) Build and Push
  • GitHub Check: (Dev2) Build and Push ECS Services / call-build-and-push-auxo-lambda / (auxo) Build and Push Lambda
  • GitHub Check: (Dev2) Build and Push ECS Services / call-build-and-push-bazooka-lambda / (bazooka) Build and Push Lambda
  • GitHub Check: (Dev2) Build and Push ECS Services / call-build-and-push-vulcan / (vulcan) Build and Push
  • GitHub Check: (Dev2) Build and Push ECS Services / call-build-and-push-ecs-service-roundtable / (roundtable) Build and Push
  • GitHub Check: (Dev) Build and Push ECS Services / call-build-and-push-vulcan / (vulcan) Build and Push
  • GitHub Check: (Dev) Build and Push ECS Services / call-build-and-push-ecs-service-roundtable / (roundtable) Build and Push
  • GitHub Check: (Dev) Build and Push ECS Services / call-build-and-push-ecs-service-socks / (socks) Build and Push
  • GitHub Check: (Dev) Build and Push ECS Services / call-build-and-push-ecs-service-comlink / (comlink) Build and Push
  • GitHub Check: (Dev) Build and Push ECS Services / call-build-and-push-ecs-service-ender / (ender) Build and Push
  • GitHub Check: (Dev) Build and Push ECS Services / call-build-and-push-bazooka-lambda / (bazooka) Build and Push Lambda
  • GitHub Check: (Dev) Build and Push ECS Services / call-build-and-push-auxo-lambda / (auxo) Build and Push Lambda
  • GitHub Check: Summary
  • GitHub Check: build-and-push-staging
  • GitHub Check: build-and-push-dev4
  • GitHub Check: build-and-push-dev
  • GitHub Check: build-and-push-snapshot-staging
  • GitHub Check: build-and-push-dev2
  • GitHub Check: build-and-push-snapshot-dev
  • GitHub Check: build-and-push-snapshot-dev4
  • GitHub Check: build-and-push-snapshot-dev2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants