Skip to content

Commit

Permalink
chore: change order cancellation event in spot markets
Browse files Browse the repository at this point in the history
Signed-off-by: Elias Van Ootegem <[email protected]>
  • Loading branch information
EVODelavega committed Apr 16, 2024
1 parent 3bcc677 commit 66099eb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
19 changes: 17 additions & 2 deletions core/execution/spot/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -1712,16 +1712,20 @@ func (m *Market) CancelAllOrders(ctx context.Context, partyID string) ([]*types.
})

cancellations := make([]*types.OrderCancellationConfirmation, 0, len(orders))
orderIDs := make([]string, 0, len(orders))

// now iterate over all orders and cancel one by one.
for _, order := range orders {
cancellation, err := m.cancelOrder(ctx, partyID, order.ID)
cancellation, err := m.cancelOrderInBatch(ctx, partyID, order.ID)
if err != nil {
return nil, err
}
cancellations = append(cancellations, cancellation)
orderIDs = append(orderIDs, order.ID)
}

m.broker.Send(events.NewCancelledOrdersEvent(ctx, m.GetID(), partyID, orderIDs...))

m.checkForReferenceMoves(ctx, false)

return cancellations, nil
Expand Down Expand Up @@ -1789,8 +1793,17 @@ func (m *Market) CancelOrderWithIDGenerator(ctx context.Context, partyID, orderI
return conf, nil
}

func (m *Market) cancelOrderInBatch(ctx context.Context, partyID, orderID string) (*types.OrderCancellationConfirmation, error) {
return m.cancelSingleOrder(ctx, partyID, orderID, true)
}

// CancelOrder cancels the given order. If the order is found on the book, we release locked funds from holding account to the general account of the party.
func (m *Market) cancelOrder(ctx context.Context, partyID, orderID string) (*types.OrderCancellationConfirmation, error) {
return m.cancelSingleOrder(ctx, partyID, orderID, false)
}

// CancelOrder cancels the given order. If the order is found on the book, we release locked funds from holding account to the general account of the party.
func (m *Market) cancelSingleOrder(ctx context.Context, partyID, orderID string, inBatch bool) (*types.OrderCancellationConfirmation, error) {
timer := metrics.NewTimeCounter(m.mkt.ID, "market", "CancelOrder")
defer timer.EngineTimeCounterAdd()

Expand Down Expand Up @@ -1841,7 +1854,9 @@ func (m *Market) cancelOrder(ctx context.Context, partyID, orderID string) (*typ

// Publish the changed order details
order.UpdatedAt = m.timeService.GetTimeNow().UnixNano()
m.broker.Send(events.NewOrderEvent(ctx, order))
if !inBatch {
m.broker.Send(events.NewCancelledOrdersEvent(ctx, m.GetID(), partyID, orderID))
}

return &types.OrderCancellationConfirmation{Order: order}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Feature: Testing batch orders in spot markets
| mark price | trading mode | horizon | min bound | max bound | target stake | supplied stake | open interest |
| 1000 | TRADING_MODE_CONTINUOUS | 3600 | 959 | 1042 | 0 | 0 | 0 |

@OrdCan
Scenario: Funds released by cancellations or amendments within the batch should be immediately available
for later instructions (0074-BTCH-019)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"

"code.vegaprotocol.io/vega/core/integration/stubs"
types "code.vegaprotocol.io/vega/protos/vega"

"github.com/cucumber/godog"
)
Expand All @@ -36,15 +37,21 @@ func TheOrdersShouldHaveTheFollowingStates(broker *stubs.BrokerStub, table *godo
status := row.MustOrderStatus("status")
ref, hasRef := row.StrB("reference")

checkCancel := status == types.Order_STATUS_CANCELLED

match := false
for i := len(orderEvents) - 1; i >= 0; i-- {
e := orderEvents[i]
o := e.Order()
cancelled := false
if checkCancel {
cancelled = broker.IsCancelledOrder(marketID, party, o.Id)
}
if hasRef {
if ref != o.Reference {
continue
}
if o.PartyId == party && o.Status == status && o.MarketId == marketID && o.Side == side {
if o.PartyId == party && (o.Status == status || cancelled) && o.MarketId == marketID && o.Side == side {
if o.Size != size || stringToU64(o.Price) != price || o.Remaining != remaining {
return formatDiff(fmt.Sprintf("the order didn't match the expectation for reference %q", ref),
map[string]string{
Expand All @@ -61,7 +68,7 @@ func TheOrdersShouldHaveTheFollowingStates(broker *stubs.BrokerStub, table *godo
}
}
}
if o.PartyId != party || o.Status != status || o.MarketId != marketID || o.Side != side || o.Size != size || stringToU64(o.Price) != price || o.Remaining != remaining {
if o.PartyId != party || (o.Status != status && !cancelled) || o.MarketId != marketID || o.Side != side || o.Size != size || stringToU64(o.Price) != price || o.Remaining != remaining {
continue
}
match = true
Expand Down

0 comments on commit 66099eb

Please sign in to comment.