@drift-labs/sdk 2.59.0-beta.6 → 2.60.0-beta.1

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.59.0-beta.6
1
+ 2.60.0-beta.1
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.58.0",
2
+ "version": "2.59.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/lib/math/amm.js CHANGED
@@ -425,16 +425,13 @@ function calculateSpread(amm, oraclePriceData, now, reservePrice) {
425
425
  reservePrice = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
426
426
  }
427
427
  const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) || reservePrice;
428
- const confInterval = oraclePriceData.confidence || numericConstants_1.ZERO;
429
428
  const targetMarkSpreadPct = reservePrice
430
429
  .sub(targetPrice)
431
430
  .mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
432
431
  .div(reservePrice);
433
- const confIntervalPct = confInterval
434
- .mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
435
- .div(reservePrice);
436
432
  now = now || new anchor_1.BN(new Date().getTime() / 1000); //todo
437
433
  const liveOracleStd = (0, oracles_1.calculateLiveOracleStd)(amm, oraclePriceData, now);
434
+ const confIntervalPct = (0, oracles_1.getNewOracleConfPct)(amm, oraclePriceData, reservePrice, now);
438
435
  const spreads = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.baseAssetAmountWithAmm, reservePrice, amm.totalFeeMinusDistributions, amm.netRevenueSinceLastFunding, amm.baseAssetReserve, amm.minBaseAssetReserve, amm.maxBaseAssetReserve, amm.markStd, liveOracleStd, amm.longIntensityVolume, amm.shortIntensityVolume, amm.volume24H);
439
436
  const longSpread = spreads[0];
440
437
  const shortSpread = spreads[1];
