Skip to content

Commit

Permalink
Add whitelisted LPs
Browse files Browse the repository at this point in the history
  • Loading branch information
jcompagni10 committed Jan 9, 2025
1 parent 921dc82 commit 6c764f9
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 26 deletions.
1 change: 1 addition & 0 deletions proto/neutron/dex/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ message Params {
];
uint64 max_jits_per_block = 4;
uint64 good_til_purge_allowance = 5;
repeated string whitelisted_lps = 6;
}
3 changes: 2 additions & 1 deletion wasmbinding/stargate_allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
consumertypes "github.com/cosmos/interchain-security/v5/x/ccv/consumer/types"
globalfeetypes "github.com/neutron-org/neutron/v5/x/globalfee/types"
feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"
marketmaptypes "github.com/skip-mev/slinky/x/marketmap/types"
oracletypes "github.com/skip-mev/slinky/x/oracle/types"

globalfeetypes "github.com/neutron-org/neutron/v5/x/globalfee/types"

dynamicfeestypes "github.com/neutron-org/neutron/v5/x/dynamicfees/types"

crontypes "github.com/neutron-org/neutron/v5/x/cron/types"
Expand Down
9 changes: 9 additions & 0 deletions x/dex/keeper/core_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func (k Keeper) ValidateFee(ctx sdk.Context, fee uint64) error {
return nil
}

func (k Keeper) GetWhitelistedLPs(ctx sdk.Context) []string {
return k.GetParams(ctx).WhitelistedLps
}

func (k Keeper) IsWhitelistedLP(ctx sdk.Context, addr sdk.AccAddress) bool {
whitelistedLPs := k.GetWhitelistedLPs(ctx)
return slices.Contains(whitelistedLPs, addr.String())
}

func (k Keeper) GetMaxJITsPerBlock(ctx sdk.Context) uint64 {
return k.GetParams(ctx).MaxJitsPerBlock
}
Expand Down
7 changes: 5 additions & 2 deletions x/dex/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ func (k Keeper) ExecuteDeposit(
}
autoswap := !option.DisableAutoswap

