Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GetAllRevshare to handle liquidations #2413

Merged
merged 5 commits into from
Oct 2, 2024
Merged

Conversation

affanv14
Copy link
Contributor

@affanv14 affanv14 commented Sep 30, 2024

Changelist

Failure error logs when going through liquidations since maker and taker fees are 0. This handles that case

Test Plan

[Describe how this PR was tested (if applicable)]

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

  • New Features

    • Enhanced revenue sharing functionality with improved checks for net fees.
    • Refined logic for calculating affiliate revenue shares based on specified thresholds.
    • Clarified error messages related to affiliate fees.
  • Bug Fixes

    • Improved error handling for revenue share calculations to prevent exceeding defined limits.
  • Tests

    • Added new test cases to validate behavior with zero fees and refined existing tests for accuracy in revenue share calculations.
    • Standardized setup functions for consistency across test scenarios.

@affanv14 affanv14 requested a review from a team as a code owner September 30, 2024 21:43
Copy link
Contributor

coderabbitai bot commented Sep 30, 2024

Walkthrough

The pull request introduces enhancements to the revenue sharing logic within the keeper package of the revshare.go file. Key improvements include added checks for net fees, refined calculations for affiliate revenue shares, and updates to related methods for robust revenue share computations. The test suite has been expanded and modified to effectively validate the new logic and error handling. Additionally, changes in error declarations clarify conditions related to affiliate fees.

Changes

Files Change Summary
protocol/x/revshare/keeper/revshare.go, protocol/x/revshare/keeper/revshare_test.go Modifications to revenue sharing methods, including checks for net fees, refined affiliate share calculations, and improved error handling; new and modified test cases validate updated logic.
protocol/x/revshare/types/errors.go Renaming and clarification of error messages related to affiliate fees, enhancing understanding of conditions checked in revenue share calculations.

Possibly related PRs

Suggested labels

revshare

Suggested reviewers

  • vincentwschau
  • teddyding

Poem

In the fields where profits grow,
Revenue shares begin to flow.
With checks and balances in place,
Our logic finds a steady pace.
So hop along, let's celebrate,
For sharing wealth is truly great! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 08e5f4f and a4dc21f.

📒 Files selected for processing (1)
  • protocol/x/revshare/types/errors.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • protocol/x/revshare/types/errors.go

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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

🧹 Outside diff range and nitpick comments (1)
protocol/x/revshare/keeper/revshare.go (1)

178-182: Consider Adding Unit Tests for Zero Net Fees Scenario

While the new condition appropriately handles the zero netFees case, it's important to include unit tests to ensure this scenario is covered and to prevent future regressions.

Would you like assistance in writing unit tests for this case or opening a GitHub issue to track this task?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 07fc0b6 and 42ab021.

📒 Files selected for processing (2)
  • protocol/x/revshare/keeper/revshare.go (1 hunks)
  • protocol/x/revshare/keeper/revshare_test.go (1 hunks)
🔇 Additional comments (1)
protocol/x/revshare/keeper/revshare.go (1)

178-182: Proper Handling of Zero Net Fees during Liquidations

The added code correctly handles scenarios where netFees is zero, such as during liquidations when both maker and taker fees are zero. By returning an empty RevSharesForFill, the function avoids unnecessary calculations and potential errors when there's no revenue to share.

