code.vegaprotocol.io/vega@v0.79.0/core/matching/cancelorders_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 matching 17 18 import ( 19 "testing" 20 21 "code.vegaprotocol.io/vega/core/types" 22 vgcrypto "code.vegaprotocol.io/vega/libs/crypto" 23 "code.vegaprotocol.io/vega/libs/num" 24 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestOrderBookSimple_CancelWrongOrderIncorrectOrderID(t *testing.T) { 29 market := "testMarket" 30 book := getTestOrderBook(t, market) 31 defer book.Finish() 32 order := types.Order{ 33 MarketID: market, 34 Party: "A", 35 Side: types.SideBuy, 36 Price: num.NewUint(100), 37 Size: 10, 38 Remaining: 10, 39 TimeInForce: types.OrderTimeInForceGTC, 40 Type: types.OrderTypeLimit, 41 ID: "v0000000000000-0000001", 42 } 43 confirm, err := book.SubmitOrder(&order) 44 assert.NoError(t, err) 45 assert.Equal(t, 0, len(confirm.Trades)) 46 47 order2 := types.Order{ 48 MarketID: market, 49 Party: "A", 50 Side: types.SideBuy, 51 Price: num.NewUint(100), 52 Size: 10, 53 Remaining: 10, 54 TimeInForce: types.OrderTimeInForceGTC, 55 Type: types.OrderTypeLimit, 56 ID: "v0000000000000-0000002", // Invalid, must match original 57 } 58 _, err = book.CancelOrder(&order2) 59 assert.Error(t, err, types.ErrOrderRemovalFailure) 60 assert.Equal(t, book.getNumberOfBuyLevels(), 1) 61 assert.Equal(t, book.getNumberOfSellLevels(), 0) 62 } 63 64 func TestOrderBookSimple_CancelWrongOrderIncorrectMarketID(t *testing.T) { 65 market := "testMarket" 66 book := getTestOrderBook(t, market) 67 defer book.Finish() 68 order := types.Order{ 69 MarketID: market, 70 Party: "A", 71 Side: types.SideBuy, 72 Price: num.NewUint(100), 73 Size: 10, 74 Remaining: 10, 75 TimeInForce: types.OrderTimeInForceGTC, 76 Type: types.OrderTypeLimit, 77 ID: "v0000000000000-0000001", 78 } 79 confirm, err := book.SubmitOrder(&order) 80 assert.NoError(t, err) 81 assert.Equal(t, 0, len(confirm.Trades)) 82 83 order2 := types.Order{ 84 MarketID: "incorrectMarket", // Invalid, must match original 85 Party: "A", 86 Side: types.SideBuy, 87 Price: num.NewUint(100), 88 Size: 10, 89 Remaining: 10, 90 TimeInForce: types.OrderTimeInForceGTC, 91 Type: types.OrderTypeLimit, 92 ID: "v0000000000000-0000001", 93 } 94 95 assert.Panics(t, func() { 96 _, err = book.CancelOrder(&order2) 97 }, 98 ) 99 100 assert.Equal(t, book.getNumberOfBuyLevels(), 1) 101 assert.Equal(t, book.getNumberOfSellLevels(), 0) 102 } 103 104 func TestOrderBookSimple_CancelWrongOrderIncorrectSide(t *testing.T) { 105 market := "testMarket" 106 book := getTestOrderBook(t, market) 107 defer book.Finish() 108 order := types.Order{ 109 MarketID: market, 110 Party: "A", 111 Side: types.SideBuy, 112 Price: num.NewUint(100), 113 Size: 10, 114 Remaining: 10, 115 TimeInForce: types.OrderTimeInForceGTC, 116 Type: types.OrderTypeLimit, 117 ID: "v0000000000000-0000001", 118 } 119 confirm, err := book.SubmitOrder(&order) 120 assert.NoError(t, err) 121 assert.Equal(t, 0, len(confirm.Trades)) 122 123 order2 := types.Order{ 124 MarketID: market, 125 Party: "A", 126 Side: types.SideSell, // Invalid, must match original 127 Price: num.NewUint(100), 128 Size: 10, 129 Remaining: 10, 130 TimeInForce: types.OrderTimeInForceGTC, 131 Type: types.OrderTypeLimit, 132 ID: "v0000000000000-0000001", 133 } 134 _, err = book.CancelOrder(&order2) 135 assert.Error(t, err, types.ErrOrderRemovalFailure) 136 assert.Equal(t, book.getNumberOfBuyLevels(), 1) 137 assert.Equal(t, book.getNumberOfSellLevels(), 0) 138 } 139 140 func TestOrderBookSimple_CancelWrongOrderIncorrectPrice(t *testing.T) { 141 market := "testMarket" 142 book := getTestOrderBook(t, market) 143 defer book.Finish() 144 order := types.Order{ 145 MarketID: market, 146 Party: "A", 147 Side: types.SideBuy, 148 Price: num.NewUint(100), 149 Size: 10, 150 Remaining: 10, 151 TimeInForce: types.OrderTimeInForceGTC, 152 Type: types.OrderTypeLimit, 153 ID: "v0000000000000-0000001", 154 } 155 confirm, err := book.SubmitOrder(&order) 156 assert.NoError(t, err) 157 assert.Equal(t, 0, len(confirm.Trades)) 158 159 order2 := types.Order{ 160 MarketID: market, 161 Party: "A", 162 Side: types.SideBuy, 163 Price: num.NewUint(101), // Invalid, must match original 164 Size: 10, 165 Remaining: 10, 166 TimeInForce: types.OrderTimeInForceGTC, 167 Type: types.OrderTypeLimit, 168 ID: "v0000000000000-0000001", 169 } 170 _, err = book.CancelOrder(&order2) 171 assert.Error(t, err, types.ErrOrderRemovalFailure) 172 assert.Equal(t, book.getNumberOfBuyLevels(), 1) 173 assert.Equal(t, book.getNumberOfSellLevels(), 0) 174 } 175 176 func TestOrderBookSimple_CancelOrderIncorrectNonCriticalFields(t *testing.T) { 177 market := "testMarket" 178 book := getTestOrderBook(t, market) 179 defer book.Finish() 180 181 orderID := vgcrypto.RandomHash() 182 order := types.Order{ 183 MarketID: market, 184 Party: "A", 185 Side: types.SideBuy, 186 Price: num.NewUint(100), 187 Size: 10, 188 Remaining: 10, 189 TimeInForce: types.OrderTimeInForceGTC, 190 Type: types.OrderTypeLimit, 191 ID: orderID, 192 } 193 confirm, err := book.SubmitOrder(&order) 194 assert.NoError(t, err) 195 assert.Equal(t, 0, len(confirm.Trades)) 196 197 order2 := types.Order{ 198 MarketID: market, // Must match 199 Party: "B", // Does not matter 200 Side: types.SideBuy, // Must match 201 Price: num.NewUint(100), // Must match 202 Size: 10, // Does not matter 203 Remaining: 10, // Does not matter 204 TimeInForce: types.OrderTimeInForceGTC, // Does not matter 205 Type: types.OrderTypeLimit, // Does not matter 206 ID: orderID, // Must match 207 } 208 _, err = book.CancelOrder(&order2) 209 assert.NoError(t, err) 210 assert.Equal(t, book.getNumberOfBuyLevels(), 0) 211 assert.Equal(t, book.getNumberOfSellLevels(), 0) 212 }