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-776] Implement GetAllRevshares #2228

Merged
merged 8 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions protocol/x/clob/types/types.go
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
package types

import "math/big"
affanv14 marked this conversation as resolved.
Show resolved Hide resolved

type FillForProcess interface {
TakerAddr() string
TakerFeeQuoteQuantums() *big.Int
MakerAddr() string
MakerFeeQuoteQuantums() *big.Int
FillQuoteQuantums() *big.Int
FillSourceId() uint32
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) FillSourceId() 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,
}
}
124 changes: 124 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,123 @@ 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, affiliateRevFeesShared, err := k.getAffiliateRevShares(ctx, fill)
teddyding marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

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

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

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

totalFeesShared.Add(totalFeesShared, affiliateRevFeesShared)
totalFeesShared.Add(totalFeesShared, unconditionalRevFeesShared)
totalFeesShared.Add(totalFeesShared, marketMapperRevFeesShared)

//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, *big.Int, error) {
takerAddr := fill.TakerAddr()
takerFee := fill.TakerFeeQuoteQuantums()
if fill.MonthlyRollingTakerVolumeQuantums() >= types.Max30dRefereeVolumeQuantums {
return nil, big.NewInt(0), nil
}

takerAffiliateAddr, feeSharePpm, exists, err := k.affiliatesKeeper.GetTakerFeeShare(ctx, takerAddr)
if err != nil {
return nil, big.NewInt(0), err
}
if !exists {
return nil, big.NewInt(0), 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,
},
}, feesShared, nil
}

func (k Keeper) getUnconditionalRevShares(
ctx sdk.Context,
netFees *big.Int,
) ([]types.RevShare, *big.Int, error) {
revShares := []types.RevShare{}
totalFeesShared := big.NewInt(0)
unconditionalRevShareConfig, err := k.GetUnconditionalRevShareConfigParams(ctx)
if err != nil {
return nil, big.NewInt(0), err
}
for _, revShare := range unconditionalRevShareConfig.Configs {
feeShared := lib.BigMulPpm(netFees, lib.BigU(revShare.SharePpm), false)
totalFeesShared.Add(totalFeesShared, feeShared)
revShare := types.RevShare{
Recipient: revShare.Address,
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_FEE,
Copy link
Contributor

@teddyding teddyding Sep 11, 2024

Choose a reason for hiding this comment

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

On second thought, do we need this in any downstream logic, besides metrics?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dont we need revshare Fee source in trading rewards formula?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And revshare type to retrieve affiliate revshares later for sending indexer events

Copy link
Contributor

Choose a reason for hiding this comment

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

Dont we need revshare Fee source in trading rewards formula?

Ah yes we need so that we can separate reward logic for maker and taker fee

RevShareType: types.REV_SHARE_TYPE_UNCONDITIONAL,
QuoteQuantums: feeShared,
}
revShares = append(revShares, revShare)
}
return revShares, totalFeesShared, nil
}

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

marketMapperRevshareAmount := lib.BigMulPpm(netFees, lib.BigU(revenueSharePpm), false)
feesShared.Add(feesShared, marketMapperRevshareAmount)
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, feesShared, nil
}
Loading
Loading