Skip to content

Commit

Permalink
Bump fee tiers for referee
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 committed Sep 5, 2024
1 parent 3faadf9 commit e610afc
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 19 deletions.
21 changes: 11 additions & 10 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,9 +1023,20 @@ func New(
)
statsModule := statsmodule.NewAppModule(appCodec, app.StatsKeeper)

app.AffiliatesKeeper = *affiliatesmodulekeeper.NewKeeper(
appCodec,
keys[affiliatesmoduletypes.StoreKey],
[]string{
lib.GovModuleAddress.String(),
},
app.StatsKeeper,
)
affiliatesModule := affiliatesmodule.NewAppModule(appCodec, app.AffiliatesKeeper)

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 Expand Up @@ -1198,16 +1209,6 @@ func New(
)
accountplusModule := accountplusmodule.NewAppModule(appCodec, app.AccountPlusKeeper)

app.AffiliatesKeeper = *affiliatesmodulekeeper.NewKeeper(
appCodec,
keys[affiliatesmoduletypes.StoreKey],
[]string{
lib.GovModuleAddress.String(),
},
app.StatsKeeper,
)
affiliatesModule := affiliatesmodule.NewAppModule(appCodec, app.AffiliatesKeeper)

/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down
28 changes: 28 additions & 0 deletions protocol/testutil/keeper/affiliates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package keeper

import (
storetypes "cosmossdk.io/store/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/codec"
affiliateskeeper "github.com/dydxprotocol/v4-chain/protocol/x/affiliates/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/revshare/types"
statskeeper "github.com/dydxprotocol/v4-chain/protocol/x/stats/keeper"
)

func createAffiliatesKeeper(
stateStore storetypes.CommitMultiStore,
db *dbm.MemDB,
cdc *codec.ProtoCodec,
statsKeeper *statskeeper.Keeper,
) (*affiliateskeeper.Keeper, storetypes.StoreKey) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)

k := affiliateskeeper.NewKeeper(
cdc,
storeKey,
[]string{},
statsKeeper,
)
return k, storeKey
}
7 changes: 7 additions & 0 deletions protocol/testutil/keeper/clob.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,17 @@ func NewClobKeepersTestContextWithUninitializedMemStore(
cdc,
indexerEventsTransientStoreKey,
)
affiliatesKeeper, _ := createAffiliatesKeeper(
stateStore,
db,
cdc,
ks.StatsKeeper,
)
ks.FeeTiersKeeper, _ = createFeeTiersKeeper(
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
7 changes: 7 additions & 0 deletions protocol/testutil/keeper/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,17 @@ func ListingKeepers(
cdc,
transientStoreKey,
)
affiliatesKeeper, _ := createAffiliatesKeeper(
stateStore,
db,
cdc,
statsKeeper,
)
feeTiersKeeper, _ := createFeeTiersKeeper(
stateStore,
statsKeeper,
vaultKeeper,
affiliatesKeeper,
db,
cdc,
)
Expand Down
7 changes: 7 additions & 0 deletions protocol/testutil/keeper/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,17 @@ func RewardsKeepers(
cdc,
transientStoreKey,
)
affiliatesKeeper, _ := createAffiliatesKeeper(
stateStore,
db,
cdc,
statsKeeper,
)
feetiersKeeper, _ = createFeeTiersKeeper(
stateStore,
statsKeeper,
vaultKeeper,
affiliatesKeeper,
db,
cdc,
)
Expand Down
30 changes: 21 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,15 @@ func (k Keeper) getUserFeeTier(ctx sdk.Context, address string) (uint32, *types.
idx = uint32(i)
}

// Bump up to RefereeStartingFeeTier if the user is referred by an affiliate.
// We subtract 1 because the fee tiers are 1-indexed.
if idx < types.RefereeStartingFeeTier-1 {
_, hasReferree := k.affiliatesKeeper.GetReferredBy(ctx, address)
if hasReferree {
idx = types.RefereeStartingFeeTier - 1
}
}

return idx, tiers[idx]
}

Expand Down
73 changes: 73 additions & 0 deletions protocol/x/feetiers/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ 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"
stattypes "github.com/dydxprotocol/v4-chain/protocol/x/stats/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -131,6 +133,77 @@ func TestGetPerpetualFeePpm(t *testing.T) {
}
}

func TestGetPerpetualFeePpm_Referral(t *testing.T) {
tests := map[string]struct {
expectedTakerFeePpm int32
setup func(ctx sdk.Context, affiliatesKeeper *affiliateskeeper.Keeper)
}{
"regular user, first tier, no referral": {
expectedTakerFeePpm: 10,
setup: func(ctx sdk.Context, affiliatesKeeper *affiliateskeeper.Keeper) {},
},
"regular user, first tier, referral": {
expectedTakerFeePpm: 30,
setup: func(ctx sdk.Context, affiliatesKeeper *affiliateskeeper.Keeper) {
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()
tApp.App.VaultKeeper.AddVaultToAddressStore(ctx, constants.Vault_Clob0)
k := tApp.App.FeeTiersKeeper
err := k.SetPerpetualFeeParams(
ctx,
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,
},
},
},
)
require.NoError(t, err)

statsKeeper := tApp.App.StatsKeeper
statsKeeper.SetUserStats(ctx, constants.AliceAccAddress.String(), &stattypes.UserStats{
TakerNotional: 10,
MakerNotional: 10,
})
statsKeeper.SetGlobalStats(ctx, &stattypes.GlobalStats{
NotionalTraded: 10_000,
})

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

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
5 changes: 5 additions & 0 deletions protocol/x/feetiers/types/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

const (
RefereeStartingFeeTier uint32 = 3
)
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 e610afc

Please sign in to comment.