Skip to content

Commit

Permalink
program: refactor oracle map to allow same oracle w diff oracle sourc…
Browse files Browse the repository at this point in the history
…es (#1346)

* program: refactor oracle map to allow same oracle w diff oracle sources

* rm some code

* rename get_oracle_id to oracle_id

* start fixing tests

* fix remainder of tests

* add another test

* more red diffs

* fix a compiler warning

* start adding ts changes

* add ws drift client account sub

* add grpc implementation

* prettify:fix

* check in missing OracleSourceNum

* update some missing use of getOracleId

* tweak findDelistedPerpMarketsAndOracles logic

* add bankrun test

* prettify:fix

* fix findDelistedPerpMarketsAndOracles

* update test

* fix linters

* CHANGELOG
  • Loading branch information
crispheaney authored Dec 2, 2024
1 parent a26727c commit d09652a
Show file tree
Hide file tree
Showing 36 changed files with 701 additions and 492 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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.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 @@ -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.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 @@ -340,7 +340,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 @@ -787,7 +789,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 @@ -915,7 +917,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 @@ -1233,7 +1237,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 @@ -1282,7 +1286,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 @@ -1820,7 +1824,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 @@ -1846,7 +1852,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 @@ -1940,7 +1946,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 @@ -2131,7 +2137,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 @@ -2276,7 +2282,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 @@ -2353,7 +2359,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 @@ -2430,7 +2438,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 @@ -2633,7 +2641,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 @@ -2759,7 +2767,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 @@ -2980,7 +2988,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
18 changes: 14 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,10 @@ 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 +2360,10 @@ 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 +3680,9 @@ 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 +4892,9 @@ 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

0 comments on commit d09652a

Please sign in to comment.