Skip to content

Commit 552ce80

Browse files
committed
fix: prevent a pegged order submission that would cross mid
1 parent 694061d commit 552ce80

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

core/execution/future/market.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,13 @@ func (m *Market) validateOrder(ctx context.Context, order *types.Order) (err err
18301830
}
18311831
return reason
18321832
}
1833+
if order.PeggedOrder.Reference == types.PeggedReferenceMid {
1834+
offsetInAsset, _ := num.UintFromDecimal(order.PeggedOrder.Offset.ToDecimal().Mul(m.priceFactor))
1835+
tickSizeInAsset, _ := num.UintFromDecimal(m.mkt.TickSize.ToDecimal().Mul(m.priceFactor))
1836+
if offsetInAsset.IsZero() && tickSizeInAsset.IsZero() {
1837+
return fmt.Errorf("invalid offset - pegged mid will cross")
1838+
}
1839+
}
18331840
return m.validateTickSize(order.PeggedOrder.Offset)
18341841
}
18351842

core/execution/spot/market.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,13 @@ func (m *Market) validateOrder(ctx context.Context, order *types.Order) (err err
10461046
}
10471047
return reason
10481048
}
1049+
if order.PeggedOrder.Reference == types.PeggedReferenceMid {
1050+
offsetInAsset, _ := num.UintFromDecimal(order.PeggedOrder.Offset.ToDecimal().Mul(m.priceFactor))
1051+
tickSizeInAsset, _ := num.UintFromDecimal(m.mkt.TickSize.ToDecimal().Mul(m.priceFactor))
1052+
if offsetInAsset.IsZero() && tickSizeInAsset.IsZero() {
1053+
return fmt.Errorf("invalid offset - pegged mid will cross")
1054+
}
1055+
}
10491056
return m.validateTickSize(order.PeggedOrder.Offset)
10501057
}
10511058

core/integration/features/verified/pegged_orders.feature

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11

22
Feature: Pegged orders do not cross
33

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

66
- Market decimals > asset decimals
77
- Market decimals < asset decimals
88
99
- For each of the above
10-
- tick size cannot be expressed in asset decimals
11-
- tick size can just be expressed in asset decimals
12-
- tick size can be expressed in asset decimals
10+
- tick size cannot be expressed in asset decimals
11+
- tick size can just be expressed in asset decimals
12+
- tick size can be expressed in asset decimals
1313
14-
- For each of the above
15-
- offset cannot be expressed in asset decimals
16-
- offset can just be expressed in asset decimals
17-
- offset can be expressed in asset decimals
14+
- For each of the above
15+
- offset cannot be expressed in asset decimals
16+
- offset can just be expressed in asset decimals
17+
- offset can be expressed in asset decimals
1818
1919
Background:
2020
@@ -52,17 +52,17 @@ Feature: Pegged orders do not cross
5252
When the opening auction period ends for market "ETH.1.10/DEC21"
5353
Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH.1.10/DEC21"
5454

55-
Examples:
56-
| bo | tick size | offset | error |
57-
| 1010 | 1 | 1 | |
58-
| 1010 | 1 | 10 | |
59-
| 1010 | 1 | 100 | |
60-
| 1010 | 10 | 1 | OrderError: price not in tick size |
61-
| 1010 | 10 | 10 | |
62-
| 1010 | 10 | 100 | |
63-
| 1100 | 100 | 1 | OrderError: price not in tick size |
64-
| 1100 | 100 | 10 | OrderError: price not in tick size |
65-
| 1100 | 100 | 100 | |
55+
Examples:
56+
| bo | tick size | offset | error |
57+
| 1010 | 1 | 1 | invalid offset - pegged mid will cross |
58+
| 1010 | 1 | 10 | |
59+
| 1010 | 1 | 100 | |
60+
| 1010 | 10 | 1 | OrderError: price not in tick size |
61+
| 1010 | 10 | 10 | |
62+
| 1010 | 10 | 100 | |
63+
| 1100 | 100 | 1 | OrderError: price not in tick size |
64+
| 1100 | 100 | 10 | OrderError: price not in tick size |
65+
| 1100 | 100 | 100 | |
6666

6767

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

86-
Examples:
86+
Examples:
8787
| bo | tick size | offset | error |
8888
| 1001 | 1 | 1 | |
8989
| 1001 | 1 | 10 | |

0 commit comments

Comments
 (0)