diff --git a/contracts/neutron_chain_manager.wasm b/contracts/neutron_chain_manager.wasm index 5b1fd4f49..ac80c5751 100644 Binary files a/contracts/neutron_chain_manager.wasm and b/contracts/neutron_chain_manager.wasm differ diff --git a/proto/neutron/dex/params.proto b/proto/neutron/dex/params.proto index a25c48d61..07c9ebf2f 100644 --- a/proto/neutron/dex/params.proto +++ b/proto/neutron/dex/params.proto @@ -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" + ]; } diff --git a/x/dex/keeper/core_helper.go b/x/dex/keeper/core_helper.go index bcf72159e..3b9883bfb 100644 --- a/x/dex/keeper/core_helper.go +++ b/x/dex/keeper/core_helper.go @@ -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 } diff --git a/x/dex/keeper/deposit.go b/x/dex/keeper/deposit.go index 379bd99d0..15351f67f 100644 --- a/x/dex/keeper/deposit.go +++ b/x/dex/keeper/deposit.go @@ -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] @@ -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) { diff --git a/x/dex/keeper/integration_deposit_singlesided_test.go b/x/dex/keeper/integration_deposit_singlesided_test.go index 81a8e65e9..4f7181f0c 100644 --- a/x/dex/keeper/integration_deposit_singlesided_test.go +++ b/x/dex/keeper/integration_deposit_singlesided_test.go @@ -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) diff --git a/x/dex/keeper/params_test.go b/x/dex/keeper/params_test.go index c84ae9294..9afb15901 100644 --- a/x/dex/keeper/params_test.go +++ b/x/dex/keeper/params_test.go @@ -24,6 +24,7 @@ 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) @@ -31,7 +32,7 @@ func TestSetParams(t *testing.T) { 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()) @@ -39,6 +40,31 @@ func TestValidateParams(t *testing.T) { 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) diff --git a/x/dex/types/params.go b/x/dex/types/params.go index ce219d815..3da1017e6 100644 --- a/x/dex/types/params.go +++ b/x/dex/types/params.go @@ -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" ) @@ -18,6 +19,8 @@ var ( DefaultMaxJITsPerBlock uint64 = 25 KeyGoodTilPurgeAllowance = []byte("PurgeAllowance") DefaultGoodTilPurgeAllowance uint64 = 540_000 + KeyWhitelistedLPs = []byte("WhiteListedLPs") + DefaultKeyWhitelistedLPs []string ) // ParamKeyTable the param key table for launch module @@ -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 @@ -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), } } @@ -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 } @@ -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 +} diff --git a/x/dex/types/params.pb.go b/x/dex/types/params.pb.go index 8821ec7dd..eb5504231 100644 --- a/x/dex/types/params.pb.go +++ b/x/dex/types/params.pb.go @@ -29,6 +29,9 @@ type Params struct { Paused bool `protobuf:"varint,3,opt,name=paused,proto3" json:"paused"` MaxJitsPerBlock uint64 `protobuf:"varint,4,opt,name=max_jits_per_block,json=maxJitsPerBlock,proto3" json:"max_jits_per_block,omitempty"` GoodTilPurgeAllowance uint64 `protobuf:"varint,5,opt,name=good_til_purge_allowance,json=goodTilPurgeAllowance,proto3" json:"good_til_purge_allowance,omitempty"` + // Whitelisted_lps have special LP privileges; + // currently, the only such privilege is depositing outside of the allowed fee_tiers. + WhitelistedLps []string `protobuf:"bytes,6,rep,name=whitelisted_lps,json=whitelistedLps,proto3" json:"whitelisted_lps"` } func (m *Params) Reset() { *m = Params{} } @@ -91,6 +94,13 @@ func (m *Params) GetGoodTilPurgeAllowance() uint64 { return 0 } +func (m *Params) GetWhitelistedLps() []string { + if m != nil { + return m.WhitelistedLps + } + return nil +} + func init() { proto.RegisterType((*Params)(nil), "neutron.dex.Params") } @@ -98,26 +108,28 @@ func init() { func init() { proto.RegisterFile("neutron/dex/params.proto", fileDescriptor_84a6bffcfc21009c) } var fileDescriptor_84a6bffcfc21009c = []byte{ - // 290 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00,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4b, 0x4c, 0x95, - 0x31, 0x89, 0x08, 0x3a, 0x15, 0x36, 0x77, 0x0a, 0x84, 0x4c, 0x53, 0xf3, 0x2c, 0x37, 0x2b, 0xa0, - 0x7b, 0x4d, 0xe4, 0xb8, 0xe6, 0x0b, 0x9d, 0xce, 0x6a, 0x7a, 0xbd, 0x83, 0x57, 0xf1, 0xdb, 0x7b, - 0x3f, 0x9a, 0xde, 0x7c, 0x96, 0x0c, 0x6d, 0x4b, 0x86, 0x7e, 0x4a, 0x86, 0x5e, 0x2b, 0x16, 0x6d, - 0x2b, 0x16, 0x7d, 0x55, 0x2c, 0xba, 0x1b, 0x2b, 0x1d, 0xee, 0xf3, 0xe5, 0x64, 0x65, 0x32, 0xde, - 0xfe, 0x3f, 0x36, 0x4e, 0xed, 0x34, 0x7f, 0xba, 0xe0, 0x45, 0x53, 0x55, 0x78, 0xb1, 0xe0, 0x97, - 0xdd, 0xa6, 0x87, 0xf3, 0xbf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc6, 0xda, 0x12, 0xf5, 0x46, 0x01, - 0x00, 0x00, + // 324 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x5c, 0x90, 0x31, 0x4b, 0xf3, 0x40, + 0x1c, 0xc6, 0x13, 0x9a, 0x37, 0xb4, 0xf7, 0xc2, 0x5b, 0xc8, 0xab, 0x70, 0x28, 0xa4, 0xa1, 0x53, + 0x40, 0xda, 0x0c, 0x22, 0x82, 0xb8, 0x98, 0x45, 0x10, 0x87, 0x12, 0x3a, 0xb9, 0x1c, 0xd7, 0xe6, + 0xdf, 0xf4, 0xf4, 0xd2, 0x3b, 0xee, 0x2e, 0x36, 0x7e, 0x0b, 0x47, 0x47, 0x3f, 0x8e, 0x63, 0x47, + 0xa7, 0x22, 0xed, 0xd6, 0xd5, 0x2f, 0x20, 0x89, 0x29, 0x88, 0xd3, 0x3d, 0xf7, 0xfb, 0x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f1, 0xed, 0xd5, 0xc6, 0xb7, 0x3f, 0x36, 0xbe, 0xfd, 0xbc, 0xf5, + 0xad, 0xd5, 0xd6, 0xb7, 0xde, 0xb7, 0xbe, 0x75, 0x37, 0xc8, 0x98, 0x99, 0x17, 0x93, 0xe1, 0x54, + 0xe4, 0x51, 0xb3, 0xde, 0x40, 0xa8, 0x6c, 0x9f, 0xa3, 0xc7, 0xb3, 0xa8, 0xac, 0x87, 0x36, 0x4f, + 0x12, 0xf4, 0xc4, 0xad, 0x57, 0x3c, 0xfd, 0x0a, 0x00, 0x00, 0xff, 0xff, 0xda, 0x2d, 0x94, 0xb7, + 0x84, 0x01, 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -140,6 +152,15 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.WhitelistedLps) > 0 { + for iNdEx := len(m.WhitelistedLps) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.WhitelistedLps[iNdEx]) + copy(dAtA[i:], m.WhitelistedLps[iNdEx]) + i = encodeVarintParams(dAtA, i, uint64(len(m.WhitelistedLps[iNdEx]))) + i-- + dAtA[i] = 0x32 + } + } if m.GoodTilPurgeAllowance != 0 { i = encodeVarintParams(dAtA, i, uint64(m.GoodTilPurgeAllowance)) i-- @@ -214,6 +235,12 @@ func (m *Params) Size() (n int) { if m.GoodTilPurgeAllowance != 0 { n += 1 + sovParams(uint64(m.GoodTilPurgeAllowance)) } + if len(m.WhitelistedLps) > 0 { + for _, s := range m.WhitelistedLps { + l = len(s) + n += 1 + l + sovParams(uint64(l)) + } + } return n } @@ -386,6 +413,38 @@ func (m *Params) Unmarshal(dAtA []byte) error { break } } + case 6: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field WhitelistedLps", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthParams + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthParams + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.WhitelistedLps = append(m.WhitelistedLps, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipParams(dAtA[iNdEx:])