code.vegaprotocol.io/vega@v0.79.0/core/matching/validation.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 matching 17 18 import ( 19 "encoding/hex" 20 21 "code.vegaprotocol.io/vega/core/types" 22 "code.vegaprotocol.io/vega/libs/num" 23 "code.vegaprotocol.io/vega/logging" 24 ) 25 26 const ( 27 orderIDLen = 64 28 ) 29 30 func (b OrderBook) validateOrder(orderMessage *types.Order) (err error) { 31 if orderMessage.Price == nil { 32 orderMessage.Price = num.UintZero() 33 } 34 if orderMessage.MarketID != b.marketID { 35 b.log.Error("Market ID mismatch", 36 logging.String("market", orderMessage.MarketID), 37 logging.String("order-book", b.marketID), 38 logging.Order(*orderMessage)) 39 err = types.ErrInvalidMarketID 40 } else if orderMessage.Type == types.OrderTypeUnspecified { 41 err = types.ErrInvalidType 42 } else if orderMessage.Remaining == 0 { 43 err = types.ErrInvalidRemainingSize 44 } else if orderMessage.TimeInForce == types.OrderTimeInForceGTT && orderMessage.ExpiresAt == 0 { 45 // if order is GTT, validate timestamp and convert to block number 46 err = types.ErrInvalidExpirationDatetime 47 } else if len(orderMessage.Party) == 0 { 48 err = types.ErrInvalidPartyID 49 } else if orderMessage.Size == 0 { 50 err = types.ErrInvalidSize 51 } else if orderMessage.Remaining > orderMessage.Size { 52 err = types.ErrInvalidRemainingSize 53 } else if orderMessage.Type == types.OrderTypeNetwork && orderMessage.TimeInForce != types.OrderTimeInForceFOK { 54 err = types.ErrInvalidPersistence 55 } else if orderMessage.TimeInForce == types.OrderTimeInForceGTT && orderMessage.Type != types.OrderTypeLimit { 56 err = types.ErrInvalidPersistence 57 } else if orderMessage.Type == types.OrderTypeMarket && 58 (orderMessage.TimeInForce == types.OrderTimeInForceGTT || orderMessage.TimeInForce == types.OrderTimeInForceGTC) { 59 err = types.ErrInvalidPersistence 60 } else if b.auction && orderMessage.TimeInForce == types.OrderTimeInForceGFN { 61 err = types.ErrInvalidTimeInForce 62 } else if !b.auction && orderMessage.TimeInForce == types.OrderTimeInForceGFA { 63 err = types.ErrInvalidTimeInForce 64 } else if orderMessage.ExpiresAt > 0 && orderMessage.Type == types.OrderTypeMarket { 65 err = types.ErrInvalidExpirationDatetime 66 } 67 68 return err 69 } 70 71 func validateOrderID(orderID string) error { 72 _, err := hex.DecodeString(orderID) 73 idLen := len(orderID) 74 if err != nil || idLen != orderIDLen { 75 return types.ErrInvalidOrderID 76 } 77 return nil 78 }