code.vegaprotocol.io/vega@v0.79.0/core/execution/future/version_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 future_test 17 18 import ( 19 "context" 20 "testing" 21 "time" 22 23 "code.vegaprotocol.io/vega/core/types" 24 vgcrypto "code.vegaprotocol.io/vega/libs/crypto" 25 "code.vegaprotocol.io/vega/libs/num" 26 27 "github.com/golang/mock/gomock" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestVersioning(t *testing.T) { 32 party1 := "party1" 33 now := time.Unix(10, 0) 34 tm := getTestMarket(t, now, nil, nil) 35 price := uint64(100) 36 size := uint64(100) 37 38 addAccount(t, tm, party1) 39 tm.broker.EXPECT().Send(gomock.Any()).AnyTimes() 40 41 orderBuy := &types.Order{ 42 Status: types.OrderStatusActive, 43 Type: types.OrderTypeLimit, 44 TimeInForce: types.OrderTimeInForceGTC, 45 ID: "someid", 46 Side: types.SideBuy, 47 Party: party1, 48 MarketID: tm.market.GetID(), 49 Size: size, 50 Price: num.NewUint(price), 51 Remaining: 100, 52 CreatedAt: now.UnixNano(), 53 Reference: "party1-buy-order", 54 } 55 // Create an order and check version is set to 1 56 confirmation, err := tm.market.SubmitOrder(context.TODO(), orderBuy) 57 assert.NotNil(t, confirmation) 58 assert.NoError(t, err) 59 assert.EqualValues(t, confirmation.Order.Version, uint64(1)) 60 61 orderID := confirmation.Order.ID 62 63 // Amend price up, check version moves to 2 64 amend := &types.OrderAmendment{ 65 OrderID: orderID, 66 MarketID: tm.market.GetID(), 67 Price: num.NewUint(price + 1), 68 } 69 70 amendment, err := tm.market.AmendOrder(context.TODO(), amend, party1, vgcrypto.RandomHash()) 71 assert.NotNil(t, amendment) 72 assert.NoError(t, err) 73 74 // Amend price down, check version moves to 3 75 amend.Price = num.NewUint(price - 1) 76 amendment, err = tm.market.AmendOrder(context.TODO(), amend, party1, vgcrypto.RandomHash()) 77 assert.NotNil(t, amendment) 78 assert.NoError(t, err) 79 80 // Amend quantity up, check version moves to 4 81 amend.Price = nil 82 amend.SizeDelta = 1 83 amendment, err = tm.market.AmendOrder(context.TODO(), amend, party1, vgcrypto.RandomHash()) 84 assert.NotNil(t, amendment) 85 assert.NoError(t, err) 86 87 // Amend quantity down, check version moves to 5 88 amend.SizeDelta = -2 89 amendment, err = tm.market.AmendOrder(context.TODO(), amend, party1, vgcrypto.RandomHash()) 90 assert.NotNil(t, amendment) 91 assert.NoError(t, err) 92 93 // Flip to GTT, check version moves to 6 94 amend.TimeInForce = types.OrderTimeInForceGTT 95 exp := now.UnixNano() + 100000000000 96 amend.ExpiresAt = &exp 97 amend.SizeDelta = 0 98 amendment, err = tm.market.AmendOrder(context.TODO(), amend, party1, vgcrypto.RandomHash()) 99 assert.NotNil(t, amendment) 100 assert.NoError(t, err) 101 102 // Update expiry time, check version moves to 7 103 exp = now.UnixNano() + 100000000000 104 amend.ExpiresAt = &exp 105 amendment, err = tm.market.AmendOrder(context.TODO(), amend, party1, vgcrypto.RandomHash()) 106 assert.NotNil(t, amendment) 107 assert.NoError(t, err) 108 109 // Flip back GTC, check version moves to 8 110 amend.TimeInForce = types.OrderTimeInForceGTC 111 amend.ExpiresAt = nil 112 amendment, err = tm.market.AmendOrder(context.TODO(), amend, party1, vgcrypto.RandomHash()) 113 assert.NotNil(t, amendment) 114 assert.NoError(t, err) 115 }