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-790]Implement affiliates whitelist logic #2258

Merged
merged 13 commits into from
Sep 19, 2024
Merged

Conversation

affanv14
Copy link
Contributor

@affanv14 affanv14 commented Sep 16, 2024

Changelist

Implements whitelisting functionality for affiliates

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 the ability to update an affiliate's whitelist with new methods for managing affiliate relationships.
    • Added functionality to retrieve and manage affiliate whitelist data dynamically.
    • Enhanced affiliate management with the ability to filter revenue shares based on whitelist status.
    • Expanded internal operations to consider affiliate relationships during match processing.
    • Streamlined key management for affiliate-related data with new naming conventions.
    • Implemented new logic for determining affiliate tiers and fee shares based on whitelist status.
  • Bug Fixes

    • Improved error handling for duplicate affiliate addresses in the whitelist.
  • Tests

    • Expanded test coverage for affiliate management, including new test cases for whitelisted affiliates and revenue share retrieval.
  • Documentation

    • Updated error messages and comments for clarity regarding the new affiliate management features.

@affanv14 affanv14 requested a review from a team as a code owner September 16, 2024 13:00
Copy link

linear bot commented Sep 16, 2024

Copy link
Contributor

coderabbitai bot commented Sep 16, 2024

Walkthrough

The changes introduce enhancements to the affiliate management system across various components, including the addition of methods for updating affiliate whitelists, new message types, and modifications to existing functions. Key functionalities include the ability to manage affiliate relationships more dynamically, implement whitelists, and improve the handling of revenue shares based on affiliate status. The updates also include extensive testing to ensure the reliability of these new features.

Changes

File Path Change Summary
protocol/app/app.go Modified the New function to include app.AffiliatesKeeper.
protocol/app/msgs/all_msgs.go Added new message types related to affiliate management.
protocol/app/msgs/internal_msgs.go Introduced new message types for internal message handling related to affiliates.
protocol/app/msgs/internal_msgs_test.go Enhanced test coverage for new affiliate message types.
protocol/lib/ante/internal_msg.go Updated to recognize the new MsgUpdateAffiliateWhitelist as an internal message.
protocol/x/affiliates/keeper/grpc_query.go Modified AffiliateInfo and implemented AffiliateWhitelist function for better affiliate management.
protocol/x/clob/keeper/process_operations.go Integrated affiliatesWhitelistMap into internal operation processing methods.
protocol/x/revshare/keeper/revshare.go Updated functions to utilize the affiliate whitelist for revenue share management.
protocol/x/affiliates/keeper/msg_server.go Added UpdateAffiliateWhitelist method to handle whitelist updates based on authority checks.
protocol/x/affiliates/keeper/keeper_test.go Expanded tests for the new affiliate management functionalities.
protocol/x/clob/types/expected_keepers.go Introduced a new AffiliatesKeeper interface for retrieving affiliate whitelist information.
protocol/x/clob/types/mem_clob_keeper.go Updated ProcessSingleMatch method to accept an affiliate whitelist map.
protocol/x/revshare/keeper/revshare_test.go Enhanced tests for revenue shares based on affiliate whitelist status.

Possibly related PRs

Suggested labels

feature:indexer/affiliates

Suggested reviewers

  • teddyding

Poem

🐇 In the garden where affiliates play,
New whitelists bloom, brightening the day.
With each update, we hop with glee,
Managing partners, as happy as can be!
So let’s cheer for the changes, oh so grand,
Together we thrive, a united band! 🌼


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2fa8b55 and 1491ef0.

Files selected for processing (1)
  • protocol/x/clob/memclob/memclob.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/x/clob/memclob/memclob.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 generate interesting stats about this repository and render them as a table.
    -- @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: 2

Outside diff range and nitpick comments (2)
protocol/testutil/memclob/keeper.go (1)

330-330: Approve the addition of affiliatesWhitelistMap parameter.

The new affiliatesWhitelistMap parameter has been added to the ProcessSingleMatch function signature, which is a step towards implementing the affiliate whitelisting logic.

However, the parameter is currently unused within the function body. Consider utilizing it to filter or process matches based on the affiliate whitelist. For example:

// Check if the maker or taker order belongs to a whitelisted affiliate
if _, ok := affiliatesWhitelistMap[makerOrder.AffiliateAddress]; !ok {
    // Handle case when maker order is not from a whitelisted affiliate
}
if _, ok := affiliatesWhitelistMap[takerOrder.AffiliateAddress]; !ok {
    // Handle case when taker order is not from a whitelisted affiliate
}
protocol/app/app.go (1)

1124-1124: Consider defining an interface for the AffiliatesKeeper dependency.

Passing the concrete AffiliatesKeeper to the ClobKeeper creates a direct dependency between these modules. If possible, consider defining an interface that encapsulates just the functionality the ClobKeeper needs from the AffiliatesKeeper. This would decouple the modules and improve testability.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cbd23b1 and 4c61284.

