code.vegaprotocol.io/vega@v0.79.0/commands/liquidity_provision_submission.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  	"strconv"
    22  
    23  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    24  )
    25  
    26  var (
    27  	ErrOrderInShapeWithoutReference         = errors.New("order in shape without reference")
    28  	ErrOrderInShapeWithoutProportion        = errors.New("order in shape without a proportion")
    29  	ErrOrderInBuySideShapeWithBestAskPrice  = errors.New("order in buy side shape with best ask price reference")
    30  	ErrOrderInBuySideShapeOffsetInf0        = errors.New("order in buy side shape offset must be >= 0")
    31  	ErrOrderInBuySideShapeOffsetInfEq0      = errors.New("order in buy side shape offset must be > 0")
    32  	ErrOrderInSellSideShapeOffsetInf0       = errors.New("order in sell shape offset must be >= 0")
    33  	ErrOrderInSellSideShapeWithBestBidPrice = errors.New("order in sell side shape with best bid price reference")
    34  	ErrOrderInSellSideShapeOffsetInfEq0     = errors.New("order in sell shape offset must be > 0")
    35  )
    36  
    37  func CheckLiquidityProvisionSubmission(cmd *commandspb.LiquidityProvisionSubmission) error {
    38  	return checkLiquidityProvisionSubmission(cmd).ErrorOrNil()
    39  }
    40  
    41  func checkLiquidityProvisionSubmission(cmd *commandspb.LiquidityProvisionSubmission) Errors {
    42  	errs := NewErrors()
    43  
    44  	if cmd == nil {
    45  		return errs.FinalAddForProperty("liquidity_provision_submission", ErrIsRequired)
    46  	}
    47  
    48  	if len(cmd.MarketId) <= 0 {
    49  		errs.AddForProperty("liquidity_provision_submission.market_id", ErrIsRequired)
    50  	} else if !IsVegaID(cmd.MarketId) {
    51  		errs.AddForProperty("liquidity_provision_submission.market_id", ErrShouldBeAValidVegaID)
    52  	}
    53  
    54  	if len(cmd.Reference) > ReferenceMaxLen {
    55  		errs.AddForProperty("liquidity_provision_submission.reference", ErrReferenceTooLong)
    56  	}
    57  
    58  	// if the commitment amount is 0, then the command should be interpreted as
    59  	// a cancellation of the liquidity provision. As a result, the validation
    60  	// shouldn't be made on the rest of the field.
    61  	// However, since the user might by sending an blank command to probe the
    62  	// validation, we want to return as many error message as possible.
    63  	// A cancellation is only valid if a market is specified, and the commitment is
    64  	// 0. In any case the core will consider that as a cancellation, so we return
    65  	// the error that we go from the market id check.
    66  
    67  	if len(cmd.CommitmentAmount) > 0 {
    68  		if commitment, ok := big.NewInt(0).SetString(cmd.CommitmentAmount, 10); !ok {
    69  			errs.AddForProperty("liquidity_provision_submission.commitment_amount", ErrNotAValidInteger)
    70  		} else if commitment.Cmp(big.NewInt(0)) <= 0 {
    71  			return errs.FinalAddForProperty("liquidity_provision_submission.commitment_amount", ErrIsNotValidNumber)
    72  		}
    73  	} else {
    74  		errs.AddForProperty("liquidity_provision_submission.commitment_amount", ErrIsRequired)
    75  	}
    76  
    77  	if len(cmd.Fee) <= 0 {
    78  		errs.AddForProperty("liquidity_provision_submission.fee", ErrIsRequired)
    79  	} else {
    80  		if fee, err := strconv.ParseFloat(cmd.Fee, 64); err != nil {
    81  			errs.AddForProperty(
    82  				"liquidity_provision_submission.fee",
    83  				ErrIsNotValid,
    84  			)
    85  		} else if fee < 0 {
    86  			errs.AddForProperty("liquidity_provision_submission.fee", ErrMustBePositive)
    87  		}
    88  	}
    89  	return errs
    90  }
    91  
    92  func CheckLiquidityProvisionCancellation(cmd *commandspb.LiquidityProvisionCancellation) error {
    93  	return checkLiquidityProvisionCancellation(cmd).ErrorOrNil()
    94  }
    95  
    96  func checkLiquidityProvisionCancellation(cmd *commandspb.LiquidityProvisionCancellation) Errors {
    97  	errs := NewErrors()
    98  
    99  	if cmd == nil {
   100  		return errs.FinalAddForProperty("liquidity_provision_cancellation", ErrIsRequired)
   101  	}
   102  
   103  	if len(cmd.MarketId) <= 0 {
   104  		return errs.FinalAddForProperty("liquidity_provision_cancellation.market_id", ErrIsRequired)
   105  	} else if !IsVegaID(cmd.MarketId) {
   106  		errs.AddForProperty("liquidity_provision_cancellation.market_id", ErrShouldBeAValidVegaID)
   107  	}
   108  
   109  	return errs
   110  }
   111  
   112  func CheckLiquidityProvisionAmendment(cmd *commandspb.LiquidityProvisionAmendment) error {
   113  	return checkLiquidityProvisionAmendment(cmd).ErrorOrNil()
   114  }
   115  
   116  func checkLiquidityProvisionAmendment(cmd *commandspb.LiquidityProvisionAmendment) Errors {
   117  	errs := NewErrors()
   118  
   119  	if cmd == nil {
   120  		return errs.FinalAddForProperty("liquidity_provision_amendment", ErrIsRequired)
   121  	}
   122  
   123  	if len(cmd.MarketId) <= 0 {
   124  		return errs.FinalAddForProperty("liquidity_provision_amendment.market_id", ErrIsRequired)
   125  	}
   126  
   127  	if len(cmd.CommitmentAmount) <= 0 &&
   128  		len(cmd.Fee) <= 0 &&
   129  		len(cmd.Reference) <= 0 {
   130  		return errs.FinalAddForProperty("liquidity_provision_amendment", ErrIsRequired)
   131  	}
   132  
   133  	if len(cmd.Reference) > ReferenceMaxLen {
   134  		errs.AddForProperty("liquidity_provision_amendment.reference", ErrReferenceTooLong)
   135  	}
   136  	return errs
   137  }