Skip to content

Commit

Permalink
wip implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfung-dydx committed Feb 28, 2024
1 parent 9bf9875 commit 3f61fa0
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 3 deletions.
7 changes: 4 additions & 3 deletions protocol/lib/log/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ const (
// Module tag values are prefixed with `x/`
Clob = "x/clob"

CheckTx = "check_tx"
RecheckTx = "recheck_tx"
DeliverTx = "deliver_tx"
CheckTx = "check_tx"
RecheckTx = "recheck_tx"
DeliverTx = "deliver_tx"
MsgBatchCancel = "msg_batch_canel"
)

// Special tag values that should be PascalCased (i.e function names)
Expand Down
14 changes: 14 additions & 0 deletions protocol/mocks/ClobKeeper.go

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

19 changes: 19 additions & 0 deletions protocol/x/clob/ante/clob.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ func (cd ClobDecorator) AnteHandle(
log.Error, err,
)
}
case *types.MsgBatchCancel:
// MsgBatchCancel currently only processes short-term cancels right now.
// No need to process short term orders on `ReCheckTx`.
if ctx.IsReCheckTx() {
return next(ctx, tx, simulate)
}
ctx = log.AddPersistentTagsToLogger(ctx,
log.Handler, log.MsgBatchCancel,
)

err := cd.clobKeeper.BatchCancelShortTermOrder(
ctx,
msg,
)

log.DebugLog(ctx, "Received new batch cancellation",
log.Tx, cometbftlog.NewLazySprintf("%X", tmhash.Sum(ctx.TxBytes())),
log.Error, err,
)
}
if err != nil {
return ctx, err
Expand Down
75 changes: 75 additions & 0 deletions protocol/x/clob/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,81 @@ func (k Keeper) GetOperations(ctx sdk.Context) *types.MsgProposedOperations {
return msgProposedOperations
}

// BatchCancelShortTermOrder removes a specified batch of short term orders from all order-related data
// structures in the memclob. As well, BatchCancelShortTermOrder adds (or updates) cancels to the desired
// `goodTilBlock` in the memclob for all specified orders.
// This message is not atomic. It will optimistically call `CancelShortTermOrder` for every order in the batch.
// If any of the orders error, the error will be silently logged. This msg will only error if:
// - Stateful validation fails
// - Every cancel in the batch fails.
// This method assumes the provided MsgBatchCancel has already passed ValidateBasic in CheckTx.
func (k Keeper) BatchCancelShortTermOrder(
ctx sdk.Context,
msg *types.MsgBatchCancel,
) error {
lib.AssertCheckTxMode(ctx)
// Note that we add `+1` here to account for the fact that `ctx.BlockHeight()` is technically the
// previously mined block, not the next block that will be proposed. This is due to the fact that
// this function is only ever called during `CheckTx`.
nextBlockHeight := lib.MustConvertIntegerToUint32(ctx.BlockHeight() + 1)

// Statefully validate the GTB and the clob pair ids.
if err := k.validateGoodTilBlock(msg.GetGoodTilBlock(), nextBlockHeight); err != nil {
return err
}
for _, batchOrder := range msg.GetShortTermCancels() {
clobPairId := batchOrder.GetClobPairId()
if _, found := k.GetClobPair(ctx, types.ClobPairId(clobPairId)); !found {
return errorsmod.Wrapf(
types.ErrInvalidClobPairParameter,
"Invalid clob pair id %+v",
clobPairId,
)
}
}

subaccountId := msg.GetSubaccountId()
oneCancelSucceeded := false
for _, batchOrder := range msg.GetShortTermCancels() {
clobPairId := batchOrder.GetClobPairId()
for _, clientId := range batchOrder.GetClientIds() {
msgCancelOrder := types.MsgCancelOrder{
OrderId: types.OrderId{
SubaccountId: subaccountId,
OrderFlags: types.OrderIdFlags_ShortTerm,
ClobPairId: clobPairId,
ClientId: clientId,
},
}

// Run the short term order. If it errors, just log silently.
err := k.CancelShortTermOrder(
ctx,
&msgCancelOrder,
)

if err != nil {
log.InfoLog(
ctx,
"Failed to cancel short term order.",
log.Error, err,
)
} else {
oneCancelSucceeded = true
}
}
}
if !oneCancelSucceeded {
log.ErrorLog(
ctx,
"All cancels in this batch have failed.",
)
return types.ErrBatchCancelAllFailed
}

return nil
}

// CancelShortTermOrder removes a Short-Term order by `OrderId` (if it exists) from all order-related data structures
// in the memclob. As well, CancelShortTermOrder adds (or updates) a cancel to the desired `goodTilBlock` in the
// memclob.
Expand Down
4 changes: 4 additions & 0 deletions protocol/x/clob/types/clob_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type ClobKeeper interface {
success bool,
successPerUpdate map[satypes.SubaccountId]satypes.UpdateResult,
)
BatchCancelShortTermOrder(
ctx sdk.Context,
msg *MsgBatchCancel,
) error
CancelShortTermOrder(ctx sdk.Context, msg *MsgCancelOrder) error
CancelStatefulOrder(ctx sdk.Context, msg *MsgCancelOrder) error
CreatePerpetualClobPair(
Expand Down
5 changes: 5 additions & 0 deletions protocol/x/clob/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ var (
45,
"Invalid batch cancel message",
)
ErrBatchCancelAllFailed = errorsmod.Register(
ModuleName,
46,
"All order cancels in this batch have failed",
)

// Liquidations errors.
ErrInvalidLiquidationsConfig = errorsmod.Register(
Expand Down

0 comments on commit 3f61fa0

Please sign in to comment.