Skip to content

Commit

Permalink
[OTE-776] Implement GetAllRevshares (#2228)
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 authored Sep 12, 2024
1 parent 9fd093a commit ac1e578
Show file tree
Hide file tree
Showing 6 changed files with 642 additions and 1 deletion.
18 changes: 18 additions & 0 deletions protocol/x/clob/types/types.go
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
package types

import "math/big"

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
}
119 changes: 119 additions & 0 deletions protocol/x/revshare/keeper/revshare.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
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"
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 @@ -142,3 +146,118 @@ func (k Keeper) ValidateRevShareSafety(
totalRevSharePpm := totalUnconditionalRevSharePpm + totalMarketMapperRevSharePpm + highestTierRevSharePpm
return totalRevSharePpm < lib.OneMillion
}

func (k Keeper) GetAllRevShares(
ctx sdk.Context,
fill clobtypes.FillForProcess,
) ([]types.RevShare, error) {
revShares := []types.RevShare{}
totalFeesShared := big.NewInt(0)
takerFees := fill.TakerFeeQuoteQuantums
makerFees := fill.MakerFeeQuoteQuantums
netFees := big.NewInt(0).Add(takerFees, makerFees)

affiliateRevShares, err := k.getAffiliateRevShares(ctx, fill)
if err != nil {
return nil, err
}

unconditionalRevShares, err := k.getUnconditionalRevShares(ctx, netFees)
if err != nil {
return nil, err
}

marketMapperRevShares, err := k.getMarketMapperRevShare(ctx, fill.ProductId, netFees)
if err != nil {
return nil, err
}

revShares = append(revShares, affiliateRevShares...)
revShares = append(revShares, unconditionalRevShares...)
revShares = append(revShares, marketMapperRevShares...)

for _, revShare := range revShares {
totalFeesShared.Add(totalFeesShared, revShare.QuoteQuantums)
}
//check total fees shared is less than or equal to net fees
if totalFeesShared.Cmp(netFees) > 0 {
return nil, errorsmod.Wrap(types.ErrTotalFeesSharedExceedsNetFees, "total fees shared exceeds net fees")
}

return revShares, nil
}

func (k Keeper) getAffiliateRevShares(
ctx sdk.Context,
fill clobtypes.FillForProcess,
) ([]types.RevShare, error) {
takerAddr := fill.TakerAddr
takerFee := fill.TakerFeeQuoteQuantums
if fill.MonthlyRollingTakerVolumeQuantums >= types.Max30dRefereeVolumeQuantums {
return nil, nil
}

takerAffiliateAddr, feeSharePpm, exists, err := k.affiliatesKeeper.GetTakerFeeShare(ctx, takerAddr)
if err != nil {
return nil, err
}
if !exists {
return nil, nil
}
feesShared := lib.BigMulPpm(takerFee, lib.BigU(feeSharePpm), false)
return []types.RevShare{
{
Recipient: takerAffiliateAddr,
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_TAKER_FEE,
RevShareType: types.REV_SHARE_TYPE_AFFILIATE,
QuoteQuantums: feesShared,
},
}, nil
}

func (k Keeper) getUnconditionalRevShares(
ctx sdk.Context,
netFees *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)
revShare := types.RevShare{
Recipient: revShare.Address,
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_FEE,
RevShareType: types.REV_SHARE_TYPE_UNCONDITIONAL,
QuoteQuantums: feeShared,
}
revShares = append(revShares, revShare)
}
return revShares, nil
}

func (k Keeper) getMarketMapperRevShare(
ctx sdk.Context,
marketId uint32,
netFees *big.Int,
) ([]types.RevShare, error) {
revShares := []types.RevShare{}
marketMapperRevshareAddress, revenueSharePpm, err := k.GetMarketMapperRevenueShareForMarket(ctx, marketId)
if err != nil {
return nil, err
}
if revenueSharePpm == 0 {
return nil, nil
}

marketMapperRevshareAmount := lib.BigMulPpm(netFees, lib.BigU(revenueSharePpm), false)
revShares = append(revShares, types.RevShare{
Recipient: marketMapperRevshareAddress.String(),
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_FEE,
RevShareType: types.REV_SHARE_TYPE_MARKET_MAPPER,
QuoteQuantums: marketMapperRevshareAmount,
})

return revShares, nil
}
Loading

0 comments on commit ac1e578

Please # to comment.