Files ignored due to path filters (1)
  • protocol/x/affiliates/types/tx.pb.go is excluded by !**/*.pb.go
Files selected for processing (80)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/affiliates/tx.rpc.msg.ts (4 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/affiliates/tx.ts (3 hunks)
  • proto/dydxprotocol/affiliates/tx.proto (2 hunks)
  • protocol/app/app.go (1 hunks)
  • protocol/app/msgs/all_msgs.go (1 hunks)
  • protocol/app/msgs/internal_msgs.go (1 hunks)
  • protocol/app/msgs/internal_msgs_test.go (1 hunks)
  • protocol/lib/ante/internal_msg.go (1 hunks)
  • protocol/mocks/AnteDecorator.go (1 hunks)
  • protocol/mocks/AppOptions.go (1 hunks)
  • protocol/mocks/BankKeeper.go (1 hunks)
  • protocol/mocks/BridgeKeeper.go (1 hunks)
  • protocol/mocks/BridgeQueryClient.go (1 hunks)
  • protocol/mocks/BridgeServiceClient.go (1 hunks)
  • protocol/mocks/CacheMultiStore.go (1 hunks)
  • protocol/mocks/ClobKeeper.go (2 hunks)
  • protocol/mocks/Configurator.go (1 hunks)
  • protocol/mocks/DelayMsgKeeper.go (1 hunks)
  • protocol/mocks/EthClient.go (1 hunks)
  • protocol/mocks/ExchangeConfigUpdater.go (1 hunks)
  • protocol/mocks/ExchangeQueryHandler.go (1 hunks)
  • protocol/mocks/ExchangeToMarketPrices.go (1 hunks)
  • protocol/mocks/ExtendVoteHandler.go (1 hunks)
  • protocol/mocks/FileHandler.go (1 hunks)
  • protocol/mocks/GrpcClient.go (1 hunks)
  • protocol/mocks/GrpcServer.go (1 hunks)
  • protocol/mocks/HealthCheckable.go (1 hunks)
  • protocol/mocks/IndexerEventManager.go (1 hunks)
  • protocol/mocks/IndexerMessageSender.go (1 hunks)
  • protocol/mocks/Logger.go (1 hunks)
  • protocol/mocks/MarketPairFetcher.go (1 hunks)
  • protocol/mocks/MemClob.go (1 hunks)
  • protocol/mocks/MemClobKeeper.go (3 hunks)
  • protocol/mocks/MsgRouter.go (1 hunks)
  • protocol/mocks/MultiStore.go (1 hunks)
  • protocol/mocks/OracleClient.go (1 hunks)
  • protocol/mocks/PerpetualsClobKeeper.go (1 hunks)
  • protocol/mocks/PerpetualsKeeper.go (1 hunks)
  • protocol/mocks/PrepareBridgeKeeper.go (1 hunks)
  • protocol/mocks/PrepareClobKeeper.go (1 hunks)
  • protocol/mocks/PreparePerpetualsKeeper.go (1 hunks)
  • protocol/mocks/PriceFeedMutableMarketConfigs.go (1 hunks)
  • protocol/mocks/PriceFetcher.go (1 hunks)
  • protocol/mocks/PriceUpdateGenerator.go (1 hunks)
  • protocol/mocks/PricesKeeper.go (1 hunks)
  • protocol/mocks/ProcessBridgeKeeper.go (1 hunks)
  • protocol/mocks/ProcessClobKeeper.go (1 hunks)
  • protocol/mocks/ProcessPerpetualKeeper.go (1 hunks)
  • protocol/mocks/ProcessStakingKeeper.go (1 hunks)
  • protocol/mocks/QueryClient.go (1 hunks)
  • protocol/mocks/QueryServer.go (1 hunks)
  • protocol/mocks/RequestHandler.go (1 hunks)
  • protocol/mocks/SendingKeeper.go (1 hunks)
  • protocol/mocks/Server.go (1 hunks)
  • protocol/mocks/SubaccountsKeeper.go (3 hunks)
  • protocol/mocks/TimeProvider.go (1 hunks)
  • protocol/mocks/TxBuilder.go (1 hunks)
  • protocol/mocks/TxConfig.go (1 hunks)
  • protocol/mocks/UpdateMarketPriceTxDecoder.go (1 hunks)
  • protocol/mocks/VaultKeeper.go (1 hunks)
  • protocol/testutil/keeper/clob.go (6 hunks)
  • protocol/testutil/keeper/listing.go (1 hunks)
  • protocol/testutil/memclob/keeper.go (1 hunks)
  • protocol/x/affiliates/keeper/grpc_query.go (3 hunks)
  • protocol/x/affiliates/keeper/grpc_query_test.go (4 hunks)
  • protocol/x/affiliates/keeper/keeper.go (3 hunks)
  • protocol/x/affiliates/keeper/keeper_test.go (5 hunks)
  • protocol/x/affiliates/keeper/msg_server.go (1 hunks)
  • protocol/x/affiliates/keeper/msg_server_test.go (1 hunks)
  • protocol/x/affiliates/types/errors.go (1 hunks)
  • protocol/x/affiliates/types/keys.go (1 hunks)
  • protocol/x/clob/keeper/keeper.go (3 hunks)
  • protocol/x/clob/keeper/process_operations.go (6 hunks)
  • protocol/x/clob/keeper/process_single_match.go (4 hunks)
  • protocol/x/clob/memclob/memclob.go (1 hunks)
  • protocol/x/clob/types/clob_keeper.go (1 hunks)
  • protocol/x/clob/types/expected_keepers.go (1 hunks)
  • protocol/x/clob/types/mem_clob_keeper.go (1 hunks)
  • protocol/x/revshare/keeper/revshare.go (2 hunks)
  • protocol/x/revshare/keeper/revshare_test.go (3 hunks)
Files skipped from review due to trivial changes (49)
  • protocol/mocks/AnteDecorator.go
  • protocol/mocks/AppOptions.go
  • protocol/mocks/BankKeeper.go
  • protocol/mocks/BridgeKeeper.go
  • protocol/mocks/BridgeQueryClient.go
  • protocol/mocks/BridgeServiceClient.go
  • protocol/mocks/CacheMultiStore.go
  • protocol/mocks/Configurator.go
  • protocol/mocks/DelayMsgKeeper.go
  • protocol/mocks/EthClient.go
  • protocol/mocks/ExchangeConfigUpdater.go
  • protocol/mocks/ExchangeQueryHandler.go
  • protocol/mocks/ExchangeToMarketPrices.go
  • protocol/mocks/ExtendVoteHandler.go
  • protocol/mocks/FileHandler.go
  • protocol/mocks/GrpcClient.go
  • protocol/mocks/GrpcServer.go
  • protocol/mocks/HealthCheckable.go
  • protocol/mocks/IndexerEventManager.go
  • protocol/mocks/IndexerMessageSender.go
  • protocol/mocks/Logger.go
  • protocol/mocks/MarketPairFetcher.go
  • protocol/mocks/MemClob.go
  • protocol/mocks/MsgRouter.go
  • protocol/mocks/MultiStore.go
  • protocol/mocks/OracleClient.go
  • protocol/mocks/PerpetualsClobKeeper.go
  • protocol/mocks/PerpetualsKeeper.go
  • protocol/mocks/PrepareBridgeKeeper.go
  • protocol/mocks/PrepareClobKeeper.go
  • protocol/mocks/PreparePerpetualsKeeper.go
  • protocol/mocks/PriceFeedMutableMarketConfigs.go
  • protocol/mocks/PriceFetcher.go
  • protocol/mocks/PriceUpdateGenerator.go
  • protocol/mocks/PricesKeeper.go
  • protocol/mocks/ProcessBridgeKeeper.go
  • protocol/mocks/ProcessClobKeeper.go
  • protocol/mocks/ProcessPerpetualKeeper.go
  • protocol/mocks/ProcessStakingKeeper.go
  • protocol/mocks/QueryClient.go
  • protocol/mocks/QueryServer.go
  • protocol/mocks/RequestHandler.go
  • protocol/mocks/SendingKeeper.go
  • protocol/mocks/Server.go
  • protocol/mocks/TimeProvider.go
  • protocol/mocks/TxBuilder.go
  • protocol/mocks/TxConfig.go
  • protocol/mocks/UpdateMarketPriceTxDecoder.go
  • protocol/mocks/VaultKeeper.go
Additional context used
Biome
indexer/packages/v4-protos/src/codegen/dydxprotocol/affiliates/tx.ts

[error] 72-72: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)


[error] 75-75: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

Additional comments not posted (59)
protocol/x/affiliates/types/keys.go (1)

19-20: LGTM!

The introduction of the AffiliateWhitelistKey constant is a good addition. It follows the naming convention of the existing constants and is assigned an appropriate string value.

This constant lays the foundation for implementing affiliate whitelist functionality in the module.

protocol/x/affiliates/types/errors.go (2)

9-10: LGTM!

The new error declaration is syntactically correct, and the error message is clear and descriptive. The formatting change improves readability without altering the functionality.


15-16: Verify the error handling in the whitelisting logic.

The new error declaration for duplicate affiliate addresses in the whitelist is clear and consistent with the existing code. However, please ensure that this error is handled appropriately wherever the whitelisting logic is implemented.

Run the following script to verify the error handling:

Verification successful

Error handling for duplicate affiliate addresses is implemented correctly

The ErrDuplicateAffiliateAddressForWhitelist error is well-defined and properly handled in the codebase:

  • Defined in protocol/x/affiliates/types/errors.go
  • Used in test cases in protocol/x/affiliates/keeper/keeper_test.go
  • Correctly implemented in protocol/x/affiliates/keeper/keeper.go

The implementation efficiently checks for duplicate addresses using a map and returns the error with additional context when a duplicate is found. This approach ensures proper handling of the duplicate affiliate address scenario in the whitelist logic.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the handling of `ErrDuplicateAffiliateAddressForWhitelist` in the whitelisting logic.

# Test: Search for the error usage. Expect: Occurrences in the whitelisting logic with proper error handling.
rg --type go -A 5 $'ErrDuplicateAffiliateAddressForWhitelist'

Length of output: 1133

proto/dydxprotocol/affiliates/tx.proto (3)

19-21: LGTM!

The new RPC method UpdateAffiliateWhitelist is declared correctly in the Msg service. The method signature, input message, and output message are consistent with the existing code style and naming conventions.


51-58: Looks good!

The MsgUpdateAffiliateWhitelist message is defined correctly with appropriate fields and types. The authority field uses the cosmos.AddressString scalar type, and the whitelist field is marked as non-nullable. The message also includes the necessary Cosmos-specific option to indicate the required signer.


60-61: Approved.

The MsgUpdateAffiliateWhitelistResponse message is defined correctly as an empty message. This is acceptable since no specific response data is required for the UpdateAffiliateWhitelist RPC method.

indexer/packages/v4-protos/src/codegen/dydxprotocol/affiliates/tx.rpc.msg.ts (4)

3-3: LGTM!

The import statement correctly adds the new message types for updating affiliate whitelists, which aligns with the PR objective.


12-14: LGTM!

The new updateAffiliateWhitelist method is correctly added to the Msg interface with the appropriate request and response types, which aligns with the PR objective.


23-23: LGTM!

The updateAffiliateWhitelist method is correctly bound in the constructor of the MsgClientImpl class, which is necessary for its implementation.


38-43: LGTM!

The updateAffiliateWhitelist method is correctly implemented in the MsgClientImpl class, following the same pattern as the other methods. It properly encodes the request, sends the RPC request, and decodes the response.

protocol/x/affiliates/keeper/msg_server.go (1)

62-74: LGTM!

The UpdateAffiliateWhitelist function is implemented correctly and follows the expected behavior:

  • It checks for the caller's authority before performing the update, ensuring only authorized modifications.
  • It returns appropriate errors in case of failure, providing informative error messages.
  • It updates the whitelist by calling the keeper's SetAffiliateWhitelist method, ensuring the update is performed correctly.
  • It returns the expected response type MsgUpdateAffiliateWhitelistResponse upon successful execution.

The function enhances the functionality of the msgServer by allowing for dynamic updates to the affiliate whitelist while maintaining proper authorization checks.

protocol/x/clob/types/mem_clob_keeper.go (1)

24-24: LGTM! Consider the implementation details.

The addition of the affiliatesWhitelistMap parameter to the ProcessSingleMatch method is a good change that enhances the affiliate management capabilities of the system. This change enables more granular control over affiliate involvement in the matching process.

When implementing this change, ensure that:

  1. The affiliatesWhitelistMap is properly populated and maintained with the correct affiliate IDs and their corresponding whitelist status.
  2. The ProcessSingleMatch method implementation is updated to utilize the affiliatesWhitelistMap effectively, filtering or managing matches based on the affiliate whitelist status.
  3. The change is thoroughly tested to verify that the affiliate whitelisting logic works as intended and does not introduce any unintended side effects.

Overall, this is a positive change that aligns with the goal of improving affiliate management in the system.

protocol/x/affiliates/keeper/grpc_query.go (2)

25-40: LGTM! The new whitelist functionality enhances affiliate management.

The changes introduce a new way to manage affiliates using a whitelist, which takes precedence over the tier-based system. This allows for more flexibility in managing affiliate relationships and revenue shares.

The implementation is well-structured and does not introduce any apparent issues or bugs. The function first checks the affiliate whitelist map and retrieves the fee share ppm if the address is whitelisted. If not, it falls back to the original tier-based logic.

Overall, the changes improve the affiliate management system by providing a more dynamic and flexible approach.


92-101: Great job implementing the previously unimplemented function!

The changes complete the implementation of the AffiliateWhitelist function, which was previously marked as a TODO. The function now retrieves the affiliate whitelist using the GetAffiliateWhitelist method and returns it in the response.

The implementation is straightforward and does not introduce any apparent issues or bugs. This addition provides a concrete way to access the affiliate whitelist, enhancing the functionality of the RPC method.

protocol/x/clob/types/clob_keeper.go (1)

84-84: Approve the addition of the affiliatesWhitelistMap parameter to the ProcessSingleMatch method.

The inclusion of the affiliatesWhitelistMap parameter in the ProcessSingleMatch method signature is a reasonable change to incorporate affiliate whitelisting logic into the match processing workflow.

However, please ensure the following:

  1. The affiliatesWhitelistMap is correctly populated and maintained throughout the system. Verify that the map is updated whenever there are changes to the affiliate whitelist.

  2. The match processing logic within the ProcessSingleMatch method has been updated to handle the new affiliate whitelisting behavior correctly. Ensure that matches are processed according to the affiliate status of the involved parties.

  3. The performance impact of this change has been considered and deemed acceptable. Assess whether the addition of affiliate whitelisting logic significantly affects the execution time of the ProcessSingleMatch method, especially for large whitelists.

You can use the following script to verify the points mentioned above:

Please let me know if you have any further concerns or if you need assistance with the verification process.

protocol/x/clob/types/expected_keepers.go (1)

173-176: LGTM!

The introduction of the AffiliatesKeeper interface and the GetAffiliateWhitelistMap method aligns with the PR objectives and enhances the modularity and extensibility of the codebase. The method signature is clear and follows the naming convention of the codebase.

The changes provide a clear contract for affiliate-related operations and enable the retrieval of a structured representation of affiliates, which could be leveraged in other parts of the application for validation, access control, or business logic involving partnerships or revenue sharing.

protocol/lib/ante/internal_msg.go (1)

147-148: LGTM!

The addition of *affiliates.MsgUpdateAffiliateWhitelist to the list of internal message types in the IsInternalMsg function is consistent with the existing pattern and appears to be a valid change.

This will ensure that messages of type *affiliates.MsgUpdateAffiliateWhitelist are correctly identified as internal messages, which may affect their processing and handling within the application.

protocol/x/affiliates/keeper/msg_server_test.go (1)

136-182: LGTM!

The TestMsgServer_UpdateAffiliateWhitelist function is a well-structured and comprehensive test that covers the essential scenarios for updating the affiliate whitelist. It ensures that only authorized entities (i.e., the governance module) can modify the whitelist, while unauthorized attempts are correctly rejected.

The test cases are clearly defined, with appropriate setup and assertions to validate the expected behavior. This addition enhances the overall test coverage and reinforces the integrity of the affiliate management system.

protocol/testutil/keeper/listing.go (1)

152-155: LGTM!

The addition of affiliatesKeeper to the ListingKeepers function and passing it to the createClobKeeper function is consistent with the provided summary. This change enables the clobKeeper to interact with the affiliatesKeeper, potentially for managing affiliate-related data or operations.

The change is unlikely to have any negative impact on the existing functionality.

protocol/app/msgs/internal_msgs_test.go (1)

69-70: LGTM!

The added message paths expand the test coverage for affiliate-related functionalities, improving the robustness of the codebase. The changes are well-placed within the TestInternalMsgSamples_Gov_Key function and align with the existing code structure.

protocol/x/affiliates/keeper/grpc_query_test.go (2)

32-32: LGTM!

The changes to the TestAffiliateInfo function enhance the test coverage by including a new test case for a whitelisted affiliate. The test setup for the whitelisted case ensures that the system behaves as expected when an affiliate is part of the whitelist. Setting the IsWhitelisted field to false for existing test cases ensures that the system correctly identifies non-whitelisted affiliates.

Also applies to: 62-62, 94-131


228-249: LGTM!

The new TestAffiliateWhitelist function provides coverage for the SetAffiliateWhitelist and AffiliateWhitelist methods of the keeper package. It ensures that the system behaves as expected when setting and retrieving the affiliate whitelist.

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

153-161: LGTM!

The addition of the affiliatesWhitelistMap parameter to GetAllRevShares and the corresponding update to the getAffiliateRevShares function call enable more granular control over affiliate revenue sharing. This change allows for filtering affiliate revenue shares based on a predefined whitelist, providing better management of affiliate relationships and fee distributions.

The core logic of the function remains unchanged, minimizing the risk of introducing correctness issues or bugs.


194-203: Looks good!

The addition of the affiliatesWhitelistMap parameter to getAffiliateRevShares and the corresponding update to the GetTakerFeeShare function call from the affiliatesKeeper allow for more granular control over affiliate fee sharing. This change enables filtering of affiliate fee shares based on a predefined whitelist, providing better management of affiliate relationships and fee distributions.

The core logic of the function remains unchanged, minimizing the risk of introducing correctness issues or bugs.

protocol/testutil/keeper/clob.go (4)

20-20: LGTM!

The import statement for the affiliateskeeper package is necessary and looks good.


52-52: LGTM!

The AffiliatesKeeper field is correctly added to the ClobKeepersTestContext struct.


110-112: LGTM!

The AffiliatesKeeper and revShareKeeper are initialized correctly in the NewClobKeepersTestContextWithUninitializedMemStore function.


189-189: LGTM!

The createClobKeeper function is correctly modified to include the affiliatesKeeper parameter and pass it to the NewKeeper function.

Also applies to: 228-228, 260-260

indexer/packages/v4-protos/src/codegen/dydxprotocol/affiliates/tx.ts (2)

52-60: LGTM!

The MsgUpdateAffiliateWhitelist interface is well-defined and follows the expected structure for a message type. The authority and optional whitelist properties are appropriately typed.


61-69: LGTM!

The MsgUpdateAffiliateWhitelistSDKType interface correctly mirrors the structure of MsgUpdateAffiliateWhitelist and uses the appropriate SDK type for the whitelist property.

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

Line range hint 164-184: LGTM!

The changes to the GetTakerFeeShare function look good. The addition of the affiliatesWhitelistMap parameter and the logic to override the fee share for whitelisted affiliates is implemented correctly.


270-282: Looks good!

The new GetAffiliateWhitelistMap function is a useful addition. It correctly retrieves the affiliate whitelist and constructs a map of addresses to their fee shares, which can be used for efficient lookups.


284-299: Nice work!

The new SetAffiliateWhitelist function is implemented well. It correctly checks for duplicate addresses in the provided whitelist and returns an appropriate error if any are found. The marshaling and storing of the whitelist in the key-value store is handled properly.


301-315: Excellent!

The new GetAffiliateWhitelist function is implemented correctly. It properly retrieves the affiliate whitelist from the key-value store and handles the case when no whitelist exists by returning an empty whitelist. The unmarshaling of the whitelist and error propagation is handled appropriately.

protocol/x/clob/keeper/keeper.go (2)

45-45: LGTM!

The addition of the affiliatesKeeper field to the Keeper struct is a valid change that aligns with the summary. This change introduces a new dependency on the affiliates module, allowing the Keeper to manage or interact with affiliate-related data or operations.


89-89: LGTM!

The addition of the affiliatesKeeper parameter to the NewKeeper function and its assignment to the corresponding field in the Keeper struct are valid changes that align with the summary. These changes ensure that the Keeper is properly initialized with the affiliatesKeeper dependency, allowing it to be used within the keeper's methods for managing or interacting with affiliate-related data or operations.

Also applies to: 114-114

protocol/app/msgs/internal_msgs.go (1)

111-114: LGTM!

The addition of the new message types MsgUpdateAffiliateWhitelist and MsgUpdateAffiliateWhitelistResponse to the InternalMsgSamplesDydxCustom map looks good. This change enhances the affiliate management functionality by allowing updates to an affiliate whitelist.

The code follows the existing conventions and is consistent with the AI-generated summary.

protocol/mocks/SubaccountsKeeper.go (2)

190-205: LGTM!

The mock function GetStreamSubaccountUpdate is implemented correctly. It properly handles the case when no return value is specified by panicking with a descriptive message. The function returns the predefined value if specified, otherwise it panics as expected.


254-256: LGTM!

The mock function SendFinalizedSubaccountUpdates is implemented correctly. It properly registers the call using the Called method with the provided arguments. The function doesn't return any value, which is expected for a function that sends updates.

protocol/mocks/MemClobKeeper.go (1)

Line range hint 323-362: LGTM!

The changes to the ProcessSingleMatch method are consistent with the mock's purpose of simulating the behavior of the actual implementation. The method signature has been updated to include a new parameter affiliatesWhitelistMap of type map[string]uint32, and the method body has been updated to pass the new parameter to the mock's Called method and retrieve the return values using the new parameter.

protocol/app/msgs/all_msgs.go (1)

152-157: LGTM! The addition of new message types enhances the affiliate management functionality.

The introduction of MsgUpdateAffiliateWhitelist and MsgUpdateAffiliateWhitelistResponse message types suggests an expansion of the affiliate management capabilities. These new message types likely enable the updating of affiliate whitelists, providing more granular control over affiliate participation and compliance.

The existing message types for registering affiliates and updating affiliate tiers remain unchanged, indicating that the new whitelist functionality is a supplementary feature rather than a modification of the core affiliate management flow.

Overall, these changes reflect a well-structured enhancement of the affiliate management system.

protocol/x/affiliates/keeper/keeper_test.go (7)

396-464: LGTM!

The test cases for SetAffiliateWhitelist are comprehensive and cover various scenarios, including single tier with single address, multiple tiers with multiple addresses, and duplicate addresses across tiers. The test cases ensure that the function behaves as expected and handles both valid and invalid whitelist configurations correctly.


466-540: LGTM!

The test cases for GetAffiliateWhiteListMap are comprehensive and cover various scenarios, including multiple tiers with multiple addresses, single tier with single address, empty tiers, and nil whitelist. The test cases ensure that the function correctly converts the affiliate whitelist to a map format and handles different whitelist configurations as expected.


542-617: LGTM!

The test cases for GetTakerFeeShareViaWhitelist are comprehensive and cover various scenarios, including affiliate in whitelist, affiliate not in whitelist, and referee not registered. The test cases ensure that the GetTakerFeeShare function correctly retrieves the taker fee share based on the affiliate whitelist and handles different scenarios as expected.


188-188: Verify the usage of the additional parameter in the codebase.

The changes look good. However, ensure that the additional parameter passed to GetTakerFeeShare is being used correctly and consistently across the codebase.

Run the following script to verify the usage of the additional parameter:

Verification successful

Verification successful: Additional parameter usage is consistent

The additional parameter affiliatesWhitelistMap is being used correctly and consistently across the codebase. All calls to GetTakerFeeShare include this parameter, and the implementation in the keeper uses it as expected.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the additional parameter in `GetTakerFeeShare` calls.

# Test: Search for `GetTakerFeeShare` calls. Expect: Consistent usage of the additional parameter.
rg --type go -A 5 $'GetTakerFeeShare'

Length of output: 6391


175-175: Verify the usage of the additional parameter in the codebase.

The changes look good. However, ensure that the additional parameter passed to GetTakerFeeShare is being used correctly and consistently across the codebase.

Run the following script to verify the usage of the additional parameter:

Verification successful

Additional parameter usage verified and consistent

The affiliatesWhitelistMap parameter is consistently used in the GetTakerFeeShare function definition and all calls to the function throughout the codebase. In production code, it's passed as expected, while in test files, an empty map is used, which is a valid scenario. This consistent usage indicates that the changes are intentional and correctly implemented.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the additional parameter in `GetTakerFeeShare` calls.

# Test: Search for `GetTakerFeeShare` calls. Expect: Consistent usage of the additional parameter.
rg --type go -A 5 $'GetTakerFeeShare'

Length of output: 6391


224-224: Verify the usage of the additional parameter in the codebase.

The changes look good. However, ensure that the additional parameter passed to GetTakerFeeShare is being used correctly and consistently across the codebase.

Run the following script to verify the usage of the additional parameter:

Verification successful

Approve changes: Additional parameter consistently used

The additional affiliatesWhitelistMap parameter in GetTakerFeeShare is consistently used across the codebase. It's properly implemented in the keeper and utilized in production code (revshare.go). In test files, an empty map is passed, which is valid but might not fully test the whitelist functionality. Consider adding tests with non-empty whitelist maps to ensure comprehensive coverage.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the additional parameter in `GetTakerFeeShare` calls.

# Test: Search for `GetTakerFeeShare` calls. Expect: Consistent usage of the additional parameter.
rg --type go -A 5 $'GetTakerFeeShare'

Length of output: 6391


242-242: Verify the usage of the additional parameter in the codebase.

The changes look good. However, ensure that the additional parameter passed to GetTakerFeeShare is being used correctly and consistently across the codebase.

Run the following script to verify the usage of the additional parameter:

Verification successful

The additional parameter is used correctly and consistently across the codebase.

The GetTakerFeeShare function is called with the new affiliatesWhitelistMap parameter in all relevant locations:

  • In protocol/x/revshare/keeper/revshare.go
  • In multiple test cases in protocol/x/affiliates/keeper/keeper_test.go

The usage is consistent with the function signature defined in protocol/x/affiliates/keeper/keeper.go.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the additional parameter in `GetTakerFeeShare` calls.

# Test: Search for `GetTakerFeeShare` calls. Expect: Consistent usage of the additional parameter.
rg --type go -A 5 $'GetTakerFeeShare'

Length of output: 6391

protocol/x/clob/keeper/process_single_match.go (2)

44-44: LGTM! Consider implementing the affiliate whitelist logic.

The addition of the affiliatesWhitelistMap parameter is a preparatory change for implementing the affiliate whitelist logic. The parameter is not used within the function, indicating that the whitelist logic is not yet implemented.

Consider implementing the whitelist logic in a future PR to complete the feature.


309-309: LGTM! Implement the affiliate whitelist logic in the getAllRevShares function.

The addition of the affiliatesWhitelistMap parameter is a preparatory change for implementing the affiliate whitelist logic. The parameter is not used within the function, indicating that the whitelist logic is not yet implemented.

The TODO comment suggests that the whitelist map should be used in the getAllRevShares function. Consider implementing the whitelist logic in the getAllRevShares function to complete the feature.

Also applies to: 441-442

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

556-606: LGTM!

The new test case is well-structured and follows the existing pattern. It correctly sets up the necessary parameters for market mappers, affiliates, and affiliate whitelists. The expected revenue shares are properly defined based on the whitelisted affiliate's share and the market mapper's share. The test case verifies that the returned revenue shares match the expected values.


638-641: LGTM!

The changes to retrieve the affiliate whitelist map and pass it to the GetAllRevShares function are necessary and consistent with the new test case and the overall functionality of the revenue share system. Retrieving the affiliate whitelist map from the affiliates keeper ensures that the latest whitelist information is used when calculating the revenue shares.

protocol/x/clob/keeper/process_operations.go (4)

167-173: LGTM!

The code segment correctly retrieves the affiliates whitelist map from the affiliates keeper and handles the error case appropriately.


175-175: LGTM!

The PersistMatchToState function call has been updated to include the affiliatesWhitelistMap argument, which is consistent with the function signature change.


231-243: LGTM!

The PersistMatchToState function signature has been updated to include the affiliatesWhitelistMap parameter, which is then passed to the respective functions for processing match orders and liquidations. The changes are consistent and ensure the availability of the affiliatesWhitelistMap in the downstream functions.


474-474: LGTM!

The PersistMatchOrdersToState and PersistMatchLiquidationToState functions have been updated to include the affiliatesWhitelistMap parameter, which is then passed to the ProcessSingleMatch function for each match fill. The changes are consistent and ensure the availability of the affiliatesWhitelistMap in the ProcessSingleMatch function for processing individual match fills.

Also applies to: 519-519, 590-631

protocol/mocks/ClobKeeper.go (1)

Line range hint 1002-1040: Approve the mock function signature update.

The addition of the affiliatesWhitelistMap parameter to the ProcessSingleMatch mock function signature looks good. The parameter is correctly propagated to the underlying mock call.

Verify that the actual implementation of ProcessSingleMatch correctly handles the affiliatesWhitelistMap parameter and applies the affiliates whitelist logic as intended. Pay close attention to how the whitelist is used during match processing and ensure it aligns with the expected behavior.

You can use the following script to search for the ProcessSingleMatch function in the codebase:

protocol/app/app.go (1)

1124-1124: Integration of AffiliatesKeeper looks good.

The AffiliatesKeeper is now being passed to the ClobKeeper, allowing the ClobKeeper to interact with the affiliates module as needed.

protocol/x/clob/memclob/memclob.go (1)

1699-1701: LGTM! The new parameter enables affiliate whitelisting logic in order matching.

The introduction of the affiliatesWhitelistMap parameter, initialized as an empty map, suggests an enhancement to handle affiliate-related logic during the order matching process. By passing this map to the ProcessSingleMatch method, it allows the utilization of the affiliates whitelist data within the matching logic.

The map type map[string]uint32 indicates that the keys represent affiliate identifiers, while the values hold some numeric metadata associated with each affiliate.

This change expands the method signature to accommodate additional functionality without altering the existing control flow or error handling.

Comment on lines +73 to +75
/** Response to MsgUpdateAffiliateWhitelist */

