@drift-labs/sdk 2.71.0-beta.4 → 2.71.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.71.0-beta.4
1
+ 2.71.0-beta.5
@@ -3,7 +3,8 @@ import { AMM, OracleGuardRails } from '../types';
3
3
  import { OraclePriceData } from '../oracles/types';
4
4
  import { BN, HistoricalOracleData, PerpMarketAccount } from '../index';
5
5
  export declare function oraclePriceBands(market: PerpMarketAccount, oraclePriceData: OraclePriceData): [BN, BN];
6
- export declare function isOracleValid(amm: AMM, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, slot: number): boolean;
6
+ export declare function getMaxConfidenceIntervalMultiplier(market: PerpMarketAccount): BN;
7
+ export declare function isOracleValid(market: PerpMarketAccount, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, slot: number): boolean;
7
8
  export declare function isOracleTooDivergent(amm: AMM, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, now: BN): boolean;
8
9
  export declare function calculateLiveOracleTwap(histOracleData: HistoricalOracleData, oraclePriceData: OraclePriceData, now: BN, period: BN): BN;
9
10
  export declare function calculateLiveOracleStd(amm: AMM, oraclePriceData: OraclePriceData, now: BN): BN;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.oraclePriceBands = void 0;
3
+ exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.getMaxConfidenceIntervalMultiplier = exports.oraclePriceBands = void 0;
4
+ const types_1 = require("../types");
4
5
  const numericConstants_1 = require("../constants/numericConstants");
5
6
  const index_1 = require("../index");
6
7
  const assert_1 = require("../assert/assert");
@@ -13,7 +14,29 @@ function oraclePriceBands(market, oraclePriceData) {
13
14
  return [oraclePriceData.price.sub(offset), oraclePriceData.price.add(offset)];
14
15
  }
15
16
  exports.oraclePriceBands = oraclePriceBands;
