Skip to content

add reference offset test example #1683

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

Open
wants to merge 4 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
9 changes: 5 additions & 4 deletions programs/drift/src/controller/position/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,9 @@ fn amm_perp_ref_offset() {

let reserve_price = perp_market.amm.reserve_price().unwrap();
let (b1, a1) = perp_market.amm.bid_ask_price(reserve_price).unwrap();
assert_eq!(b1, 7098048);
assert_eq!(a1, 7105178);
assert_eq!(reserve_price, 7101599);
assert_eq!(b1, 7225876);
assert_eq!(a1, 7233006);
assert_eq!(
perp_market.amm.historical_oracle_data.last_oracle_price,
7101600
Expand Down Expand Up @@ -690,8 +691,8 @@ fn amm_perp_ref_offset() {

let r = perp_market.amm.reserve_price().unwrap();
let (b, a) = perp_market.amm.bid_ask_price(r).unwrap();
assert_eq!(b, 7098048);
assert_eq!(a, 7105178);
assert_eq!(b, 7417620);
assert_eq!(a, 7424750);
assert_eq!(
perp_market.amm.historical_oracle_data.last_oracle_price,
7101600
Expand Down
29 changes: 22 additions & 7 deletions programs/drift/src/state/perp_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ use crate::math::constants::{
};
use crate::math::constants::{
AMM_RESERVE_PRECISION_I128, AMM_TO_QUOTE_PRECISION_RATIO, BID_ASK_SPREAD_PRECISION,
BID_ASK_SPREAD_PRECISION_U128, DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT,
LIQUIDATION_FEE_PRECISION, LIQUIDATION_FEE_TO_MARGIN_PRECISION_RATIO, LP_FEE_SLICE_DENOMINATOR,
LP_FEE_SLICE_NUMERATOR, MARGIN_PRECISION, MARGIN_PRECISION_U128, MAX_LIQUIDATION_MULTIPLIER,
PEG_PRECISION, PERCENTAGE_PRECISION, PERCENTAGE_PRECISION_I128, PERCENTAGE_PRECISION_I64,
BID_ASK_SPREAD_PRECISION_I128, BID_ASK_SPREAD_PRECISION_U128,
DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT, LIQUIDATION_FEE_PRECISION,
LIQUIDATION_FEE_TO_MARGIN_PRECISION_RATIO, LP_FEE_SLICE_DENOMINATOR, LP_FEE_SLICE_NUMERATOR,
MARGIN_PRECISION, MARGIN_PRECISION_U128, MAX_LIQUIDATION_MULTIPLIER, PEG_PRECISION,
PERCENTAGE_PRECISION, PERCENTAGE_PRECISION_I128, PERCENTAGE_PRECISION_I64,
PERCENTAGE_PRECISION_U64, PRICE_PRECISION, SPOT_WEIGHT_PRECISION, TWENTY_FOUR_HOUR,
};
use crate::math::helpers::get_proportion_i128;
Expand Down Expand Up @@ -1410,19 +1411,33 @@ impl AMM {
}

pub fn bid_price(&self, reserve_price: u64) -> DriftResult<u64> {
let adjusted_spread = (-(self
.short_spread
.cast::<i32>()?))
.safe_add(self.reference_price_offset)?;

let multiplier = BID_ASK_SPREAD_PRECISION_I128.safe_add(adjusted_spread.cast::<i128>()?)?;

reserve_price
.cast::<u128>()?
.safe_mul(BID_ASK_SPREAD_PRECISION_U128.safe_sub(self.short_spread.cast()?)?)?
.safe_mul(multiplier.cast::<u128>()?)?
.safe_div(BID_ASK_SPREAD_PRECISION_U128)?
.cast()
}

pub fn ask_price(&self, reserve_price: u64) -> DriftResult<u64> {
let adjusted_spread = self
.long_spread
.cast::<i32>()?
.safe_add(self.reference_price_offset)?;

let multiplier = BID_ASK_SPREAD_PRECISION_I128.safe_add(adjusted_spread.cast::<i128>()?)?;

reserve_price
.cast::<u128>()?
.safe_mul(BID_ASK_SPREAD_PRECISION_U128.safe_add(self.long_spread.cast()?)?)?
.safe_mul(multiplier.cast::<u128>()?)?
.safe_div(BID_ASK_SPREAD_PRECISION_U128)?
.cast::<u64>()
.cast()
}

pub fn bid_ask_price(&self, reserve_price: u64) -> DriftResult<(u64, u64)> {
Expand Down
9 changes: 8 additions & 1 deletion sdk/src/bankrun/bankrunConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
}

async moveTimeForward(increment: number): Promise<void> {

const approxSlots = increment / 0.4;
const slot = await this.connection.getSlot();
console.log(`warping to slot ${slot} -> ${BigInt(slot) + BigInt(approxSlots)}`)

Check failure on line 127 in sdk/src/bankrun/bankrunConnection.ts

View workflow job for this annotation

GitHub Actions / yarn-lint

Missing semicolon
this.context.warpToSlot(BigInt(Number(slot) + approxSlots));

const currentClock = await this.context.banksClient.getClock();
const newUnixTimestamp = currentClock.unixTimestamp + BigInt(increment);
const newClock = new Clock(
Expand All @@ -130,6 +136,7 @@
currentClock.leaderScheduleEpoch,
newUnixTimestamp
);

await this.context.setClock(newClock);
}

Expand Down Expand Up @@ -290,7 +297,7 @@
return signature;
}

private async updateSlotAndClock() {
async updateSlotAndClock() {
const currentSlot = await this.getSlot();
const nextSlot = currentSlot + BigInt(1);
this.context.warpToSlot(nextSlot);
Expand Down
2 changes: 1 addition & 1 deletion test-scripts/single-anchor-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fi

export ANCHOR_WALLET=~/.config/solana/id.json

test_files=(overwritePerpAccounts.ts)
test_files=(referencePriceOffset.ts)

for test_file in ${test_files[@]}; do
ts-mocha -t 300000 ./tests/${test_file}
Expand Down
Loading
Loading