Skip to content

Commit

Permalink
Revert "program: refactor oracle map to allow same oracle w diff orac…
Browse files Browse the repository at this point in the history
…le sources (#1346)"

This reverts commit d09652a.
  • Loading branch information
crispheaney committed Dec 2, 2024
1 parent 404db7f commit 6b5f75d
Show file tree
Hide file tree
Showing 36 changed files with 492 additions and 701 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Features

- program: add spot market pool ids ([#1250](https://github.com/drift-labs/protocol-v2/pull/1250))
- program: make oracle map work with different sources ([#1346](https://github.com/drift-labs/protocol-v2/pull/1346))

### Fixes

Expand Down
4 changes: 2 additions & 2 deletions programs/drift/src/controller/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub fn update_funding_rate(
// Pause funding if oracle is invalid or if mark/oracle spread is too divergent
let block_funding_rate_update = oracle::block_operation(
market,
oracle_map.get_price_data(&market.oracle_id())?,
oracle_map.get_price_data(&market.amm.oracle)?,
guard_rails,
reserve_price,
slot,
Expand All @@ -186,7 +186,7 @@ pub fn update_funding_rate(
!funding_paused && !block_funding_rate_update && (time_until_next_update == 0);

if valid_funding_update {
let oracle_price_data = oracle_map.get_price_data(&market.oracle_id())?;
let oracle_price_data = oracle_map.get_price_data(&market.amm.oracle)?;
let sanitize_clamp_denominator = market.get_sanitize_clamp_denominator()?;

let oracle_price_twap = amm::update_oracle_price_twap(
Expand Down
40 changes: 16 additions & 24 deletions programs/drift/src/controller/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub fn liquidate_perp(
)?;

let mut market = perp_market_map.get_ref_mut(&market_index)?;
let oracle_price_data = oracle_map.get_price_data(&market.oracle_id())?;
let oracle_price_data = oracle_map.get_price_data(&market.amm.oracle)?;

update_amm_and_check_validity(
&mut market,
Expand Down Expand Up @@ -340,9 +340,7 @@ pub fn liquidate_perp(

let market = perp_market_map.get_ref(&market_index)?;
let quote_spot_market = spot_market_map.get_ref(&market.quote_spot_market_index)?;
let quote_oracle_price = oracle_map
.get_price_data(&quote_spot_market.oracle_id())?
.price;
let quote_oracle_price = oracle_map.get_price_data(&quote_spot_market.oracle)?.price;
let liquidator_fee = market.liquidator_fee;
let if_liquidation_fee = calculate_perp_if_fee(
intermediate_margin_calculation.tracked_market_margin_shortage(margin_shortage)?,
Expand Down Expand Up @@ -789,7 +787,7 @@ pub fn liquidate_perp_with_fill(
)?;

let mut market = perp_market_map.get_ref_mut(&market_index)?;
let oracle_price_data = oracle_map.get_price_data(&market.oracle_id())?;
let oracle_price_data = oracle_map.get_price_data(&market.amm.oracle)?;

update_amm_and_check_validity(
&mut market,
Expand Down Expand Up @@ -917,9 +915,7 @@ pub fn liquidate_perp_with_fill(

let market = perp_market_map.get_ref(&market_index)?;
let quote_spot_market = spot_market_map.get_ref(&market.quote_spot_market_index)?;
let quote_oracle_price = oracle_map
.get_price_data(&quote_spot_market.oracle_id())?
.price;
let quote_oracle_price = oracle_map.get_price_data(&quote_spot_market.oracle)?.price;
let liquidator_fee = market.liquidator_fee;
let if_liquidation_fee = calculate_perp_if_fee(
intermediate_margin_calculation.tracked_market_margin_shortage(margin_shortage)?,
Expand Down Expand Up @@ -1237,7 +1233,7 @@ pub fn liquidate_spot(
let (asset_amount, asset_price, asset_decimals, asset_weight, asset_liquidation_multiplier) = {
let mut asset_market = spot_market_map.get_ref_mut(&asset_market_index)?;
let (asset_price_data, validity_guard_rails) =
oracle_map.get_price_data_and_guard_rails(&asset_market.oracle_id())?;
oracle_map.get_price_data_and_guard_rails(&asset_market.oracle)?;

update_spot_market_and_check_validity(
&mut asset_market,
Expand Down Expand Up @@ -1286,7 +1282,7 @@ pub fn liquidate_spot(
) = {
let mut liability_market = spot_market_map.get_ref_mut(&liability_market_index)?;
let (liability_price_data, validity_guard_rails) =
oracle_map.get_price_data_and_guard_rails(&liability_market.oracle_id())?;
oracle_map.get_price_data_and_guard_rails(&liability_market.oracle)?;

update_spot_market_and_check_validity(
&mut liability_market,
Expand Down Expand Up @@ -1824,9 +1820,7 @@ pub fn liquidate_borrow_for_perp_pnl(
let market = perp_market_map.get_ref(&perp_market_index)?;

let quote_spot_market = spot_market_map.get_ref(&market.quote_spot_market_index)?;
let quote_price = oracle_map
.get_price_data(&quote_spot_market.oracle_id())?
.price;
let quote_price = oracle_map.get_price_data(&quote_spot_market.oracle)?.price;

let pnl_asset_weight =
market.get_unrealized_asset_weight(pnl, MarginRequirementType::Maintenance)?;
Expand All @@ -1852,7 +1846,7 @@ pub fn liquidate_borrow_for_perp_pnl(
) = {
let mut liability_market = spot_market_map.get_ref_mut(&liability_market_index)?;
let (liability_price_data, validity_guard_rails) =
oracle_map.get_price_data_and_guard_rails(&liability_market.oracle_id())?;
oracle_map.get_price_data_and_guard_rails(&liability_market.oracle)?;

update_spot_market_and_check_validity(
&mut liability_market,
Expand Down Expand Up @@ -1946,7 +1940,7 @@ pub fn liquidate_borrow_for_perp_pnl(

if intermediate_margin_calculation.can_exit_liquidation()? {
let market = perp_market_map.get_ref(&perp_market_index)?;
let market_oracle_price = oracle_map.get_price_data(&market.oracle_id())?.price;
let market_oracle_price = oracle_map.get_price_data(&market.amm.oracle)?.price;

emit!(LiquidationRecord {
ts: now,
Expand Down Expand Up @@ -2137,7 +2131,7 @@ pub fn liquidate_borrow_for_perp_pnl(

let market_oracle_price = {
let market = perp_market_map.get_ref_mut(&perp_market_index)?;
oracle_map.get_price_data(&market.oracle_id())?.price
oracle_map.get_price_data(&market.amm.oracle)?.price
};

emit!(LiquidationRecord {
Expand Down Expand Up @@ -2282,7 +2276,7 @@ pub fn liquidate_perp_pnl_for_deposit(
) = {
let mut asset_market = spot_market_map.get_ref_mut(&asset_market_index)?;
let (asset_price_data, validity_guard_rails) =
oracle_map.get_price_data_and_guard_rails(&asset_market.oracle_id())?;
oracle_map.get_price_data_and_guard_rails(&asset_market.oracle)?;

update_spot_market_and_check_validity(
&mut asset_market,
Expand Down Expand Up @@ -2359,9 +2353,7 @@ pub fn liquidate_perp_pnl_for_deposit(
let market = perp_market_map.get_ref(&perp_market_index)?;

let quote_spot_market = spot_market_map.get_ref(&market.quote_spot_market_index)?;
let quote_price = oracle_map
.get_price_data(&quote_spot_market.oracle_id())?
.price;
let quote_price = oracle_map.get_price_data(&quote_spot_market.oracle)?.price;

(
unsettled_pnl.unsigned_abs(),
Expand Down Expand Up @@ -2438,7 +2430,7 @@ pub fn liquidate_perp_pnl_for_deposit(

if exiting_liq_territory || is_contract_tier_violation {
let market = perp_market_map.get_ref(&perp_market_index)?;
let market_oracle_price = oracle_map.get_price_data(&market.oracle_id())?.price;
let market_oracle_price = oracle_map.get_price_data(&market.amm.oracle)?.price;

emit!(LiquidationRecord {
ts: now,
Expand Down Expand Up @@ -2641,7 +2633,7 @@ pub fn liquidate_perp_pnl_for_deposit(

let market_oracle_price = {
let market = perp_market_map.get_ref_mut(&perp_market_index)?;
oracle_map.get_price_data(&market.oracle_id())?.price
oracle_map.get_price_data(&market.amm.oracle)?.price
};

emit!(LiquidationRecord {
Expand Down Expand Up @@ -2767,7 +2759,7 @@ pub fn resolve_perp_bankruptcy(

// move if payment to pnl pool
let spot_market = &mut spot_market_map.get_ref_mut(&QUOTE_SPOT_MARKET_INDEX)?;
let oracle_price_data = oracle_map.get_price_data(&spot_market.oracle_id())?;
let oracle_price_data = oracle_map.get_price_data(&spot_market.oracle)?;
update_spot_market_cumulative_interest(spot_market, Some(oracle_price_data), now)?;

update_spot_balances(
Expand Down Expand Up @@ -2988,7 +2980,7 @@ pub fn resolve_spot_bankruptcy(

{
let mut spot_market = spot_market_map.get_ref_mut(&market_index)?;
let oracle_price_data = &oracle_map.get_price_data(&spot_market.oracle_id())?;
let oracle_price_data = &oracle_map.get_price_data(&spot_market.oracle)?;
let quote_social_loss = get_token_value(
-borrow_amount.cast()?,
spot_market.decimals,
Expand Down
18 changes: 4 additions & 14 deletions programs/drift/src/controller/liquidation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,7 @@ pub mod liquidate_perp {
margin_requirement_plus_buffer
);

let oracle_price = oracle_map
.get_price_data(&(oracle_price_key, OracleSource::Pyth))
.unwrap()
.price;
let oracle_price = oracle_map.get_price_data(&oracle_price_key).unwrap().price;

let perp_value = calculate_base_asset_value_with_oracle_price(
user.perp_positions[0].base_asset_amount as i128,
Expand Down Expand Up @@ -2360,10 +2357,7 @@ pub mod liquidate_perp {
margin_requirement_plus_buffer
);

let oracle_price = oracle_map
.get_price_data(&(oracle_price_key, OracleSource::Pyth))
.unwrap()
.price;
let oracle_price = oracle_map.get_price_data(&oracle_price_key).unwrap().price;

let perp_value = calculate_base_asset_value_with_oracle_price(
user.perp_positions[0].base_asset_amount as i128,
Expand Down Expand Up @@ -3680,9 +3674,7 @@ pub mod liquidate_spot {
&user.spot_positions[1].balance_type,
)
.unwrap();
let oracle_price_data = oracle_map
.get_price_data(&(sol_oracle_price_key, OracleSource::Pyth))
.unwrap();
let oracle_price_data = oracle_map.get_price_data(&sol_oracle_price_key).unwrap();
let token_value =
get_token_value(token_amount as i128, 6, oracle_price_data.price).unwrap();

Expand Down Expand Up @@ -4892,9 +4884,7 @@ pub mod liquidate_borrow_for_perp_pnl {
&user.spot_positions[0].balance_type,
)
.unwrap();
let oracle_price_data = oracle_map
.get_price_data(&(sol_oracle_price_key, OracleSource::Pyth))
.unwrap();
let oracle_price_data = oracle_map.get_price_data(&sol_oracle_price_key).unwrap();
let token_value =
get_token_value(token_amount as i128, 6, oracle_price_data.price).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion programs/drift/src/controller/lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub fn remove_perp_lp_shares(
ErrorCode::InsufficientLPTokens
)?;

let oracle_price = oracle_map.get_price_data(&market.oracle_id())?.price;
let oracle_price = oracle_map.get_price_data(&market.amm.oracle)?.price;
let (position_delta, pnl) =
burn_lp_shares(position, &mut market, shares_to_burn, oracle_price)?;

Expand Down
Loading

0 comments on commit 6b5f75d

Please sign in to comment.