code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/the_parties_update_margin_mode.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 steps
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	"code.vegaprotocol.io/vega/libs/num"
    24  
    25  	"github.com/cucumber/godog"
    26  )
    27  
    28  func ThePartiesUpdateMarginMode(
    29  	execution Execution,
    30  	table *godog.Table,
    31  ) error {
    32  	for _, r := range parseUpdateMarginModeTable(table) {
    33  		party := r.MustStr("party")
    34  		market := r.MustStr("market")
    35  		var marginMode types.MarginMode
    36  		if r.MustStr("margin_mode") == "cross margin" {
    37  			marginMode = types.MarginModeCrossMargin
    38  		} else if r.MustStr("margin_mode") == "isolated margin" {
    39  			marginMode = types.MarginModeIsolatedMargin
    40  		} else {
    41  			panic(fmt.Errorf("invalid margin mode"))
    42  		}
    43  		factor := num.DecimalZero()
    44  		if r.HasColumn("margin_factor") && marginMode == types.MarginModeIsolatedMargin {
    45  			factor = num.MustDecimalFromString(r.MustStr("margin_factor"))
    46  		}
    47  		expErr := ""
    48  		if r.HasColumn("error") && len(r.Str("error")) > 0 {
    49  			expErr = r.Str("error")
    50  		}
    51  		err := execution.UpdateMarginMode(context.Background(), party, market, marginMode, factor)
    52  		if err != nil && len(expErr) == 0 {
    53  			return fmt.Errorf("unexpected error when updating margin mode: %v", err)
    54  		}
    55  		if len(expErr) > 0 && (err == nil || err != nil && expErr != err.Error()) {
    56  			return fmt.Errorf("invalid error expected %v got %v", expErr, err)
    57  		}
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  func parseUpdateMarginModeTable(table *godog.Table) []RowWrapper {
    64  	return StrictParseTable(table, []string{
    65  		"party",
    66  		"market",
    67  		"margin_mode",
    68  	}, []string{
    69  		"margin_factor",
    70  		"error",
    71  	})
    72  }