code.vegaprotocol.io/vega@v0.79.0/datanode/entities/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 entities_test
    17  
    18  import (
    19  	"testing"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	"code.vegaprotocol.io/vega/libs/ptr"
    24  	"code.vegaprotocol.io/vega/protos/vega"
    25  	commandpb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    26  	pbevents "code.vegaprotocol.io/vega/protos/vega/events/v1"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestStopOrderFromProto(t *testing.T) {
    33  	t.Run("Should set ExpiresAt if ExpiryStrategy is set", func(t *testing.T) {
    34  		createdAt := time.Now().Round(time.Microsecond)
    35  		expiresAt := createdAt.Add(time.Minute)
    36  		sop := vega.StopOrder{
    37  			Id:               "deadbeef",
    38  			ExpiresAt:        ptr.From(expiresAt.UnixNano()),
    39  			ExpiryStrategy:   ptr.From(vega.StopOrder_EXPIRY_STRATEGY_CANCELS),
    40  			TriggerDirection: vega.StopOrder_TRIGGER_DIRECTION_RISES_ABOVE,
    41  			Status:           vega.StopOrder_STATUS_PENDING,
    42  			CreatedAt:        createdAt.UnixNano(),
    43  			UpdatedAt:        nil,
    44  			OrderId:          "deadbeef",
    45  			PartyId:          "deadbaad",
    46  			MarketId:         "dead2bad",
    47  			Trigger: &vega.StopOrder_Price{
    48  				Price: "100",
    49  			},
    50  		}
    51  		sub := commandpb.OrderSubmission{
    52  			MarketId:    "dead2bad",
    53  			Price:       "100",
    54  			Size:        100,
    55  			Side:        vega.Side_SIDE_BUY,
    56  			TimeInForce: vega.Order_TIME_IN_FORCE_GTC,
    57  			Reference:   "some-reference",
    58  		}
    59  		soEvent := pbevents.StopOrderEvent{
    60  			Submission: &sub,
    61  			StopOrder:  &sop,
    62  		}
    63  
    64  		vegaTime := time.Now().Round(time.Microsecond)
    65  		seqNum := uint64(0)
    66  		txHash := entities.TxHash(`deadbaad`)
    67  
    68  		got, err := entities.StopOrderFromProto(&soEvent, vegaTime, seqNum, txHash)
    69  		require.NoError(t, err)
    70  
    71  		want := entities.StopOrder{
    72  			ID:                   entities.StopOrderID("deadbeef"),
    73  			ExpiresAt:            ptr.From(expiresAt),
    74  			ExpiryStrategy:       entities.StopOrderExpiryStrategyCancels,
    75  			TriggerDirection:     entities.StopOrderTriggerDirectionRisesAbove,
    76  			Status:               entities.StopOrderStatusPending,
    77  			CreatedAt:            createdAt,
    78  			UpdatedAt:            nil,
    79  			OrderID:              "deadbeef",
    80  			TriggerPrice:         ptr.From("100"),
    81  			TriggerPercentOffset: nil,
    82  			PartyID:              "deadbaad",
    83  			MarketID:             "dead2bad",
    84  			VegaTime:             vegaTime,
    85  			SeqNum:               0,
    86  			TxHash:               entities.TxHash(`deadbaad`),
    87  			Submission:           &sub,
    88  		}
    89  
    90  		assert.Equal(t, want, got)
    91  	})
    92  }