Skip to content
Merged
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
3 changes: 1 addition & 2 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9402,8 +9402,7 @@ export class DriftClient {
confidence: conf,
hasSufficientNumberOfDataPoints: true,
},
stateAccountAndSlot.data.oracleGuardRails,
perpMarket.amm.mmOracleSlot
stateAccountAndSlot.data.oracleGuardRails
) ||
perpMarket.amm.mmOraclePrice.eq(ZERO) ||
isExchangeOracleMoreRecent ||
Expand Down
27 changes: 7 additions & 20 deletions sdk/src/math/oracles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { OraclePriceData } from '../oracles/types';
import {
BID_ASK_SPREAD_PRECISION,
MARGIN_PRECISION,
PRICE_PRECISION,
ONE,
ZERO,
FIVE_MINUTE,
Expand Down Expand Up @@ -97,29 +96,17 @@ export function isOracleValid(
export function isOracleTooDivergent(
amm: AMM,
oraclePriceData: OraclePriceData,
oracleGuardRails: OracleGuardRails,
now: BN
oracleGuardRails: OracleGuardRails
): boolean {
const sinceLastUpdate = now.sub(
amm.historicalOracleData.lastOraclePriceTwapTs
);
const sinceStart = BN.max(ZERO, FIVE_MINUTE.sub(sinceLastUpdate));
const oracleTwap5min = amm.historicalOracleData.lastOraclePriceTwap5Min
.mul(sinceStart)
.add(oraclePriceData.price)
.mul(sinceLastUpdate)
.div(sinceStart.add(sinceLastUpdate));

const oracleSpread = oracleTwap5min.sub(oraclePriceData.price);
const oracleSpreadPct = oracleSpread.mul(PRICE_PRECISION).div(oracleTwap5min);

const oracleSpreadPct = oraclePriceData.price
.sub(amm.historicalOracleData.lastOraclePriceTwap5Min)
.mul(PERCENTAGE_PRECISION)
.div(amm.historicalOracleData.lastOraclePriceTwap5Min);
const maxDivergence = BN.max(
oracleGuardRails.priceDivergence.markOraclePercentDivergence,
PERCENTAGE_PRECISION.div(new BN(10))
oracleGuardRails.priceDivergence.oracleTwap5MinPercentDivergence,
PERCENTAGE_PRECISION.div(new BN(2))
);

const tooDivergent = oracleSpreadPct.abs().gte(maxDivergence);

return tooDivergent;
}

Expand Down
Loading