Skip to content

Commit

Permalink
refactor place order msg server logic into keeper (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 authored Mar 15, 2024
1 parent f0fe4cc commit 00cd3d5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
18 changes: 18 additions & 0 deletions protocol/mocks/ClobKeeper.go

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

35 changes: 26 additions & 9 deletions protocol/x/clob/keeper/msg_server_place_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ func (k msgServer) PlaceOrder(goCtx context.Context, msg *types.MsgPlaceOrder) (
) {
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

if err := k.Keeper.HandleMsgPlaceOrder(ctx, msg); err != nil {
return nil, err
}

return &types.MsgPlaceOrderResponse{}, nil
}

// HandleMsgPlaceOrder handles a MsgPlaceOrder by
// 1. persisting the placement on chain.
// 2. updating ProcessProposerMatchesEvents with the new stateful order placement.
// 3. adding order placement on-chain indexer event.
func (k Keeper) HandleMsgPlaceOrder(
ctx sdk.Context,
msg *types.MsgPlaceOrder,
) (err error) {
lib.AssertDeliverTxMode(ctx)

// Attach various logging tags relative to this request. These should be static with no changes.
ctx = log.AddPersistentTagsToLogger(ctx,
log.Module, log.Clob,
Expand Down Expand Up @@ -66,18 +83,18 @@ func (k msgServer) PlaceOrder(goCtx context.Context, msg *types.MsgPlaceOrder) (
order.MustBeStatefulOrder()

// 2. Return an error if an associated cancellation or removal already exists in the current block.
processProposerMatchesEvents := k.Keeper.GetProcessProposerMatchesEvents(ctx)
processProposerMatchesEvents := k.GetProcessProposerMatchesEvents(ctx)
cancelledOrderIds := lib.UniqueSliceToSet(processProposerMatchesEvents.PlacedStatefulCancellationOrderIds)
if _, found := cancelledOrderIds[order.GetOrderId()]; found {
return nil, errorsmod.Wrapf(
return errorsmod.Wrapf(
types.ErrStatefulOrderPreviouslyCancelled,
"PlaceOrder: order (%+v)",
order,
)
}
removedOrderIds := lib.UniqueSliceToSet(processProposerMatchesEvents.RemovedStatefulOrderIds)
if _, found := removedOrderIds[order.GetOrderId()]; found {
return nil, errorsmod.Wrapf(
return errorsmod.Wrapf(
types.ErrStatefulOrderPreviouslyRemoved,
"PlaceOrder: order (%+v)",
order,
Expand All @@ -88,13 +105,13 @@ func (k msgServer) PlaceOrder(goCtx context.Context, msg *types.MsgPlaceOrder) (
// - stateful order validation.
// - collateralization check.
// - writing the order to state and the memstore.
if err := k.Keeper.PlaceStatefulOrder(ctx, msg); err != nil {
return nil, err
if err := k.PlaceStatefulOrder(ctx, msg); err != nil {
return err
}

// 4. Emit the new order placement indexer event.
if order.IsConditionalOrder() {
k.Keeper.GetIndexerEventManager().AddTxnEvent(
k.GetIndexerEventManager().AddTxnEvent(
ctx,
indexerevents.SubtypeStatefulOrder,
indexerevents.StatefulOrderEventVersion,
Expand All @@ -109,7 +126,7 @@ func (k msgServer) PlaceOrder(goCtx context.Context, msg *types.MsgPlaceOrder) (
order.OrderId,
)
} else {
k.Keeper.GetIndexerEventManager().AddTxnEvent(
k.GetIndexerEventManager().AddTxnEvent(
ctx,
indexerevents.SubtypeStatefulOrder,
indexerevents.StatefulOrderEventVersion,
Expand All @@ -125,10 +142,10 @@ func (k msgServer) PlaceOrder(goCtx context.Context, msg *types.MsgPlaceOrder) (
)
}
// 5. Add the newly-placed stateful order to `ProcessProposerMatchesEvents` for use in `PrepareCheckState`.
k.Keeper.MustSetProcessProposerMatchesEvents(
k.MustSetProcessProposerMatchesEvents(
ctx,
processProposerMatchesEvents,
)

return &types.MsgPlaceOrderResponse{}, nil
return nil
}
5 changes: 5 additions & 0 deletions protocol/x/clob/types/clob_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type ClobKeeper interface {
ctx sdk.Context,
msg *MsgCancelOrder,
) (err error)
HandleMsgPlaceOrder(
ctx sdk.Context,
msg *MsgPlaceOrder,
) (err error)
GetAllClobPairs(ctx sdk.Context) (list []ClobPair)
GetClobPair(ctx sdk.Context, id ClobPairId) (val ClobPair, found bool)
HasAuthority(authority string) bool
Expand All @@ -53,6 +57,7 @@ type ClobKeeper interface {
err error,
)
PlaceStatefulOrder(ctx sdk.Context, msg *MsgPlaceOrder) error

PruneStateFillAmountsForShortTermOrders(
ctx sdk.Context,
)
Expand Down

0 comments on commit 00cd3d5

Please sign in to comment.