Skip to content

Commit

Permalink
Update GetAllRevshare to handle liquidations (#2413)
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 authored Oct 2, 2024
1 parent 53b298b commit 829b68b
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 9 deletions.
5 changes: 1 addition & 4 deletions protocol/x/affiliates/abci.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package affiliates

import (
"runtime/debug"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/lib/log"
"github.com/dydxprotocol/v4-chain/protocol/x/affiliates/keeper"
Expand All @@ -13,7 +11,6 @@ func EndBlocker(
keeper *keeper.Keeper,
) {
if err := keeper.AggregateAffiliateReferredVolumeForFills(ctx); err != nil {
log.ErrorLog(ctx, "error aggregating affiliate volume for fills", "error",
err, "stack", string(debug.Stack()))
log.ErrorLogWithError(ctx, "error aggregating affiliate volume for fills", err)
}
}
2 changes: 1 addition & 1 deletion protocol/x/clob/keeper/process_single_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func (k Keeper) persistMatchedOrders(
revSharesForFill, err := k.revshareKeeper.GetAllRevShares(ctx, fillForProcess, affiliatesWhitelistMap)
if err != nil {
revSharesForFill = revsharetypes.RevSharesForFill{}
log.ErrorLog(ctx, "error getting rev shares for fill", "error", err)
log.ErrorLogWithError(ctx, "error getting rev shares for fill", err)
}
if revSharesForFill.AffiliateRevShare != nil {
affiliateRevSharesQuoteQuantums = revSharesForFill.AffiliateRevShare.QuoteQuantums
Expand Down
10 changes: 8 additions & 2 deletions protocol/x/revshare/keeper/revshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,18 @@ func (k Keeper) GetAllRevShares(
makerFees := fill.MakerFeeQuoteQuantums
netFees := big.NewInt(0).Add(takerFees, makerFees)

// when net fee is zero, no rev share is generated from the fill
if netFees.Sign() == 0 {
return types.RevSharesForFill{}, nil
}

affiliateRevShares, affiliateFeesShared, err := k.getAffiliateRevShares(ctx, fill, affiliatesWhitelistMap)
if err != nil {
return types.RevSharesForFill{}, err
}
netFeesSubAffiliateFeesShared := new(big.Int).Sub(netFees, affiliateFeesShared)
if netFeesSubAffiliateFeesShared.Sign() <= 0 {
return types.RevSharesForFill{}, types.ErrAffiliateFeesSharedExceedsNetFees
return types.RevSharesForFill{}, types.ErrAffiliateFeesSharedGreaterThanOrEqualToNetFees
}

unconditionalRevShares, err := k.getUnconditionalRevShares(ctx, netFeesSubAffiliateFeesShared)
Expand Down Expand Up @@ -233,7 +238,8 @@ func (k Keeper) getAffiliateRevShares(
) ([]types.RevShare, *big.Int, error) {
takerAddr := fill.TakerAddr
takerFee := fill.TakerFeeQuoteQuantums
if fill.MonthlyRollingTakerVolumeQuantums >= types.MaxReferee30dVolumeForAffiliateShareQuantums {
if fill.MonthlyRollingTakerVolumeQuantums >= types.MaxReferee30dVolumeForAffiliateShareQuantums ||
takerFee.Sign() == 0 {
return nil, big.NewInt(0), nil
}

Expand Down
118 changes: 118 additions & 0 deletions protocol/x/revshare/keeper/revshare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,124 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) {
affiliatesKeeper *affiliateskeeper.Keeper) {
},
},
{
name: "Revshares with 0 fees from maker and taker",
expectedRevSharesForFill: types.RevSharesForFill{},
fill: clobtypes.FillForProcess{
TakerAddr: constants.AliceAccAddress.String(),
TakerFeeQuoteQuantums: big.NewInt(0),
MakerAddr: constants.BobAccAddress.String(),
MakerFeeQuoteQuantums: big.NewInt(0),
FillQuoteQuantums: big.NewInt(100_000_000_000),
ProductId: perpetualId,
MonthlyRollingTakerVolumeQuantums: 1_000_000_000_000,
MarketId: marketId,
},
setup: func(tApp *testapp.TestApp, ctx sdk.Context, keeper *keeper.Keeper,
affiliatesKeeper *affiliateskeeper.Keeper) {
err := keeper.SetMarketMapperRevenueShareParams(ctx, types.MarketMapperRevenueShareParams{
Address: constants.AliceAccAddress.String(),
RevenueSharePpm: 100_000, // 10%
ValidDays: 1,
})
require.NoError(t, err)

keeper.SetUnconditionalRevShareConfigParams(ctx, types.UnconditionalRevShareConfig{
Configs: []types.UnconditionalRevShareConfig_RecipientConfig{
{
Address: constants.BobAccAddress.String(),
SharePpm: 200_000, // 20%
},
{
Address: constants.AliceAccAddress.String(),
SharePpm: 300_000, // 30%
},
},
})

err = affiliatesKeeper.UpdateAffiliateTiers(ctx, affiliatetypes.DefaultAffiliateTiers)
require.NoError(t, err)
err = affiliatesKeeper.RegisterAffiliate(ctx, constants.AliceAccAddress.String(),
constants.BobAccAddress.String())
require.NoError(t, err)
},
},
{
name: "Valid revenue share with 0 taker fee and positive maker fees",
expectedRevSharesForFill: types.RevSharesForFill{
AllRevShares: []types.RevShare{
{
Recipient: constants.BobAccAddress.String(),
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE,
RevShareType: types.REV_SHARE_TYPE_UNCONDITIONAL,
QuoteQuantums: big.NewInt(400_000),
RevSharePpm: 200_000,
},
{
Recipient: constants.AliceAccAddress.String(),
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE,
RevShareType: types.REV_SHARE_TYPE_UNCONDITIONAL,
QuoteQuantums: big.NewInt(600_000),
RevSharePpm: 300_000,
},
{
Recipient: constants.AliceAccAddress.String(),
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE,
RevShareType: types.REV_SHARE_TYPE_MARKET_MAPPER,
QuoteQuantums: big.NewInt(200_000),
RevSharePpm: 100_000,
},
},
AffiliateRevShare: nil,
FeeSourceToQuoteQuantums: map[types.RevShareFeeSource]*big.Int{
types.REV_SHARE_FEE_SOURCE_TAKER_FEE: big.NewInt(0),
// unconditional + market mapper rev shares fees
types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: big.NewInt(1_200_000),
},
FeeSourceToRevSharePpm: map[types.RevShareFeeSource]uint32{
types.REV_SHARE_FEE_SOURCE_TAKER_FEE: 0,
types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: 600_000,
},
},
fill: clobtypes.FillForProcess{
TakerAddr: constants.AliceAccAddress.String(),
TakerFeeQuoteQuantums: big.NewInt(0),
MakerAddr: constants.BobAccAddress.String(),
MakerFeeQuoteQuantums: big.NewInt(2_000_000),
FillQuoteQuantums: big.NewInt(100_000_000_000),
ProductId: perpetualId,
MonthlyRollingTakerVolumeQuantums: 1_000_000_000_000,
MarketId: marketId,
},
setup: func(tApp *testapp.TestApp, ctx sdk.Context, keeper *keeper.Keeper,
affiliatesKeeper *affiliateskeeper.Keeper) {
err := keeper.SetMarketMapperRevenueShareParams(ctx, types.MarketMapperRevenueShareParams{
Address: constants.AliceAccAddress.String(),
RevenueSharePpm: 100_000, // 10%
ValidDays: 1,
})
require.NoError(t, err)

keeper.SetUnconditionalRevShareConfigParams(ctx, types.UnconditionalRevShareConfig{
Configs: []types.UnconditionalRevShareConfig_RecipientConfig{
{
Address: constants.BobAccAddress.String(),
SharePpm: 200_000, // 20%
},
{
Address: constants.AliceAccAddress.String(),
SharePpm: 300_000, // 30%
},
},
})

err = affiliatesKeeper.UpdateAffiliateTiers(ctx, affiliatetypes.DefaultAffiliateTiers)
require.NoError(t, err)
err = affiliatesKeeper.RegisterAffiliate(ctx, constants.AliceAccAddress.String(),
constants.BobAccAddress.String())
require.NoError(t, err)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/revshare/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ var (
6,
"total fees shared exceeds net fees",
)
ErrAffiliateFeesSharedExceedsNetFees = errorsmod.Register(
ErrAffiliateFeesSharedGreaterThanOrEqualToNetFees = errorsmod.Register(
ModuleName,
7,
"affiliate fees shared exceeds net fees",
"affiliate fees shared greater than or equal to net fees",
)
)

0 comments on commit 829b68b

Please sign in to comment.