if err := k.ValidateFee(ctx, fee); err != nil {
return nil, nil, math.ZeroInt(), math.ZeroInt(), nil, nil, nil, err
// Enforce deposits only at valid fee tiers. This does not apply to whitelistedLPs
if !k.IsWhitelistedLP(ctx, callerAddr) {
if err := k.ValidateFee(ctx, fee); err != nil {
return nil, nil, math.ZeroInt(), math.ZeroInt(), nil, nil, nil, err
}
}

if k.IsPoolBehindEnemyLines(ctx, pairID, tickIndex, fee, amount0, amount1) {
Expand Down
16 changes: 16 additions & 0 deletions x/dex/keeper/integration_deposit_singlesided_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,22 @@ func (s *DexTestSuite) TestDepositSingleInvalidFeeFails() {
)
}

func (s *DexTestSuite) TestDepositSinglewWhitelistedLPWithInvalidFee() {
s.fundAliceBalances(0, 50)

// Whitelist alice's address
params := s.App.DexKeeper.GetParams(s.Ctx)
params.WhitelistedLps = []string{s.alice.String()}
err := s.App.DexKeeper.SetParams(s.Ctx, params)
s.NoError(err)

// WHEN Deposit at fee 43 (invalid)
// THEN no error
s.aliceDeposits(
NewDeposit(0, 50, 10, 43),
)
}

func (s *DexTestSuite) TestDepositSingleToken0BELFails() {
s.fundAliceBalances(50, 50)

Expand Down
28 changes: 27 additions & 1 deletion x/dex/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,47 @@ func TestSetParams(t *testing.T) {
FeeTiers: []uint64{0, 1},
MaxJitsPerBlock: 0,
GoodTilPurgeAllowance: 0,
WhitelistedLps: []string{"neutron10h9stc5v6ntgeygf5xf945njqq5h32r54rf7kf"},
}
err := k.SetParams(ctx, newParams)
require.NoError(t, err)

require.EqualValues(t, newParams, k.GetParams(ctx))
}

func TestValidateParams(t *testing.T) {
func TestValidateFees(t *testing.T) {
goodFees := []uint64{1, 2, 3, 4, 5, 200}
require.NoError(t, types.Params{FeeTiers: goodFees}.Validate())

badFees := []uint64{1, 2, 3, 3}
require.Error(t, types.Params{FeeTiers: badFees}.Validate())
}

func TestValidateWhitelistedLPs(t *testing.T) {
// No whitelists
require.NoError(t, types.Params{WhitelistedLps: []string{}}.Validate())

// With account address
require.NoError(t, types.Params{WhitelistedLps: []string{"neutron10h9stc5v6ntgeygf5xf945njqq5h32r54rf7kf"}}.Validate())

// With contract address
require.NoError(t, types.Params{WhitelistedLps: []string{"neutron10a3k4hvk37cc4hnxctw4p95fhscd2z6h2rmx0aukc6rm8u9qqx9s0methe"}}.
Validate())

// With contract address
require.NoError(t, types.Params{WhitelistedLps: []string{
"neutron1dft8nwxzr0u27wvr2cknpermjkreqvp9fdy0uz",
"neutron10a3k4hvk37cc4hnxctw4p95fhscd2z6h2rmx0aukc6rm8u9qqx9s0methe",
"neutron10h9stc5v6ntgeygf5xf945njqq5h32r54rf7kf",
}}.Validate())

// With invalid address
require.Error(t, types.Params{WhitelistedLps: []string{
"neutron1dft8nwxzr0u27wvr2cknpermjkreqvp9fdy0uz",
"BADADDR",
}}.Validate())
}

func (s *DexTestSuite) TestPauseDex() {
s.fundAliceBalances(100, 100)
trancheKey := s.aliceLimitSells("TokenA", 0, 10, types.LimitOrderType_GOOD_TIL_CANCELLED)
Expand Down
30 changes: 28 additions & 2 deletions x/dex/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"gopkg.in/yaml.v2"
)
Expand All @@ -18,6 +19,8 @@ var (
DefaultMaxJITsPerBlock uint64 = 25
KeyGoodTilPurgeAllowance = []byte("PurgeAllowance")
DefaultGoodTilPurgeAllowance uint64 = 540_000
KeyWhitelistedLPs = []byte("PurgeAllowance")
DefaultKeyWhitelistedLPs []string
)

// ParamKeyTable the param key table for launch module
Expand All @@ -26,18 +29,19 @@ func ParamKeyTable() paramtypes.KeyTable {
}

// NewParams creates a new Params instance
func NewParams(feeTiers []uint64, paused bool, maxJITsPerBlock, goodTilPurgeAllowance uint64) Params {
func NewParams(feeTiers []uint64, paused bool, maxJITsPerBlock, goodTilPurgeAllowance uint64, whitelistedLPs []string) Params {
return Params{
FeeTiers: feeTiers,
Paused: paused,
MaxJitsPerBlock: maxJITsPerBlock,
GoodTilPurgeAllowance: goodTilPurgeAllowance,
WhitelistedLps: whitelistedLPs,
}
}

// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(DefaultFeeTiers, DefaultPaused, DefaultMaxJITsPerBlock, DefaultGoodTilPurgeAllowance)
return NewParams(DefaultFeeTiers, DefaultPaused, DefaultMaxJITsPerBlock, DefaultGoodTilPurgeAllowance, DefaultKeyWhitelistedLPs)
}

// ParamSetPairs get the params.ParamSet
Expand All @@ -47,6 +51,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
paramtypes.NewParamSetPair(KeyPaused, &p.Paused, validatePaused),
paramtypes.NewParamSetPair(KeyMaxJITsPerBlock, &p.MaxJitsPerBlock, validateMaxJITsPerBlock),
paramtypes.NewParamSetPair(KeyGoodTilPurgeAllowance, &p.GoodTilPurgeAllowance, validatePurgeAllowance),
paramtypes.NewParamSetPair(KeyWhitelistedLPs, &p.WhitelistedLps, validateWhitelistedLPs),
}
}

Expand All @@ -73,6 +78,11 @@ func (p Params) Validate() error {
if err := validatePurgeAllowance(p.GoodTilPurgeAllowance); err != nil {
return err
}

if err := validateWhitelistedLPs(p.WhitelistedLps); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -118,3 +128,19 @@ func validatePurgeAllowance(v interface{}) error {

return nil
}

func validateWhitelistedLPs(v interface{}) error {
whitelistedLPs, ok := v.([]string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}

for _, addr := range whitelistedLPs {
_, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return fmt.Errorf("invalid Reserve address (%s): %w, ", addr, err)
}
}

return nil
}
96 changes: 76 additions & 20 deletions x/dex/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6c764f9

Please sign in to comment.