@drift-labs/sdk 2.12.0-beta.2 → 2.12.0-beta.3

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 (52) hide show
  1. package/lib/accounts/bulkAccountLoader.js +3 -3
  2. package/lib/accounts/fetch.js +2 -2
  3. package/lib/accounts/pollingDriftClientAccountSubscriber.js +7 -7
  4. package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
  5. package/lib/accounts/pollingUserAccountSubscriber.js +2 -2
  6. package/lib/accounts/pollingUserStatsAccountSubscriber.js +2 -2
  7. package/lib/accounts/webSocketAccountSubscriber.js +1 -1
  8. package/lib/accounts/webSocketDriftClientAccountSubscriber.js +3 -3
  9. package/lib/addresses/marketAddresses.js +1 -1
  10. package/lib/addresses/pda.js +1 -5
  11. package/lib/adminClient.js +57 -61
  12. package/lib/dlob/DLOB.js +67 -67
  13. package/lib/dlob/DLOBNode.js +7 -7
  14. package/lib/dlob/NodeList.js +2 -2
  15. package/lib/driftClient.d.ts +3 -3
  16. package/lib/driftClient.js +83 -87
  17. package/lib/events/eventSubscriber.js +2 -2
  18. package/lib/events/pollingLogProvider.js +1 -1
  19. package/lib/examples/makeTradeExample.js +9 -9
  20. package/lib/factory/bigNum.js +9 -9
  21. package/lib/factory/oracleClient.js +2 -2
  22. package/lib/idl/drift.json +1 -1
  23. package/lib/index.js +1 -5
  24. package/lib/math/amm.js +23 -23
  25. package/lib/math/auction.js +6 -6
  26. package/lib/math/exchangeStatus.js +2 -2
  27. package/lib/math/funding.js +2 -2
  28. package/lib/math/margin.js +5 -5
  29. package/lib/math/market.js +12 -12
  30. package/lib/math/oracles.js +1 -1
  31. package/lib/math/orders.js +23 -23
  32. package/lib/math/position.js +5 -5
  33. package/lib/math/repeg.js +1 -1
  34. package/lib/math/spotBalance.js +7 -7
  35. package/lib/math/spotPosition.js +3 -3
  36. package/lib/math/trade.js +34 -34
  37. package/lib/oracles/oracleClientCache.js +1 -1
  38. package/lib/oracles/pythClient.js +1 -1
  39. package/lib/tokenFaucet.js +1 -5
  40. package/lib/tx/retryTxSender.js +1 -1
  41. package/lib/tx/utils.d.ts +1 -1
  42. package/lib/tx/utils.js +7 -4
  43. package/lib/types.d.ts +4 -0
  44. package/lib/user.js +47 -47
  45. package/lib/userMap/userMap.js +1 -1
  46. package/lib/userMap/userStatsMap.js +3 -3
  47. package/lib/userStats.js +2 -2
  48. package/package.json +1 -1
  49. package/src/driftClient.ts +11 -4
  50. package/src/idl/drift.json +1 -1
  51. package/src/tx/utils.ts +11 -3
  52. package/src/types.ts +5 -0
package/lib/user.js CHANGED
@@ -141,7 +141,7 @@ class User {
141
141
  return [numericConstants_1.ZERO, numericConstants_1.ZERO];
142
142
  }
143
143
  const market = this.driftClient.getPerpMarketAccount(marketIndex);
