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: refactor oracle map to allow same oracle w diff oracle sources #1346

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
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.amm.oracle)?,
oracle_map.get_price_data(&market.oracle_id())?,
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.amm.oracle)?;
let oracle_price_data = oracle_map.get_price_data(&market.oracle_id())?;
let sanitize_clamp_denominator = market.get_sanitize_clamp_denominator()?;

let oracle_price_twap = amm::update_oracle_price_twap(
Expand Down
40 changes: 24 additions & 16 deletions programs/drift/src/controller/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,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.amm.oracle)?;
let oracle_price_data = oracle_map.get_price_data(&market.oracle_id())?;

update_amm_and_check_validity(
&mut market,
Expand Down Expand Up @@ -333,7 +333,9 @@ 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)?.price;
let quote_oracle_price = oracle_map
.get_price_data(&quote_spot_market.oracle_id())?
.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 @@ -773,7 +775,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.amm.oracle)?;
let oracle_price_data = oracle_map.get_price_data(&market.oracle_id())?;

update_amm_and_check_validity(
&mut market,
Expand Down Expand Up @@ -901,7 +903,9 @@ 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)?.price;
let quote_oracle_price = oracle_map
.get_price_data(&quote_spot_market.oracle_id())?
.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 @@ -1203,7 +1207,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)?;
oracle_map.get_price_data_and_guard_rails(&asset_market.oracle_id())?;

update_spot_market_and_check_validity(
&mut asset_market,
Expand Down Expand Up @@ -1252,7 +1256,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)?;
oracle_map.get_price_data_and_guard_rails(&liability_market.oracle_id())?;

update_spot_market_and_check_validity(
&mut liability_market,
Expand Down Expand Up @@ -1783,7 +1787,9 @@ 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)?.price;
let quote_price = oracle_map
.get_price_data(&quote_spot_market.oracle_id())?
.price;

let pnl_asset_weight =
market.get_unrealized_asset_weight(pnl, MarginRequirementType::Maintenance)?;
Expand All @@ -1809,7 +1815,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)?;
oracle_map.get_price_data_and_guard_rails(&liability_market.oracle_id())?;

update_spot_market_and_check_validity(
&mut liability_market,
Expand Down Expand Up @@ -1903,7 +1909,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.amm.oracle)?.price;
let market_oracle_price = oracle_map.get_price_data(&market.oracle_id())?.price;

emit!(LiquidationRecord {
ts: now,
Expand Down Expand Up @@ -2094,7 +2100,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.amm.oracle)?.price
oracle_map.get_price_data(&market.oracle_id())?.price
};

emit!(LiquidationRecord {
Expand Down Expand Up @@ -2232,7 +2238,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)?;
oracle_map.get_price_data_and_guard_rails(&asset_market.oracle_id())?;

update_spot_market_and_check_validity(
&mut asset_market,
Expand Down Expand Up @@ -2309,7 +2315,9 @@ 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)?.price;
let quote_price = oracle_map
.get_price_data(&quote_spot_market.oracle_id())?
.price;

(
unsettled_pnl.unsigned_abs(),
Expand Down Expand Up @@ -2386,7 +2394,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.amm.oracle)?.price;
let market_oracle_price = oracle_map.get_price_data(&market.oracle_id())?.price;

emit!(LiquidationRecord {
ts: now,
Expand Down Expand Up @@ -2589,7 +2597,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.amm.oracle)?.price
oracle_map.get_price_data(&market.oracle_id())?.price
};

emit!(LiquidationRecord {
Expand Down Expand Up @@ -2715,7 +2723,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)?;
let oracle_price_data = oracle_map.get_price_data(&spot_market.oracle_id())?;
update_spot_market_cumulative_interest(spot_market, Some(oracle_price_data), now)?;

update_spot_balances(
Expand Down Expand Up @@ -2936,7 +2944,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)?;
let oracle_price_data = &oracle_map.get_price_data(&spot_market.oracle_id())?;
let quote_social_loss = get_token_value(
-borrow_amount.cast()?,
spot_market.decimals,
Expand Down
8 changes: 4 additions & 4 deletions programs/drift/src/controller/liquidation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ pub mod liquidate_perp {
margin_requirement_plus_buffer
);

let oracle_price = oracle_map.get_price_data(&oracle_price_key).unwrap().price;
let oracle_price = oracle_map.get_price_data(&(oracle_price_key, OracleSource::Pyth)).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 @@ -2357,7 +2357,7 @@ pub mod liquidate_perp {
margin_requirement_plus_buffer
);

let oracle_price = oracle_map.get_price_data(&oracle_price_key).unwrap().price;
let oracle_price = oracle_map.get_price_data(&(oracle_price_key, OracleSource::Pyth)).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 @@ -3674,7 +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).unwrap();
let oracle_price_data = oracle_map.get_price_data(&(sol_oracle_price_key, OracleSource::Pyth)).unwrap();
let token_value =
get_token_value(token_amount as i128, 6, oracle_price_data.price).unwrap();

Expand Down Expand Up @@ -4884,7 +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).unwrap();
let oracle_price_data = oracle_map.get_price_data(&(sol_oracle_price_key, OracleSource::Pyth)).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.amm.oracle)?.price;
let oracle_price = oracle_map.get_price_data(&market.oracle_id())?.price;
let (position_delta, pnl) =
burn_lp_shares(position, &mut market, shares_to_burn, oracle_price)?;

Expand Down
Loading
Loading