code.vegaprotocol.io/vega@v0.79.0/core/execution/stoporders/stop_orders_snapshot_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  	"encoding/hex"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/core/execution/stoporders"
    23  	"code.vegaprotocol.io/vega/core/types"
    24  	"code.vegaprotocol.io/vega/libs/num"
    25  	"code.vegaprotocol.io/vega/libs/proto"
    26  	"code.vegaprotocol.io/vega/logging"
    27  	v1 "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestStopOrdersSnapshot(t *testing.T) {
    33  	log := logging.NewTestLogger()
    34  	// create the pool add the price
    35  	pool := stoporders.New(log)
    36  	pool.PriceUpdated(num.NewUint(50))
    37  
    38  	pool.Insert(newPricedStopOrder("a", "p1", "b", num.NewUint(40), types.StopOrderTriggerDirectionFallsBelow))
    39  	pool.Insert(newTrailingStopOrder("b", "p1", "a", num.MustDecimalFromString("0.5"), types.StopOrderTriggerDirectionRisesAbove))
    40  
    41  	// move the price a little
    42  	pool.PriceUpdated(num.NewUint(51))
    43  	pool.Insert(newPricedStopOrder("c", "p2", "d", num.NewUint(70), types.StopOrderTriggerDirectionRisesAbove))
    44  	pool.Insert(newTrailingStopOrder("d", "p2", "c", num.MustDecimalFromString("0.5"), types.StopOrderTriggerDirectionFallsBelow))
    45  
    46  	// move the price a little more
    47  	pool.PriceUpdated(num.NewUint(52))
    48  	pool.Insert(newPricedStopOrder("e", "p3", "f", num.NewUint(35), types.StopOrderTriggerDirectionFallsBelow))
    49  	pool.Insert(newTrailingStopOrder("f", "p3", "e", num.MustDecimalFromString("0.5"), types.StopOrderTriggerDirectionRisesAbove))
    50  
    51  	pool.Insert(newPricedStopOrder("h", "p2", "", num.NewUint(34), types.StopOrderTriggerDirectionFallsBelow))
    52  	// same with new offset
    53  	pool.Insert(newTrailingStopOrder("i", "p2", "", num.MustDecimalFromString("0.2"), types.StopOrderTriggerDirectionRisesAbove))
    54  
    55  	// now we get the protos
    56  	serialized := pool.ToProto()
    57  
    58  	buf, err := proto.Marshal(serialized)
    59  	assert.NoError(t, err)
    60  
    61  	deserialized := &v1.StopOrders{}
    62  	err = proto.Unmarshal(buf, deserialized)
    63  	assert.NoError(t, err)
    64  
    65  	pool2 := stoporders.NewFromProto(log, deserialized)
    66  	assert.True(t, pool.Equal(pool2))
    67  
    68  	serialized2 := pool2.ToProto()
    69  	buf2, err := proto.Marshal(serialized2)
    70  	assert.NoError(t, err)
    71  	assert.Equal(t, hex.EncodeToString(buf), hex.EncodeToString(buf2))
    72  }