Skip to content

Commit 12fc68d

Browse files
committed
Merge branch 'crispheaney/isolated-position' into devnet
2 parents 1b19a32 + 283016a commit 12fc68d

File tree

17 files changed

+130
-115
lines changed

17 files changed

+130
-115
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Features
11+
12+
### Fixes
13+
14+
### Breaking
15+
16+
## [2.143.0] - 2025-10-22
17+
1018
- program: relax filling conditions for low risk orders vs amm ([#1968](https://github.com/drift-labs/protocol-v2/pull/1968))
1119
- sdk: make oracle validity match program and propogate to dlob and math functions ([#1968](https://github.com/drift-labs/protocol-v2/pull/1968))
1220

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

programs/drift/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "drift"
3-
version = "2.142.0"
3+
version = "2.143.0"
44
description = "Created with Anchor"
55
edition = "2018"
66

programs/drift/src/controller/pnl.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ pub fn settle_pnl(
5858
mut mode: SettlePnlMode,
5959
) -> DriftResult {
6060
validate!(!user.is_bankrupt(), ErrorCode::UserBankrupt)?;
61-
let slot = clock.slot;
6261
let now = clock.unix_timestamp;
6362
let tvl_before;
6463
let deposits_balance_before;
@@ -354,21 +353,6 @@ pub fn settle_pnl(
354353
drop(perp_market);
355354
drop(spot_market);
356355

357-
if user.perp_positions[position_index].can_transfer_isolated_position_deposit() {
358-
transfer_isolated_perp_position_deposit(
359-
user,
360-
None,
361-
perp_market_map,
362-
spot_market_map,
363-
oracle_map,
364-
slot,
365-
now,
366-
0,
367-
market_index,
368-
i64::MIN,
369-
)?;
370-
}
371-
372356
let perp_market = perp_market_map.get_ref(&market_index)?;
373357
let spot_market = spot_market_map.get_quote_spot_market()?;
374358

programs/drift/src/instructions/keeper.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::controller::liquidation::{
1818
};
1919
use crate::controller::orders::cancel_orders;
2020
use crate::controller::orders::validate_market_within_price_band;
21+
use crate::controller::position::get_position_index;
2122
use crate::controller::position::PositionDirection;
2223
use crate::controller::spot_balance::update_spot_balances;
2324
use crate::controller::token::{receive, send_from_program_vault};
@@ -652,7 +653,7 @@ pub fn handle_place_signed_msg_taker_order<'c: 'info, 'info>(
652653
// TODO: generalize to support multiple market types
653654
let AccountMaps {
654655
perp_market_map,
655-
spot_market_map,
656+
mut spot_market_map,
656657
mut oracle_map,
657658
} = load_maps(
658659
&mut remaining_accounts,
@@ -683,7 +684,7 @@ pub fn handle_place_signed_msg_taker_order<'c: 'info, 'info>(
683684
signed_msg_order_params_message_bytes,
684685
&ctx.accounts.ix_sysvar.to_account_info(),
685686
&perp_market_map,
686-
&spot_market_map,
687+
&mut spot_market_map,
687688
&mut oracle_map,
688689
high_leverage_mode_config,
689690
escrow,
@@ -701,7 +702,7 @@ pub fn place_signed_msg_taker_order<'c: 'info, 'info>(
701702
taker_order_params_message_bytes: Vec<u8>,
702703
ix_sysvar: &AccountInfo<'info>,
703704
perp_market_map: &PerpMarketMap,
704-
spot_market_map: &SpotMarketMap,
705+
spot_market_map: &mut SpotMarketMap,
705706
oracle_map: &mut OracleMap,
706707
high_leverage_mode_config: Option<AccountLoader<HighLeverageModeConfig>>,
707708
escrow: Option<RevenueShareEscrowZeroCopyMut<'info>>,
@@ -849,6 +850,7 @@ pub fn place_signed_msg_taker_order<'c: 'info, 'info>(
849850
}
850851

851852
if let Some(isolated_position_deposit) = verified_message_and_signature.isolated_position_deposit {
853+
spot_market_map.update_writable_spot_market(0)?;
852854
transfer_isolated_perp_position_deposit(
853855
taker,
854856
Some(taker_stats),
@@ -1091,7 +1093,6 @@ pub fn handle_settle_pnl<'c: 'info, 'info>(
10911093
)?;
10921094

10931095
let mut remaining_accounts = ctx.remaining_accounts.iter().peekable();
1094-
10951096
let AccountMaps {
10961097
perp_market_map,
10971098
spot_market_map,
@@ -1176,6 +1177,23 @@ pub fn handle_settle_pnl<'c: 'info, 'info>(
11761177
}
11771178
}
11781179

1180+
if let Ok(position_index) = get_position_index(&user.perp_positions, market_index) {
1181+
if user.perp_positions[position_index].can_transfer_isolated_position_deposit() {
1182+
transfer_isolated_perp_position_deposit(
1183+
user,
1184+
None,
1185+
&perp_market_map,
1186+
&spot_market_map,
1187+
&mut oracle_map,
1188+
clock.slot,
1189+
clock.unix_timestamp,
1190+
QUOTE_SPOT_MARKET_INDEX,
1191+
market_index,
1192+
i64::MIN,
1193+
)?;
1194+
}
1195+
}
1196+
11791197
let spot_market = spot_market_map.get_quote_spot_market()?;
11801198
validate_spot_market_vault_amount(&spot_market, ctx.accounts.spot_market_vault.amount)?;
11811199

@@ -1197,7 +1215,6 @@ pub fn handle_settle_multiple_pnls<'c: 'info, 'info>(
11971215
let user = &mut load_mut!(ctx.accounts.user)?;
11981216

11991217
let mut remaining_accounts = ctx.remaining_accounts.iter().peekable();
1200-
12011218
let AccountMaps {
12021219
perp_market_map,
12031220
spot_market_map,
@@ -1289,6 +1306,23 @@ pub fn handle_settle_multiple_pnls<'c: 'info, 'info>(
12891306
}
12901307
}
12911308
}
1309+
1310+
if let Ok(position_index) = get_position_index(&user.perp_positions, *market_index) {
1311+
if user.perp_positions[position_index].can_transfer_isolated_position_deposit() {
1312+
transfer_isolated_perp_position_deposit(
1313+
user,
1314+
None,
1315+
&perp_market_map,
1316+
&spot_market_map,
1317+
&mut oracle_map,
1318+
clock.slot,
1319+
clock.unix_timestamp,
1320+
QUOTE_SPOT_MARKET_INDEX,
1321+
*market_index,
1322+
i64::MIN,
1323+
)?;
1324+
}
1325+
}
12921326
}
12931327

12941328
let spot_market = spot_market_map.get_quote_spot_market()?;

programs/drift/src/instructions/lp_admin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ pub fn handle_begin_lp_swap<'c: 'info, 'info>(
599599
let admin = &ctx.accounts.admin;
600600
#[cfg(feature = "anchor-test")]
601601
validate!(
602-
admin.key() == admin_hot_wallet::id() || admin.key() == state.admin,
602+
admin.key() == admin_hot_wallet::id() || admin.key() == ctx.accounts.state.key(),
603603
ErrorCode::Unauthorized,
604604
"Wrong signer for lp taker swap"
605605
)?;

programs/drift/src/state/spot_market_map.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,21 @@ impl<'a> SpotMarketMap<'a> {
221221

222222
Ok(spot_market_map)
223223
}
224+
225+
pub fn update_writable_spot_market(&mut self, market_index: u16) -> DriftResult {
226+
if !self.0.contains_key(&market_index) {
227+
return Err(ErrorCode::InvalidSpotMarketAccount);
228+
}
229+
230+
let account_loader = self.0.get(&market_index).safe_unwrap()?;
231+
if !account_loader.as_ref().is_writable {
232+
return Err(ErrorCode::SpotMarketWrongMutability);
233+
}
234+
235+
self.1.insert(market_index);
236+
237+
Ok(())
238+
}
224239
}
225240

226241
#[cfg(test)]

programs/drift/src/state/user.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ impl PerpPosition {
12861286

12871287
pub fn can_transfer_isolated_position_deposit(&self) -> bool {
12881288
self.is_isolated()
1289+
&& self.isolated_position_scaled_balance > 0
12891290
&& !self.is_open_position()
12901291
&& !self.has_open_order()
12911292
&& !self.has_unsettled_pnl()

programs/drift/src/validation/sig_verification/tests.rs

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ mod sig_verification {
156156
32, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 10, 1, 128, 133, 181, 13, 0, 0, 0, 0,
157157
1, 64, 85, 32, 14, 0, 0, 0, 0, 2, 0, 41, 9, 0, 0, 0, 0, 0, 0, 67, 82, 79, 51, 105, 114,
158158
71, 49, 1, 0, 28, 78, 14, 0, 0, 0, 0, 0, 96, 254, 205, 0, 0, 0, 0, 1, 64, 58, 105, 13,
159-
0, 0, 0, 0, 0, 96, 254, 205, 0, 0, 0, 0, 0, 1, 1,
159+
0, 0, 0, 0, 0, 96, 254, 205, 0, 0, 0, 0, 0, 0, 0, 1, 1,
160160
];
161161

162162
// Test deserialization with delegate signer
@@ -355,52 +355,4 @@ mod sig_verification {
355355
assert_eq!(order_params.auction_start_price, Some(240000000i64));
356356
assert_eq!(order_params.auction_end_price, Some(238000000i64));
357357
}
358-
359-
#[test]
360-
fn test_deserialize_into_verified_message_delegate_with_max_margin_ratio_and_builder_params() {
361-
let signature = [1u8; 64];
362-
let payload = vec![
363-
200, 213, 166, 94, 34, 52, 245, 93, 0, 1, 0, 3, 0, 96, 254, 205, 0, 0, 0, 0, 64, 85,
364-
32, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 10, 1, 128, 133, 181, 13, 0, 0, 0, 0,
365-
1, 64, 85, 32, 14, 0, 0, 0, 0, 2, 0, 41, 9, 0, 0, 0, 0, 0, 0, 67, 82, 79, 51, 105, 114,
366-
71, 49, 1, 0, 28, 78, 14, 0, 0, 0, 0, 0, 96, 254, 205, 0, 0, 0, 0, 0, 1, 255, 255, 1,
367-
1, 1, 58, 0,
368-
];
369-
370-
// Test deserialization with delegate signer
371-
let result = deserialize_into_verified_message(payload, &signature, false);
372-
373-
assert!(result.is_ok());
374-
375-
let verified_message = result.unwrap();
376-
377-
// Verify the deserialized message has expected structure
378-
assert_eq!(verified_message.signature, signature);
379-
assert_eq!(verified_message.sub_account_id, Some(2));
380-
assert_eq!(verified_message.delegate_signed_taker_pubkey, None);
381-
assert_eq!(verified_message.slot, 2345);
382-
assert_eq!(verified_message.uuid, [67, 82, 79, 51, 105, 114, 71, 49]);
383-
assert_eq!(verified_message.max_margin_ratio.unwrap(), 65535);
384-
assert_eq!(verified_message.builder_idx.unwrap(), 1);
385-
assert_eq!(verified_message.builder_fee_tenth_bps.unwrap(), 58);
386-
387-
assert!(verified_message.take_profit_order_params.is_some());
388-
let tp = verified_message.take_profit_order_params.unwrap();
389-
assert_eq!(tp.base_asset_amount, 3456000000u64);
390-
assert_eq!(tp.trigger_price, 240000000u64);
391-
392-
assert!(verified_message.stop_loss_order_params.is_none());
393-
394-
// Verify order params
395-
let order_params = &verified_message.signed_msg_order_params;
396-
assert_eq!(order_params.user_order_id, 3);
397-
assert_eq!(order_params.direction, PositionDirection::Long);
398-
assert_eq!(order_params.base_asset_amount, 3456000000u64);
399-
assert_eq!(order_params.price, 237000000u64);
400-
assert_eq!(order_params.market_index, 0);
401-
assert_eq!(order_params.reduce_only, false);
402-
assert_eq!(order_params.auction_duration, Some(10));
403-
assert_eq!(order_params.auction_start_price, Some(230000000i64));
404-
assert_eq!(order_params.auction_end_price, Some(237000000i64));
405-
}
406358
}

sdk/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.143.0-beta.8
1+
2.144.0-beta.1

0 commit comments

Comments
 (0)