code.vegaprotocol.io/vega@v0.79.0/core/matching/networkorder_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_test 17 18 import ( 19 "testing" 20 21 "code.vegaprotocol.io/vega/core/types" 22 "code.vegaprotocol.io/vega/libs/num" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 // reproducing bug from https://github.com/vegaprotocol/vega/issues/2180 28 29 func TestNetworkOrder_ValidAveragedPrice(t *testing.T) { 30 market := "testMarket" 31 book := getTestOrderBook(t, market) 32 defer book.Finish() 33 34 orders := []types.Order{ 35 { 36 MarketID: market, 37 Status: types.OrderStatusActive, 38 Party: "A", 39 Side: types.SideBuy, 40 Price: num.NewUint(100), 41 OriginalPrice: num.NewUint(100), 42 Size: 4, 43 Remaining: 4, 44 TimeInForce: types.OrderTimeInForceGTC, 45 Type: types.OrderTypeLimit, 46 ID: "v0000000000000-0000001", 47 }, 48 { 49 MarketID: market, 50 Status: types.OrderStatusActive, 51 Party: "B", 52 Side: types.SideBuy, 53 Price: num.NewUint(75), 54 OriginalPrice: num.NewUint(75), 55 Size: 4, 56 Remaining: 4, 57 TimeInForce: types.OrderTimeInForceGTC, 58 Type: types.OrderTypeLimit, 59 ID: "v0000000000000-0000002", 60 }, 61 } 62 63 var ( 64 totalSize uint64 65 totalPrice, expectedPrice = num.UintZero(), num.NewUint(0) 66 ) 67 for _, v := range orders { 68 v := v 69 _, err := book.ob.SubmitOrder(&v) 70 assert.NoError(t, err) 71 // totalPrice += v.Price * v.Size 72 totalPrice.Add( 73 totalPrice, 74 num.UintZero().Mul(v.Price, num.NewUint(v.Size)), 75 ) 76 totalSize += v.Size 77 } 78 expectedPrice.Div(totalPrice, num.NewUint(totalSize)) 79 assert.Equal(t, uint64(87), expectedPrice.Uint64()) 80 81 // now let's place the network order and validate it's price 82 netorder := types.Order{ 83 MarketID: market, 84 Size: 8, 85 Remaining: 8, 86 Status: types.OrderStatusActive, 87 Party: "network", 88 Side: types.SideSell, 89 Price: num.UintZero(), 90 OriginalPrice: num.UintZero(), 91 CreatedAt: 0, 92 TimeInForce: types.OrderTimeInForceFOK, 93 Type: types.OrderTypeNetwork, 94 } 95 96 _, err := book.ob.SubmitOrder(&netorder) 97 assert.NoError(t, err) 98 // now we expect the price of the order to be updated 99 assert.Equal(t, expectedPrice.Uint64(), netorder.Price.Uint64()) 100 }