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