export interface MsgUpdateAffiliateWhitelistResponseSDKType {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using a type alias instead of an empty interface.

Similar to the MsgUpdateAffiliateWhitelistResponse interface, the MsgUpdateAffiliateWhitelistResponseSDKType interface is empty. Consider using a type alias instead to improve code clarity and maintainability.

-export interface MsgUpdateAffiliateWhitelistResponseSDKType {}
+export type MsgUpdateAffiliateWhitelistResponseSDKType = Record<string, never>;
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
/** Response to MsgUpdateAffiliateWhitelist */
export interface MsgUpdateAffiliateWhitelistResponseSDKType {}
/** Response to MsgUpdateAffiliateWhitelist */
export type MsgUpdateAffiliateWhitelistResponseSDKType = Record<string, never>;
Tools
Biome

[error] 75-75: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

Comment on lines +70 to +72
/** Response to MsgUpdateAffiliateWhitelist */

export interface MsgUpdateAffiliateWhitelistResponse {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using a type alias instead of an empty interface.

The MsgUpdateAffiliateWhitelistResponse interface is empty, which is equivalent to {}. To improve code clarity and maintainability, consider using a type alias instead.

-export interface MsgUpdateAffiliateWhitelistResponse {}
+export type MsgUpdateAffiliateWhitelistResponse = Record<string, never>;
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
/** Response to MsgUpdateAffiliateWhitelist */
export interface MsgUpdateAffiliateWhitelistResponse {}
/** Response to MsgUpdateAffiliateWhitelist */
export type MsgUpdateAffiliateWhitelistResponse = Record<string, never>;
Tools
Biome

[error] 72-72: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

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/affiliates/keeper/keeper_test.go (1)

176-176: Add a comment to clarify the purpose of the empty map argument.

The empty map passed as the third argument to GetTakerFeeShare is not immediately clear without additional context. Consider adding a comment to explain that it represents an affiliate whitelist or any other relevant information.

Also applies to: 189-189

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4c61284 and e0a6f72.

Files selected for processing (24)
  • protocol/app/app.go (1 hunks)
  • protocol/app/msgs/all_msgs.go (1 hunks)
  • protocol/app/msgs/internal_msgs.go (1 hunks)
  • protocol/app/msgs/internal_msgs_test.go (1 hunks)
  • protocol/lib/ante/internal_msg.go (1 hunks)
  • protocol/mocks/ClobKeeper.go (2 hunks)
  • protocol/mocks/MemClobKeeper.go (2 hunks)
  • protocol/testutil/keeper/clob.go (7 hunks)
  • protocol/testutil/keeper/listing.go (1 hunks)
  • protocol/testutil/memclob/keeper.go (1 hunks)
  • protocol/x/affiliates/keeper/grpc_query.go (3 hunks)
  • protocol/x/affiliates/keeper/grpc_query_test.go (4 hunks)
  • protocol/x/affiliates/keeper/keeper.go (3 hunks)
  • protocol/x/affiliates/keeper/keeper_test.go (5 hunks)
  • protocol/x/clob/keeper/keeper.go (3 hunks)
  • protocol/x/clob/keeper/process_operations.go (6 hunks)
  • protocol/x/clob/keeper/process_single_match.go (4 hunks)
  • protocol/x/clob/memclob/memclob.go (1 hunks)
  • protocol/x/clob/types/clob_keeper.go (1 hunks)
  • protocol/x/clob/types/expected_keepers.go (1 hunks)
  • protocol/x/clob/types/mem_clob_keeper.go (1 hunks)
  • protocol/x/revshare/keeper/revshare.go (3 hunks)
  • protocol/x/revshare/keeper/revshare_test.go (3 hunks)
  • protocol/x/subaccounts/keeper/transfer_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (19)
  • protocol/app/app.go
  • protocol/app/msgs/all_msgs.go
  • protocol/app/msgs/internal_msgs.go
  • protocol/app/msgs/internal_msgs_test.go
  • protocol/lib/ante/internal_msg.go
  • protocol/mocks/ClobKeeper.go
  • protocol/mocks/MemClobKeeper.go
  • protocol/testutil/keeper/clob.go
  • protocol/testutil/keeper/listing.go
  • protocol/testutil/memclob/keeper.go
  • protocol/x/affiliates/keeper/keeper.go
  • protocol/x/clob/keeper/keeper.go
  • protocol/x/clob/keeper/process_operations.go
  • protocol/x/clob/keeper/process_single_match.go
  • protocol/x/clob/memclob/memclob.go
  • protocol/x/clob/types/clob_keeper.go
  • protocol/x/clob/types/mem_clob_keeper.go
  • protocol/x/revshare/keeper/revshare.go
  • protocol/x/revshare/keeper/revshare_test.go
Additional comments not posted (13)
protocol/x/affiliates/keeper/grpc_query.go (2)

25-40: Verify the impact on the affiliate revenue sharing logic.

The changes introduce a new way to manage affiliate fee shares through a whitelist, which takes precedence over the tier-based system. This allows for more flexibility in managing affiliates.

Please ensure that the overall affiliate revenue sharing logic is thoroughly tested, considering the following scenarios:

