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

[OTE-776] Implement GetAllRevshares #2228

Merged
merged 8 commits into from
Sep 12, 2024
Merged

[OTE-776] Implement GetAllRevshares #2228

merged 8 commits into from
Sep 12, 2024

Conversation

affanv14
Copy link
Contributor

@affanv14 affanv14 commented Sep 9, 2024

Changelist

Implement functions to get all revshares for all market types

Test Plan

Adds unit tests

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

    • Introduced a new interface and struct for processing perpetual fills, improving data organization and encapsulation.
    • Enhanced revenue share calculations with new methods for retrieving and aggregating shares from various sources.
    • Added a constant for maximum referee transaction volume over 30 days to ensure consistency in financial metrics.
    • Implemented new error handling for scenarios where total fees shared exceed net fees.
  • Bug Fixes

    • Improved error handling in revenue share calculations to manage potential issues effectively.
  • Tests

    • Added tests to validate revenue share retrieval under both valid and invalid scenarios, enhancing the reliability of the revenue sharing logic.

@affanv14 affanv14 requested a review from a team as a code owner September 9, 2024 19:42
Copy link

linear bot commented Sep 9, 2024

tests for revshare

changes
Copy link
Contributor

coderabbitai bot commented Sep 9, 2024

Walkthrough

This pull request introduces enhancements to the types and revshare packages in a Go module. It adds a new interface and struct for processing perpetual fills, methods for calculating revenue shares, a constant for financial metrics, and error handling improvements. The changes facilitate better organization of fill-related data and modular revenue share calculations, ensuring robust functionality across the application.

Changes

File Path Change Summary
protocol/x/clob/types/types.go Added FillForProcess interface, PerpetualFillForProcess struct, associated methods, and a factory function CreatePerpetualFillForProcess.
protocol/x/revshare/keeper/revshare.go Introduced GetAllRevShares method and private methods for calculating revenue shares from different sources, along with error handling for fee calculations.
protocol/x/revshare/keeper/revshare_test.go Added tests for valid and invalid scenarios of revenue share retrieval, ensuring functionality and error handling are properly validated.
protocol/x/revshare/types/constants.go Created a constant Max30dRefereeVolumeQuantums to represent a financial threshold for referee transactions.
protocol/x/revshare/types/errors.go Added error constant ErrTotalFeesSharedExceedsNetFees for improved error handling in revenue share calculations.
protocol/x/revshare/types/types.go Defined a new RevShare struct and enumerated types RevShareFeeSource and RevShareType with associated constants to enhance revenue sharing capabilities.

Assessment against linked issues

Objective Addressed Explanation
Validate revenue share calculations No linked issues to assess against.
Improve error handling for fee calculations No linked issues to assess against.

Possibly related issues

  • No related issues were identified that align with the changes in this PR.

Possibly related PRs

  • [CT-1178] Only emit finalized subaccount update #2181: The changes in this PR involve modifications to the Keeper struct, specifically adding a method that utilizes the FillForProcess interface, which is directly related to the main PR's introduction of the FillForProcess interface and its associated struct.
  • [OTE-791] Add proto for affiliate whitelist #2232: This PR focuses on affiliate whitelisting, introducing new interfaces and functions that may relate to the handling of financial metrics and structures relevant to the PerpetualFillForProcess struct's functionality.

Suggested labels

indexer, proto

Poem

🐰 In the meadow where fills abound,
New structures and methods are now found.
With revenue shares and constants bright,
Our code dances in the soft moonlight.
Hooray for the changes, let’s hop and cheer,
For a future of fills that’s wonderfully clear! 🌼


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1072ab8 and 5d22979.

Files selected for processing (6)
  • protocol/x/clob/types/types.go (1 hunks)
  • protocol/x/revshare/keeper/revshare.go (2 hunks)
  • protocol/x/revshare/keeper/revshare_test.go (2 hunks)
  • protocol/x/revshare/types/constants.go (1 hunks)
  • protocol/x/revshare/types/errors.go (1 hunks)
  • protocol/x/revshare/types/types.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • protocol/x/revshare/types/constants.go
Additional comments not posted (22)
protocol/x/revshare/types/errors.go (1)

34-38: LGTM!

The new error constant ErrTotalFeesSharedExceedsNetFees is appropriately named, has a unique error code, and a clear error message. The code changes are approved.

protocol/x/revshare/types/types.go (6)

3-7: LGTM!

The imports are necessary for the new types and constants added in this file. The code changes are approved.


32-37: LGTM!

The RevShare struct is well-defined with appropriately named and typed fields. The code changes are approved.


39-40: LGTM!

The RevShareFeeSource type is correctly defined as an integer. The code changes are approved.


41-45: LGTM!

The RevShareFeeSource constants are correctly defined using the iota keyword and are appropriately named. The code changes are approved.


47-48: LGTM!

The RevShareType type is correctly defined as an integer. The code changes are approved.


49-54: LGTM!

The RevShareType constants are correctly defined using the iota keyword and are appropriately named. The code changes are approved.

protocol/x/clob/types/types.go (9)

2-3: LGTM!

The import is necessary for the new types and functions added in this file. The code changes are approved.


5-12: LGTM!

The FillForProcess interface is well-defined with appropriately named and typed methods. The code changes are approved.


14-21: LGTM!

The PerpetualFillForProcess struct is well-defined with appropriately named and typed fields. The code changes are approved.


23-25: LGTM!

The TakerAddr method is correctly implemented and follows the FillForProcess interface. The code changes are approved.


27-29: LGTM!

