@drift-labs/sdk 0.2.0-master.30 → 0.2.0-master.32

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 (60) hide show
  1. package/lib/admin.d.ts +11 -7
  2. package/lib/admin.js +50 -12
  3. package/lib/clearingHouse.d.ts +26 -23
  4. package/lib/clearingHouse.js +290 -652
  5. package/lib/clearingHouseUser.d.ts +1 -1
  6. package/lib/clearingHouseUser.js +9 -12
  7. package/lib/config.js +1 -1
  8. package/lib/constants/numericConstants.d.ts +7 -0
  9. package/lib/constants/numericConstants.js +8 -1
  10. package/lib/constants/spotMarkets.js +4 -4
  11. package/lib/dlob/DLOB.d.ts +7 -5
  12. package/lib/dlob/DLOB.js +159 -88
  13. package/lib/dlob/DLOBNode.d.ts +2 -0
  14. package/lib/dlob/DLOBNode.js +3 -0
  15. package/lib/dlob/NodeList.d.ts +2 -1
  16. package/lib/dlob/NodeList.js +10 -4
  17. package/lib/events/types.d.ts +2 -1
  18. package/lib/events/types.js +1 -0
  19. package/lib/factory/bigNum.d.ts +1 -0
  20. package/lib/factory/bigNum.js +14 -0
  21. package/lib/idl/clearing_house.json +1091 -611
  22. package/lib/math/amm.d.ts +2 -2
  23. package/lib/math/amm.js +31 -28
  24. package/lib/math/market.d.ts +1 -1
  25. package/lib/math/market.js +6 -6
  26. package/lib/math/spotBalance.js +7 -8
  27. package/lib/math/trade.js +11 -11
  28. package/lib/types.d.ts +118 -41
  29. package/lib/types.js +34 -4
  30. package/package.json +4 -2
  31. package/src/admin.ts +129 -29
  32. package/src/clearingHouse.ts +380 -787
  33. package/src/clearingHouseUser.ts +11 -14
  34. package/src/config.ts +1 -1
  35. package/src/constants/numericConstants.ts +7 -0
  36. package/src/constants/spotMarkets.ts +5 -4
  37. package/src/dlob/DLOB.ts +221 -103
  38. package/src/dlob/DLOBNode.ts +5 -0
  39. package/src/dlob/NodeList.ts +15 -5
  40. package/src/events/types.ts +3 -0
  41. package/src/factory/bigNum.ts +23 -0
  42. package/src/idl/clearing_house.json +1091 -611
  43. package/src/math/amm.ts +42 -29
  44. package/src/math/market.ts +9 -4
  45. package/src/math/spotBalance.ts +11 -8
  46. package/src/math/trade.ts +11 -11
  47. package/src/types.ts +82 -41
  48. package/tests/bn/test.ts +13 -8
  49. package/tests/dlob/helpers.ts +56 -33
  50. package/tests/dlob/test.ts +1397 -495
  51. package/src/assert/assert.js +0 -9
  52. package/src/events/eventList.js +0 -77
  53. package/src/examples/makeTradeExample.js +0 -157
  54. package/src/token/index.js +0 -38
  55. package/src/tx/types.js +0 -2
  56. package/src/tx/utils.js +0 -17
  57. package/src/util/computeUnits.js +0 -27
  58. package/src/util/getTokenAddress.js +0 -9
  59. package/src/util/promiseTimeout.js +0 -14
  60. package/src/util/tps.js +0 -27
