Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 committed Sep 23, 2024
1 parent 3f21cf6 commit f162535
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 150 deletions.
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)
if err != nil {
return types.RevSharesForFill{}, err
log.ErrorLogWithError(ctx, "error getting unconditional rev shares", err)
return types.RevSharesForFill{}, nil
}

if netFeesSubAffiliateFeesShared.Sign() <= 0 {
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
20 changes: 10 additions & 10 deletions protocol/x/revshare/keeper/revshare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,45 +327,45 @@ 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,
},
{
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_NET_PROTOCOL_REVENUE: big.NewInt(6_300_000),
types.REV_SHARE_FEE_SOURCE_TAKER_FEE: big.NewInt(1_500_000), // affiliate rev share fees
types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: big.NewInt(6_300_000), // unconditional + market mapper rev shares fees
},
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 +408,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
Loading

0 comments on commit f162535

Please sign in to comment.