Skip to content

Commit

Permalink
deliverTx process opqueue clob match emissions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfung-dydx committed Apr 30, 2024
1 parent cec38f7 commit ea262ba
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
18 changes: 18 additions & 0 deletions protocol/mocks/MemClob.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions protocol/x/clob/keeper/process_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ func (k Keeper) PersistMatchOrdersToState(
}
}

makerOrders := make([]types.Order, 0)
makerFills := matchOrders.GetFills()
for _, makerFill := range makerFills {
// Fetch the maker order from either short term orders or state.
Expand All @@ -475,6 +476,7 @@ func (k Keeper) PersistMatchOrdersToState(
MakerOrder: &makerOrder,
FillAmount: satypes.BaseQuantums(makerFill.GetFillAmount()),
}
makerOrders = append(makerOrders, makerOrder)

_, _, _, _, err = k.ProcessSingleMatch(ctx, &matchWithOrders)
if err != nil {
Expand Down Expand Up @@ -518,6 +520,26 @@ func (k Keeper) PersistMatchOrdersToState(
)
}

// if GRPC streaming is on, emit a generated clob match to stream.
if streamingManager := k.GetGrpcStreamingManager(); streamingManager.Enabled() {
streamOrderbookFill := k.MemClob.GenerateStreamOrderbookFill(
ctx,
types.ClobMatch{
Match: &types.ClobMatch_MatchOrders{
MatchOrders: matchOrders,
},
},
&takerOrder,
makerOrders,
)
streamingManager.SendOrderbookFillUpdates(
ctx,
[]types.StreamOrderbookFill{
streamOrderbookFill,
},
)
}

return nil
}

Expand Down Expand Up @@ -547,12 +569,14 @@ func (k Keeper) PersistMatchLiquidationToState(
return err
}

makerOrders := make([]types.Order, 0)
for _, fill := range matchLiquidation.GetFills() {
// Fetch the maker order from either short term orders or state.
makerOrder, err := k.FetchOrderFromOrderId(ctx, fill.MakerOrderId, ordersMap)
if err != nil {
return err
}
makerOrders = append(makerOrders, makerOrder)

matchWithOrders := types.MatchWithOrders{
MakerOrder: &makerOrder,
Expand Down Expand Up @@ -604,6 +628,26 @@ func (k Keeper) PersistMatchLiquidationToState(
matchLiquidation.Liquidated,
matchLiquidation.PerpetualId,
)

// if GRPC streaming is on, emit a generated clob match to stream.
if streamingManager := k.GetGrpcStreamingManager(); streamingManager.Enabled() {
streamOrderbookFill := k.MemClob.GenerateStreamOrderbookFill(
ctx,
types.ClobMatch{
Match: &types.ClobMatch_MatchPerpetualLiquidation{
MatchPerpetualLiquidation: matchLiquidation,
},
},
takerOrder,
makerOrders,
)
streamingManager.SendOrderbookFillUpdates(
ctx,
[]types.StreamOrderbookFill{
streamOrderbookFill,
},
)
}
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions protocol/x/clob/types/memclob.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,10 @@ type MemClob interface {
ctx sdk.Context,
orderId OrderId,
) (offchainUpdates *OffchainUpdates)
GenerateStreamOrderbookFill(
ctx sdk.Context,
clobMatch ClobMatch,
takerOrder MatchableOrder,
makerOrders []Order,
) StreamOrderbookFill
}

0 comments on commit ea262ba

Please sign in to comment.