  • Affiliates who are on the whitelist
  • Affiliates who are not on the whitelist but have a tier level
  • Affiliates who are neither on the whitelist nor have a tier level

88-97: LGTM, but note the breaking change in the response type.

The AffiliateWhitelist function has been implemented to retrieve the affiliate whitelist from the context and return it in the response. The implementation looks good.

However, please note that the response type has been updated to include the whitelist, which is a breaking change for any clients using this RPC method. Ensure that the relevant documentation and clients are updated accordingly.

protocol/x/clob/types/expected_keepers.go (2)

184-186: LGTM!

The addition of the AffiliatesKeeper interface and the GetAffiliateWhitelistMap method is a great way to enhance the affiliate management functionality in a modular and extensible manner. This will enable better integration of affiliate whitelisting across the application.


178-178: Update implementations and callers of GetAllRevShares.

The change to the GetAllRevShares method signature is a good enhancement for integrating affiliate management with revenue sharing. However, please ensure that:

  1. All implementations of the RevShareKeeper interface are updated to handle the new affiliateWhitelistMap parameter.
  2. All callers of the GetAllRevShares method are updated to provide the affiliateWhitelistMap argument.

Run the following script to verify the updates:

Verification successful

Verification successful: GetAllRevShares updates are complete

The implementation of GetAllRevShares and its callers have been correctly updated to include the new affiliateWhitelistMap parameter. This change has been consistently applied across the codebase, ensuring compatibility with the modified interface.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify implementations and callers of `GetAllRevShares` have been updated.

# Test 1: Search for implementations of `RevShareKeeper` interface. Expect: Updated method signature.
rg --type go -A 5 $'func \(.*RevShareKeeper\) GetAllRevShares\('

# Test 2: Search for callers of `GetAllRevShares` method. Expect: Updated method invocation with `affiliateWhitelistMap` argument.
rg --type go -A 5 $'GetAllRevShares\('

Length of output: 2852

protocol/x/affiliates/keeper/grpc_query_test.go (4)

32-32: LGTM!

Setting IsWhitelisted to false is correct since the affiliate is not added to the whitelist in this test case.


62-62: LGTM!

Setting IsWhitelisted to false is correct since no affiliate relationship is registered in this test case.


94-131: LGTM!

The "Whitelisted" test case correctly sets up a whitelisted affiliate and verifies the expected response:

