Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 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
e1bde42
feat: initial SDK Changes for iso pos
LukasDeco Aug 25, 2025
db4a374
feat: margin calc unit tests
LukasDeco Aug 25, 2025
950b3c3
temp
LukasDeco Aug 26, 2025
b24334c
feat: finally - parity with on-chain cargo test
LukasDeco Aug 28, 2025
3606bb4
fix: PR feedback and cleanup + decoding position flag wrong
LukasDeco Aug 28, 2025
99c8268
feat: deposit into iso position ixs
LukasDeco Aug 30, 2025
88d7b76
temp: pr feedback nother round
LukasDeco Sep 2, 2025
d956c23
feat: per perp pos max margin ratio
LukasDeco Sep 2, 2025
826c962
feat: additional ixs for transfer into iso + update perp margin ratio
LukasDeco Sep 2, 2025
46cc352
feat: revamp liquidation checker functions for cross vs iso margin
LukasDeco Sep 3, 2025
9c6b4b0
fix: adjust health getter for user
LukasDeco Sep 3, 2025
9294340
fix: liq statuses add to return signature
LukasDeco Sep 3, 2025
c45be38
chore: post rebase cleaner upper
LukasDeco Oct 14, 2025
afd9fee
fix: missing params from per market lev
LukasDeco Oct 15, 2025
788bdef
feat: zero out account withdraw from perp position
LukasDeco Oct 15, 2025
5f90487
fix: available positions logic update for iso
LukasDeco Oct 15, 2025
b2999c8
Merge remote-tracking branch 'origin/master' into lukas/isolated-posi…
LukasDeco Oct 15, 2025
e11a2ec
feat: iso position txs cleanup + ix ordering
LukasDeco Oct 19, 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 @@ -590,7 +590,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 @@ -738,12 +738,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 @@ -783,15 +784,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 @@ -822,14 +823,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