diff --git a/CHANGELOG.md b/CHANGELOG.md index f66acb9f71..eaf9a44228 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Features +- program: allow resolve perp pnl deficit if pnl pool isnt 0 but at deficit ([#1909](https://github.com/drift-labs/protocol-v2/pull/1909)) - program: auction order params account for twap divergence ([#1882](https://github.com/drift-labs/protocol-v2/pull/1882)) - program: add delegate stake if ([#1859](https://github.com/drift-labs/protocol-v2/pull/1859)) diff --git a/programs/drift/src/controller/amm.rs b/programs/drift/src/controller/amm.rs index 1088b680e7..0012b4804f 100644 --- a/programs/drift/src/controller/amm.rs +++ b/programs/drift/src/controller/amm.rs @@ -11,7 +11,7 @@ use crate::controller::spot_balance::{ }; use crate::error::{DriftResult, ErrorCode}; use crate::get_then_update_id; -use crate::math::amm::calculate_quote_asset_amount_swapped; +use crate::math::amm::{calculate_net_user_pnl, calculate_quote_asset_amount_swapped}; use crate::math::amm_spread::{calculate_spread_reserves, get_spread_reserves}; use crate::math::casting::Cast; use crate::math::constants::{ @@ -942,7 +942,7 @@ pub fn calculate_perp_market_amm_summary_stats( .safe_add(fee_pool_token_amount)? .cast()?; - let net_user_pnl = amm::calculate_net_user_pnl(&perp_market.amm, perp_market_oracle_price)?; + let net_user_pnl = calculate_net_user_pnl(&perp_market.amm, perp_market_oracle_price)?; // amm's mm_fee can be incorrect with drifting integer math error let mut new_total_fee_minus_distributions = pnl_tokens_available.safe_sub(net_user_pnl)?; diff --git a/programs/drift/src/controller/insurance.rs b/programs/drift/src/controller/insurance.rs index 04f98f7e89..05770be9cf 100644 --- a/programs/drift/src/controller/insurance.rs +++ b/programs/drift/src/controller/insurance.rs @@ -843,11 +843,20 @@ pub fn resolve_perp_pnl_deficit( &SpotBalanceType::Deposit, )?; + let net_user_pnl = calculate_net_user_pnl( + &market.amm, + market + .amm + .historical_oracle_data + .last_oracle_price_twap_5min, + )?; + validate!( - pnl_pool_token_amount == 0, + pnl_pool_token_amount.cast::()? < net_user_pnl, ErrorCode::SufficientPerpPnlPool, - "pnl_pool_token_amount > 0 (={})", - pnl_pool_token_amount + "pnl_pool_token_amount >= net_user_pnl ({} >= {})", + pnl_pool_token_amount, + net_user_pnl )?; update_spot_market_cumulative_interest(spot_market, None, now)?;