Skip to content

Commit

Permalink
Add feeTiers to dex params; only allow legal feeTier deposits
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Compagni Portis committed Sep 19, 2023
1 parent 49b2019 commit 3dd750c
Show file tree
Hide file tree
Showing 17 changed files with 355 additions and 184 deletions.
2 changes: 1 addition & 1 deletion proto/duality/dex/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ option go_package = "github.com/duality-labs/duality/x/dex/types";
// Params defines the parameters for the module.
message Params {
option (gogoproto.goproto_stringer) = false;

repeated uint64 fee_tiers = 1;
}
2 changes: 1 addition & 1 deletion proto/duality/dex/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ message QueryParamsRequest {}
message QueryParamsResponse {

// params holds all the parameters of this module.
Params params = 1 [(gogoproto.nullable) = true];
Params params = 1 [(gogoproto.nullable) = false];
}

message QueryGetLimitOrderTrancheUserRequest {
Expand Down
4 changes: 2 additions & 2 deletions x/dex/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
// Set poolMetadata count
k.SetPoolCount(ctx, genState.PoolCount)
// this line is used by starport scaffolding # genesis/module/init
k.SetParams(ctx, &genState.Params)
k.SetParams(ctx, genState.Params)
}

// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = *k.GetParams(ctx)
genesis.Params = k.GetParams(ctx)
genesis.LimitOrderTrancheUserList = k.GetAllLimitOrderTrancheUser(ctx)
genesis.TickLiquidityList = k.GetAllTickLiquidity(ctx)
genesis.InactiveLimitOrderTrancheList = k.GetAllInactiveLimitOrderTranche(ctx)
Expand Down
2 changes: 1 addition & 1 deletion x/dex/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestGenesis(t *testing.T) {
genesisState := types.GenesisState{
Params: *types.DefaultParams(),
Params: types.DefaultParams(),
LimitOrderTrancheUserList: []*types.LimitOrderTrancheUser{
{
TradePairID: &types.TradePairID{
Expand Down
4 changes: 4 additions & 0 deletions x/dex/keeper/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (k Keeper) DepositCore(
amount1 := amounts1[i]
tickIndex := tickIndices[i]
fee := fees[i]

if err := k.ValidateFee(ctx, fee); err != nil {
return nil, nil, nil, err
}
autoswap := !options[i].DisableAutoswap

pool, err := k.GetOrInitPool(
Expand Down
15 changes: 15 additions & 0 deletions x/dex/keeper/core_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/duality-labs/duality/x/dex/types"
"golang.org/x/exp/slices"
)

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -56,6 +58,19 @@ func (k Keeper) GetCurrLiq(ctx sdk.Context, tradePairID *types.TradePairID) *typ
return nil
}

func (k Keeper) GetValidFees(ctx sdk.Context) []uint64 {
return k.GetParams(ctx).FeeTiers
}

func (k Keeper) ValidateFee(ctx sdk.Context, fee uint64) error {
validFees := k.GetValidFees(ctx)
if !slices.Contains(validFees, fee) {
return sdkerrors.Wrapf(types.ErrInvalidFee, "%s", validFees)
}

return nil
}

///////////////////////////////////////////////////////////////////////////////
// TOKENIZER UTILS //
///////////////////////////////////////////////////////////////////////////////
Expand Down
12 changes: 12 additions & 0 deletions x/dex/keeper/integration_deposit_singlesided_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,15 @@ func (s *MsgServerTestSuite) TestDepositSingleLowTickUnderflowFails() {
NewDeposit(0, 50, -352436, 0),
)
}

func (s *MsgServerTestSuite) TestDepositSingleInvalidFeeFails() {
s.fundAliceBalances(0, 50)

// GIVEN
// Deposit at fee 43 (invalid)
// THEN FAILURE
s.assertAliceDepositFails(
types.ErrInvalidFee,
NewDeposit(0, 50, 10, 43),
)
}
9 changes: 5 additions & 4 deletions x/dex/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
)

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(_ sdk.Context) *types.Params {
return types.NewParams()
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramstore.GetParamSet(ctx, &params)
return params
}

// SetParams set the params
func (k Keeper) SetParams(ctx sdk.Context, params *types.Params) {
k.paramstore.SetParamSet(ctx, params)
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, &params)
}
8 changes: 8 additions & 0 deletions x/dex/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ func TestGetParams(t *testing.T) {

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

func TestValidateParams(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())
}
2 changes: 1 addition & 1 deletion x/dex/module_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
accs[i] = acc.Address.String()
}
dexGenesis := types.GenesisState{
Params: *types.DefaultParams(),
Params: types.DefaultParams(),
// this line is used by starport scaffolding # simapp/module/genesisState
}
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&dexGenesis)
Expand Down
2 changes: 1 addition & 1 deletion x/dex/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,6 @@ var (
ErrInvalidFee = sdkerrors.Register(
ModuleName,
1148,
"Fee plus/minus center tick cannot exceed tick range [-352437, 352437]",
"Fee must must a legal fee amount:",
)
)
2 changes: 1 addition & 1 deletion x/dex/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func DefaultGenesis() *GenesisState {
InactiveLimitOrderTrancheList: []*LimitOrderTranche{},
PoolMetadataList: []PoolMetadata{},
// this line is used by starport scaffolding # genesis/types/default
Params: *DefaultParams(),
Params: DefaultParams(),
}
}

Expand Down
46 changes: 37 additions & 9 deletions x/dex/types/params.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,67 @@
package types

import (
fmt "fmt"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"gopkg.in/yaml.v2"
)

var _ paramtypes.ParamSet = (*Params)(nil)

var (
KeyFeeTiers = []byte("FeeTiers")
DefaultFeeTiers []uint64 = []uint64{0, 1, 2, 3, 4, 5, 10, 20, 50, 100, 150, 200}
)

// ParamKeyTable the param key table for launch module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

// NewParams creates a new Params instance
func NewParams() *Params {
return &Params{}
func NewParams(feeTiers []uint64) Params {
return Params{FeeTiers: feeTiers}
}

// DefaultParams returns a default set of parameters
func DefaultParams() *Params {
return NewParams()
func DefaultParams() Params {
return NewParams(DefaultFeeTiers)
}

// ParamSetPairs get the params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{}
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyFeeTiers, &p.FeeTiers, validateFeeTiers),
}
}

// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}

// Validate validates the set of params
func (p Params) Validate() error {
if err := validateFeeTiers(p.FeeTiers); err != nil {
return err
}
return nil
}

// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
func validateFeeTiers(v interface{}) error {
feeTiers, ok := v.([]uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}

feeTierMap := make(map[uint64]bool)
for _, f := range feeTiers {
if _, ok := feeTierMap[f]; ok {
return fmt.Errorf("duplicate fee tier found")
}
feeTierMap[f] = true
}
return nil
}
Loading

0 comments on commit 3dd750c

Please sign in to comment.