@drift-labs/sdk 0.1.12 → 0.1.13

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 (56) hide show
  1. package/lib/accounts/types.d.ts +0 -21
  2. package/lib/accounts/types.d.ts.map +1 -1
  3. package/lib/clearingHouseUser.d.ts +27 -3
  4. package/lib/clearingHouseUser.d.ts.map +1 -1
  5. package/lib/clearingHouseUser.js +195 -47
  6. package/lib/constants/markets.d.ts.map +1 -1
  7. package/lib/constants/markets.js +7 -0
  8. package/lib/constants/numericConstants.d.ts +1 -0
  9. package/lib/constants/numericConstants.d.ts.map +1 -1
  10. package/lib/constants/numericConstants.js +2 -1
  11. package/lib/examples/makeTradeExample.d.ts.map +1 -1
  12. package/lib/examples/makeTradeExample.js +14 -13
  13. package/lib/idl/clearing_house.json +94 -42
  14. package/lib/index.d.ts +2 -1
  15. package/lib/index.d.ts.map +1 -1
  16. package/lib/index.js +2 -1
  17. package/lib/math/amm.d.ts +26 -1
  18. package/lib/math/amm.d.ts.map +1 -1
  19. package/lib/math/amm.js +84 -10
  20. package/lib/math/funding.d.ts +6 -6
  21. package/lib/math/funding.d.ts.map +1 -1
  22. package/lib/math/funding.js +41 -22
  23. package/lib/math/insuranceFund.d.ts +14 -0
  24. package/lib/math/insuranceFund.d.ts.map +1 -0
  25. package/lib/math/insuranceFund.js +35 -0
  26. package/lib/math/position.d.ts +1 -1
  27. package/lib/math/position.d.ts.map +1 -1
  28. package/lib/math/position.js +15 -23
  29. package/lib/math/trade.d.ts +1 -1
  30. package/lib/math/trade.d.ts.map +1 -1
  31. package/lib/math/trade.js +9 -4
  32. package/lib/types.d.ts +0 -13
  33. package/lib/types.d.ts.map +1 -1
  34. package/lib/wallet.d.ts +10 -0
  35. package/lib/wallet.d.ts.map +1 -0
  36. package/lib/wallet.js +35 -0
  37. package/package.json +2 -2
  38. package/src/accounts/types.ts +0 -27
  39. package/src/clearingHouse.ts +2 -2
  40. package/src/clearingHouseUser.ts +282 -65
  41. package/src/constants/markets.ts +7 -0
  42. package/src/constants/numericConstants.ts +3 -0
  43. package/src/examples/makeTradeExample.ts +2 -1
  44. package/src/idl/clearing_house.json +94 -42
  45. package/src/index.ts +2 -1
  46. package/src/math/amm.ts +119 -12
  47. package/src/math/funding.ts +47 -25
  48. package/src/math/insuranceFund.ts +22 -0
  49. package/src/math/position.ts +15 -28
  50. package/src/math/trade.ts +9 -5
  51. package/src/types.ts +0 -14
  52. package/src/wallet.ts +22 -0
  53. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -29
  54. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts.map +0 -1
  55. package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
  56. package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -179
@@ -37,27 +37,6 @@ export interface ClearingHouseAccountSubscriber {
37
37
  getCurveHistoryAccount(): CurveHistoryAccount;
38
38
  getLiquidationHistoryAccount(): LiquidationHistoryAccount;
39
39
  }
