Skip to content

Commit

Permalink
program: account for fuel when there is full withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Jan 7, 2025
1 parent 68e25c7 commit da6a129
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
23 changes: 23 additions & 0 deletions programs/drift/src/math/margin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,29 @@ pub fn calculate_margin_requirement_and_total_collateral_and_liability_info(
validation::position::validate_spot_position(spot_position)?;

if spot_position.is_available() {
// might need to update fuel to account for spot market deltas
if calculation.context.has_fuel_spot_delta(spot_position.market_index) {
let spot_market = spot_market_map.get_ref(&spot_position.market_index)?;
let (oracle_price_data, oracle_validity) = oracle_map.get_price_data_and_validity(
MarketType::Spot,
spot_market.market_index,
&spot_market.oracle_id(),
spot_market.historical_oracle_data.last_oracle_price_twap,
spot_market.get_max_confidence_interval_multiplier()?,
)?;

let strict_oracle_price = StrictOraclePrice::new(
oracle_price_data.price,
spot_market
.historical_oracle_data
.last_oracle_price_twap_5min,
calculation.context.strict,
);
strict_oracle_price.validate()?;

calculation.update_fuel_spot_bonus(&spot_market, 0, &strict_oracle_price)?;
}

continue;
}

Expand Down
4 changes: 4 additions & 0 deletions programs/drift/src/state/margin_calculation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ impl MarginContext {
self
}

pub fn has_fuel_spot_delta(&self, market_index: u16) -> bool {
self.fuel_spot_deltas.iter().any(|(index, amount)| *index == market_index && *amount != 0)
}

pub fn fuel_numerator(mut self, user: &User, now: i64) -> Self {
self.fuel_bonus_numerator = user.get_fuel_bonus_numerator(now).unwrap();
self
Expand Down

0 comments on commit da6a129

Please sign in to comment.