code.vegaprotocol.io/vega@v0.79.0/core/positions/market_position_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 positions_test 17 18 import ( 19 "testing" 20 21 "code.vegaprotocol.io/vega/core/positions" 22 "code.vegaprotocol.io/vega/core/types" 23 "code.vegaprotocol.io/vega/libs/num" 24 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestUpdateInPlace(t *testing.T) { 29 pos := positions.NewMarketPosition("zohar") 30 buy := &types.Order{ 31 ID: "1", 32 MarketID: "2", 33 Party: "zohar", 34 Side: types.SideBuy, 35 Size: 10, 36 Price: num.NewUint(100), 37 Remaining: 10, 38 } 39 sell := &types.Order{ 40 ID: "2", 41 MarketID: "2", 42 Party: "zohar", 43 Side: types.SideSell, 44 Size: 20, 45 Price: num.NewUint(200), 46 Remaining: 20, 47 } 48 pos.RegisterOrder(nil, buy) 49 pos.RegisterOrder(nil, sell) 50 trade1 := &types.Trade{ 51 ID: "t1", 52 Size: 3, 53 Price: num.NewUint(120), 54 } 55 updatedPos := pos.UpdateInPlaceOnTrades(nil, types.SideBuy, []*types.Trade{trade1}, buy) 56 require.Equal(t, int64(3), updatedPos.Size()) 57 require.Equal(t, int64(7), updatedPos.Buy()) 58 require.Equal(t, int64(20), updatedPos.Sell()) 59 60 // now trade the whole size of the sell order 61 trade2 := &types.Trade{ 62 ID: "t2", 63 Size: 20, 64 Price: num.NewUint(150), 65 } 66 updatedPos = updatedPos.UpdateInPlaceOnTrades(nil, types.SideSell, []*types.Trade{trade2}, sell) 67 require.Equal(t, int64(-17), updatedPos.Size()) 68 require.Equal(t, int64(7), updatedPos.Buy()) 69 require.Equal(t, int64(0), updatedPos.Sell()) 70 71 // now unregister the remaining buy order 72 buy.Remaining = 7 73 74 updatedPos.UnregisterOrder(nil, buy) 75 require.Equal(t, int64(-17), updatedPos.Size()) 76 require.Equal(t, int64(0), updatedPos.Buy()) 77 require.Equal(t, int64(0), updatedPos.Sell()) 78 }