code.vegaprotocol.io/vega@v0.79.0/commands/order_amendment.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" 21 "math/big" 22 23 types "code.vegaprotocol.io/vega/protos/vega" 24 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 25 ) 26 27 func CheckOrderAmendment(cmd *commandspb.OrderAmendment) error { 28 return checkOrderAmendment(cmd).ErrorOrNil() 29 } 30 31 func checkOrderAmendment(cmd *commandspb.OrderAmendment) Errors { 32 var ( 33 errs = NewErrors() 34 isAmending bool 35 ) 36 37 if cmd == nil { 38 return errs.FinalAddForProperty("order_amendment", ErrIsRequired) 39 } 40 41 if len(cmd.OrderId) <= 0 { 42 errs.AddForProperty("order_amendment.order_id", ErrIsRequired) 43 } else if !IsVegaID(cmd.OrderId) { 44 errs.AddForProperty("order_amendment.order_id", ErrShouldBeAValidVegaID) 45 } 46 47 if len(cmd.MarketId) <= 0 { 48 errs.AddForProperty("order_amendment.market_id", ErrIsRequired) 49 } else if !IsVegaID(cmd.MarketId) { 50 errs.AddForProperty("order_amendment.market_id", ErrShouldBeAValidVegaID) 51 } 52 53 // Check we are not trying to amend to a GFA 54 if cmd.TimeInForce == types.Order_TIME_IN_FORCE_GFA { 55 errs.AddForProperty("order_amendment.time_in_force", ErrCannotAmendToGFA) 56 } 57 58 // Check we are not trying to amend to a GFN 59 if cmd.TimeInForce == types.Order_TIME_IN_FORCE_GFN { 60 errs.AddForProperty("order_amendment.time_in_force", ErrCannotAmendToGFN) 61 } 62 63 if cmd.Price != nil { 64 isAmending = true 65 if price, ok := big.NewInt(0).SetString(*cmd.Price, 10); !ok { 66 errs.AddForProperty("order_amendment.price", ErrNotAValidInteger) 67 } else if price.Cmp(big.NewInt(0)) <= 0 { 68 errs.AddForProperty("order_amendment.price", ErrIsRequired) 69 } 70 } 71 72 if cmd.Size != nil { 73 isAmending = true 74 if *cmd.Size > math.MaxInt64/2 { 75 errs.AddForProperty("order_amendment.size", ErrSizeIsTooLarge) 76 } 77 } 78 79 if cmd.SizeDelta != 0 { 80 if cmd.Size != nil { 81 errs.AddForProperty("order_amendment.size_delta", ErrMustBeSetTo0IfSizeSet) 82 } 83 isAmending = true 84 } 85 86 if cmd.TimeInForce == types.Order_TIME_IN_FORCE_GTT { 87 isAmending = true 88 if cmd.ExpiresAt == nil { 89 errs.AddForProperty( 90 "order_amendment.time_in_force", ErrGTTOrderWithNoExpiry) 91 } 92 } 93 94 if cmd.TimeInForce != types.Order_TIME_IN_FORCE_UNSPECIFIED { 95 isAmending = true 96 if _, ok := types.Order_TimeInForce_name[int32(cmd.TimeInForce)]; !ok { 97 errs.AddForProperty("order_amendment.time_in_force", ErrIsNotValid) 98 } 99 } 100 101 if cmd.PeggedReference != types.PeggedReference_PEGGED_REFERENCE_UNSPECIFIED { 102 isAmending = true 103 if _, ok := types.PeggedReference_name[int32(cmd.PeggedReference)]; !ok { 104 errs.AddForProperty("order_amendment.pegged_reference", ErrIsNotValid) 105 } 106 } 107 108 if cmd.ExpiresAt != nil && *cmd.ExpiresAt > 0 { 109 isAmending = true 110 if cmd.TimeInForce != types.Order_TIME_IN_FORCE_GTT && 111 cmd.TimeInForce != types.Order_TIME_IN_FORCE_UNSPECIFIED { 112 errs.AddForProperty( 113 "order_amendment.expires_at", ErrNonGTTOrderWithExpiry) 114 } 115 } 116 117 if cmd.PeggedOffset != "" { 118 isAmending = true 119 if peggedOffset, ok := big.NewInt(0).SetString(cmd.PeggedOffset, 10); !ok { 120 errs.AddForProperty("order_amendment.pegged_offset", ErrNotAValidInteger) 121 } else if peggedOffset.Cmp(big.NewInt(0)) <= 0 { 122 errs.AddForProperty("order_amendment.pegged_offset", ErrMustBePositive) 123 } 124 } 125 126 if !isAmending { 127 errs.Add(errors.New("order_amendment does not amend anything")) 128 } 129 130 return errs 131 }