Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: whitelist LPs #NTRN-442 #799

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified contracts/neutron_chain_manager.wasm
Binary file not shown.
6 changes: 6 additions & 0 deletions proto/neutron/dex/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ message Params {
];
uint64 max_jits_per_block = 4;
uint64 good_til_purge_allowance = 5;
// Whitelisted_lps have special LP privileges;
// currently, the only such privilege is depositing outside of the allowed fee_tiers.
repeated string whitelisted_lps = 6 [
// Adding jsontag prevents protoc from adding `omitempty` tag
(gogoproto.jsontag) = "whitelisted_lps"
];
}
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
8 changes: 6 additions & 2 deletions x/dex/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (k Keeper) ExecuteDeposit(
amounts0Deposited[i] = math.ZeroInt()
amounts1Deposited[i] = math.ZeroInt()
}
isWhitelistedLP := k.IsWhitelistedLP(ctx, callerAddr)

for i, amount0 := range amounts0 {
amount1 := amounts1[i]
Expand All @@ -100,8 +101,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 !isWhitelistedLP {
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
42 changes: 34 additions & 8 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("WhiteListedLPs")
DefaultKeyWhitelistedLPs []string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe assign an empty array to default to be more clear here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter does not like assigning an empty array

)

// 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 @@ -58,21 +63,26 @@ func (p Params) String() string {

// Validate validates the set of params
func (p Params) Validate() error {
err := validateFeeTiers(p.FeeTiers)
if err != nil {
if err := validateFeeTiers(p.FeeTiers); err != nil {
return fmt.Errorf("invalid fee tiers: %w", err)
}

err = validatePaused(p.Paused)
if err != nil {
if err := validatePaused(p.Paused); err != nil {
return fmt.Errorf("invalid paused: %w", err)
}

if err := validateMaxJITsPerBlock(p.MaxJitsPerBlock); err != nil {
return err
return fmt.Errorf("invalid max JITs per block: %w", err)
}

if err := validatePurgeAllowance(p.GoodTilPurgeAllowance); err != nil {
return err
return fmt.Errorf("invalid good til purge allowance: %w", err)
}

if err := validateWhitelistedLPs(p.WhitelistedLps); err != nil {
return fmt.Errorf("invalid whitelisted LPs: %w", 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 LP address (%s): %w, ", addr, err)
}
}

return nil
}
99 changes: 79 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.

Loading