@drift-labs/sdk 2.18.0 → 2.19.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.
Files changed (86) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +2 -1
  2. package/lib/accounts/bulkAccountLoader.js +3 -3
  3. package/lib/accounts/fetch.js +2 -2
  4. package/lib/accounts/pollingDriftClientAccountSubscriber.js +7 -7
  5. package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
  6. package/lib/accounts/pollingUserAccountSubscriber.js +2 -2
  7. package/lib/accounts/pollingUserStatsAccountSubscriber.js +2 -2
  8. package/lib/accounts/types.d.ts +5 -4
  9. package/lib/accounts/webSocketAccountSubscriber.js +1 -1
  10. package/lib/accounts/webSocketDriftClientAccountSubscriber.js +3 -3
  11. package/lib/addresses/marketAddresses.js +1 -1
  12. package/lib/addresses/pda.js +5 -1
  13. package/lib/adminClient.js +61 -57
  14. package/lib/config.d.ts +2 -2
  15. package/lib/constants/perpMarkets.d.ts +1 -1
  16. package/lib/constants/perpMarkets.js +33 -3
  17. package/lib/constants/spotMarkets.d.ts +1 -1
  18. package/lib/dlob/DLOB.d.ts +4 -4
  19. package/lib/dlob/DLOB.js +89 -76
  20. package/lib/dlob/DLOBNode.d.ts +2 -2
  21. package/lib/dlob/DLOBNode.js +7 -7
  22. package/lib/dlob/DLOBOrders.d.ts +2 -2
  23. package/lib/dlob/NodeList.d.ts +2 -2
  24. package/lib/dlob/NodeList.js +4 -4
  25. package/lib/driftClient.d.ts +1 -1
  26. package/lib/driftClient.js +99 -95
  27. package/lib/driftClientConfig.d.ts +3 -3
  28. package/lib/events/eventSubscriber.js +2 -2
  29. package/lib/events/fetchLogs.d.ts +2 -2
  30. package/lib/events/pollingLogProvider.js +1 -1
  31. package/lib/events/types.d.ts +14 -14
  32. package/lib/examples/loadDlob.js +2 -2
  33. package/lib/examples/makeTradeExample.js +9 -9
  34. package/lib/factory/bigNum.js +13 -13
  35. package/lib/factory/oracleClient.js +6 -3
  36. package/lib/idl/drift.json +7 -4
  37. package/lib/index.js +5 -1
  38. package/lib/math/amm.d.ts +1 -1
  39. package/lib/math/amm.js +33 -38
  40. package/lib/math/auction.js +6 -6
  41. package/lib/math/exchangeStatus.js +2 -2
  42. package/lib/math/funding.js +2 -2
  43. package/lib/math/margin.js +4 -4
  44. package/lib/math/market.js +15 -15
  45. package/lib/math/oracles.js +1 -1
  46. package/lib/math/orders.js +24 -24
  47. package/lib/math/position.js +5 -5
  48. package/lib/math/repeg.js +1 -1
  49. package/lib/math/spotBalance.js +9 -9
  50. package/lib/math/spotMarket.js +3 -3
  51. package/lib/math/spotPosition.js +3 -3
  52. package/lib/math/trade.d.ts +1 -1
  53. package/lib/math/trade.js +43 -43
  54. package/lib/math/utils.js +1 -1
  55. package/lib/oracles/oracleClientCache.js +1 -1
  56. package/lib/oracles/pythClient.js +1 -1
  57. package/lib/oracles/types.d.ts +2 -2
  58. package/lib/serum/serumSubscriber.d.ts +4 -3
  59. package/lib/serum/serumSubscriber.js +40 -11
  60. package/lib/serum/types.d.ts +3 -1
  61. package/lib/slot/SlotSubscriber.d.ts +1 -1
  62. package/lib/tokenFaucet.js +5 -1
  63. package/lib/tx/retryTxSender.d.ts +1 -1
  64. package/lib/tx/retryTxSender.js +1 -1
  65. package/lib/tx/types.d.ts +1 -1
  66. package/lib/types.d.ts +49 -46
  67. package/lib/types.js +2 -1
  68. package/lib/user.js +63 -63
  69. package/lib/userConfig.d.ts +2 -2
  70. package/lib/userMap/userMap.js +1 -1
  71. package/lib/userMap/userStatsMap.js +3 -3
  72. package/lib/userStats.js +2 -2
  73. package/lib/userStatsConfig.d.ts +2 -2
  74. package/package.json +1 -1
  75. package/src/constants/perpMarkets.ts +33 -3
  76. package/src/dlob/DLOB.ts +17 -5
  77. package/src/dlob/NodeList.ts +2 -5
  78. package/src/factory/oracleClient.ts +5 -1
  79. package/src/idl/drift.json +7 -4
  80. package/src/math/amm.ts +22 -23
  81. package/src/math/trade.ts +1 -1
  82. package/src/serum/serumSubscriber.ts +62 -20
  83. package/src/serum/types.ts +9 -5
  84. package/src/types.ts +2 -1
  85. package/tests/amm/test.ts +177 -5
  86. package/tests/dlob/test.ts +2 -1