Comment on lines 729 to 730
{
name: "Valid revenue share from affiliates, unconditional and market mapper",
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Duplicate Test Case Name: Rename the test case to reflect zero fees scenario

The test case at lines 729-770 has the same name as a previous test case: "Valid revenue share from affiliates, unconditional and market mapper". To avoid confusion and improve clarity, consider renaming the test case to reflect that it's testing the scenario where both maker and taker fees are zero.

Apply this diff to rename the test case:

-			name: "Valid revenue share from affiliates, unconditional and market mapper",
+			name: "Valid revenue share with zero maker and taker fees",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{
name: "Valid revenue share from affiliates, unconditional and market mapper",
{
name: "Valid revenue share with zero maker and taker fees",

@affanv14 affanv14 force-pushed the affan/liquidation-fix branch from 42ab021 to 9565562 Compare October 1, 2024 15:37
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: 1

🧹 Outside diff range and nitpick comments (2)
protocol/x/affiliates/abci.go (2)

14-14: Approve the simplified error logging, with a minor suggestion.

The change from using runtime/debug.Stack() to log.ErrorLogWithError is a good improvement. It simplifies the error logging and likely reduces log verbosity while still capturing essential information.

Consider adding more context to the error message to aid in debugging:

- log.ErrorLogWithError(ctx, "error aggregating affiliate volume for fills", err)
+ log.ErrorLogWithError(ctx, "EndBlocker: error aggregating affiliate volume for fills", err)

This minor change would make it easier to trace the origin of the error in logs.


14-14: Consider the trade-offs of simplified error logging.

While the simplified error logging is generally an improvement, it's worth considering the following points:

  1. Performance: This change might slightly improve performance by reducing the overhead of capturing full stack traces.
  2. Debugging: There's a trade-off between reduced log verbosity and potentially losing some debug information. Ensure that the log.ErrorLogWithError function captures enough context to facilitate effective debugging, especially for issues related to the liquidation process mentioned in the PR objectives.
  3. Consistency: If this is a new logging approach, consider applying it consistently across the codebase for uniform error handling.

Could you provide more information about the log.ErrorLogWithError function? Specifically, what context it captures and how it differs from the previous logging mechanism? This would help in assessing whether any critical information is being lost in this transition.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9565562 and 806dc05.

📒 Files selected for processing (2)
  • protocol/x/affiliates/abci.go (1 hunks)
  • protocol/x/clob/keeper/process_single_match.go (1 hunks)

@@ -467,7 +467,7 @@ func (k Keeper) persistMatchedOrders(
revSharesForFill, err := k.revshareKeeper.GetAllRevShares(ctx, fillForProcess, affiliatesWhitelistMap)
if err != nil {
revSharesForFill = revsharetypes.RevSharesForFill{}
log.ErrorLog(ctx, "error getting rev shares for fill", "error", err)
log.ErrorLogWithError(ctx, "error getting rev shares for fill", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle error from GetAllRevShares appropriately instead of proceeding

When k.revshareKeeper.GetAllRevShares returns an error, the current implementation logs the error but continues execution with an empty revSharesForFill. This may lead to incorrect fee distributions since subsequent calls, such as DistributeFees, rely on revSharesForFill to accurately represent revenue shares.

It's important to handle this error by returning it to ensure the system does not proceed in an invalid state.

Apply this diff to return the error:

 if err != nil {
     revSharesForFill = revsharetypes.RevSharesForFill{}
-    log.ErrorLogWithError(ctx, "error getting rev shares for fill", err)
+    return takerUpdateResult, makerUpdateResult, affiliateRevSharesQuoteQuantums, err
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
log.ErrorLogWithError(ctx, "error getting rev shares for fill", err)
if err != nil {
revSharesForFill = revsharetypes.RevSharesForFill{}
return takerUpdateResult, makerUpdateResult, affiliateRevSharesQuoteQuantums, err
}

if netFees.Sign() == 0 {
return types.RevSharesForFill{}, nil
}

affiliateRevShares, affiliateFeesShared, err := k.getAffiliateRevShares(ctx, fill, affiliatesWhitelistMap)
if err != nil {
return types.RevSharesForFill{}, err
Copy link
Contributor

@teddyding teddyding Oct 1, 2024

Choose a reason for hiding this comment

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

Comment on this line below: error message affiliate fees shared exceeds net fees" is not accurate since we return error when netFeesSubAffiliateFeesShared = 0 as well. Should be sth like net fees minus affiliate fee share is not larger than zero

@@ -175,6 +175,11 @@ func (k Keeper) GetAllRevShares(
makerFees := fill.MakerFeeQuoteQuantums
netFees := big.NewInt(0).Add(takerFees, makerFees)

// net fees is 0 in case of liquidations where maker and taker fees are 0
Copy link
Contributor

@teddyding teddyding Oct 1, 2024

Choose a reason for hiding this comment

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

This comment is not general enough. Net fees can be zero if takerFee > 0 and makerFee < 0. It should just say "when net fee is zero, no rev share is generated from the fill'

@@ -726,6 +726,48 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) {
affiliatesKeeper *affiliateskeeper.Keeper) {
},
},
{
Copy link
Contributor

Choose a reason for hiding this comment

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

From thread, could we also add a unit test with positive maker fees and zero taker fee?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch!

@@ -238,7 +238,8 @@ func (k Keeper) getAffiliateRevShares(
) ([]types.RevShare, *big.Int, error) {
takerAddr := fill.TakerAddr
takerFee := fill.TakerFeeQuoteQuantums
if fill.MonthlyRollingTakerVolumeQuantums >= types.MaxReferee30dVolumeForAffiliateShareQuantums {
if fill.MonthlyRollingTakerVolumeQuantums >= types.MaxReferee30dVolumeForAffiliateShareQuantums ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Better to stay consistent with the 0 net fee use case here instead of return a 0 fee revshare

ModuleName,
7,
"affiliate fees shared exceeds net fees",
"net fees minus affiliate fee share is not larger than zero",
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: var name and err message is saying the same thing but differently:

AffiliateFee >= netFee
netFee - AffiliateFee <= 0

Let's make them consistent for readability

@affanv14
Copy link
Contributor Author

affanv14 commented Oct 2, 2024

@Mergifyio backport release/protocol/v7.x

Copy link
Contributor

mergify bot commented Oct 2, 2024

backport release/protocol/v7.x

✅ Backports have been created

@affanv14 affanv14 merged commit 829b68b into main Oct 2, 2024
22 checks passed
@affanv14 affanv14 deleted the affan/liquidation-fix branch October 2, 2024 20:24
mergify bot pushed a commit that referenced this pull request Oct 2, 2024
affanv14 added a commit that referenced this pull request Oct 3, 2024
Co-authored-by: Mohammed Affan <affan@dydx.exchange>
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.

3 participants