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

program: account for fuel when there is full withdraw #1413

Merged
merged 6 commits into from
Jan 8, 2025
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
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
Loading