Skip to content

Commit

Permalink
program: settle pnl looks at fee pool for postive pnl (#687)
Browse files Browse the repository at this point in the history
* bigz/tactical-settle-pnl-pool-excess-fix

* CHANGELOG

---------

Co-authored-by: Chris Heaney <[email protected]>
  • Loading branch information
0xbigz and crispheaney committed Nov 9, 2023
1 parent 9568d48 commit 96cf75d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions programs/drift/src/controller/pnl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down

0 comments on commit 96cf75d

Please sign in to comment.