@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
@@ -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 ((0, types_1.isVariant)(order.status, 'init')) {
10
+ if (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) && (0, types_1.isVariant)(order.direction, 'long')) {
20
+ if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && 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
- (0, types_1.isVariant)(order.direction, 'short')) {
25
+ 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 ((0, types_1.isVariant)(order.status, 'init')) {
37
+ if (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) && (0, types_1.isVariant)(order.direction, 'long')) {
47
+ if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && 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
- (0, types_1.isVariant)(order.direction, 'short')) {
52
+ 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 ((0, types_1.isVariant)(order.status, 'init')) {
59
+ if (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
- (0, types_1.isVariant)(order.direction, 'long')) {
66
+ 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
- (0, types_1.isVariant)(order.direction, 'short')) {
71
+ 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 = (0, auction_1.getAuctionPrice)(order, slot, oraclePriceData.price);
85
+ limitPrice = 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,15 +99,15 @@ exports.getLimitPrice = getLimitPrice;
99
99
  function hasLimitPrice(order, slot) {
100
100
  return (order.price.gt(numericConstants_1.ZERO) ||
101
101
  order.oraclePriceOffset != 0 ||
102
- !(0, auction_1.isAuctionComplete)(order, slot));
102
+ !auction_1.isAuctionComplete(order, slot));
103
103
  }
104
104
  exports.hasLimitPrice = hasLimitPrice;
105
105
  function hasAuctionPrice(order, slot) {
106
- return isMarketOrder(order) && !(0, auction_1.isAuctionComplete)(order, slot);
106
+ return isMarketOrder(order) && !auction_1.isAuctionComplete(order, slot);
107
107
  }
108
108
  exports.hasAuctionPrice = hasAuctionPrice;
109
109
  function isFillableByVAMM(order, market, oraclePriceData, slot, ts) {
110
- return (((0, auction_1.isAuctionComplete)(order, slot) &&
110
+ return ((auction_1.isAuctionComplete(order, slot) &&
111
111
  !calculateBaseAssetAmountForAmmToFulfill(order, market, oraclePriceData, slot).eq(numericConstants_1.ZERO)) ||
112
112
  isOrderExpired(order, ts));
113
113
  }
@@ -118,19 +118,19 @@ function calculateBaseAssetAmountForAmmToFulfill(order, market, oraclePriceData,
118
118
  }
119
119
  const limitPrice = getLimitPrice(order, oraclePriceData, slot);
120
120
  let baseAssetAmount;
121
- const updatedAMM = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
121
+ const updatedAMM = amm_1.calculateUpdatedAMM(market.amm, oraclePriceData);
122
122
  if (limitPrice !== undefined) {
123
123
  baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(order, updatedAMM, limitPrice, oraclePriceData);
124
124
  }
125
125
  else {
126
126
  baseAssetAmount = order.baseAssetAmount.sub(order.baseAssetAmountFilled);
127
127
  }
128
- const maxBaseAssetAmount = (0, amm_1.calculateMaxBaseAssetAmountFillable)(updatedAMM, order.direction);
128
+ const maxBaseAssetAmount = amm_1.calculateMaxBaseAssetAmountFillable(updatedAMM, order.direction);
129
129
  return anchor_1.BN.min(maxBaseAssetAmount, baseAssetAmount);
130
130
  }
131
131
  exports.calculateBaseAssetAmountForAmmToFulfill = calculateBaseAssetAmountForAmmToFulfill;
132
132
  function calculateBaseAssetAmountToFillUpToLimitPrice(order, amm, limitPrice, oraclePriceData) {
133
- const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(amm, limitPrice, order.direction, oraclePriceData);
133
+ const [maxAmountToTrade, direction] = amm_1.calculateMaxBaseAssetAmountToTrade(amm, limitPrice, order.direction, oraclePriceData);
134
134
  const baseAssetAmount = standardizeBaseAssetAmount(maxAmountToTrade, amm.orderStepSize);
135
135
  // Check that directions are the same
136
136
  const sameDirection = isSameDirection(direction, order.direction);
@@ -144,12 +144,12 @@ function calculateBaseAssetAmountToFillUpToLimitPrice(order, amm, limitPrice, or
144
144
  }
145
145
  exports.calculateBaseAssetAmountToFillUpToLimitPrice = calculateBaseAssetAmountToFillUpToLimitPrice;
146
146
  function isSameDirection(firstDirection, secondDirection) {
147
- return (((0, types_1.isVariant)(firstDirection, 'long') && (0, types_1.isVariant)(secondDirection, 'long')) ||
148
- ((0, types_1.isVariant)(firstDirection, 'short') && (0, types_1.isVariant)(secondDirection, 'short')));
147
+ return ((types_1.isVariant(firstDirection, 'long') && types_1.isVariant(secondDirection, 'long')) ||
148
+ (types_1.isVariant(firstDirection, 'short') && types_1.isVariant(secondDirection, 'short')));
149
149
  }
150
150
  function isOrderExpired(order, ts) {
151
151
  if (mustBeTriggered(order) ||
152
- !(0, types_1.isVariant)(order.status, 'open') ||
152
+ !types_1.isVariant(order.status, 'open') ||
153
153
  order.maxTs.eq(numericConstants_1.ZERO)) {
154
154
  return false;
155
155
  }
@@ -157,19 +157,19 @@ function isOrderExpired(order, ts) {
157
157
  }
158
158
  exports.isOrderExpired = isOrderExpired;
159
159
  function isMarketOrder(order) {
160
- return (0, types_1.isOneOfVariant)(order.orderType, ['market', 'triggerMarket', 'oracle']);
160
+ return types_1.isOneOfVariant(order.orderType, ['market', 'triggerMarket', 'oracle']);
161
161
  }
162
162
  exports.isMarketOrder = isMarketOrder;
163
163
  function isLimitOrder(order) {
164
- return (0, types_1.isOneOfVariant)(order.orderType, ['limit', 'triggerLimit']);
164
+ return types_1.isOneOfVariant(order.orderType, ['limit', 'triggerLimit']);
165
165
  }
166
166
  exports.isLimitOrder = isLimitOrder;
167
167
  function mustBeTriggered(order) {
168
- return (0, types_1.isOneOfVariant)(order.orderType, ['triggerMarket', 'triggerLimit']);
168
+ return types_1.isOneOfVariant(order.orderType, ['triggerMarket', 'triggerLimit']);
169
169
  }
170
170
  exports.mustBeTriggered = mustBeTriggered;
171
171
  function isTriggered(order) {
172
- return (0, types_1.isOneOfVariant)(order.triggerCondition, [
172
+ return types_1.isOneOfVariant(order.triggerCondition, [
173
173
  'triggeredAbove',
174
174
  'triggeredBelow',
175
175
  ]);
@@ -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 } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, directionToClose, oraclePriceData);
26
+ const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = 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 = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
35
+ prepegAmm = amm_1.calculateUpdatedAMM(market.amm, oraclePriceData);
36
36
  }
37
37
  }
38
38
  else {
39
39
  prepegAmm = market.amm;
40
40
  }
41
- const [newQuoteAssetReserve, _] = (0, amm_1.calculateAmmReservesAfterSwap)(prepegAmm, 'base', userPosition.baseAssetAmount.abs(), (0, amm_1.getSwapDirection)('base', directionToClose));
41
+ const [newQuoteAssetReserve, _] = amm_1.calculateAmmReservesAfterSwap(prepegAmm, 'base', userPosition.baseAssetAmount.abs(), 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 = (0, margin_1.calculateBaseAssetValueWithOracle)(market, perpPosition, oraclePriceData);
70
+ const baseAssetValue = 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, (0, market_1.calculateNetUserPnlImbalance)(market, spotMarket, oraclePriceData).mul(new __1.BN(-1)));
89
+ const excessPnlPool = __1.BN.max(numericConstants_1.ZERO, 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
- (0, assert_1.assert)(Q.gt(new anchor_1.BN(0)));
80
+ 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))) {
@@ -8,11 +8,11 @@ const margin_1 = require("./margin");
8
8
  const numericConstants_2 = require("../constants/numericConstants");
9
9
  function getBalance(tokenAmount, spotMarket, balanceType) {
10
10
  const precisionIncrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
11
- const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
11
+ const cumulativeInterest = types_1.isVariant(balanceType, 'deposit')
12
12
  ? spotMarket.cumulativeDepositInterest
13
13
  : spotMarket.cumulativeBorrowInterest;
14
14
  let balance = tokenAmount.mul(precisionIncrease).div(cumulativeInterest);
15
- if (!balance.eq(numericConstants_1.ZERO) && (0, types_1.isVariant)(balanceType, 'borrow')) {
15
+ if (!balance.eq(numericConstants_1.ZERO) && types_1.isVariant(balanceType, 'borrow')) {
16
16
  balance = balance.add(numericConstants_1.ONE);
17
17
  }
18
18
  return balance;
@@ -20,14 +20,14 @@ function getBalance(tokenAmount, spotMarket, balanceType) {
20
20
  exports.getBalance = getBalance;
21
21
  function getTokenAmount(balanceAmount, spotMarket, balanceType) {
22
22
  const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
23
- const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
23
+ const cumulativeInterest = types_1.isVariant(balanceType, 'deposit')
24
24
  ? spotMarket.cumulativeDepositInterest
25
25
  : spotMarket.cumulativeBorrowInterest;
26
26
  return balanceAmount.mul(cumulativeInterest).div(precisionDecrease);
27
27
  }
28
28
  exports.getTokenAmount = getTokenAmount;
29
29
  function getSignedTokenAmount(tokenAmount, balanceType) {
30
- if ((0, types_1.isVariant)(balanceType, 'deposit')) {
30
+ if (types_1.isVariant(balanceType, 'deposit')) {
31
31
  return tokenAmount;
32
32
  }
33
33
  else {
@@ -57,7 +57,7 @@ function calculateAssetWeight(balanceAmount, spotMarket, marginCategory) {
57
57
  let assetWeight;
58
58
  switch (marginCategory) {
59
59
  case 'Initial':
60
- assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialAssetWeight));
60
+ assetWeight = margin_1.calculateSizeDiscountAssetWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialAssetWeight));
61
61
  break;
62
62
  case 'Maintenance':
63
63
  assetWeight = new anchor_1.BN(spotMarket.maintenanceAssetWeight);
@@ -83,10 +83,10 @@ function calculateLiabilityWeight(balanceAmount, spotMarket, marginCategory) {
83
83
  let assetWeight;
84
84
  switch (marginCategory) {
85
85
  case 'Initial':
86
- assetWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
86
+ assetWeight = margin_1.calculateSizePremiumLiabilityWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
87
87
  break;
88
88
  case 'Maintenance':
89
- assetWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
89
+ assetWeight = margin_1.calculateSizePremiumLiabilityWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
90
90
  break;
91
91
  default:
92
92
  assetWeight = spotMarket.initialLiabilityWeight;
@@ -8,15 +8,15 @@ function isSpotPositionAvailable(position) {
8
8
  }
9
9
  exports.isSpotPositionAvailable = isSpotPositionAvailable;
10
10
  function getWorstCaseTokenAmounts(spotPosition, spotMarketAccount, oraclePriceData) {
11
- const tokenAmount = (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType), spotPosition.balanceType);
11
+ const tokenAmount = spotBalance_1.getSignedTokenAmount(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 (tokenAmountAllAsksFill.abs().gt(tokenAmountAllBidsFill.abs())) {
15
- const worstCaseQuoteTokenAmount = (0, spotBalance_1.getTokenValue)(spotPosition.openAsks.neg(), spotMarketAccount.decimals, oraclePriceData);
15
+ const worstCaseQuoteTokenAmount = spotBalance_1.getTokenValue(spotPosition.openAsks.neg(), spotMarketAccount.decimals, oraclePriceData);
16
16
  return [tokenAmountAllBidsFill, worstCaseQuoteTokenAmount];
17
17
  }
18
18
  else {
19
- const worstCaseQuoteTokenAmount = (0, spotBalance_1.getTokenValue)(spotPosition.openBids.neg(), spotMarketAccount.decimals, oraclePriceData);
19
+ const worstCaseQuoteTokenAmount = spotBalance_1.getTokenValue(spotPosition.openBids.neg(), spotMarketAccount.decimals, oraclePriceData);
20
20
  return [tokenAmountAllAsksFill, worstCaseQuoteTokenAmount];
21
21
  }
22
22
  }
package/lib/math/trade.js CHANGED
@@ -30,15 +30,15 @@ const MAXPCT = new anchor_1.BN(1000); //percentage units are [0,1000] => [0,1]
30
30
  function calculateTradeSlippage(direction, amount, market, inputAssetType = 'quote', oraclePriceData, useSpread = true) {
31
31
  let oldPrice;
32
32
  if (useSpread && market.amm.baseSpread > 0) {
33
- if ((0, types_2.isVariant)(direction, 'long')) {
34
- oldPrice = (0, market_1.calculateAskPrice)(market, oraclePriceData);
33
+ if (types_2.isVariant(direction, 'long')) {
34
+ oldPrice = market_1.calculateAskPrice(market, oraclePriceData);
35
35
  }
36
36
  else {
37
- oldPrice = (0, market_1.calculateBidPrice)(market, oraclePriceData);
37
+ oldPrice = market_1.calculateBidPrice(market, oraclePriceData);
38
38
  }
39
39
  }
40
40
  else {
41
- oldPrice = (0, market_1.calculateReservePrice)(market, oraclePriceData);
41
+ oldPrice = market_1.calculateReservePrice(market, oraclePriceData);
42
42
  }
43
43
  if (amount.eq(numericConstants_1.ZERO)) {
44
44
  return [numericConstants_1.ZERO, numericConstants_1.ZERO, oldPrice, oldPrice];
@@ -50,7 +50,7 @@ function calculateTradeSlippage(direction, amount, market, inputAssetType = 'quo
50
50
  .div(acquiredBaseReserve.abs());
51
51
  let amm;
52
52
  if (useSpread && market.amm.baseSpread > 0) {
53
- const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData);
53
+ const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
54
54
  amm = {
55
55
  baseAssetReserve,
56
56
  quoteAssetReserve,
@@ -61,12 +61,12 @@ function calculateTradeSlippage(direction, amount, market, inputAssetType = 'quo
61
61
  else {
62
62
  amm = market.amm;
63
63
  }
64
- const newPrice = (0, amm_1.calculatePrice)(amm.baseAssetReserve.sub(acquiredBaseReserve), amm.quoteAssetReserve.sub(acquiredQuoteReserve), amm.pegMultiplier);
64
+ const newPrice = amm_1.calculatePrice(amm.baseAssetReserve.sub(acquiredBaseReserve), amm.quoteAssetReserve.sub(acquiredQuoteReserve), amm.pegMultiplier);
65
65
  if (direction == types_1.PositionDirection.SHORT) {
66
- (0, assert_1.assert)(newPrice.lte(oldPrice));
66
+ assert_1.assert(newPrice.lte(oldPrice));
67
67
  }
68
68
  else {
69
- (0, assert_1.assert)(oldPrice.lte(newPrice));
69
+ assert_1.assert(oldPrice.lte(newPrice));
70
70
  }
71
71
  const pctMaxSlippage = newPrice
72
72
  .sub(oldPrice)
@@ -96,10 +96,10 @@ function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType
96
96
  if (amount.eq(numericConstants_1.ZERO)) {
97
97
  return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
98
98
  }
99
- const swapDirection = (0, amm_1.getSwapDirection)(inputAssetType, direction);
99
+ const swapDirection = amm_1.getSwapDirection(inputAssetType, direction);
100
100
  let amm;
101
101
  if (useSpread && market.amm.baseSpread > 0) {
102
- const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData);
102
+ const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
103
103
  amm = {
104
104
  baseAssetReserve,
105
105
  quoteAssetReserve,
@@ -110,10 +110,10 @@ function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType
110
110
  else {
111
111
  amm = market.amm;
112
112
  }
113
- const [newQuoteAssetReserve, newBaseAssetReserve] = (0, amm_1.calculateAmmReservesAfterSwap)(amm, inputAssetType, amount, swapDirection);
113
+ const [newQuoteAssetReserve, newBaseAssetReserve] = amm_1.calculateAmmReservesAfterSwap(amm, inputAssetType, amount, swapDirection);
114
114
  const acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
115
115
  const acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
116
- const acquiredQuoteAssetAmount = (0, amm_1.calculateQuoteAssetAmountSwapped)(acquiredQuote.abs(), amm.pegMultiplier, swapDirection);
116
+ const acquiredQuoteAssetAmount = amm_1.calculateQuoteAssetAmountSwapped(acquiredQuote.abs(), amm.pegMultiplier, swapDirection);
117
117
  return [acquiredBase, acquiredQuote, acquiredQuoteAssetAmount];
118
118
  }
119
119
  exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
@@ -135,12 +135,12 @@ exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
135
135
  * ]
136
136
  */
137
137
  function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAssetType = 'quote', oraclePriceData, useSpread = true) {
138
- (0, assert_1.assert)(market.amm.baseAssetReserve.gt(numericConstants_1.ZERO));
139
- (0, assert_1.assert)(targetPrice.gt(numericConstants_1.ZERO));
140
- (0, assert_1.assert)(pct.lte(MAXPCT) && pct.gt(numericConstants_1.ZERO));
141
- const reservePriceBefore = (0, market_1.calculateReservePrice)(market, oraclePriceData);
142
- const bidPriceBefore = (0, market_1.calculateBidPrice)(market, oraclePriceData);
143
- const askPriceBefore = (0, market_1.calculateAskPrice)(market, oraclePriceData);
138
+ assert_1.assert(market.amm.baseAssetReserve.gt(numericConstants_1.ZERO));
139
+ assert_1.assert(targetPrice.gt(numericConstants_1.ZERO));
140
+ assert_1.assert(pct.lte(MAXPCT) && pct.gt(numericConstants_1.ZERO));
141
+ const reservePriceBefore = market_1.calculateReservePrice(market, oraclePriceData);
142
+ const bidPriceBefore = market_1.calculateBidPrice(market, oraclePriceData);
143
+ const askPriceBefore = market_1.calculateAskPrice(market, oraclePriceData);
144
144
  let direction;
145
145
  if (targetPrice.gt(reservePriceBefore)) {
146
146
  const priceGap = targetPrice.sub(reservePriceBefore);
@@ -160,7 +160,7 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
160
160
  let quoteAssetReserveBefore;
161
161
  let peg = market.amm.pegMultiplier;
162
162
  if (useSpread && market.amm.baseSpread > 0) {
163
- const { baseAssetReserve, quoteAssetReserve, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData);
163
+ const { baseAssetReserve, quoteAssetReserve, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
164
164
  baseAssetReserveBefore = baseAssetReserve;
165
165
  quoteAssetReserveBefore = quoteAssetReserve;
166
166
  peg = newPeg;
@@ -190,9 +190,9 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
190
190
  }
191
191
  else if (reservePriceBefore.gt(targetPrice)) {
192
192
  // overestimate y2
193
- baseAssetReserveAfter = (0, utils_1.squareRootBN)(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).sub(biasModifier)).sub(new anchor_1.BN(1));
193
+ baseAssetReserveAfter = utils_1.squareRootBN(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).sub(biasModifier)).sub(new anchor_1.BN(1));
194
194
  quoteAssetReserveAfter = k.div(numericConstants_1.PRICE_PRECISION).div(baseAssetReserveAfter);
195
- markPriceAfter = (0, amm_1.calculatePrice)(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
195
+ markPriceAfter = amm_1.calculatePrice(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
196
196
  direction = types_1.PositionDirection.SHORT;
197
197
  tradeSize = quoteAssetReserveBefore
198
198
  .sub(quoteAssetReserveAfter)
@@ -203,9 +203,9 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
203
203
  }
204
204
  else if (reservePriceBefore.lt(targetPrice)) {
205
205
  // underestimate y2
206
- baseAssetReserveAfter = (0, utils_1.squareRootBN)(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).add(biasModifier)).add(new anchor_1.BN(1));
206
+ baseAssetReserveAfter = utils_1.squareRootBN(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).add(biasModifier)).add(new anchor_1.BN(1));
207
207
  quoteAssetReserveAfter = k.div(numericConstants_1.PRICE_PRECISION).div(baseAssetReserveAfter);
208
- markPriceAfter = (0, amm_1.calculatePrice)(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
208
+ markPriceAfter = amm_1.calculatePrice(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
209
209
  direction = types_1.PositionDirection.LONG;
210
210
  tradeSize = quoteAssetReserveAfter
211
211
  .sub(quoteAssetReserveBefore)
@@ -232,8 +232,8 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
232
232
  .mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
233
233
  .mul(numericConstants_1.PRICE_PRECISION)
234
234
  .div(baseSize.abs());
235
- (0, assert_1.assert)(tp1.sub(tp2).lte(originalDiff), 'Target Price Calculation incorrect');
236
- (0, assert_1.assert)(tp2.lte(tp1) || tp2.sub(tp1).abs() < 100000, 'Target Price Calculation incorrect' +
235
+ assert_1.assert(tp1.sub(tp2).lte(originalDiff), 'Target Price Calculation incorrect');
236
+ assert_1.assert(tp2.lte(tp1) || tp2.sub(tp1).abs() < 100000, 'Target Price Calculation incorrect' +
237
237
  tp2.toString() +
238
238
  '>=' +
239
239
  tp1.toString() +
@@ -264,10 +264,10 @@ function calculateEstimatedPerpEntryPrice(assetType, amount, direction, market,
264
264
  if (amount.eq(numericConstants_1.ZERO)) {
265
265
  return [numericConstants_1.ZERO, numericConstants_1.ZERO];
266
266
  }
267
- const takerIsLong = (0, types_2.isVariant)(direction, 'long');
267
+ const takerIsLong = types_2.isVariant(direction, 'long');
268
268
  const limitOrders = dlob[takerIsLong ? 'getRestingLimitAsks' : 'getRestingLimitBids'](market.marketIndex, slot, types_1.MarketType.PERP, oraclePriceData, minPerpAuctionDuration);
269
- const swapDirection = (0, amm_1.getSwapDirection)(assetType, direction);
270
- const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData);
269
+ const swapDirection = amm_1.getSwapDirection(assetType, direction);
270
+ const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
271
271
  const amm = {
272
272
  baseAssetReserve,
273
273
  quoteAssetReserve,
@@ -275,7 +275,7 @@ function calculateEstimatedPerpEntryPrice(assetType, amount, direction, market,
275
275
  pegMultiplier: newPeg,
276
276
  };
277
277
  const invariant = amm.sqrtK.mul(amm.sqrtK);
278
- let initialPrice = (0, amm_1.calculatePrice)(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
278
+ let initialPrice = amm_1.calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
279
279
  let cumulativeBaseFilled = numericConstants_1.ZERO;
280
280
  let cumulativeQuoteFilled = numericConstants_1.ZERO;
281
281
  let limitOrder = limitOrders.next().value;
@@ -290,7 +290,7 @@ function calculateEstimatedPerpEntryPrice(assetType, amount, direction, market,
290
290
  const limitOrderPrice = limitOrder === null || limitOrder === void 0 ? void 0 : limitOrder.getPrice(oraclePriceData, slot);
291
291
  let maxAmmFill;
292
292
  if (limitOrderPrice) {
293
- const newBaseReserves = (0, utils_1.squareRootBN)(invariant
293
+ const newBaseReserves = utils_1.squareRootBN(invariant
294
294
  .mul(numericConstants_1.PRICE_PRECISION)
295
295
  .mul(amm.pegMultiplier)
296
296
  .div(limitOrderPrice)
@@ -305,8 +305,8 @@ function calculateEstimatedPerpEntryPrice(assetType, amount, direction, market,
305
305
  }
306
306
  if (maxAmmFill.gt(numericConstants_1.ZERO)) {
307
307
  const baseFilled = anchor_1.BN.min(amount.sub(cumulativeBaseFilled), maxAmmFill);
308
- const [afterSwapQuoteReserves, afterSwapBaseReserves] = (0, amm_1.calculateAmmReservesAfterSwap)(amm, 'base', baseFilled, swapDirection);
309
- const quoteFilled = (0, amm_1.calculateQuoteAssetAmountSwapped)(amm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(), amm.pegMultiplier, swapDirection);
308
+ const [afterSwapQuoteReserves, afterSwapBaseReserves] = amm_1.calculateAmmReservesAfterSwap(amm, 'base', baseFilled, swapDirection);
309
+ const quoteFilled = amm_1.calculateQuoteAssetAmountSwapped(amm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(), amm.pegMultiplier, swapDirection);
310
310
  cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
311
311
  cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
312
312
  amm.baseAssetReserve = afterSwapBaseReserves;
@@ -330,7 +330,7 @@ function calculateEstimatedPerpEntryPrice(assetType, amount, direction, market,
330
330
  const limitOrderPrice = limitOrder === null || limitOrder === void 0 ? void 0 : limitOrder.getPrice(oraclePriceData, slot);
331
331
  let maxAmmFill;
332
332
  if (limitOrderPrice) {
333
- const newQuoteReserves = (0, utils_1.squareRootBN)(invariant
333
+ const newQuoteReserves = utils_1.squareRootBN(invariant
334
334
  .mul(numericConstants_1.PEG_PRECISION)
335
335
  .mul(limitOrderPrice)
336
336
  .div(amm.pegMultiplier)
@@ -345,7 +345,7 @@ function calculateEstimatedPerpEntryPrice(assetType, amount, direction, market,
345
345
  }
346
346
  if (maxAmmFill.gt(numericConstants_1.ZERO)) {
347
347
  const quoteFilled = anchor_1.BN.min(amount.sub(cumulativeQuoteFilled), maxAmmFill);
348
- const [afterSwapQuoteReserves, afterSwapBaseReserves] = (0, amm_1.calculateAmmReservesAfterSwap)(amm, 'quote', quoteFilled, swapDirection);
348
+ const [afterSwapQuoteReserves, afterSwapBaseReserves] = amm_1.calculateAmmReservesAfterSwap(amm, 'quote', quoteFilled, swapDirection);
349
349
  const baseFilled = afterSwapBaseReserves
350
350
  .sub(amm.baseAssetReserve)
351
351
  .abs();
@@ -11,7 +11,7 @@ class OracleClientCache {
11
11
  if (this.cache.has(key)) {
12
12
  return this.cache.get(key);
13
13
  }
14
- const client = (0, oracleClient_1.getOracleClient)(oracleSource, connection);
14
+ const client = oracleClient_1.getOracleClient(oracleSource, connection);
15
15
  this.cache.set(key, client);
16
16
  return client;
17
17
  }
@@ -13,7 +13,7 @@ class PythClient {
13
13
  return this.getOraclePriceDataFromBuffer(accountInfo.data);
14
14
  }
15
15
  getOraclePriceDataFromBuffer(buffer) {
16
- const priceData = (0, client_1.parsePriceData)(buffer);
16
+ const priceData = client_1.parsePriceData(buffer);
17
17
  return {
18
18
  price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
19
19
  slot: new anchor_1.BN(priceData.lastSlot.toString()),
@@ -1,11 +1,7 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
9
5
  }) : (function(o, m, k, k2) {
10
6
  if (k2 === undefined) k2 = k;
11
7
  o[k2] = m[k];
@@ -93,7 +93,7 @@ class RetryTxSender {
93
93
  catch (err) {
94
94
  throw new Error('signature must be base58 encoded: ' + signature);
95
95
  }
96
- (0, assert_1.default)(decodedSignature.length === 64, 'signature has invalid length');
96
+ assert_1.default(decodedSignature.length === 64, 'signature has invalid length');
97
97
  const start = Date.now();
98
98
  const subscriptionCommitment = commitment || this.provider.opts.commitment;
99
99
  const subscriptionIds = new Array();
package/lib/tx/utils.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import { Transaction, TransactionInstruction } from '@solana/web3.js';
2
- export declare function wrapInTx(instruction: TransactionInstruction, computeUnits?: number): Transaction;
2
+ export declare function wrapInTx(instruction: TransactionInstruction, computeUnits?: number, computeUnitsPrice?: number): Transaction;
package/lib/tx/utils.js CHANGED
@@ -3,13 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.wrapInTx = void 0;
4
4
  const web3_js_1 = require("@solana/web3.js");
5
5
  const COMPUTE_UNITS_DEFAULT = 200000;
6
- function wrapInTx(instruction, computeUnits = 600000 // TODO, requires less code change
7
- ) {
6
+ function wrapInTx(instruction, computeUnits = 600000, computeUnitsPrice = 0) {
8
7
  const tx = new web3_js_1.Transaction();
9
8
  if (computeUnits != COMPUTE_UNITS_DEFAULT) {
10
- tx.add(web3_js_1.ComputeBudgetProgram.requestUnits({
9
+ tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
11
10
  units: computeUnits,
12
- additionalFee: 0,
11
+ }));
12
+ }
13
+ if (computeUnitsPrice != 0) {
14
+ tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
15
+ microLamports: computeUnitsPrice,
13
16
  }));
14
17
  }
15
18
  return tx.add(instruction);
package/lib/types.d.ts CHANGED
@@ -923,6 +923,10 @@ export declare type ReferrerInfo = {
923
923
  referrer: PublicKey;
924
924
  referrerStats: PublicKey;
925
925
  };
926
+ export declare type TxParams = {
927
+ computeUnits?: number;
928
+ computeUnitsPrice?: number;
929
+ };
926
930
  export interface IWallet {
927
931
  signTransaction(tx: Transaction): Promise<Transaction>;
928
932
  signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;