code.vegaprotocol.io/vega@v0.79.0/commands/delegate_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  	"math/big"
    20  
    21  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    22  )
    23  
    24  func CheckDelegateSubmission(cmd *commandspb.DelegateSubmission) error {
    25  	return checkDelegateSubmission(cmd).ErrorOrNil()
    26  }
    27  
    28  func checkDelegateSubmission(cmd *commandspb.DelegateSubmission) Errors {
    29  	errs := NewErrors()
    30  
    31  	if cmd == nil {
    32  		return errs.FinalAddForProperty("delegate_submission", ErrIsRequired)
    33  	}
    34  
    35  	if len(cmd.Amount) <= 0 {
    36  		errs.AddForProperty("delegate_submission.amount", ErrIsRequired)
    37  	} else {
    38  		if amount, ok := big.NewInt(0).SetString(cmd.Amount, 10); !ok {
    39  			errs.AddForProperty("delegate_submission.amount", ErrNotAValidInteger)
    40  		} else {
    41  			if amount.Cmp(big.NewInt(0)) <= 0 {
    42  				errs.AddForProperty("delegate_submission.amount", ErrIsRequired)
    43  			}
    44  		}
    45  	}
    46  
    47  	if len(cmd.NodeId) <= 0 {
    48  		errs.AddForProperty("delegate_submission.node_id", ErrIsRequired)
    49  	} else if !IsVegaPublicKey(cmd.NodeId) {
    50  		errs.AddForProperty("delegate_submission.node_id", ErrShouldBeAValidVegaPublicKey)
    51  	}
    52  
    53  	return errs
    54  }
    55  
    56  func CheckUndelegateSubmission(cmd *commandspb.UndelegateSubmission) error {
    57  	return checkUndelegateSubmission(cmd).ErrorOrNil()
    58  }
    59  
    60  func checkUndelegateSubmission(cmd *commandspb.UndelegateSubmission) Errors {
    61  	errs := NewErrors()
    62  
    63  	if cmd == nil {
    64  		return errs.FinalAddForProperty("undelegate_submission", ErrIsRequired)
    65  	}
    66  
    67  	if _, ok := commandspb.UndelegateSubmission_Method_value[cmd.Method.String()]; !ok || cmd.Method == commandspb.UndelegateSubmission_METHOD_UNSPECIFIED {
    68  		errs.AddForProperty("undelegate_submission.method", ErrIsRequired)
    69  	}
    70  
    71  	if len(cmd.NodeId) <= 0 {
    72  		errs.AddForProperty("undelegate_submission.node_id", ErrIsRequired)
    73  	} else if !IsVegaPublicKey(cmd.NodeId) {
    74  		errs.AddForProperty("undelegate_submission.node_id", ErrShouldBeAValidVegaPublicKey)
    75  	}
    76  
    77  	return errs
    78  }