From 5a0a999035b9904f22cd08a75a6d3e36a3c1c4ed Mon Sep 17 00:00:00 2001 From: ze97286 Date: Wed, 22 May 2024 15:57:30 +0100 Subject: [PATCH] fix: handle invalid offset for pegged on market update when tick size changes --- core/execution/future/market.go | 6 +++++- core/execution/spot/market.go | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/core/execution/future/market.go b/core/execution/future/market.go index bc59879eec8..c4e19a28b23 100644 --- a/core/execution/future/market.go +++ b/core/execution/future/market.go @@ -670,6 +670,8 @@ func (m *Market) Update(ctx context.Context, config *types.Market, oracleEngine if tickSizeChanged { peggedOrders := m.matching.GetActivePeggedOrderIDs() peggedOrders = append(peggedOrders, m.peggedOrders.GetParkedIDs()...) + + tickSizeInAsset, _ := num.UintFromDecimal(m.mkt.TickSize.ToDecimal().Mul(m.priceFactor)) for _, po := range peggedOrders { order, err := m.matching.GetOrderByID(po) if err != nil { @@ -678,7 +680,9 @@ func (m *Market) Update(ctx context.Context, config *types.Market, oracleEngine continue } } - if !num.UintZero().Mod(order.PeggedOrder.Offset, m.mkt.TickSize).IsZero() { + offsetInAsset, _ := num.UintFromDecimal(order.PeggedOrder.Offset.ToDecimal().Mul(m.priceFactor)) + if !num.UintZero().Mod(order.PeggedOrder.Offset, m.mkt.TickSize).IsZero() || + (order.PeggedOrder.Reference == types.PeggedReferenceMid && offsetInAsset.IsZero() && tickSizeInAsset.IsZero()) { m.cancelOrder(ctx, order.Party, order.ID) } } diff --git a/core/execution/spot/market.go b/core/execution/spot/market.go index 33442260d29..b3d49de3e55 100644 --- a/core/execution/spot/market.go +++ b/core/execution/spot/market.go @@ -297,6 +297,7 @@ func (m *Market) Update(ctx context.Context, config *types.Market) error { m.updateLiquidityFee(ctx) if tickSizeChanged { + tickSizeInAsset, _ := num.UintFromDecimal(m.mkt.TickSize.ToDecimal().Mul(m.priceFactor)) peggedOrders := m.matching.GetActivePeggedOrderIDs() peggedOrders = append(peggedOrders, m.peggedOrders.GetParkedIDs()...) for _, po := range peggedOrders { @@ -307,7 +308,9 @@ func (m *Market) Update(ctx context.Context, config *types.Market) error { continue } } - if !num.UintZero().Mod(order.PeggedOrder.Offset, m.mkt.TickSize).IsZero() { + offsetInAsset, _ := num.UintFromDecimal(order.PeggedOrder.Offset.ToDecimal().Mul(m.priceFactor)) + if !num.UintZero().Mod(order.PeggedOrder.Offset, m.mkt.TickSize).IsZero() || + (order.PeggedOrder.Reference == types.PeggedReferenceMid && offsetInAsset.IsZero() && tickSizeInAsset.IsZero()) { m.cancelOrder(ctx, order.Party, order.ID) } }