Skip to content

Commit

Permalink
program: track total_fee_earned_per_lp on amm (#526)
Browse files Browse the repository at this point in the history
* bigz/add-total_fee_earned_per_lp-to-amm

* CHANGELOG

---------

Co-authored-by: Chris Heaney <[email protected]>
  • Loading branch information
0xbigz and crispheaney committed Jul 10, 2023
1 parent da890e2 commit 5bce22b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
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: track `total_fee_earned_per_lp` on amm (([#526](https://github.com/drift-labs/protocol-v2/pull/526)))
- program: add additional withdraw/borrow guards around fast utilization changes (([#517](https://github.com/drift-labs/protocol-v2/pull/517)))
- program: new margin type for when orders are being filled (([#518](https://github.com/drift-labs/protocol-v2/pull/518)))
- program: new fill price bands (([#516](https://github.com/drift-labs/protocol-v2/pull/516)))
Expand Down
14 changes: 9 additions & 5 deletions programs/drift/src/controller/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,19 +434,23 @@ pub fn update_lp_market_position(
.safe_mul(user_lp_shares.cast::<i128>()?)?
.safe_div(total_lp_shares.cast::<i128>()?)?;

let per_lp_fee = if lp_fee > 0 {
let per_lp_fee: i128 = if lp_fee > 0 {
lp_fee
.safe_mul(AMM_RESERVE_PRECISION_I128)?
.safe_div(user_lp_shares.cast::<i128>()?)?
} else {
0
};

// update per lp position
market.amm.quote_asset_amount_per_lp = market
// track total fee earned by lps (to attribute breakdown of IL)
market.amm.total_fee_earned_per_lp = market
.amm
.quote_asset_amount_per_lp
.safe_add(per_lp_fee.cast()?)?;
.total_fee_earned_per_lp
.saturating_add(per_lp_fee.cast()?);

// update per lp position
market.amm.quote_asset_amount_per_lp =
market.amm.quote_asset_amount_per_lp.safe_add(per_lp_fee)?;

market.amm.base_asset_amount_with_amm = market
.amm
Expand Down
4 changes: 3 additions & 1 deletion programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,9 @@ pub fn handle_initialize_perp_market(

last_oracle_valid: false,
target_base_asset_amount_per_lp: 0,
padding: [0; 44],
padding1: 0,
total_fee_earned_per_lp: 0,
padding: [0; 32],
},
};

Expand Down
8 changes: 6 additions & 2 deletions programs/drift/src/state/perp_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,9 @@ pub struct AMM {
/// the target value for `base_asset_amount_per_lp`, used during AMM JIT with LP split
/// precision: BASE_PRECISION
pub target_base_asset_amount_per_lp: i32,
pub padding: [u8; 44],
pub padding1: u32,
pub total_fee_earned_per_lp: u64,
pub padding: [u8; 32],
}

impl Default for AMM {
Expand Down Expand Up @@ -712,7 +714,9 @@ impl Default for AMM {
oracle_source: OracleSource::default(),
last_oracle_valid: false,
target_base_asset_amount_per_lp: 0,
padding: [0; 44],
padding1: 0,
total_fee_earned_per_lp: 0,
padding: [0; 32],
}
}
}
Expand Down

0 comments on commit 5bce22b

Please sign in to comment.