  • Registers an affiliate relationship between Alice and Bob.
  • Sets a delegation for Alice.
  • Adds Alice to the affiliate whitelist with a custom FeeSharePpm of 120,000 (12%).
  • Expects the response to include the custom whitelist values and IsWhitelisted set to true.

The test case looks good and thoroughly verifies the behavior for a whitelisted affiliate.


228-249: LGTM!

The new TestAffiliateWhitelist function correctly verifies setting and retrieving the affiliate whitelist:

  • Sets up an affiliate whitelist with Alice's address and a custom TakerFeeSharePpm of 1,000,000.
  • Calls the AffiliateWhitelist method and verifies the response matches the set whitelist.

The test function looks good and thoroughly verifies the behavior of setting and retrieving the affiliate whitelist.

protocol/x/affiliates/keeper/keeper_test.go (4)

397-465: LGTM!

The TestSetAffiliateWhitelist function comprehensively tests the SetAffiliateWhitelist method with various scenarios, ensuring its correctness and handling of edge cases.


467-541: LGTM!

The TestGetAffiliateWhiteListMap function thoroughly tests the GetAffiliateWhitelistMap method with different whitelist configurations, ensuring that it returns the expected map representation of the affiliate whitelist.


543-618: LGTM!

The TestGetTakerFeeShareViaWhitelist function effectively tests the GetTakerFeeShare method when using an affiliate whitelist. It covers important scenarios and ensures that the method correctly determines the taker fee share based on the whitelist and falls back to the default tiers when necessary.


Line range hint 619-719: LGTM!

The TestAggregateAffiliateReferredVolumeForFills function thoroughly tests the AggregateAffiliateReferredVolumeForFills method with different referral scenarios. It ensures that the method correctly aggregates the referred volume for affiliates based on the fill data.

protocol/x/subaccounts/keeper/transfer_test.go (1)

1645-1647: Verify the GetAllRevShares method has been updated to handle the new affiliateWhitelistMap parameter correctly.

The change looks good. It retrieves the affiliateWhitelistMap from the affiliatesKeeper and passes it as an additional parameter to the GetAllRevShares method.

Ensure that the GetAllRevShares method has been updated to:

