code.vegaprotocol.io/vega@v0.79.0/commands/submit_amm_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands_test
    17  
    18  import (
    19  	"errors"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/commands"
    23  	"code.vegaprotocol.io/vega/libs/ptr"
    24  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestCheckSubmitAMM(t *testing.T) {
    30  	cases := []struct {
    31  		submission commandspb.SubmitAMM
    32  		errStr     string
    33  	}{
    34  		{
    35  			submission: commandspb.SubmitAMM{},
    36  			errStr:     "submit_amm.market_id (is required)",
    37  		},
    38  		{
    39  			submission: commandspb.SubmitAMM{
    40  				MarketId: "notavalidmarketid",
    41  			},
    42  			errStr: "submit_amm.market_id (should be a valid Vega ID)",
    43  		},
    44  		{
    45  			submission: commandspb.SubmitAMM{
    46  				SlippageTolerance: "",
    47  			},
    48  			errStr: "submit_amm.slippage_tolerance (is required)",
    49  		},
    50  		{
    51  			submission: commandspb.SubmitAMM{
    52  				SlippageTolerance: "abc",
    53  			},
    54  			errStr: "submit_amm.slippage_tolerance (is not a valid number)",
    55  		},
    56  		{
    57  			submission: commandspb.SubmitAMM{
    58  				SlippageTolerance: "-0.5",
    59  			},
    60  			errStr: "submit_amm.slippage_tolerance (must be between 0 (excluded) and 1 (included))",
    61  		},
    62  		{
    63  			submission: commandspb.SubmitAMM{
    64  				SlippageTolerance: "0",
    65  			},
    66  			errStr: "submit_amm.slippage_tolerance (must be between 0 (excluded) and 1 (included))",
    67  		},
    68  		{
    69  			submission: commandspb.SubmitAMM{
    70  				SlippageTolerance: "2",
    71  			},
    72  			errStr: "submit_amm.slippage_tolerance (must be between 0 (excluded) and 1 (included))",
    73  		},
    74  		{
    75  			submission: commandspb.SubmitAMM{
    76  				CommitmentAmount: "",
    77  			},
    78  			errStr: "submit_amm.commitment_amount (is required)",
    79  		},
    80  		{
    81  			submission: commandspb.SubmitAMM{
    82  				CommitmentAmount: "abc",
    83  			},
    84  			errStr: "submit_amm.commitment_amount (is not a valid number)",
    85  		},
    86  		{
    87  			submission: commandspb.SubmitAMM{
    88  				CommitmentAmount: "-10",
    89  			},
    90  			errStr: "submit_amm.commitment_amount (must be positive)",
    91  		},
    92  		{
    93  			submission: commandspb.SubmitAMM{
    94  				CommitmentAmount: "0",
    95  			},
    96  			errStr: "submit_amm.commitment_amount (must be positive)",
    97  		},
    98  		{
    99  			submission: commandspb.SubmitAMM{
   100  				ProposedFee: "",
   101  			},
   102  			errStr: "submit_amm.proposed_fee (is required)",
   103  		},
   104  		{
   105  			submission: commandspb.SubmitAMM{
   106  				ProposedFee: "abc",
   107  			},
   108  			errStr: "submit_amm.proposed_fee (is not a valid value)",
   109  		},
   110  		{
   111  			submission: commandspb.SubmitAMM{
   112  				ProposedFee: "-10",
   113  			},
   114  			errStr: "submit_amm.proposed_fee (must be positive)",
   115  		},
   116  		{
   117  			submission: commandspb.SubmitAMM{
   118  				ProposedFee: "0",
   119  			},
   120  			errStr: "submit_amm.proposed_fee (must be positive)",
   121  		},
   122  		{
   123  			submission: commandspb.SubmitAMM{
   124  				ConcentratedLiquidityParameters: nil,
   125  			},
   126  			errStr: "submit_amm.concentrated_liquidity_parameters (is required)",
   127  		},
   128  		{
   129  			submission: commandspb.SubmitAMM{
   130  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   131  					Base: "",
   132  				},
   133  			},
   134  			errStr: "submit_amm.concentrated_liquidity_parameters.base (is required)",
   135  		},
   136  		{
   137  			submission: commandspb.SubmitAMM{
   138  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   139  					Base: "abc",
   140  				},
   141  			},
   142  			errStr: "submit_amm.concentrated_liquidity_parameters.base (is not a valid number)",
   143  		},
   144  		{
   145  			submission: commandspb.SubmitAMM{
   146  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   147  					Base: "-10",
   148  				},
   149  			},
   150  			errStr: "submit_amm.concentrated_liquidity_parameters.base (must be positive)",
   151  		},
   152  		{
   153  			submission: commandspb.SubmitAMM{
   154  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   155  					Base: "0",
   156  				},
   157  			},
   158  			errStr: "submit_amm.concentrated_liquidity_parameters.base (must be positive)",
   159  		},
   160  		{
   161  			submission: commandspb.SubmitAMM{
   162  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   163  					LowerBound: ptr.From(""),
   164  				},
   165  			},
   166  			errStr: "submit_amm.concentrated_liquidity_parameters.lower_bound (is not a valid number)",
   167  		},
   168  		{
   169  			submission: commandspb.SubmitAMM{
   170  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   171  					LowerBound: ptr.From("abc"),
   172  				},
   173  			},
   174  			errStr: "submit_amm.concentrated_liquidity_parameters.lower_bound (is not a valid number)",
   175  		},
   176  		{
   177  			submission: commandspb.SubmitAMM{
   178  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   179  					LowerBound: ptr.From("-10"),
   180  				},
   181  			},
   182  			errStr: "submit_amm.concentrated_liquidity_parameters.lower_bound (must be positive)",
   183  		},
   184  		{
   185  			submission: commandspb.SubmitAMM{
   186  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   187  					LowerBound: ptr.From("0"),
   188  				},
   189  			},
   190  			errStr: "submit_amm.concentrated_liquidity_parameters.lower_bound (must be positive)",
   191  		},
   192  		{
   193  			submission: commandspb.SubmitAMM{
   194  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   195  					UpperBound: ptr.From(""),
   196  				},
   197  			},
   198  			errStr: "submit_amm.concentrated_liquidity_parameters.upper_bound (is not a valid number)",
   199  		},
   200  		{
   201  			submission: commandspb.SubmitAMM{
   202  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   203  					UpperBound: ptr.From("abc"),
   204  				},
   205  			},
   206  			errStr: "submit_amm.concentrated_liquidity_parameters.upper_bound (is not a valid number)",
   207  		},
   208  		{
   209  			submission: commandspb.SubmitAMM{
   210  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   211  					UpperBound: ptr.From("-10"),
   212  				},
   213  			},
   214  			errStr: "submit_amm.concentrated_liquidity_parameters.upper_bound (must be positive)",
   215  		},
   216  		{
   217  			submission: commandspb.SubmitAMM{
   218  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   219  					UpperBound: ptr.From("0"),
   220  				},
   221  			},
   222  			errStr: "submit_amm.concentrated_liquidity_parameters.upper_bound (must be positive)",
   223  		},
   224  		{
   225  			submission: commandspb.SubmitAMM{
   226  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   227  					LeverageAtUpperBound: ptr.From(""),
   228  				},
   229  			},
   230  			errStr: "submit_amm.concentrated_liquidity_parameters.leverage_at_upper_bound (is not a valid number)",
   231  		},
   232  		{
   233  			submission: commandspb.SubmitAMM{
   234  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   235  					LeverageAtUpperBound: ptr.From("abc"),
   236  				},
   237  			},
   238  			errStr: "submit_amm.concentrated_liquidity_parameters.leverage_at_upper_bound (is not a valid number)",
   239  		},
   240  		{
   241  			submission: commandspb.SubmitAMM{
   242  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   243  					LeverageAtUpperBound: ptr.From("-10"),
   244  				},
   245  			},
   246  			errStr: "submit_amm.concentrated_liquidity_parameters.leverage_at_upper_bound (must be positive)",
   247  		},
   248  		{
   249  			submission: commandspb.SubmitAMM{
   250  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   251  					LeverageAtLowerBound: ptr.From(""),
   252  				},
   253  			},
   254  			errStr: "submit_amm.concentrated_liquidity_parameters.leverage_at_lower_bound (is not a valid number)",
   255  		},
   256  		{
   257  			submission: commandspb.SubmitAMM{
   258  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   259  					LeverageAtLowerBound: ptr.From("abc"),
   260  				},
   261  			},
   262  			errStr: "submit_amm.concentrated_liquidity_parameters.leverage_at_lower_bound (is not a valid number)",
   263  		},
   264  		{
   265  			submission: commandspb.SubmitAMM{
   266  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   267  					LeverageAtLowerBound: ptr.From("-10"),
   268  				},
   269  			},
   270  			errStr: "submit_amm.concentrated_liquidity_parameters.leverage_at_lower_bound (must be positive)",
   271  		},
   272  		{
   273  			submission: commandspb.SubmitAMM{
   274  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   275  					Base:       "1000",
   276  					UpperBound: ptr.From("900"),
   277  				},
   278  			},
   279  			errStr: "submit_amm.concentrated_liquidity_parameters.base (should be a smaller value than upper_bound)",
   280  		},
   281  		{
   282  			submission: commandspb.SubmitAMM{
   283  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   284  					Base:       "1000",
   285  					LowerBound: ptr.From("1100"),
   286  				},
   287  			},
   288  			errStr: "submit_amm.concentrated_liquidity_parameters.base (should be a bigger value than lower_bound)",
   289  		},
   290  		{
   291  			submission: commandspb.SubmitAMM{
   292  				MarketId:          "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
   293  				SlippageTolerance: "0.09",
   294  				CommitmentAmount:  "10000",
   295  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   296  					Base:                 "20000",
   297  					LeverageAtUpperBound: ptr.From("0.1"),
   298  					LeverageAtLowerBound: ptr.From("0.1"),
   299  				},
   300  			},
   301  			errStr: "submit_amm.concentrated_liquidity_parameters.lower_bound (lower_bound and upper_bound cannot both be empty)",
   302  		},
   303  		{
   304  			submission: commandspb.SubmitAMM{
   305  				MarketId:          "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
   306  				SlippageTolerance: "0.09",
   307  				CommitmentAmount:  "10000",
   308  				ProposedFee:       "0.03",
   309  				ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
   310  					Base:                 "20000",
   311  					UpperBound:           ptr.From("30000"),
   312  					LowerBound:           ptr.From("10000"),
   313  					LeverageAtUpperBound: ptr.From("0.1"),
   314  					LeverageAtLowerBound: ptr.From("0.1"),
   315  				},
   316  			},
   317  		},
   318  	}
   319  
   320  	for n, c := range cases {
   321  		if len(c.errStr) <= 0 {
   322  			assert.NoError(t, commands.CheckSubmitAMM(&c.submission), n)
   323  			continue
   324  		}
   325  
   326  		assert.Contains(t, checkSubmitAMM(&c.submission).Error(), c.errStr, n)
   327  	}
   328  }
   329  
   330  func checkSubmitAMM(cmd *commandspb.SubmitAMM) commands.Errors {
   331  	err := commands.CheckSubmitAMM(cmd)
   332  
   333  	var e commands.Errors
   334  	if ok := errors.As(err, &e); !ok {
   335  		return commands.NewErrors()
   336  	}
   337  
   338  	return e
   339  }