Skip to content

Commit

Permalink
program: skip isolated tier for auction start/end sanitize (#958)
Browse files Browse the repository at this point in the history
* program: skip-isolated-tier-for-auction-start-end-override

* update changelog
  • Loading branch information
0xbigz committed Mar 13, 2024
1 parent a0ef1b9 commit 71811c8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Features
- program: skip isolated tier for auction start/end sanitize ([#958](https://github.com/drift-labs/protocol-v2/pull/958))

- program: check isolated perp market in validate spot margin trading ([#957](https://github.com/drift-labs/protocol-v2/pull/957))
- program: improve update prelaunch oracles and add ability to delete ([#956](https://github.com/drift-labs/protocol-v2/pull/956))
Expand Down
64 changes: 33 additions & 31 deletions programs/drift/src/state/order_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,38 +207,40 @@ impl OrderParams {

return Ok(());
}

let new_start_price_offset =
OrderParams::get_perp_baseline_start_price_offset(perp_market, self.direction)?;
match self.direction {
PositionDirection::Long => {
let current_start_price_offset =
self.get_auction_start_price_offset(oracle_price)?;
if current_start_price_offset > new_start_price_offset {
self.auction_start_price = if !is_market_order {
Some(new_start_price_offset)
} else {
Some(new_start_price_offset.safe_add(oracle_price)?)
};
msg!(
"Updating auction start price to {}",
self.auction_start_price.safe_unwrap()?
);
// only update auction start price if the contract tier isn't Isolated
if perp_market.contract_tier != ContractTier::Isolated {
let new_start_price_offset =
OrderParams::get_perp_baseline_start_price_offset(perp_market, self.direction)?;
match self.direction {
PositionDirection::Long => {
let current_start_price_offset =
self.get_auction_start_price_offset(oracle_price)?;
if current_start_price_offset > new_start_price_offset {
self.auction_start_price = if !is_market_order {
Some(new_start_price_offset)
} else {
Some(new_start_price_offset.safe_add(oracle_price)?)
};
msg!(
"Updating auction start price to {}",
self.auction_start_price.safe_unwrap()?
);
}
}
}
PositionDirection::Short => {
let current_start_price_offset =
self.get_auction_start_price_offset(oracle_price)?;
if current_start_price_offset < new_start_price_offset {
self.auction_start_price = if !is_market_order {
Some(new_start_price_offset)
} else {
Some(new_start_price_offset.safe_add(oracle_price)?)
};
msg!(
"Updating auction start price to {}",
self.auction_start_price.safe_unwrap()?
);
PositionDirection::Short => {
let current_start_price_offset =
self.get_auction_start_price_offset(oracle_price)?;
if current_start_price_offset < new_start_price_offset {
self.auction_start_price = if !is_market_order {
Some(new_start_price_offset)
} else {
Some(new_start_price_offset.safe_add(oracle_price)?)
};
msg!(
"Updating auction start price to {}",
self.auction_start_price.safe_unwrap()?
);
}
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions programs/drift/src/state/order_params/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod update_perp_auction_params {
};

#[test]
fn test_limit() {
fn test_sanitize_limit() {
let oracle_price = 100 * PRICE_PRECISION_I64;
let mut amm = AMM {
base_asset_reserve: 100 * AMM_RESERVE_PRECISION,
Expand Down Expand Up @@ -237,7 +237,7 @@ mod update_perp_auction_params {
amm.last_bid_price_twap =
(amm.historical_oracle_data.last_oracle_price_twap as u64) + 17238;

let perp_market = PerpMarket {
let mut perp_market = PerpMarket {
amm,
contract_tier: ContractTier::B,
..PerpMarket::default()
Expand Down Expand Up @@ -280,6 +280,23 @@ mod update_perp_auction_params {
assert_ne!(order_params_before, order_params_after);
assert_eq!(order_params_after.auction_start_price.unwrap(), 99118879);

// skip for isolated tier
perp_market.contract_tier = ContractTier::Isolated;
let mut order_params_after = order_params_before;
order_params_after
.update_perp_auction_params(&perp_market, oracle_price)
.unwrap();
assert_eq!(
order_params_after.auction_start_price,
order_params_before.auction_start_price
);
assert_eq!(
order_params_after.auction_end_price,
order_params_before.auction_end_price
);

perp_market.contract_tier = ContractTier::B; // switch back

let order_params_before = OrderParams {
order_type: OrderType::Market,
direction: PositionDirection::Short,
Expand Down

0 comments on commit 71811c8

Please sign in to comment.