github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/types/params_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	sdk "github.com/cosmos/cosmos-sdk/types"
     8  	paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/gravity-devs/liquidity/app"
    12  	"github.com/gravity-devs/liquidity/x/liquidity/types"
    13  )
    14  
    15  func TestParams(t *testing.T) {
    16  	require.IsType(t, paramstypes.KeyTable{}, types.ParamKeyTable())
    17  
    18  	simapp, ctx := app.CreateTestInput()
    19  	defaultParams := types.DefaultParams()
    20  	require.Equal(t, defaultParams, simapp.LiquidityKeeper.GetParams(ctx))
    21  
    22  	paramsStr := `pool_types:
    23  - id: 1
    24    name: StandardLiquidityPool
    25    min_reserve_coin_num: 2
    26    max_reserve_coin_num: 2
    27    description: Standard liquidity pool with pool price function X/Y, ESPM constraint,
    28      and two kinds of reserve coins
    29  min_init_deposit_amount: "1000000"
    30  init_pool_coin_mint_amount: "1000000"
    31  max_reserve_coin_amount: "0"
    32  pool_creation_fee:
    33  - denom: stake
    34    amount: "40000000"
    35  swap_fee_rate: "0.003000000000000000"
    36  withdraw_fee_rate: "0.000000000000000000"
    37  max_order_amount_ratio: "0.100000000000000000"
    38  unit_batch_height: 1
    39  circuit_breaker_enabled: false
    40  `
    41  	require.Equal(t, paramsStr, defaultParams.String())
    42  }
    43  
    44  func TestParams_Validate(t *testing.T) {
    45  	require.NoError(t, types.DefaultParams().Validate())
    46  
    47  	testCases := []struct {
    48  		name      string
    49  		configure func(*types.Params)
    50  		errString string
    51  	}{
    52  		{
    53  			"EmptyPoolTypes",
    54  			func(params *types.Params) {
    55  				params.PoolTypes = []types.PoolType{}
    56  			},
    57  			"pool types must not be empty",
    58  		},
    59  		{
    60  			"TooManyPoolTypes",
    61  			func(params *types.Params) {
    62  				params.PoolTypes = []types.PoolType{types.DefaultPoolType, types.DefaultPoolType}
    63  			},
    64  			"pool type ids must be sorted",
    65  		},
    66  		{
    67  			"CustomPoolType",
    68  			func(params *types.Params) {
    69  				poolType := types.DefaultPoolType
    70  				poolType.Name = "CustomPoolType"
    71  				params.PoolTypes = []types.PoolType{poolType}
    72  			},
    73  			"the only supported pool type is 1",
    74  		},
    75  		{
    76  			"NilMinInitDepositAmount",
    77  			func(params *types.Params) {
    78  				params.MinInitDepositAmount = sdk.Int{}
    79  			},
    80  			"minimum initial deposit amount must not be nil",
    81  		},
    82  		{
    83  			"NonPositiveMinInitDepositAmount",
    84  			func(params *types.Params) {
    85  				params.MinInitDepositAmount = sdk.NewInt(0)
    86  			},
    87  			"minimum initial deposit amount must be positive: 0",
    88  		},
    89  		{
    90  			"NilInitPoolCoinMintAmount",
    91  			func(params *types.Params) {
    92  				params.InitPoolCoinMintAmount = sdk.Int{}
    93  			},
    94  			"initial pool coin mint amount must not be nil",
    95  		},
    96  		{
    97  			"NonPositiveInitPoolCoinMintAmount",
    98  			func(params *types.Params) {
    99  				params.InitPoolCoinMintAmount = sdk.ZeroInt()
   100  			},
   101  			"initial pool coin mint amount must be positive: 0",
   102  		},
   103  		{
   104  			"TooSmallInitPoolCoinMintAmount",
   105  			func(params *types.Params) {
   106  				params.InitPoolCoinMintAmount = sdk.NewInt(10)
   107  			},
   108  			"initial pool coin mint amount must be greater than or equal to 1000000: 10",
   109  		},
   110  		{
   111  			"NilMaxReserveCoinAmount",
   112  			func(params *types.Params) {
   113  				params.MaxReserveCoinAmount = sdk.Int{}
   114  			},
   115  			"max reserve coin amount must not be nil",
   116  		},
   117  		{
   118  			"NegativeMaxReserveCoinAmount",
   119  			func(params *types.Params) {
   120  				params.MaxReserveCoinAmount = sdk.NewInt(-1)
   121  			},
   122  			"max reserve coin amount must not be negative: -1",
   123  		},
   124  		{
   125  			"NilSwapFeeRate",
   126  			func(params *types.Params) {
   127  				params.SwapFeeRate = sdk.Dec{}
   128  			},
   129  			"swap fee rate must not be nil",
   130  		},
   131  		{
   132  			"NegativeSwapFeeRate",
   133  			func(params *types.Params) {
   134  				params.SwapFeeRate = sdk.NewDec(-1)
   135  			},
   136  			"swap fee rate must not be negative: -1.000000000000000000",
   137  		},
   138  		{
   139  			"TooLargeSwapFeeRate",
   140  			func(params *types.Params) {
   141  				params.SwapFeeRate = sdk.NewDec(2)
   142  			},
   143  			"swap fee rate too large: 2.000000000000000000",
   144  		},
   145  		{
   146  			"NilWithdrawFeeRate",
   147  			func(params *types.Params) {
   148  				params.WithdrawFeeRate = sdk.Dec{}
   149  			},
   150  			"withdraw fee rate must not be nil",
   151  		},
   152  		{
   153  			"NegativeWithdrawFeeRate",
   154  			func(params *types.Params) {
   155  				params.WithdrawFeeRate = sdk.NewDec(-1)
   156  			},
   157  			"withdraw fee rate must not be negative: -1.000000000000000000",
   158  		},
   159  		{
   160  			"TooLargeWithdrawFeeRate",
   161  			func(params *types.Params) {
   162  				params.WithdrawFeeRate = sdk.NewDec(2)
   163  			},
   164  			"withdraw fee rate too large: 2.000000000000000000",
   165  		},
   166  		{
   167  			"NilMaxOrderAmountRatio",
   168  			func(params *types.Params) {
   169  				params.MaxOrderAmountRatio = sdk.Dec{}
   170  			},
   171  			"max order amount ratio must not be nil",
   172  		},
   173  		{
   174  			"NegativeMaxOrderAmountRatio",
   175  			func(params *types.Params) {
   176  				params.MaxOrderAmountRatio = sdk.NewDec(-1)
   177  			},
   178  			"max order amount ratio must not be negative: -1.000000000000000000",
   179  		},
   180  		{
   181  			"TooLargeMaxOrderAmountRatio",
   182  			func(params *types.Params) {
   183  				params.MaxOrderAmountRatio = sdk.NewDec(2)
   184  			},
   185  			"max order amount ratio too large: 2.000000000000000000",
   186  		},
   187  		{
   188  			"EmptyPoolCreationFee",
   189  			func(params *types.Params) {
   190  				params.PoolCreationFee = sdk.NewCoins()
   191  			},
   192  			"pool creation fee must not be empty",
   193  		},
   194  		{
   195  			"InvalidPoolCreationFeeDenom",
   196  			func(params *types.Params) {
   197  				params.PoolCreationFee = sdk.Coins{
   198  					sdk.Coin{
   199  						Denom:  "invalid denom---",
   200  						Amount: params.PoolCreationFee.AmountOf(params.PoolCreationFee.GetDenomByIndex(0)),
   201  					},
   202  				}
   203  			},
   204  			"invalid denom: invalid denom---",
   205  		},
   206  		{
   207  			"NotPositivePoolCreationFeeAmount",
   208  			func(params *types.Params) {
   209  				params.PoolCreationFee = sdk.Coins{
   210  					sdk.Coin{
   211  						Denom:  params.PoolCreationFee.GetDenomByIndex(0),
   212  						Amount: sdk.ZeroInt(),
   213  					},
   214  				}
   215  			},
   216  			"coin 0stake amount is not positive",
   217  		},
   218  		{
   219  			"NonPositiveUnitBatchHeight",
   220  			func(params *types.Params) {
   221  				params.UnitBatchHeight = 0
   222  			},
   223  			"unit batch height must be positive: 0",
   224  		},
   225  	}
   226  	for _, tc := range testCases {
   227  		t.Run(tc.name, func(t *testing.T) {
   228  			params := types.DefaultParams()
   229  			tc.configure(&params)
   230  			err := params.Validate()
   231  			require.EqualError(t, err, tc.errString)
   232  			var err2 error
   233  			for _, p := range params.ParamSetPairs() {
   234  				err := p.ValidatorFn(reflect.ValueOf(p.Value).Elem().Interface())
   235  				if err != nil {
   236  					err2 = err
   237  					break
   238  				}
   239  			}
   240  			require.EqualError(t, err2, tc.errString)
   241  		})
   242  	}
   243  }