@drift-labs/sdk 2.149.1 → 2.150.0-alpha.0

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.
Files changed (58) hide show
  1. package/.env +4 -0
  2. package/VERSION +1 -1
  3. package/lib/browser/constants/perpMarkets.js +11 -0
  4. package/lib/browser/constants/spotMarkets.js +13 -0
  5. package/lib/browser/decode/user.js +2 -2
  6. package/lib/browser/driftClient.d.ts +20 -8
  7. package/lib/browser/driftClient.js +216 -17
  8. package/lib/browser/idl/drift.json +225 -21
  9. package/lib/browser/math/margin.js +2 -1
  10. package/lib/browser/math/position.d.ts +1 -0
  11. package/lib/browser/math/position.js +10 -2
  12. package/lib/browser/math/superStake.d.ts +3 -2
  13. package/lib/browser/types.d.ts +12 -6
  14. package/lib/browser/types.js +11 -6
  15. package/lib/browser/user.d.ts +3 -2
  16. package/lib/browser/user.js +24 -8
  17. package/lib/node/constants/perpMarkets.d.ts.map +1 -1
  18. package/lib/node/constants/perpMarkets.js +11 -0
  19. package/lib/node/constants/spotMarkets.d.ts.map +1 -1
  20. package/lib/node/constants/spotMarkets.js +13 -0
  21. package/lib/node/decode/user.d.ts.map +1 -1
  22. package/lib/node/decode/user.js +2 -2
  23. package/lib/node/driftClient.d.ts +20 -8
  24. package/lib/node/driftClient.d.ts.map +1 -1
  25. package/lib/node/driftClient.js +216 -17
  26. package/lib/node/idl/drift.json +225 -21
  27. package/lib/node/math/margin.d.ts.map +1 -1
  28. package/lib/node/math/margin.js +2 -1
  29. package/lib/node/math/position.d.ts +1 -0
  30. package/lib/node/math/position.d.ts.map +1 -1
  31. package/lib/node/math/position.js +10 -2
  32. package/lib/node/math/spotBalance.d.ts.map +1 -1
  33. package/lib/node/math/superStake.d.ts +3 -2
  34. package/lib/node/math/superStake.d.ts.map +1 -1
  35. package/lib/node/types.d.ts +12 -6
  36. package/lib/node/types.d.ts.map +1 -1
  37. package/lib/node/types.js +11 -6
  38. package/lib/node/user.d.ts +3 -2
  39. package/lib/node/user.d.ts.map +1 -1
  40. package/lib/node/user.js +24 -8
  41. package/package.json +1 -1
  42. package/scripts/deposit-isolated-positions.ts +110 -0
  43. package/scripts/single-grpc-client-test.ts +71 -21
  44. package/scripts/withdraw-isolated-positions.ts +174 -0
  45. package/src/constants/perpMarkets.ts +11 -0
  46. package/src/constants/spotMarkets.ts +14 -0
  47. package/src/decode/user.ts +2 -3
  48. package/src/driftClient.ts +464 -41
  49. package/src/idl/drift.json +226 -22
  50. package/src/margin/README.md +143 -0
  51. package/src/math/margin.ts +3 -4
  52. package/src/math/position.ts +12 -2
  53. package/src/math/spotBalance.ts +0 -1
  54. package/src/types.ts +15 -7
  55. package/src/user.ts +49 -15
  56. package/tests/amm/test.ts +1 -1
  57. package/tests/dlob/helpers.ts +1 -1
  58. package/tests/user/test.ts +0 -7
package/src/user.ts CHANGED
@@ -204,6 +204,7 @@ export class User {
204
204
  }
205
205
 
206
206
  public async unsubscribe(): Promise<void> {
207
+ this.eventEmitter.removeAllListeners();
207
208
  await this.accountSubscriber.unsubscribe();
208
209
  this.isSubscribed = false;
209
210
  }
@@ -352,6 +353,10 @@ export class User {
352
353
  };
353
354
  }
354
355
 
