Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bigz/add-total_fee_earned_per_lp-to-amm #526

Merged
merged 2 commits into from
Jul 10, 2023
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: 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
Loading