From f162535954dec54e90f77f6f1c942b94634155c1 Mon Sep 17 00:00:00 2001 From: affan Date: Mon, 23 Sep 2024 12:04:02 -0400 Subject: [PATCH] address comments --- protocol/x/revshare/keeper/revshare.go | 30 +- protocol/x/revshare/keeper/revshare_test.go | 20 +- .../x/subaccounts/keeper/transfer_test.go | 262 +++++++++--------- 3 files changed, 162 insertions(+), 150 deletions(-) diff --git a/protocol/x/revshare/keeper/revshare.go b/protocol/x/revshare/keeper/revshare.go index 631d3f7ad9..9ed025f7fe 100644 --- a/protocol/x/revshare/keeper/revshare.go +++ b/protocol/x/revshare/keeper/revshare.go @@ -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" @@ -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...) @@ -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{ @@ -253,7 +261,7 @@ 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) @@ -261,7 +269,7 @@ func (k Keeper) getUnconditionalRevShares( 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, @@ -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) @@ -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, diff --git a/protocol/x/revshare/keeper/revshare_test.go b/protocol/x/revshare/keeper/revshare_test.go index 4c59d5b489..5bc3a3b48d 100644 --- a/protocol/x/revshare/keeper/revshare_test.go +++ b/protocol/x/revshare/keeper/revshare_test.go @@ -327,28 +327,28 @@ 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, }, }, @@ -356,16 +356,16 @@ 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, }, 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{ @@ -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{ { diff --git a/protocol/x/subaccounts/keeper/transfer_test.go b/protocol/x/subaccounts/keeper/transfer_test.go index 6c0bd2a014..01d895c457 100644 --- a/protocol/x/subaccounts/keeper/transfer_test.go +++ b/protocol/x/subaccounts/keeper/transfer_test.go @@ -20,7 +20,6 @@ import ( keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper" sample_testutil "github.com/dydxprotocol/v4-chain/protocol/testutil/sample" testutil "github.com/dydxprotocol/v4-chain/protocol/testutil/util" - affiliatetypes "github.com/dydxprotocol/v4-chain/protocol/x/affiliates/types" asstypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types" clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" revsharetypes "github.com/dydxprotocol/v4-chain/protocol/x/revshare/types" @@ -1172,8 +1171,6 @@ func TestTransferFundsFromSubaccountToSubaccount_Failure(t *testing.T) { } func TestDistributeFees(t *testing.T) { - refereeAccAddr := constants.AliceAccAddress.String() - defaultUnconditionalRevSharePpm := uint32(100_000) tests := map[string]struct { skipSetUpUsdc bool @@ -1190,11 +1187,8 @@ func TestDistributeFees(t *testing.T) { affiliateRevShareAcctAddr string marketMapperRevShareAcctAddr string unconditionalRevShareAcctAddr string - - // Revenue share details - revshareParams revsharetypes.MarketMapperRevenueShareParams - setRevenueShare bool - revShareExpiration int64 + revShare revsharetypes.RevSharesForFill + revShareExpiration int64 // Expectations. expectedErr error @@ -1219,15 +1213,13 @@ func TestDistributeFees(t *testing.T) { MarketId: uint32(0), MonthlyRollingTakerVolumeQuantums: 1_000_000, }, - collateralPoolAddr: types.ModuleAddress, - affiliateRevShareAcctAddr: "", - marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), - unconditionalRevShareAcctAddr: "", - expectedSubaccountsModuleAccBalance: big.NewInt(100), // 600 - 500 - expectedFeeModuleAccBalance: big.NewInt(3000), // 500 + 2500 - revshareParams: revsharetypes.MarketMapperRevenueShareParams{ - Address: constants.AliceAccAddress.String(), - }, + collateralPoolAddr: types.ModuleAddress, + affiliateRevShareAcctAddr: "", + marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), + unconditionalRevShareAcctAddr: "", + expectedSubaccountsModuleAccBalance: big.NewInt(100), // 600 - 500 + expectedFeeModuleAccBalance: big.NewInt(3000), // 500 + 2500 + revShare: revsharetypes.RevSharesForFill{}, expectedMarketMapperAccBalance: big.NewInt(0), expectedAffiliateAccBalance: big.NewInt(0), expectedUnconditionalRevShareAccBalance: big.NewInt(0), @@ -1249,15 +1241,13 @@ func TestDistributeFees(t *testing.T) { collateralPoolAddr: authtypes.NewModuleAddress( types.ModuleName + ":" + lib.IntToString(3), ), - affiliateRevShareAcctAddr: "", - marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), - unconditionalRevShareAcctAddr: "", - expectedSubaccountsModuleAccBalance: big.NewInt(100), // 600 - 500 - expectedFeeModuleAccBalance: big.NewInt(3000), // 500 + 2500 - marketMapperAccBalance: big.NewInt(0), - revshareParams: revsharetypes.MarketMapperRevenueShareParams{ - Address: constants.AliceAccAddress.String(), - }, + affiliateRevShareAcctAddr: "", + marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), + unconditionalRevShareAcctAddr: "", + expectedSubaccountsModuleAccBalance: big.NewInt(100), // 600 - 500 + expectedFeeModuleAccBalance: big.NewInt(3000), // 500 + 2500 + marketMapperAccBalance: big.NewInt(0), + revShare: revsharetypes.RevSharesForFill{}, expectedMarketMapperAccBalance: big.NewInt(0), expectedAffiliateAccBalance: big.NewInt(0), expectedUnconditionalRevShareAccBalance: big.NewInt(0), @@ -1276,17 +1266,15 @@ func TestDistributeFees(t *testing.T) { MarketId: uint32(3), MonthlyRollingTakerVolumeQuantums: 1_000_000, }, - collateralPoolAddr: types.ModuleAddress, - affiliateRevShareAcctAddr: "", - marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), - unconditionalRevShareAcctAddr: "", - expectedSubaccountsModuleAccBalance: big.NewInt(300), - expectedFeeModuleAccBalance: big.NewInt(2500), - expectedErr: sdkerrors.ErrInsufficientFunds, - marketMapperAccBalance: big.NewInt(0), - revshareParams: revsharetypes.MarketMapperRevenueShareParams{ - Address: constants.AliceAccAddress.String(), - }, + collateralPoolAddr: types.ModuleAddress, + affiliateRevShareAcctAddr: "", + marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), + unconditionalRevShareAcctAddr: "", + expectedSubaccountsModuleAccBalance: big.NewInt(300), + expectedFeeModuleAccBalance: big.NewInt(2500), + expectedErr: sdkerrors.ErrInsufficientFunds, + marketMapperAccBalance: big.NewInt(0), + revShare: revsharetypes.RevSharesForFill{}, expectedMarketMapperAccBalance: big.NewInt(0), expectedAffiliateAccBalance: big.NewInt(0), expectedUnconditionalRevShareAccBalance: big.NewInt(0), @@ -1308,16 +1296,14 @@ func TestDistributeFees(t *testing.T) { collateralPoolAddr: authtypes.NewModuleAddress( types.ModuleName + ":" + lib.IntToString(3), ), - affiliateRevShareAcctAddr: "", - marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), - unconditionalRevShareAcctAddr: "", - expectedSubaccountsModuleAccBalance: big.NewInt(300), - expectedFeeModuleAccBalance: big.NewInt(2500), - expectedErr: sdkerrors.ErrInsufficientFunds, - marketMapperAccBalance: big.NewInt(0), - revshareParams: revsharetypes.MarketMapperRevenueShareParams{ - Address: constants.AliceAccAddress.String(), - }, + affiliateRevShareAcctAddr: "", + marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), + unconditionalRevShareAcctAddr: "", + expectedSubaccountsModuleAccBalance: big.NewInt(300), + expectedFeeModuleAccBalance: big.NewInt(2500), + expectedErr: sdkerrors.ErrInsufficientFunds, + marketMapperAccBalance: big.NewInt(0), + revShare: revsharetypes.RevSharesForFill{}, expectedMarketMapperAccBalance: big.NewInt(0), expectedAffiliateAccBalance: big.NewInt(0), expectedUnconditionalRevShareAccBalance: big.NewInt(0), @@ -1337,17 +1323,15 @@ func TestDistributeFees(t *testing.T) { MarketId: uint32(3), MonthlyRollingTakerVolumeQuantums: 1_000_000, }, - collateralPoolAddr: types.ModuleAddress, - affiliateRevShareAcctAddr: "", - marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), - unconditionalRevShareAcctAddr: "", - expectedErr: asstypes.ErrAssetDoesNotExist, - expectedSubaccountsModuleAccBalance: big.NewInt(500), - expectedFeeModuleAccBalance: big.NewInt(1500), - marketMapperAccBalance: big.NewInt(0), - revshareParams: revsharetypes.MarketMapperRevenueShareParams{ - Address: constants.AliceAccAddress.String(), - }, + collateralPoolAddr: types.ModuleAddress, + affiliateRevShareAcctAddr: "", + marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), + unconditionalRevShareAcctAddr: "", + expectedErr: asstypes.ErrAssetDoesNotExist, + expectedSubaccountsModuleAccBalance: big.NewInt(500), + expectedFeeModuleAccBalance: big.NewInt(1500), + marketMapperAccBalance: big.NewInt(0), + revShare: revsharetypes.RevSharesForFill{}, expectedMarketMapperAccBalance: big.NewInt(0), expectedAffiliateAccBalance: big.NewInt(0), expectedUnconditionalRevShareAccBalance: big.NewInt(0), @@ -1366,17 +1350,15 @@ func TestDistributeFees(t *testing.T) { MarketId: uint32(3), MonthlyRollingTakerVolumeQuantums: 1_000_000, }, - collateralPoolAddr: types.ModuleAddress, - affiliateRevShareAcctAddr: "", - marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), - unconditionalRevShareAcctAddr: "", - expectedErr: types.ErrAssetTransferThroughBankNotImplemented, - expectedSubaccountsModuleAccBalance: big.NewInt(500), - expectedFeeModuleAccBalance: big.NewInt(1500), - marketMapperAccBalance: big.NewInt(0), - revshareParams: revsharetypes.MarketMapperRevenueShareParams{ - Address: constants.AliceAccAddress.String(), - }, + collateralPoolAddr: types.ModuleAddress, + affiliateRevShareAcctAddr: "", + marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), + unconditionalRevShareAcctAddr: "", + expectedErr: types.ErrAssetTransferThroughBankNotImplemented, + expectedSubaccountsModuleAccBalance: big.NewInt(500), + expectedFeeModuleAccBalance: big.NewInt(1500), + marketMapperAccBalance: big.NewInt(0), + revShare: revsharetypes.RevSharesForFill{}, expectedMarketMapperAccBalance: big.NewInt(0), expectedAffiliateAccBalance: big.NewInt(0), expectedUnconditionalRevShareAccBalance: big.NewInt(0), @@ -1407,12 +1389,26 @@ func TestDistributeFees(t *testing.T) { affiliateRevShareAcctAddr: "", marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), unconditionalRevShareAcctAddr: "", - revshareParams: revsharetypes.MarketMapperRevenueShareParams{ - Address: constants.AliceAccAddress.String(), - RevenueSharePpm: 100_000, // 10% - ValidDays: 240, + revShare: revsharetypes.RevSharesForFill{ + AffiliateRevShare: nil, + FeeSourceToQuoteQuantums: map[revsharetypes.RevShareFeeSource]*big.Int{ + revsharetypes.REV_SHARE_FEE_SOURCE_TAKER_FEE: big.NewInt(0), + revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: big.NewInt(50), + }, + FeeSourceToRevSharePpm: map[revsharetypes.RevShareFeeSource]uint32{ + revsharetypes.REV_SHARE_FEE_SOURCE_TAKER_FEE: 0, // 0% + revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: 100_000, // 10% + }, + AllRevShares: []revsharetypes.RevShare{ + { + Recipient: constants.AliceAccAddress.String(), + RevShareFeeSource: revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE, + RevShareType: revsharetypes.REV_SHARE_TYPE_MARKET_MAPPER, + QuoteQuantums: big.NewInt(50), + RevSharePpm: 100_000, // 10% + }, + }, }, - setRevenueShare: true, revShareExpiration: 100, }, "success - market mapper rev share expired": { @@ -1441,13 +1437,18 @@ func TestDistributeFees(t *testing.T) { collateralPoolAddr: authtypes.NewModuleAddress( types.ModuleName + ":" + lib.IntToString(4), ), - - revshareParams: revsharetypes.MarketMapperRevenueShareParams{ - Address: constants.AliceAccAddress.String(), - RevenueSharePpm: 100_000, // 10% - ValidDays: 240, + revShare: revsharetypes.RevSharesForFill{ + AffiliateRevShare: nil, + FeeSourceToQuoteQuantums: map[revsharetypes.RevShareFeeSource]*big.Int{ + revsharetypes.REV_SHARE_FEE_SOURCE_TAKER_FEE: big.NewInt(0), + revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: big.NewInt(0), + }, + FeeSourceToRevSharePpm: map[revsharetypes.RevShareFeeSource]uint32{ + revsharetypes.REV_SHARE_FEE_SOURCE_TAKER_FEE: 0, // 0% + revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: 0, // 0% + }, + AllRevShares: []revsharetypes.RevShare{}, }, - setRevenueShare: true, revShareExpiration: -10, }, "success - market mapper rev share rounded down to 0": { @@ -1476,12 +1477,18 @@ func TestDistributeFees(t *testing.T) { affiliateRevShareAcctAddr: "", marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), unconditionalRevShareAcctAddr: "", - revshareParams: revsharetypes.MarketMapperRevenueShareParams{ - Address: constants.AliceAccAddress.String(), - RevenueSharePpm: 100_000, // 10% - ValidDays: 240, + revShare: revsharetypes.RevSharesForFill{ + AffiliateRevShare: nil, + FeeSourceToQuoteQuantums: map[revsharetypes.RevShareFeeSource]*big.Int{ + revsharetypes.REV_SHARE_FEE_SOURCE_TAKER_FEE: big.NewInt(0), + revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: big.NewInt(0), + }, + FeeSourceToRevSharePpm: map[revsharetypes.RevShareFeeSource]uint32{ + revsharetypes.REV_SHARE_FEE_SOURCE_TAKER_FEE: 0, // 0% + revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: 0, // 0% + }, + AllRevShares: []revsharetypes.RevShare{}, }, - setRevenueShare: true, revShareExpiration: 100, }, "success - distribute fees to market mapper, unconditional rev share, affiliate and fee collector": { @@ -1510,12 +1517,46 @@ func TestDistributeFees(t *testing.T) { affiliateRevShareAcctAddr: constants.BobAccAddress.String(), marketMapperRevShareAcctAddr: constants.AliceAccAddress.String(), unconditionalRevShareAcctAddr: constants.CarlAccAddress.String(), - revshareParams: revsharetypes.MarketMapperRevenueShareParams{ - Address: constants.AliceAccAddress.String(), - RevenueSharePpm: 100_000, // 10% - ValidDays: 240, + revShare: revsharetypes.RevSharesForFill{ + AffiliateRevShare: &revsharetypes.RevShare{ + Recipient: constants.BobAccAddress.String(), + RevShareFeeSource: revsharetypes.REV_SHARE_FEE_SOURCE_TAKER_FEE, + RevShareType: revsharetypes.REV_SHARE_TYPE_AFFILIATE, + QuoteQuantums: big.NewInt(12), + RevSharePpm: 50_000, // 5% + }, + FeeSourceToQuoteQuantums: map[revsharetypes.RevShareFeeSource]*big.Int{ + revsharetypes.REV_SHARE_FEE_SOURCE_TAKER_FEE: big.NewInt(12), + revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: big.NewInt(96), + }, + FeeSourceToRevSharePpm: map[revsharetypes.RevShareFeeSource]uint32{ + revsharetypes.REV_SHARE_FEE_SOURCE_TAKER_FEE: 50_000, // 5% + revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE: 200_000, // 20% + }, + AllRevShares: []revsharetypes.RevShare{ + { + Recipient: constants.BobAccAddress.String(), + RevShareFeeSource: revsharetypes.REV_SHARE_FEE_SOURCE_TAKER_FEE, + RevShareType: revsharetypes.REV_SHARE_TYPE_AFFILIATE, + QuoteQuantums: big.NewInt(12), + RevSharePpm: 50_000, // 5% + }, + { + Recipient: constants.AliceAccAddress.String(), + RevShareFeeSource: revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE, + RevShareType: revsharetypes.REV_SHARE_TYPE_MARKET_MAPPER, + QuoteQuantums: big.NewInt(48), + RevSharePpm: 100_000, // 10% + }, + { + Recipient: constants.CarlAccAddress.String(), + RevShareFeeSource: revsharetypes.REV_SHARE_FEE_SOURCE_NET_PROTOCOL_REVENUE, + RevShareType: revsharetypes.REV_SHARE_TYPE_UNCONDITIONAL, + QuoteQuantums: big.NewInt(48), + RevSharePpm: 100_000, // 10% + }, + }, }, - setRevenueShare: true, revShareExpiration: 100, }, // TODO(DEC-715): Add more test for non-USDC assets, after asset update @@ -1525,7 +1566,7 @@ func TestDistributeFees(t *testing.T) { for name, tc := range tests { t.Run(name, func(t *testing.T) { ctx, keeper, pricesKeeper, perpetualsKeeper, accountKeeper, - bankKeeper, assetsKeeper, _, revShareKeeper, affiliatesKeeper, _ := + bankKeeper, assetsKeeper, _, _, _, _ := keepertest.SubaccountsKeepers(t, true) keepertest.CreateTestMarkets(t, ctx, pricesKeeper) keepertest.CreateTestLiquidityTiers(t, ctx, perpetualsKeeper) @@ -1609,44 +1650,7 @@ func TestDistributeFees(t *testing.T) { require.NoError(t, err) } - // Set market mapper revenue share - err = revShareKeeper.SetMarketMapperRevenueShareParams( - ctx, - tc.revshareParams, - ) - require.NoError(t, err) - - if tc.setRevenueShare { - revShareKeeper.SetMarketMapperRevShareDetails( - ctx, - tc.fill.MarketId, - revsharetypes.MarketMapperRevShareDetails{ - ExpirationTs: uint64(ctx.BlockTime().Unix() + tc.revShareExpiration), - }, - ) - } - if tc.affiliateRevShareAcctAddr != "" { - err := affiliatesKeeper.RegisterAffiliate(ctx, refereeAccAddr, tc.affiliateRevShareAcctAddr) - require.NoError(t, err) - } - if tc.unconditionalRevShareAcctAddr != "" { - err := affiliatesKeeper.UpdateAffiliateTiers(ctx, affiliatetypes.DefaultAffiliateTiers) - require.NoError(t, err) - revShareKeeper.SetUnconditionalRevShareConfigParams(ctx, - revsharetypes.UnconditionalRevShareConfig{ - Configs: []revsharetypes.UnconditionalRevShareConfig_RecipientConfig{ - { - Address: tc.unconditionalRevShareAcctAddr, - SharePpm: defaultUnconditionalRevSharePpm, - }, - }, - }) - } - affiliateWhitelistMap, err := affiliatesKeeper.GetAffiliateWhitelistMap(ctx) - require.NoError(t, err) - revSharesForFill, err := revShareKeeper.GetAllRevShares(ctx, tc.fill, affiliateWhitelistMap) - require.NoError(t, err) - err = keeper.DistributeFees(ctx, tc.asset.Id, revSharesForFill, tc.fill) + err = keeper.DistributeFees(ctx, tc.asset.Id, tc.revShare, tc.fill) if tc.expectedErr != nil { require.ErrorIs(t,