code.vegaprotocol.io/vega@v0.79.0/commands/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 commands 17 18 import ( 19 "fmt" 20 21 "code.vegaprotocol.io/vega/libs/num" 22 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 23 ) 24 25 func CheckUpdateMarginMode(cmd *commandspb.UpdateMarginMode) error { 26 return checkUpdateMarginMode(cmd).ErrorOrNil() 27 } 28 29 func checkUpdateMarginMode(cmd *commandspb.UpdateMarginMode) Errors { 30 errs := NewErrors() 31 32 if cmd == nil { 33 return errs.FinalAddForProperty("update_margin_mode", ErrIsRequired) 34 } 35 36 if cmd.Mode == commandspb.UpdateMarginMode_MODE_UNSPECIFIED { 37 errs.AddForProperty("update_margin_mode.margin_mode", ErrIsNotValid) 38 } 39 40 if _, ok := commandspb.UpdateMarginMode_Mode_name[int32(cmd.Mode)]; !ok { 41 errs.AddForProperty("update_margin_mode.margin_mode", ErrIsNotValid) 42 } 43 44 if cmd.Mode == commandspb.UpdateMarginMode_MODE_CROSS_MARGIN && cmd.MarginFactor != nil { 45 errs.AddForProperty("update_margin_mode.margin_factor", fmt.Errorf("margin factor must not be defined when margin mode is cross margin")) 46 } 47 48 if cmd.Mode == commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN && (cmd.MarginFactor == nil || len(*cmd.MarginFactor) == 0) { 49 errs.AddForProperty("update_margin_mode.margin_factor", fmt.Errorf("margin factor must be defined when margin mode is isolated margin")) 50 } 51 52 if cmd.Mode == commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN && cmd.MarginFactor != nil && len(*cmd.MarginFactor) > 0 { 53 if factor, err := num.DecimalFromString(*cmd.MarginFactor); err != nil { 54 errs.AddForProperty("update_margin_mode.margin_factor", ErrIsNotValidNumber) 55 } else if factor.LessThanOrEqual(num.DecimalZero()) { 56 errs.AddForProperty("update_margin_mode.margin_factor", ErrMustBePositive) 57 } 58 } 59 if len(cmd.MarketId) == 0 { 60 errs.AddForProperty("update_margin_mode.market_id", ErrIsRequired) 61 } 62 63 return errs 64 }