Skip to content

Commit

Permalink
program: longer auction duration for safe contract tiers
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Feb 16, 2024
1 parent 597fad3 commit cd6bc6b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
18 changes: 16 additions & 2 deletions programs/drift/src/state/order_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl OrderParams {
.safe_sub(self.auction_start_price.safe_unwrap()?)?
.unsigned_abs(),
oracle_price.unsigned_abs(),
perp_market.contract_tier,

Check warning on line 133 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L133

Added line #L133 was not covered by tests
)?;
self.auction_duration = Some(
auction_duration_before
Expand Down Expand Up @@ -255,6 +256,7 @@ impl OrderParams {
.safe_sub(self.auction_start_price.safe_unwrap()?)?
.unsigned_abs(),
oracle_price.unsigned_abs(),
perp_market.contract_tier,

Check warning on line 259 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L259

Added line #L259 was not covered by tests
)?;
self.auction_duration = Some(
auction_duration_before
Expand Down Expand Up @@ -305,6 +307,7 @@ impl OrderParams {
.safe_sub(auction_start_price)?
.unsigned_abs(),
oracle_price.unsigned_abs(),
perp_market.contract_tier,

Check warning on line 310 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L310

Added line #L310 was not covered by tests
)?;

Ok((auction_start_price, auction_end_price, auction_duration))
Expand Down Expand Up @@ -341,6 +344,7 @@ impl OrderParams {
.safe_sub(auction_start_price)?
.unsigned_abs(),
oracle_price.unsigned_abs(),
perp_market.contract_tier,

Check warning on line 347 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L347

Added line #L347 was not covered by tests
)?;

Ok((auction_start_price, auction_end_price, auction_duration))
Expand Down Expand Up @@ -517,11 +521,21 @@ impl OrderParams {
}
}

fn get_auction_duration(price_diff: u64, price: u64) -> DriftResult<u8> {
fn get_auction_duration(
price_diff: u64,
price: u64,
contract_tier: ContractTier,
) -> DriftResult<u8> {
let percent_diff = price_diff.safe_mul(PERCENTAGE_PRECISION_U64)?.div(price);

let slots_per_bp = if contract_tier.is_as_safe_as_contract(&ContractTier::B) {
100
} else {
60
};

Ok(percent_diff
.safe_mul(60)?
.safe_mul(slots_per_bp)?
.safe_div_ceil(PERCENTAGE_PRECISION_U64 / 100)? // 1% = 60 slots
.clamp(10, 180) as u8) // 180 slots max
}
Expand Down
15 changes: 8 additions & 7 deletions programs/drift/src/state/order_params/tests.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
mod get_auction_duration {
use crate::state::order_params::get_auction_duration;
use crate::PRICE_PRECISION_U64;
use crate::{ContractTier, PRICE_PRECISION_U64};

#[test]
fn test() {
let price_diff = 0;
let price = 100 * PRICE_PRECISION_U64;
let contract_tier = ContractTier::C;

let duration = get_auction_duration(price_diff, price).unwrap();
let duration = get_auction_duration(price_diff, price, contract_tier).unwrap();
assert_eq!(duration, 10);

let price_diff = PRICE_PRECISION_U64 / 10;
let price = 100 * PRICE_PRECISION_U64;

let duration = get_auction_duration(price_diff, price).unwrap();
let duration = get_auction_duration(price_diff, price, contract_tier).unwrap();
assert_eq!(duration, 10);

let price_diff = PRICE_PRECISION_U64 / 2;
let price = 100 * PRICE_PRECISION_U64;

let duration = get_auction_duration(price_diff, price).unwrap();
let duration = get_auction_duration(price_diff, price, contract_tier).unwrap();
assert_eq!(duration, 30);

let price_diff = PRICE_PRECISION_U64;
let price = 100 * PRICE_PRECISION_U64;

let duration = get_auction_duration(price_diff, price).unwrap();
let duration = get_auction_duration(price_diff, price, contract_tier).unwrap();
assert_eq!(duration, 60);

let price_diff = 2 * PRICE_PRECISION_U64;
let price = 100 * PRICE_PRECISION_U64;

let duration = get_auction_duration(price_diff, price).unwrap();
let duration = get_auction_duration(price_diff, price, contract_tier).unwrap();
assert_eq!(duration, 120);
}
}
Expand Down Expand Up @@ -298,7 +299,7 @@ mod update_perp_auction_params {
);
assert_eq!(order_params_before.direction, order_params_after.direction);

assert_eq!(order_params_after.auction_duration, Some(61));
assert_eq!(order_params_after.auction_duration, Some(102));
}

#[test]
Expand Down

0 comments on commit cd6bc6b

Please sign in to comment.