Skip to content

Commit

Permalink
program: include usdc oracle (#397)
Browse files Browse the repository at this point in the history
* margin changes

* add quote_spot_market_index to perp market

* pass in usdc oracle

* start work on sdk

* more sdk

* example forking oracle data based on slot

* use strict mode for valuing perp position margin requirement and pnl

* some tests for usdc deposit/borrows when not equal to 1

* add test for perps

* fix max lev calculation

* fix imbalancePerpPnl.ts

* fix liquidityProvider.ts

* spot_balance/tests.rs: add usdc twap test case to explain twap != 1

* liquidate_perp account for quote oracle price

* account for quote price in all liquidation functions

* tweak order for writable market overrides for getRemainingAccounts

* stats.rs: better handle twap updates with weight=0

* pythStableCoin

* getOracleClient account for pythStableCoin

* use hardfork based on usdc oracle for devnet too

* CHANGELOG

* update idl

---------

Co-authored-by: 0xbigz <[email protected]>
  • Loading branch information
crispheaney and 0xbigz authored Mar 28, 2023
1 parent c77b446 commit 5c256f2
Show file tree
Hide file tree
Showing 34 changed files with 1,356 additions and 182 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- program: include usdc oracle ([#397](https://github.com/drift-labs/protocol-v2/pull/397))

### Fixes

### Breaking
Expand Down
14 changes: 10 additions & 4 deletions programs/drift/src/controller/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,21 @@ pub fn liquidate_perp(
let market = perp_market_map.get_ref(&market_index)?;
let liquidation_fee = market.liquidator_fee;
let if_liquidation_fee = market.if_liquidation_fee;
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 base_asset_amount_to_cover_margin_shortage = standardize_base_asset_amount_ceil(
calculate_base_asset_amount_to_cover_margin_shortage(
margin_shortage,
margin_ratio_with_buffer,
liquidation_fee,
if_liquidation_fee,
oracle_price,
quote_oracle_price,
)?,
market.amm.order_step_size,
)?;
drop(market);
drop(quote_spot_market);

let max_pct_allowed = calculate_max_pct_to_liquidate(
user,
Expand Down Expand Up @@ -1174,10 +1178,11 @@ pub fn liquidate_borrow_for_perp_pnl(
"Perp position must have position pnl"
)?;

let quote_price = oracle_map.quote_asset_price_data.price;

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 pnl_asset_weight =
market.get_unrealized_asset_weight(pnl, MarginRequirementType::Maintenance)?;

Expand Down Expand Up @@ -1694,10 +1699,11 @@ pub fn liquidate_perp_pnl_for_deposit(
"Perp position must have negative pnl"
)?;

let quote_price = oracle_map.quote_asset_price_data.price;

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;

(
unsettled_pnl.unsigned_abs(),
quote_price,
Expand Down
165 changes: 160 additions & 5 deletions programs/drift/src/controller/liquidation/tests.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2684,7 +2684,7 @@ pub fn place_spot_order(

let oracle_price_data = *oracle_map.get_price_data(&spot_market.oracle)?;
let (worst_case_token_amount_before, _) = user.spot_positions[spot_position_index]
.get_worst_case_token_amounts(spot_market, &oracle_price_data, None, None)?;
.get_worst_case_token_amount(spot_market, &oracle_price_data, None, None)?;

let balance_type = user.spot_positions[spot_position_index].balance_type;
let token_amount = user.spot_positions[spot_position_index].get_token_amount(spot_market)?;
Expand Down Expand Up @@ -2807,7 +2807,7 @@ pub fn place_spot_order(
}

let (worst_case_token_amount_after, _) = user.spot_positions[spot_position_index]
.get_worst_case_token_amounts(spot_market, &oracle_price_data, None, None)?;
.get_worst_case_token_amount(spot_market, &oracle_price_data, None, None)?;

let order_risk_decreasing =
is_spot_order_risk_decreasing(&user.orders[new_order_index], &balance_type, token_amount)?;
Expand Down
12 changes: 12 additions & 0 deletions programs/drift/src/controller/orders/amm_jit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -384,6 +385,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -579,6 +581,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -773,6 +776,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -984,6 +988,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -1187,6 +1192,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -1394,6 +1400,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -1575,6 +1582,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -1772,6 +1780,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -1982,6 +1991,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -2261,6 +2271,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -2524,6 +2535,7 @@ pub mod amm_jit {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down
19 changes: 17 additions & 2 deletions programs/drift/src/controller/orders/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2554,6 +2554,7 @@ pub mod fulfill_order {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -2784,6 +2785,7 @@ pub mod fulfill_order {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -2985,6 +2987,7 @@ pub mod fulfill_order {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -3189,6 +3192,7 @@ pub mod fulfill_order {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -3393,6 +3397,7 @@ pub mod fulfill_order {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -3702,6 +3707,7 @@ pub mod fulfill_order {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -3945,7 +3951,6 @@ pub mod fill_order {

use crate::controller::orders::fill_perp_order;
use crate::controller::position::PositionDirection;
use crate::create_account_info;
use crate::create_anchor_account_info;
use crate::math::constants::{
AMM_RESERVE_PRECISION, BASE_PRECISION_I64, BASE_PRECISION_U64, PEG_PRECISION,
Expand All @@ -3964,6 +3969,7 @@ pub mod fill_order {
use crate::test_utils::{
create_account_info, get_orders, get_positions, get_pyth_price, get_spot_positions,
};
use crate::{create_account_info, QUOTE_PRECISION_I64};

use super::*;
use crate::error::ErrorCode;
Expand Down Expand Up @@ -4049,6 +4055,7 @@ pub mod fill_order {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -4252,6 +4259,7 @@ pub mod fill_order {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -4590,6 +4598,7 @@ pub mod fill_order {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -8047,7 +8056,6 @@ pub mod get_maker_orders_info {

use crate::controller::orders::get_maker_orders_info;
use crate::controller::position::PositionDirection;
use crate::create_anchor_account_info;
use crate::math::constants::{
AMM_RESERVE_PRECISION, BASE_PRECISION_I64, BASE_PRECISION_U64, PEG_PRECISION,
PRICE_PRECISION_I64, PRICE_PRECISION_U64, SPOT_BALANCE_PRECISION_U64,
Expand All @@ -8066,6 +8074,7 @@ pub mod get_maker_orders_info {
create_account_info, get_orders, get_positions, get_pyth_price, get_spot_positions,
};
use crate::{create_account_info, get_orders};
use crate::{create_anchor_account_info, QUOTE_PRECISION_I64};

use super::*;

Expand Down Expand Up @@ -8150,6 +8159,7 @@ pub mod get_maker_orders_info {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -8343,6 +8353,7 @@ pub mod get_maker_orders_info {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -8537,6 +8548,7 @@ pub mod get_maker_orders_info {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -8717,6 +8729,7 @@ pub mod get_maker_orders_info {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -8971,6 +8984,7 @@ pub mod get_maker_orders_info {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down Expand Up @@ -9167,6 +9181,7 @@ pub mod get_maker_orders_info {
decimals: 6,
initial_asset_weight: SPOT_WEIGHT_PRECISION,
maintenance_asset_weight: SPOT_WEIGHT_PRECISION,
historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64),
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);
Expand Down
Loading

0 comments on commit 5c256f2

Please sign in to comment.