356
+ public isPositionEmpty(position: PerpPosition): boolean {
357
+ return position.baseAssetAmount.eq(ZERO) && position.openOrders === 0;
358
+ }
359
+
355
360
  public getIsolatePerpPositionTokenAmount(perpMarketIndex: number): BN {
356
361
  const perpPosition = this.getPerpPosition(perpMarketIndex);
357
362
  if (!perpPosition) return ZERO;
@@ -485,7 +490,8 @@ export class User {
485
490
  marketIndex: number,
486
491
  collateralBuffer = ZERO,
487
492
  enterHighLeverageMode = undefined,
488
- maxMarginRatio = undefined
493
+ maxMarginRatio = undefined,
494
+ positionType: 'isolated' | 'cross' = 'cross'
489
495
  ): BN {
490
496
  const perpPosition = this.getPerpPositionOrEmpty(marketIndex);
491
497
 
@@ -499,10 +505,33 @@ export class User {
499
505
  )
500
506
  : ZERO;
501
507
 
502
- const freeCollateral = this.getFreeCollateral(
503
- 'Initial',
504
- enterHighLeverageMode
505
- ).sub(collateralBuffer);
508
+ let freeCollateral: BN = ZERO;
509
+ // if position is isolated, we always add on available quote from the cross account
510
+ if (positionType === 'isolated') {
511
+ const {
512
+ totalAssetValue: quoteSpotMarketAssetValue,
513
+ totalLiabilityValue: quoteSpotMarketLiabilityValue,
514
+ } = this.getSpotMarketAssetAndLiabilityValue(
515
+ perpMarket.quoteSpotMarketIndex,
516
+ 'Initial',
517
+ undefined,
518
+ undefined,
519
+ true
520
+ );
521
+
522
+ freeCollateral = quoteSpotMarketAssetValue.sub(
523
+ quoteSpotMarketLiabilityValue
524
+ );
525
+ }
526
+
527
+ // adding free collateral from the cross account or from within isolated margin calc for this marketIndex
528
+ freeCollateral = freeCollateral.add(
529
+ this.getFreeCollateral(
530
+ 'Initial',
531
+ enterHighLeverageMode,
532
+ positionType === 'isolated' ? marketIndex : undefined
533
+ ).sub(collateralBuffer)
534
+ );
506
535
 
507
536
  return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(
508
537
  marketIndex,
@@ -544,17 +573,20 @@ export class User {
544
573
  enterHighLeverageMode = false,
545
574
  perpMarketIndex?: number
546
575
  ): BN {
547
- const { totalCollateral, marginRequirement, getIsolatedFreeCollateral } =
548
- this.getMarginCalculation(marginCategory, {
549
- enteringHighLeverage: enterHighLeverageMode,
550
- strict: marginCategory === 'Initial',
551
- });
576
+ const calc = this.getMarginCalculation(marginCategory, {
577
+ enteringHighLeverage: enterHighLeverageMode,
578
+ strict: marginCategory === 'Initial',
579
+ });
552
580
 
553
581
  if (perpMarketIndex !== undefined) {
554
- return getIsolatedFreeCollateral(perpMarketIndex);
582
+ // getIsolatedFreeCollateral will throw if no existing isolated position but we are fetching for potential new position, so we wrap in a try/catch
583
+ try {
584
+ return calc.getIsolatedFreeCollateral(perpMarketIndex);
585
+ } catch (error) {
586
+ return ZERO;
587
+ }
555
588
  } else {
556
- const freeCollateral = totalCollateral.sub(marginRequirement);
557
- return freeCollateral.gte(ZERO) ? freeCollateral : ZERO;
589
+ return calc.getCrossFreeCollateral();
558
590
  }
559
591
  }
560
592
 
@@ -2752,7 +2784,8 @@ export class User {
2752
2784
  tradeSide: PositionDirection,
2753
2785
  isLp = false,
2754
2786
  enterHighLeverageMode = undefined,
2755
- maxMarginRatio = undefined
2787
+ maxMarginRatio = undefined,
2788
+ positionType: 'isolated' | 'cross' = 'cross'
2756
2789
  ): { tradeSize: BN; oppositeSideTradeSize: BN } {
2757
2790
  let tradeSize = ZERO;
2758
2791
  let oppositeSideTradeSize = ZERO;
@@ -2792,7 +2825,8 @@ export class User {
2792
2825
  targetMarketIndex,
2793
2826
  lpBuffer,
2794
2827
  enterHighLeverageMode,
2795
- maxMarginRatio
2828
+ maxMarginRatio,
2829
+ positionType
2796
2830
  );
2797
2831
 
2798
2832
  if (maxPositionSize.gte(ZERO)) {
package/tests/amm/test.ts CHANGED
@@ -279,7 +279,7 @@ describe('AMM Tests', () => {
279
279
  longIntensity,
280
280
  shortIntensity,
281
281
  volume24H,
282
- 0
282
+ 0,
283
283
  );
284
284
  const l1 = spreads[0];
285
285
  const s1 = spreads[1];
@@ -782,4 +782,4 @@ export class MockUserMap implements UserMapInterface {
782
782
  ];
783
783
  }
784
784
  }
785
- }
785
+ }
@@ -49,7 +49,6 @@ async function makeMockUser(
49
49
  oraclePriceMap[myMockSpotMarkets[i].oracle.toString()] =
50
50
  spotOraclePriceList[i];
51
51
  }
52
- // console.log(oraclePriceMap);
53
52
 
54
53
  function getMockUserAccount(): UserAccount {
55
54
  return myMockUserAccount;
@@ -61,12 +60,6 @@ async function makeMockUser(
61
60
  return myMockSpotMarkets[marketIndex];
62
61
  }
63
62
  function getMockOracle(oracleKey: PublicKey) {
64
- // console.log('oracleKey.toString():', oracleKey.toString());
65
- // console.log(
66
- // 'oraclePriceMap[oracleKey.toString()]:',
67
- // oraclePriceMap[oracleKey.toString()]
68
- // );
69
-
70
63
  const QUOTE_ORACLE_PRICE_DATA: OraclePriceData = {
71
64
  price: new BN(
72
65
  oraclePriceMap[oracleKey.toString()] * PRICE_PRECISION.toNumber()