code.vegaprotocol.io/vega@v0.79.0/core/execution/future/amends_test_flip_gtt_gtc_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  
    26  	"github.com/golang/mock/gomock"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestOrderBookAmends_FlipToGTT(t *testing.T) {
    32  	now := time.Unix(5, 0)
    33  	tm := getTestMarket(t, now, nil, nil)
    34  	ctx := context.Background()
    35  	defer tm.ctrl.Finish()
    36  
    37  	addAccount(t, tm, "aaa")
    38  	tm.broker.EXPECT().Send(gomock.Any()).AnyTimes()
    39  
    40  	o1 := getMarketOrder(tm, now, types.OrderTypeLimit, types.OrderTimeInForceGTC, "Order01", types.SideBuy, "aaa", 2, 100)
    41  	o1conf, err := tm.market.SubmitOrder(ctx, o1)
    42  	require.NoError(t, err)
    43  	require.NotNil(t, o1conf)
    44  	require.Equal(t, 0, tm.market.GetPeggedExpiryOrderCount())
    45  
    46  	// now we edit the order t make it GTC so it should not expire
    47  	v10 := now.Add(10 * time.Second).UnixNano()
    48  	amendment := &types.OrderAmendment{
    49  		OrderID:     o1.ID,
    50  		TimeInForce: types.OrderTimeInForceGTT,
    51  		ExpiresAt:   &v10,
    52  	}
    53  
    54  	amendConf, err := tm.market.AmendOrder(ctx, amendment, "aaa", vgcrypto.RandomHash())
    55  	require.NotNil(t, amendConf)
    56  	require.NoError(t, err)
    57  	assert.Equal(t, types.OrderStatusActive, amendConf.Order.Status)
    58  	require.Equal(t, 1, tm.market.GetPeggedExpiryOrderCount())
    59  
    60  	// now we edit the order t make it GTC so it should not expire
    61  	v := now.Add(20 * time.Second).UnixNano()
    62  	amendment2 := &types.OrderAmendment{
    63  		OrderID:     o1.ID,
    64  		TimeInForce: types.OrderTimeInForceGTT,
    65  		ExpiresAt:   &v,
    66  	}
    67  
    68  	amendConf2, err := tm.market.AmendOrder(ctx, amendment2, "aaa", vgcrypto.RandomHash())
    69  	require.NotNil(t, amendConf2)
    70  	require.NoError(t, err)
    71  	assert.Equal(t, types.OrderStatusActive, amendConf2.Order.Status)
    72  	require.Equal(t, 1, tm.market.GetPeggedExpiryOrderCount())
    73  
    74  	// now we edit the order t make it GTC so it should not expire
    75  	amendment3 := &types.OrderAmendment{
    76  		OrderID:     o1.ID,
    77  		TimeInForce: types.OrderTimeInForceGTC,
    78  	}
    79  
    80  	amendConf3, err := tm.market.AmendOrder(ctx, amendment3, "aaa", vgcrypto.RandomHash())
    81  	require.NotNil(t, amendConf3)
    82  	require.NoError(t, err)
    83  	assert.Equal(t, types.OrderStatusActive, amendConf3.Order.Status)
    84  	require.Equal(t, 0, tm.market.GetPeggedExpiryOrderCount())
    85  }