code.vegaprotocol.io/vega@v0.79.0/commands/amend_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 TestCheckAmendAMM(t *testing.T) {
    30  	cases := []struct {
    31  		submission commandspb.AmendAMM
    32  		errStr     string
    33  	}{
    34  		{
    35  			submission: commandspb.AmendAMM{},
    36  			errStr:     "amend_amm.market_id (is required)",
    37  		},
    38  		{
    39  			submission: commandspb.AmendAMM{
    40  				MarketId: "notavalidmarketid",
    41  			},
    42  			errStr: "amend_amm.market_id (should be a valid Vega ID)",
    43  		},
    44  		{
    45  			submission: commandspb.AmendAMM{
    46  				SlippageTolerance: "",
    47  			},
    48  			errStr: "amend_amm.slippage_tolerance (is required)",
    49  		},
    50  		{
    51  			submission: commandspb.AmendAMM{
    52  				SlippageTolerance: "abc",
    53  			},
    54  			errStr: "amend_amm.slippage_tolerance (is not a valid number)",
    55  		},
    56  		{
    57  			submission: commandspb.AmendAMM{
    58  				SlippageTolerance: "-0.5",
    59  			},
    60  			errStr: "amend_amm.slippage_tolerance (must be between 0 (excluded) and 1 (included))",
    61  		},
    62  		{
    63  			submission: commandspb.AmendAMM{
    64  				SlippageTolerance: "0",
    65  			},
    66  			errStr: "amend_amm.slippage_tolerance (must be between 0 (excluded) and 1 (included))",
    67  		},
    68  		{
    69  			submission: commandspb.AmendAMM{
    70  				SlippageTolerance: "2",
    71  			},
    72  			errStr: "amend_amm.slippage_tolerance (must be between 0 (excluded) and 1 (included))",
    73  		},
    74  		{
    75  			submission: commandspb.AmendAMM{
    76  				CommitmentAmount: ptr.From(""),
    77  			},
    78  			errStr: "amend_amm.commitment_amount (is not a valid number)",
    79  		},
    80  		{
    81  			submission: commandspb.AmendAMM{
    82  				CommitmentAmount: ptr.From("abc"),
    83  			},
    84  			errStr: "amend_amm.commitment_amount (is not a valid number)",
    85  		},
    86  		{
    87  			submission: commandspb.AmendAMM{
    88  				CommitmentAmount: ptr.From("-10"),
    89  			},
    90  			errStr: "amend_amm.commitment_amount (must be positive)",
    91  		},
    92  		{
    93  			submission: commandspb.AmendAMM{
    94  				CommitmentAmount: ptr.From("0"),
    95  			},
    96  			errStr: "amend_amm.commitment_amount (must be positive)",
    97  		},
    98  		{
    99  			submission: commandspb.AmendAMM{
   100  				ProposedFee: ptr.From(""),
   101  			},
   102  			errStr: "amend_amm.proposed_fee (is not a valid value)",
   103  		},
   104  		{
   105  			submission: commandspb.AmendAMM{
   106  				ProposedFee: ptr.From("abc"),
   107  			},
   108  			errStr: "amend_amm.proposed_fee (is not a valid value)",
   109  		},
   110  		{
   111  			submission: commandspb.AmendAMM{
   112  				ProposedFee: ptr.From("-10"),
   113  			},
   114  			errStr: "amend_amm.proposed_fee (must be positive)",
   115  		},
   116  		{
   117  			submission: commandspb.AmendAMM{
   118  				ProposedFee: ptr.From("0"),
   119  			},
   120  			errStr: "amend_amm.proposed_fee (must be positive)",
   121  		},
   122  		{
   123  			submission: commandspb.AmendAMM{
   124  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   125  					Base: "",
   126  				},
   127  			},
   128  			errStr: "amend_amm.concentrated_liquidity_parameters.base (is required)",
   129  		},
   130  		{
   131  			submission: commandspb.AmendAMM{
   132  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   133  					Base: "abc",
   134  				},
   135  			},
   136  			errStr: "amend_amm.concentrated_liquidity_parameters.base (is not a valid number)",
   137  		},
   138  		{
   139  			submission: commandspb.AmendAMM{
   140  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   141  					Base: "-10",
   142  				},
   143  			},
   144  			errStr: "amend_amm.concentrated_liquidity_parameters.base (must be positive)",
   145  		},
   146  		{
   147  			submission: commandspb.AmendAMM{
   148  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   149  					Base: "0",
   150  				},
   151  			},
   152  			errStr: "amend_amm.concentrated_liquidity_parameters.base (must be positive)",
   153  		},
   154  		{
   155  			submission: commandspb.AmendAMM{
   156  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   157  					LowerBound: ptr.From(""),
   158  				},
   159  			},
   160  			errStr: "amend_amm.concentrated_liquidity_parameters.lower_bound (is not a valid number)",
   161  		},
   162  		{
   163  			submission: commandspb.AmendAMM{
   164  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   165  					LowerBound: ptr.From("abc"),
   166  				},
   167  			},
   168  			errStr: "amend_amm.concentrated_liquidity_parameters.lower_bound (is not a valid number)",
   169  		},
   170  		{
   171  			submission: commandspb.AmendAMM{
   172  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   173  					LowerBound: ptr.From("-10"),
   174  				},
   175  			},
   176  			errStr: "amend_amm.concentrated_liquidity_parameters.lower_bound (must be positive)",
   177  		},
   178  		{
   179  			submission: commandspb.AmendAMM{
   180  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   181  					LowerBound: ptr.From("0"),
   182  				},
   183  			},
   184  			errStr: "amend_amm.concentrated_liquidity_parameters.lower_bound (must be positive)",
   185  		},
   186  		{
   187  			submission: commandspb.AmendAMM{
   188  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   189  					UpperBound: ptr.From(""),
   190  				},
   191  			},
   192  			errStr: "amend_amm.concentrated_liquidity_parameters.upper_bound (is not a valid number)",
   193  		},
   194  		{
   195  			submission: commandspb.AmendAMM{
   196  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   197  					UpperBound: ptr.From("abc"),
   198  				},
   199  			},
   200  			errStr: "amend_amm.concentrated_liquidity_parameters.upper_bound (is not a valid number)",
   201  		},
   202  		{
   203  			submission: commandspb.AmendAMM{
   204  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   205  					UpperBound: ptr.From("-10"),
   206  				},
   207  			},
   208  			errStr: "amend_amm.concentrated_liquidity_parameters.upper_bound (must be positive)",
   209  		},
   210  		{
   211  			submission: commandspb.AmendAMM{
   212  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   213  					UpperBound: ptr.From("0"),
   214  				},
   215  			},
   216  			errStr: "amend_amm.concentrated_liquidity_parameters.upper_bound (must be positive)",
   217  		},
   218  		{
   219  			submission: commandspb.AmendAMM{
   220  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   221  					LeverageAtUpperBound: ptr.From(""),
   222  				},
   223  			},
   224  			errStr: "amend_amm.concentrated_liquidity_parameters.leverage_at_upper_bound (is not a valid number)",
   225  		},
   226  		{
   227  			submission: commandspb.AmendAMM{
   228  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   229  					LeverageAtUpperBound: ptr.From("abc"),
   230  				},
   231  			},
   232  			errStr: "amend_amm.concentrated_liquidity_parameters.leverage_at_upper_bound (is not a valid number)",
   233  		},
   234  		{
   235  			submission: commandspb.AmendAMM{
   236  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   237  					LeverageAtUpperBound: ptr.From("-10"),
   238  				},
   239  			},
   240  			errStr: "amend_amm.concentrated_liquidity_parameters.leverage_at_upper_bound (must be positive)",
   241  		},
   242  		{
   243  			submission: commandspb.AmendAMM{
   244  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   245  					LeverageAtLowerBound: ptr.From(""),
   246  				},
   247  			},
   248  			errStr: "amend_amm.concentrated_liquidity_parameters.leverage_at_lower_bound (is not a valid number)",
   249  		},
   250  		{
   251  			submission: commandspb.AmendAMM{
   252  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   253  					LeverageAtLowerBound: ptr.From("abc"),
   254  				},
   255  			},
   256  			errStr: "amend_amm.concentrated_liquidity_parameters.leverage_at_lower_bound (is not a valid number)",
   257  		},
   258  		{
   259  			submission: commandspb.AmendAMM{
   260  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   261  					LeverageAtLowerBound: ptr.From("-10"),
   262  				},
   263  			},
   264  			errStr: "amend_amm.concentrated_liquidity_parameters.leverage_at_lower_bound (must be positive)",
   265  		},
   266  		{
   267  			submission: commandspb.AmendAMM{
   268  				MarketId:          "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
   269  				SlippageTolerance: "0.09",
   270  			},
   271  			errStr: "* (no updates provided)",
   272  		},
   273  		{
   274  			submission: commandspb.AmendAMM{
   275  				MarketId:          "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
   276  				SlippageTolerance: "0.09",
   277  				CommitmentAmount:  ptr.From("10000"),
   278  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   279  					Base: "20000",
   280  				},
   281  			},
   282  			errStr: "amend_amm.concentrated_liquidity_parameters.lower_bound (lower_bound and upper_bound cannot both be empty)",
   283  		},
   284  		{
   285  			submission: commandspb.AmendAMM{
   286  				MarketId:          "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
   287  				SlippageTolerance: "0.09",
   288  				CommitmentAmount:  ptr.From("10000"),
   289  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   290  					LowerBound: ptr.From("10000"),
   291  					Base:       "20000",
   292  					UpperBound: ptr.From("15000"),
   293  				},
   294  			},
   295  			errStr: "amend_amm.concentrated_liquidity_parameters.base (should be a smaller value than upper_bound)",
   296  		},
   297  		{
   298  			submission: commandspb.AmendAMM{
   299  				MarketId:          "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
   300  				SlippageTolerance: "0.09",
   301  				CommitmentAmount:  ptr.From("10000"),
   302  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   303  					LowerBound: ptr.From("25000"),
   304  					Base:       "20000",
   305  					UpperBound: ptr.From("30000"),
   306  				},
   307  			},
   308  			errStr: "amend_amm.concentrated_liquidity_parameters.base (should be a bigger value than lower_bound)",
   309  		},
   310  		{
   311  			submission: commandspb.AmendAMM{
   312  				MarketId:          "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
   313  				SlippageTolerance: "0.09",
   314  				CommitmentAmount:  ptr.From("10000"),
   315  				ConcentratedLiquidityParameters: &commandspb.AmendAMM_ConcentratedLiquidityParameters{
   316  					Base:       "20000",
   317  					UpperBound: ptr.From("30000"),
   318  					LowerBound: ptr.From("10000"),
   319  				},
   320  			},
   321  		},
   322  	}
   323  
   324  	for n, c := range cases {
   325  		if len(c.errStr) <= 0 {
   326  			assert.NoError(t, commands.CheckAmendAMM(&c.submission), n)
   327  			continue
   328  		}
   329  
   330  		assert.Contains(t, checkAmendAMM(&c.submission).Error(), c.errStr, n)
   331  	}
   332  }
   333  
   334  func checkAmendAMM(cmd *commandspb.AmendAMM) commands.Errors {
   335  	err := commands.CheckAmendAMM(cmd)
   336  
   337  	var e commands.Errors
   338  	if ok := errors.As(err, &e); !ok {
   339  		return commands.NewErrors()
   340  	}
   341  
   342  	return e
   343  }