40
- export interface HistoryAccountEvents {
41
- fundingPaymentHistoryAccountUpdate: (payload: FundingPaymentHistoryAccount) => void;
42
- fundingRateHistoryAccountUpdate: (payload: FundingRateHistoryAccount) => void;
43
- tradeHistoryAccountUpdate: (payload: TradeHistoryAccount) => void;
44
- liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
45
- depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
46
- curveHistoryAccountUpdate: (payload: CurveHistoryAccount) => void;
47
- update: void;
48
- }
49
- export interface HistoryAccountSubscriber {
50
- eventEmitter: StrictEventEmitter<EventEmitter, HistoryAccountEvents>;
51
- isSubscribed: boolean;
52
- subscribe(): Promise<boolean>;
53
- unsubscribe(): Promise<void>;
54
- getTradeHistoryAccount(): TradeHistoryAccount;
55
- getDepositHistoryAccount(): DepositHistoryAccount;
56
- getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount;
57
- getFundingRateHistoryAccount(): FundingRateHistoryAccount;
58
- getCurveHistoryAccount(): CurveHistoryAccount;
59
- getLiquidationHistoryAccount(): LiquidationHistoryAccount;
60
- }
61
40
  export interface UserAccountEvents {
62
41
  userAccountData: (payload: UserAccount) => void;
63
42
  userPositionsData: (payload: UserPositionsAccount) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/accounts/types.ts"],"names":[],"mappings":";AAAA,OAAO,EACN,mBAAmB,EACnB,qBAAqB,EACrB,4BAA4B,EAC5B,yBAAyB,EACzB,yBAAyB,EACzB,cAAc,EACd,YAAY,EACZ,mBAAmB,EACnB,WAAW,EACX,oBAAoB,EACpB,MAAM,UAAU,CAAC;AAClB,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEtC,MAAM,WAAW,iBAAiB,CAAC,CAAC;IACnC,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,SAAS,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACtD,WAAW,IAAI,IAAI,CAAC;CACpB;AAED,qBAAa,kBAAmB,SAAQ,KAAK;IAC5C,IAAI,SAAwB;CAC5B;AAED,MAAM,WAAW,0BAA0B;IAC1C,kBAAkB,EAAE,CAAC,OAAO,EAAE,YAAY,KAAK,IAAI,CAAC;IACpD,oBAAoB,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC;IACxD,kCAAkC,EAAE,CACnC,OAAO,EAAE,4BAA4B,KACjC,IAAI,CAAC;IACV,+BAA+B,EAAE,CAAC,OAAO,EAAE,yBAAyB,KAAK,IAAI,CAAC;IAC9E,yBAAyB,EAAE,CAAC,OAAO,EAAE,mBAAmB,KAAK,IAAI,CAAC;IAClE,+BAA+B,EAAE,CAAC,OAAO,EAAE,yBAAyB,KAAK,IAAI,CAAC;IAC9E,2BAA2B,EAAE,CAAC,OAAO,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BAAqB,CAAC,EAAE,yBAAyB,EAAE,GACjD,OAAO,CAAC,OAAO,CAAC,CAAC;IAEpB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE7B,eAAe,IAAI,YAAY,CAAC;IAChC,iBAAiB,IAAI,cAAc,CAAC;IACpC,sBAAsB,IAAI,mBAAmB,CAAC;IAC9C,wBAAwB,IAAI,qBAAqB,CAAC;IAClD,+BAA+B,IAAI,4BAA4B,CAAC;IAChE,4BAA4B,IAAI,yBAAyB,CAAC;IAC1D,sBAAsB,IAAI,mBAAmB,CAAC;IAC9C,4BAA4B,IAAI,yBAAyB,CAAC;CAC1D;AAED,MAAM,WAAW,iBAAiB;IACjC,eAAe,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,CAAC;IAChD,iBAAiB,EAAE,CAAC,OAAO,EAAE,oBAAoB,KAAK,IAAI,CAAC;IAC3D,MAAM,EAAE,IAAI,CAAC;CACb;AAED,MAAM,WAAW,qBAAqB;IACrC,YAAY,EAAE,kBAAkB,CAAC,YAAY,EAAE,iBAAiB,CAAC,CAAC;IAClE,YAAY,EAAE,OAAO,CAAC;IAEtB,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9B,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE7B,cAAc,IAAI,WAAW,CAAC;IAC9B,uBAAuB,IAAI,oBAAoB,CAAC;CAChD"}
@@ -25,11 +25,12 @@ export declare class ClearingHouseUser {
25
25
  getUserAccount(): UserAccount;
26
26
  getUserPositionsAccount(): UserPositionsAccount;
27
27
  /**
28
- * Gets the user's current position for a given market
28
+ * Gets the user's current position for a given market. If the user has no position returns undefined
29
29
  * @param marketIndex
30
30
  * @returns userPosition
31
31
  */
32
- getUserPosition(marketIndex: BN): UserPosition;
32
+ getUserPosition(marketIndex: BN): UserPosition | undefined;
33
+ getEmptyPosition(marketIndex: BN): UserPosition;
33
34
  getUserAccountPublicKey(): Promise<PublicKey>;
34
35
  exists(): Promise<boolean>;
35
36
  /**
@@ -72,7 +73,7 @@ export declare class ClearingHouseUser {
72
73
  * calculates average exit price for closing 100% of position
73
74
  * @returns : Precision MARK_PRICE_PRECISION
74
75
  */
75
- getPositionEstimatedExitPrice(position: UserPosition, amountToClose?: BN): BN;
76
+ getPositionEstimatedExitPriceAndPnl(position: UserPosition, amountToClose?: BN): [BN, BN];
76
77
  /**
77
78
  * calculates current user leverage across all positions
78
79
  * @returns : Precision TEN_THOUSAND
@@ -95,6 +96,14 @@ export declare class ClearingHouseUser {
95
96
  * @returns
96
97
  */
97
98
  needsToSettleFundingPayment(): boolean;
99
+ /**
100
+ * Calculate the liquidation price of a position, with optional parameter to calculate the liquidation price after a trade
101
+ * @param targetMarket
102
+ * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
103
+ * @param partial
104
+ * @returns Precision : MARK_PRICE_PRECISION
105
+ */
106
+ liquidationPriceOld(targetMarket: Pick<UserPosition, 'marketIndex'>, positionBaseSizeChange?: BN, partial?: boolean): BN;
98
107
  /**
99
108
  * Calculate the liquidation price of a position, with optional parameter to calculate the liquidation price after a trade
100
109
  * @param targetMarket
@@ -112,6 +121,21 @@ export declare class ClearingHouseUser {
112
121
  liquidationPriceAfterClose(positionMarketIndex: BN, closeQuoteAmount: BN): BN;
113
122
  /**
114
123
  * Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
124
+ *
125
+ * To Calculate Max Quote Available:
126
+ *
127
+ * Case 1: SameSide
128
+ * => Remaining quote to get to maxLeverage
129
+ *
130
+ * Case 2: NOT SameSide && currentLeverage <= maxLeverage
131
+ * => Current opposite position x2 + remaining to get to maxLeverage
132
+ *
133
+ * Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
134
+ * => strictly reduce current position size
135
+ *
136
+ * Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
137
+ * => current position + remaining to get to maxLeverage
138
+ *
115
139
  * @param marketIndex
116
140
  * @param tradeSide
117
141
  * @param userMaxLeverageSetting - leverage : Precision TEN_THOUSAND
@@ -1 +1 @@
1
- {"version":3,"file":"clearingHouseUser.d.ts","sourceRoot":"","sources":["../src/clearingHouseUser.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,MAAM,OAAO,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAW1E,OAAO,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAE5E,OAAO,EAKN,iBAAiB,EACjB,MAAM,GAAG,CAAC;AAGX,qBAAa,iBAAiB;IAC7B,aAAa,EAAE,aAAa,CAAC;IAC7B,SAAS,EAAE,SAAS,CAAC;IACrB,iBAAiB,EAAE,qBAAqB,CAAC;IACzC,oBAAoB,CAAC,EAAE,SAAS,CAAC;IACjC,YAAY,UAAS;IACrB,YAAY,EAAE,kBAAkB,CAAC,YAAY,EAAE,iBAAiB,CAAC,CAAC;WAEpD,IAAI,CACjB,aAAa,EAAE,aAAa,EAC5B,SAAS,EAAE,SAAS,GAClB,iBAAiB;gBASnB,aAAa,EAAE,aAAa,EAC5B,SAAS,EAAE,SAAS,EACpB,iBAAiB,EAAE,qBAAqB;IAQzC;;;OAGG;IACU,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAQ7B,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAKlC,cAAc,IAAI,WAAW;IAI7B,uBAAuB,IAAI,oBAAoB;IAItD;;;;OAIG;IACI,eAAe,CAAC,WAAW,EAAE,EAAE,GAAG,YAAY;IAaxC,uBAAuB,IAAI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OAMG;IACI,mBAAmB,CACzB,YAAY,EAAE,IAAI,CAAC,YAAY,EAAE,aAAa,CAAC,EAC/C,sBAAsB,GAAE,EAAS,EACjC,OAAO,UAAQ,GACb,EAAE;IAkHL;;;;;;OAMG;IACI,gBAAgB,CACtB,YAAY,EAAE,IAAI,CAAC,YAAY,EAAE,aAAa,CAAC,EAC/C,sBAAsB,GAAE,EAAS,EACjC,OAAO,UAAQ,GACb,EAAE;IA8GL;;;;;OAKG;IACI,0BAA0B,CAChC,mBAAmB,EAAE,EAAE,EACvB,gBAAgB,EAAE,EAAE,GAClB,EAAE;IAuBL;;;;;;;;;;;;;;;;;;;;;OAqBG;IACI,mBAAmB,CACzB,iBAAiB,EAAE,EAAE,EACrB,SAAS,EAAE,iBAAiB,EAC5B,sBAAsB,EAAE,EAAE,GACxB,EAAE;IAgGL;;;;;;OAMG;IACI,8BAA8B,CACpC,iBAAiB,EAAE,EAAE,EACrB,gBAAgB,EAAE,EAAE,EACpB,SAAS,EAAE,iBAAiB,GAC1B,EAAE;IAuCL;;;;OAIG;IACI,0BAA0B,CAAC,WAAW,EAAE,EAAE,GAAG,EAAE;IAQtD;;;;OAIG;IACH,OAAO,CAAC,oCAAoC;CAY5C"}
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.ClearingHouseUser = void 0;
16
16
  const bn_js_1 = __importDefault(require("bn.js"));
17
+ const position_1 = require("./math/position");
17
18
  const numericConstants_1 = require("./constants/numericConstants");
18
19
  const defaultUserAccountSubscriber_1 = require("./accounts/defaultUserAccountSubscriber");
19
20
  const _1 = require(".");
@@ -55,18 +56,20 @@ class ClearingHouseUser {
55
56
  return this.accountSubscriber.getUserPositionsAccount();
56
57
  }
57
58
  /**
58
- * Gets the user's current position for a given market
59
+ * Gets the user's current position for a given market. If the user has no position returns undefined
59
60
  * @param marketIndex
60
61
  * @returns userPosition
61
62
  */
62
63
  getUserPosition(marketIndex) {
63
- var _a;
64
- return ((_a = this.getUserPositionsAccount().positions.find((position) => position.marketIndex.eq(marketIndex))) !== null && _a !== void 0 ? _a : {
64
+ return this.getUserPositionsAccount().positions.find((position) => position.marketIndex.eq(marketIndex));
65
+ }
66
+ getEmptyPosition(marketIndex) {
67
+ return {
65
68
  baseAssetAmount: numericConstants_1.ZERO,
66
69
  lastCumulativeFundingRate: numericConstants_1.ZERO,
67
70
  marketIndex,
68
71
  quoteAssetAmount: numericConstants_1.ZERO,
69
- });
72
+ };
70
73
  }
71
74
  getUserAccountPublicKey() {
72
75
  return __awaiter(this, void 0, void 0, function* () {
@@ -149,7 +152,7 @@ class ClearingHouseUser {
149
152
  * @returns : Precision QUOTE_PRECISION
150
153
  */
151
154
  getPositionValue(marketIndex) {
152
- const userPosition = this.getUserPosition(marketIndex);
155
+ const userPosition = this.getUserPosition(marketIndex) || this.getEmptyPosition(marketIndex);
153
156
  const market = this.clearingHouse.getMarket(userPosition.marketIndex);
154
157
  return _1.calculateBaseAssetValue(market, userPosition);
155
158
  }
@@ -168,11 +171,12 @@ class ClearingHouseUser {
168
171
  * calculates average exit price for closing 100% of position
169
172
  * @returns : Precision MARK_PRICE_PRECISION
170
173
  */
171
- getPositionEstimatedExitPrice(position, amountToClose) {
174
+ getPositionEstimatedExitPriceAndPnl(position, amountToClose) {
172
175
  const market = this.clearingHouse.getMarket(position.marketIndex);
176
+ const entryPrice = position_1.calculateEntryPrice(position);
173
177
  if (amountToClose) {
174
178
  if (amountToClose.eq(numericConstants_1.ZERO)) {
175
- return _1.calculateMarkPrice(market);
179
+ return [_1.calculateMarkPrice(market), numericConstants_1.ZERO];
176
180
  }
177
181
  position = {
178
182
  baseAssetAmount: amountToClose,
@@ -183,12 +187,18 @@ class ClearingHouseUser {
183
187
  }
184
188
  const baseAssetValue = _1.calculateBaseAssetValue(market, position);
185
189
  if (position.baseAssetAmount.eq(numericConstants_1.ZERO)) {
186
- return numericConstants_1.ZERO;
190
+ return [numericConstants_1.ZERO, numericConstants_1.ZERO];
187
191
  }
188
- return baseAssetValue
192
+ const exitPrice = baseAssetValue
189
193
  .mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
190
194
  .mul(numericConstants_1.MARK_PRICE_PRECISION)
191
195
  .div(position.baseAssetAmount.abs());
196
+ const pnlPerBase = exitPrice.sub(entryPrice);
197
+ const pnl = pnlPerBase
198
+ .mul(position.baseAssetAmount)
199
+ .div(numericConstants_1.MARK_PRICE_PRECISION)
200
+ .div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
201
+ return [exitPrice, pnl];
192
202
  }
193
203
  /**
194
204
  * calculates current user leverage across all positions
@@ -269,7 +279,7 @@ class ClearingHouseUser {
269
279
  * @param partial
270
280
  * @returns Precision : MARK_PRICE_PRECISION
271
281
  */
272
- liquidationPrice(targetMarket, positionBaseSizeChange = numericConstants_1.ZERO, partial = false) {
282
+ liquidationPriceOld(targetMarket, positionBaseSizeChange = numericConstants_1.ZERO, partial = false) {
273
283
  // +/-(margin_ratio-liq_ratio) * price_now = price_liq
274
284
  // todo: margin_ratio is not symmetric on price action (both numer and denom change)
275
285
  // margin_ratio = collateral / base_asset_value
@@ -286,10 +296,9 @@ class ClearingHouseUser {
286
296
  const totalCollateralUSDC = this.getTotalCollateral();
287
297
  // calculate the total position value ignoring any value from the target market of the trade
288
298
  const totalCurrentPositionValueIgnoringTargetUSDC = this.getTotalPositionValueExcludingMarket(targetMarket.marketIndex);
289
- const currentMarketPosition = this.getUserPosition(targetMarket.marketIndex);
290
- const currentMarketPositionBaseSize = currentMarketPosition
291
- ? currentMarketPosition.baseAssetAmount
292
- : numericConstants_1.ZERO;
299
+ const currentMarketPosition = this.getUserPosition(targetMarket.marketIndex) ||
300
+ this.getEmptyPosition(targetMarket.marketIndex);
301
+ const currentMarketPositionBaseSize = currentMarketPosition.baseAssetAmount;
293
302
  // calculate position for current market after trade
294
303
  const proposedMarketPosition = {
295
304
  marketIndex: targetMarket.marketIndex,
@@ -301,8 +310,16 @@ class ClearingHouseUser {
301
310
  const proposedMarketPositionValueUSDC = _1.calculateBaseAssetValue(market, proposedMarketPosition);
302
311
  // total position value after trade
303
312
  const targetTotalPositionValueUSDC = totalCurrentPositionValueIgnoringTargetUSDC.add(proposedMarketPositionValueUSDC);
313
+ let totalFreeCollateralUSDC = this.getTotalCollateral().sub(this.getTotalPositionValue()
314
+ .mul(numericConstants_1.TEN_THOUSAND)
315
+ .div(this.getMaxLeverage('Maintenance')));
316
+ if (partial) {
317
+ totalFreeCollateralUSDC = this.getTotalCollateral().sub(this.getTotalPositionValue()
318
+ .mul(numericConstants_1.TEN_THOUSAND)
319
+ .div(this.getMaxLeverage('Partial')));
320
+ }
304
321
  // if the position value after the trade is less than total collateral, there is no liq price
305
- if (targetTotalPositionValueUSDC.lte(totalCollateralUSDC) &&
322
+ if (targetTotalPositionValueUSDC.lte(totalFreeCollateralUSDC) &&
306
323
  proposedMarketPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
307
324
  return new bn_js_1.default(-1);
308
325
  }
@@ -338,6 +355,83 @@ class ClearingHouseUser {
338
355
  const liqPrice = currentPrice.mul(pctChange).div(numericConstants_1.TEN_THOUSAND);
339
356
  return liqPrice;
340
357
  }
358
+ /**
359
+ * Calculate the liquidation price of a position, with optional parameter to calculate the liquidation price after a trade
360
+ * @param targetMarket
361
+ * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
362
+ * @param partial
363
+ * @returns Precision : MARK_PRICE_PRECISION
364
+ */
365
+ liquidationPrice(targetMarket, positionBaseSizeChange = numericConstants_1.ZERO, partial = false) {
366
+ // solves formula for example calc below
367
+ /* example: assume BTC price is $40k (examine 10% up/down)
368
+
369
+ if 10k deposit and levered 10x short BTC => BTC up $400 means:
370
+ 1. higher base_asset_value (+$4k)
371
+ 2. lower collateral (-$4k)
372
+ 3. (10k - 4k)/(100k + 4k) => 6k/104k => .0576
373
+
374
+ for 10x long, BTC down $400:
375
+ 3. (10k - 4k) / (100k - 4k) = 6k/96k => .0625 */
376
+ const tc = this.getTotalCollateral();
377
+ const tpv = this.getTotalPositionValue();
378
+ const partialLev = 16;
379
+ const maintLev = 20;
380
+ const thisLev = partial ? new bn_js_1.default(partialLev) : new bn_js_1.default(maintLev);
381
+ // calculate the total position value ignoring any value from the target market of the trade
382
+ const totalCurrentPositionValueIgnoringTargetUSDC = this.getTotalPositionValueExcludingMarket(targetMarket.marketIndex);
383
+ const currentMarketPosition = this.getUserPosition(targetMarket.marketIndex) ||
384
+ this.getEmptyPosition(targetMarket.marketIndex);
385
+ const currentMarketPositionBaseSize = currentMarketPosition.baseAssetAmount;
386
+ const proposedBaseAssetAmount = currentMarketPositionBaseSize.add(positionBaseSizeChange);
387
+ // calculate position for current market after trade
388
+ const proposedMarketPosition = {
389
+ marketIndex: targetMarket.marketIndex,
390
+ baseAssetAmount: proposedBaseAssetAmount,
391
+ lastCumulativeFundingRate: currentMarketPosition.lastCumulativeFundingRate,
392
+ quoteAssetAmount: new bn_js_1.default(0),
393
+ };
394
+ const market = this.clearingHouse.getMarket(proposedMarketPosition.marketIndex);
395
+ const proposedMarketPositionValueUSDC = _1.calculateBaseAssetValue(market, proposedMarketPosition);
396
+ // total position value after trade
397
+ const targetTotalPositionValueUSDC = totalCurrentPositionValueIgnoringTargetUSDC.add(proposedMarketPositionValueUSDC);
398
+ let totalFreeCollateralUSDC = tc.sub(totalCurrentPositionValueIgnoringTargetUSDC
399
+ .mul(numericConstants_1.TEN_THOUSAND)
400
+ .div(this.getMaxLeverage('Maintenance')));
401
+ if (partial) {
402
+ totalFreeCollateralUSDC = tc.sub(totalCurrentPositionValueIgnoringTargetUSDC
403
+ .mul(numericConstants_1.TEN_THOUSAND)
404
+ .div(this.getMaxLeverage('Partial')));
405
+ }
406
+ let priceDelt;
407
+ if (currentMarketPositionBaseSize.lt(numericConstants_1.ZERO)) {
408
+ priceDelt = tc
409
+ .mul(thisLev)
410
+ .sub(tpv)
411
+ .mul(numericConstants_1.PRICE_TO_QUOTE_PRECISION)
412
+ .div(thisLev.add(new bn_js_1.default(1)));
413
+ }
414
+ else {
415
+ priceDelt = tc
416
+ .mul(thisLev)
417
+ .sub(tpv)
418
+ .mul(numericConstants_1.PRICE_TO_QUOTE_PRECISION)
419
+ .div(thisLev.sub(new bn_js_1.default(1)));
420
+ }
421
+ const currentPrice = _1.calculateMarkPrice(this.clearingHouse.getMarket(targetMarket.marketIndex));
422
+ // if the position value after the trade is less than total collateral, there is no liq price
423
+ if (targetTotalPositionValueUSDC.lte(totalFreeCollateralUSDC) &&
424
+ proposedMarketPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
425
+ return new bn_js_1.default(-1);
426
+ }
427
+ if (proposedBaseAssetAmount.eq(numericConstants_1.ZERO))
428
+ return new bn_js_1.default(-1);
429
+ const eatMargin2 = priceDelt
430
+ .mul(numericConstants_1.AMM_RESERVE_PRECISION)
431
+ .div(proposedBaseAssetAmount);
432
+ const liqPrice = currentPrice.sub(eatMargin2);
433
+ return liqPrice;
434
+ }
341
435
  /**
342
436
  * Calculates the estimated liquidation price for a position after closing a quote amount of the position.
343
437
  * @param positionMarketIndex
@@ -345,7 +439,8 @@ class ClearingHouseUser {
345
439
  * @returns : Precision MARK_PRICE_PRECISION
346
440
  */
347
441
  liquidationPriceAfterClose(positionMarketIndex, closeQuoteAmount) {
348
- const currentPosition = this.getUserPosition(positionMarketIndex);
442
+ const currentPosition = this.getUserPosition(positionMarketIndex) ||
443
+ this.getEmptyPosition(positionMarketIndex);
349
444
  const closeBaseAmount = currentPosition.baseAssetAmount
350
445
  .mul(closeQuoteAmount)
351
446
  .div(currentPosition.quoteAssetAmount)
@@ -359,31 +454,42 @@ class ClearingHouseUser {
359
454
  }
360
455
  /**
361
456
  * Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
457
+ *
458
+ * To Calculate Max Quote Available:
459
+ *
460
+ * Case 1: SameSide
461
+ * => Remaining quote to get to maxLeverage
462
+ *
463
+ * Case 2: NOT SameSide && currentLeverage <= maxLeverage
464
+ * => Current opposite position x2 + remaining to get to maxLeverage
465
+ *
466
+ * Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
467
+ * => strictly reduce current position size
468
+ *
469
+ * Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
470
+ * => current position + remaining to get to maxLeverage
471
+ *
362
472
  * @param marketIndex
363
473
  * @param tradeSide
364
474
  * @param userMaxLeverageSetting - leverage : Precision TEN_THOUSAND
365
475
  * @returns tradeSizeAllowed : Precision QUOTE_PRECISION
366
476
  */
367
477
  getMaxTradeSizeUSDC(targetMarketIndex, tradeSide, userMaxLeverageSetting) {
368
- // inline function which get's the current position size on the opposite side of the target trade
369
- const getOppositePositionValueUSDC = () => {
370
- if (!currentPosition)
371
- return numericConstants_1.ZERO;
372
- const side = tradeSide === _1.PositionDirection.SHORT ? 'short' : 'long';
373
- if (side === 'long' && (currentPosition === null || currentPosition === void 0 ? void 0 : currentPosition.baseAssetAmount.isNeg())) {
374
- return this.getPositionValue(targetMarketIndex);
375
- }
376
- else if (side === 'short' &&
377
- !(currentPosition === null || currentPosition === void 0 ? void 0 : currentPosition.baseAssetAmount.isNeg())) {
378
- return this.getPositionValue(targetMarketIndex);
379
- }
380
- return numericConstants_1.ZERO;
381
- };
382
- const currentPosition = this.getUserPosition(targetMarketIndex);
478
+ const currentPosition = this.getUserPosition(targetMarketIndex) ||
479
+ this.getEmptyPosition(targetMarketIndex);
480
+ const targetSide = tradeSide === _1.PositionDirection.SHORT ? 'short' : 'long';
481
+ const currentPositionSide = (currentPosition === null || currentPosition === void 0 ? void 0 : currentPosition.baseAssetAmount.isNeg())
482
+ ? 'short'
483
+ : 'long';
484
+ const targettingSameSide = !currentPosition
485
+ ? true
486
+ : targetSide === currentPositionSide;
487
+ // add any position we have on the opposite side of the current trade, because we can "flip" the size of this position without taking any extra leverage.
488
+ const oppositeSizeValueUSDC = targettingSameSide
489
+ ? numericConstants_1.ZERO
490
+ : this.getPositionValue(targetMarketIndex);
383
491
  // get current leverage
384
492
  const currentLeverage = this.getLeverage();
385
- // remaining leverage
386
- // let remainingLeverage = userMaxLeverageSetting;
387
493
  const remainingLeverage = bn_js_1.default.max(userMaxLeverageSetting.sub(currentLeverage), numericConstants_1.ZERO);
388
494
  // get total collateral
389
495
  const totalCollateral = this.getTotalCollateral();
@@ -391,9 +497,43 @@ class ClearingHouseUser {
391
497
  let maxPositionSize = remainingLeverage
392
498
  .mul(totalCollateral)
393
499
  .div(numericConstants_1.TEN_THOUSAND);
394
- // add any position we have on the opposite side of the current trade, because we can "flip" the size of this position without taking any extra leverage.
395
- const oppositeSizeValueUSDC = getOppositePositionValueUSDC();
396
- maxPositionSize = maxPositionSize.add(oppositeSizeValueUSDC);
500
+ if (userMaxLeverageSetting.sub(currentLeverage).gte(numericConstants_1.ZERO)) {
501
+ if (oppositeSizeValueUSDC.eq(numericConstants_1.ZERO)) {
502
+ // case 1 : Regular trade where current total position less than max, and no opposite position to account for
503
+ // do nothing
504
+ }
505
+ else {
506
+ // case 2 : trade where current total position less than max, but need to account for flipping the current position over to the other side
507
+ maxPositionSize = maxPositionSize.add(oppositeSizeValueUSDC.mul(new bn_js_1.default(2)));
508
+ }
509
+ }
510
+ else {
511
+ // current leverage is greater than max leverage - can only reduce position size
512
+ if (!targettingSameSide) {
513
+ const currentPositionQuoteSize = this.getPositionValue(targetMarketIndex);
514
+ const currentTotalQuoteSize = currentLeverage
515
+ .mul(totalCollateral)
516
+ .div(numericConstants_1.TEN_THOUSAND);
517
+ const otherPositionsTotalQuoteSize = currentTotalQuoteSize.sub(currentPositionQuoteSize);
518
+ const quoteValueOfMaxLeverage = userMaxLeverageSetting
519
+ .mul(totalCollateral)
520
+ .div(numericConstants_1.TEN_THOUSAND);
521
+ if (otherPositionsTotalQuoteSize
522
+ .sub(currentPositionQuoteSize)
523
+ .gte(quoteValueOfMaxLeverage)) {
524
+ // case 3: Can only reduce the current position because it will still be greater than max leverage
525
+ maxPositionSize = currentPositionQuoteSize;
526
+ }
527
+ else {
528
+ // case 4: Can reduce the position, and then take extra remaining quote to get to max leverage
529
+ const allowedQuoteSizeAfterClosingCurrentPosition = quoteValueOfMaxLeverage.sub(otherPositionsTotalQuoteSize);
530
+ maxPositionSize = currentPositionQuoteSize.add(allowedQuoteSizeAfterClosingCurrentPosition);
531
+ }
532
+ }
533
+ else {
534
+ // do nothing if targetting same side
535
+ }
536
+ }
397
537
  // subtract oneMillionth of maxPositionSize
398
538
  // => to avoid rounding errors when taking max leverage
399
539
  const oneMilli = maxPositionSize.div(numericConstants_1.QUOTE_PRECISION);
@@ -408,9 +548,10 @@ class ClearingHouseUser {
408
548
  * @returns leverageRatio : Precision TEN_THOUSAND
409
549
  */
410
550
  accountLeverageRatioAfterTrade(targetMarketIndex, tradeQuoteAmount, tradeSide) {
411
- const currentPosition = this.getUserPosition(targetMarketIndex);
412
- let currentPositionQuoteAmount = currentPosition.quoteAssetAmount;
413
- const currentSide = currentPosition.baseAssetAmount.isNeg()
551
+ const currentPosition = this.getUserPosition(targetMarketIndex) ||
552
+ this.getEmptyPosition(targetMarketIndex);
553
+ let currentPositionQuoteAmount = this.getPositionValue(targetMarketIndex);
554
+ const currentSide = currentPosition && currentPosition.baseAssetAmount.isNeg()
414
555
  ? _1.PositionDirection.SHORT
415
556
  : _1.PositionDirection.LONG;
416
557
  if (currentSide === _1.PositionDirection.SHORT)
@@ -421,11 +562,18 @@ class ClearingHouseUser {
421
562
  .add(tradeQuoteAmount)
422
563
  .abs();
423
564
  const totalPositionAfterTradeExcludingTargetMarket = this.getTotalPositionValueExcludingMarket(targetMarketIndex);
424
- return currentMarketPositionAfterTrade
425
- .add(totalPositionAfterTradeExcludingTargetMarket)
426
- .abs()
427
- .mul(numericConstants_1.TEN_THOUSAND)
428
- .div(this.getTotalCollateral());
565
+ const totalCollateral = this.getTotalCollateral();
566
+ if (totalCollateral.gt(numericConstants_1.ZERO)) {
567
+ const newLeverage = currentMarketPositionAfterTrade
568
+ .add(totalPositionAfterTradeExcludingTargetMarket)
569
+ .abs()
570
+ .mul(numericConstants_1.TEN_THOUSAND)
571
+ .div(totalCollateral);
572
+ return newLeverage;
573
+ }
574
+ else {
575
+ return new bn_js_1.default(0);
576
+ }
429
577
  }
430
578
  /**
431
579
  * Calculates how much fee will be taken for a given sized trade
@@ -444,11 +592,11 @@ class ClearingHouseUser {
444
592
  * @returns positionValue : Precision QUOTE_PRECISION
445
593
  */
446
594
  getTotalPositionValueExcludingMarket(marketToIgnore) {
447
- const currentMarketPosition = this.getUserPosition(marketToIgnore);
595
+ const currentMarketPosition = this.getUserPosition(marketToIgnore) ||
596
+ this.getEmptyPosition(marketToIgnore);
448
597
  let currentMarketPositionValueUSDC = numericConstants_1.ZERO;
449
598
  if (currentMarketPosition) {
450
- const market = this.clearingHouse.getMarket(currentMarketPosition.marketIndex);
451
- currentMarketPositionValueUSDC = _1.calculateBaseAssetValue(market, currentMarketPosition);
599
+ currentMarketPositionValueUSDC = this.getPositionValue(marketToIgnore);
452
600
  }
453
601
  return this.getTotalPositionValue().sub(currentMarketPositionValueUSDC);
454
602
  }
@@ -1 +1 @@
1
- {"version":3,"file":"markets.d.ts","sourceRoot":"","sources":["../../src/constants/markets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AAEvB,aAAK,MAAM,GAAG;IACb,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,EAAE,CAAC;IAChB,gBAAgB,EAAE,MAAM,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,eAAO,MAAM,OAAO,EAAE,MAAM,EA2C3B,CAAC"}
1
+ {"version":3,"file":"markets.d.ts","sourceRoot":"","sources":["../../src/constants/markets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AAEvB,aAAK,MAAM,GAAG;IACb,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,EAAE,CAAC;IAChB,gBAAgB,EAAE,MAAM,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,eAAO,MAAM,OAAO,EAAE,MAAM,EAkD3B,CAAC"}
@@ -48,4 +48,11 @@ exports.Markets = [
48
48
  devnetPythOracle: 'GwzBgrXb4PG59zjce24SF2b9JXbLEjJJTBkmytuEZj1b',
49
49
  mainnetPythOracle: '4CkQJBxhU8EZ2UjhigbtdaPbpTe6mqf811fipYBFbSYN',
50
50
  },
51
+ {
52
+ symbol: 'MATIC-PERP',
53
+ baseAssetSymbol: 'MATIC',
54
+ marketIndex: new bn_js_1.default(6),
55
+ devnetPythOracle: 'FBirwuDFuRAu4iSGc7RGxN5koHB7EJM1wbCmyPuQoGur',
56
+ mainnetPythOracle: '7KVswB9vkCgeM3SHP7aGDijvdRAHK8P5wi9JXViCrtYh',
57
+ },
51
58
  ];
@@ -13,4 +13,5 @@ export declare const PEG_PRECISION: BN;
13
13
  export declare const AMM_RESERVE_PRECISION: BN;
14
14
  export declare const AMM_TO_QUOTE_PRECISION_RATIO: BN;
15
15
  export declare const PRICE_TO_QUOTE_PRECISION: BN;
16
+ export declare const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO: BN;
16
17
  //# sourceMappingURL=numericConstants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"numericConstants.d.ts","sourceRoot":"","sources":["../../src/constants/numericConstants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AAEvB,eAAO,MAAM,IAAI,IAAY,CAAC;AAC9B,eAAO,MAAM,GAAG,IAAY,CAAC;AAC7B,eAAO,MAAM,YAAY,IAAgB,CAAC;AAC1C,eAAO,MAAM,MAAM,IAAkC,CAAC;AAEtD,eAAO,MAAM,YAAY,IAAY,CAAC;AACtC,eAAO,MAAM,sBAAsB,IAAc,CAAC;AAClD,eAAO,MAAM,yBAAyB,IAAc,CAAC;AAErD,eAAO,MAAM,eAAe,IAAkB,CAAC;AAC/C,eAAO,MAAM,oBAAoB,IAAmB,CAAC;AACrD,eAAO,MAAM,yBAAyB,IAAgB,CAAC;AACvD,eAAO,MAAM,aAAa,IAAe,CAAC;AAE1C,eAAO,MAAM,qBAAqB,IAAmB,CAAC;AACtD,eAAO,MAAM,4BAA4B,IACE,CAAC;AAC5C,eAAO,MAAM,wBAAwB,IACK,CAAC"}
1
+ {"version":3,"file":"numericConstants.d.ts","sourceRoot":"","sources":["../../src/constants/numericConstants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AAEvB,eAAO,MAAM,IAAI,IAAY,CAAC;AAC9B,eAAO,MAAM,GAAG,IAAY,CAAC;AAC7B,eAAO,MAAM,YAAY,IAAgB,CAAC;AAC1C,eAAO,MAAM,MAAM,IAAkC,CAAC;AAEtD,eAAO,MAAM,YAAY,IAAY,CAAC;AACtC,eAAO,MAAM,sBAAsB,IAAc,CAAC;AAClD,eAAO,MAAM,yBAAyB,IAAc,CAAC;AAErD,eAAO,MAAM,eAAe,IAAkB,CAAC;AAC/C,eAAO,MAAM,oBAAoB,IAAmB,CAAC;AACrD,eAAO,MAAM,yBAAyB,IAAgB,CAAC;AACvD,eAAO,MAAM,aAAa,IAAe,CAAC;AAE1C,eAAO,MAAM,qBAAqB,IAAmB,CAAC;AACtD,eAAO,MAAM,4BAA4B,IACE,CAAC;AAC5C,eAAO,MAAM,wBAAwB,IACK,CAAC;AAC3C,eAAO,MAAM,sCAAsC,IACW,CAAC"}
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.PARTIAL_LIQUIDATION_RATIO = exports.FULL_LIQUIDATION_RATIO = exports.MAX_LEVERAGE = exports.BN_MAX = exports.TEN_THOUSAND = exports.ONE = exports.ZERO = void 0;
6
+ exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.PARTIAL_LIQUIDATION_RATIO = exports.FULL_LIQUIDATION_RATIO = exports.MAX_LEVERAGE = exports.BN_MAX = exports.TEN_THOUSAND = exports.ONE = exports.ZERO = void 0;
7
7
  const bn_js_1 = __importDefault(require("bn.js"));
8
8
  exports.ZERO = new bn_js_1.default(0);
9
9
  exports.ONE = new bn_js_1.default(1);
@@ -19,3 +19,4 @@ exports.PEG_PRECISION = new bn_js_1.default(1000);
19
19
  exports.AMM_RESERVE_PRECISION = new bn_js_1.default(Math.pow(10, 13));
20
20
  exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.div(exports.QUOTE_PRECISION); // 10^7
21
21
  exports.PRICE_TO_QUOTE_PRECISION = exports.MARK_PRICE_PRECISION.div(exports.QUOTE_PRECISION);
22
+ exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.mul(exports.PEG_PRECISION).div(exports.QUOTE_PRECISION); // 10^10
@@ -1 +1 @@
1
- {"version":3,"file":"makeTradeExample.d.ts","sourceRoot":"","sources":["../../src/examples/makeTradeExample.ts"],"names":[],"mappings":"AAEA,OAAO,EAAuB,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAcjE,eAAO,MAAM,eAAe,gBACd,MAAM,cACP,MAAM,KAChB,QAAQ,SAAS,CAOnB,CAAC"}
1
+ {"version":3,"file":"makeTradeExample.d.ts","sourceRoot":"","sources":["../../src/examples/makeTradeExample.ts"],"names":[],"mappings":"AAGA,OAAO,EAAuB,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAcjE,eAAO,MAAM,eAAe,gBACd,MAAM,cACP,MAAM,KAChB,QAAQ,SAAS,CAOnB,CAAC"}
@@ -11,20 +11,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.getTokenAddress = void 0;
13
13
  const anchor_1 = require("@project-serum/anchor");
14
+ const __1 = require("..");
14
15
  const spl_token_1 = require("@solana/spl-token");
15
16
  const web3_js_1 = require("@solana/web3.js");
16
- const __1 = require("..");
17
+ const __2 = require("..");
17
18
  const getTokenAddress = (mintAddress, userPubKey) => {
18
19
  return spl_token_1.Token.getAssociatedTokenAddress(new web3_js_1.PublicKey(`ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`), spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
19
20
  };
20
21
  exports.getTokenAddress = getTokenAddress;
21
22
  const main = () => __awaiter(void 0, void 0, void 0, function* () {
22
23
  // Initialize Drift SDK
23
- const sdkConfig = __1.initialize({ env: 'devnet' });
24
+ const sdkConfig = __2.initialize({ env: 'devnet' });
24
25
  // Set up the Wallet and Provider
25
26
  const privateKey = process.env.BOT_PRIVATE_KEY; // stored as an array string
26
27
  const keypair = web3_js_1.Keypair.fromSecretKey(Uint8Array.from(JSON.parse(privateKey)));
27
- const wallet = new anchor_1.Wallet(keypair);
28
+ const wallet = new __1.Wallet(keypair);
28
29
  // Set up the Connection
29
30
  const rpcAddress = process.env.RPC_ADDRESS; // can use: https://api.devnet.solana.com for devnet; https://api.mainnet-beta.solana.com for mainnet;
30
31
  const connection = new web3_js_1.Connection(rpcAddress);
@@ -37,35 +38,35 @@ const main = () => __awaiter(void 0, void 0, void 0, function* () {
37
38
  const usdcTokenAddress = yield exports.getTokenAddress(sdkConfig.USDC_MINT_ADDRESS, wallet.publicKey.toString());
38
39
  // Set up the Drift Clearing House
39
40
  const clearingHousePublicKey = new web3_js_1.PublicKey(sdkConfig.CLEARING_HOUSE_PROGRAM_ID);
40
- const clearingHouse = __1.ClearingHouse.from(connection, provider.wallet, clearingHousePublicKey);
41
+ const clearingHouse = __2.ClearingHouse.from(connection, provider.wallet, clearingHousePublicKey);
41
42
  yield clearingHouse.subscribe();
42
43
  // Set up Clearing House user client
43
- const user = __1.ClearingHouseUser.from(clearingHouse, wallet.publicKey);
44
+ const user = __2.ClearingHouseUser.from(clearingHouse, wallet.publicKey);
44
45
  //// Check if clearing house account exists for the current wallet
45
46
  const userAccountExists = yield user.exists();
46
47
  if (!userAccountExists) {
47
48
  //// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
48
- const depositAmount = new anchor_1.BN(10000).mul(__1.QUOTE_PRECISION);
49
+ const depositAmount = new anchor_1.BN(10000).mul(__2.QUOTE_PRECISION);
49
50
  yield clearingHouse.initializeUserAccountAndDepositCollateral(depositAmount, yield exports.getTokenAddress(usdcTokenAddress.toString(), wallet.publicKey.toString()));
50
51
  }
51
52
  yield user.subscribe();
52
53
  // Get current price
53
- const solMarketInfo = __1.Markets.find((market) => market.baseAssetSymbol === 'SOL');
54
- const currentMarketPrice = __1.calculateMarkPrice(clearingHouse.getMarket(solMarketInfo.marketIndex));
55
- const formattedPrice = __1.convertToNumber(currentMarketPrice, __1.QUOTE_PRECISION);
54
+ const solMarketInfo = __2.Markets.find((market) => market.baseAssetSymbol === 'SOL');
55
+ const currentMarketPrice = __2.calculateMarkPrice(clearingHouse.getMarket(solMarketInfo.marketIndex));
56
+ const formattedPrice = __2.convertToNumber(currentMarketPrice, __2.QUOTE_PRECISION);
56
57
  console.log(`Current Market Price is $${formattedPrice}`);
57
58
  // Estimate the slippage for a $5000 LONG trade
58
59
  const solMarketAccount = clearingHouse.getMarket(solMarketInfo.marketIndex);
59
- const slippage = __1.convertToNumber(__1.calculateTradeSlippage(__1.PositionDirection.LONG, new anchor_1.BN(5000).mul(__1.QUOTE_PRECISION), solMarketAccount)[0], __1.MARK_PRICE_PRECISION);
60
+ const slippage = __2.convertToNumber(__2.calculateTradeSlippage(__2.PositionDirection.LONG, new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION), solMarketAccount)[0], __2.MARK_PRICE_PRECISION);
60
61
  console.log(`Slippage for a $5000 LONG on the SOL market would be $${slippage}`);
61
62
  // Make a $5000 LONG trade
62
- yield clearingHouse.openPosition(__1.PositionDirection.LONG, new anchor_1.BN(5000).mul(__1.QUOTE_PRECISION), solMarketInfo.marketIndex);
63
+ yield clearingHouse.openPosition(__2.PositionDirection.LONG, new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION), solMarketInfo.marketIndex);
63
64
  console.log(`LONGED $5000 SOL`);
64
65
  // Make a $5000 LONG trade
65
- yield clearingHouse.openPosition(__1.PositionDirection.LONG, new anchor_1.BN(5000).mul(__1.QUOTE_PRECISION), solMarketInfo.marketIndex);
66
+ yield clearingHouse.openPosition(__2.PositionDirection.LONG, new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION), solMarketInfo.marketIndex);
66
67
  console.log(`LONGED $5000 worth of SOL`);
67
68
  // Reduce the position by $2000
68
- yield clearingHouse.openPosition(__1.PositionDirection.SHORT, new anchor_1.BN(2000).mul(__1.QUOTE_PRECISION), solMarketInfo.marketIndex);
69
+ yield clearingHouse.openPosition(__2.PositionDirection.SHORT, new anchor_1.BN(2000).mul(__2.QUOTE_PRECISION), solMarketInfo.marketIndex);
69
70
  // Close the rest of the position
70
71
  yield clearingHouse.closePosition(solMarketInfo.marketIndex);
71
72
  });