@@ -7,3 +7,4 @@ export declare function isOracleValid(amm: AMM, oraclePriceData: OraclePriceData
7
7
  export declare function isOracleTooDivergent(amm: AMM, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, now: BN): boolean;
8
8
  export declare function calculateLiveOracleTwap(histOracleData: HistoricalOracleData, oraclePriceData: OraclePriceData, now: BN, period: BN): BN;
9
9
  export declare function calculateLiveOracleStd(amm: AMM, oraclePriceData: OraclePriceData, now: BN): BN;
10
+ export declare function getNewOracleConfPct(amm: AMM, oraclePriceData: OraclePriceData, reservePrice: BN, now: BN): BN;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.oraclePriceBands = void 0;
3
+ exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.oraclePriceBands = void 0;
4
4
  const numericConstants_1 = require("../constants/numericConstants");
5
5
  const index_1 = require("../index");
6
6
  const assert_1 = require("../assert/assert");
@@ -80,3 +80,18 @@ function calculateLiveOracleStd(amm, oraclePriceData, now) {
80
80
  return oracleStd;
81
81
  }
82
82
  exports.calculateLiveOracleStd = calculateLiveOracleStd;
83
+ function getNewOracleConfPct(amm, oraclePriceData, reservePrice, now) {
84
+ const confInterval = oraclePriceData.confidence || numericConstants_1.ZERO;
85
+ const sinceLastUpdate = index_1.BN.max(numericConstants_1.ZERO, now.sub(amm.historicalOracleData.lastOraclePriceTwapTs));
86
+ let lowerBoundConfPct = amm.lastOracleConfPct;
87
+ if (sinceLastUpdate.gt(numericConstants_1.ZERO)) {
88
+ const lowerBoundConfDivisor = index_1.BN.max(new index_1.BN(21).sub(sinceLastUpdate), new index_1.BN(5));
89
+ lowerBoundConfPct = amm.lastOracleConfPct.sub(amm.lastOracleConfPct.div(lowerBoundConfDivisor));
90
+ }
91
+ const confIntervalPct = confInterval
92
+ .mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
93
+ .div(reservePrice);
94
+ const confIntervalPctResult = index_1.BN.max(confIntervalPct, lowerBoundConfPct);
95
+ return confIntervalPctResult;
96
+ }
97
+ exports.getNewOracleConfPct = getNewOracleConfPct;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.59.0-beta.6",
3
+ "version": "2.60.0-beta.1",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.58.0",
2
+ "version": "2.59.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/src/math/amm.ts CHANGED
@@ -32,7 +32,7 @@ import {
32
32
  calculateBudgetedPeg,
33
33
  } from './repeg';
34
34
 
35
- import { calculateLiveOracleStd } from './oracles';
35
+ import { calculateLiveOracleStd, getNewOracleConfPct } from './oracles';
36
36
 
37
37
  export function calculatePegFromTargetPrice(
38
38
  targetPrice: BN,
@@ -792,18 +792,19 @@ export function calculateSpread(
792
792
  }
793
793
 
794
794
  const targetPrice = oraclePriceData?.price || reservePrice;
795
- const confInterval = oraclePriceData.confidence || ZERO;
796
795
  const targetMarkSpreadPct = reservePrice
797
796
  .sub(targetPrice)
798
797
  .mul(BID_ASK_SPREAD_PRECISION)
799
798
  .div(reservePrice);
800
799
 
801
- const confIntervalPct = confInterval
802
- .mul(BID_ASK_SPREAD_PRECISION)
803
- .div(reservePrice);
804
-
805
800
  now = now || new BN(new Date().getTime() / 1000); //todo
806
801
  const liveOracleStd = calculateLiveOracleStd(amm, oraclePriceData, now);
802
+ const confIntervalPct = getNewOracleConfPct(
803
+ amm,
804
+ oraclePriceData,
805
+ reservePrice,
806
+ now
807
+ );
807
808
 
808
809
  const spreads = calculateSpreadBN(
809
810
  amm.baseSpread,
@@ -151,3 +151,34 @@ export function calculateLiveOracleStd(
151
151
 
152
152
  return oracleStd;
153
153
  }
154
+
155
+ export function getNewOracleConfPct(
156
+ amm: AMM,
157
+ oraclePriceData: OraclePriceData,
158
+ reservePrice: BN,
159
+ now: BN
160
+ ): BN {
161
+ const confInterval = oraclePriceData.confidence || ZERO;
162
+
163
+ const sinceLastUpdate = BN.max(
164
+ ZERO,
165
+ now.sub(amm.historicalOracleData.lastOraclePriceTwapTs)
166
+ );
167
+ let lowerBoundConfPct = amm.lastOracleConfPct;
168
+ if (sinceLastUpdate.gt(ZERO)) {
169
+ const lowerBoundConfDivisor = BN.max(
170
+ new BN(21).sub(sinceLastUpdate),
171
+ new BN(5)
172
+ );
173
+ lowerBoundConfPct = amm.lastOracleConfPct.sub(
174
+ amm.lastOracleConfPct.div(lowerBoundConfDivisor)
175
+ );
176
+ }
177
+ const confIntervalPct = confInterval
178
+ .mul(BID_ASK_SPREAD_PRECISION)
179
+ .div(reservePrice);
180
+
181
+ const confIntervalPctResult = BN.max(confIntervalPct, lowerBoundConfPct);
182
+
183
+ return confIntervalPctResult;
184
+ }
package/tests/amm/test.ts CHANGED
@@ -31,6 +31,7 @@ import {
31
31
  ContractTier,
32
32
  isOracleValid,
33
33
  OracleGuardRails,
34
+ getNewOracleConfPct,
34
35
  // calculateReservePrice,
35
36
  } from '../../src';
36
37
  import { mockPerpMarkets } from '../dlob/helpers';
@@ -662,7 +663,7 @@ describe('AMM Tests', () => {
662
663
  new BN(suiExample.amm.totalFeeMinusDistributions),
663
664
  new BN(suiExample.amm.netRevenueSinceLastFunding),
664
665
  new BN(suiExample.amm.sqrtK),
665
- new BN(suiExample.amm.sqrtK.sub()),
666
+ new BN(suiExample.amm.sqrtK),
666
667
  new BN(suiExample.amm.maxBaseAssetReserve),
667
668
  new BN(suiExample.amm.markStd),
668
669
  new BN(suiExample.amm.oracleStd),
@@ -723,7 +724,7 @@ describe('AMM Tests', () => {
723
724
  );
724
725
 
725
726
  console.log('starting rr:');
726
- let reservePrice = undefined;
727
+ let reservePrice: BN | undefined = undefined;
727
728
  if (!reservePrice) {
728
729
  reservePrice = calculatePrice(
729
730
  mockAmm.baseAssetReserve,
@@ -748,7 +749,6 @@ describe('AMM Tests', () => {
748
749
  console.log('reservePrice:', reservePrice.toString());
749
750
  console.log('targetMarkSpreadPct:', targetMarkSpreadPct.toString());
750
751
  console.log('confIntervalPct:', confIntervalPct.toString());
751
-
752
752
  console.log('liveOracleStd:', liveOracleStd.toString());
753
753
 
754
754
  const tt = calculateSpread(mockAmm, oraclePriceData, now);
@@ -1138,6 +1138,17 @@ describe('AMM Tests', () => {
1138
1138
  console.log('liveOracleStd:', liveOracleStd.toNumber());
1139
1139
  assert(liveOracleStd.eq(new BN(192962)));
1140
1140
 
1141
+ mockAmm.lastOracleConfPct = new BN(150000);
1142
+ const reservePrice = new BN(13.553 * PRICE_PRECISION.toNumber());
1143
+ const newConfPct = getNewOracleConfPct(mockAmm, oraclePriceData, reservePrice, now);
1144
+ console.log('newConfPct:', newConfPct.toString());
1145
+
1146
+ assert(
1147
+ now.sub(mockAmm.historicalOracleData.lastOraclePriceTwapTs).gt(ZERO)
1148
+ );
1149
+
1150
+ assert(newConfPct.eq(new BN(135000)));
1151
+
1141
1152
  const oracleGuardRails: OracleGuardRails = {
1142
1153
  priceDivergence: {
1143
1154
  markOraclePercentDivergence: PERCENTAGE_PRECISION.divn(10),