  1. Accept the new affiliateWhitelistMap parameter.
  2. Use the affiliateWhitelistMap to correctly determine the revenue shares based on affiliate relationships.

You can use the following script to verify the GetAllRevShares method:

Verification successful

GetAllRevShares method has been correctly updated to handle the new affiliateWhitelistMap parameter

The verification process has confirmed that the GetAllRevShares method in protocol/x/revshare/keeper/revshare.go has been successfully updated to incorporate the affiliateWhitelistMap parameter. Key observations:

  1. The method signature now includes affiliatesWhitelistMap map[string]uint32 as expected.
  2. The new parameter is utilized in the getAffiliateRevShares function, indicating that affiliate revenue shares are calculated based on this whitelist.
  3. The affiliate rev shares are properly integrated into the overall revenue share calculation for the fill.

These changes align with the intended purpose of using the affiliate whitelist to determine revenue shares. The implementation appears to be correct and consistent with the expected behavior.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the `GetAllRevShares` method has been updated to handle the new `affiliateWhitelistMap` parameter.

# Test 1: Check if the method signature has been updated to accept the new parameter.
# Expect: The method should accept an `affiliateWhitelistMap map[string]bool` parameter.
ast-grep --lang go --pattern $'func GetAllRevShares(ctx sdk.Context, fill Fill, affiliateWhitelistMap map[string]bool) (RevShares, error)'

# Test 2: Check if the method uses the `affiliateWhitelistMap` to determine revenue shares.
# Expect: The method should reference the `affiliateWhitelistMap` when calculating shares.
rg --type go -A 5 $'affiliateWhitelistMap'

Length of output: 4633


Script:

#!/bin/bash
# Description: Locate the implementation of GetAllRevShares and its usage of affiliateWhitelistMap

# Search for GetAllRevShares implementation
echo "Searching for GetAllRevShares implementation:"
ast-grep --lang go --pattern 'func ($_) GetAllRevShares($_) {
  $$$
}'

# Look for usages of affiliateWhitelistMap within the revshare module
echo "Searching for affiliateWhitelistMap usage in revshare module:"
rg --type go -g 'protocol/x/revshare/**' 'affiliateWhitelistMap'

Length of output: 627


Script:

#!/bin/bash
# Description: Further investigation to locate GetAllRevShares implementation

# Search for interface defining GetAllRevShares
echo "Searching for interface with GetAllRevShares:"
ast-grep --lang go --pattern 'type $_ interface {
  $$$
  GetAllRevShares($_)
  $$$
}'

