Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
4 changes: 2 additions & 2 deletions programs/drift/src/controller/amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)?;
Expand Down
15 changes: 12 additions & 3 deletions programs/drift/src/controller/insurance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i128>()? < 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)?;
Expand Down
Loading