Skip to content

Commit 6a3ab21

Browse files
committed
avoid duplicate remainder standarize
1 parent 4170be5 commit 6a3ab21

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

programs/drift/src/controller/lp.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ pub fn settle_lp_position(
183183

184184
apply_lp_rebase_to_perp_position(market, position)?;
185185

186-
let mut lp_metrics: crate::math::lp::LPMetrics =
186+
let lp_metrics: crate::math::lp::LPMetrics =
187187
calculate_settle_lp_metrics(&market.amm, position)?;
188188

189189
let position_delta = PositionDelta {
@@ -192,30 +192,7 @@ pub fn settle_lp_position(
192192
remainder_base_asset_amount: Some(lp_metrics.remainder_base_asset_amount.cast::<i64>()?),
193193
};
194194

195-
let new_remainder_base_asset_amount = position
196-
.remainder_base_asset_amount
197-
.cast::<i64>()?
198-
.safe_add(lp_metrics.remainder_base_asset_amount.cast()?)?;
199-
200-
201-
let pnl = update_position_and_market(position, market, &position_delta)?;
202-
203-
if new_remainder_base_asset_amount.unsigned_abs() >= market.amm.order_step_size {
204-
let (standardized_remainder_base_asset_amount, _remainder_base_asset_amount) =
205-
crate::math::orders::standardize_base_asset_amount_with_remainder_i128(
206-
new_remainder_base_asset_amount.cast()?,
207-
market.amm.order_step_size.cast()?,
208-
)?;
209-
210-
lp_metrics.base_asset_amount = lp_metrics
211-
.base_asset_amount
212-
.safe_add(standardized_remainder_base_asset_amount)?;
213-
}
214-
215-
market.amm.base_asset_amount_with_unsettled_lp = market
216-
.amm
217-
.base_asset_amount_with_unsettled_lp
218-
.safe_add(lp_metrics.base_asset_amount.cast()?)?;
195+
let pnl: i64 = update_position_and_market(position, market, &position_delta)?;
219196

220197
position.last_base_asset_amount_per_lp = market.amm.base_asset_amount_per_lp.cast()?;
221198
position.last_quote_asset_amount_per_lp = market.amm.quote_asset_amount_per_lp.cast()?;

programs/drift/src/controller/position.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,27 +111,16 @@ pub fn update_position_and_market(
111111

112112
let update_type = get_position_update_type(position, delta)?;
113113

114-
let (standardized_delta_base, _remainder_base_asset_amount) =
115-
crate::math::orders::standardize_base_asset_amount_with_remainder_i128(
116-
delta.base_asset_amount.cast()?,
117-
market.amm.order_step_size.cast()?,
118-
)?;
119-
validate!(
120-
_remainder_base_asset_amount == 0,
121-
ErrorCode::InvalidPositionDelta
122-
)?;
123-
124-
msg!("delta base");
125-
crate::dlog!(standardized_delta_base, _remainder_base_asset_amount);
126-
127114
// Update User
128115
let (
129116
new_base_asset_amount,
130-
_new_settled_base_asset_amount,
117+
new_settled_base_asset_amount,
131118
new_quote_asset_amount,
132119
new_remainder_base_asset_amount,
133120
) = get_new_position_amounts(position, delta, market)?;
134121

122+
market.update_market_with_counterparty(delta, new_settled_base_asset_amount)?;
123+
135124
let (new_quote_entry_amount, new_quote_break_even_amount, pnl) = match update_type {
136125
PositionUpdateType::Open | PositionUpdateType::Increase => {
137126
let new_quote_entry_amount = position

programs/drift/src/state/perp_market.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,23 @@ impl PerpMarket {
441441

442442
Ok(depth)
443443
}
444+
445+
pub fn update_market_with_counterparty(
446+
&mut self,
447+
delta: &PositionDelta,
448+
new_settled_base_asset_amount: i64,
449+
) -> DriftResult {
450+
if delta.remainder_base_asset_amount.is_some() {
451+
// todo: name for this is confusing, but adding is correct as is
452+
// definition: net position of users in the market that has the LP as a counterparty (which have NOT settled)
453+
self.amm.base_asset_amount_with_unsettled_lp = self
454+
.amm
455+
.base_asset_amount_with_unsettled_lp
456+
.safe_add(new_settled_base_asset_amount.cast()?)?;
457+
}
458+
459+
Ok(())
460+
}
444461
}
445462

446463
#[cfg(test)]

0 commit comments

Comments
 (0)