Skip to content

Commit 3b8cc48

Browse files
moosecat2harsh4786
authored andcommitted
is oracle divergent sdk fix (drift-labs#1978)
* is oracle divergent sdk fix * remove console log * linter fix * patch
1 parent a6e7125 commit 3b8cc48

File tree

2 files changed

+8
-22
lines changed

2 files changed

+8
-22
lines changed

sdk/src/driftClient.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9402,8 +9402,7 @@ export class DriftClient {
94029402
confidence: conf,
94039403
hasSufficientNumberOfDataPoints: true,
94049404
},
9405-
stateAccountAndSlot.data.oracleGuardRails,
9406-
perpMarket.amm.mmOracleSlot
9405+
stateAccountAndSlot.data.oracleGuardRails
94079406
) ||
94089407
perpMarket.amm.mmOraclePrice.eq(ZERO) ||
94099408
isExchangeOracleMoreRecent ||

sdk/src/math/oracles.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { OraclePriceData } from '../oracles/types';
1010
import {
1111
BID_ASK_SPREAD_PRECISION,
1212
MARGIN_PRECISION,
13-
PRICE_PRECISION,
1413
ONE,
1514
ZERO,
1615
FIVE_MINUTE,
@@ -97,29 +96,17 @@ export function isOracleValid(
9796
export function isOracleTooDivergent(
9897
amm: AMM,
9998
oraclePriceData: OraclePriceData,
100-
oracleGuardRails: OracleGuardRails,
101-
now: BN
99+
oracleGuardRails: OracleGuardRails
102100
): boolean {
103-
const sinceLastUpdate = now.sub(
104-
amm.historicalOracleData.lastOraclePriceTwapTs
105-
);
106-
const sinceStart = BN.max(ZERO, FIVE_MINUTE.sub(sinceLastUpdate));
107-
const oracleTwap5min = amm.historicalOracleData.lastOraclePriceTwap5Min
108-
.mul(sinceStart)
109-
.add(oraclePriceData.price)
110-
.mul(sinceLastUpdate)
111-
.div(sinceStart.add(sinceLastUpdate));
112-
113-
const oracleSpread = oracleTwap5min.sub(oraclePriceData.price);
114-
const oracleSpreadPct = oracleSpread.mul(PRICE_PRECISION).div(oracleTwap5min);
115-
101+
const oracleSpreadPct = oraclePriceData.price
102+
.sub(amm.historicalOracleData.lastOraclePriceTwap5Min)
103+
.mul(PERCENTAGE_PRECISION)
104+
.div(amm.historicalOracleData.lastOraclePriceTwap5Min);
116105
const maxDivergence = BN.max(
117-
oracleGuardRails.priceDivergence.markOraclePercentDivergence,
118-
PERCENTAGE_PRECISION.div(new BN(10))
106+
oracleGuardRails.priceDivergence.oracleTwap5MinPercentDivergence,
107+
PERCENTAGE_PRECISION.div(new BN(2))
119108
);
120-
121109
const tooDivergent = oracleSpreadPct.abs().gte(maxDivergence);
122-
123110
return tooDivergent;
124111
}
125112

0 commit comments

Comments
 (0)