@drift-labs/sdk 0.2.0-master.25 → 0.2.0-master.26

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 (184) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
  3. package/lib/accounts/types.d.ts +8 -9
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
  6. package/lib/addresses/pda.d.ts +7 -6
  7. package/lib/addresses/pda.js +31 -27
  8. package/lib/admin.d.ts +9 -6
  9. package/lib/admin.js +83 -42
  10. package/lib/clearingHouse.d.ts +69 -42
  11. package/lib/clearingHouse.js +753 -277
  12. package/lib/clearingHouseConfig.d.ts +2 -2
  13. package/lib/clearingHouseUser.d.ts +16 -16
  14. package/lib/clearingHouseUser.js +139 -119
  15. package/lib/config.d.ts +7 -7
  16. package/lib/config.js +20 -20
  17. package/lib/constants/numericConstants.d.ts +12 -12
  18. package/lib/constants/numericConstants.js +13 -13
  19. package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
  20. package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
  21. package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
  22. package/lib/constants/{banks.js → spotMarkets.js} +16 -16
  23. package/lib/events/types.d.ts +2 -1
  24. package/lib/events/types.js +1 -0
  25. package/lib/examples/makeTradeExample.js +7 -7
  26. package/lib/idl/clearing_house.json +1008 -279
  27. package/lib/index.d.ts +5 -3
  28. package/lib/index.js +5 -3
  29. package/lib/math/amm.d.ts +2 -2
  30. package/lib/math/amm.js +1 -1
  31. package/lib/math/funding.d.ts +6 -6
  32. package/lib/math/funding.js +2 -1
  33. package/lib/math/margin.d.ts +4 -4
  34. package/lib/math/margin.js +18 -11
  35. package/lib/math/market.d.ts +10 -9
  36. package/lib/math/market.js +29 -6
  37. package/lib/math/oracles.d.ts +2 -1
  38. package/lib/math/oracles.js +11 -1
  39. package/lib/math/orders.d.ts +5 -5
  40. package/lib/math/position.d.ts +13 -13
  41. package/lib/math/position.js +19 -19
  42. package/lib/math/spotBalance.d.ts +19 -0
  43. package/lib/math/spotBalance.js +176 -0
  44. package/lib/math/spotMarket.d.ts +4 -0
  45. package/lib/math/spotMarket.js +8 -0
  46. package/lib/math/spotPosition.d.ts +2 -0
  47. package/lib/math/spotPosition.js +8 -0
  48. package/lib/math/state.js +2 -2
  49. package/lib/math/trade.d.ts +4 -4
  50. package/lib/orderParams.d.ts +4 -4
  51. package/lib/orderParams.js +12 -4
  52. package/lib/serum/serumSubscriber.d.ts +23 -0
  53. package/lib/serum/serumSubscriber.js +41 -0
  54. package/lib/serum/types.d.ts +11 -0
  55. package/lib/serum/types.js +2 -0
  56. package/lib/tx/retryTxSender.d.ts +1 -1
  57. package/lib/tx/retryTxSender.js +4 -2
  58. package/lib/tx/types.d.ts +1 -1
  59. package/lib/types.d.ts +123 -33
  60. package/lib/types.js +31 -9
  61. package/my-script/.env +7 -0
  62. package/my-script/getUserStats.ts +106 -0
  63. package/my-script/multiConnections.ts +119 -0
  64. package/my-script/test-regex.ts +11 -0
  65. package/my-script/utils.ts +52 -0
  66. package/package.json +1 -1
  67. package/src/accounts/bulkAccountLoader.js +249 -0
  68. package/src/accounts/bulkUserStatsSubscription.js +75 -0
  69. package/src/accounts/bulkUserSubscription.js +75 -0
  70. package/src/accounts/fetch.js +92 -0
  71. package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
  72. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
  73. package/src/accounts/pollingOracleSubscriber.js +156 -0
  74. package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
  75. package/src/accounts/pollingUserAccountSubscriber.js +208 -0
  76. package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
  77. package/src/accounts/types.js +28 -0
  78. package/src/accounts/types.ts +11 -9
  79. package/src/accounts/utils.js +7 -0
  80. package/src/accounts/webSocketAccountSubscriber.js +138 -0
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
  82. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
  83. package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
  84. package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
  85. package/src/addresses/pda.js +186 -0
  86. package/src/addresses/pda.ts +49 -44
  87. package/src/admin.js +1284 -0
  88. package/src/admin.ts +140 -47
  89. package/src/assert/assert.js +1 -1
  90. package/src/clearingHouse.js +3433 -0
  91. package/src/clearingHouse.ts +1083 -378
  92. package/src/clearingHouseConfig.js +2 -0
  93. package/src/clearingHouseConfig.ts +2 -2
  94. package/src/clearingHouseUser.js +874 -0
  95. package/src/clearingHouseUser.ts +232 -168
  96. package/src/clearingHouseUserConfig.js +2 -0
  97. package/src/clearingHouseUserStats.js +115 -0
  98. package/src/clearingHouseUserStatsConfig.js +2 -0
  99. package/src/config.js +80 -0
  100. package/src/config.ts +29 -29
  101. package/src/constants/numericConstants.js +18 -11
  102. package/src/constants/numericConstants.ts +17 -15
  103. package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
  104. package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
  105. package/src/constants/spotMarkets.js +51 -0
  106. package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
  107. package/src/events/eventList.js +66 -23
  108. package/src/events/eventSubscriber.js +202 -0
  109. package/src/events/fetchLogs.js +117 -0
  110. package/src/events/pollingLogProvider.js +113 -0
  111. package/src/events/sort.js +41 -0
  112. package/src/events/txEventCache.js +22 -19
  113. package/src/events/types.js +25 -0
  114. package/src/events/types.ts +3 -0
  115. package/src/events/webSocketLogProvider.js +76 -0
  116. package/src/examples/makeTradeExample.ts +10 -8
  117. package/src/factory/bigNum.js +183 -180
  118. package/src/factory/oracleClient.js +9 -9
  119. package/src/idl/clearing_house.json +1008 -279
  120. package/src/index.js +75 -0
  121. package/src/index.ts +5 -3
  122. package/src/math/amm.js +422 -0
  123. package/src/math/amm.ts +6 -3
  124. package/src/math/auction.js +10 -10
  125. package/src/math/conversion.js +4 -3
  126. package/src/math/funding.js +223 -175
  127. package/src/math/funding.ts +7 -7
  128. package/src/math/insurance.js +27 -0
  129. package/src/math/margin.js +77 -0
  130. package/src/math/margin.ts +34 -23
  131. package/src/math/market.js +105 -0
  132. package/src/math/market.ts +71 -19
  133. package/src/math/oracles.js +40 -10
  134. package/src/math/oracles.ts +18 -1
  135. package/src/math/orders.js +153 -0
  136. package/src/math/orders.ts +5 -5
  137. package/src/math/position.js +172 -0
  138. package/src/math/position.ts +31 -31
  139. package/src/math/repeg.js +40 -40
  140. package/src/math/spotBalance.js +176 -0
  141. package/src/math/spotBalance.ts +290 -0
  142. package/src/math/spotMarket.js +8 -0
  143. package/src/math/spotMarket.ts +9 -0
  144. package/src/math/spotPosition.js +8 -0
  145. package/src/math/spotPosition.ts +6 -0
  146. package/src/math/state.ts +2 -2
  147. package/src/math/trade.js +81 -74
  148. package/src/math/trade.ts +4 -4
  149. package/src/math/utils.js +8 -7
  150. package/src/oracles/oracleClientCache.js +10 -9
  151. package/src/oracles/pythClient.js +52 -17
  152. package/src/oracles/quoteAssetOracleClient.js +44 -13
  153. package/src/oracles/switchboardClient.js +69 -37
  154. package/src/oracles/types.js +1 -1
  155. package/src/orderParams.js +14 -6
  156. package/src/orderParams.ts +16 -8
  157. package/src/serum/serumSubscriber.js +102 -0
  158. package/src/serum/serumSubscriber.ts +80 -0
  159. package/src/serum/types.js +2 -0
  160. package/src/serum/types.ts +13 -0
  161. package/src/slot/SlotSubscriber.js +67 -20
  162. package/src/token/index.js +4 -4
  163. package/src/tokenFaucet.js +288 -154
  164. package/src/tx/retryTxSender.js +280 -0
  165. package/src/tx/retryTxSender.ts +5 -2
  166. package/src/tx/types.js +1 -1
  167. package/src/tx/types.ts +2 -1
  168. package/src/tx/utils.js +7 -6
  169. package/src/types.js +216 -0
  170. package/src/types.ts +110 -33
  171. package/src/userName.js +5 -5
  172. package/src/util/computeUnits.js +46 -11
  173. package/src/util/promiseTimeout.js +5 -5
  174. package/src/util/tps.js +46 -12
  175. package/src/wallet.js +55 -18
  176. package/lib/math/bankBalance.d.ts +0 -15
  177. package/lib/math/bankBalance.js +0 -150
  178. package/src/addresses/marketAddresses.js +0 -26
  179. package/src/constants/banks.js +0 -42
  180. package/src/examples/makeTradeExample.js +0 -80
  181. package/src/math/bankBalance.ts +0 -258
  182. package/src/math/state.js +0 -15
  183. package/src/math/utils.js.map +0 -1
  184. package/src/util/getTokenAddress.js +0 -9
