Skip to content

Commit

Permalink
[OTE-687] Bump fee tiers for referees (#2217)
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 authored Sep 16, 2024
1 parent 46a1c88 commit 9577fe0
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 9 deletions.
1 change: 1 addition & 0 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,7 @@ func New(
app.FeeTiersKeeper = feetiersmodulekeeper.NewKeeper(
appCodec,
app.StatsKeeper,
app.AffiliatesKeeper,
keys[feetiersmoduletypes.StoreKey],
// set the governance and delaymsg module accounts as the authority for conducting upgrades
[]string{
Expand Down
1 change: 1 addition & 0 deletions protocol/testutil/keeper/clob.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func NewClobKeepersTestContextWithUninitializedMemStore(
stateStore,
ks.StatsKeeper,
ks.VaultKeeper,
affiliatesKeeper,
db,
cdc,
)
Expand Down
3 changes: 3 additions & 0 deletions protocol/testutil/keeper/feetiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
affiliateskeeper "github.com/dydxprotocol/v4-chain/protocol/x/affiliates/keeper"
delaymsgtypes "github.com/dydxprotocol/v4-chain/protocol/x/delaymsg/types"
"github.com/dydxprotocol/v4-chain/protocol/x/feetiers/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/feetiers/types"
Expand All @@ -18,6 +19,7 @@ func createFeeTiersKeeper(
stateStore storetypes.CommitMultiStore,
statsKeeper *statskeeper.Keeper,
vaultKeeper *vaultkeeper.Keeper,
affiliatesKeeper *affiliateskeeper.Keeper,
db *dbm.MemDB,
cdc *codec.ProtoCodec,
) (*keeper.Keeper, storetypes.StoreKey) {
Expand All @@ -34,6 +36,7 @@ func createFeeTiersKeeper(
k := keeper.NewKeeper(
cdc,
statsKeeper,
affiliatesKeeper,
storeKey,
authorities,
)
Expand Down
1 change: 1 addition & 0 deletions protocol/testutil/keeper/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func ListingKeepers(
stateStore,
statsKeeper,
vaultKeeper,
affiliatesKeeper,
db,
cdc,
)
Expand Down
1 change: 1 addition & 0 deletions protocol/testutil/keeper/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func RewardsKeepers(
stateStore,
statsKeeper,
vaultKeeper,
affiliatesKeeper,
db,
cdc,
)
Expand Down
28 changes: 19 additions & 9 deletions protocol/x/feetiers/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,28 @@ import (

type (
Keeper struct {
cdc codec.BinaryCodec
statsKeeper types.StatsKeeper
vaultKeeper types.VaultKeeper
storeKey storetypes.StoreKey
authorities map[string]struct{}
cdc codec.BinaryCodec
statsKeeper types.StatsKeeper
vaultKeeper types.VaultKeeper
storeKey storetypes.StoreKey
authorities map[string]struct{}
affiliatesKeeper types.AffiliatesKeeper
}
)

func NewKeeper(
cdc codec.BinaryCodec,
statsKeeper types.StatsKeeper,
affiliatesKeeper types.AffiliatesKeeper,
storeKey storetypes.StoreKey,
authorities []string,
) *Keeper {
return &Keeper{
cdc: cdc,
statsKeeper: statsKeeper,
storeKey: storeKey,
authorities: lib.UniqueSliceToSet(authorities),
cdc: cdc,
statsKeeper: statsKeeper,
storeKey: storeKey,
authorities: lib.UniqueSliceToSet(authorities),
affiliatesKeeper: affiliatesKeeper,
}
}

Expand Down Expand Up @@ -97,6 +100,13 @@ func (k Keeper) getUserFeeTier(ctx sdk.Context, address string) (uint32, *types.
idx = uint32(i)
}

if idx < types.RefereeStartingFeeTier {
_, hasReferree := k.affiliatesKeeper.GetReferredBy(ctx, address)
if hasReferree {
idx = types.RefereeStartingFeeTier
}
}

return idx, tiers[idx]
}

Expand Down
126 changes: 126 additions & 0 deletions protocol/x/feetiers/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package keeper_test
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
affiliateskeeper "github.com/dydxprotocol/v4-chain/protocol/x/affiliates/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/feetiers/types"
statskeeper "github.com/dydxprotocol/v4-chain/protocol/x/stats/keeper"
stattypes "github.com/dydxprotocol/v4-chain/protocol/x/stats/types"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -131,6 +134,129 @@ func TestGetPerpetualFeePpm(t *testing.T) {
}
}

func TestGetPerpetualFeePpm_Referral(t *testing.T) {
testFeePerpetualParams := types.PerpetualFeeParams{
Tiers: []*types.PerpetualFeeTier{
{
Name: "1",
TakerFeePpm: 10,
MakerFeePpm: 1,
},
{
Name: "2",
AbsoluteVolumeRequirement: 1_000,
TakerFeePpm: 20,
MakerFeePpm: 2,
},
{
Name: "3",
AbsoluteVolumeRequirement: 1_000_000_000,
MakerVolumeShareRequirementPpm: 500_000,
TakerFeePpm: 30,
MakerFeePpm: 3,
},
{
Name: "4",
AbsoluteVolumeRequirement: 2_000_000_000,
MakerVolumeShareRequirementPpm: 600_000,
TakerFeePpm: 40,
MakerFeePpm: 4,
},
{
Name: "5",
AbsoluteVolumeRequirement: 5_000_000_000,
MakerVolumeShareRequirementPpm: 700_000,
TakerFeePpm: 50,
MakerFeePpm: 5,
},
},
}
tests := map[string]struct {
expectedTakerFeePpm int32
setup func(ctx sdk.Context, affiliatesKeeper *affiliateskeeper.Keeper, statsKeeper *statskeeper.Keeper)
}{
"regular user, first tier, no referral": {
expectedTakerFeePpm: 10,
setup: func(ctx sdk.Context, affiliatesKeeper *affiliateskeeper.Keeper, statsKeeper *statskeeper.Keeper) {
statsKeeper.SetUserStats(ctx, constants.AliceAccAddress.String(), &stattypes.UserStats{
TakerNotional: 10,
MakerNotional: 10,
})
statsKeeper.SetGlobalStats(ctx, &stattypes.GlobalStats{
NotionalTraded: 10_000,
})
},
},
"regular user, referral": {
expectedTakerFeePpm: 30,
setup: func(ctx sdk.Context, affiliatesKeeper *affiliateskeeper.Keeper, statsKeeper *statskeeper.Keeper) {
statsKeeper.SetUserStats(ctx, constants.AliceAccAddress.String(), &stattypes.UserStats{
TakerNotional: 10,
MakerNotional: 10,
})
statsKeeper.SetGlobalStats(ctx, &stattypes.GlobalStats{
NotionalTraded: 10_000,
})

err := affiliatesKeeper.RegisterAffiliate(ctx, constants.AliceAccAddress.String(), constants.BobAccAddress.String())
require.NoError(t, err)
},
},
"regular user, referral, already in tier 3": {
expectedTakerFeePpm: 30,
setup: func(ctx sdk.Context, affiliatesKeeper *affiliateskeeper.Keeper, statsKeeper *statskeeper.Keeper) {
statsKeeper.SetUserStats(ctx, constants.AliceAccAddress.String(), &stattypes.UserStats{
TakerNotional: 10,
MakerNotional: 1_000_000_000,
})
statsKeeper.SetGlobalStats(ctx, &stattypes.GlobalStats{
NotionalTraded: 1_000_000_000,
})

err := affiliatesKeeper.RegisterAffiliate(ctx, constants.AliceAccAddress.String(), constants.BobAccAddress.String())
require.NoError(t, err)
},
},
"regular user, referral, above tier 3": {
expectedTakerFeePpm: 40,
setup: func(ctx sdk.Context, affiliatesKeeper *affiliateskeeper.Keeper, statsKeeper *statskeeper.Keeper) {
statsKeeper.SetUserStats(ctx, constants.AliceAccAddress.String(), &stattypes.UserStats{
TakerNotional: 10,
MakerNotional: 2_000_000_001,
})
statsKeeper.SetGlobalStats(ctx, &stattypes.GlobalStats{
NotionalTraded: 3_000_000_000,
})

err := affiliatesKeeper.RegisterAffiliate(ctx, constants.AliceAccAddress.String(), constants.BobAccAddress.String())
require.NoError(t, err)
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.FeeTiersKeeper
err := k.SetPerpetualFeeParams(
ctx,
testFeePerpetualParams,
)
require.NoError(t, err)

statsKeeper := tApp.App.StatsKeeper
affiliatesKeeper := tApp.App.AffiliatesKeeper
if tc.setup != nil {
tc.setup(ctx, &affiliatesKeeper, &statsKeeper)
}

require.Equal(t, tc.expectedTakerFeePpm,
k.GetPerpetualFeePpm(ctx, constants.AliceAccAddress.String(), true))
})
}
}

func TestGetMaxMakerRebate(t *testing.T) {
tests := map[string]struct {
expectedLowestMakerFee int32
Expand Down
7 changes: 7 additions & 0 deletions protocol/x/feetiers/types/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

const (
// Tiers are 0-indexed.
// i.e the first tier is 0, the second tier is 1, etc.
RefereeStartingFeeTier uint32 = 2
)
5 changes: 5 additions & 0 deletions protocol/x/feetiers/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ type StatsKeeper interface {
type VaultKeeper interface {
IsVault(ctx sdk.Context, address string) bool
}

// AffiliatesKeeper defines the expected affiliates keeper.
type AffiliatesKeeper interface {
GetReferredBy(ctx sdk.Context, referee string) (string, bool)
}

0 comments on commit 9577fe0

Please sign in to comment.