diff --git a/CHANGELOG.md b/CHANGELOG.md index b749df6c4..175bd0d02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixes +- program: account for fee pool when settling positive pnl ([#687](https://github.com/drift-labs/protocol-v2/pull/687)) + ### Breaking ## [2.42.0] - 2023-10-26 diff --git a/programs/drift/src/controller/pnl.rs b/programs/drift/src/controller/pnl.rs index fed6f85f8..e39bdaea6 100644 --- a/programs/drift/src/controller/pnl.rs +++ b/programs/drift/src/controller/pnl.rs @@ -109,11 +109,23 @@ pub fn settle_pnl( perp_market.pnl_pool.scaled_balance, spot_market, perp_market.pnl_pool.balance_type(), + )?; + + let fraction_of_fee_pool_token_amount = get_token_amount( + perp_market.amm.fee_pool.scaled_balance, + spot_market, + perp_market.amm.fee_pool.balance_type(), )? - .cast()?; + .safe_div(5)?; + + // add a buffer from fee pool for pnl pool balance + let pnl_tokens_available: i128 = pnl_pool_token_amount + .safe_add(fraction_of_fee_pool_token_amount)? + .cast()?; + let net_user_pnl = calculate_net_user_pnl(&perp_market.amm, oracle_price)?; - let max_pnl_pool_excess = if net_user_pnl < pnl_pool_token_amount { - pnl_pool_token_amount.safe_sub(net_user_pnl.max(0))? + let max_pnl_pool_excess = if net_user_pnl < pnl_tokens_available { + pnl_tokens_available.safe_sub(net_user_pnl.max(0))? } else { 0 };