code.vegaprotocol.io/vega@v0.79.0/commands/amend_amm.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
    17  
    18  import (
    19  	"errors"
    20  	"math/big"
    21  
    22  	"code.vegaprotocol.io/vega/libs/num"
    23  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    24  )
    25  
    26  func CheckAmendAMM(cmd *commandspb.AmendAMM) error {
    27  	return checkAmendAMM(cmd).ErrorOrNil()
    28  }
    29  
    30  func checkAmendAMM(cmd *commandspb.AmendAMM) Errors {
    31  	errs := NewErrors()
    32  
    33  	if cmd == nil {
    34  		return errs.FinalAddForProperty("amend_amm", ErrIsRequired)
    35  	}
    36  
    37  	if len(cmd.MarketId) <= 0 {
    38  		errs.AddForProperty("amend_amm.market_id", ErrIsRequired)
    39  	} else if !IsVegaID(cmd.MarketId) {
    40  		errs.AddForProperty("amend_amm.market_id", ErrShouldBeAValidVegaID)
    41  	}
    42  
    43  	if len(cmd.SlippageTolerance) <= 0 {
    44  		errs.AddForProperty("amend_amm.slippage_tolerance", ErrIsRequired)
    45  	} else if slippageTolerance, err := num.DecimalFromString(cmd.SlippageTolerance); err != nil {
    46  		errs.AddForProperty("amend_amm.slippage_tolerance", ErrIsNotValidNumber)
    47  	} else if slippageTolerance.LessThanOrEqual(num.DecimalZero()) || slippageTolerance.GreaterThan(num.DecimalOne()) {
    48  		errs.AddForProperty("amend_amm.slippage_tolerance", ErrMustBeBetween01)
    49  	}
    50  
    51  	var hasUpdate bool
    52  
    53  	if cmd.CommitmentAmount != nil {
    54  		hasUpdate = true
    55  		if amount, _ := big.NewInt(0).SetString(*cmd.CommitmentAmount, 10); amount == nil {
    56  			errs.FinalAddForProperty("amend_amm.commitment_amount", ErrIsNotValidNumber)
    57  		} else if amount.Cmp(big.NewInt(0)) <= 0 {
    58  			errs.AddForProperty("amend_amm.commitment_amount", ErrMustBePositive)
    59  		}
    60  	}
    61  
    62  	if cmd.ProposedFee != nil {
    63  		hasUpdate = true
    64  		if proposedFee, err := num.DecimalFromString(*cmd.ProposedFee); err != nil {
    65  			errs.AddForProperty("amend_amm.proposed_fee", ErrIsNotValid)
    66  		} else if proposedFee.LessThanOrEqual(num.DecimalZero()) {
    67  			errs.AddForProperty("amend_amm.proposed_fee", ErrMustBePositive)
    68  		}
    69  	}
    70  
    71  	if cmd.MinimumPriceChangeTrigger != nil {
    72  		if minPriceChange, err := num.DecimalFromString(*cmd.MinimumPriceChangeTrigger); err != nil {
    73  			errs.AddForProperty("submit_amm.mimimum_price_change_trigger", ErrIsNotValid)
    74  		} else if minPriceChange.LessThan(num.DecimalZero()) {
    75  			errs.AddForProperty("submit_amm.proposed_fee", ErrMustBePositiveOrZero)
    76  		}
    77  	}
    78  
    79  	if cmd.ConcentratedLiquidityParameters != nil {
    80  		var haveLower, haveUpper, emptyBase bool
    81  		hasUpdate = true
    82  		var base, lowerBound, upperBound *big.Int
    83  		if len(cmd.ConcentratedLiquidityParameters.Base) == 0 {
    84  			emptyBase = true
    85  		} else if base, _ = big.NewInt(0).SetString(cmd.ConcentratedLiquidityParameters.Base, 10); base == nil {
    86  			errs.FinalAddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrIsNotValidNumber)
    87  		} else if base.Cmp(big.NewInt(0)) <= 0 {
    88  			errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrMustBePositive)
    89  		}
    90  
    91  		if cmd.ConcentratedLiquidityParameters.LowerBound != nil {
    92  			haveLower = true
    93  			if lowerBound, _ = big.NewInt(0).SetString(*cmd.ConcentratedLiquidityParameters.LowerBound, 10); lowerBound == nil {
    94  				errs.FinalAddForProperty("amend_amm.concentrated_liquidity_parameters.lower_bound", ErrIsNotValidNumber)
    95  			} else if lowerBound.Cmp(big.NewInt(0)) <= 0 {
    96  				errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.lower_bound", ErrMustBePositive)
    97  			}
    98  		}
    99  		if cmd.ConcentratedLiquidityParameters.UpperBound != nil {
   100  			haveUpper = true
   101  			if upperBound, _ = big.NewInt(0).SetString(*cmd.ConcentratedLiquidityParameters.UpperBound, 10); upperBound == nil {
   102  				errs.FinalAddForProperty("amend_amm.concentrated_liquidity_parameters.upper_bound", ErrIsNotValidNumber)
   103  			} else if upperBound.Cmp(big.NewInt(0)) <= 0 {
   104  				errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.upper_bound", ErrMustBePositive)
   105  			}
   106  		}
   107  
   108  		if !haveLower && !haveUpper {
   109  			errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.lower_bound", errors.New("lower_bound and upper_bound cannot both be empty"))
   110  		}
   111  
   112  		if base != nil && lowerBound != nil && base.Cmp(lowerBound) <= 0 {
   113  			errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.base", errors.New("should be a bigger value than lower_bound"))
   114  		}
   115  
   116  		if base != nil && upperBound != nil && base.Cmp(upperBound) >= 0 {
   117  			errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.base", errors.New("should be a smaller value than upper_bound"))
   118  		}
   119  
   120  		if cmd.ConcentratedLiquidityParameters.LeverageAtUpperBound != nil {
   121  			if leverage, err := num.DecimalFromString(*cmd.ConcentratedLiquidityParameters.LeverageAtUpperBound); err != nil {
   122  				errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.leverage_at_upper_bound", ErrIsNotValidNumber)
   123  			} else if leverage.LessThan(num.DecimalZero()) {
   124  				errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.leverage_at_upper_bound", ErrMustBePositive)
   125  			}
   126  		}
   127  
   128  		if cmd.ConcentratedLiquidityParameters.LeverageAtLowerBound != nil {
   129  			if leverage, err := num.DecimalFromString(*cmd.ConcentratedLiquidityParameters.LeverageAtLowerBound); err != nil {
   130  				errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.leverage_at_lower_bound", ErrIsNotValidNumber)
   131  			} else if leverage.LessThan(num.DecimalZero()) {
   132  				errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.leverage_at_lower_bound", ErrMustBePositive)
   133  			}
   134  		}
   135  
   136  		if len(cmd.SlippageTolerance) <= 0 {
   137  			errs.AddForProperty("amend_amm.slippage_tolerance", ErrIsRequired)
   138  		} else if slippageTolerance, err := num.DecimalFromString(cmd.SlippageTolerance); err != nil {
   139  			errs.AddForProperty("amend_amm.slippage_tolerance", ErrIsNotValidNumber)
   140  		} else if slippageTolerance.LessThan(num.DecimalZero()) {
   141  			errs.AddForProperty("amend_amm.slippage_tolerance", ErrMustBePositive)
   142  		}
   143  
   144  		if cmd.ConcentratedLiquidityParameters.DataSourceId == nil && emptyBase {
   145  			errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrIsRequired)
   146  		}
   147  
   148  		if cmd.ConcentratedLiquidityParameters.DataSourceId != nil && !IsVegaID(*cmd.ConcentratedLiquidityParameters.DataSourceId) {
   149  			errs.AddForProperty("amend_amm.data_source_id", ErrShouldBeAValidVegaID)
   150  		}
   151  	}
   152  
   153  	// no update, but also no error, invalid
   154  	if !hasUpdate && errs.Empty() {
   155  		errs.FinalAdd(ErrNoUpdatesProvided)
   156  	}
   157  
   158  	return errs
   159  }