144
- const [marketOpenBids, marketOpenAsks] = (0, amm_1.calculateMarketOpenBidAsk)(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
144
+ const [marketOpenBids, marketOpenAsks] = amm_1.calculateMarketOpenBidAsk(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
145
145
  const lpOpenBids = marketOpenBids
146
146
  .mul(position.lpShares)
147
147
  .div(market.amm.sqrtK);
@@ -294,11 +294,11 @@ class User {
294
294
  if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
295
295
  perpPosition = this.getSettledLPPosition(perpPosition.marketIndex)[0];
296
296
  }
297
- let positionUnrealizedPnl = (0, _1.calculatePositionPNL)(market, perpPosition, withFunding, oraclePriceData);
297
+ let positionUnrealizedPnl = _1.calculatePositionPNL(market, perpPosition, withFunding, oraclePriceData);
298
298
  if (withWeightMarginCategory !== undefined) {
299
299
  if (positionUnrealizedPnl.gt(numericConstants_1.ZERO)) {
300
300
  positionUnrealizedPnl = positionUnrealizedPnl
301
- .mul((0, _1.calculateUnrealizedAssetWeight)(market, quoteSpotMarket, positionUnrealizedPnl, withWeightMarginCategory, oraclePriceData))
301
+ .mul(_1.calculateUnrealizedAssetWeight(market, quoteSpotMarket, positionUnrealizedPnl, withWeightMarginCategory, oraclePriceData))
302
302
  .div(new _1.BN(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION));
303
303
  }
304
304
  }
@@ -314,20 +314,20 @@ class User {
314
314
  .perpPositions.filter((pos) => marketIndex ? pos.marketIndex === marketIndex : true)
315
315
  .reduce((pnl, perpPosition) => {
316
316
  const market = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
317
- return pnl.add((0, _1.calculatePositionFundingPNL)(market, perpPosition));
317
+ return pnl.add(_1.calculatePositionFundingPNL(market, perpPosition));
318
318
  }, numericConstants_1.ZERO);
319
319
  }
320
320
  getSpotMarketLiabilityValue(marketIndex, marginCategory, liquidationBuffer, includeOpenOrders) {
321
321
  return this.getUserAccount().spotPositions.reduce((totalLiabilityValue, spotPosition) => {
322
- if ((0, spotPosition_1.isSpotPositionAvailable)(spotPosition) ||
322
+ if (spotPosition_1.isSpotPositionAvailable(spotPosition) ||
323
323
  (marketIndex !== undefined &&
324
324
  spotPosition.marketIndex !== marketIndex)) {
325
325
  return totalLiabilityValue;
326
326
  }
327
327
  const spotMarketAccount = this.driftClient.getSpotMarketAccount(spotPosition.marketIndex);
328
328
  if (spotPosition.marketIndex === numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
329
- if ((0, types_1.isVariant)(spotPosition.balanceType, 'borrow')) {
330
- const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
329
+ if (types_1.isVariant(spotPosition.balanceType, 'borrow')) {
330
+ const tokenAmount = spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
331
331
  let weight = numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION;
332
332
  if (marginCategory === 'Initial') {
333
333
  weight = _1.BN.max(weight, new _1.BN(this.getUserAccount().maxMarginRatio));
@@ -343,8 +343,8 @@ class User {
343
343
  }
344
344
  const oraclePriceData = this.getOracleDataForSpotMarket(spotPosition.marketIndex);
345
345
  if (!includeOpenOrders) {
346
- if ((0, types_1.isVariant)(spotPosition.balanceType, 'borrow')) {
347
- const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
346
+ if (types_1.isVariant(spotPosition.balanceType, 'borrow')) {
347
+ const tokenAmount = spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
348
348
  const liabilityValue = this.getSpotLiabilityValue(tokenAmount, oraclePriceData, spotMarketAccount, marginCategory, liquidationBuffer);
349
349
  return totalLiabilityValue.add(liabilityValue);
350
350
  }
@@ -352,7 +352,7 @@ class User {
352
352
  return totalLiabilityValue;
353
353
  }
354
354
  }
355
- const [worstCaseTokenAmount, worstCaseQuoteTokenAmount] = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, this.getOracleDataForSpotMarket(spotPosition.marketIndex));
355
+ const [worstCaseTokenAmount, worstCaseQuoteTokenAmount] = spotPosition_1.getWorstCaseTokenAmounts(spotPosition, spotMarketAccount, this.getOracleDataForSpotMarket(spotPosition.marketIndex));
356
356
  let newTotalLiabilityValue = totalLiabilityValue;
357
357
  if (worstCaseTokenAmount.lt(numericConstants_1.ZERO)) {
358
358
  const baseLiabilityValue = this.getSpotLiabilityValue(worstCaseTokenAmount.abs(), oraclePriceData, spotMarketAccount, marginCategory, liquidationBuffer);
@@ -376,9 +376,9 @@ class User {
376
376
  }, numericConstants_1.ZERO);
377
377
  }
378
378
  getSpotLiabilityValue(tokenAmount, oraclePriceData, spotMarketAccount, marginCategory, liquidationBuffer) {
379
- let liabilityValue = (0, _1.getTokenValue)(tokenAmount, spotMarketAccount.decimals, oraclePriceData);
379
+ let liabilityValue = _1.getTokenValue(tokenAmount, spotMarketAccount.decimals, oraclePriceData);
380
380
  if (marginCategory !== undefined) {
381
- let weight = (0, spotBalance_1.calculateLiabilityWeight)(tokenAmount, spotMarketAccount, marginCategory);
381
+ let weight = spotBalance_1.calculateLiabilityWeight(tokenAmount, spotMarketAccount, marginCategory);
382
382
  if (marginCategory === 'Initial') {
383
383
  weight = _1.BN.max(weight, new _1.BN(this.getUserAccount().maxMarginRatio));
384
384
  }
@@ -393,7 +393,7 @@ class User {
393
393
  }
394
394
  getSpotMarketAssetValue(marketIndex, marginCategory, includeOpenOrders) {
395
395
  return this.getUserAccount().spotPositions.reduce((totalAssetValue, spotPosition) => {
396
- if ((0, spotPosition_1.isSpotPositionAvailable)(spotPosition) ||
396
+ if (spotPosition_1.isSpotPositionAvailable(spotPosition) ||
397
397
  (marketIndex !== undefined &&
398
398
  spotPosition.marketIndex !== marketIndex)) {
399
399
  return totalAssetValue;
@@ -401,8 +401,8 @@ class User {
401
401
  // Todo this needs to account for whether it's based on initial or maintenance requirements
402
402
  const spotMarketAccount = this.driftClient.getSpotMarketAccount(spotPosition.marketIndex);
403
403
  if (spotPosition.marketIndex === numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
404
- if ((0, types_1.isVariant)(spotPosition.balanceType, 'deposit')) {
405
- const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
404
+ if (types_1.isVariant(spotPosition.balanceType, 'deposit')) {
405
+ const tokenAmount = spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
406
406
  return totalAssetValue.add(tokenAmount);
407
407
  }
408
408
  else {
@@ -411,8 +411,8 @@ class User {
411
411
  }
412
412
  const oraclePriceData = this.getOracleDataForSpotMarket(spotPosition.marketIndex);
413
413
  if (!includeOpenOrders) {
414
- if ((0, types_1.isVariant)(spotPosition.balanceType, 'deposit')) {
415
- const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
414
+ if (types_1.isVariant(spotPosition.balanceType, 'deposit')) {
415
+ const tokenAmount = spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
416
416
  const assetValue = this.getSpotAssetValue(tokenAmount, oraclePriceData, spotMarketAccount, marginCategory);
417
417
  return totalAssetValue.add(assetValue);
418
418
  }
@@ -420,7 +420,7 @@ class User {
420
420
  return totalAssetValue;
421
421
  }
422
422
  }
423
- const [worstCaseTokenAmount, worstCaseQuoteTokenAmount] = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, this.getOracleDataForSpotMarket(spotPosition.marketIndex));
423
+ const [worstCaseTokenAmount, worstCaseQuoteTokenAmount] = spotPosition_1.getWorstCaseTokenAmounts(spotPosition, spotMarketAccount, this.getOracleDataForSpotMarket(spotPosition.marketIndex));
424
424
  let newTotalAssetValue = totalAssetValue;
425
425
  if (worstCaseTokenAmount.gt(numericConstants_1.ZERO)) {
426
426
  const baseAssetValue = this.getSpotAssetValue(worstCaseTokenAmount, oraclePriceData, spotMarketAccount, marginCategory);
@@ -433,9 +433,9 @@ class User {
433
433
  }, numericConstants_1.ZERO);
434
434
  }
435
435
  getSpotAssetValue(tokenAmount, oraclePriceData, spotMarketAccount, marginCategory) {
436
- let assetValue = (0, _1.getTokenValue)(tokenAmount, spotMarketAccount.decimals, oraclePriceData);
436
+ let assetValue = _1.getTokenValue(tokenAmount, spotMarketAccount.decimals, oraclePriceData);
437
437
  if (marginCategory !== undefined) {
438
- const weight = (0, spotBalance_1.calculateAssetWeight)(tokenAmount, spotMarketAccount, marginCategory);
438
+ const weight = spotBalance_1.calculateAssetWeight(tokenAmount, spotMarketAccount, marginCategory);
439
439
  assetValue = assetValue.mul(weight).div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
440
440
  }
441
441
  return assetValue;
@@ -456,8 +456,8 @@ class User {
456
456
  */
457
457
  getHealth() {
458
458
  const userAccount = this.getUserAccount();
459
- if ((0, types_1.isVariant)(userAccount.status, 'beingLiquidated') ||
460
- (0, types_1.isVariant)(userAccount.status, 'bankrupt')) {
459
+ if (types_1.isVariant(userAccount.status, 'beingLiquidated') ||
460
+ types_1.isVariant(userAccount.status, 'bankrupt')) {
461
461
  return 0;
462
462
  }
463
463
  const totalCollateral = this.getTotalCollateral('Maintenance');
@@ -507,25 +507,25 @@ class User {
507
507
  perpPosition.openBids = totalOpenBids;
508
508
  }
509
509
  let valuationPrice = this.getOracleDataForPerpMarket(market.marketIndex).price;
510
- if ((0, types_1.isVariant)(market.status, 'settlement')) {
510
+ if (types_1.isVariant(market.status, 'settlement')) {
511
511
  valuationPrice = market.expiryPrice;
512
512
  }
513
513
  const baseAssetAmount = includeOpenOrders
514
- ? (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition)
514
+ ? margin_1.calculateWorstCaseBaseAssetAmount(perpPosition)
515
515
  : perpPosition.baseAssetAmount;
516
516
  let baseAssetValue = baseAssetAmount
517
517
  .abs()
518
518
  .mul(valuationPrice)
519
519
  .div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.PRICE_PRECISION));
520
520
  if (marginCategory) {
521
- let marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory));
521
+ let marginRatio = new _1.BN(_1.calculateMarketMarginRatio(market, baseAssetAmount.abs(), marginCategory));
522
522
  if (marginCategory === 'Initial') {
523
523
  marginRatio = _1.BN.max(marginRatio, new _1.BN(this.getUserAccount().maxMarginRatio));
524
524
  }
525
525
  if (liquidationBuffer !== undefined) {
526
526
  marginRatio = marginRatio.add(liquidationBuffer);
527
527
  }
528
- if ((0, types_1.isVariant)(market.status, 'settlement')) {
528
+ if (types_1.isVariant(market.status, 'settlement')) {
529
529
  marginRatio = numericConstants_1.ZERO;
530
530
  }
531
531
  baseAssetValue = baseAssetValue
@@ -545,7 +545,7 @@ class User {
545
545
  getPerpPositionValue(marketIndex, oraclePriceData) {
546
546
  const userPosition = this.getPerpPosition(marketIndex) || this.getEmptyPosition(marketIndex);
547
547
  const market = this.driftClient.getPerpMarketAccount(userPosition.marketIndex);
548
- return (0, margin_1.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData);
548
+ return margin_1.calculateBaseAssetValueWithOracle(market, userPosition, oraclePriceData);
549
549
  }
550
550
  getPositionSide(currentPosition) {
551
551
  if (currentPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
@@ -564,11 +564,11 @@ class User {
564
564
  */
565
565
  getPositionEstimatedExitPriceAndPnl(position, amountToClose, useAMMClose = false) {
566
566
  const market = this.driftClient.getPerpMarketAccount(position.marketIndex);
567
- const entryPrice = (0, position_1.calculateEntryPrice)(position);
567
+ const entryPrice = position_1.calculateEntryPrice(position);
568
568
  const oraclePriceData = this.getOracleDataForPerpMarket(position.marketIndex);
569
569
  if (amountToClose) {
570
570
  if (amountToClose.eq(numericConstants_1.ZERO)) {
571
- return [(0, _1.calculateReservePrice)(market, oraclePriceData), numericConstants_1.ZERO];
571
+ return [_1.calculateReservePrice(market, oraclePriceData), numericConstants_1.ZERO];
572
572
  }
573
573
  position = {
574
574
  baseAssetAmount: amountToClose,
@@ -579,10 +579,10 @@ class User {
579
579
  }
580
580
  let baseAssetValue;
581
581
  if (useAMMClose) {
582
- baseAssetValue = (0, _1.calculateBaseAssetValue)(market, position, oraclePriceData);
582
+ baseAssetValue = _1.calculateBaseAssetValue(market, position, oraclePriceData);
583
583
  }
584
584
  else {
585
- baseAssetValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, position, oraclePriceData);
585
+ baseAssetValue = margin_1.calculateBaseAssetValueWithOracle(market, position, oraclePriceData);
586
586
  }
587
587
  if (position.baseAssetAmount.eq(numericConstants_1.ZERO)) {
588
588
  return [numericConstants_1.ZERO, numericConstants_1.ZERO];
@@ -628,7 +628,7 @@ class User {
628
628
  return numericConstants_1.ZERO;
629
629
  }
630
630
  const totalLiabilityValue = this.getTotalLiabilityValue();
631
- const marginRatio = (0, _1.calculateMarketMarginRatio)(market,
631
+ const marginRatio = _1.calculateMarketMarginRatio(market,
632
632
  // worstCaseBaseAssetAmount.abs(),
633
633
  numericConstants_1.ZERO, // todo
634
634
  category);
@@ -658,7 +658,7 @@ class User {
658
658
  const totalCollateral = this.getTotalCollateral('Maintenance');
659
659
  // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
660
660
  let liquidationBuffer = undefined;
661
- const isBeingLiquidated = (0, types_1.isVariant)(this.getUserAccount().status, 'beingLiquidated');
661
+ const isBeingLiquidated = types_1.isVariant(this.getUserAccount().status, 'beingLiquidated');
662
662
  if (isBeingLiquidated) {
663
663
  liquidationBuffer = new _1.BN(this.driftClient.getStateAccount().liquidationMarginBufferRatio);
664
664
  }
@@ -666,13 +666,13 @@ class User {
666
666
  return totalCollateral.lt(maintenanceRequirement);
667
667
  }
668
668
  isBeingLiquidated() {
669
- return (0, types_1.isOneOfVariant)(this.getUserAccount().status, [
669
+ return types_1.isOneOfVariant(this.getUserAccount().status, [
670
670
  'beingLiquidated',
671
671
  'bankrupt',
672
672
  ]);
673
673
  }
674
674
  isBankrupt() {
675
- return (0, types_1.isVariant)(this.getUserAccount().status, 'bankrupt');
675
+ return types_1.isVariant(this.getUserAccount().status, 'bankrupt');
676
676
  }
677
677
  /**
678
678
  * Checks if any user position cumulative funding differs from respective market cumulative funding
@@ -708,7 +708,7 @@ class User {
708
708
  const mmr = this.getMaintenanceMarginRequirement();
709
709
  const deltaValueToLiq = mtc.sub(mmr); // QUOTE_PRECISION
710
710
  const currentSpotMarket = this.driftClient.getSpotMarketAccount(spotPosition.marketIndex);
711
- const tokenAmount = (0, spotBalance_1.getTokenAmount)(currentSpotPosition.scaledBalance, currentSpotMarket, currentSpotPosition.balanceType);
711
+ const tokenAmount = spotBalance_1.getTokenAmount(currentSpotPosition.scaledBalance, currentSpotMarket, currentSpotPosition.balanceType);
712
712
  const tokenAmountQP = tokenAmount
713
713
  .mul(numericConstants_1.QUOTE_PRECISION)
714
714
  .div(new _1.BN(10 ** currentSpotMarket.decimals));
@@ -716,7 +716,7 @@ class User {
716
716
  return new _1.BN(-1);
717
717
  }
718
718
  let liqPriceDelta;
719
- if ((0, types_1.isVariant)(currentSpotPosition.balanceType, 'borrow')) {
719
+ if (types_1.isVariant(currentSpotPosition.balanceType, 'borrow')) {
720
720
  liqPriceDelta = deltaValueToLiq
721
721
  .mul(numericConstants_1.PRICE_PRECISION)
722
722
  .mul(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
@@ -780,15 +780,15 @@ class User {
780
780
  if (proposedBaseAssetAmount.eq(numericConstants_1.ZERO))
781
781
  return new _1.BN(-1);
782
782
  const market = this.driftClient.getPerpMarketAccount(proposedPerpPosition.marketIndex);
783
- const proposedPerpPositionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, proposedPerpPosition, this.getOracleDataForPerpMarket(market.marketIndex));
783
+ const proposedPerpPositionValue = margin_1.calculateBaseAssetValueWithOracle(market, proposedPerpPosition, this.getOracleDataForPerpMarket(market.marketIndex));
784
784
  // total position value after trade
785
785
  const totalPositionValueAfterTrade = totalPositionValueExcludingTargetMarket.add(proposedPerpPositionValue);
786
786
  const marginRequirementExcludingTargetMarket = this.getUserAccount().perpPositions.reduce((totalMarginRequirement, position) => {
787
787
  if (position.marketIndex !== perpPosition.marketIndex) {
788
788
  const market = this.driftClient.getPerpMarketAccount(position.marketIndex);
789
- const positionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, position, this.getOracleDataForPerpMarket(market.marketIndex));
789
+ const positionValue = margin_1.calculateBaseAssetValueWithOracle(market, position, this.getOracleDataForPerpMarket(market.marketIndex));
790
790
  const marketMarginRequirement = positionValue
791
- .mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, position.baseAssetAmount.abs(), 'Maintenance')))
791
+ .mul(new _1.BN(_1.calculateMarketMarginRatio(market, position.baseAssetAmount.abs(), 'Maintenance')))
792
792
  .div(numericConstants_1.MARGIN_PRECISION);
793
793
  totalMarginRequirement = totalMarginRequirement.add(marketMarginRequirement);
794
794
  }
@@ -801,7 +801,7 @@ class User {
801
801
  return new _1.BN(-1);
802
802
  }
803
803
  const marginRequirementAfterTrade = marginRequirementExcludingTargetMarket.add(proposedPerpPositionValue
804
- .mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, proposedPerpPosition.baseAssetAmount.abs(), 'Maintenance')))
804
+ .mul(new _1.BN(_1.calculateMarketMarginRatio(market, proposedPerpPosition.baseAssetAmount.abs(), 'Maintenance')))
805
805
  .div(numericConstants_1.MARGIN_PRECISION));
806
806
  const freeCollateralAfterTrade = totalCollateral.sub(marginRequirementAfterTrade);
807
807
  const marketMaxLeverage = this.getMaxLeverage(proposedPerpPosition.marketIndex, 'Maintenance');
@@ -824,13 +824,13 @@ class User {
824
824
  }
825
825
  let markPriceAfterTrade;
826
826
  if (positionBaseSizeChange.eq(numericConstants_1.ZERO)) {
827
- markPriceAfterTrade = (0, _1.calculateReservePrice)(this.driftClient.getPerpMarketAccount(perpPosition.marketIndex), this.getOracleDataForPerpMarket(perpPosition.marketIndex));
827
+ markPriceAfterTrade = _1.calculateReservePrice(this.driftClient.getPerpMarketAccount(perpPosition.marketIndex), this.getOracleDataForPerpMarket(perpPosition.marketIndex));
828
828
  }
829
829
  else {
830
830
  const direction = positionBaseSizeChange.gt(numericConstants_1.ZERO)
831
831
  ? _1.PositionDirection.LONG
832
832
  : _1.PositionDirection.SHORT;
833
- markPriceAfterTrade = (0, _1.calculateTradeSlippage)(direction, positionBaseSizeChange.abs(), this.driftClient.getPerpMarketAccount(perpPosition.marketIndex), 'base', this.getOracleDataForPerpMarket(perpPosition.marketIndex))[3]; // newPrice after swap
833
+ markPriceAfterTrade = _1.calculateTradeSlippage(direction, positionBaseSizeChange.abs(), this.driftClient.getPerpMarketAccount(perpPosition.marketIndex), 'base', this.getOracleDataForPerpMarket(perpPosition.marketIndex))[3]; // newPrice after swap
834
834
  }
835
835
  if (priceDelta.gt(markPriceAfterTrade)) {
836
836
  return new _1.BN(-1);
@@ -881,7 +881,7 @@ class User {
881
881
  getMaxTradeSizeUSDC(targetMarketIndex, tradeSide) {
882
882
  const currentPosition = this.getPerpPosition(targetMarketIndex) ||
883
883
  this.getEmptyPosition(targetMarketIndex);
884
- const targetSide = (0, types_1.isVariant)(tradeSide, 'short') ? 'short' : 'long';
884
+ const targetSide = types_1.isVariant(tradeSide, 'short') ? 'short' : 'long';
885
885
  const currentPositionSide = (currentPosition === null || currentPosition === void 0 ? void 0 : currentPosition.baseAssetAmount.isNeg())
886
886
  ? 'short'
887
887
  : 'long';
@@ -992,7 +992,7 @@ class User {
992
992
  getWithdrawalLimit(marketIndex, reduceOnly) {
993
993
  const nowTs = new _1.BN(Math.floor(Date.now() / 1000));
994
994
  const spotMarket = this.driftClient.getSpotMarketAccount(marketIndex);
995
- const { borrowLimit, withdrawLimit } = (0, spotBalance_1.calculateWithdrawLimit)(spotMarket, nowTs);
995
+ const { borrowLimit, withdrawLimit } = spotBalance_1.calculateWithdrawLimit(spotMarket, nowTs);
996
996
  const freeCollateral = this.getFreeCollateral();
997
997
  const oracleData = this.getOracleDataForSpotMarket(marketIndex);
998
998
  const precisionIncrease = numericConstants_1.TEN.pow(new _1.BN(spotMarket.decimals - 6));
@@ -1002,10 +1002,10 @@ class User {
1002
1002
  .mul(numericConstants_1.PRICE_PRECISION)
1003
1003
  .div(oracleData.price)
1004
1004
  .mul(precisionIncrease);
1005
- const userSpotPosition = this.getUserAccount().spotPositions.find((spotPosition) => (0, types_1.isVariant)(spotPosition.balanceType, 'deposit') &&
1005
+ const userSpotPosition = this.getUserAccount().spotPositions.find((spotPosition) => types_1.isVariant(spotPosition.balanceType, 'deposit') &&
1006
1006
  spotPosition.marketIndex == marketIndex);
1007
1007
  const userSpotBalance = userSpotPosition
1008
- ? (0, spotBalance_1.getTokenAmount)(userSpotPosition.scaledBalance, this.driftClient.getSpotMarketAccount(marketIndex), _1.SpotBalanceType.DEPOSIT)
1008
+ ? spotBalance_1.getTokenAmount(userSpotPosition.scaledBalance, this.driftClient.getSpotMarketAccount(marketIndex), _1.SpotBalanceType.DEPOSIT)
1009
1009
  : numericConstants_1.ZERO;
1010
1010
  const maxWithdrawValue = _1.BN.min(_1.BN.min(amountWithdrawable, userSpotBalance), withdrawLimit.abs());
1011
1011
  if (reduceOnly) {
@@ -26,7 +26,7 @@ class UserMap {
26
26
  userAccountArray.push(programUserAccount.account);
27
27
  }
28
28
  if (this.accountSubscription.type === 'polling') {
29
- await (0, __1.bulkPollingUserSubscribe)(userArray, this.accountSubscription.accountLoader);
29
+ await __1.bulkPollingUserSubscribe(userArray, this.accountSubscription.accountLoader);
30
30
  }
31
31
  else {
32
32
  await Promise.all(userArray.map((user, i) => user.subscribe(userAccountArray[i])));
@@ -23,14 +23,14 @@ class UserStatsMap {
23
23
  }
24
24
  const chUserStat = new __1.UserStats({
25
25
  driftClient: this.driftClient,
26
- userStatsAccountPublicKey: (0, __1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, userStat.authority),
26
+ userStatsAccountPublicKey: __1.getUserStatsAccountPublicKey(this.driftClient.program.programId, userStat.authority),
27
27
  accountSubscription: this.accountSubscription,
28
28
  });
29
29
  userStatArray.push(chUserStat);
30
30
  userStatsAccountArray.push(userStat);
31
31
  }
32
32
  if (this.accountSubscription.type === 'polling') {
33
- await (0, __1.bulkPollingUserStatsSubscribe)(userStatArray, this.accountSubscription.accountLoader);
33
+ await __1.bulkPollingUserStatsSubscribe(userStatArray, this.accountSubscription.accountLoader);
34
34
  }
35
35
  else {
36
36
  await Promise.all(userStatArray.map((userStat, i) => userStat.subscribe(userStatsAccountArray[i])));
@@ -42,7 +42,7 @@ class UserStatsMap {
42
42
  async addUserStat(authority) {
43
43
  const userStat = new __1.UserStats({
44
44
  driftClient: this.driftClient,
45
- userStatsAccountPublicKey: (0, __1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, authority),
45
+ userStatsAccountPublicKey: __1.getUserStatsAccountPublicKey(this.driftClient.program.programId, authority),
46
46
  accountSubscription: this.accountSubscription,
47
47
  });
48
48
  await userStat.subscribe();
package/lib/userStats.js CHANGED
@@ -40,8 +40,8 @@ class UserStats {
40
40
  }
41
41
  else {
42
42
  return {
43
- referrer: (0, pda_1.getUserAccountPublicKeySync)(this.driftClient.program.programId, this.getAccount().referrer, 0),
44
- referrerStats: (0, pda_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, this.getAccount().referrer),
43
+ referrer: pda_1.getUserAccountPublicKeySync(this.driftClient.program.programId, this.getAccount().referrer, 0),
44
+ referrerStats: pda_1.getUserStatsAccountPublicKey(this.driftClient.program.programId, this.getAccount().referrer),
45
45
  };
46
46
  }
47
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.12.0-beta.2",
3
+ "version": "2.12.0-beta.3",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -22,6 +22,7 @@ import {
22
22
  OrderType,
23
23
  ReferrerInfo,
24
24
  MarketType,
25
+ TxParams,
25
26
  SerumV3FulfillmentConfigAccount,
26
27
  isVariant,
27
28
  } from './types';
@@ -2248,7 +2249,8 @@ export class DriftClient {
2248
2249
  order?: Order,
2249
2250
  fulfillmentConfig?: SerumV3FulfillmentConfigAccount,
2250
2251
  makerInfo?: MakerInfo,
2251
- referrerInfo?: ReferrerInfo
2252
+ referrerInfo?: ReferrerInfo,
2253
+ txParams?: TxParams
2252
2254
  ): Promise<TransactionSignature> {
2253
2255
  const { txSig } = await this.sendTransaction(
2254
2256
  wrapInTx(
@@ -2259,7 +2261,9 @@ export class DriftClient {
2259
2261
  fulfillmentConfig,
2260
2262
  makerInfo,
2261
2263
  referrerInfo
2262
- )
2264
+ ),
2265
+ txParams?.computeUnits,
2266
+ txParams?.computeUnitsPrice
2263
2267
  ),
2264
2268
  [],
2265
2269
  this.opts
@@ -2704,7 +2708,8 @@ export class DriftClient {
2704
2708
  orderParams: OptionalOrderParams,
2705
2709
  fulfillmentConfig?: SerumV3FulfillmentConfigAccount,
2706
2710
  makerInfo?: MakerInfo,
2707
- referrerInfo?: ReferrerInfo
2711
+ referrerInfo?: ReferrerInfo,
2712
+ txParams?: TxParams
2708
2713
  ): Promise<TransactionSignature> {
2709
2714
  const { txSig, slot } = await this.sendTransaction(
2710
2715
  wrapInTx(
@@ -2713,7 +2718,9 @@ export class DriftClient {
2713
2718
  fulfillmentConfig,
2714
2719
  makerInfo,
2715
2720
  referrerInfo
2716
- )
2721
+ ),
2722
+ txParams?.computeUnits,
2723
+ txParams?.computeUnitsPrice
2717
2724
  ),
2718
2725
  [],
2719
2726
  this.opts
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.12.0-beta.2",
2
+ "version": "2.12.0-beta.3",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/src/tx/utils.ts CHANGED
@@ -8,14 +8,22 @@ const COMPUTE_UNITS_DEFAULT = 200_000;
8
8
 
9
9
  export function wrapInTx(
10
10
  instruction: TransactionInstruction,
11
- computeUnits = 600_000 // TODO, requires less code change
11
+ computeUnits = 600_000,
12
+ computeUnitsPrice = 0
12
13
  ): Transaction {
13
14
  const tx = new Transaction();
14
15
  if (computeUnits != COMPUTE_UNITS_DEFAULT) {
15
16
  tx.add(
16
- ComputeBudgetProgram.requestUnits({
17
+ ComputeBudgetProgram.setComputeUnitLimit({
17
18
  units: computeUnits,
18
- additionalFee: 0,
19
+ })
20
+ );
21
+ }
22
+
23
+ if (computeUnitsPrice != 0) {
24
+ tx.add(
25
+ ComputeBudgetProgram.setComputeUnitPrice({
26
+ microLamports: computeUnitsPrice,
19
27
  })
20
28
  );
21
29
  }
package/src/types.ts CHANGED
@@ -909,6 +909,11 @@ export type ReferrerInfo = {
909
909
  referrerStats: PublicKey;
910
910
  };
911
911
 
912
+ export type TxParams = {
913
+ computeUnits?: number;
914
+ computeUnitsPrice?: number;
915
+ };
916
+
912
917
  // # Misc Types
913
918
  export interface IWallet {
914
919
  signTransaction(tx: Transaction): Promise<Transaction>;