code.vegaprotocol.io/vega@v0.79.0/commands/withdraw_submission_test.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_test
    17  
    18  import (
    19  	"errors"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/commands"
    23  	types "code.vegaprotocol.io/vega/protos/vega"
    24  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestNilWithdrawSubmissionFails(t *testing.T) {
    30  	err := checkWithdrawSubmission(nil)
    31  
    32  	assert.Contains(t, err.Get("withdraw_submission"), commands.ErrIsRequired)
    33  }
    34  
    35  func TestWithdrawSubmission(t *testing.T) {
    36  	cases := []struct {
    37  		withdraw  commandspb.WithdrawSubmission
    38  		errString string
    39  	}{
    40  		{
    41  			withdraw: commandspb.WithdrawSubmission{
    42  				Amount: "100",
    43  				Asset:  "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    44  			},
    45  		},
    46  		{
    47  			withdraw: commandspb.WithdrawSubmission{
    48  				Amount: "100",
    49  				Asset:  "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    50  				Ext: &types.WithdrawExt{
    51  					Ext: &types.WithdrawExt_Erc20{
    52  						Erc20: &types.Erc20WithdrawExt{
    53  							ReceiverAddress: "0x9135f5afd6F055e731bca2348429482eE614CFfA",
    54  						},
    55  					},
    56  				},
    57  			},
    58  		},
    59  		{
    60  			withdraw: commandspb.WithdrawSubmission{
    61  				Amount: "100",
    62  				Asset:  "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    63  				Ext: &types.WithdrawExt{
    64  					Ext: &types.WithdrawExt_Erc20{
    65  						Erc20: &types.Erc20WithdrawExt{
    66  							ReceiverAddress: "0xsomething",
    67  						},
    68  					},
    69  				},
    70  			},
    71  			errString: "withdraw_ext.erc20.received_address (is not a valid ethereum address)",
    72  		},
    73  		{
    74  			withdraw: commandspb.WithdrawSubmission{
    75  				Asset: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    76  			},
    77  			errString: "withdraw_submission.amount (is required)",
    78  		},
    79  		{
    80  			withdraw: commandspb.WithdrawSubmission{
    81  				Amount: "100",
    82  			},
    83  			errString: "withdraw_submission.asset (is required)",
    84  		},
    85  		{
    86  			withdraw:  commandspb.WithdrawSubmission{},
    87  			errString: "withdraw_submission.amount (is required), withdraw_submission.asset (is required)",
    88  		},
    89  		{
    90  			withdraw: commandspb.WithdrawSubmission{
    91  				Ext: &types.WithdrawExt{},
    92  			},
    93  			errString: "withdraw_ext.ext (unsupported withdraw extended details), withdraw_submission.amount (is required), withdraw_submission.asset (is required)",
    94  		},
    95  		{
    96  			withdraw: commandspb.WithdrawSubmission{
    97  				Amount: "100",
    98  				Asset:  "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    99  				Ext: &types.WithdrawExt{
   100  					Ext: &types.WithdrawExt_Erc20{
   101  						Erc20: &types.Erc20WithdrawExt{},
   102  					},
   103  				},
   104  			},
   105  			errString: "withdraw_ext.erc20.received_address (is required)",
   106  		},
   107  		{
   108  			withdraw: commandspb.WithdrawSubmission{
   109  				Ext: &types.WithdrawExt{
   110  					Ext: &types.WithdrawExt_Erc20{
   111  						Erc20: &types.Erc20WithdrawExt{},
   112  					},
   113  				},
   114  			},
   115  			errString: "withdraw_ext.erc20.received_address (is required), withdraw_submission.amount (is required), withdraw_submission.asset (is required)",
   116  		},
   117  	}
   118  
   119  	for _, c := range cases {
   120  		err := commands.CheckWithdrawSubmission(&c.withdraw)
   121  		if len(c.errString) <= 0 {
   122  			assert.NoError(t, err)
   123  			continue
   124  		}
   125  		assert.Error(t, err)
   126  		assert.EqualError(t, err, c.errString)
   127  	}
   128  }
   129  
   130  func checkWithdrawSubmission(cmd *commandspb.WithdrawSubmission) commands.Errors {
   131  	err := commands.CheckWithdrawSubmission(cmd)
   132  
   133  	var e commands.Errors
   134  	if ok := errors.As(err, &e); !ok {
   135  		return commands.NewErrors()
   136  	}
   137  
   138  	return e
   139  }