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