16
- function isOracleValid(amm, oraclePriceData, oracleGuardRails, slot) {
17
+ function getMaxConfidenceIntervalMultiplier(market) {
18
+ let maxConfidenceIntervalMultiplier;
19
+ if ((0, types_1.isVariant)(market.contractTier, 'a')) {
20
+ maxConfidenceIntervalMultiplier = new index_1.BN(1);
21
+ }
22
+ else if ((0, types_1.isVariant)(market.contractTier, 'b')) {
23
+ maxConfidenceIntervalMultiplier = new index_1.BN(1);
24
+ }
25
+ else if ((0, types_1.isVariant)(market.contractTier, 'c')) {
26
+ maxConfidenceIntervalMultiplier = new index_1.BN(2);
27
+ }
28
+ else if ((0, types_1.isVariant)(market.contractTier, 'speculative')) {
29
+ maxConfidenceIntervalMultiplier = new index_1.BN(10);
30
+ }
31
+ else if ((0, types_1.isVariant)(market.contractTier, 'isolated')) {
32
+ maxConfidenceIntervalMultiplier = new index_1.BN(50);
33
+ }
34
+ return maxConfidenceIntervalMultiplier;
35
+ }
36
+ exports.getMaxConfidenceIntervalMultiplier = getMaxConfidenceIntervalMultiplier;
37
+ function isOracleValid(market, oraclePriceData, oracleGuardRails, slot) {
38
+ // checks if oracle is valid for an AMM only fill
39
+ const amm = market.amm;
17
40
  const isOraclePriceNonPositive = oraclePriceData.price.lte(numericConstants_1.ZERO);
18
41
  const isOraclePriceTooVolatile = oraclePriceData.price
19
42
  .div(index_1.BN.max(numericConstants_1.ONE, amm.historicalOracleData.lastOraclePriceTwap))
@@ -21,10 +44,11 @@ function isOracleValid(amm, oraclePriceData, oracleGuardRails, slot) {
21
44
  amm.historicalOracleData.lastOraclePriceTwap
22
45
  .div(index_1.BN.max(numericConstants_1.ONE, oraclePriceData.price))
23
46
  .gt(oracleGuardRails.validity.tooVolatileRatio);
47
+ const maxConfidenceIntervalMultiplier = getMaxConfidenceIntervalMultiplier(market);
24
48
  const isConfidenceTooLarge = index_1.BN.max(numericConstants_1.ONE, oraclePriceData.confidence)
25
49
  .mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
26
50
  .div(oraclePriceData.price)
27
- .gt(oracleGuardRails.validity.confidenceIntervalMaxSize);
51
+ .gt(oracleGuardRails.validity.confidenceIntervalMaxSize.mul(maxConfidenceIntervalMultiplier));
28
52
  const oracleIsStale = new index_1.BN(slot)
29
53
  .sub(oraclePriceData.slot)
30
54
  .gt(oracleGuardRails.validity.slotsBeforeStaleForAmm);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.71.0-beta.4",
3
+ "version": "2.71.0-beta.5",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -1,4 +1,4 @@
1
- import { AMM, OracleGuardRails } from '../types';
1
+ import { AMM, OracleGuardRails, isVariant } from '../types';
2
2
  import { OraclePriceData } from '../oracles/types';
3
3
  import {
4
4
  BID_ASK_SPREAD_PRECISION,
@@ -27,12 +27,33 @@ export function oraclePriceBands(
27
27
  return [oraclePriceData.price.sub(offset), oraclePriceData.price.add(offset)];
28
28
  }
29
29
 
30
+ export function getMaxConfidenceIntervalMultiplier(
31
+ market: PerpMarketAccount
32
+ ): BN {
33
+ let maxConfidenceIntervalMultiplier;
34
+ if (isVariant(market.contractTier, 'a')) {
35
+ maxConfidenceIntervalMultiplier = new BN(1);
36
+ } else if (isVariant(market.contractTier, 'b')) {
37
+ maxConfidenceIntervalMultiplier = new BN(1);
38
+ } else if (isVariant(market.contractTier, 'c')) {
39
+ maxConfidenceIntervalMultiplier = new BN(2);
40
+ } else if (isVariant(market.contractTier, 'speculative')) {
41
+ maxConfidenceIntervalMultiplier = new BN(10);
42
+ } else if (isVariant(market.contractTier, 'isolated')) {
43
+ maxConfidenceIntervalMultiplier = new BN(50);
44
+ }
45
+ return maxConfidenceIntervalMultiplier;
46
+ }
47
+
30
48
  export function isOracleValid(
31
- amm: AMM,
49
+ market: PerpMarketAccount,
32
50
  oraclePriceData: OraclePriceData,
33
51
  oracleGuardRails: OracleGuardRails,
34
52
  slot: number
35
53
  ): boolean {
54
+ // checks if oracle is valid for an AMM only fill
55
+
56
+ const amm = market.amm;
36
57
  const isOraclePriceNonPositive = oraclePriceData.price.lte(ZERO);
37
58
  const isOraclePriceTooVolatile =
38
59
  oraclePriceData.price
@@ -42,10 +63,16 @@ export function isOracleValid(
42
63
  .div(BN.max(ONE, oraclePriceData.price))
43
64
  .gt(oracleGuardRails.validity.tooVolatileRatio);
44
65
 
66
+ const maxConfidenceIntervalMultiplier =
67
+ getMaxConfidenceIntervalMultiplier(market);
45
68
  const isConfidenceTooLarge = BN.max(ONE, oraclePriceData.confidence)
46
69
  .mul(BID_ASK_SPREAD_PRECISION)
47
70
  .div(oraclePriceData.price)
48
- .gt(oracleGuardRails.validity.confidenceIntervalMaxSize);
71
+ .gt(
72
+ oracleGuardRails.validity.confidenceIntervalMaxSize.mul(
73
+ maxConfidenceIntervalMultiplier
74
+ )
75
+ );
49
76
 
50
77
  const oracleIsStale = new BN(slot)
51
78
  .sub(oraclePriceData.slot)
package/tests/amm/test.ts CHANGED
@@ -1193,12 +1193,12 @@ describe('AMM Tests', () => {
1193
1193
  };
1194
1194
 
1195
1195
  // good oracle
1196
- assert(isOracleValid(mockAmm, oraclePriceData, oracleGuardRails, slot + 5));
1196
+ assert(isOracleValid(mockMarket1, oraclePriceData, oracleGuardRails, slot + 5));
1197
1197
 
1198
1198
  // conf too high
1199
1199
  assert(
1200
1200
  !isOracleValid(
1201
- mockAmm,
1201
+ mockMarket1,
1202
1202
  {
1203
1203
  price: new BN(13.553 * PRICE_PRECISION.toNumber()),
1204
1204
  slot: new BN(slot),
@@ -1213,7 +1213,7 @@ describe('AMM Tests', () => {
1213
1213
  // not hasSufficientNumberOfDataPoints
1214
1214
  assert(
1215
1215
  !isOracleValid(
1216
- mockAmm,
1216
+ mockMarket1,
1217
1217
  {
1218
1218
  price: new BN(13.553 * PRICE_PRECISION.toNumber()),
1219
1219
  slot: new BN(slot),
@@ -1228,7 +1228,7 @@ describe('AMM Tests', () => {
1228
1228
  // negative oracle price
1229
1229
  assert(
1230
1230
  !isOracleValid(
1231
- mockAmm,
1231
+ mockMarket1,
1232
1232
  {
1233
1233
  price: new BN(-1 * PRICE_PRECISION.toNumber()),
1234
1234
  slot: new BN(slot),
@@ -1243,7 +1243,7 @@ describe('AMM Tests', () => {
1243
1243
  // too delayed for amm
1244
1244
  assert(
1245
1245
  !isOracleValid(
1246
- mockAmm,
1246
+ mockMarket1,
1247
1247
  {
1248
1248
  price: new BN(13.553 * PRICE_PRECISION.toNumber()),
1249
1249
  slot: new BN(slot),
@@ -1258,7 +1258,7 @@ describe('AMM Tests', () => {
1258
1258
  // im passing stale slot (should not call oracle invalid)
1259
1259
  assert(
1260
1260
  isOracleValid(
1261
- mockAmm,
1261
+ mockMarket1,
1262
1262
  {
1263
1263
  price: new BN(13.553 * PRICE_PRECISION.toNumber()),
1264
1264
  slot: new BN(slot + 100),
@@ -1273,7 +1273,7 @@ describe('AMM Tests', () => {
1273
1273
  // too volatile (more than 5x higher)
1274
1274
  assert(
1275
1275
  !isOracleValid(
1276
- mockAmm,
1276
+ mockMarket1,
1277
1277
  {
1278
1278
  price: new BN(113.553 * PRICE_PRECISION.toNumber()),
1279
1279
  slot: new BN(slot + 5),
@@ -1288,7 +1288,7 @@ describe('AMM Tests', () => {
1288
1288
  // too volatile (more than 1/5 lower)
1289
1289
  assert(
1290
1290
  !isOracleValid(
1291
- mockAmm,
1291
+ mockMarket1,
1292
1292
  {
1293
1293
  price: new BN(0.553 * PRICE_PRECISION.toNumber()),
1294
1294
  slot: new BN(slot + 5),