code.vegaprotocol.io/vega@v0.79.0/core/execution/stoporders/priced_stop_orders_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 stoporders_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/core/execution/stoporders"
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	"code.vegaprotocol.io/vega/libs/num"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestPricedStopOrders(t *testing.T) {
    29  	t.Run("remove", testPricedStopOrderRemove)
    30  	t.Run("test trigger price both direction", testPricedStopOrderTriggerPriceBothDirection)
    31  }
    32  
    33  func testPricedStopOrderRemove(t *testing.T) {
    34  	priced := stoporders.NewPricedStopOrders()
    35  
    36  	// first try an empty one
    37  	assert.EqualError(t, priced.Remove("a"), stoporders.ErrOrderNotFound.Error())
    38  
    39  	// now insert one each direction
    40  	priced.Insert("a", num.NewUint(10), types.StopOrderTriggerDirectionFallsBelow)
    41  	priced.Insert("b", num.NewUint(10), types.StopOrderTriggerDirectionRisesAbove)
    42  	priced.Insert("c", num.NewUint(11), types.StopOrderTriggerDirectionFallsBelow)
    43  
    44  	// now remove some
    45  	assert.NoError(t, priced.Remove("a"))
    46  
    47  	// try again, it should fail
    48  	assert.EqualError(t, priced.Remove("a"), stoporders.ErrOrderNotFound.Error())
    49  
    50  	// now remove the last one
    51  	assert.NoError(t, priced.Remove("b"))
    52  	// try again, it should fail
    53  	assert.EqualError(t, priced.Remove("b"), stoporders.ErrOrderNotFound.Error())
    54  }
    55  
    56  func testPricedStopOrderTriggerPriceBothDirection(t *testing.T) {
    57  	priced := stoporders.NewPricedStopOrders()
    58  
    59  	priced.Insert("a", num.NewUint(10), types.StopOrderTriggerDirectionFallsBelow)
    60  	priced.Insert("b", num.NewUint(11), types.StopOrderTriggerDirectionFallsBelow)
    61  	priced.Insert("c", num.NewUint(12), types.StopOrderTriggerDirectionFallsBelow)
    62  	priced.Insert("d", num.NewUint(13), types.StopOrderTriggerDirectionFallsBelow)
    63  	priced.Insert("e", num.NewUint(14), types.StopOrderTriggerDirectionFallsBelow)
    64  	priced.Insert("f", num.NewUint(15), types.StopOrderTriggerDirectionFallsBelow)
    65  
    66  	priced.Insert("g", num.NewUint(10), types.StopOrderTriggerDirectionRisesAbove)
    67  	priced.Insert("h", num.NewUint(11), types.StopOrderTriggerDirectionRisesAbove)
    68  	priced.Insert("i", num.NewUint(12), types.StopOrderTriggerDirectionRisesAbove)
    69  	priced.Insert("j", num.NewUint(13), types.StopOrderTriggerDirectionRisesAbove)
    70  	priced.Insert("k", num.NewUint(14), types.StopOrderTriggerDirectionRisesAbove)
    71  	priced.Insert("l", num.NewUint(15), types.StopOrderTriggerDirectionRisesAbove)
    72  
    73  	// Remove them once
    74  	assert.EqualValues(t,
    75  		priced.PriceUpdated(num.NewUint(13)),
    76  		[]string{"f", "e", "d", "g", "h", "i", "j"},
    77  	)
    78  
    79  	// try again
    80  	assert.EqualValues(t, priced.PriceUpdated(num.NewUint(13)), []string{})
    81  }