@drift-labs/sdk 2.37.1-beta.1 → 2.37.1-beta.2

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/tests/amm/test.ts CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  calculateInventoryScale,
13
13
  calculateAllEstimatedFundingRate,
14
14
  calculateLongShortFundingRateAndLiveTwaps,
15
- OraclePriceData
15
+ OraclePriceData,
16
16
  } from '../../src';
17
17
  import { mockPerpMarkets } from '../dlob/helpers';
18
18
 
@@ -408,33 +408,51 @@ describe('AMM Tests', () => {
408
408
  confidence: new BN(1),
409
409
  hasSufficientNumberOfDataPoints: true,
410
410
  };
411
- mockMarket1.amm.historicalOracleData.lastOraclePrice = new BN(1.9535 * PRICE_PRECISION.toNumber());
411
+ mockMarket1.amm.historicalOracleData.lastOraclePrice = new BN(
412
+ 1.9535 * PRICE_PRECISION.toNumber()
413
+ );
412
414
 
413
415
  // mockMarket1.amm.pegMultiplier = new BN(1.897573 * 1e3);
414
416
 
415
- mockMarket1.amm.lastMarkPriceTwap = new BN(1.945594 * PRICE_PRECISION.toNumber());
416
- mockMarket1.amm.lastBidPriceTwap = new BN(1.941629 * PRICE_PRECISION.toNumber());
417
- mockMarket1.amm.lastAskPriceTwap = new BN(1.94956 * PRICE_PRECISION.toNumber());
417
+ mockMarket1.amm.lastMarkPriceTwap = new BN(
418
+ 1.945594 * PRICE_PRECISION.toNumber()
419
+ );
420
+ mockMarket1.amm.lastBidPriceTwap = new BN(
421
+ 1.941629 * PRICE_PRECISION.toNumber()
422
+ );
423
+ mockMarket1.amm.lastAskPriceTwap = new BN(
424
+ 1.94956 * PRICE_PRECISION.toNumber()
425
+ );
418
426
  mockMarket1.amm.lastMarkPriceTwapTs = new BN(1688877729);
419
427
 
420
- mockMarket1.amm.historicalOracleData.lastOraclePriceTwap = new BN(1.942449 * PRICE_PRECISION.toNumber());
421
- mockMarket1.amm.historicalOracleData.lastOraclePriceTwapTs = new BN(1688878333);
422
-
423
- const [_markTwapLive, _oracleTwapLive, _lowerboundEst, _cappedAltEst, _interpEst] =
424
- await calculateAllEstimatedFundingRate(
425
- mockMarket1,
426
- oraclePriceData,
427
- currentMarkPrice,
428
- now
429
- );
428
+ mockMarket1.amm.historicalOracleData.lastOraclePriceTwap = new BN(
429
+ 1.942449 * PRICE_PRECISION.toNumber()
430
+ );
431
+ mockMarket1.amm.historicalOracleData.lastOraclePriceTwapTs = new BN(
432
+ 1688878333
433
+ );
430
434
 
431
- const [markTwapLive, oracleTwapLive, est1, est2] = await calculateLongShortFundingRateAndLiveTwaps(
435
+ const [
436
+ _markTwapLive,
437
+ _oracleTwapLive,
438
+ _lowerboundEst,
439
+ _cappedAltEst,
440
+ _interpEst,
441
+ ] = await calculateAllEstimatedFundingRate(
432
442
  mockMarket1,
433
443
  oraclePriceData,
434
444
  currentMarkPrice,
435
445
  now
436
446
  );
437
447
 
448
+ const [markTwapLive, oracleTwapLive, est1, est2] =
449
+ await calculateLongShortFundingRateAndLiveTwaps(
450
+ mockMarket1,
451
+ oraclePriceData,
452
+ currentMarkPrice,
453
+ now
454
+ );
455
+
438
456
  // console.log(markTwapLive.toString());
439
457
  // console.log(oracleTwapLive.toString());
440
458
  // console.log(est1.toString());
@@ -444,7 +462,6 @@ describe('AMM Tests', () => {
444
462
  assert(oracleTwapLive.eq(new BN('1942510')));
445
463
  assert(est1.eq(new BN('15692')));
446
464
  assert(est2.eq(new BN('15692')));
447
-
448
465
  });
449
466
 
450
467
  it('predicted funding rate mock2', async () => {
@@ -464,25 +481,42 @@ describe('AMM Tests', () => {
464
481
  confidence: new BN(1),
465
482
  hasSufficientNumberOfDataPoints: true,
466
483
  };
467
- mockMarket1.amm.historicalOracleData.lastOraclePrice = new BN(1.9535 * PRICE_PRECISION.toNumber());
484
+ mockMarket1.amm.historicalOracleData.lastOraclePrice = new BN(
485
+ 1.9535 * PRICE_PRECISION.toNumber()
486
+ );
468
487
 
469
488
  // mockMarket1.amm.pegMultiplier = new BN(1.897573 * 1e3);
470
489
 
471
- mockMarket1.amm.lastMarkPriceTwap = new BN(1.218363 * PRICE_PRECISION.toNumber());
472
- mockMarket1.amm.lastBidPriceTwap = new BN(1.218363 * PRICE_PRECISION.toNumber());
473
- mockMarket1.amm.lastAskPriceTwap = new BN(1.218364 * PRICE_PRECISION.toNumber());
490
+ mockMarket1.amm.lastMarkPriceTwap = new BN(
491
+ 1.218363 * PRICE_PRECISION.toNumber()
492
+ );
493
+ mockMarket1.amm.lastBidPriceTwap = new BN(
494
+ 1.218363 * PRICE_PRECISION.toNumber()
495
+ );
496
+ mockMarket1.amm.lastAskPriceTwap = new BN(
497
+ 1.218364 * PRICE_PRECISION.toNumber()
498
+ );
474
499
  mockMarket1.amm.lastMarkPriceTwapTs = new BN(1688878815);
475
500
 
476
- mockMarket1.amm.historicalOracleData.lastOraclePriceTwap = new BN(1.220964 * PRICE_PRECISION.toNumber());
477
- mockMarket1.amm.historicalOracleData.lastOraclePriceTwapTs = new BN(1688879991);
501
+ mockMarket1.amm.historicalOracleData.lastOraclePriceTwap = new BN(
502
+ 1.220964 * PRICE_PRECISION.toNumber()
503
+ );
504
+ mockMarket1.amm.historicalOracleData.lastOraclePriceTwapTs = new BN(
505
+ 1688879991
506
+ );
478
507
 
479
- const [_markTwapLive, _oracleTwapLive, _lowerboundEst, _cappedAltEst, _interpEst] =
480
- await calculateAllEstimatedFundingRate(
481
- mockMarket1,
482
- oraclePriceData,
483
- currentMarkPrice,
484
- now
485
- );
508
+ const [
509
+ _markTwapLive,
510
+ _oracleTwapLive,
511
+ _lowerboundEst,
512
+ _cappedAltEst,
513
+ _interpEst,
514
+ ] = await calculateAllEstimatedFundingRate(
515
+ mockMarket1,
516
+ oraclePriceData,
517
+ currentMarkPrice,
518
+ now
519
+ );
486
520
 
487
521
  // console.log(_markTwapLive.toString());
488
522
  // console.log(_oracleTwapLive.toString());
@@ -491,21 +525,31 @@ describe('AMM Tests', () => {
491
525
  // console.log(_interpEst.toString());
492
526
  // console.log('-----');
493
527
 
494
- const [markTwapLive, oracleTwapLive, est1, est2] = await calculateLongShortFundingRateAndLiveTwaps(
495
- mockMarket1,
496
- oraclePriceData,
497
- currentMarkPrice,
498
- now
499
- );
528
+ const [markTwapLive, oracleTwapLive, est1, est2] =
529
+ await calculateLongShortFundingRateAndLiveTwaps(
530
+ mockMarket1,
531
+ oraclePriceData,
532
+ currentMarkPrice,
533
+ now
534
+ );
500
535
 
501
- console.log('markTwapLive:', mockMarket1.amm.lastMarkPriceTwap.toString(), '->', markTwapLive.toString());
502
- console.log('oracTwapLive:', mockMarket1.amm.historicalOracleData.lastOraclePriceTwap.toString(), '->', oracleTwapLive.toString());
536
+ console.log(
537
+ 'markTwapLive:',
538
+ mockMarket1.amm.lastMarkPriceTwap.toString(),
539
+ '->',
540
+ markTwapLive.toString()
541
+ );
542
+ console.log(
543
+ 'oracTwapLive:',
544
+ mockMarket1.amm.historicalOracleData.lastOraclePriceTwap.toString(),
545
+ '->',
546
+ oracleTwapLive.toString()
547
+ );
503
548
  console.log('pred funding:', est1.toString(), est2.toString());
504
549
 
505
550
  assert(markTwapLive.eq(new BN('1222131')));
506
551
  assert(oracleTwapLive.eq(new BN('1222586')));
507
552
  assert(est1.eq(est2));
508
553
  assert(est2.eq(new BN('-1550')));
509
-
510
554
  });
511
555
  });
@@ -23,7 +23,7 @@ import {
23
23
  convertToNumber,
24
24
  QUOTE_PRECISION,
25
25
  isVariant,
26
- TWO
26
+ TWO,
27
27
  } from '../../src';
28
28
 
29
29
  import { mockPerpMarkets, mockSpotMarkets, mockStateAccount } from './helpers';
@@ -137,11 +137,16 @@ function printOrderNode(
137
137
  slot: number | undefined
138
138
  ) {
139
139
  console.log(
140
- ` . vAMMNode? ${node.isVammNode()},\t${node.order ? getVariant(node.order?.orderType) : '~'
141
- } ${node.order ? getVariant(node.order?.direction) : '~'}\t, slot: ${node.order?.slot.toString() || '~'
142
- }, orderId: ${node.order?.orderId.toString() || '~'},\tnode.getPrice: ${oracle ? node.getPrice(oracle, slot!) : '~'
143
- }, node.price: ${node.order?.price.toString() || '~'}, priceOffset: ${node.order?.oraclePriceOffset.toString() || '~'
144
- } quantity: ${node.order?.baseAssetAmountFilled.toString() || '~'}/${node.order?.baseAssetAmount.toString() || '~'
140
+ ` . vAMMNode? ${node.isVammNode()},\t${
141
+ node.order ? getVariant(node.order?.orderType) : '~'
142
+ } ${node.order ? getVariant(node.order?.direction) : '~'}\t, slot: ${
143
+ node.order?.slot.toString() || '~'
144
+ }, orderId: ${node.order?.orderId.toString() || '~'},\tnode.getPrice: ${
145
+ oracle ? node.getPrice(oracle, slot!) : '~'
146
+ }, node.price: ${node.order?.price.toString() || '~'}, priceOffset: ${
147
+ node.order?.oraclePriceOffset.toString() || '~'
148
+ } quantity: ${node.order?.baseAssetAmountFilled.toString() || '~'}/${
149
+ node.order?.baseAssetAmount.toString() || '~'
145
150
  }`
146
151
  );
147
152
  }
@@ -188,7 +193,8 @@ function printBookState(
188
193
 
189
194
  function printCrossedNodes(n: NodeToFill, slot: number) {
190
195
  console.log(
191
- `Cross Found, takerExists: ${n.node.order !== undefined}, makerExists: ${n.makerNodes !== undefined
196
+ `Cross Found, takerExists: ${n.node.order !== undefined}, makerExists: ${
197
+ n.makerNodes !== undefined
192
198
  }`
193
199
  );
194
200
  console.log(
@@ -210,8 +216,10 @@ function printCrossedNodes(n: NodeToFill, slot: number) {
210
216
  console.log(
211
217
  ` orderId: ${o.orderId}, ${getVariant(o.orderType)}, ${getVariant(
212
218
  o.direction
213
- )},\texpired: ${isOrderExpired(o, slot)}, postOnly: ${o.postOnly
214
- }, reduceOnly: ${o.reduceOnly
219
+ )},\texpired: ${isOrderExpired(o, slot)}, postOnly: ${
220
+ o.postOnly
221
+ }, reduceOnly: ${
222
+ o.reduceOnly
215
223
  }, price: ${o.price.toString()}, priceOffset: ${o.oraclePriceOffset.toString()}, baseAmtFileld: ${o.baseAssetAmountFilled.toString()}/${o.baseAssetAmount.toString()}`
216
224
  );
217
225
  };
@@ -5790,10 +5798,7 @@ describe('DLOB Spot Tests', () => {
5790
5798
  slot,
5791
5799
  oraclePriceData: oracle,
5792
5800
  });
5793
- const quoteAmtOut = convertToNumber(
5794
- out,
5795
- QUOTE_PRECISION
5796
- );
5801
+ const quoteAmtOut = convertToNumber(out, QUOTE_PRECISION);
5797
5802
 
5798
5803
  // 1 * 20.69 + 2 * 20.70 + 1 * 20.71 = 82.8
5799
5804
  expect(quoteAmtOut === 82.8).to.be.true;
@@ -5882,10 +5887,7 @@ describe('DLOB Spot Tests', () => {
5882
5887
  slot,
5883
5888
  oraclePriceData: oracle,
5884
5889
  });
5885
- const quoteAmtOut = convertToNumber(
5886
- out,
5887
- QUOTE_PRECISION
5888
- );
5890
+ const quoteAmtOut = convertToNumber(out, QUOTE_PRECISION);
5889
5891
 
5890
5892
  // 1 * 20.69 + 2 * 20.68 + 1 * 20.67 = 82.72
5891
5893
  expect(quoteAmtOut === 82.72).to.be.true;