From 0e5d315ebe29f8ac1ed6334f2a343b9f76d60366 Mon Sep 17 00:00:00 2001 From: affan Date: Thu, 12 Sep 2024 17:48:16 -0400 Subject: [PATCH] address comments --- protocol/x/clob/types/types.go | 74 +-------- protocol/x/revshare/keeper/revshare.go | 12 +- protocol/x/revshare/keeper/revshare_test.go | 174 ++++++++++++++------ 3 files changed, 134 insertions(+), 126 deletions(-) diff --git a/protocol/x/clob/types/types.go b/protocol/x/clob/types/types.go index b3db826a15..1858064651 100644 --- a/protocol/x/clob/types/types.go +++ b/protocol/x/clob/types/types.go @@ -2,76 +2,18 @@ package types import "math/big" -type FillForProcess interface { - TakerAddr() string - TakerFeeQuoteQuantums() *big.Int - MakerAddr() string - MakerFeeQuoteQuantums() *big.Int - FillQuoteQuantums() *big.Int - ProductId() uint32 +type FillForProcess struct { + TakerAddr string + TakerFeeQuoteQuantums *big.Int + MakerAddr string + MakerFeeQuoteQuantums *big.Int + FillQuoteQuantums *big.Int + ProductId uint32 // MonthlyRollingTakerVolumeQuantums is the total taker volume for // the given taker address in the last 30 days. This rolling volume // does not include stats of the current block being processed. // If there are multiple fills for the taker address in the // same block, this volume will not be included in the function // below - MonthlyRollingTakerVolumeQuantums() uint64 -} - -type PerpetualFillForProcess struct { - takerAddr string - takerFeeQuoteQuantums *big.Int - makerAddr string - makerFeeQuoteQuantums *big.Int - fillQuoteQuantums *big.Int - perpetualId uint32 - monthlyRollingTakerVolumeQuantums uint64 -} - -func (perpetualFillForProcess PerpetualFillForProcess) TakerAddr() string { - return perpetualFillForProcess.takerAddr -} - -func (perpetualFillForProcess PerpetualFillForProcess) TakerFeeQuoteQuantums() *big.Int { - return perpetualFillForProcess.takerFeeQuoteQuantums -} - -func (perpetualFillForProcess PerpetualFillForProcess) MakerAddr() string { - return perpetualFillForProcess.makerAddr -} - -func (perpetualFillForProcess PerpetualFillForProcess) MakerFeeQuoteQuantums() *big.Int { - return perpetualFillForProcess.makerFeeQuoteQuantums -} - -func (perpetualFillForProcess PerpetualFillForProcess) FillQuoteQuantums() *big.Int { - return perpetualFillForProcess.fillQuoteQuantums -} - -func (perpetualFillForProcess PerpetualFillForProcess) ProductId() uint32 { - return perpetualFillForProcess.perpetualId -} - -func (perpetualFillForProcess PerpetualFillForProcess) MonthlyRollingTakerVolumeQuantums() uint64 { - return perpetualFillForProcess.monthlyRollingTakerVolumeQuantums -} - -func CreatePerpetualFillForProcess( - takerAddr string, - takerFeeQuoteQuantums *big.Int, - makerAddr string, - makerFeeQuoteQuantums *big.Int, - fillQuoteQuantums *big.Int, - perpetualId uint32, - monthlyRollingTakerVolumeQuantums uint64, -) PerpetualFillForProcess { - return PerpetualFillForProcess{ - takerAddr: takerAddr, - takerFeeQuoteQuantums: takerFeeQuoteQuantums, - makerAddr: makerAddr, - makerFeeQuoteQuantums: makerFeeQuoteQuantums, - fillQuoteQuantums: fillQuoteQuantums, - perpetualId: perpetualId, - monthlyRollingTakerVolumeQuantums: monthlyRollingTakerVolumeQuantums, - } + MonthlyRollingTakerVolumeQuantums uint64 } diff --git a/protocol/x/revshare/keeper/revshare.go b/protocol/x/revshare/keeper/revshare.go index d14ccec82a..7545f223ef 100644 --- a/protocol/x/revshare/keeper/revshare.go +++ b/protocol/x/revshare/keeper/revshare.go @@ -153,8 +153,8 @@ func (k Keeper) GetAllRevShares( ) ([]types.RevShare, error) { revShares := []types.RevShare{} totalFeesShared := big.NewInt(0) - takerFees := fill.TakerFeeQuoteQuantums() - makerFees := fill.MakerFeeQuoteQuantums() + takerFees := fill.TakerFeeQuoteQuantums + makerFees := fill.MakerFeeQuoteQuantums netFees := big.NewInt(0).Add(takerFees, makerFees) affiliateRevShares, err := k.getAffiliateRevShares(ctx, fill) @@ -167,7 +167,7 @@ func (k Keeper) GetAllRevShares( return nil, err } - marketMapperRevShares, err := k.getMarketMapperRevShare(ctx, fill.ProductId(), netFees) + marketMapperRevShares, err := k.getMarketMapperRevShare(ctx, fill.ProductId, netFees) if err != nil { return nil, err } @@ -191,9 +191,9 @@ func (k Keeper) getAffiliateRevShares( ctx sdk.Context, fill clobtypes.FillForProcess, ) ([]types.RevShare, error) { - takerAddr := fill.TakerAddr() - takerFee := fill.TakerFeeQuoteQuantums() - if fill.MonthlyRollingTakerVolumeQuantums() >= types.Max30dRefereeVolumeQuantums { + takerAddr := fill.TakerAddr + takerFee := fill.TakerFeeQuoteQuantums + if fill.MonthlyRollingTakerVolumeQuantums >= types.Max30dRefereeVolumeQuantums { return nil, nil } diff --git a/protocol/x/revshare/keeper/revshare_test.go b/protocol/x/revshare/keeper/revshare_test.go index a1be6f8e61..ddffb9abd8 100644 --- a/protocol/x/revshare/keeper/revshare_test.go +++ b/protocol/x/revshare/keeper/revshare_test.go @@ -304,15 +304,81 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) { QuoteQuantums: big.NewInt(1_200_000), }, }, - fill: clobtypes.CreatePerpetualFillForProcess( - constants.AliceAccAddress.String(), - big.NewInt(10_000_000), - constants.BobAccAddress.String(), - big.NewInt(2_000_000), - big.NewInt(100000), - marketId, - 1_000_000_000_000, - ), + fill: clobtypes.FillForProcess{ + TakerAddr: constants.AliceAccAddress.String(), + TakerFeeQuoteQuantums: big.NewInt(10_000_000), + MakerAddr: constants.BobAccAddress.String(), + MakerFeeQuoteQuantums: big.NewInt(2_000_000), + FillQuoteQuantums: big.NewInt(100_000_000_000), + ProductId: marketId, + MonthlyRollingTakerVolumeQuantums: 1_000_000_000_000, + }, + 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% + }, + }, + }) + + affiliatesKeeper.UpdateAffiliateTiers(ctx, affiliatetypes.DefaultAffiliateTiers) + err = affiliatesKeeper.RegisterAffiliate(ctx, constants.AliceAccAddress.String(), + constants.BobAccAddress.String()) + require.NoError(t, err) + }, + }, + { + name: "Valid rev-share from affiliates, negative unconditional and market mapper", + expectedRevShares: []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), + }, + { + Recipient: constants.BobAccAddress.String(), + RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_FEE, + RevShareType: types.REV_SHARE_TYPE_UNCONDITIONAL, + QuoteQuantums: big.NewInt(1_600_000), + }, + { + Recipient: constants.AliceAccAddress.String(), + RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_FEE, + RevShareType: types.REV_SHARE_TYPE_UNCONDITIONAL, + QuoteQuantums: big.NewInt(2_400_000), + }, + { + Recipient: constants.AliceAccAddress.String(), + RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_FEE, + RevShareType: types.REV_SHARE_TYPE_MARKET_MAPPER, + QuoteQuantums: big.NewInt(800_000), + }, + }, + fill: clobtypes.FillForProcess{ + TakerAddr: constants.AliceAccAddress.String(), + TakerFeeQuoteQuantums: big.NewInt(10_000_000), + MakerAddr: constants.BobAccAddress.String(), + MakerFeeQuoteQuantums: big.NewInt(-2_000_000), + FillQuoteQuantums: big.NewInt(100_000_000_000), + ProductId: marketId, + MonthlyRollingTakerVolumeQuantums: 1_000_000_000_000, + }, setup: func(tApp *testapp.TestApp, ctx sdk.Context, keeper *keeper.Keeper, affiliatesKeeper *affiliateskeeper.Keeper) { err := keeper.SetMarketMapperRevenueShareParams(ctx, types.MarketMapperRevenueShareParams{ @@ -343,15 +409,15 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) { }, { name: "Valid revenue share with 30d volume greater than max 30d referral volume", - fill: clobtypes.CreatePerpetualFillForProcess( - constants.AliceAccAddress.String(), - big.NewInt(10_000_000), - constants.BobAccAddress.String(), - big.NewInt(2_000_000), - big.NewInt(100000), - marketId, - types.Max30dRefereeVolumeQuantums+1, - ), + fill: clobtypes.FillForProcess{ + TakerAddr: constants.AliceAccAddress.String(), + TakerFeeQuoteQuantums: big.NewInt(10_000_000), + MakerAddr: constants.BobAccAddress.String(), + MakerFeeQuoteQuantums: big.NewInt(2_000_000), + FillQuoteQuantums: big.NewInt(100_000_000_000), + ProductId: marketId, + MonthlyRollingTakerVolumeQuantums: types.Max30dRefereeVolumeQuantums + 1, + }, expectedRevShares: []types.RevShare{ { Recipient: constants.BobAccAddress.String(), @@ -416,15 +482,15 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) { QuoteQuantums: big.NewInt(1_200_000), }, }, - fill: clobtypes.CreatePerpetualFillForProcess( - constants.AliceAccAddress.String(), - big.NewInt(10_000_000), - constants.BobAccAddress.String(), - big.NewInt(2_000_000), - big.NewInt(100000), - marketId, - 1_000_000_000_000, // 1 million USDC - ), + fill: clobtypes.FillForProcess{ + TakerAddr: constants.AliceAccAddress.String(), + TakerFeeQuoteQuantums: big.NewInt(10_000_000), + MakerAddr: constants.BobAccAddress.String(), + MakerFeeQuoteQuantums: big.NewInt(2_000_000), + FillQuoteQuantums: big.NewInt(100_000_000_000), + ProductId: marketId, + MonthlyRollingTakerVolumeQuantums: 1_000_000_000_000, + }, setup: func(tApp *testapp.TestApp, ctx sdk.Context, keeper *keeper.Keeper, affiliatesKeeper *affiliateskeeper.Keeper) { err := keeper.SetMarketMapperRevenueShareParams(ctx, types.MarketMapperRevenueShareParams{ @@ -457,15 +523,15 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) { QuoteQuantums: big.NewInt(2_400_000), }, }, - fill: clobtypes.CreatePerpetualFillForProcess( - constants.AliceAccAddress.String(), - big.NewInt(10_000_000), - constants.BobAccAddress.String(), - big.NewInt(2_000_000), - big.NewInt(100000), - marketId, - 1_000_000_000_000, // 1 million USDC - ), + fill: clobtypes.FillForProcess{ + TakerAddr: constants.AliceAccAddress.String(), + TakerFeeQuoteQuantums: big.NewInt(10_000_000), + MakerAddr: constants.BobAccAddress.String(), + MakerFeeQuoteQuantums: big.NewInt(2_000_000), + FillQuoteQuantums: big.NewInt(100_000_000_000), + ProductId: marketId, + MonthlyRollingTakerVolumeQuantums: 1_000_000_000_000, + }, setup: func(tApp *testapp.TestApp, ctx sdk.Context, keeper *keeper.Keeper, affiliatesKeeper *affiliateskeeper.Keeper) { keeper.SetUnconditionalRevShareConfigParams(ctx, types.UnconditionalRevShareConfig{ @@ -485,15 +551,15 @@ func TestKeeper_GetAllRevShares_Valid(t *testing.T) { { name: "No rev shares", expectedRevShares: []types.RevShare{}, - fill: clobtypes.CreatePerpetualFillForProcess( - constants.AliceAccAddress.String(), - big.NewInt(10_000_000), - constants.BobAccAddress.String(), - big.NewInt(2_000_000), - big.NewInt(100000), - marketId, - 1_000_000_000_000, // 1 million USDC - ), + fill: clobtypes.FillForProcess{ + TakerAddr: constants.AliceAccAddress.String(), + TakerFeeQuoteQuantums: big.NewInt(10_000_000), + MakerAddr: constants.BobAccAddress.String(), + MakerFeeQuoteQuantums: big.NewInt(2_000_000), + FillQuoteQuantums: big.NewInt(100_000_000_000), + ProductId: marketId, + MonthlyRollingTakerVolumeQuantums: 1_000_000_000_000, + }, setup: func(tApp *testapp.TestApp, ctx sdk.Context, keeper *keeper.Keeper, affiliatesKeeper *affiliateskeeper.Keeper) { }, @@ -635,15 +701,15 @@ func TestKeeper_GetAllRevShares_Invalid(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { // Setup - fill := clobtypes.CreatePerpetualFillForProcess( - constants.AliceAccAddress.String(), - big.NewInt(10_000_000), - constants.BobAccAddress.String(), - big.NewInt(2_000_000), - big.NewInt(100000), - uint32(1), - tc.monthlyRollingTakerVolumeQuantums, - ) + fill := clobtypes.FillForProcess{ + TakerAddr: constants.AliceAccAddress.String(), + TakerFeeQuoteQuantums: big.NewInt(10_000_000), + MakerAddr: constants.BobAccAddress.String(), + MakerFeeQuoteQuantums: big.NewInt(2_000_000), + FillQuoteQuantums: big.NewInt(100_000_000_000), + ProductId: uint32(1), + MonthlyRollingTakerVolumeQuantums: 1_000_000_000_000, + } tApp := testapp.NewTestAppBuilder(t).Build() ctx := tApp.InitChain() keeper := tApp.App.RevShareKeeper