Skip to content

Commit 52d7f3d

Browse files
lowkeynicccrispheaney0xahzam
authored
Devnet auto cancel tpsl 2 (#2109)
* program: add auto cancel reduce only tpsl * cargo test * cargo fmt -- * tweaks * program: rm should_expire_order_before_fill (#2103) * fix conditional * program: remove same slot matching restriction (#2104) * remove same slot restriction for swift limit fills * update comment * add changelog --------- Co-authored-by: Chris Heaney <chrisheaney30@gmail.com> Co-authored-by: 0xahzam <104062587+0xahzam@users.noreply.github.com>
1 parent ffc1d81 commit 52d7f3d

File tree

5 files changed

+60
-26
lines changed

5 files changed

+60
-26
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Features
1111

12+
- program: remove same slot matching restriction [#2104](https://github.com/drift-labs/protocol-v2/pull/2104)
13+
1214
### Fixes
1315

1416
### Breaking
@@ -47,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4749
### Fixes
4850

4951
### Breaking
52+
5053
- sdk: `channelOptions` in the GrpcConfigs type has been updated to work with new grpc lib
5154

5255
## [2.153.0] - 2025-12-30
@@ -104,11 +107,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
104107
### Fixes
105108

106109
### Breaking
110+
107111
- program: add DepositRecord::spot_balance_after [#2034](https://github.com/drift-labs/protocol-v2/pull/2034)
108112

109113
## [2.149.0] - 2025-11-19
110114

111115
### Features
116+
112117
- sdk: allow deposit from external authority directly to drift account
113118

114119
### Fixes
@@ -171,7 +176,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
171176

172177
### Features
173178

174-
- program: use-5min-for-target-expiry-price ([#1967](https://github.com/drift-labs/protocol-v2/pull/1967))
179+
- program: use-5min-for-target-expiry-price ([#1967](https://github.com/drift-labs/protocol-v2/pull/1967))
175180

176181
### Fixes
177182

programs/drift/src/controller/orders.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ pub fn fill_perp_order(
12321232
}
12331233
}
12341234

1235-
let should_expire_order = should_expire_order_before_fill(user, order_index, now)?;
1235+
let should_expire_order = should_expire_order(user, order_index, now)?;
12361236

12371237
let position_index =
12381238
get_position_index(&user.perp_positions, user.orders[order_index].market_index)?;
@@ -1369,7 +1369,7 @@ pub fn fill_perp_order(
13691369
)?
13701370
}
13711371

1372-
if base_asset_amount_after == 0 && user.perp_positions[position_index].open_orders != 0 {
1372+
if base_asset_amount_after == 0 && user.perp_positions[position_index].open_asks == 0 && user.perp_positions[position_index].open_bids == 0 {
13731373
cancel_reduce_only_trigger_orders(
13741374
user,
13751375
&user_key,
@@ -4134,7 +4134,7 @@ pub fn fill_spot_order(
41344134
}
41354135
}
41364136

4137-
let should_expire_order = should_expire_order_before_fill(user, order_index, now)?;
4137+
let should_expire_order = should_expire_order(user, order_index, now)?;
41384138

41394139
let should_cancel_reduce_only = if user.orders[order_index].reduce_only {
41404140
let market_index = user.orders[order_index].market_index;

programs/drift/src/math/matching.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ pub fn is_maker_for_taker(
1717
taker_order: &Order,
1818
slot: u64,
1919
) -> DriftResult<bool> {
20-
// Maker and taker order not allowed to match if both were placed in the current slot
21-
if slot == maker_order.slot && slot == taker_order.slot && !maker_order.is_jit_maker() {
22-
return Ok(false);
23-
};
20+
// Self match protection handled upstream via maker_key != taker_key check.
21+
// Removed slot equality restriction to enable same slot fills.
2422

2523
// taker cant be post only and maker must be resting limit order
2624
if taker_order.post_only || !maker_order.is_resting_limit_order(slot)? {

programs/drift/src/math/matching/tests.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,55 @@ mod is_maker_for_taker {
177177

178178
assert_eq!(is_maker_for_taker(&maker, &taker, slot).unwrap(), true);
179179
}
180+
181+
#[test]
182+
fn same_slot_maker_taker_non_resting() {
183+
let slot = 0;
184+
185+
// Taker market order
186+
let taker = Order {
187+
post_only: false,
188+
order_type: OrderType::Market,
189+
slot: 0,
190+
..Default::default()
191+
};
192+
193+
// Maker resting with 0 auction
194+
let maker = Order {
195+
post_only: false,
196+
order_type: OrderType::Limit,
197+
slot: 0,
198+
auction_duration: 0,
199+
..Default::default()
200+
};
201+
202+
assert_eq!(is_maker_for_taker(&maker, &taker, slot).unwrap(), true);
203+
}
204+
205+
#[test]
206+
fn same_slot_maker_taker_both_resting() {
207+
let slot = 20;
208+
209+
// Resting taker order
210+
let taker = Order {
211+
post_only: false,
212+
order_type: OrderType::Limit,
213+
slot: 10,
214+
auction_duration: 5,
215+
..Default::default()
216+
};
217+
218+
// Resting maker order (older)
219+
let maker = Order {
220+
post_only: false,
221+
order_type: OrderType::Limit,
222+
slot: 5,
223+
auction_duration: 2,
224+
..Default::default()
225+
};
226+
227+
assert_eq!(is_maker_for_taker(&maker, &taker, slot).unwrap(), true);
228+
}
180229
}
181230

182231
#[test]

programs/drift/src/math/orders.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -354,24 +354,6 @@ pub fn get_position_delta_for_fill(
354354
})
355355
}
356356

357-
#[inline(always)]
358-
pub fn should_expire_order_before_fill(
359-
user: &User,
360-
order_index: usize,
361-
now: i64,
362-
) -> DriftResult<bool> {
363-
let should_order_be_expired = should_expire_order(user, order_index, now)?;
364-
if should_order_be_expired && user.orders[order_index].is_limit_order() {
365-
let now_sub_buffer = now.safe_sub(15)?;
366-
if !should_expire_order(user, order_index, now_sub_buffer)? {
367-
msg!("invalid fill. cant force expire limit order until 15s after max_ts. max ts {}, now {}, now plus buffer {}", user.orders[order_index].max_ts, now, now_sub_buffer);
368-
return Err(ErrorCode::ImpossibleFill);
369-
}
370-
}
371-
372-
Ok(should_order_be_expired)
373-
}
374-
375357
#[inline(always)]
376358
pub fn should_expire_order(user: &User, user_order_index: usize, now: i64) -> DriftResult<bool> {
377359
let order = &user.orders[user_order_index];

0 commit comments

Comments
 (0)