# Look for potential RevShareKeeper implementations
echo "Searching for potential RevShareKeeper implementations:"
rg --type go 'type Keeper struct'

# Broader search for GetAllRevShares
echo "Broader search for GetAllRevShares:"
rg --type go 'func.*GetAllRevShares'

Length of output: 797


Script:

#!/bin/bash
# Description: Inspect the implementation of GetAllRevShares in revshare.go

echo "Content of GetAllRevShares function in revshare.go:"
sed -n '/func (k Keeper) GetAllRevShares/,/^}/p' protocol/x/revshare/keeper/revshare.go

Length of output: 2681

@@ -16,4 +16,6 @@ const (
ReferredVolumeKeyPrefix = "ReferredVolume:"

AffiliateTiersKey = "AffiliateTiers"

AffiliateWhitelistKey = "AffiliateWhitelist"
Copy link
Contributor

@teddyding teddyding Sep 17, 2024

Choose a reason for hiding this comment

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

Let's actually optimize the keys and prefixes a bit (for faster insert/read from IAVL tree)

	ReferredByKeyPrefix = "RB:"
	ReferredVolumeKeyPrefix = "RV:"

	AffiliateTiersKey = "AT"

	AffiliateWhitelistKey = "AW"

@@ -307,6 +309,7 @@ func (k Keeper) persistMatchedOrders(
makerFeePpm int32,
bigFillQuoteQuantums *big.Int,
insuranceFundDelta *big.Int,
affiliatesWhitelistMap map[string]uint32, // nolint: unused
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the nolint? Isn't affiliatesWhitelistMapused below?

In general nolint in non-testing files is red flag

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was a remnant from before the clob pr was merged. Should be removed

@@ -144,8 +144,15 @@ func (k Keeper) ProcessInternalOperations(

switch castedOperation := operation.Operation.(type) {
case *types.InternalOperation_Match:
affiliatesWhitelistMap, err := k.affiliatesKeeper.GetAffiliateWhitelistMap(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

Declare affiliatesWhitelistMap before the for loop, and query it at most once for all matches in the operations queue.

func (k Keeper) SetAffiliateWhitelist(ctx sdk.Context, whitelist types.AffiliateWhitelist) error {
store := ctx.KVStore(k.storeKey)
addressSet := make(map[string]bool)
for _, tier := range whitelist.Tiers {
Copy link
Contributor

Choose a reason for hiding this comment

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

New change: let's also add a protocol constant = 50% as upper bound of the fee share ppm, for fee security

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it would be better to add this to ValidateRevShareSafety

Now that I think about it, Im gonna make ValidateRevShareSafety more accurate - can discuss offline

Copy link
Contributor

@teddyding teddyding Sep 18, 2024

Choose a reason for hiding this comment

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

To clarify, we explicitly want a constant Cap = 50% for the affiliate whitelist fee share (doesn't hurt to apply this to regular affiliate tier as well) - don't see being added?

success, takerUpdateResult, makerUpdateResult, _, err := m.clobKeeper.ProcessSingleMatch(ctx, &matchWithOrders)
affiliatesWhitelistMap := make(map[string]uint32)
success, takerUpdateResult, makerUpdateResult, _, err := m.clobKeeper.ProcessSingleMatch(
ctx, &matchWithOrders, affiliatesWhitelistMap)
Copy link
Contributor

Choose a reason for hiding this comment

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

Explicitly pass in an empty map, and document why the input is empty.

}
}

func TestGetTakerFeeShareViaWhitelist(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a unit test where the affiliate qualifies through regular affiliate program and gets overridden to the higher whitelist fee share?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the first unit test here registers an affiliate(which should have revshare of 15%) and we bump it to 40%

@@ -136,6 +136,13 @@ func (k Keeper) ProcessInternalOperations(
// All short term orders in this map have passed validation.
placedShortTermOrders := make(map[types.OrderId]types.Order, 0)

affiliatesWhitelistMap, err := k.affiliatesKeeper.GetAffiliateWhitelistMap(ctx)
Copy link
Contributor

@teddyding teddyding Sep 18, 2024

Choose a reason for hiding this comment

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

Can you do it in a way that make this state read at most once when there are matches, but zero if there's no match: declare affiliatesWhitelistMap outside of the scope of for loop, but assign to it only if it's not been initialized, under case *types.InternalOperation_Match:

This may look a bit clumsy, so could you add comment where applicable?

success, takerUpdateResult, makerUpdateResult, _, err := m.clobKeeper.ProcessSingleMatch(
ctx, &matchWithOrders, affiliatesWhitelistMap)
ctx, &matchWithOrders, make(map[string]uint32))
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: pass in literal for readability

Suggested change
ctx, &matchWithOrders, make(map[string]uint32))
ctx, &matchWithOrders, map[string]uint32{})

// check if affiliate whitelist map is nil and initialize it if it is.
// This is done to avoid getting whitelist map on list of operations
// where there are no matches.
if affiliatesWhitelistMap == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

If the AffiliateWhitelist is empty in state, then this will be nil everytime right? Do we need a separate boolean to track whether the state read was done?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

golang dfiferentiates between a nil map and an empty map

Copy link
Contributor Author

@affanv14 affanv14 Sep 18, 2024

Choose a reason for hiding this comment

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

package main

import "fmt"

func main() {
	x := map[string]uint32{}
	if x == nil {
		fmt.Println("x is nil")
	}
	var y map[string]uint32
	if y == nil {
		fmt.Println("y is nil")
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

try this piece of code(confirmed it myself)

@@ -1698,8 +1698,12 @@ func (m *MemClobPriceTimePriority) mustPerformTakerOrderMatching(

// affiliate revshare doesnt affect subaccount balance, we can pass an empty map
Copy link
Contributor

@teddyding teddyding Sep 18, 2024

Choose a reason for hiding this comment

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

Let's use below comment:

Pass in empty map to avoid reading `AffiliateWhitelist` from state in every `CheckTx`. This deviates from `DeliverTx` which accounts for affiliate whitelist correctly. 
This deviation is ok becasue rev shares/fees are distributed to the recipient’s bank balance and not settled at the subaccount level, and won’t affect the collateralization of future operations in the operations queue.

@affanv14 affanv14 merged commit 728d53a into main Sep 19, 2024
39 checks passed
@affanv14 affanv14 deleted the affan/affiliate-whitelist branch September 19, 2024 18:02
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.

2 participants