-
Notifications
You must be signed in to change notification settings - Fork 101
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-816] Update Revshare logic for affiliates #2298
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe changes enhance the revenue sharing functionality by modifying the Changes
Possibly related PRs
Suggested labels
Poem
Recent review detailsConfiguration used: CodeRabbit UI Files selected for processing (6)
Files skipped from review as they are similar to previous changes (1)
Additional comments not posted (11)
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
@@ -856,52 +856,6 @@ func TestKeeper_GetAllRevShares_Invalid(t *testing.T) { | |||
require.NoError(t, err) | |||
}, | |||
}, | |||
{ | |||
name: "Total fees shared exceeds net fees from market mapper and affiliates", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests wont work anymore
There was a problem hiding this 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/x/revshare/keeper/revshare.go (2)
Line range hint
227-251
: Update unit tests to cover changes ingetAffiliateRevShares
The function
getAffiliateRevShares
now returns an additional*big.Int
representingaffiliateFeesShared
. Ensure that unit tests are updated or added to cover this new return value and to verify the correctness ofaffiliateFeesShared
in various scenarios.
227-227
: Document the new return value ingetAffiliateRevShares
Update the function comment for
getAffiliateRevShares
to include information about the new*big.Int
return valueaffiliateFeesShared
. This helps maintain clarity for future maintainers.
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- protocol/x/revshare/keeper/revshare.go (3 hunks)
- protocol/x/revshare/keeper/revshare_test.go (8 hunks)
- protocol/x/subaccounts/keeper/transfer_test.go (1 hunks)
Additional comments not posted (5)
protocol/x/revshare/keeper/revshare_test.go (2)
Line range hint
337-364
: LGTM!The changes in the test cases consistently reduce the expected
QuoteQuantums
for various recipients and fee sources. This suggests a deliberate adjustment in the revenue share calculation, potentially to align with updated business requirements or to optimize the distribution of fees among the participants.The reductions appear to be proportional across the test cases, maintaining the overall integrity of the revenue share logic. The updated values should be verified against the intended revenue share model to ensure accuracy.
Line range hint
848-860
: Verify the reason for removing the test cases.The test cases that specifically checked for exceeding net fees from market mappers and affiliates have been removed. This suggests a change in the validation logic or requirements related to these entities.
Please confirm the following:
- Is the validation for exceeding net fees from market mappers and affiliates still necessary? If not, please provide the rationale for removing these test cases.
- Are there any alternative mechanisms in place to validate the fee distribution for market mappers and affiliates?
Ensuring comprehensive test coverage is crucial for maintaining the integrity of the revenue share system. If the removed test cases are no longer relevant, please consider adding comments to document the reasons for their removal.
protocol/x/subaccounts/keeper/transfer_test.go (1)
1503-1506
: LGTM!The updated expected balances for the fee module account, market mapper account, affiliate account, and unconditional rev share account are calculated correctly based on the respective revenue share percentages.
protocol/x/revshare/keeper/revshare.go (2)
184-184
: EnsuregetMarketMapperRevShare
handles updated net fees correctlyPassing the adjusted
netFeesSubAffiliateFeesShared
togetMarketMapperRevShare
is appropriate. Verify thatgetMarketMapperRevShare
correctly handles cases where the net fees might be reduced due to affiliate fee sharing.
Line range hint
227-251
: Ensure all callers handle the new return value fromgetAffiliateRevShares
With the addition of the new return value
*big.Int
, verify that all callers ofgetAffiliateRevShares
have been updated to handle the extra return parameter to prevent potential runtime errors.Run the following script to identify all usages of
getAffiliateRevShares
:Verification successful
To ensure a comprehensive verification, we need to perform two additional checks. First, let's search for any other potential usages of the function, and then we'll examine the context around the function call we found.
Changes to
getAffiliateRevShares
have been correctly implementedThe verification process has confirmed that the new return value from
getAffiliateRevShares
is being properly handled in its only usage within the codebase. The function call correctly assigns three variables, including the newaffiliateFeesShared
return value, which is then used in subsequent calculations. No other calls to this function were found in the codebase, so no further updates are necessary.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Find all usages of `getAffiliateRevShares` to ensure they are handling the new return value. # Search for the function calls in the Go codebase. rg --type go --fixed-strings 'getAffiliateRevShares(' -A 2Length of output: 550
Script:
#!/bin/bash # Search for any mentions of 'getAffiliateRevShares' in the entire codebase rg --type go 'getAffiliateRevShares' # Show more context around the function call we found rg --type go --fixed-strings 'getAffiliateRevShares(' -B 5 -A 10Length of output: 2723
affiliateRevShares, affiliateFeesShared, err := k.getAffiliateRevShares(ctx, fill, affiliatesWhitelistMap) | ||
if err != nil { | ||
return types.RevSharesForFill{}, err | ||
} | ||
|
||
unconditionalRevShares, err := k.getUnconditionalRevShares(ctx, netFees) | ||
netFeesSubAffiliateFeesShared := big.NewInt(0).Sub(netFees, affiliateFeesShared) | ||
unconditionalRevShares, err := k.getUnconditionalRevShares(ctx, netFeesSubAffiliateFeesShared) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verify that netFeesSubAffiliateFeesShared
is non-negative after subtraction
Subtracting affiliateFeesShared
from netFees
may result in netFeesSubAffiliateFeesShared
becoming negative if affiliateFeesShared
exceeds netFees
. This could lead to unexpected behavior in subsequent revenue share calculations.
Consider adding a check to ensure netFeesSubAffiliateFeesShared
is not negative, or handle scenarios where it could be zero or negative appropriately.
} | ||
|
||
takerAffiliateAddr, feeSharePpm, exists, err := k.affiliatesKeeper.GetTakerFeeShare( | ||
ctx, takerAddr, affiliatesWhitelistMap) | ||
if err != nil { | ||
return nil, err | ||
return nil, big.NewInt(0), err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return nil
for affiliateFeesShared
when an error occurs
In the error case at line 237, consider returning nil
for affiliateFeesShared
instead of big.NewInt(0)
. This makes it clear that the value is invalid due to the error, aligning with common Go practices.
Apply this diff to adjust the return value:
-return nil, big.NewInt(0), err
+return nil, nil, 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.
return nil, big.NewInt(0), err | |
return nil, nil, err |
@@ -171,17 +171,17 @@ func (k Keeper) GetAllRevShares( | |||
makerFees := fill.MakerFeeQuoteQuantums |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also change REV_SHARE_FEE_SOURCE_NET_FEE
to REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE
to disambiguate
NET_FEE
could be understood as taker fee minus rebates
|
||
totalFeesShared := big.NewInt(0) | ||
takerFees := fill.TakerFeeQuoteQuantums | ||
makerFees := fill.MakerFeeQuoteQuantums | ||
netFees := big.NewInt(0).Add(takerFees, makerFees) | ||
|
||
affiliateRevShares, err := k.getAffiliateRevShares(ctx, fill, affiliatesWhitelistMap) | ||
affiliateRevShares, affiliateFeesShared, err := k.getAffiliateRevShares(ctx, fill, affiliatesWhitelistMap) | ||
if err != nil { | ||
return types.RevSharesForFill{}, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment on caller function persistMatchedOrders
:
Instead propagating error returned from GetAllRevShares
(which would ultimately fail the match transaction), it's better to just pass in empty RevSharesForFill
to DistributeFees
. This way if any rev share calculation encounters issue, the chain will continue to be able to process matches (without rev share).
Order matching + rev share both working > Only order matching working > Order matching not working
if err != nil { | ||
return types.RevSharesForFill{}, err | ||
} | ||
|
||
unconditionalRevShares, err := k.getUnconditionalRevShares(ctx, netFees) | ||
netFeesSubAffiliateFeesShared := big.NewInt(0).Sub(netFees, affiliateFeesShared) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sanity check, let's return error if netFeesSubAffiliateFeesShared
<= 0
if err != nil { | ||
return types.RevSharesForFill{}, err | ||
} | ||
|
||
unconditionalRevShares, err := k.getUnconditionalRevShares(ctx, netFees) | ||
netFeesSubAffiliateFeesShared := big.NewInt(0).Sub(netFees, affiliateFeesShared) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
netFeesSubAffiliateFeesShared := big.NewInt(0).Sub(netFees, affiliateFeesShared) | |
netFeesSubAffiliateFeesShared := new(big.Int).Sub( | |
netFees, | |
affiliateFeesShared, | |
) |
|
||
unconditionalRevShares, err := k.getUnconditionalRevShares(ctx, netFees) | ||
netFeesSubAffiliateFeesShared := big.NewInt(0).Sub(netFees, affiliateFeesShared) | ||
unconditionalRevShares, err := k.getUnconditionalRevShares(ctx, netFeesSubAffiliateFeesShared) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For getMarketMapperRevShare
and getUnconditionalRevShares
update input variable name from netFees
to netFeesSubAffiliateFeesShared
@@ -1500,10 +1500,10 @@ func TestDistributeFees(t *testing.T) { | |||
MonthlyRollingTakerVolumeQuantums: 1_000_000, | |||
}, | |||
expectedSubaccountsModuleAccBalance: big.NewInt(100), // 600 - 500 | |||
expectedFeeModuleAccBalance: big.NewInt(2888), // 2500 + 500 - 50 | |||
expectedMarketMapperAccBalance: big.NewInt(50), // 10% of 500 | |||
expectedFeeModuleAccBalance: big.NewInt(2892), // 2500 + 500 - 108 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realized that we aren't purely testing DistributeFees
function in here, but it's more of an e2e test on affiliate + rev share + distribute fees. This isn't the best practice - should have a function that exclusively tests DistributeFees
, which doesn't rely on affiliatesKeeper
or revshareKeeper.
(We should also remove delete the revShareKeeper
on subaccountsKeeper
@@ -332,23 +332,23 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) { | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my understanding, how did we set up the tier for Bob
in this test case? Didn't find anything related in the setUp()
@@ -420,33 +420,33 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) { | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment on line 411: negative unconditional and market mapper
doesn't make sense - did you mean maker fee is negative
RevShareType: types.REV_SHARE_TYPE_UNCONDITIONAL, | ||
QuoteQuantums: big.NewInt(1_600_000), | ||
QuoteQuantums: big.NewInt(1_300_000), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least for one of the test cases, could we add comment on math behind these expected values? For example,
QuoteQuantums: big.NewInt(1_300_000), // (10 - 2 - 10 * 0.15) * 20% = 1.3
Changelist
[Describe or list the changes made in this PR]
Test Plan
[Describe how this PR was tested (if applicable)]
Author/Reviewer Checklist
state-breaking
label.indexer-postgres-breaking
label.PrepareProposal
orProcessProposal
, manually add the labelproposal-breaking
.feature:[feature-name]
.backport/[branch-name]
.refactor
,chore
,bug
.Summary by CodeRabbit
New Features
Bug Fixes
Refactor