Skip to content

Commit

Permalink
program: account for fuel when there is full withdraw (#1413)
Browse files Browse the repository at this point in the history
* program: account for fuel when there is full withdraw

* simplification

* update to have its own loop

* rm has fuel delta

* cAHNGELOG
  • Loading branch information
crispheaney authored Jan 8, 2025
1 parent f316013 commit 51b7053
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 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 fuel when there is full withdraw ([#1413](https://github.com/drift-labs/protocol-v2/pull/1413))

### Breaking

## [2.105.0] - 2025-01-02
Expand Down
31 changes: 31 additions & 0 deletions programs/drift/src/math/margin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,37 @@ pub fn calculate_margin_requirement_and_total_collateral_and_liability_info(

calculation.validate_num_spot_liabilities()?;

// update fuel to account for spot market deltas where there is no spot position
let spot_fuel_deltas = calculation.context.fuel_spot_deltas;
for (market_index, delta) in spot_fuel_deltas.iter() {
if *delta == 0 {
continue;
}

let spot_position = match user
.spot_positions
.iter()
.find(|p| p.market_index == *market_index && !p.is_available())
{
Some(p) => p,
None => continue,
};

let spot_market = spot_market_map.get_ref(&spot_position.market_index)?;
let oracle_price_data = oracle_map.get_price_data(&spot_market.oracle_id())?;

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)?;
}

Ok(calculation)
}

Expand Down

0 comments on commit 51b7053

Please sign in to comment.