Skip to content

Commit f941e0d

Browse files
0xbigzcrispheaney
andauthored
program: allow resolve perp pnl deficit if pnl pool isnt 0 but at deficit (#1909)
* program: update-resolve-perp-pnl-pool-validate * CHANGELOG --------- Co-authored-by: Chris Heaney <[email protected]>
1 parent d02d126 commit f941e0d

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

CHANGELOG.md

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

1010
### Features
1111

12+
- program: allow resolve perp pnl deficit if pnl pool isnt 0 but at deficit ([#1909](https://github.com/drift-labs/protocol-v2/pull/1909))
1213
- program: auction order params account for twap divergence ([#1882](https://github.com/drift-labs/protocol-v2/pull/1882))
1314
- program: add delegate stake if ([#1859](https://github.com/drift-labs/protocol-v2/pull/1859))
1415

programs/drift/src/controller/amm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::controller::spot_balance::{
1111
};
1212
use crate::error::{DriftResult, ErrorCode};
1313
use crate::get_then_update_id;
14-
use crate::math::amm::calculate_quote_asset_amount_swapped;
14+
use crate::math::amm::{calculate_net_user_pnl, calculate_quote_asset_amount_swapped};
1515
use crate::math::amm_spread::{calculate_spread_reserves, get_spread_reserves};
1616
use crate::math::casting::Cast;
1717
use crate::math::constants::{
@@ -942,7 +942,7 @@ pub fn calculate_perp_market_amm_summary_stats(
942942
.safe_add(fee_pool_token_amount)?
943943
.cast()?;
944944

945-
let net_user_pnl = amm::calculate_net_user_pnl(&perp_market.amm, perp_market_oracle_price)?;
945+
let net_user_pnl = calculate_net_user_pnl(&perp_market.amm, perp_market_oracle_price)?;
946946

947947
// amm's mm_fee can be incorrect with drifting integer math error
948948
let mut new_total_fee_minus_distributions = pnl_tokens_available.safe_sub(net_user_pnl)?;

programs/drift/src/controller/insurance.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -843,11 +843,20 @@ pub fn resolve_perp_pnl_deficit(
843843
&SpotBalanceType::Deposit,
844844
)?;
845845

846+
let net_user_pnl = calculate_net_user_pnl(
847+
&market.amm,
848+
market
849+
.amm
850+
.historical_oracle_data
851+
.last_oracle_price_twap_5min,
852+
)?;
853+
846854
validate!(
847-
pnl_pool_token_amount == 0,
855+
pnl_pool_token_amount.cast::<i128>()? < net_user_pnl,
848856
ErrorCode::SufficientPerpPnlPool,
849-
"pnl_pool_token_amount > 0 (={})",
850-
pnl_pool_token_amount
857+
"pnl_pool_token_amount >= net_user_pnl ({} >= {})",
858+
pnl_pool_token_amount,
859+
net_user_pnl
851860
)?;
852861

853862
update_spot_market_cumulative_interest(spot_market, None, now)?;

0 commit comments

Comments
 (0)