code.vegaprotocol.io/vega@v0.79.0/commands/update_margin_mode_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  	"fmt"
    21  	"testing"
    22  
    23  	"code.vegaprotocol.io/vega/commands"
    24  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestUpdateMarginMode(t *testing.T) {
    30  	positiveMarginFactor := "123"
    31  	invalidNumderMarginFactor := "banana"
    32  	invalidDecimalMarginFactor := "1.2.3"
    33  	negativeMarginFactor := "-0.2"
    34  	zeroMarginFactor := "0"
    35  	largeMarginFactor101 := "1.01"
    36  	validMarginFactor1 := "1"
    37  	validMarginFactorHalf := "0.5"
    38  
    39  	txs := []struct {
    40  		name    string
    41  		cmd     *commandspb.UpdateMarginMode
    42  		errName string
    43  		err     error
    44  	}{
    45  		{
    46  			"cmd is nil",
    47  			nil,
    48  			"update_margin_mode",
    49  			commands.ErrIsRequired,
    50  		},
    51  		{
    52  			"unspecified mode",
    53  			&commandspb.UpdateMarginMode{
    54  				Mode: commandspb.UpdateMarginMode_MODE_UNSPECIFIED,
    55  			},
    56  			"update_margin_mode.margin_mode",
    57  			commands.ErrIsNotValid,
    58  		},
    59  		{
    60  			"isolated margin mode without margin factor",
    61  			&commandspb.UpdateMarginMode{
    62  				Mode: commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN,
    63  			},
    64  			"update_margin_mode.margin_factor",
    65  			fmt.Errorf("margin factor must be defined when margin mode is isolated margin"),
    66  		},
    67  		{
    68  			"cross margin mode with margin factor",
    69  			&commandspb.UpdateMarginMode{
    70  				Mode:         commandspb.UpdateMarginMode_MODE_CROSS_MARGIN,
    71  				MarginFactor: &positiveMarginFactor,
    72  			},
    73  			"update_margin_mode.margin_factor",
    74  			fmt.Errorf("margin factor must not be defined when margin mode is cross margin"),
    75  		},
    76  		{
    77  			"cross margin mode with invalid number as margin factor 1",
    78  			&commandspb.UpdateMarginMode{
    79  				Mode:         commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN,
    80  				MarginFactor: &invalidNumderMarginFactor,
    81  			},
    82  			"update_margin_mode.margin_factor",
    83  			commands.ErrIsNotValidNumber,
    84  		},
    85  		{
    86  			"cross margin mode with invalid number as margin factor 2",
    87  			&commandspb.UpdateMarginMode{
    88  				Mode:         commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN,
    89  				MarginFactor: &invalidDecimalMarginFactor,
    90  			},
    91  			"update_margin_mode.margin_factor",
    92  			commands.ErrIsNotValidNumber,
    93  		},
    94  		{
    95  			"cross margin mode with negative number as margin factor",
    96  			&commandspb.UpdateMarginMode{
    97  				Mode:         commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN,
    98  				MarginFactor: &negativeMarginFactor,
    99  			},
   100  			"update_margin_mode.margin_factor",
   101  			commands.ErrMustBePositive,
   102  		},
   103  		{
   104  			"cross margin mode with 0 as margin factor",
   105  			&commandspb.UpdateMarginMode{
   106  				Mode:         commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN,
   107  				MarginFactor: &zeroMarginFactor,
   108  			},
   109  			"update_margin_mode.margin_factor",
   110  			commands.ErrMustBePositive,
   111  		},
   112  		{
   113  			"cross margin mode with >1 as margin factor",
   114  			&commandspb.UpdateMarginMode{
   115  				Mode:         commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN,
   116  				MarginFactor: &largeMarginFactor101,
   117  				MarketId:     "123",
   118  			},
   119  			"update_margin_mode.margin_factor",
   120  			nil,
   121  		},
   122  		{
   123  			"cross margin mode with missing market id",
   124  			&commandspb.UpdateMarginMode{
   125  				Mode:         commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN,
   126  				MarginFactor: &validMarginFactor1,
   127  			},
   128  			"update_margin_mode.market_id",
   129  			commands.ErrIsRequired,
   130  		},
   131  		{
   132  			"valid cross margin update",
   133  			&commandspb.UpdateMarginMode{
   134  				Mode:     commandspb.UpdateMarginMode_MODE_CROSS_MARGIN,
   135  				MarketId: "123",
   136  			},
   137  			"",
   138  			nil,
   139  		},
   140  		{
   141  			"valid isolated margin update",
   142  			&commandspb.UpdateMarginMode{
   143  				Mode:         commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN,
   144  				MarketId:     "123",
   145  				MarginFactor: &validMarginFactorHalf,
   146  			},
   147  			"",
   148  			nil,
   149  		},
   150  	}
   151  
   152  	for _, v := range txs {
   153  		err := checkUpdateMarginMode(t, v.cmd)
   154  		if v.err == nil {
   155  			require.Empty(t, len(err), v.name)
   156  		} else {
   157  			require.Contains(t, err.Get(v.errName), v.err, v.name)
   158  		}
   159  	}
   160  }
   161  
   162  func checkUpdateMarginMode(t *testing.T, cmd *commandspb.UpdateMarginMode) commands.Errors {
   163  	t.Helper()
   164  	err := commands.CheckUpdateMarginMode(cmd)
   165  	var e commands.Errors
   166  	if ok := errors.As(err, &e); !ok {
   167  		return commands.NewErrors()
   168  	}
   169  	return e
   170  }