The TakerFeeQuoteQuantums method is correctly implemented and follows the FillForProcess interface. The code changes are approved.


31-33: LGTM!

The MakerAddr method is correctly implemented and follows the FillForProcess interface. The code changes are approved.


35-37: LGTM!

The MakerFeeQuoteQuantums method is correctly implemented and follows the FillForProcess interface. The code changes are approved.


39-41: LGTM!

The FillQuoteQuantums method is correctly implemented and follows the FillForProcess interface. The code changes are approved.


43-63: LGTM!

The PerpetualId method and the CreatePerpetualFillForProcess factory function are correctly implemented and follow the FillForProcess interface. The code changes are approved.

protocol/x/revshare/keeper/revshare.go (4)

150-191: LGTM!

The code changes are approved. The function is well-structured, modular, and correctly aggregates revenue shares from various sources while performing the necessary checks.


193-221: LGTM!

The code changes are approved. The function correctly handles the logic for calculating affiliate revenue shares.


223-245: LGTM!

The code changes are approved. The function correctly handles the logic for calculating unconditional revenue shares.


247-272: LGTM!

The code changes are approved. The function correctly handles the logic for calculating market mapper revenue shares.

protocol/x/revshare/keeper/revshare_test.go (2)

270-444: LGTM!

The code changes are approved. The test function is comprehensive and covers various scenarios for valid revenue share retrieval, ensuring the correctness of the GetAllRevShares function.


446-583: LGTM!

The code changes are approved. The test function thoroughly tests the error scenarios for revenue share retrieval, ensuring that the GetAllRevShares function correctly handles invalid configurations.

totalFeesShared.Add(totalFeesShared, feeShared)
revShare := types.RevShare{
Recipient: revShare.Address,
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_FEE,
Copy link
Contributor

@teddyding teddyding Sep 11, 2024

Choose a reason for hiding this comment

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

On second thought, do we need this in any downstream logic, besides metrics?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dont we need revshare Fee source in trading rewards formula?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And revshare type to retrieve affiliate revshares later for sending indexer events

Copy link
Contributor

Choose a reason for hiding this comment

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

Dont we need revshare Fee source in trading rewards formula?

Ah yes we need so that we can separate reward logic for maker and taker fee

name string
revenueSharePpmNetFees uint32
revenueSharePpmTakerFees uint32
expectedRevShares int
Copy link
Contributor

Choose a reason for hiding this comment

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

Should have more granular expected value than just # of rev shares. e.g. []types.RevShare

},
fill: clobtypes.CreatePerpetualFillForProcess(
Copy link
Contributor

@teddyding teddyding Sep 12, 2024

Choose a reason for hiding this comment

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

This function actually makes the code less readable, could we just replace this with a struct literal whereever it's used? For example it's hard for reader to tell what these fields are. For sensitive object like fills, we want no room for accidentally setting the wrong fields

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we want to get rid of the function and update the struct directly, we have to rename the fields to be capitalized(same as the interface - causing errors)
two options here:

  1. Remove interface entirely and stick with structs
  2. Change the anme of underlying stuct vars(like PerpetualMarketAddr or MarketAddr_) which might be a little more confusing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

im leaning 1

big.NewInt(10_000_000),
constants.BobAccAddress.String(),
big.NewInt(2_000_000),
big.NewInt(100000),
Copy link
Contributor

Choose a reason for hiding this comment

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

Fees are usually in the magnitude of ~0.01% of fill quote quantums, so let's use more realistic values here

expectedNetFeeRevSharesPpmByAddress: map[string]uint32{
constants.AliceAccAddress.String(): 400_000, // 40% of net fees
constants.BobAccAddress.String(): 200_000, // 20% of net fees
name: "Valid revenue share with 30d volume greater than max 30d referral volume",
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test where maker fee is negative and taker fee is positive, and check that market mapper and unconditonal rev share are based net fee

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

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

411-411: Consider adding a test case for negative maker fee and positive taker fee.

To ensure the robustness of the GetAllRevShares function, it would be beneficial to add a test case where the maker fee is negative and the taker fee is positive. This test case should verify that the market mapper and unconditional revenue shares are based on the net fee.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f34b786 and 0e5d315.

Files selected for processing (3)
  • protocol/x/clob/types/types.go (1 hunks)
  • protocol/x/revshare/keeper/revshare.go (2 hunks)
  • protocol/x/revshare/keeper/revshare_test.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/x/revshare/keeper/revshare.go
Additional comments not posted (3)
protocol/x/clob/types/types.go (1)

5-19: LGTM!

The FillForProcess struct is well-defined and serves its purpose of encapsulating fill-related data. The decision to keep the struct in the types package is justified based on the past review comments. The use of ProductId instead of PerpetualId aligns with the suggestion for generalization. The addition of the MonthlyRollingTakerVolumeQuantums field enhances the ability to analyze trading activity over time.

The code changes are consistent with the AI-generated summary and address the concerns raised in the past review comments.

protocol/x/revshare/keeper/revshare_test.go (2)

279-279: Great job on the descriptive test case name!

The test case name clearly conveys the scenario being tested.


591-728: Excellent test coverage for invalid scenarios!

The test cases thoroughly cover scenarios where the total fees shared exceed the net fees from different sources. This helps ensure that the GetAllRevShares function correctly handles and returns the expected errors in such cases.

@affanv14 affanv14 merged commit ac1e578 into main Sep 12, 2024
22 checks passed
@affanv14 affanv14 deleted the affan/revshare-types branch September 12, 2024 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants