code.vegaprotocol.io/vega@v0.79.0/commands/withdraw_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  
    22  	"code.vegaprotocol.io/vega/libs/crypto"
    23  	types "code.vegaprotocol.io/vega/protos/vega"
    24  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    25  )
    26  
    27  func CheckWithdrawSubmission(cmd *commandspb.WithdrawSubmission) error {
    28  	return checkWithdrawSubmission(cmd).ErrorOrNil()
    29  }
    30  
    31  func checkWithdrawSubmission(cmd *commandspb.WithdrawSubmission) Errors {
    32  	errs := NewErrors()
    33  
    34  	if cmd == nil {
    35  		return errs.FinalAddForProperty("withdraw_submission", ErrIsRequired)
    36  	}
    37  
    38  	if len(cmd.Amount) <= 0 {
    39  		errs.AddForProperty("withdraw_submission.amount", ErrIsRequired)
    40  	} else {
    41  		if amount, ok := big.NewInt(0).SetString(cmd.Amount, 10); !ok {
    42  			errs.AddForProperty("withdraw_submission.amount", ErrNotAValidInteger)
    43  		} else if amount.Cmp(big.NewInt(0)) <= 0 {
    44  			errs.AddForProperty("withdraw_submission.amount", ErrIsRequired)
    45  		}
    46  	}
    47  
    48  	if len(cmd.Asset) <= 0 {
    49  		errs.AddForProperty("withdraw_submission.asset", ErrIsRequired)
    50  	} else if !IsVegaID(cmd.Asset) {
    51  		errs.AddForProperty("withdraw_submission.asset", ErrShouldBeAValidVegaID)
    52  	}
    53  
    54  	if cmd.Ext != nil {
    55  		errs.Merge(checkWithdrawExt(cmd.Ext))
    56  	}
    57  
    58  	return errs
    59  }
    60  
    61  func checkWithdrawExt(wext *types.WithdrawExt) Errors {
    62  	errs := NewErrors()
    63  	switch v := wext.Ext.(type) {
    64  	case *types.WithdrawExt_Erc20:
    65  		if len(v.Erc20.ReceiverAddress) <= 0 {
    66  			errs.AddForProperty(
    67  				"withdraw_ext.erc20.received_address",
    68  				ErrIsRequired,
    69  			)
    70  		} else if !crypto.EthereumIsValidAddress(v.Erc20.ReceiverAddress) {
    71  			errs.AddForProperty("withdraw_ext.erc20.received_address", ErrIsNotValidEthereumAddress)
    72  		}
    73  	default:
    74  		errs.AddForProperty("withdraw_ext.ext", errors.New("unsupported withdraw extended details"))
    75  	}
    76  	return errs
    77  }