Skip to content

Commit

Permalink
fix: prevent a pegged order submission that would cross mid
Browse files Browse the repository at this point in the history
  • Loading branch information
ze97286 committed May 22, 2024
1 parent 694061d commit 552ce80
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
7 changes: 7 additions & 0 deletions core/execution/future/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,13 @@ func (m *Market) validateOrder(ctx context.Context, order *types.Order) (err err
}
return reason
}
if order.PeggedOrder.Reference == types.PeggedReferenceMid {
offsetInAsset, _ := num.UintFromDecimal(order.PeggedOrder.Offset.ToDecimal().Mul(m.priceFactor))
tickSizeInAsset, _ := num.UintFromDecimal(m.mkt.TickSize.ToDecimal().Mul(m.priceFactor))
if offsetInAsset.IsZero() && tickSizeInAsset.IsZero() {
return fmt.Errorf("invalid offset - pegged mid will cross")
}
}
return m.validateTickSize(order.PeggedOrder.Offset)
}

Expand Down
7 changes: 7 additions & 0 deletions core/execution/spot/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,13 @@ func (m *Market) validateOrder(ctx context.Context, order *types.Order) (err err
}
return reason
}
if order.PeggedOrder.Reference == types.PeggedReferenceMid {
offsetInAsset, _ := num.UintFromDecimal(order.PeggedOrder.Offset.ToDecimal().Mul(m.priceFactor))
tickSizeInAsset, _ := num.UintFromDecimal(m.mkt.TickSize.ToDecimal().Mul(m.priceFactor))
if offsetInAsset.IsZero() && tickSizeInAsset.IsZero() {
return fmt.Errorf("invalid offset - pegged mid will cross")
}
}
return m.validateTickSize(order.PeggedOrder.Offset)
}

Expand Down
40 changes: 20 additions & 20 deletions core/integration/features/verified/pegged_orders.feature
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

Feature: Pegged orders do not cross

Aiming for full coverage of edge-cases, check the following:
Aiming for full coverage of edge-cases, check the following:

- Market decimals > asset decimals
- Market decimals < asset decimals
- For each of the above
- tick size cannot be expressed in asset decimals
- tick size can just be expressed in asset decimals
- tick size can be expressed in asset decimals
- tick size cannot be expressed in asset decimals
- tick size can just be expressed in asset decimals
- tick size can be expressed in asset decimals
- For each of the above
- offset cannot be expressed in asset decimals
- offset can just be expressed in asset decimals
- offset can be expressed in asset decimals
- For each of the above
- offset cannot be expressed in asset decimals
- offset can just be expressed in asset decimals
- offset can be expressed in asset decimals
Background:
Expand Down Expand Up @@ -52,17 +52,17 @@ Feature: Pegged orders do not cross
When the opening auction period ends for market "ETH.1.10/DEC21"
Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH.1.10/DEC21"

Examples:
| bo | tick size | offset | error |
| 1010 | 1 | 1 | |
| 1010 | 1 | 10 | |
| 1010 | 1 | 100 | |
| 1010 | 10 | 1 | OrderError: price not in tick size |
| 1010 | 10 | 10 | |
| 1010 | 10 | 100 | |
| 1100 | 100 | 1 | OrderError: price not in tick size |
| 1100 | 100 | 10 | OrderError: price not in tick size |
| 1100 | 100 | 100 | |
Examples:
| bo | tick size | offset | error |
| 1010 | 1 | 1 | invalid offset - pegged mid will cross |
| 1010 | 1 | 10 | |
| 1010 | 1 | 100 | |
| 1010 | 10 | 1 | OrderError: price not in tick size |
| 1010 | 10 | 10 | |
| 1010 | 10 | 100 | |
| 1100 | 100 | 1 | OrderError: price not in tick size |
| 1100 | 100 | 10 | OrderError: price not in tick size |
| 1100 | 100 | 100 | |


Scenario Outline: # Market decimals < asset decimals
Expand All @@ -83,7 +83,7 @@ Feature: Pegged orders do not cross
When the opening auction period ends for market "ETH.1.10/DEC21"
Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH.1.10/DEC21"

Examples:
Examples:
| bo | tick size | offset | error |
| 1001 | 1 | 1 | |
| 1001 | 1 | 10 | |
Expand Down

0 comments on commit 552ce80

Please sign in to comment.