Skip to content
Open
Show file tree
Hide file tree
Changes from 77 commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
df3ab76
program: make lp shares reduce only
crispheaney Jul 16, 2025
fed9dc6
init
crispheaney Jul 19, 2025
e99ffa7
rm more fields
crispheaney Jul 19, 2025
25ab531
make tests build
crispheaney Jul 19, 2025
eba3f11
start sdk changes
crispheaney Jul 19, 2025
b58cda0
init new margin calc
crispheaney Jul 20, 2025
820c232
deposit and transfer into
crispheaney Jul 23, 2025
9efd808
add settle pnl
crispheaney Jul 23, 2025
75b92f8
program: add withdraw
crispheaney Jul 24, 2025
162fc23
add more ix
crispheaney Jul 24, 2025
82463f3
add new meets withdraw req fn
crispheaney Jul 26, 2025
fb57e5f
enter/exit liquidation logic
crispheaney Jul 27, 2025
4de579a
moar
crispheaney Jul 27, 2025
085e805
start liquidation logic
crispheaney Jul 30, 2025
4e7db0f
other liquidation fns
crispheaney Jul 30, 2025
8e89ef4
make build work
crispheaney Jul 30, 2025
8062d60
more updates
crispheaney Jul 31, 2025
991dda9
always calc isolated pos
crispheaney Aug 4, 2025
c627c1e
rm isolated position market index logic
crispheaney Aug 4, 2025
a00f3a9
moar
crispheaney Aug 5, 2025
d435dad
program: rm the isolated position market index
crispheaney Aug 5, 2025
ed76b47
some tweaks
crispheaney Aug 5, 2025
c13a605
rm some old margin code
crispheaney Aug 5, 2025
4a9aadc
tweak meets withdraw requirements
crispheaney Aug 5, 2025
0d56488
rm liquidation mode changing context
crispheaney Aug 6, 2025
584337b
handle liquidation id and bit flags
crispheaney Aug 7, 2025
15c05ee
more liquidation changes
crispheaney Aug 7, 2025
adc2815
clean
crispheaney Aug 8, 2025
0de7802
fix force cancel orders
crispheaney Aug 8, 2025
830c7c9
update validate liquidation
crispheaney Aug 8, 2025
5d09739
moar
crispheaney Aug 8, 2025
7392d3e
rename is_being_liquidated
crispheaney Aug 8, 2025
26960c8
start adding test
crispheaney Aug 15, 2025
2ab06e3
program: add validate for liq borrow for perp pnl
crispheaney Aug 15, 2025
9a56326
program: add test for isolated margin calc
crispheaney Aug 15, 2025
b171c23
is bankrupt test
crispheaney Aug 15, 2025
2821269
fix cancel orders
crispheaney Aug 19, 2025
424987f
fix set liquidation status
crispheaney Aug 19, 2025
b84daf1
more tweaks
crispheaney Aug 19, 2025
ea09842
clean up naming
crispheaney Aug 19, 2025
cc397f0
update last active slot for isolated position liq
crispheaney Aug 20, 2025
9833303
another liquidation review
crispheaney Aug 21, 2025
9cb040a
add test
crispheaney Aug 21, 2025
8177496
cargo fmt --
crispheaney Aug 21, 2025
9a8ec1a
tweak naming
crispheaney Aug 21, 2025
6ddbaf8
add test to make sure false liquidaiton wont be triggered
crispheaney Aug 25, 2025
2db2907
test meets withdraw
crispheaney Aug 25, 2025
8314bbe
change is bankrupt
crispheaney Aug 26, 2025
654683c
more
crispheaney Aug 26, 2025
4cab732
update uses of exit isolated liquidaiton
crispheaney Aug 26, 2025
6a6a150
moar
crispheaney Aug 26, 2025
7c46187
moar
crispheaney Aug 26, 2025
51ae2eb
reduce diff
crispheaney Aug 26, 2025
bae1b6b
moar
crispheaney Aug 26, 2025
d2f08ea
modularize some for tests
crispheaney Aug 26, 2025
ba8866a
add tests for the pnl for deposit liquidation
crispheaney Aug 27, 2025
9fa04fa
tests for isolated position transfer
crispheaney Aug 27, 2025
a732348
test for update spot balance
crispheaney Aug 28, 2025
91baee3
test for settle pnl
crispheaney Aug 28, 2025
101e311
add perp position max margin
crispheaney Sep 2, 2025
0bc6132
program: test for custom perp position margin ratio
crispheaney Sep 2, 2025
608928f
add test for margin calc for disable hlm
crispheaney Sep 2, 2025
fc6bebc
update test name
crispheaney Sep 2, 2025
5f3b7d0
make max margin ratio persist
crispheaney Sep 2, 2025
3c56869
add liquidation mode test
crispheaney Sep 2, 2025
bf2839e
more tests to make sure liqudiations dont bleed over
crispheaney Sep 2, 2025
eb60940
change test name
crispheaney Sep 2, 2025
e8708a8
Merge branch 'master' into crispheaney/isolated-position
crispheaney Sep 20, 2025
ba10482
fix broken cargo tests
crispheaney Sep 20, 2025
58df2ff
cargo fmt --
crispheaney Sep 20, 2025
5d61bd3
Merge branch 'master' into crispheaney/isolated-position
crispheaney Sep 20, 2025
dd49970
Merge branch 'master' into crispheaney/isolated-position
crispheaney Sep 20, 2025
71fcdfa
first ts test
crispheaney Sep 20, 2025
7af9f65
isolatedPositionLiquidatePerp test
crispheaney Sep 21, 2025
e40563e
isolatedPositionLiquidatePerpwithFill test
crispheaney Sep 21, 2025
c643a50
fix expired position
crispheaney Sep 23, 2025
16bea30
cargo fmt --
crispheaney Sep 23, 2025
453d93f
program: add transfer isolated pos deposit into swift (#1964)
crispheaney Oct 16, 2025
82fc87f
program: auto transfer to cross margin account (#1939)
crispheaney Oct 16, 2025
28ed029
make build
crispheaney Oct 22, 2025
b0a2b67
isolated pos deposit sdk
crispheaney Oct 23, 2025
3bd6eeb
test working
crispheaney Oct 23, 2025
3bd7525
move the auto transfer
crispheaney Oct 23, 2025
283016a
Merge branch 'master' into crispheaney/isolated-position
crispheaney Oct 23, 2025
bb45dcd
fix deposit order in swift
crispheaney Nov 18, 2025
69793cf
program: handle looser transfer iso deposit
crispheaney Dec 2, 2025
69b3a72
tweak
crispheaney Dec 2, 2025
a7f633c
fix warnings
crispheaney Dec 2, 2025
55d3c65
cargo fmt --
crispheaney Dec 2, 2025
d601d35
Merge branch 'master' into crispheaney/isolated-position
crispheaney Dec 2, 2025
ddeaa3a
add get total token amount
crispheaney Dec 2, 2025
355bfac
tweaks
crispheaney Dec 4, 2025
64c887d
Merge branch 'master' into crispheaney/isolated-position
crispheaney Dec 4, 2025
f4bc397
fmt
crispheaney Dec 4, 2025
c994787
fix tests
crispheaney Dec 4, 2025
9aa8dbe
fmt --
crispheaney Dec 4, 2025
83fb642
fix anchor test
crispheaney Dec 4, 2025
b6b2d69
more
crispheaney Dec 4, 2025
37a905e
add OrderBitFlag::IsIsolatedPosition (#2049)
wphan Dec 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions programs/drift/src/controller/amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ fn calculate_revenue_pool_transfer(
pub fn update_pool_balances(
market: &mut PerpMarket,
spot_market: &mut SpotMarket,
user_quote_position: &SpotPosition,
user_quote_token_amount: i128,
user_unsettled_pnl: i128,
now: i64,
) -> DriftResult<i128> {
Expand Down Expand Up @@ -725,12 +725,13 @@ pub fn update_pool_balances(
let pnl_to_settle_with_user = if user_unsettled_pnl > 0 {
min(user_unsettled_pnl, pnl_pool_token_amount.cast::<i128>()?)
} else {
let token_amount = user_quote_position.get_signed_token_amount(spot_market)?;

// dont settle negative pnl to spot borrows when utilization is high (> 80%)
let max_withdraw_amount =
-get_max_withdraw_for_market_with_token_amount(spot_market, token_amount, false)?
.cast::<i128>()?;
let max_withdraw_amount = -get_max_withdraw_for_market_with_token_amount(
spot_market,
user_quote_token_amount,
false,
)?
.cast::<i128>()?;

max_withdraw_amount.max(user_unsettled_pnl)
};
Expand Down Expand Up @@ -770,15 +771,15 @@ pub fn update_pool_balances(

pub fn update_pnl_pool_and_user_balance(
market: &mut PerpMarket,
bank: &mut SpotMarket,
quote_spot_market: &mut SpotMarket,
user: &mut User,
unrealized_pnl_with_fee: i128,
) -> DriftResult<i128> {
let pnl_to_settle_with_user = if unrealized_pnl_with_fee > 0 {
unrealized_pnl_with_fee.min(
get_token_amount(
market.pnl_pool.scaled_balance,
bank,
quote_spot_market,
market.pnl_pool.balance_type(),
)?
.cast()?,
Expand Down Expand Up @@ -809,14 +810,37 @@ pub fn update_pnl_pool_and_user_balance(
return Ok(0);
}

let user_spot_position = user.get_quote_spot_position_mut();
let is_isolated_position = user.get_perp_position(market.market_index)?.is_isolated();
if is_isolated_position {
let perp_position = user.force_get_isolated_perp_position_mut(market.market_index)?;
let perp_position_token_amount =
perp_position.get_isolated_token_amount(quote_spot_market)?;

if pnl_to_settle_with_user < 0 {
validate!(
perp_position_token_amount >= pnl_to_settle_with_user.unsigned_abs(),
ErrorCode::InsufficientCollateral,
"user has insufficient deposit for market {}",
market.market_index
)?;
}

transfer_spot_balances(
pnl_to_settle_with_user,
bank,
&mut market.pnl_pool,
user_spot_position,
)?;
transfer_spot_balances(
pnl_to_settle_with_user,
quote_spot_market,
&mut market.pnl_pool,
perp_position,
)?;
} else {
let user_spot_position = user.get_quote_spot_position_mut();

transfer_spot_balances(
pnl_to_settle_with_user,
quote_spot_market,
&mut market.pnl_pool,
user_spot_position,
)?;
}

Ok(pnl_to_settle_with_user)
}
Expand Down
Loading
Loading