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-816] Update Revshare logic for affiliates #2298

Merged
merged 14 commits into from
Sep 24, 2024
1 change: 0 additions & 1 deletion protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,6 @@ func New(
app.BankKeeper,
app.PerpetualsKeeper,
app.BlockTimeKeeper,
app.RevShareKeeper,
app.IndexerEventManager,
app.FullNodeStreamingManager,
)
Expand Down
1 change: 0 additions & 1 deletion protocol/testutil/keeper/clob.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func NewClobKeepersTestContextWithUninitializedMemStore(
bankKeeper,
ks.PerpetualsKeeper,
ks.BlockTimeKeeper,
revShareKeeper,
indexerEventsTransientStoreKey,
true,
)
Expand Down
1 change: 0 additions & 1 deletion protocol/testutil/keeper/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func ListingKeepers(
bankKeeper,
perpetualsKeeper,
blockTimeKeeper,
revShareKeeper,
transientStoreKey,
true,
)
Expand Down
1 change: 0 additions & 1 deletion protocol/testutil/keeper/sending.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func SendingKeepersWithSubaccountsKeeper(t testing.TB, saKeeper types.Subaccount
ks.BankKeeper,
ks.PerpetualsKeeper,
blockTimeKeeper,
revShareKeeper,
transientStoreKey,
true,
)
Expand Down
3 changes: 0 additions & 3 deletions protocol/testutil/keeper/subaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func SubaccountsKeepers(t testing.TB, msgSenderEnabled bool) (
bankKeeper,
perpetualsKeeper,
blocktimeKeeper,
revShareKeeper,
transientStoreKey,
msgSenderEnabled,
)
Expand Down Expand Up @@ -132,7 +131,6 @@ func createSubaccountsKeeper(
bk types.BankKeeper,
pk *perpskeeper.Keeper,
btk *blocktimekeeper.Keeper,
rsk *revsharekeeper.Keeper,
transientStoreKey storetypes.StoreKey,
msgSenderEnabled bool,
) (*keeper.Keeper, storetypes.StoreKey) {
Expand All @@ -151,7 +149,6 @@ func createSubaccountsKeeper(
bk,
pk,
btk,
rsk,
mockIndexerEventsManager,
streaming.NewNoopGrpcStreamingManager(),
)
Expand Down
30 changes: 19 additions & 11 deletions protocol/x/revshare/keeper/revshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package keeper
import (
"math/big"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/lib/log"
affiliatetypes "github.com/dydxprotocol/v4-chain/protocol/x/affiliates/types"
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
"github.com/dydxprotocol/v4-chain/protocol/x/revshare/types"
Expand Down Expand Up @@ -173,17 +173,25 @@ func (k Keeper) GetAllRevShares(

affiliateRevShares, affiliateFeesShared, err := k.getAffiliateRevShares(ctx, fill, affiliatesWhitelistMap)
if err != nil {
return types.RevSharesForFill{}, err
log.ErrorLogWithError(ctx, "error getting affiliate rev shares", err)
return types.RevSharesForFill{}, nil
}
netFeesSubAffiliateFeesShared := big.NewInt(0).Sub(netFees, affiliateFeesShared)
netFeesSubAffiliateFeesShared := new(big.Int).Sub(netFees, affiliateFeesShared)
unconditionalRevShares, err := k.getUnconditionalRevShares(ctx, netFeesSubAffiliateFeesShared)
teddyding marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return types.RevSharesForFill{}, err
log.ErrorLogWithError(ctx, "error getting unconditional rev shares", err)
return types.RevSharesForFill{}, nil
}

if netFeesSubAffiliateFeesShared.Sign() <= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: move to immediately below line 129

log.ErrorLog(ctx, "net fees sub affiliate fees shared is less than or equal to 0")
return types.RevSharesForFill{}, nil
}

marketMapperRevShares, err := k.getMarketMapperRevShare(ctx, fill.MarketId, netFeesSubAffiliateFeesShared)
if err != nil {
return types.RevSharesForFill{}, err
log.ErrorLogWithError(ctx, "error getting market mapper rev shares", err)
return types.RevSharesForFill{}, nil
}

revShares = append(revShares, affiliateRevShares...)
Expand All @@ -208,8 +216,8 @@ func (k Keeper) GetAllRevShares(
}
//check total fees shared is less than or equal to net fees
if totalFeesShared.Cmp(netFees) > 0 {
return types.RevSharesForFill{}, errorsmod.Wrap(
types.ErrTotalFeesSharedExceedsNetFees, "total fees shared exceeds net fees")
log.ErrorLog(ctx, "total fees shared exceeds net fees")
return types.RevSharesForFill{}, nil
}

return types.RevSharesForFill{
Expand Down Expand Up @@ -253,15 +261,15 @@ func (k Keeper) getAffiliateRevShares(

func (k Keeper) getUnconditionalRevShares(
ctx sdk.Context,
netFees *big.Int,
netFeesSubAffiliateFeesShared *big.Int,
) ([]types.RevShare, error) {
revShares := []types.RevShare{}
unconditionalRevShareConfig, err := k.GetUnconditionalRevShareConfigParams(ctx)
if err != nil {
return nil, err
}
for _, revShare := range unconditionalRevShareConfig.Configs {
feeShared := lib.BigMulPpm(netFees, lib.BigU(revShare.SharePpm), false)
feeShared := lib.BigMulPpm(netFeesSubAffiliateFeesShared, lib.BigU(revShare.SharePpm), false)
revShare := types.RevShare{
Recipient: revShare.Address,
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE,
Expand All @@ -277,7 +285,7 @@ func (k Keeper) getUnconditionalRevShares(
func (k Keeper) getMarketMapperRevShare(
ctx sdk.Context,
marketId uint32,
netFees *big.Int,
netFeesSubAffiliateFeesShared *big.Int,
) ([]types.RevShare, error) {
revShares := []types.RevShare{}
marketMapperRevshareAddress, revenueSharePpm, err := k.GetMarketMapperRevenueShareForMarket(ctx, marketId)
Expand All @@ -288,7 +296,7 @@ func (k Keeper) getMarketMapperRevShare(
return nil, nil
}

marketMapperRevshareAmount := lib.BigMulPpm(netFees, lib.BigU(revenueSharePpm), false)
marketMapperRevshareAmount := lib.BigMulPpm(netFeesSubAffiliateFeesShared, lib.BigU(revenueSharePpm), false)
revShares = append(revShares, types.RevShare{
Recipient: marketMapperRevshareAddress.String(),
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE,
Expand Down
35 changes: 17 additions & 18 deletions protocol/x/revshare/keeper/revshare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,45 +327,46 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) {
Recipient: constants.BobAccAddress.String(),
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_TAKER_FEE,
RevShareType: types.REV_SHARE_TYPE_AFFILIATE,
QuoteQuantums: big.NewInt(1_500_000),
QuoteQuantums: big.NewInt(1_500_000), // 15 % of 10 million taker fee quote quantums
RevSharePpm: 150_000,
},
Copy link
Contributor

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()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

bob address has coins already setup here for staking. This just puts bob at the max tier

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, could we document in the unit test? Not very obvious to reader

{
Recipient: constants.BobAccAddress.String(),
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE,
RevShareType: types.REV_SHARE_TYPE_UNCONDITIONAL,
QuoteQuantums: big.NewInt(2_100_000),
QuoteQuantums: big.NewInt(2_100_000), // (10 + 2 - 1.5) * 20%
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(3_150_000),
QuoteQuantums: big.NewInt(3_150_000), // (10 + 2 - 1.5) * 30%
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(1_050_000),
QuoteQuantums: big.NewInt(1_050_000), // (10 + 2 - 1.5) * 10%
RevSharePpm: 100_000,
},
},
AffiliateRevShare: &types.RevShare{
Recipient: constants.BobAccAddress.String(),
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_TAKER_FEE,
RevShareType: types.REV_SHARE_TYPE_AFFILIATE,
QuoteQuantums: big.NewInt(1_500_000),
QuoteQuantums: big.NewInt(1_500_000), // 15 % of 10 million taker fee quote quantums
RevSharePpm: 150_000,
},
FeeSourceToQuoteQuantums: map[types.RevShareFeeSource]*big.Int{
types.REV_SHARE_FEE_SOURCE_TAKER_FEE: big.NewInt(1_500_000),
types.REV_SHARE_FEE_SOURCE_TAKER_FEE: big.NewInt(1_500_000), // affiliate rev share fees
// unconditional + market mapper rev shares fees
types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: big.NewInt(6_300_000),
},
FeeSourceToRevSharePpm: map[types.RevShareFeeSource]uint32{
types.REV_SHARE_FEE_SOURCE_TAKER_FEE: 150_000,
types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: 600_000,
types.REV_SHARE_FEE_SOURCE_TAKER_FEE: 150_000, // affiliate rev share fee ppm
types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: 600_000, // unconditional + market mapper rev share fee ppm
},
},
fill: clobtypes.FillForProcess{
Expand Down Expand Up @@ -408,7 +409,7 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) {
},
},
{
name: "Valid rev-share from affiliates, negative unconditional and market mapper",
name: "Valid rev-share from affiliates, negative maker fee and unconditional and market mapper",
expectedRevSharesForFill: types.RevSharesForFill{
AllRevShares: []types.RevShare{
{
Expand Down Expand Up @@ -821,16 +822,14 @@ func TestKeeper_GetAllRevShares_Invalid(t *testing.T) {
name string
revenueSharePpmNetFees uint32
revenueSharePpmTakerFees uint32
expectedError error
monthlyRollingTakerVolumeQuantums uint64
setup func(tApp *testapp.TestApp, ctx sdk.Context,
keeper *keeper.Keeper, affiliatesKeeper *affiliateskeeper.Keeper)
}{
{
name: "Total fees shared exceeds net fees from all sources",
revenueSharePpmNetFees: 950_000, // 95%,
revenueSharePpmTakerFees: 150_000, // 15%
expectedError: types.ErrTotalFeesSharedExceedsNetFees,
revenueSharePpmNetFees: 950_000, // 95%,
revenueSharePpmTakerFees: 150_000, // 15%
monthlyRollingTakerVolumeQuantums: 1_000_000_000_000, // 1 million USDC
setup: func(tApp *testapp.TestApp, ctx sdk.Context, keeper *keeper.Keeper,
affiliatesKeeper *affiliateskeeper.Keeper) {
Expand Down Expand Up @@ -858,9 +857,8 @@ func TestKeeper_GetAllRevShares_Invalid(t *testing.T) {
},
{
name: "Total fees shared exceeds net fees - no affiliate rev shares",
revenueSharePpmNetFees: 1_150_000, // 115%,
revenueSharePpmTakerFees: 0, // 0%
expectedError: types.ErrTotalFeesSharedExceedsNetFees,
revenueSharePpmNetFees: 1_150_000, // 115%,
revenueSharePpmTakerFees: 0, // 0%
monthlyRollingTakerVolumeQuantums: 1_000_000_000_000, // 1 million USDC
setup: func(tApp *testapp.TestApp, ctx sdk.Context, keeper *keeper.Keeper,
affiliatesKeeper *affiliateskeeper.Keeper) {
Expand Down Expand Up @@ -906,9 +904,10 @@ func TestKeeper_GetAllRevShares_Invalid(t *testing.T) {

keeper.CreateNewMarketRevShare(ctx, marketId)

_, err := keeper.GetAllRevShares(ctx, fill, map[string]uint32{})
revSharesForFill, err := keeper.GetAllRevShares(ctx, fill, map[string]uint32{})

require.ErrorIs(t, err, tc.expectedError)
require.NoError(t, err)
require.Equal(t, types.RevSharesForFill{}, revSharesForFill)
})
}
}
4 changes: 1 addition & 3 deletions protocol/x/subaccounts/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"

streamingtypes "github.com/dydxprotocol/v4-chain/protocol/streaming/types"

"cosmossdk.io/log"
Expand All @@ -20,7 +21,6 @@ type (
bankKeeper types.BankKeeper
perpetualsKeeper types.PerpetualsKeeper
blocktimeKeeper types.BlocktimeKeeper
revShareKeeper types.RevShareKeeper
indexerEventManager indexer_manager.IndexerEventManager
streamingManager streamingtypes.FullNodeStreamingManager
}
Expand All @@ -33,7 +33,6 @@ func NewKeeper(
bankKeeper types.BankKeeper,
perpetualsKeeper types.PerpetualsKeeper,
blocktimeKeeper types.BlocktimeKeeper,
revShareKeeper types.RevShareKeeper,
indexerEventManager indexer_manager.IndexerEventManager,
streamingManager streamingtypes.FullNodeStreamingManager,
) *Keeper {
Expand All @@ -44,7 +43,6 @@ func NewKeeper(
bankKeeper: bankKeeper,
perpetualsKeeper: perpetualsKeeper,
blocktimeKeeper: blocktimeKeeper,
revShareKeeper: revShareKeeper,
indexerEventManager: indexerEventManager,
streamingManager: streamingManager,
}
Expand Down
Loading
Loading