@@ -5,7 +5,7 @@ const types_1 = require("./types");
5
5
  const position_1 = require("./math/position");
6
6
  const numericConstants_1 = require("./constants/numericConstants");
7
7
  const _1 = require(".");
8
- const bankBalance_1 = require("./math/bankBalance");
8
+ const spotBalance_1 = require("./math/spotBalance");
9
9
  const margin_1 = require("./math/margin");
10
10
  const pollingUserAccountSubscriber_1 = require("./accounts/pollingUserAccountSubscriber");
11
11
  const webSocketUserAccountSubscriber_1 = require("./accounts/webSocketUserAccountSubscriber");
@@ -59,11 +59,12 @@ class ClearingHouseUser {
59
59
  * @returns userPosition
60
60
  */
61
61
  getUserPosition(marketIndex) {
62
- return this.getUserAccount().positions.find((position) => position.marketIndex.eq(marketIndex));
62
+ return this.getUserAccount().perpPositions.find((position) => position.marketIndex.eq(marketIndex));
63
63
  }
64
64
  getEmptyPosition(marketIndex) {
65
65
  return {
66
66
  baseAssetAmount: numericConstants_1.ZERO,
67
+ remainderBaseAssetAmount: numericConstants_1.ZERO,
67
68
  lastCumulativeFundingRate: numericConstants_1.ZERO,
68
69
  marketIndex,
69
70
  quoteAssetAmount: numericConstants_1.ZERO,
@@ -112,7 +113,7 @@ class ClearingHouseUser {
112
113
  getSettledLPPosition(marketIndex) {
113
114
  const _position = this.getUserPosition(marketIndex);
114
115
  const position = this.getClonedPosition(_position);
115
- const market = this.clearingHouse.getMarketAccount(position.marketIndex);
116
+ const market = this.clearingHouse.getPerpMarketAccount(position.marketIndex);
116
117
  const nShares = position.lpShares;
117
118
  const deltaBaa = market.amm.marketPositionPerLp.baseAssetAmount
118
119
  .sub(position.lastNetBaseAssetAmountPerLp)
@@ -126,22 +127,22 @@ class ClearingHouseUser {
126
127
  const sign = { true: new _1.BN(1), false: new _1.BN(-1) }[v.gte(numericConstants_1.ZERO).toString()];
127
128
  return sign;
128
129
  }
129
- const remainder = deltaBaa
130
- .abs()
131
- .mod(market.amm.baseAssetAmountStepSize)
132
- .mul(sign(deltaBaa));
133
- const _standardizedBaa = deltaBaa.sub(remainder);
134
- let remainderBaa;
135
- if (_standardizedBaa.abs().gte(market.amm.baseAssetAmountStepSize)) {
136
- remainderBaa = remainder;
130
+ function standardize(amount, stepsize) {
131
+ const remainder = amount.abs().mod(stepsize).mul(sign(amount));
132
+ const standardizedAmount = amount.sub(remainder);
133
+ return [standardizedAmount, remainder];
137
134
  }
138
- else {
139
- remainderBaa = deltaBaa;
135
+ const [standardizedBaa, remainderBaa] = standardize(deltaBaa, market.amm.baseAssetAmountStepSize);
136
+ position.remainderBaseAssetAmount =
137
+ position.remainderBaseAssetAmount.add(remainderBaa);
138
+ if (position.remainderBaseAssetAmount
139
+ .abs()
140
+ .gte(market.amm.baseAssetAmountStepSize)) {
141
+ const [newStandardizedBaa, newRemainderBaa] = standardize(position.remainderBaseAssetAmount, market.amm.baseAssetAmountStepSize);
142
+ position.baseAssetAmount =
143
+ position.baseAssetAmount.add(newStandardizedBaa);
144
+ position.remainderBaseAssetAmount = newRemainderBaa;
140
145
  }
141
- const standardizedBaa = deltaBaa.sub(remainderBaa);
142
- const reaminderPerLP = remainderBaa.mul(numericConstants_1.AMM_RESERVE_PRECISION).div(nShares);
143
- position.lastNetBaseAssetAmountPerLp =
144
- market.amm.marketPositionPerLp.baseAssetAmount.sub(reaminderPerLP);
145
146
  let updateType;
146
147
  if (position.baseAssetAmount.eq(numericConstants_1.ZERO)) {
147
148
  updateType = 'open';
@@ -211,25 +212,25 @@ class ClearingHouseUser {
211
212
  /**
212
213
  * @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
213
214
  */
214
- getMarginRequirement(type) {
215
+ getMarginRequirement(type, liquidationBuffer) {
215
216
  return this.getUserAccount()
216
- .positions.reduce((marginRequirement, marketPosition) => {
217
- const market = this.clearingHouse.getMarketAccount(marketPosition.marketIndex);
218
- if (marketPosition.lpShares.gt(numericConstants_1.ZERO)) {
217
+ .perpPositions.reduce((marginRequirement, perpPosition) => {
218
+ const market = this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex);
219
+ if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
219
220
  // is an lp
220
221
  // clone so we dont mutate the position
221
- marketPosition = this.getClonedPosition(marketPosition);
222
+ perpPosition = this.getClonedPosition(perpPosition);
222
223
  // settle position
223
224
  const [settledPosition, dustBaa, _] = this.getSettledLPPosition(market.marketIndex);
224
- marketPosition.baseAssetAmount =
225
+ perpPosition.baseAssetAmount =
225
226
  settledPosition.baseAssetAmount.add(dustBaa);
226
- marketPosition.quoteAssetAmount = settledPosition.quoteAssetAmount;
227
+ perpPosition.quoteAssetAmount = settledPosition.quoteAssetAmount;
227
228
  // open orders
228
229
  let openAsks;
229
230
  if (market.amm.maxBaseAssetReserve > market.amm.baseAssetReserve) {
230
231
  openAsks = market.amm.maxBaseAssetReserve
231
232
  .sub(market.amm.baseAssetReserve)
232
- .mul(marketPosition.lpShares)
233
+ .mul(perpPosition.lpShares)
233
234
  .div(market.amm.sqrtK)
234
235
  .mul(new _1.BN(-1));
235
236
  }
@@ -240,25 +241,33 @@ class ClearingHouseUser {
240
241
  if (market.amm.minBaseAssetReserve < market.amm.baseAssetReserve) {
241
242
  openBids = market.amm.baseAssetReserve
242
243
  .sub(market.amm.minBaseAssetReserve)
243
- .mul(marketPosition.lpShares)
244
+ .mul(perpPosition.lpShares)
244
245
  .div(market.amm.sqrtK);
245
246
  }
246
247
  else {
247
248
  openBids = numericConstants_1.ZERO;
248
249
  }
249
- marketPosition.openAsks = marketPosition.openAsks.add(openAsks);
250
- marketPosition.openBids = marketPosition.openBids.add(openBids);
250
+ perpPosition.openAsks = perpPosition.openAsks.add(openAsks);
251
+ perpPosition.openBids = perpPosition.openBids.add(openBids);
252
+ }
253
+ let valuationPrice = this.getOracleDataForMarket(market.marketIndex).price;
254
+ if ((0, types_1.isVariant)(market.status, 'settlement')) {
255
+ valuationPrice = market.settlementPrice;
251
256
  }
252
- const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(marketPosition);
257
+ const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
253
258
  const worstCaseAssetValue = worstCaseBaseAssetAmount
254
259
  .abs()
255
- .mul(this.getOracleDataForMarket(market.marketIndex).price)
260
+ .mul(valuationPrice)
256
261
  .div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
257
- return marginRequirement.add(worstCaseAssetValue
262
+ const positionMarginRequirement = worstCaseAssetValue
258
263
  .mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(), type)))
259
- .div(numericConstants_1.MARGIN_PRECISION));
264
+ .div(numericConstants_1.MARGIN_PRECISION);
265
+ if (liquidationBuffer !== undefined) {
266
+ positionMarginRequirement.add(worstCaseAssetValue.mul(liquidationBuffer).div(numericConstants_1.MARGIN_PRECISION));
267
+ }
268
+ return marginRequirement.add(positionMarginRequirement);
260
269
  }, numericConstants_1.ZERO)
261
- .add(this.getBankLiabilityValue(undefined, type));
270
+ .add(this.getSpotMarketLiabilityValue(undefined, type, liquidationBuffer));
262
271
  }
263
272
  /**
264
273
  * @returns The initial margin requirement in USDC. : QUOTE_PRECISION
@@ -269,24 +278,26 @@ class ClearingHouseUser {
269
278
  /**
270
279
  * @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
271
280
  */
272
- getMaintenanceMarginRequirement() {
273
- return this.getMarginRequirement('Maintenance');
281
+ getMaintenanceMarginRequirement(liquidationBuffer) {
282
+ return this.getMarginRequirement('Maintenance', liquidationBuffer);
274
283
  }
275
284
  /**
276
285
  * calculates unrealized position price pnl
277
286
  * @returns : Precision QUOTE_PRECISION
278
287
  */
279
288
  getUnrealizedPNL(withFunding, marketIndex, withWeightMarginCategory) {
289
+ const quoteSpotMarket = this.clearingHouse.getQuoteSpotMarketAccount();
280
290
  return this.getUserAccount()
281
- .positions.filter((pos) => marketIndex ? pos.marketIndex === marketIndex : true)
282
- .reduce((unrealizedPnl, marketPosition) => {
283
- const market = this.clearingHouse.getMarketAccount(marketPosition.marketIndex);
284
- let positionUnrealizedPnl = (0, _1.calculatePositionPNL)(market, marketPosition, withFunding, this.getOracleDataForMarket(market.marketIndex));
291
+ .perpPositions.filter((pos) => marketIndex ? pos.marketIndex === marketIndex : true)
292
+ .reduce((unrealizedPnl, perpPosition) => {
293
+ const market = this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex);
294
+ const oraclePriceData = this.getOracleDataForMarket(market.marketIndex);
295
+ let positionUnrealizedPnl = (0, _1.calculatePositionPNL)(market, perpPosition, withFunding, oraclePriceData);
285
296
  if (withWeightMarginCategory !== undefined) {
286
297
  if (positionUnrealizedPnl.gt(numericConstants_1.ZERO)) {
287
298
  positionUnrealizedPnl = positionUnrealizedPnl
288
- .mul((0, _1.calculateUnrealizedAssetWeight)(market, positionUnrealizedPnl, withWeightMarginCategory))
289
- .div(new _1.BN(numericConstants_1.BANK_WEIGHT_PRECISION));
299
+ .mul((0, _1.calculateUnrealizedAssetWeight)(market, quoteSpotMarket, positionUnrealizedPnl, withWeightMarginCategory, oraclePriceData))
300
+ .div(new _1.BN(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION));
290
301
  }
291
302
  }
292
303
  return unrealizedPnl.add(positionUnrealizedPnl);
@@ -298,74 +309,79 @@ class ClearingHouseUser {
298
309
  */
299
310
  getUnrealizedFundingPNL(marketIndex) {
300
311
  return this.getUserAccount()
301
- .positions.filter((pos) => marketIndex ? pos.marketIndex === marketIndex : true)
302
- .reduce((pnl, marketPosition) => {
303
- const market = this.clearingHouse.getMarketAccount(marketPosition.marketIndex);
304
- return pnl.add((0, _1.calculatePositionFundingPNL)(market, marketPosition));
312
+ .perpPositions.filter((pos) => marketIndex ? pos.marketIndex === marketIndex : true)
313
+ .reduce((pnl, perpPosition) => {
314
+ const market = this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex);
315
+ return pnl.add((0, _1.calculatePositionFundingPNL)(market, perpPosition));
305
316
  }, numericConstants_1.ZERO);
306
317
  }
307
- getBankLiabilityValue(bankIndex, withWeightMarginCategory) {
308
- return this.getUserAccount().bankBalances.reduce((totalLiabilityValue, bankBalance) => {
309
- if (bankBalance.balance.eq(numericConstants_1.ZERO) ||
310
- (0, types_1.isVariant)(bankBalance.balanceType, 'deposit') ||
311
- (bankIndex !== undefined && !bankBalance.bankIndex.eq(bankIndex))) {
318
+ getSpotMarketLiabilityValue(marketIndex, withWeightMarginCategory, liquidationBuffer) {
319
+ return this.getUserAccount().spotPositions.reduce((totalLiabilityValue, spotPosition) => {
320
+ if (spotPosition.balance.eq(numericConstants_1.ZERO) ||
321
+ (0, types_1.isVariant)(spotPosition.balanceType, 'deposit') ||
322
+ (marketIndex !== undefined &&
323
+ !spotPosition.marketIndex.eq(marketIndex))) {
312
324
  return totalLiabilityValue;
313
325
  }
314
326
  // Todo this needs to account for whether it's based on initial or maintenance requirements
315
- const bankAccount = this.clearingHouse.getBankAccount(bankBalance.bankIndex);
316
- const tokenAmount = (0, bankBalance_1.getTokenAmount)(bankBalance.balance, bankAccount, bankBalance.balanceType);
327
+ const spotMarketAccount = this.clearingHouse.getSpotMarketAccount(spotPosition.marketIndex);
328
+ const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.balance, spotMarketAccount, spotPosition.balanceType);
317
329
  let liabilityValue = tokenAmount
318
- .mul(this.getOracleDataForBank(bankAccount.bankIndex).price)
330
+ .mul(this.getOracleDataForSpotMarket(spotMarketAccount.marketIndex).price)
319
331
  .div(numericConstants_1.MARK_PRICE_PRECISION)
320
- .div(new _1.BN(10).pow(new _1.BN(bankAccount.decimals).sub(numericConstants_1.BANK_BALANCE_PRECISION_EXP)));
332
+ .div(new _1.BN(10).pow(new _1.BN(spotMarketAccount.decimals).sub(numericConstants_1.SPOT_MARKET_BALANCE_PRECISION_EXP)));
321
333
  if (withWeightMarginCategory !== undefined) {
322
- const weight = (0, bankBalance_1.calculateLiabilityWeight)(tokenAmount, bankAccount, withWeightMarginCategory);
334
+ let weight = (0, spotBalance_1.calculateLiabilityWeight)(tokenAmount, spotMarketAccount, withWeightMarginCategory);
335
+ if (liquidationBuffer !== undefined) {
336
+ weight = weight.add(liquidationBuffer);
337
+ }
323
338
  liabilityValue = liabilityValue
324
339
  .mul(weight)
325
- .div(numericConstants_1.BANK_WEIGHT_PRECISION);
340
+ .div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
326
341
  }
327
342
  return totalLiabilityValue.add(liabilityValue);
328
343
  }, numericConstants_1.ZERO);
329
344
  }
330
- getBankAssetValue(bankIndex, withWeightMarginCategory) {
331
- return this.getUserAccount().bankBalances.reduce((totalAssetValue, bankBalance) => {
332
- if (bankBalance.balance.eq(numericConstants_1.ZERO) ||
333
- (0, types_1.isVariant)(bankBalance.balanceType, 'borrow') ||
334
- (bankIndex !== undefined && !bankBalance.bankIndex.eq(bankIndex))) {
345
+ getSpotMarketAssetValue(marketIndex, withWeightMarginCategory) {
346
+ return this.getUserAccount().spotPositions.reduce((totalAssetValue, spotPosition) => {
347
+ if (spotPosition.balance.eq(numericConstants_1.ZERO) ||
348
+ (0, types_1.isVariant)(spotPosition.balanceType, 'borrow') ||
349
+ (marketIndex !== undefined &&
350
+ !spotPosition.marketIndex.eq(marketIndex))) {
335
351
  return totalAssetValue;
336
352
  }
337
353
  // Todo this needs to account for whether it's based on initial or maintenance requirements
338
- const bankAccount = this.clearingHouse.getBankAccount(bankBalance.bankIndex);
339
- const tokenAmount = (0, bankBalance_1.getTokenAmount)(bankBalance.balance, bankAccount, bankBalance.balanceType);
354
+ const spotMarketAccount = this.clearingHouse.getSpotMarketAccount(spotPosition.marketIndex);
355
+ const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.balance, spotMarketAccount, spotPosition.balanceType);
340
356
  let assetValue = tokenAmount
341
- .mul(this.getOracleDataForBank(bankAccount.bankIndex).price)
357
+ .mul(this.getOracleDataForSpotMarket(spotMarketAccount.marketIndex).price)
342
358
  .div(numericConstants_1.MARK_PRICE_PRECISION)
343
- .div(new _1.BN(10).pow(new _1.BN(bankAccount.decimals).sub(numericConstants_1.BANK_BALANCE_PRECISION_EXP)));
359
+ .div(new _1.BN(10).pow(new _1.BN(spotMarketAccount.decimals).sub(numericConstants_1.SPOT_MARKET_BALANCE_PRECISION_EXP)));
344
360
  if (withWeightMarginCategory !== undefined) {
345
- const weight = (0, bankBalance_1.calculateAssetWeight)(tokenAmount, bankAccount, withWeightMarginCategory);
346
- assetValue = assetValue.mul(weight).div(numericConstants_1.BANK_WEIGHT_PRECISION);
361
+ const weight = (0, spotBalance_1.calculateAssetWeight)(tokenAmount, spotMarketAccount, withWeightMarginCategory);
362
+ assetValue = assetValue.mul(weight).div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
347
363
  }
348
364
  return totalAssetValue.add(assetValue);
349
365
  }, numericConstants_1.ZERO);
350
366
  }
351
- getNetBankValue(withWeightMarginCategory) {
352
- return this.getBankAssetValue(undefined, withWeightMarginCategory).sub(this.getBankLiabilityValue(undefined, withWeightMarginCategory));
367
+ getNetSpotMarketValue(withWeightMarginCategory) {
368
+ return this.getSpotMarketAssetValue(undefined, withWeightMarginCategory).sub(this.getSpotMarketLiabilityValue(undefined, withWeightMarginCategory));
353
369
  }
354
370
  /**
355
371
  * calculates TotalCollateral: collateral + unrealized pnl
356
372
  * @returns : Precision QUOTE_PRECISION
357
373
  */
358
374
  getTotalCollateral(marginCategory = 'Initial') {
359
- return this.getBankAssetValue(undefined, marginCategory).add(this.getUnrealizedPNL(true, undefined, marginCategory));
375
+ return this.getSpotMarketAssetValue(undefined, marginCategory).add(this.getUnrealizedPNL(true, undefined, marginCategory));
360
376
  }
361
377
  /**
362
378
  * calculates sum of position value across all positions in margin system
363
379
  * @returns : Precision QUOTE_PRECISION
364
380
  */
365
381
  getTotalPositionValue() {
366
- return this.getUserAccount().positions.reduce((positionValue, marketPosition) => {
367
- const market = this.clearingHouse.getMarketAccount(marketPosition.marketIndex);
368
- const posVal = (0, margin_1.calculateBaseAssetValueWithOracle)(market, marketPosition, this.getOracleDataForMarket(market.marketIndex));
382
+ return this.getUserAccount().perpPositions.reduce((positionValue, perpPosition) => {
383
+ const market = this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex);
384
+ const posVal = (0, margin_1.calculateBaseAssetValueWithOracle)(market, perpPosition, this.getOracleDataForMarket(market.marketIndex));
369
385
  return positionValue.add(posVal);
370
386
  }, numericConstants_1.ZERO);
371
387
  }
@@ -375,7 +391,7 @@ class ClearingHouseUser {
375
391
  */
376
392
  getPositionValue(marketIndex, oraclePriceData) {
377
393
  const userPosition = this.getUserPosition(marketIndex) || this.getEmptyPosition(marketIndex);
378
- const market = this.clearingHouse.getMarketAccount(userPosition.marketIndex);
394
+ const market = this.clearingHouse.getPerpMarketAccount(userPosition.marketIndex);
379
395
  return (0, margin_1.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData);
380
396
  }
381
397
  getPositionSide(currentPosition) {
@@ -394,7 +410,7 @@ class ClearingHouseUser {
394
410
  * @returns : Precision MARK_PRICE_PRECISION
395
411
  */
396
412
  getPositionEstimatedExitPriceAndPnl(position, amountToClose, useAMMClose = false) {
397
- const market = this.clearingHouse.getMarketAccount(position.marketIndex);
413
+ const market = this.clearingHouse.getPerpMarketAccount(position.marketIndex);
398
414
  const entryPrice = (0, position_1.calculateEntryPrice)(position);
399
415
  const oraclePriceData = this.getOracleDataForMarket(position.marketIndex);
400
416
  if (amountToClose) {
@@ -447,7 +463,7 @@ class ClearingHouseUser {
447
463
  * @returns : Precision TEN_THOUSAND
448
464
  */
449
465
  getMaxLeverage(marketIndex, category = 'Initial') {
450
- const market = this.clearingHouse.getMarketAccount(marketIndex);
466
+ const market = this.clearingHouse.getPerpMarketAccount(marketIndex);
451
467
  const marginRatioCategory = (0, _1.calculateMarketMarginRatio)(market,
452
468
  // worstCaseBaseAssetAmount.abs(),
453
469
  numericConstants_1.ZERO, // todo
@@ -468,21 +484,24 @@ class ClearingHouseUser {
468
484
  }
469
485
  canBeLiquidated() {
470
486
  const totalCollateral = this.getTotalCollateral();
471
- const partialMaintenanceRequirement = this.getMaintenanceMarginRequirement();
472
- const marginRatio = this.getMarginRatio();
473
- const canLiquidate = totalCollateral.lt(partialMaintenanceRequirement);
474
- return [canLiquidate, marginRatio];
487
+ // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
488
+ let liquidationBuffer = undefined;
489
+ if (this.getUserAccount().beingLiquidated) {
490
+ liquidationBuffer = new _1.BN(this.clearingHouse.getStateAccount().liquidationMarginBufferRatio);
491
+ }
492
+ const maintenanceRequirement = this.getMaintenanceMarginRequirement(liquidationBuffer);
493
+ return totalCollateral.lt(maintenanceRequirement);
475
494
  }
476
495
  /**
477
496
  * Checks if any user position cumulative funding differs from respective market cumulative funding
478
497
  * @returns
479
498
  */
480
499
  needsToSettleFundingPayment() {
481
- for (const userPosition of this.getUserAccount().positions) {
500
+ for (const userPosition of this.getUserAccount().perpPositions) {
482
501
  if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
483
502
  continue;
484
503
  }
485
- const market = this.clearingHouse.getMarketAccount(userPosition.marketIndex);
504
+ const market = this.clearingHouse.getPerpMarketAccount(userPosition.marketIndex);
486
505
  if (market.amm.cumulativeFundingRateLong.eq(userPosition.lastCumulativeFundingRate) ||
487
506
  market.amm.cumulativeFundingRateShort.eq(userPosition.lastCumulativeFundingRate)) {
488
507
  continue;
@@ -493,12 +512,12 @@ class ClearingHouseUser {
493
512
  }
494
513
  /**
495
514
  * Calculate the liquidation price of a position, with optional parameter to calculate the liquidation price after a trade
496
- * @param marketPosition
515
+ * @param PerpPosition
497
516
  * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
498
517
  * @param partial
499
518
  * @returns Precision : MARK_PRICE_PRECISION
500
519
  */
501
- liquidationPrice(marketPosition, positionBaseSizeChange = numericConstants_1.ZERO) {
520
+ liquidationPrice(perpPosition, positionBaseSizeChange = numericConstants_1.ZERO) {
502
521
  // solves formula for example canBeLiquidated below
503
522
  /* example: assume BTC price is $40k (examine 10% up/down)
504
523
 
@@ -511,15 +530,16 @@ class ClearingHouseUser {
511
530
  3. (10k - 4k) / (100k - 4k) = 6k/96k => .0625 */
512
531
  const totalCollateral = this.getTotalCollateral();
513
532
  // calculate the total position value ignoring any value from the target market of the trade
514
- const totalPositionValueExcludingTargetMarket = this.getTotalPositionValueExcludingMarket(marketPosition.marketIndex);
515
- const currentMarketPosition = this.getUserPosition(marketPosition.marketIndex) ||
516
- this.getEmptyPosition(marketPosition.marketIndex);
517
- const currentMarketPositionBaseSize = currentMarketPosition.baseAssetAmount;
518
- const proposedBaseAssetAmount = currentMarketPositionBaseSize.add(positionBaseSizeChange);
533
+ const totalPositionValueExcludingTargetMarket = this.getTotalPositionValueExcludingMarket(perpPosition.marketIndex);
534
+ const currentPerpPosition = this.getUserPosition(perpPosition.marketIndex) ||
535
+ this.getEmptyPosition(perpPosition.marketIndex);
536
+ const currentPerpPositionBaseSize = currentPerpPosition.baseAssetAmount;
537
+ const proposedBaseAssetAmount = currentPerpPositionBaseSize.add(positionBaseSizeChange);
519
538
  // calculate position for current market after trade
520
- const proposedMarketPosition = {
521
- marketIndex: marketPosition.marketIndex,
539
+ const proposedPerpPosition = {
540
+ marketIndex: perpPosition.marketIndex,
522
541
  baseAssetAmount: proposedBaseAssetAmount,
542
+ remainderBaseAssetAmount: numericConstants_1.ZERO,
523
543
  quoteAssetAmount: new _1.BN(0),
524
544
  lastCumulativeFundingRate: numericConstants_1.ZERO,
525
545
  quoteEntryAmount: new _1.BN(0),
@@ -534,13 +554,13 @@ class ClearingHouseUser {
534
554
  };
535
555
  if (proposedBaseAssetAmount.eq(numericConstants_1.ZERO))
536
556
  return new _1.BN(-1);
537
- const market = this.clearingHouse.getMarketAccount(proposedMarketPosition.marketIndex);
538
- const proposedMarketPositionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, proposedMarketPosition, this.getOracleDataForMarket(market.marketIndex));
557
+ const market = this.clearingHouse.getPerpMarketAccount(proposedPerpPosition.marketIndex);
558
+ const proposedPerpPositionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, proposedPerpPosition, this.getOracleDataForMarket(market.marketIndex));
539
559
  // total position value after trade
540
- const totalPositionValueAfterTrade = totalPositionValueExcludingTargetMarket.add(proposedMarketPositionValue);
541
- const marginRequirementExcludingTargetMarket = this.getUserAccount().positions.reduce((totalMarginRequirement, position) => {
542
- if (!position.marketIndex.eq(marketPosition.marketIndex)) {
543
- const market = this.clearingHouse.getMarketAccount(position.marketIndex);
560
+ const totalPositionValueAfterTrade = totalPositionValueExcludingTargetMarket.add(proposedPerpPositionValue);
561
+ const marginRequirementExcludingTargetMarket = this.getUserAccount().perpPositions.reduce((totalMarginRequirement, position) => {
562
+ if (!position.marketIndex.eq(perpPosition.marketIndex)) {
563
+ const market = this.clearingHouse.getPerpMarketAccount(position.marketIndex);
544
564
  const positionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, position, this.getOracleDataForMarket(market.marketIndex));
545
565
  const marketMarginRequirement = positionValue
546
566
  .mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, position.baseAssetAmount.abs(), 'Maintenance')))
@@ -552,14 +572,14 @@ class ClearingHouseUser {
552
572
  const freeCollateralExcludingTargetMarket = totalCollateral.sub(marginRequirementExcludingTargetMarket);
553
573
  // if the position value after the trade is less than free collateral, there is no liq price
554
574
  if (totalPositionValueAfterTrade.lte(freeCollateralExcludingTargetMarket) &&
555
- proposedMarketPosition.baseAssetAmount.abs().gt(numericConstants_1.ZERO)) {
575
+ proposedPerpPosition.baseAssetAmount.abs().gt(numericConstants_1.ZERO)) {
556
576
  return new _1.BN(-1);
557
577
  }
558
- const marginRequirementAfterTrade = marginRequirementExcludingTargetMarket.add(proposedMarketPositionValue
559
- .mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, proposedMarketPosition.baseAssetAmount.abs(), 'Maintenance')))
578
+ const marginRequirementAfterTrade = marginRequirementExcludingTargetMarket.add(proposedPerpPositionValue
579
+ .mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, proposedPerpPosition.baseAssetAmount.abs(), 'Maintenance')))
560
580
  .div(numericConstants_1.MARGIN_PRECISION));
561
581
  const freeCollateralAfterTrade = totalCollateral.sub(marginRequirementAfterTrade);
562
- const marketMaxLeverage = this.getMaxLeverage(proposedMarketPosition.marketIndex, 'Maintenance');
582
+ const marketMaxLeverage = this.getMaxLeverage(proposedPerpPosition.marketIndex, 'Maintenance');
563
583
  let priceDelta;
564
584
  if (proposedBaseAssetAmount.lt(numericConstants_1.ZERO)) {
565
585
  priceDelta = freeCollateralAfterTrade
@@ -579,13 +599,13 @@ class ClearingHouseUser {
579
599
  }
580
600
  let markPriceAfterTrade;
581
601
  if (positionBaseSizeChange.eq(numericConstants_1.ZERO)) {
582
- markPriceAfterTrade = (0, _1.calculateMarkPrice)(this.clearingHouse.getMarketAccount(marketPosition.marketIndex), this.getOracleDataForMarket(marketPosition.marketIndex));
602
+ markPriceAfterTrade = (0, _1.calculateMarkPrice)(this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex), this.getOracleDataForMarket(perpPosition.marketIndex));
583
603
  }
584
604
  else {
585
605
  const direction = positionBaseSizeChange.gt(numericConstants_1.ZERO)
586
606
  ? _1.PositionDirection.LONG
587
607
  : _1.PositionDirection.SHORT;
588
- markPriceAfterTrade = (0, _1.calculateTradeSlippage)(direction, positionBaseSizeChange.abs(), this.clearingHouse.getMarketAccount(marketPosition.marketIndex), 'base', this.getOracleDataForMarket(marketPosition.marketIndex))[3]; // newPrice after swap
608
+ markPriceAfterTrade = (0, _1.calculateTradeSlippage)(direction, positionBaseSizeChange.abs(), this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex), 'base', this.getOracleDataForMarket(perpPosition.marketIndex))[3]; // newPrice after swap
589
609
  }
590
610
  if (priceDelta.gt(markPriceAfterTrade)) {
591
611
  return new _1.BN(-1);
@@ -662,23 +682,23 @@ class ClearingHouseUser {
662
682
  else {
663
683
  // current leverage is greater than max leverage - can only reduce position size
664
684
  if (!targetingSameSide) {
665
- const market = this.clearingHouse.getMarketAccount(targetMarketIndex);
666
- const marketPositionValue = this.getPositionValue(targetMarketIndex, oracleData);
685
+ const market = this.clearingHouse.getPerpMarketAccount(targetMarketIndex);
686
+ const perpPositionValue = this.getPositionValue(targetMarketIndex, oracleData);
667
687
  const totalCollateral = this.getTotalCollateral();
668
688
  const marginRequirement = this.getInitialMarginRequirement();
669
- const marginFreedByClosing = marketPositionValue
689
+ const marginFreedByClosing = perpPositionValue
670
690
  .mul(new _1.BN(market.marginRatioInitial))
671
691
  .div(numericConstants_1.MARGIN_PRECISION);
672
692
  const marginRequirementAfterClosing = marginRequirement.sub(marginFreedByClosing);
673
693
  if (marginRequirementAfterClosing.gt(totalCollateral)) {
674
- maxPositionSize = marketPositionValue;
694
+ maxPositionSize = perpPositionValue;
675
695
  }
676
696
  else {
677
697
  const freeCollateralAfterClose = totalCollateral.sub(marginRequirementAfterClosing);
678
698
  const buyingPowerAfterClose = freeCollateralAfterClose
679
699
  .mul(this.getMaxLeverage(targetMarketIndex))
680
700
  .div(numericConstants_1.TEN_THOUSAND);
681
- maxPositionSize = marketPositionValue.add(buyingPowerAfterClose);
701
+ maxPositionSize = perpPositionValue.add(buyingPowerAfterClose);
682
702
  }
683
703
  }
684
704
  else {
@@ -710,13 +730,13 @@ class ClearingHouseUser {
710
730
  currentPositionQuoteAmount = currentPositionQuoteAmount.neg();
711
731
  if (tradeSide === _1.PositionDirection.SHORT)
712
732
  tradeQuoteAmount = tradeQuoteAmount.neg();
713
- const currentMarketPositionAfterTrade = currentPositionQuoteAmount
733
+ const currentPerpPositionAfterTrade = currentPositionQuoteAmount
714
734
  .add(tradeQuoteAmount)
715
735
  .abs();
716
736
  const totalPositionAfterTradeExcludingTargetMarket = this.getTotalPositionValueExcludingMarket(targetMarketIndex);
717
737
  const totalCollateral = this.getTotalCollateral();
718
738
  if (totalCollateral.gt(numericConstants_1.ZERO)) {
719
- const newLeverage = currentMarketPositionAfterTrade
739
+ const newLeverage = currentPerpPositionAfterTrade
720
740
  .add(totalPositionAfterTradeExcludingTargetMarket)
721
741
  .abs()
722
742
  .mul(numericConstants_1.TEN_THOUSAND)
@@ -733,7 +753,7 @@ class ClearingHouseUser {
733
753
  * @returns feeForQuote : Precision QUOTE_PRECISION
734
754
  */
735
755
  calculateFeeForQuoteAmount(quoteAmount) {
736
- const feeStructure = this.clearingHouse.getStateAccount().feeStructure;
756
+ const feeStructure = this.clearingHouse.getStateAccount().perpFeeStructure;
737
757
  return quoteAmount
738
758
  .mul(feeStructure.feeNumerator)
739
759
  .div(feeStructure.feeDenominator);
@@ -744,22 +764,22 @@ class ClearingHouseUser {
744
764
  * @returns positionValue : Precision QUOTE_PRECISION
745
765
  */
746
766
  getTotalPositionValueExcludingMarket(marketToIgnore) {
747
- const currentMarketPosition = this.getUserPosition(marketToIgnore) ||
767
+ const currentPerpPosition = this.getUserPosition(marketToIgnore) ||
748
768
  this.getEmptyPosition(marketToIgnore);
749
769
  const oracleData = this.getOracleDataForMarket(marketToIgnore);
750
- let currentMarketPositionValueUSDC = numericConstants_1.ZERO;
751
- if (currentMarketPosition) {
752
- currentMarketPositionValueUSDC = this.getPositionValue(marketToIgnore, oracleData);
770
+ let currentPerpPositionValueUSDC = numericConstants_1.ZERO;
771
+ if (currentPerpPosition) {
772
+ currentPerpPositionValueUSDC = this.getPositionValue(marketToIgnore, oracleData);
753
773
  }
754
- return this.getTotalPositionValue().sub(currentMarketPositionValueUSDC);
774
+ return this.getTotalPositionValue().sub(currentPerpPositionValueUSDC);
755
775
  }
756
776
  getOracleDataForMarket(marketIndex) {
757
- const oracleKey = this.clearingHouse.getMarketAccount(marketIndex).amm.oracle;
777
+ const oracleKey = this.clearingHouse.getPerpMarketAccount(marketIndex).amm.oracle;
758
778
  const oracleData = this.clearingHouse.getOraclePriceDataAndSlot(oracleKey).data;
759
779
  return oracleData;
760
780
  }
761
- getOracleDataForBank(bankIndex) {
762
- const oracleKey = this.clearingHouse.getBankAccount(bankIndex).oracle;
781
+ getOracleDataForSpotMarket(marketIndex) {
782
+ const oracleKey = this.clearingHouse.getSpotMarketAccount(marketIndex).oracle;
763
783
  const oracleData = this.clearingHouse.getOraclePriceDataAndSlot(oracleKey).data;
764
784
  return oracleData;
765
785
  }
package/lib/config.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="bn.js" />
2
- import { MarketConfig } from './constants/markets';
3
- import { BankConfig } from './constants/banks';
2
+ import { PerpMarketConfig } from './constants/perpMarkets';
3
+ import { SpotMarketConfig } from './constants/spotMarkets';
4
4
  import { BN } from '@project-serum/anchor';
5
5
  import { OracleInfo } from './oracles/types';
6
6
  declare type DriftConfig = {
@@ -8,8 +8,8 @@ declare type DriftConfig = {
8
8
  PYTH_ORACLE_MAPPING_ADDRESS: string;
9
9
  CLEARING_HOUSE_PROGRAM_ID: string;
10
10
  USDC_MINT_ADDRESS: string;
11
- MARKETS: MarketConfig[];
12
- BANKS: BankConfig[];
11
+ PERP_MARKETS: PerpMarketConfig[];
12
+ SPOT_MARKETS: SpotMarketConfig[];
13
13
  };
14
14
  export declare type DriftEnv = 'devnet' | 'mainnet-beta';
15
15
  export declare const configs: {
@@ -27,9 +27,9 @@ export declare const initialize: (props: {
27
27
  env: DriftEnv;
28
28
  overrideEnv?: Partial<DriftConfig>;
29
29
  }) => DriftConfig;
30
- export declare function getMarketsBanksAndOraclesForSubscription(env: DriftEnv): {
31
- marketIndexes: BN[];
32
- bankIndexes: BN[];
30
+ export declare function getMarketsAndOraclesForSubscription(env: DriftEnv): {
31
+ perpMarketIndexes: BN[];
32
+ spotMarketIndexes: BN[];
33
33
  oracleInfos: OracleInfo[];
34
34
  };
35
35
  export {};
package/lib/config.js CHANGED
@@ -1,24 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMarketsBanksAndOraclesForSubscription = exports.initialize = exports.getConfig = exports.configs = void 0;
4
- const markets_1 = require("./constants/markets");
5
- const banks_1 = require("./constants/banks");
3
+ exports.getMarketsAndOraclesForSubscription = exports.initialize = exports.getConfig = exports.configs = void 0;
4
+ const perpMarkets_1 = require("./constants/perpMarkets");
5
+ const spotMarkets_1 = require("./constants/spotMarkets");
6
6
  exports.configs = {
7
7
  devnet: {
8
8
  ENV: 'devnet',
9
9
  PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
10
10
  CLEARING_HOUSE_PROGRAM_ID: '3v1iEjbSSLSSYyt1pmx4UB5rqJGurmz71RibXF7X6UF3',
11
11
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
12
- MARKETS: markets_1.DevnetMarkets,
13
- BANKS: banks_1.DevnetBanks,
12
+ PERP_MARKETS: perpMarkets_1.DevnetPerpMarkets,
13
+ SPOT_MARKETS: spotMarkets_1.DevnetSpotMarkets,
14
14
  },
15
15
  'mainnet-beta': {
16
16
  ENV: 'mainnet-beta',
17
17
  PYTH_ORACLE_MAPPING_ADDRESS: 'AHtgzX45WTKfkPG53L6WYhGEXwQkN1BVknET3sVsLL8J',
18
18
  CLEARING_HOUSE_PROGRAM_ID: 'dammHkt7jmytvbS3nHTxQNEcP59aE57nxwV21YdqEDN',
19
19
  USDC_MINT_ADDRESS: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
20
- MARKETS: markets_1.MainnetMarkets,
21
- BANKS: banks_1.MainnetBanks,
20
+ PERP_MARKETS: perpMarkets_1.MainnetMarkets,
21
+ SPOT_MARKETS: spotMarkets_1.MainnetSpotMarkets,
22
22
  },
23
23
  };
24
24
  let currentConfig = exports.configs.devnet;
@@ -40,28 +40,28 @@ const initialize = (props) => {
40
40
  return currentConfig;
41
41
  };
42
42
  exports.initialize = initialize;
43
- function getMarketsBanksAndOraclesForSubscription(env) {
44
- const marketIndexes = [];
45
- const bankIndexes = [];
43
+ function getMarketsAndOraclesForSubscription(env) {
44
+ const perpMarketIndexes = [];
45
+ const spotMarketIndexes = [];
46
46
  const oracleInfos = new Map();
47
- for (const market of markets_1.Markets[env]) {
48
- marketIndexes.push(market.marketIndex);
47
+ for (const market of perpMarkets_1.PerpMarkets[env]) {
48
+ perpMarketIndexes.push(market.marketIndex);
49
49
  oracleInfos.set(market.oracle.toString(), {
50
50
  publicKey: market.oracle,
51
51
  source: market.oracleSource,
52
52
  });
53
53
  }
54
- for (const bank of banks_1.Banks[env]) {
55
- bankIndexes.push(bank.bankIndex);
56
- oracleInfos.set(bank.oracle.toString(), {
57
- publicKey: bank.oracle,
58
- source: bank.oracleSource,
54
+ for (const spotMarket of spotMarkets_1.SpotMarkets[env]) {
55
+ spotMarketIndexes.push(spotMarket.marketIndex);
56
+ oracleInfos.set(spotMarket.oracle.toString(), {
57
+ publicKey: spotMarket.oracle,
58
+ source: spotMarket.oracleSource,
59
59
  });
60
60
  }
61
61
  return {
62
- marketIndexes,
63
- bankIndexes,
62
+ perpMarketIndexes: perpMarketIndexes,
63
+ spotMarketIndexes: spotMarketIndexes,
64
64
  oracleInfos: Array.from(oracleInfos.values()),
65
65
  };
66
66
  }
67
- exports.getMarketsBanksAndOraclesForSubscription = getMarketsBanksAndOraclesForSubscription;
67
+ exports.getMarketsAndOraclesForSubscription = getMarketsAndOraclesForSubscription;