Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 committed Sep 12, 2024
1 parent f34b786 commit 0e5d315
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 126 deletions.
74 changes: 8 additions & 66 deletions protocol/x/clob/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 6 additions & 6 deletions protocol/x/revshare/keeper/revshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
174 changes: 120 additions & 54 deletions protocol/x/revshare/keeper/revshare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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) {
},
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0e5d315

Please sign in to comment.