package/src/math/amm.ts CHANGED
@@ -47,7 +47,7 @@ export function calculateOptimalPegAndBudget(
47
47
  amm: AMM,
48
48
  oraclePriceData: OraclePriceData
49
49
  ): [BN, BN, BN, boolean] {
50
- const markPriceBefore = calculatePrice(
50
+ const reservePriceBefore = calculatePrice(
51
51
  amm.baseAssetReserve,
52
52
  amm.quoteAssetReserve,
53
53
  amm.pegMultiplier
@@ -70,15 +70,15 @@ export function calculateOptimalPegAndBudget(
70
70
  let newTargetPrice: BN;
71
71
  let newOptimalPeg: BN;
72
72
  let newBudget: BN;
73
- const targetPriceGap = markPriceBefore.sub(targetPrice);
73
+ const targetPriceGap = reservePriceBefore.sub(targetPrice);
74
74
 
75
75
  if (targetPriceGap.abs().gt(maxPriceSpread)) {
76
76
  const markAdj = targetPriceGap.abs().sub(maxPriceSpread);
77
77
 
78
78
  if (targetPriceGap.lt(new BN(0))) {
79
- newTargetPrice = markPriceBefore.add(markAdj);
79
+ newTargetPrice = reservePriceBefore.add(markAdj);
80
80
  } else {
81
- newTargetPrice = markPriceBefore.sub(markAdj);
81
+ newTargetPrice = reservePriceBefore.sub(markAdj);
82
82
  }
83
83
 
84
84
  newOptimalPeg = calculatePegFromTargetPrice(
@@ -336,6 +336,7 @@ export function calculateInventoryScale(
336
336
  minBaseAssetReserve: BN,
337
337
  maxBaseAssetReserve: BN
338
338
  ): number {
339
+ const maxScale = BID_ASK_SPREAD_PRECISION.mul(new BN(10));
339
340
  // inventory skew
340
341
  const [openBids, openAsks] = calculateMarketOpenBidAsk(
341
342
  baseAssetReserve,
@@ -348,10 +349,10 @@ export function calculateInventoryScale(
348
349
  BN.min(openBids.abs(), openAsks.abs())
349
350
  );
350
351
  const inventoryScale =
351
- BN.min(netBaseAssetAmount.abs(), minSideLiquidity)
352
- .mul(BID_ASK_SPREAD_PRECISION.mul(new BN(10)))
353
- .div(minSideLiquidity)
354
- .toNumber() / BID_ASK_SPREAD_PRECISION.toNumber();
352
+ BN.min(
353
+ maxScale,
354
+ netBaseAssetAmount.mul(maxScale).div(minSideLiquidity).abs()
355
+ ).toNumber() / BID_ASK_SPREAD_PRECISION.toNumber();
355
356
 
356
357
  return inventoryScale;
357
358
  }
@@ -362,7 +363,7 @@ export function calculateEffectiveLeverage(
362
363
  terminalQuoteAssetReserve: BN,
363
364
  pegMultiplier: BN,
364
365
  netBaseAssetAmount: BN,
365
- markPrice: BN,
366
+ reservePrice: BN,
366
367
  totalFeeMinusDistributions: BN
367
368
  ): number {
368
369
  // inventory skew
@@ -372,7 +373,7 @@ export function calculateEffectiveLeverage(
372
373
  .div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
373
374
 
374
375
  const localBaseAssetValue = netBaseAssetAmount
375
- .mul(markPrice)
376
+ .mul(reservePrice)
376
377
  .div(AMM_TO_QUOTE_PRECISION_RATIO.mul(PRICE_PRECISION));
377
378
 
378
379
  const effectiveLeverage =
@@ -393,14 +394,14 @@ export function calculateMaxSpread(marginRatioInitial: number): number {
393
394
 
394
395
  export function calculateSpreadBN(
395
396
  baseSpread: number,
396
- lastOracleMarkSpreadPct: BN,
397
+ lastOracleReservePriceSpreadPct: BN,
397
398
  lastOracleConfPct: BN,
398
399
  maxSpread: number,
399
400
  quoteAssetReserve: BN,
400
401
  terminalQuoteAssetReserve: BN,
401
402
  pegMultiplier: BN,
402
403
  netBaseAssetAmount: BN,
403
- markPrice: BN,
404
+ reservePrice: BN,
404
405
  totalFeeMinusDistributions: BN,
405
406
  baseAssetReserve: BN,
406
407
  minBaseAssetReserve: BN,
@@ -409,24 +410,26 @@ export function calculateSpreadBN(
409
410
  let longSpread = baseSpread / 2;
410
411
  let shortSpread = baseSpread / 2;
411
412
 
412
- if (lastOracleMarkSpreadPct.gt(ZERO)) {
413
+ if (lastOracleReservePriceSpreadPct.gt(ZERO)) {
413
414
  shortSpread = Math.max(
414
415
  shortSpread,
415
- lastOracleMarkSpreadPct.abs().toNumber() + lastOracleConfPct.toNumber()
416
+ lastOracleReservePriceSpreadPct.abs().toNumber() +
417
+ lastOracleConfPct.toNumber()
416
418
  );
417
- } else if (lastOracleMarkSpreadPct.lt(ZERO)) {
419
+ } else if (lastOracleReservePriceSpreadPct.lt(ZERO)) {
418
420
  longSpread = Math.max(
419
421
  longSpread,
420
- lastOracleMarkSpreadPct.abs().toNumber() + lastOracleConfPct.toNumber()
422
+ lastOracleReservePriceSpreadPct.abs().toNumber() +
423
+ lastOracleConfPct.toNumber()
421
424
  );
422
425
  }
423
426
 
424
427
  const maxTargetSpread: number = Math.max(
425
428
  maxSpread,
426
- lastOracleMarkSpreadPct.abs().toNumber()
429
+ lastOracleReservePriceSpreadPct.abs().toNumber()
427
430
  );
428
431
 
429
- const MAX_INVENTORY_SKEW = 5;
432
+ const MAX_BID_ASK_INVENTORY_SKEW_FACTOR = 10;
430
433
 
431
434
  const inventoryScale = calculateInventoryScale(
432
435
  netBaseAssetAmount,
@@ -434,7 +437,10 @@ export function calculateSpreadBN(
434
437
  minBaseAssetReserve,
435
438
  maxBaseAssetReserve
436
439
  );
437
- const inventorySpreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + inventoryScale);
440
+ const inventorySpreadScale = Math.min(
441
+ MAX_BID_ASK_INVENTORY_SKEW_FACTOR,
442
+ 1 + inventoryScale
443
+ );
438
444
 
439
445
  if (netBaseAssetAmount.gt(ZERO)) {
440
446
  longSpread *= inventorySpreadScale;
@@ -448,20 +454,23 @@ export function calculateSpreadBN(
448
454
  terminalQuoteAssetReserve,
449
455
  pegMultiplier,
450
456
  netBaseAssetAmount,
451
- markPrice,
457
+ reservePrice,
452
458
  totalFeeMinusDistributions
453
459
  );
454
460
 
455
461
  if (totalFeeMinusDistributions.gt(ZERO)) {
456
- const spreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + effectiveLeverage);
462
+ const spreadScale = Math.min(
463
+ MAX_BID_ASK_INVENTORY_SKEW_FACTOR,
464
+ 1 + effectiveLeverage
465
+ );
457
466
  if (netBaseAssetAmount.gt(ZERO)) {
458
467
  longSpread *= spreadScale;
459
468
  } else {
460
469
  shortSpread *= spreadScale;
461
470
  }
462
471
  } else {
463
- longSpread *= MAX_INVENTORY_SKEW;
464
- shortSpread *= MAX_INVENTORY_SKEW;
472
+ longSpread *= MAX_BID_ASK_INVENTORY_SKEW_FACTOR;
473
+ shortSpread *= MAX_BID_ASK_INVENTORY_SKEW_FACTOR;
465
474
  }
466
475
 
467
476
  const totalSpread = longSpread + shortSpread;
@@ -487,23 +496,23 @@ export function calculateSpread(
487
496
  return amm.baseSpread / 2;
488
497
  }
489
498
 
490
- const markPrice = calculatePrice(
499
+ const reservePrice = calculatePrice(
491
500
  amm.baseAssetReserve,
492
501
  amm.quoteAssetReserve,
493
502
  amm.pegMultiplier
494
503
  );
495
504
 
496
- const targetPrice = oraclePriceData?.price || markPrice;
505
+ const targetPrice = oraclePriceData?.price || reservePrice;
497
506
  const confInterval = oraclePriceData.confidence || ZERO;
498
507
 
499
- const targetMarkSpreadPct = markPrice
508
+ const targetMarkSpreadPct = reservePrice
500
509
  .sub(targetPrice)
501
510
  .mul(BID_ASK_SPREAD_PRECISION)
502
- .div(markPrice);
511
+ .div(reservePrice);
503
512
 
504
513
  const confIntervalPct = confInterval
505
514
  .mul(BID_ASK_SPREAD_PRECISION)
506
- .div(markPrice);
515
+ .div(reservePrice);
507
516
 
508
517
  const [longSpread, shortSpread] = calculateSpreadBN(
509
518
  amm.baseSpread,
@@ -514,7 +523,7 @@ export function calculateSpread(
514
523
  amm.terminalQuoteAssetReserve,
515
524
  amm.pegMultiplier,
516
525
  amm.netBaseAssetAmount,
517
- markPrice,
526
+ reservePrice,
518
527
  amm.totalFeeMinusDistributions,
519
528
  amm.baseAssetReserve,
520
529
  amm.minBaseAssetReserve,
@@ -684,6 +693,10 @@ export function calculateQuoteAssetAmountSwapped(
684
693
  pegMultiplier: BN,
685
694
  swapDirection: SwapDirection
686
695
  ): BN {
696
+ if (isVariant(swapDirection, 'remove')) {
697
+ quoteAssetReserves = quoteAssetReserves.add(ONE);
698
+ }
699
+
687
700
  let quoteAssetAmount = quoteAssetReserves
688
701
  .mul(pegMultiplier)
689
702
  .div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
@@ -106,12 +106,12 @@ export function calculateNewMarketAfterTrade(
106
106
  return newMarket;
107
107
  }
108
108
 
109
- export function calculateMarkOracleSpread(
109
+ export function calculateOracleReserveSpread(
110
110
  market: PerpMarketAccount,
111
111
  oraclePriceData: OraclePriceData
112
112
  ): BN {
113
- const markPrice = calculateReservePrice(market, oraclePriceData);
114
- return calculateOracleSpread(markPrice, oraclePriceData);
113
+ const reservePrice = calculateReservePrice(market, oraclePriceData);
114
+ return calculateOracleSpread(reservePrice, oraclePriceData);
115
115
  }
116
116
 
117
117
  export function calculateOracleSpread(
@@ -137,7 +137,12 @@ export function calculateMarketMarginRatio(
137
137
  ).toNumber();
138
138
  break;
139
139
  case 'Maintenance':
140
- marginRatio = market.marginRatioMaintenance;
140
+ marginRatio = calculateSizePremiumLiabilityWeight(
141
+ size,
142
+ market.imfFactor,
143
+ new BN(market.marginRatioMaintenance),
144
+ MARGIN_PRECISION
145
+ ).toNumber();
141
146
  break;
142
147
  }
143
148
 
@@ -196,22 +196,25 @@ export function calculateInterestRate(bank: SpotMarketAccount): BN {
196
196
  const utilization = calculateUtilization(bank);
197
197
 
198
198
  let interestRate: BN;
199
- if (utilization.gt(bank.optimalUtilization)) {
200
- const surplusUtilization = utilization.sub(bank.optimalUtilization);
201
- const borrowRateSlope = bank.maxBorrowRate
202
- .sub(bank.optimalBorrowRate)
199
+ if (utilization.gt(new BN(bank.optimalUtilization))) {
200
+ const surplusUtilization = utilization.sub(new BN(bank.optimalUtilization));
201
+ const borrowRateSlope = new BN(bank.maxBorrowRate - bank.optimalBorrowRate)
203
202
  .mul(SPOT_MARKET_UTILIZATION_PRECISION)
204
- .div(SPOT_MARKET_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
203
+ .div(
204
+ SPOT_MARKET_UTILIZATION_PRECISION.sub(new BN(bank.optimalUtilization))
205
+ );
205
206
 
206
- interestRate = bank.optimalBorrowRate.add(
207
+ interestRate = new BN(bank.optimalBorrowRate).add(
207
208
  surplusUtilization
208
209
  .mul(borrowRateSlope)
209
210
  .div(SPOT_MARKET_UTILIZATION_PRECISION)
210
211
  );
211
212
  } else {
212
- const borrowRateSlope = bank.optimalBorrowRate
213
+ const borrowRateSlope = new BN(bank.optimalBorrowRate)
213
214
  .mul(SPOT_MARKET_UTILIZATION_PRECISION)
214
- .div(SPOT_MARKET_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
215
+ .div(
216
+ SPOT_MARKET_UTILIZATION_PRECISION.sub(new BN(bank.optimalUtilization))
217
+ );
215
218
 
216
219
  interestRate = utilization
217
220
  .mul(borrowRateSlope)
package/src/math/trade.ts CHANGED
@@ -215,20 +215,20 @@ export function calculateTargetPriceTrade(
215
215
  assert(targetPrice.gt(ZERO));
216
216
  assert(pct.lte(MAXPCT) && pct.gt(ZERO));
217
217
 
218
- const markPriceBefore = calculateReservePrice(market, oraclePriceData);
218
+ const reservePriceBefore = calculateReservePrice(market, oraclePriceData);
219
219
  const bidPriceBefore = calculateBidPrice(market, oraclePriceData);
220
220
  const askPriceBefore = calculateAskPrice(market, oraclePriceData);
221
221
 
222
222
  let direction;
223
- if (targetPrice.gt(markPriceBefore)) {
224
- const priceGap = targetPrice.sub(markPriceBefore);
223
+ if (targetPrice.gt(reservePriceBefore)) {
224
+ const priceGap = targetPrice.sub(reservePriceBefore);
225
225
  const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
226
- targetPrice = markPriceBefore.add(priceGapScaled);
226
+ targetPrice = reservePriceBefore.add(priceGapScaled);
227
227
  direction = PositionDirection.LONG;
228
228
  } else {
229
- const priceGap = markPriceBefore.sub(targetPrice);
229
+ const priceGap = reservePriceBefore.sub(targetPrice);
230
230
  const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
231
- targetPrice = markPriceBefore.sub(priceGapScaled);
231
+ targetPrice = reservePriceBefore.sub(priceGapScaled);
232
232
  direction = PositionDirection.SHORT;
233
233
  }
234
234
 
@@ -265,14 +265,14 @@ export function calculateTargetPriceTrade(
265
265
  targetPrice.gt(bidPriceBefore)
266
266
  ) {
267
267
  // no trade, market is at target
268
- if (markPriceBefore.gt(targetPrice)) {
268
+ if (reservePriceBefore.gt(targetPrice)) {
269
269
  direction = PositionDirection.SHORT;
270
270
  } else {
271
271
  direction = PositionDirection.LONG;
272
272
  }
273
273
  tradeSize = ZERO;
274
274
  return [direction, tradeSize, targetPrice, targetPrice];
275
- } else if (markPriceBefore.gt(targetPrice)) {
275
+ } else if (reservePriceBefore.gt(targetPrice)) {
276
276
  // overestimate y2
277
277
  baseAssetReserveAfter = squareRootBN(
278
278
  k.div(targetPrice).mul(peg).div(PEG_PRECISION).sub(biasModifier)
@@ -291,7 +291,7 @@ export function calculateTargetPriceTrade(
291
291
  .div(PEG_PRECISION)
292
292
  .div(AMM_TO_QUOTE_PRECISION_RATIO);
293
293
  baseSize = baseAssetReserveAfter.sub(baseAssetReserveBefore);
294
- } else if (markPriceBefore.lt(targetPrice)) {
294
+ } else if (reservePriceBefore.lt(targetPrice)) {
295
295
  // underestimate y2
296
296
  baseAssetReserveAfter = squareRootBN(
297
297
  k.div(targetPrice).mul(peg).div(PEG_PRECISION).add(biasModifier)
@@ -320,12 +320,12 @@ export function calculateTargetPriceTrade(
320
320
 
321
321
  let tp1 = targetPrice;
322
322
  let tp2 = markPriceAfter;
323
- let originalDiff = targetPrice.sub(markPriceBefore);
323
+ let originalDiff = targetPrice.sub(reservePriceBefore);
324
324
 
325
325
  if (direction == PositionDirection.SHORT) {
326
326
  tp1 = markPriceAfter;
327
327
  tp2 = targetPrice;
328
- originalDiff = markPriceBefore.sub(targetPrice);
328
+ originalDiff = reservePriceBefore.sub(targetPrice);
329
329
  }
330
330
 
331
331
  const entryPrice = tradeSize
package/src/types.ts CHANGED
@@ -3,9 +3,24 @@ import { BN, ZERO } from '.';
3
3
 
4
4
  // # Utility Types / Enums / Constants
5
5
 
6
+ export class ExchangeStatus {
7
+ static readonly ACTIVE = { active: {} };
8
+ static readonly FUNDINGPAUSED = { fundingPaused: {} };
9
+ static readonly AMMPAUSED = { ammPaused: {} };
10
+ static readonly FILLPAUSED = { fillPaused: {} };
11
+ static readonly LIQPAUSED = { liqPaused: {} };
12
+ static readonly WITHDRAWPAUSED = { withdrawPaused: {} };
13
+ static readonly PAUSED = { paused: {} };
14
+ }
15
+
6
16
  export class MarketStatus {
7
17
  static readonly INITIALIZED = { initialized: {} };
8
- static readonly REDUCEONLY = { reduceonly: {} };
18
+ static readonly ACTIVE = { active: {} };
19
+ static readonly FUNDINGPAUSED = { fundingPaused: {} };
20
+ static readonly AMMPAUSED = { ammPaused: {} };
21
+ static readonly FILLPAUSED = { fillPaused: {} };
22
+ static readonly WITHDRAWPAUSED = { withdrawPaused: {} };
23
+ static readonly REDUCEONLY = { reduceOnly: {} };
9
24
  static readonly SETTLEMENT = { settlement: {} };
10
25
  static readonly DELISTED = { delisted: {} };
11
26
  }
@@ -15,6 +30,21 @@ export class ContractType {
15
30
  static readonly FUTURE = { future: {} };
16
31
  }
17
32
 
33
+ export class ContractTier {
34
+ static readonly A = { a: {} };
35
+ static readonly B = { b: {} };
36
+ static readonly C = { c: {} };
37
+ static readonly Speculative = { speculative: {} };
38
+ }
39
+
40
+ export class AssetTier {
41
+ static readonly COLLATERAL = { collateral: {} };
42
+ static readonly PROTECTED = { protected: {} };
43
+ static readonly CROSS = { cross: {} };
44
+ static readonly ISOLATED = { isolated: {} };
45
+ static readonly UNLISTED = { unlisted: {} };
46
+ }
47
+
18
48
  export class SwapDirection {
19
49
  static readonly ADD = { add: {} };
20
50
  static readonly REMOVE = { remove: {} };
@@ -155,9 +185,23 @@ export type DepositRecord = {
155
185
  marketIndex: number;
156
186
  amount: BN;
157
187
  oraclePrice: BN;
158
- referrer: PublicKey;
159
- from?: PublicKey;
160
- to?: PublicKey;
188
+ marketDepositBalance: BN;
189
+ marketWithdrawBalance: BN;
190
+ marketCumulativeDepositInterest: BN;
191
+ marketCumulativeBorrowInterest: BN;
192
+ transferUser?: PublicKey;
193
+ };
194
+
195
+ export type SpotInterestRecord = {
196
+ ts: BN;
197
+ marketIndex: number;
198
+ depositBalance: BN;
199
+ cumulativeDepositInterest: BN;
200
+ borrowBalance: BN;
201
+ cumulativeBorrowInterest: BN;
202
+ optimalUtilization: number;
203
+ optimalBorrowRate: number;
204
+ maxBorrowRate: number;
161
205
  };
162
206
 
163
207
  export type CurveRecord = {
@@ -184,8 +228,8 @@ export declare type InsuranceFundRecord = {
184
228
  ts: BN;
185
229
  bankIndex: BN;
186
230
  marketIndex: number;
187
- userIfFactor: BN;
188
- totalIfFactor: BN;
231
+ userIfFactor: number;
232
+ totalIfFactor: number;
189
233
  vaultAmountBefore: BN;
190
234
  insuranceVaultAmountBefore: BN;
191
235
  amount: BN;
@@ -234,7 +278,6 @@ export type FundingPaymentRecord = {
234
278
  fundingPayment: BN;
235
279
  baseAssetAmount: BN;
236
280
  userLastCumulativeFunding: BN;
237
- userLastFundingRateTs: BN;
238
281
  ammCumulativeFundingLong: BN;
239
282
  ammCumulativeFundingShort: BN;
240
283
  };
@@ -249,11 +292,11 @@ export type LiquidationRecord = {
249
292
  liquidationId: number;
250
293
  canceledOrderIds: BN[];
251
294
  liquidatePerp: LiquidatePerpRecord;
252
- liquidateBorrow: LiquidateBorrowRecord;
295
+ liquidateSpot: LiquidateSpotRecord;
253
296
  liquidateBorrowForPerpPnl: LiquidateBorrowForPerpPnlRecord;
254
297
  liquidatePerpPnlForDeposit: LiquidatePerpPnlForDepositRecord;
255
298
  perpBankruptcy: PerpBankruptcyRecord;
256
- borrowBankruptcy: BorrowBankruptcyRecord;
299
+ spotBankruptcy: SpotBankruptcyRecord;
257
300
  };
258
301
 
259
302
  export class LiquidationType {
@@ -279,15 +322,13 @@ export type LiquidatePerpRecord = {
279
322
  baseAssetAmount: BN;
280
323
  quoteAssetAmount: BN;
281
324
  lpShares: BN;
282
- userPnl: BN;
283
- liquidatorPnl: BN;
284
325
  userOrderId: BN;
285
326
  liquidatorOrderId: BN;
286
327
  fillRecordId: BN;
287
328
  ifFee: BN;
288
329
  };
289
330
 
290
- export type LiquidateBorrowRecord = {
331
+ export type LiquidateSpotRecord = {
291
332
  assetMarketIndex: number;
292
333
  assetPrice: BN;
293
334
  assetTransfer: BN;
@@ -321,7 +362,7 @@ export type PerpBankruptcyRecord = {
321
362
  cumulativeFundingRateDelta: BN;
322
363
  };
323
364
 
324
- export type BorrowBankruptcyRecord = {
365
+ export type SpotBankruptcyRecord = {
325
366
  marketIndex: number;
326
367
  borrowAmount: BN;
327
368
  cumulativeDepositInterestDelta: BN;
@@ -360,14 +401,14 @@ export type OrderActionRecord = {
360
401
  referrerReward: number | null;
361
402
  quoteAssetAmountSurplus: BN | null;
362
403
  taker: PublicKey | null;
363
- takerOrderId: BN | null;
404
+ takerOrderId: number | null;
364
405
  takerOrderDirection: PositionDirection | null;
365
406
  takerOrderBaseAssetAmount: BN | null;
366
407
  takerOrderCumulativeBaseAssetAmountFilled: BN | null;
367
408
  takerOrderCumulativeQuoteAssetAmountFilled: BN | null;
368
409
  takerOrderFee: BN | null;
369
410
  maker: PublicKey | null;
370
- makerOrderId: BN | null;
411
+ makerOrderId: number | null;
371
412
  makerOrderDirection: PositionDirection | null;
372
413
  makerOrderBaseAssetAmount: BN | null;
373
414
  makerOrderCumulativeBaseAssetAmountFilled: BN | null;
@@ -378,24 +419,20 @@ export type OrderActionRecord = {
378
419
 
379
420
  export type StateAccount = {
380
421
  admin: PublicKey;
381
- fundingPaused: boolean;
382
- exchangePaused: boolean;
383
- adminControlsPrices: boolean;
384
- totalFee: BN;
385
- totalFeeWithdrawn: BN;
422
+ exchangeStatus: ExchangeStatus;
386
423
  whitelistMint: PublicKey;
387
424
  discountMint: PublicKey;
388
425
  oracleGuardRails: OracleGuardRails;
389
- maxDeposit: BN;
390
426
  numberOfMarkets: number;
391
427
  numberOfSpotMarkets: number;
392
428
  minOrderQuoteAssetAmount: BN;
393
- signer: PublicKey;
394
- signerNonce: number;
395
- defaultMarketOrderTimeInForce: number;
396
429
  minPerpAuctionDuration: number;
430
+ defaultMarketOrderTimeInForce: number;
397
431
  defaultSpotAuctionDuration: number;
398
432
  liquidationMarginBufferRatio: number;
433
+ settlementDuration: number;
434
+ signer: PublicKey;
435
+ signerNonce: number;
399
436
  srmVault: PublicKey;
400
437
  perpFeeStructure: FeeStructure;
401
438
  spotFeeStructure: FeeStructure;
@@ -449,6 +486,9 @@ export type HistoricalIndexData = {
449
486
  };
450
487
 
451
488
  export type SpotMarketAccount = {
489
+ status: MarketStatus;
490
+ assetTier: AssetTier;
491
+
452
492
  marketIndex: number;
453
493
  pubkey: PublicKey;
454
494
  mint: PublicKey;
@@ -466,18 +506,20 @@ export type SpotMarketAccount = {
466
506
  totalIfShares: BN;
467
507
  userIfShares: BN;
468
508
 
469
- userIfFactor: BN;
470
- totalIfFactor: BN;
509
+ userIfFactor: number;
510
+ totalIfFactor: number;
471
511
  ifLiquidationFee: BN;
472
512
 
473
513
  decimals: number;
474
- optimalUtilization: BN;
475
- optimalBorrowRate: BN;
476
- maxBorrowRate: BN;
514
+ optimalUtilization: number;
515
+ optimalBorrowRate: number;
516
+ maxBorrowRate: number;
477
517
  cumulativeDepositInterest: BN;
478
518
  cumulativeBorrowInterest: BN;
479
519
  depositBalance: BN;
480
520
  borrowBalance: BN;
521
+ maxTokenDeposits: BN;
522
+
481
523
  lastInterestTs: BN;
482
524
  lastTwapTs: BN;
483
525
  initialAssetWeight: BN;
@@ -494,14 +536,13 @@ export type SpotMarketAccount = {
494
536
 
495
537
  orderStepSize: BN;
496
538
  nextFillRecordId: BN;
497
- spotFeePool: {
498
- balance: BN;
499
- };
539
+ spotFeePool: PoolBalance;
500
540
  totalSpotFee: BN;
501
541
  };
502
542
 
503
543
  export type PoolBalance = {
504
544
  balance: BN;
545
+ marketIndex: number;
505
546
  };
506
547
 
507
548
  export type AMM = {
@@ -518,7 +559,7 @@ export type AMM = {
518
559
  oracleSource: OracleSource;
519
560
  historicalOracleData: HistoricalOracleData;
520
561
 
521
- lastOracleMarkSpreadPct: BN;
562
+ lastOracleReservePriceSpreadPct: BN;
522
563
  lastOracleConfPct: BN;
523
564
 
524
565
  fundingPeriod: BN;
@@ -567,7 +608,6 @@ export type AMM = {
567
608
  // # User Account Types
568
609
  export type PerpPosition = {
569
610
  baseAssetAmount: BN;
570
- remainderBaseAssetAmount: BN;
571
611
  lastCumulativeFundingRate: BN;
572
612
  marketIndex: number;
573
613
  quoteAssetAmount: BN;
@@ -577,7 +617,7 @@ export type PerpPosition = {
577
617
  openAsks: BN;
578
618
  settledPnl: BN;
579
619
  lpShares: BN;
580
- lastFeePerLp: BN;
620
+ remainderBaseAssetAmount: number;
581
621
  lastNetBaseAssetAmountPerLp: BN;
582
622
  lastNetQuoteAssetAmountPerLp: BN;
583
623
  };
@@ -614,7 +654,7 @@ export type UserAccount = {
614
654
  beingLiquidated: boolean;
615
655
  bankrupt: boolean;
616
656
  nextLiquidationId: number;
617
- nextOrderId: BN;
657
+ nextOrderId: number;
618
658
  customMarginRatio: number;
619
659
  };
620
660
 
@@ -634,7 +674,7 @@ export type Order = {
634
674
  marketType: MarketType;
635
675
  ts: BN;
636
676
  slot: BN;
637
- orderId: BN;
677
+ orderId: number;
638
678
  userOrderId: number;
639
679
  marketIndex: number;
640
680
  price: BN;
@@ -669,10 +709,10 @@ export type OrderParams = {
669
709
  reduceOnly: boolean;
670
710
  postOnly: boolean;
671
711
  immediateOrCancel: boolean;
672
- triggerPrice: BN;
712
+ triggerPrice: BN | null;
673
713
  triggerCondition: OrderTriggerCondition;
674
714
  positionLimit: BN;
675
- oraclePriceOffset: BN;
715
+ oraclePriceOffset: BN | null;
676
716
  auctionDuration: number | null;
677
717
  timeInForce: number | null;
678
718
  auctionStartPrice: BN | null;
@@ -700,10 +740,10 @@ export const DefaultOrderParams = {
700
740
  reduceOnly: false,
701
741
  postOnly: false,
702
742
  immediateOrCancel: false,
703
- triggerPrice: ZERO,
743
+ triggerPrice: null,
704
744
  triggerCondition: OrderTriggerCondition.ABOVE,
705
745
  positionLimit: ZERO,
706
- oraclePriceOffset: ZERO,
746
+ oraclePriceOffset: null,
707
747
  auctionDuration: null,
708
748
  timeInForce: null,
709
749
  auctionStartPrice: null,
@@ -712,6 +752,7 @@ export const DefaultOrderParams = {
712
752
  export type MakerInfo = {
713
753
  maker: PublicKey;
714
754
  makerStats: PublicKey;
755
+ makerUserAccount: UserAccount;
715
756
  order: Order;
716
757
  };
717
758