Skip to content

Commit

Permalink
[OTE-816] Update Revshare logic for affiliates (#2298)
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 authored Sep 24, 2024
1 parent cc59dd2 commit 1659f93
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 342 deletions.
1 change: 0 additions & 1 deletion protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,6 @@ func New(
app.BankKeeper,
app.PerpetualsKeeper,
app.BlockTimeKeeper,
app.RevShareKeeper,
app.IndexerEventManager,
app.FullNodeStreamingManager,
)
Expand Down
1 change: 0 additions & 1 deletion protocol/testutil/keeper/clob.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func NewClobKeepersTestContextWithUninitializedMemStore(
bankKeeper,
ks.PerpetualsKeeper,
ks.BlockTimeKeeper,
revShareKeeper,
indexerEventsTransientStoreKey,
true,
)
Expand Down
1 change: 0 additions & 1 deletion protocol/testutil/keeper/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func ListingKeepers(
bankKeeper,
perpetualsKeeper,
blockTimeKeeper,
revShareKeeper,
transientStoreKey,
true,
)
Expand Down
1 change: 0 additions & 1 deletion protocol/testutil/keeper/sending.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func SendingKeepersWithSubaccountsKeeper(t testing.TB, saKeeper types.Subaccount
ks.BankKeeper,
ks.PerpetualsKeeper,
blockTimeKeeper,
revShareKeeper,
transientStoreKey,
true,
)
Expand Down
3 changes: 0 additions & 3 deletions protocol/testutil/keeper/subaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func SubaccountsKeepers(t testing.TB, msgSenderEnabled bool) (
bankKeeper,
perpetualsKeeper,
blocktimeKeeper,
revShareKeeper,
transientStoreKey,
msgSenderEnabled,
)
Expand Down Expand Up @@ -132,7 +131,6 @@ func createSubaccountsKeeper(
bk types.BankKeeper,
pk *perpskeeper.Keeper,
btk *blocktimekeeper.Keeper,
rsk *revsharekeeper.Keeper,
transientStoreKey storetypes.StoreKey,
msgSenderEnabled bool,
) (*keeper.Keeper, storetypes.StoreKey) {
Expand All @@ -151,7 +149,6 @@ func createSubaccountsKeeper(
bk,
pk,
btk,
rsk,
mockIndexerEventsManager,
streaming.NewNoopGrpcStreamingManager(),
)
Expand Down
10 changes: 5 additions & 5 deletions protocol/x/clob/keeper/process_single_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
assettypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
revsharetypes "github.com/dydxprotocol/v4-chain/protocol/x/revshare/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
gometrics "github.com/hashicorp/go-metrics"
)
Expand Down Expand Up @@ -464,14 +465,13 @@ func (k Keeper) persistMatchedOrders(
// Distribute the fee amount from subacounts module to fee collector and rev share accounts
bigTotalFeeQuoteQuantums := new(big.Int).Add(bigTakerFeeQuoteQuantums, bigMakerFeeQuoteQuantums)
revSharesForFill, err := k.revshareKeeper.GetAllRevShares(ctx, fillForProcess, affiliatesWhitelistMap)

if err != nil {
revSharesForFill = revsharetypes.RevSharesForFill{}
log.ErrorLog(ctx, "error getting rev shares for fill", "error", err)
}
if revSharesForFill.AffiliateRevShare != nil {
affiliateRevSharesQuoteQuantums = revSharesForFill.AffiliateRevShare.QuoteQuantums
}

if err != nil {
return takerUpdateResult, makerUpdateResult, affiliateRevSharesQuoteQuantums, err
}
if err := k.subaccountsKeeper.DistributeFees(
ctx,
assettypes.AssetUsdc.Id,
Expand Down
41 changes: 21 additions & 20 deletions protocol/x/revshare/keeper/revshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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"
Expand Down Expand Up @@ -163,25 +162,28 @@ func (k Keeper) GetAllRevShares(
feeSourceToRevSharePpm := make(map[types.RevShareFeeSource]uint32)
feeSourceToQuoteQuantums[types.REV_SHARE_FEE_SOURCE_TAKER_FEE] = big.NewInt(0)
feeSourceToRevSharePpm[types.REV_SHARE_FEE_SOURCE_TAKER_FEE] = 0
feeSourceToQuoteQuantums[types.REV_SHARE_FEE_SOURCE_NET_FEE] = big.NewInt(0)
feeSourceToRevSharePpm[types.REV_SHARE_FEE_SOURCE_NET_FEE] = 0
feeSourceToQuoteQuantums[types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE] = big.NewInt(0)
feeSourceToRevSharePpm[types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE] = 0

totalFeesShared := big.NewInt(0)
takerFees := fill.TakerFeeQuoteQuantums
makerFees := fill.MakerFeeQuoteQuantums
netFees := big.NewInt(0).Add(takerFees, makerFees)

affiliateRevShares, err := k.getAffiliateRevShares(ctx, fill, affiliatesWhitelistMap)
affiliateRevShares, affiliateFeesShared, err := k.getAffiliateRevShares(ctx, fill, affiliatesWhitelistMap)
if err != nil {
return types.RevSharesForFill{}, err
}
netFeesSubAffiliateFeesShared := new(big.Int).Sub(netFees, affiliateFeesShared)
if netFeesSubAffiliateFeesShared.Sign() <= 0 {
return types.RevSharesForFill{}, types.ErrAffiliateFeesSharedExceedsNetFees
}

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

marketMapperRevShares, err := k.getMarketMapperRevShare(ctx, fill.MarketId, netFees)
marketMapperRevShares, err := k.getMarketMapperRevShare(ctx, fill.MarketId, netFeesSubAffiliateFeesShared)
if err != nil {
return types.RevSharesForFill{}, err
}
Expand All @@ -208,8 +210,7 @@ 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")
return types.RevSharesForFill{}, types.ErrTotalFeesSharedExceedsNetFees
}

return types.RevSharesForFill{
Expand All @@ -224,20 +225,20 @@ func (k Keeper) getAffiliateRevShares(
ctx sdk.Context,
fill clobtypes.FillForProcess,
affiliatesWhitelistMap map[string]uint32,
) ([]types.RevShare, error) {
) ([]types.RevShare, *big.Int, error) {
takerAddr := fill.TakerAddr
takerFee := fill.TakerFeeQuoteQuantums
if fill.MonthlyRollingTakerVolumeQuantums >= types.MaxReferee30dVolumeForAffiliateShareQuantums {
return nil, nil
return nil, big.NewInt(0), nil
}

takerAffiliateAddr, feeSharePpm, exists, err := k.affiliatesKeeper.GetTakerFeeShare(
ctx, takerAddr, affiliatesWhitelistMap)
if err != nil {
return nil, err
return nil, big.NewInt(0), err
}
if !exists {
return nil, nil
return nil, big.NewInt(0), nil
}
feesShared := lib.BigMulPpm(takerFee, lib.BigU(feeSharePpm), false)
return []types.RevShare{
Expand All @@ -248,23 +249,23 @@ func (k Keeper) getAffiliateRevShares(
QuoteQuantums: feesShared,
RevSharePpm: feeSharePpm,
},
}, nil
}, feesShared, nil
}

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_FEE,
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE,
RevShareType: types.REV_SHARE_TYPE_UNCONDITIONAL,
QuoteQuantums: feeShared,
RevSharePpm: revShare.SharePpm,
Expand All @@ -277,7 +278,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,10 +289,10 @@ 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_FEE,
RevShareFeeSource: types.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE,
RevShareType: types.REV_SHARE_TYPE_MARKET_MAPPER,
QuoteQuantums: marketMapperRevshareAmount,
RevSharePpm: revenueSharePpm,
Expand Down
Loading

0 comments on commit 1659f93

Please sign in to comment.