@@ -20,7 +20,7 @@ async function calculateAllEstimatedFundingRate(market, oraclePriceData, periodA
20
20
  const secondsInHour = new anchor_1.BN(3600);
21
21
  const hoursInDay = new anchor_1.BN(24);
22
22
  const ONE = new anchor_1.BN(1);
23
- if (types_1.isVariant(market.status, 'uninitialized')) {
23
+ if ((0, types_1.isVariant)(market.status, 'uninitialized')) {
24
24
  return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
25
25
  }
26
26
  const payFreq = new anchor_1.BN(market.amm.fundingPeriod);
@@ -32,7 +32,7 @@ async function calculateAllEstimatedFundingRate(market, oraclePriceData, periodA
32
32
  const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
33
33
  const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
34
34
  const markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, anchor_1.BN.max(numericConstants_1.ZERO, secondsInHour.sub(timeSinceLastMarkChange)));
35
- const [bid, ask] = amm_1.calculateBidAskPrice(market.amm, oraclePriceData);
35
+ const [bid, ask] = (0, amm_1.calculateBidAskPrice)(market.amm, oraclePriceData);
36
36
  const baseAssetPriceWithMantissa = bid.add(ask).div(new anchor_1.BN(2));
37
37
  const markTwapWithMantissa = markTwapTimeSinceLastUpdate
38
38
  .mul(lastMarkTwapWithMantissa)
@@ -11,10 +11,10 @@ imfFactor, liabilityWeight, precision) {
11
11
  if (imfFactor.eq(numericConstants_1.ZERO)) {
12
12
  return liabilityWeight;
13
13
  }
14
- const sizeSqrt = utils_1.squareRootBN(size.abs().mul(new anchor_1.BN(10)).add(new anchor_1.BN(1))); //1e9 -> 1e10 -> 1e5
14
+ const sizeSqrt = (0, utils_1.squareRootBN)(size.abs().mul(new anchor_1.BN(10)).add(new anchor_1.BN(1))); //1e9 -> 1e10 -> 1e5
15
15
  const liabilityWeightNumerator = liabilityWeight.sub(liabilityWeight.div(new anchor_1.BN(5)));
16
16
  const denom = new anchor_1.BN(100000).mul(numericConstants_1.SPOT_MARKET_IMF_PRECISION).div(precision);
17
- assert_1.assert(denom.gt(numericConstants_1.ZERO));
17
+ (0, assert_1.assert)(denom.gt(numericConstants_1.ZERO));
18
18
  const sizePremiumLiabilityWeight = liabilityWeightNumerator.add(sizeSqrt // 1e5
19
19
  .mul(imfFactor)
20
20
  .div(denom) // 1e5
@@ -28,7 +28,7 @@ imfFactor, assetWeight) {
28
28
  if (imfFactor.eq(numericConstants_1.ZERO)) {
29
29
  return assetWeight;
30
30
  }
31
- const sizeSqrt = utils_1.squareRootBN(size.abs().mul(new anchor_1.BN(10)).add(new anchor_1.BN(1))); //1e9 -> 1e10 -> 1e5
31
+ const sizeSqrt = (0, utils_1.squareRootBN)(size.abs().mul(new anchor_1.BN(10)).add(new anchor_1.BN(1))); //1e9 -> 1e10 -> 1e5
32
32
  const imfNumerator = numericConstants_1.SPOT_MARKET_IMF_PRECISION.add(numericConstants_1.SPOT_MARKET_IMF_PRECISION.div(new anchor_1.BN(10)));
33
33
  const sizeDiscountAssetWeight = imfNumerator
34
34
  .mul(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
@@ -58,7 +58,7 @@ function calculateOraclePriceForPerpMargin(perpPosition, market, oraclePriceData
58
58
  exports.calculateOraclePriceForPerpMargin = calculateOraclePriceForPerpMargin;
59
59
  function calculateBaseAssetValueWithOracle(market, perpPosition, oraclePriceData, includeOpenOrders = false) {
60
60
  let price = oraclePriceData.price;
61
- if (types_1.isVariant(market.status, 'settlement')) {
61
+ if ((0, types_1.isVariant)(market.status, 'settlement')) {
62
62
  price = market.expiryPrice;
63
63
  }
64
64
  const baseAssetAmount = includeOpenOrders
@@ -15,8 +15,8 @@ const assert_1 = require("../assert/assert");
15
15
  * @return markPrice : Precision PRICE_PRECISION
16
16
  */
17
17
  function calculateReservePrice(market, oraclePriceData) {
18
- const newAmm = amm_1.calculateUpdatedAMM(market.amm, oraclePriceData);
19
- return amm_1.calculatePrice(newAmm.baseAssetReserve, newAmm.quoteAssetReserve, newAmm.pegMultiplier);
18
+ const newAmm = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
19
+ return (0, amm_1.calculatePrice)(newAmm.baseAssetReserve, newAmm.quoteAssetReserve, newAmm.pegMultiplier);
20
20
  }
21
21
  exports.calculateReservePrice = calculateReservePrice;
22
22
  /**
@@ -26,8 +26,8 @@ exports.calculateReservePrice = calculateReservePrice;
26
26
  * @return bidPrice : Precision PRICE_PRECISION
27
27
  */
28
28
  function calculateBidPrice(market, oraclePriceData) {
29
- const { baseAssetReserve, quoteAssetReserve, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, types_1.PositionDirection.SHORT, oraclePriceData);
30
- return amm_1.calculatePrice(baseAssetReserve, quoteAssetReserve, newPeg);
29
+ const { baseAssetReserve, quoteAssetReserve, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, types_1.PositionDirection.SHORT, oraclePriceData);
30
+ return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, newPeg);
31
31
  }
32
32
  exports.calculateBidPrice = calculateBidPrice;
33
33
  /**
@@ -37,12 +37,12 @@ exports.calculateBidPrice = calculateBidPrice;
37
37
  * @return askPrice : Precision PRICE_PRECISION
38
38
  */
39
39
  function calculateAskPrice(market, oraclePriceData) {
40
- const { baseAssetReserve, quoteAssetReserve, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, types_1.PositionDirection.LONG, oraclePriceData);
41
- return amm_1.calculatePrice(baseAssetReserve, quoteAssetReserve, newPeg);
40
+ const { baseAssetReserve, quoteAssetReserve, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, types_1.PositionDirection.LONG, oraclePriceData);
41
+ return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, newPeg);
42
42
  }
43
43
  exports.calculateAskPrice = calculateAskPrice;
44
44
  function calculateNewMarketAfterTrade(baseAssetAmount, direction, market) {
45
- const [newQuoteAssetReserve, newBaseAssetReserve] = amm_1.calculateAmmReservesAfterSwap(market.amm, 'base', baseAssetAmount.abs(), amm_1.getSwapDirection('base', direction));
45
+ const [newQuoteAssetReserve, newBaseAssetReserve] = (0, amm_1.calculateAmmReservesAfterSwap)(market.amm, 'base', baseAssetAmount.abs(), (0, amm_1.getSwapDirection)('base', direction));
46
46
  const newAmm = Object.assign({}, market.amm);
47
47
  const newMarket = Object.assign({}, market);
48
48
  newMarket.amm = newAmm;
@@ -64,11 +64,11 @@ function calculateMarketMarginRatio(market, size, marginCategory) {
64
64
  let marginRatio;
65
65
  switch (marginCategory) {
66
66
  case 'Initial': {
67
- marginRatio = margin_1.calculateSizePremiumLiabilityWeight(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(market.marginRatioInitial), numericConstants_1.MARGIN_PRECISION).toNumber();
67
+ marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(market.marginRatioInitial), numericConstants_1.MARGIN_PRECISION).toNumber();
68
68
  break;
69
69
  }
70
70
  case 'Maintenance': {
71
- marginRatio = margin_1.calculateSizePremiumLiabilityWeight(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(market.marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
71
+ marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(market.marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
72
72
  break;
73
73
  }
74
74
  }
@@ -88,7 +88,7 @@ function calculateUnrealizedAssetWeight(market, quoteSpotMarket, unrealizedPnl,
88
88
  .div(netUnsettledPnl);
89
89
  }
90
90
  }
91
- assetWeight = margin_1.calculateSizeDiscountAssetWeight(unrealizedPnl, new anchor_1.BN(market.unrealizedPnlImfFactor), assetWeight);
91
+ assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(unrealizedPnl, new anchor_1.BN(market.unrealizedPnlImfFactor), assetWeight);
92
92
  break;
93
93
  case 'Maintenance':
94
94
  assetWeight = new anchor_1.BN(market.unrealizedPnlMaintenanceAssetWeight);
@@ -98,14 +98,14 @@ function calculateUnrealizedAssetWeight(market, quoteSpotMarket, unrealizedPnl,
98
98
  }
99
99
  exports.calculateUnrealizedAssetWeight = calculateUnrealizedAssetWeight;
100
100
  function calculateMarketAvailablePNL(perpMarket, spotMarket) {
101
- return spotBalance_1.getTokenAmount(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
101
+ return (0, spotBalance_1.getTokenAmount)(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
102
102
  }
103
103
  exports.calculateMarketAvailablePNL = calculateMarketAvailablePNL;
104
104
  function calculateMarketMaxAvailableInsurance(perpMarket, spotMarket) {
105
- assert_1.assert(spotMarket.marketIndex == numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
105
+ (0, assert_1.assert)(spotMarket.marketIndex == numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
106
106
  // todo: insuranceFundAllocation technically not guaranteed to be in Insurance Fund
107
107
  const insuranceFundAllocation = perpMarket.insuranceClaim.quoteMaxInsurance.sub(perpMarket.insuranceClaim.quoteSettledInsurance);
108
- const ammFeePool = spotBalance_1.getTokenAmount(perpMarket.amm.feePool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
108
+ const ammFeePool = (0, spotBalance_1.getTokenAmount)(perpMarket.amm.feePool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
109
109
  return insuranceFundAllocation.add(ammFeePool);
110
110
  }
111
111
  exports.calculateMarketMaxAvailableInsurance = calculateMarketMaxAvailableInsurance;
@@ -121,13 +121,13 @@ function calculateNetUserPnl(perpMarket, oraclePriceData) {
121
121
  exports.calculateNetUserPnl = calculateNetUserPnl;
122
122
  function calculateNetUserPnlImbalance(perpMarket, spotMarket, oraclePriceData) {
123
123
  const netUserPnl = calculateNetUserPnl(perpMarket, oraclePriceData);
124
- const pnlPool = spotBalance_1.getTokenAmount(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
124
+ const pnlPool = (0, spotBalance_1.getTokenAmount)(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
125
125
  const imbalance = netUserPnl.sub(pnlPool);
126
126
  return imbalance;
127
127
  }
128
128
  exports.calculateNetUserPnlImbalance = calculateNetUserPnlImbalance;
129
129
  function calculateAvailablePerpLiquidity(market, oraclePriceData, dlob, slot) {
130
- let [bids, asks] = amm_1.calculateMarketOpenBidAsk(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
130
+ let [bids, asks] = (0, amm_1.calculateMarketOpenBidAsk)(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
131
131
  asks = asks.abs();
132
132
  const bidPrice = calculateBidPrice(market, oraclePriceData);
133
133
  const askPrice = calculateAskPrice(market, oraclePriceData);
@@ -9,7 +9,7 @@ function oraclePriceBands(market, oraclePriceData) {
9
9
  const offset = oraclePriceData.price
10
10
  .mul(new index_1.BN(maxPercentDiff))
11
11
  .div(numericConstants_1.MARGIN_PRECISION);
12
- assert_1.assert(offset.gt(numericConstants_1.ZERO));
12
+ (0, assert_1.assert)(offset.gt(numericConstants_1.ZERO));
13
13
  return [oraclePriceData.price.sub(offset), oraclePriceData.price.add(offset)];
14
14
  }
15
15
  exports.oraclePriceBands = oraclePriceBands;
@@ -7,7 +7,7 @@ const anchor_1 = require("@project-serum/anchor");
7
7
  const auction_1 = require("./auction");
8
8
  const amm_1 = require("./amm");
9
9
  function isOrderRiskIncreasing(user, order) {
10
- if (types_1.isVariant(order.status, 'init')) {
10
+ if ((0, types_1.isVariant)(order.status, 'init')) {
11
11
  return false;
12
12
  }
13
13
  const position = user.getPerpPosition(order.marketIndex) ||
@@ -17,12 +17,12 @@ function isOrderRiskIncreasing(user, order) {
17
17
  return true;
18
18
  }
19
19
  // if position is long and order is long
20
- if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && types_1.isVariant(order.direction, 'long')) {
20
+ if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && (0, types_1.isVariant)(order.direction, 'long')) {
21
21
  return true;
22
22
  }
23
23
  // if position is short and order is short
24
24
  if (position.baseAssetAmount.lt(numericConstants_1.ZERO) &&
25
- types_1.isVariant(order.direction, 'short')) {
25
+ (0, types_1.isVariant)(order.direction, 'short')) {
26
26
  return true;
27
27
  }
28
28
  const baseAssetAmountToFill = order.baseAssetAmount.sub(order.baseAssetAmountFilled);
@@ -34,7 +34,7 @@ function isOrderRiskIncreasing(user, order) {
34
34
  }
35
35
  exports.isOrderRiskIncreasing = isOrderRiskIncreasing;
36
36
  function isOrderRiskIncreasingInSameDirection(user, order) {
37
- if (types_1.isVariant(order.status, 'init')) {
37
+ if ((0, types_1.isVariant)(order.status, 'init')) {
38
38
  return false;
39
39
  }
40
40
  const position = user.getPerpPosition(order.marketIndex) ||
@@ -44,31 +44,31 @@ function isOrderRiskIncreasingInSameDirection(user, order) {
44
44
  return true;
45
45
  }
46
46
  // if position is long and order is long
47
- if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && types_1.isVariant(order.direction, 'long')) {
47
+ if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && (0, types_1.isVariant)(order.direction, 'long')) {
48
48
  return true;
49
49
  }
50
50
  // if position is short and order is short
51
51
  if (position.baseAssetAmount.lt(numericConstants_1.ZERO) &&
52
- types_1.isVariant(order.direction, 'short')) {
52
+ (0, types_1.isVariant)(order.direction, 'short')) {
53
53
  return true;
54
54
  }
55
55
  return false;
56
56
  }
57
57
  exports.isOrderRiskIncreasingInSameDirection = isOrderRiskIncreasingInSameDirection;
58
58
  function isOrderReduceOnly(user, order) {
59
- if (types_1.isVariant(order.status, 'init')) {
59
+ if ((0, types_1.isVariant)(order.status, 'init')) {
60
60
  return false;
61
61
  }
62
62
  const position = user.getPerpPosition(order.marketIndex) ||
63
63
  user.getEmptyPosition(order.marketIndex);
64
64
  // if position is long and order is long
65
65
  if (position.baseAssetAmount.gte(numericConstants_1.ZERO) &&
66
- types_1.isVariant(order.direction, 'long')) {
66
+ (0, types_1.isVariant)(order.direction, 'long')) {
67
67
  return false;
68
68
  }
69
69
  // if position is short and order is short
70
70
  if (position.baseAssetAmount.lte(numericConstants_1.ZERO) &&
71
- types_1.isVariant(order.direction, 'short')) {
71
+ (0, types_1.isVariant)(order.direction, 'short')) {
72
72
  return false;
73
73
  }
74
74
  return true;
@@ -82,7 +82,7 @@ exports.standardizeBaseAssetAmount = standardizeBaseAssetAmount;
82
82
  function getLimitPrice(order, oraclePriceData, slot, fallbackPrice) {
83
83
  let limitPrice;
84
84
  if (hasAuctionPrice(order, slot)) {
85
- limitPrice = auction_1.getAuctionPrice(order, slot, oraclePriceData.price);
85
+ limitPrice = (0, auction_1.getAuctionPrice)(order, slot, oraclePriceData.price);
86
86
  }
87
87
  else if (order.oraclePriceOffset !== 0) {
88
88
  limitPrice = oraclePriceData.price.add(new anchor_1.BN(order.oraclePriceOffset));
@@ -99,16 +99,16 @@ exports.getLimitPrice = getLimitPrice;
99
99
  function hasLimitPrice(order, slot) {
100
100
  return (order.price.gt(numericConstants_1.ZERO) ||
101
101
  order.oraclePriceOffset != 0 ||
102
- !auction_1.isAuctionComplete(order, slot));
102
+ !(0, auction_1.isAuctionComplete)(order, slot));
103
103
  }
104
104
  exports.hasLimitPrice = hasLimitPrice;
105
105
  function hasAuctionPrice(order, slot) {
106
- return (!auction_1.isAuctionComplete(order, slot) &&
106
+ return (!(0, auction_1.isAuctionComplete)(order, slot) &&
107
107
  (!order.auctionStartPrice.eq(numericConstants_1.ZERO) || !order.auctionEndPrice.eq(numericConstants_1.ZERO)));
108
108
  }
109
109
  exports.hasAuctionPrice = hasAuctionPrice;
110
110
  function isFillableByVAMM(order, market, oraclePriceData, slot, ts) {
111
- return ((auction_1.isAuctionComplete(order, slot) &&
111
+ return (((0, auction_1.isAuctionComplete)(order, slot) &&
112
112
  calculateBaseAssetAmountForAmmToFulfill(order, market, oraclePriceData, slot).gte(market.amm.minOrderSize)) ||
113
113
  isOrderExpired(order, ts));
114
114
  }
@@ -119,19 +119,19 @@ function calculateBaseAssetAmountForAmmToFulfill(order, market, oraclePriceData,
119
119
  }
120
120
  const limitPrice = getLimitPrice(order, oraclePriceData, slot);
121
121
  let baseAssetAmount;
122
- const updatedAMM = amm_1.calculateUpdatedAMM(market.amm, oraclePriceData);
122
+ const updatedAMM = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
123
123
  if (limitPrice !== undefined) {
124
124
  baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(order, updatedAMM, limitPrice, oraclePriceData);
125
125
  }
126
126
  else {
127
127
  baseAssetAmount = order.baseAssetAmount.sub(order.baseAssetAmountFilled);
128
128
  }
129
- const maxBaseAssetAmount = amm_1.calculateMaxBaseAssetAmountFillable(updatedAMM, order.direction);
129
+ const maxBaseAssetAmount = (0, amm_1.calculateMaxBaseAssetAmountFillable)(updatedAMM, order.direction);
130
130
  return anchor_1.BN.min(maxBaseAssetAmount, baseAssetAmount);
131
131
  }
132
132
  exports.calculateBaseAssetAmountForAmmToFulfill = calculateBaseAssetAmountForAmmToFulfill;
133
133
  function calculateBaseAssetAmountToFillUpToLimitPrice(order, amm, limitPrice, oraclePriceData) {
134
- const [maxAmountToTrade, direction] = amm_1.calculateMaxBaseAssetAmountToTrade(amm, limitPrice, order.direction, oraclePriceData);
134
+ const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(amm, limitPrice, order.direction, oraclePriceData);
135
135
  const baseAssetAmount = standardizeBaseAssetAmount(maxAmountToTrade, amm.orderStepSize);
136
136
  // Check that directions are the same
137
137
  const sameDirection = isSameDirection(direction, order.direction);
@@ -145,12 +145,12 @@ function calculateBaseAssetAmountToFillUpToLimitPrice(order, amm, limitPrice, or
145
145
  }
146
146
  exports.calculateBaseAssetAmountToFillUpToLimitPrice = calculateBaseAssetAmountToFillUpToLimitPrice;
147
147
  function isSameDirection(firstDirection, secondDirection) {
148
- return ((types_1.isVariant(firstDirection, 'long') && types_1.isVariant(secondDirection, 'long')) ||
149
- (types_1.isVariant(firstDirection, 'short') && types_1.isVariant(secondDirection, 'short')));
148
+ return (((0, types_1.isVariant)(firstDirection, 'long') && (0, types_1.isVariant)(secondDirection, 'long')) ||
149
+ ((0, types_1.isVariant)(firstDirection, 'short') && (0, types_1.isVariant)(secondDirection, 'short')));
150
150
  }
151
151
  function isOrderExpired(order, ts) {
152
152
  if (mustBeTriggered(order) ||
153
- !types_1.isVariant(order.status, 'open') ||
153
+ !(0, types_1.isVariant)(order.status, 'open') ||
154
154
  order.maxTs.eq(numericConstants_1.ZERO)) {
155
155
  return false;
156
156
  }
@@ -158,26 +158,26 @@ function isOrderExpired(order, ts) {
158
158
  }
159
159
  exports.isOrderExpired = isOrderExpired;
160
160
  function isMarketOrder(order) {
161
- return types_1.isOneOfVariant(order.orderType, ['market', 'triggerMarket', 'oracle']);
161
+ return (0, types_1.isOneOfVariant)(order.orderType, ['market', 'triggerMarket', 'oracle']);
162
162
  }
163
163
  exports.isMarketOrder = isMarketOrder;
164
164
  function isLimitOrder(order) {
165
- return types_1.isOneOfVariant(order.orderType, ['limit', 'triggerLimit']);
165
+ return (0, types_1.isOneOfVariant)(order.orderType, ['limit', 'triggerLimit']);
166
166
  }
167
167
  exports.isLimitOrder = isLimitOrder;
168
168
  function mustBeTriggered(order) {
169
- return types_1.isOneOfVariant(order.orderType, ['triggerMarket', 'triggerLimit']);
169
+ return (0, types_1.isOneOfVariant)(order.orderType, ['triggerMarket', 'triggerLimit']);
170
170
  }
171
171
  exports.mustBeTriggered = mustBeTriggered;
172
172
  function isTriggered(order) {
173
- return types_1.isOneOfVariant(order.triggerCondition, [
173
+ return (0, types_1.isOneOfVariant)(order.triggerCondition, [
174
174
  'triggeredAbove',
175
175
  'triggeredBelow',
176
176
  ]);
177
177
  }
178
178
  exports.isTriggered = isTriggered;
179
179
  function isRestingLimitOrder(order, slot) {
180
- return (isLimitOrder(order) && (order.postOnly || auction_1.isAuctionComplete(order, slot)));
180
+ return (isLimitOrder(order) && (order.postOnly || (0, auction_1.isAuctionComplete)(order, slot)));
181
181
  }
182
182
  exports.isRestingLimitOrder = isRestingLimitOrder;
183
183
  function isTakingOrder(order, slot) {
@@ -23,7 +23,7 @@ function calculateBaseAssetValue(market, userPosition, oraclePriceData, useSprea
23
23
  let prepegAmm;
24
24
  if (!skipUpdate) {
25
25
  if (market.amm.baseSpread > 0 && useSpread) {
26
- const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, directionToClose, oraclePriceData);
26
+ const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, directionToClose, oraclePriceData);
27
27
  prepegAmm = {
28
28
  baseAssetReserve,
29
29
  quoteAssetReserve,
@@ -32,13 +32,13 @@ function calculateBaseAssetValue(market, userPosition, oraclePriceData, useSprea
32
32
  };
33
33
  }
34
34
  else {
35
- prepegAmm = amm_1.calculateUpdatedAMM(market.amm, oraclePriceData);
35
+ prepegAmm = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
36
36
  }
37
37
  }
38
38
  else {
39
39
  prepegAmm = market.amm;
40
40
  }
41
- const [newQuoteAssetReserve, _] = amm_1.calculateAmmReservesAfterSwap(prepegAmm, 'base', userPosition.baseAssetAmount.abs(), amm_1.getSwapDirection('base', directionToClose));
41
+ const [newQuoteAssetReserve, _] = (0, amm_1.calculateAmmReservesAfterSwap)(prepegAmm, 'base', userPosition.baseAssetAmount.abs(), (0, amm_1.getSwapDirection)('base', directionToClose));
42
42
  switch (directionToClose) {
43
43
  case types_1.PositionDirection.SHORT:
44
44
  return prepegAmm.quoteAssetReserve
@@ -67,7 +67,7 @@ function calculatePositionPNL(market, perpPosition, withFunding = false, oracleP
67
67
  if (perpPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
68
68
  return perpPosition.quoteAssetAmount;
69
69
  }
70
- const baseAssetValue = margin_1.calculateBaseAssetValueWithOracle(market, perpPosition, oraclePriceData);
70
+ const baseAssetValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, perpPosition, oraclePriceData);
71
71
  const baseAssetValueSign = perpPosition.baseAssetAmount.isNeg()
72
72
  ? new __1.BN(-1)
73
73
  : new __1.BN(1);
@@ -86,7 +86,7 @@ function calculateClaimablePnl(market, spotMarket, perpPosition, oraclePriceData
86
86
  const fundingPnL = calculatePositionFundingPNL(market, perpPosition);
87
87
  let unsettledPnl = unrealizedPnl.add(fundingPnL);
88
88
  if (unrealizedPnl.gt(numericConstants_1.ZERO)) {
89
- const excessPnlPool = __1.BN.max(numericConstants_1.ZERO, market_1.calculateNetUserPnlImbalance(market, spotMarket, oraclePriceData).mul(new __1.BN(-1)));
89
+ const excessPnlPool = __1.BN.max(numericConstants_1.ZERO, (0, market_1.calculateNetUserPnlImbalance)(market, spotMarket, oraclePriceData).mul(new __1.BN(-1)));
90
90
  const maxPositivePnl = __1.BN.max(perpPosition.quoteAssetAmount.sub(perpPosition.quoteEntryAmount), numericConstants_1.ZERO).add(excessPnlPool);
91
91
  unsettledPnl = __1.BN.min(maxPositivePnl, unrealizedPnl);
92
92
  }
package/lib/math/repeg.js CHANGED
@@ -77,7 +77,7 @@ function calculateRepegCost(amm, newPeg) {
77
77
  }
78
78
  exports.calculateRepegCost = calculateRepegCost;
79
79
  function calculateBudgetedKBN(x, y, budget, Q, d) {
80
- assert_1.assert(Q.gt(new anchor_1.BN(0)));
80
+ (0, assert_1.assert)(Q.gt(new anchor_1.BN(0)));
81
81
  const C = budget.mul(new anchor_1.BN(-1));
82
82
  let dSign = new anchor_1.BN(1);
83
83
  if (d.lt(new anchor_1.BN(0))) {
@@ -9,11 +9,11 @@ const numericConstants_2 = require("../constants/numericConstants");
9
9
  const utils_1 = require("./utils");
10
10
  function getBalance(tokenAmount, spotMarket, balanceType) {
11
11
  const precisionIncrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
12
- const cumulativeInterest = types_1.isVariant(balanceType, 'deposit')
12
+ const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
13
13
  ? spotMarket.cumulativeDepositInterest
14
14
  : spotMarket.cumulativeBorrowInterest;
15
15
  let balance = tokenAmount.mul(precisionIncrease).div(cumulativeInterest);
16
- if (!balance.eq(numericConstants_1.ZERO) && types_1.isVariant(balanceType, 'borrow')) {
16
+ if (!balance.eq(numericConstants_1.ZERO) && (0, types_1.isVariant)(balanceType, 'borrow')) {
17
17
  balance = balance.add(numericConstants_1.ONE);
18
18
  }
19
19
  return balance;
@@ -21,18 +21,18 @@ function getBalance(tokenAmount, spotMarket, balanceType) {
21
21
  exports.getBalance = getBalance;
22
22
  function getTokenAmount(balanceAmount, spotMarket, balanceType) {
23
23
  const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
24
- if (types_1.isVariant(balanceType, 'deposit')) {
24
+ if ((0, types_1.isVariant)(balanceType, 'deposit')) {
25
25
  return balanceAmount
26
26
  .mul(spotMarket.cumulativeDepositInterest)
27
27
  .div(precisionDecrease);
28
28
  }
29
29
  else {
30
- return utils_1.divCeil(balanceAmount.mul(spotMarket.cumulativeBorrowInterest), precisionDecrease);
30
+ return (0, utils_1.divCeil)(balanceAmount.mul(spotMarket.cumulativeBorrowInterest), precisionDecrease);
31
31
  }
32
32
  }
33
33
  exports.getTokenAmount = getTokenAmount;
34
34
  function getSignedTokenAmount(tokenAmount, balanceType) {
35
- if (types_1.isVariant(balanceType, 'deposit')) {
35
+ if ((0, types_1.isVariant)(balanceType, 'deposit')) {
36
36
  return tokenAmount;
37
37
  }
38
38
  else {
@@ -77,10 +77,10 @@ function calculateAssetWeight(balanceAmount, spotMarket, marginCategory) {
77
77
  let assetWeight;
78
78
  switch (marginCategory) {
79
79
  case 'Initial':
80
- assetWeight = margin_1.calculateSizeDiscountAssetWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialAssetWeight));
80
+ assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialAssetWeight));
81
81
  break;
82
82
  case 'Maintenance':
83
- assetWeight = margin_1.calculateSizeDiscountAssetWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceAssetWeight));
83
+ assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceAssetWeight));
84
84
  break;
85
85
  default:
86
86
  assetWeight = new anchor_1.BN(spotMarket.initialAssetWeight);
@@ -103,10 +103,10 @@ function calculateLiabilityWeight(size, spotMarket, marginCategory) {
103
103
  let liabilityWeight;
104
104
  switch (marginCategory) {
105
105
  case 'Initial':
106
- liabilityWeight = margin_1.calculateSizePremiumLiabilityWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
106
+ liabilityWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
107
107
  break;
108
108
  case 'Maintenance':
109
- liabilityWeight = margin_1.calculateSizePremiumLiabilityWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
109
+ liabilityWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
110
110
  break;
111
111
  default:
112
112
  liabilityWeight = spotMarket.initialLiabilityWeight;
@@ -10,12 +10,12 @@ function castNumberToSpotPrecision(value, spotMarket) {
10
10
  }
11
11
  exports.castNumberToSpotPrecision = castNumberToSpotPrecision;
12
12
  function calculateSpotMarketMarginRatio(market, marginCategory, size, balanceType) {
13
- if (types_1.isVariant(balanceType, 'deposit')) {
14
- const assetWeight = spotBalance_1.calculateAssetWeight(size, market, marginCategory);
13
+ if ((0, types_1.isVariant)(balanceType, 'deposit')) {
14
+ const assetWeight = (0, spotBalance_1.calculateAssetWeight)(size, market, marginCategory);
15
15
  return numericConstants_1.MARGIN_PRECISION.sub(assetWeight).toNumber();
16
16
  }
17
17
  else {
18
- const liabilityWeight = spotBalance_1.calculateLiabilityWeight(size, market, marginCategory);
18
+ const liabilityWeight = (0, spotBalance_1.calculateLiabilityWeight)(size, market, marginCategory);
19
19
  return liabilityWeight.sub(numericConstants_1.MARGIN_PRECISION).toNumber();
20
20
  }
21
21
  }
@@ -8,15 +8,15 @@ function isSpotPositionAvailable(position) {
8
8
  }
9
9
  exports.isSpotPositionAvailable = isSpotPositionAvailable;
10
10
  function getWorstCaseTokenAmounts(spotPosition, spotMarketAccount, oraclePriceData) {
11
- const tokenAmount = spotBalance_1.getSignedTokenAmount(spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType), spotPosition.balanceType);
11
+ const tokenAmount = (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType), spotPosition.balanceType);
12
12
  const tokenAmountAllBidsFill = tokenAmount.add(spotPosition.openBids);
13
13
  const tokenAmountAllAsksFill = tokenAmount.add(spotPosition.openAsks);
14
14
  if (tokenAmountAllBidsFill.abs().gt(tokenAmountAllAsksFill.abs())) {
15
- const worstCaseQuoteTokenAmount = spotBalance_1.getTokenValue(spotPosition.openBids.neg(), spotMarketAccount.decimals, oraclePriceData);
15
+ const worstCaseQuoteTokenAmount = (0, spotBalance_1.getTokenValue)(spotPosition.openBids.neg(), spotMarketAccount.decimals, oraclePriceData);
16
16
  return [tokenAmountAllBidsFill, worstCaseQuoteTokenAmount];
17
17
  }
18
18
  else {
19
- const worstCaseQuoteTokenAmount = spotBalance_1.getTokenValue(spotPosition.openAsks.neg(), spotMarketAccount.decimals, oraclePriceData);
19
+ const worstCaseQuoteTokenAmount = (0, spotBalance_1.getTokenValue)(spotPosition.openAsks.neg(), spotMarketAccount.decimals, oraclePriceData);
20
20
  return [tokenAmountAllAsksFill, worstCaseQuoteTokenAmount];
21
21
  }
22
22
  }
@@ -6,7 +6,7 @@ import { OraclePriceData } from '../oracles/types';
6
6
  import { DLOB } from '../dlob/DLOB';
7
7
  import { PublicKey } from '@solana/web3.js';
8
8
  import { Orderbook } from '@project-serum/serum';
9
- export declare type PriceImpactUnit = 'entryPrice' | 'maxPrice' | 'priceDelta' | 'priceDeltaAsNumber' | 'pctAvg' | 'pctMax' | 'quoteAssetAmount' | 'quoteAssetAmountPeg' | 'acquiredBaseAssetAmount' | 'acquiredQuoteAssetAmount' | 'all';
9
+ export type PriceImpactUnit = 'entryPrice' | 'maxPrice' | 'priceDelta' | 'priceDeltaAsNumber' | 'pctAvg' | 'pctMax' | 'quoteAssetAmount' | 'quoteAssetAmountPeg' | 'acquiredBaseAssetAmount' | 'acquiredQuoteAssetAmount' | 'all';
10
10
  /**
11
11
  * Calculates avg/max slippage (price impact) for candidate trade
12
12
  *