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

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 (106) hide show
  1. package/README.md +13 -13
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +10 -11
  3. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +4 -4
  4. package/lib/accounts/types.d.ts +4 -6
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +10 -11
  6. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +6 -8
  7. package/lib/addresses/marketAddresses.d.ts +1 -3
  8. package/lib/addresses/pda.d.ts +5 -5
  9. package/lib/addresses/pda.js +5 -5
  10. package/lib/admin.d.ts +27 -28
  11. package/lib/admin.js +12 -37
  12. package/lib/clearingHouse.d.ts +61 -62
  13. package/lib/clearingHouse.js +92 -110
  14. package/lib/clearingHouseConfig.d.ts +2 -4
  15. package/lib/clearingHouseUser.d.ts +16 -16
  16. package/lib/clearingHouseUser.js +16 -16
  17. package/lib/config.d.ts +2 -4
  18. package/lib/config.js +1 -1
  19. package/lib/constants/numericConstants.d.ts +7 -8
  20. package/lib/constants/numericConstants.js +17 -18
  21. package/lib/constants/perpMarkets.d.ts +2 -3
  22. package/lib/constants/perpMarkets.js +3 -3
  23. package/lib/constants/spotMarkets.d.ts +2 -1
  24. package/lib/constants/spotMarkets.js +6 -4
  25. package/lib/dlob/DLOB.d.ts +13 -13
  26. package/lib/dlob/DLOB.js +36 -40
  27. package/lib/dlob/DLOBNode.js +2 -2
  28. package/lib/events/sort.js +1 -1
  29. package/lib/examples/makeTradeExample.js +3 -3
  30. package/lib/idl/clearing_house.json +359 -310
  31. package/lib/math/amm.d.ts +2 -2
  32. package/lib/math/amm.js +10 -10
  33. package/lib/math/conversion.js +1 -1
  34. package/lib/math/funding.js +9 -9
  35. package/lib/math/margin.js +3 -3
  36. package/lib/math/market.d.ts +4 -4
  37. package/lib/math/market.js +7 -7
  38. package/lib/math/oracles.js +6 -8
  39. package/lib/math/position.d.ts +2 -2
  40. package/lib/math/position.js +9 -9
  41. package/lib/math/repeg.js +7 -6
  42. package/lib/math/spotBalance.js +5 -5
  43. package/lib/math/spotPosition.js +2 -2
  44. package/lib/math/trade.d.ts +6 -6
  45. package/lib/math/trade.js +15 -19
  46. package/lib/oracles/pythClient.js +1 -1
  47. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  48. package/lib/oracles/switchboardClient.js +1 -1
  49. package/lib/types.d.ts +63 -51
  50. package/lib/types.js +1 -1
  51. package/package.json +2 -1
  52. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +15 -15
  53. package/src/accounts/types.ts +4 -5
  54. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +17 -25
  55. package/src/addresses/marketAddresses.ts +1 -2
  56. package/src/addresses/pda.ts +10 -10
  57. package/src/admin.ts +42 -79
  58. package/src/clearingHouse.ts +164 -212
  59. package/src/clearingHouseConfig.ts +2 -3
  60. package/src/clearingHouseUser.ts +35 -35
  61. package/src/config.ts +3 -4
  62. package/src/constants/numericConstants.ts +19 -21
  63. package/src/constants/perpMarkets.ts +5 -5
  64. package/src/constants/spotMarkets.ts +8 -5
  65. package/src/dlob/DLOB.ts +54 -70
  66. package/src/dlob/DLOBNode.ts +5 -6
  67. package/src/events/sort.ts +1 -1
  68. package/src/examples/makeTradeExample.js +2 -2
  69. package/src/examples/makeTradeExample.ts +5 -8
  70. package/src/idl/clearing_house.json +359 -310
  71. package/src/math/amm.ts +14 -11
  72. package/src/math/conversion.ts +2 -2
  73. package/src/math/funding.ts +13 -11
  74. package/src/math/margin.ts +4 -5
  75. package/src/math/market.ts +5 -5
  76. package/src/math/oracles.ts +9 -9
  77. package/src/math/position.ts +11 -19
  78. package/src/math/repeg.ts +8 -7
  79. package/src/math/spotBalance.ts +6 -6
  80. package/src/math/spotPosition.ts +2 -2
  81. package/src/math/trade.ts +17 -21
  82. package/src/oracles/pythClient.ts +2 -2
  83. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  84. package/src/oracles/switchboardClient.ts +2 -2
  85. package/src/types.ts +69 -51
  86. package/tests/dlob/helpers.ts +56 -4
  87. package/src/addresses/marketAddresses.js +0 -26
  88. package/src/constants/banks.js +0 -42
  89. package/src/constants/markets.js +0 -42
  90. package/src/events/txEventCache.js +0 -71
  91. package/src/factory/bigNum.js +0 -390
  92. package/src/factory/oracleClient.js +0 -20
  93. package/src/math/auction.js +0 -42
  94. package/src/math/conversion.js +0 -11
  95. package/src/math/funding.js +0 -248
  96. package/src/math/repeg.js +0 -128
  97. package/src/math/trade.js +0 -253
  98. package/src/math/utils.js +0 -26
  99. package/src/math/utils.js.map +0 -1
  100. package/src/oracles/oracleClientCache.js +0 -19
  101. package/src/oracles/pythClient.js +0 -46
  102. package/src/oracles/quoteAssetOracleClient.js +0 -32
  103. package/src/oracles/switchboardClient.js +0 -69
  104. package/src/oracles/types.js +0 -2
  105. package/src/userName.js +0 -20
  106. package/src/wallet.js +0 -35
@@ -178,7 +178,6 @@ class ClearingHouse {
178
178
  }
179
179
  getPerpMarketAccount(marketIndex) {
180
180
  var _a;
181
- marketIndex = marketIndex instanceof anchor_1.BN ? marketIndex : new anchor_1.BN(marketIndex);
182
181
  return (_a = this.accountSubscriber.getMarketAccountAndSlot(marketIndex)) === null || _a === void 0 ? void 0 : _a.data;
183
182
  }
184
183
  getPerpMarketAccounts() {
@@ -187,7 +186,6 @@ class ClearingHouse {
187
186
  .map((value) => value.data);
188
187
  }
189
188
  getSpotMarketAccount(marketIndex) {
190
- marketIndex = marketIndex instanceof anchor_1.BN ? marketIndex : new anchor_1.BN(marketIndex);
191
189
  return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
192
190
  }
193
191
  getSpotMarketAccounts() {
@@ -219,6 +217,7 @@ class ClearingHouse {
219
217
  this.wallet = newWallet;
220
218
  this.provider = newProvider;
221
219
  this.program = newProgram;
220
+ this.authority = newWallet.publicKey;
222
221
  if (this.isSubscribed) {
223
222
  await Promise.all(this.unsubscribeUsers());
224
223
  }
@@ -361,8 +360,7 @@ class ClearingHouse {
361
360
  return this.getUser(userId).getUserAccountAndSlot();
362
361
  }
363
362
  getSpotPosition(marketIndex) {
364
- const marketIndexBN = marketIndex instanceof anchor_1.BN ? marketIndex : new anchor_1.BN(marketIndex);
365
- return this.getUserAccount().spotPositions.find((spotPosition) => spotPosition.marketIndex.eq(marketIndexBN));
363
+ return this.getUserAccount().spotPositions.find((spotPosition) => spotPosition.marketIndex === marketIndex);
366
364
  }
367
365
  getQuoteAssetTokenAmount() {
368
366
  const spotMarket = this.getSpotMarketAccount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
@@ -400,9 +398,8 @@ class ClearingHouse {
400
398
  }
401
399
  for (const position of userAccount.perpPositions) {
402
400
  if (!(0, position_1.positionIsAvailable)(position)) {
403
- const marketIndexNum = position.marketIndex.toNumber();
404
- const marketAccount = this.getPerpMarketAccount(marketIndexNum);
405
- perpMarketAccountMap.set(marketIndexNum, {
401
+ const marketAccount = this.getPerpMarketAccount(position.marketIndex);
402
+ perpMarketAccountMap.set(position.marketIndex, {
406
403
  pubkey: marketAccount.pubkey,
407
404
  isSigner: false,
408
405
  isWritable: false,
@@ -414,9 +411,9 @@ class ClearingHouse {
414
411
  });
415
412
  }
416
413
  }
417
- if (params.readablePerpMarketIndex) {
418
- const marketAccount = this.getPerpMarketAccount(params.readablePerpMarketIndex.toNumber());
419
- perpMarketAccountMap.set(params.readablePerpMarketIndex.toNumber(), {
414
+ if (params.readablePerpMarketIndex !== undefined) {
415
+ const marketAccount = this.getPerpMarketAccount(params.readablePerpMarketIndex);
416
+ perpMarketAccountMap.set(params.readablePerpMarketIndex, {
420
417
  pubkey: marketAccount.pubkey,
421
418
  isSigner: false,
422
419
  isWritable: false,
@@ -427,9 +424,9 @@ class ClearingHouse {
427
424
  isWritable: false,
428
425
  });
429
426
  }
430
- if (params.writablePerpMarketIndex) {
431
- const marketAccount = this.getPerpMarketAccount(params.writablePerpMarketIndex.toNumber());
432
- perpMarketAccountMap.set(params.writablePerpMarketIndex.toNumber(), {
427
+ if (params.writablePerpMarketIndex !== undefined) {
428
+ const marketAccount = this.getPerpMarketAccount(params.writablePerpMarketIndex);
429
+ perpMarketAccountMap.set(params.writablePerpMarketIndex, {
433
430
  pubkey: marketAccount.pubkey,
434
431
  isSigner: false,
435
432
  isWritable: true,
@@ -443,12 +440,12 @@ class ClearingHouse {
443
440
  for (const spotPosition of userAccount.spotPositions) {
444
441
  if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
445
442
  const spotMarketAccount = this.getSpotMarketAccount(spotPosition.marketIndex);
446
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
443
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
447
444
  pubkey: spotMarketAccount.pubkey,
448
445
  isSigner: false,
449
446
  isWritable: false,
450
447
  });
451
- if (!spotMarketAccount.marketIndex.eq(numericConstants_1.ZERO)) {
448
+ if (spotMarketAccount.marketIndex !== 0) {
452
449
  oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
453
450
  pubkey: spotMarketAccount.oracle,
454
451
  isSigner: false,
@@ -457,14 +454,14 @@ class ClearingHouse {
457
454
  }
458
455
  }
459
456
  }
460
- if (params.readableSpotMarketIndex) {
457
+ if (params.readableSpotMarketIndex !== undefined) {
461
458
  const spotMarketAccount = this.getSpotMarketAccount(params.readableSpotMarketIndex);
462
- spotMarketAccountMap.set(params.readableSpotMarketIndex.toNumber(), {
459
+ spotMarketAccountMap.set(params.readableSpotMarketIndex, {
463
460
  pubkey: spotMarketAccount.pubkey,
464
461
  isSigner: false,
465
462
  isWritable: false,
466
463
  });
467
- if (!spotMarketAccount.marketIndex.eq(numericConstants_1.ZERO)) {
464
+ if (spotMarketAccount.marketIndex !== 0) {
468
465
  oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
469
466
  pubkey: spotMarketAccount.oracle,
470
467
  isSigner: false,
@@ -472,14 +469,14 @@ class ClearingHouse {
472
469
  });
473
470
  }
474
471
  }
475
- if (params.writableSpotMarketIndex) {
472
+ if (params.writableSpotMarketIndex !== undefined) {
476
473
  const spotMarketAccount = this.getSpotMarketAccount(params.writableSpotMarketIndex);
477
- spotMarketAccountMap.set(params.writableSpotMarketIndex.toNumber(), {
474
+ spotMarketAccountMap.set(params.writableSpotMarketIndex, {
478
475
  pubkey: spotMarketAccount.pubkey,
479
476
  isSigner: false,
480
477
  isWritable: true,
481
478
  });
482
- if (!spotMarketAccount.marketIndex.eq(numericConstants_1.ZERO)) {
479
+ if (spotMarketAccount.marketIndex !== 0) {
483
480
  oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
484
481
  pubkey: spotMarketAccount.oracle,
485
482
  isSigner: false,
@@ -510,7 +507,7 @@ class ClearingHouse {
510
507
  const authority = this.authority;
511
508
  const createWSOLTokenAccount = isSolMarket && collateralAccountPublicKey.equals(authority);
512
509
  if (createWSOLTokenAccount) {
513
- const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(amount);
510
+ const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(amount, true);
514
511
  collateralAccountPublicKey = pubkey;
515
512
  ixs.forEach((ix) => {
516
513
  tx.add(ix);
@@ -576,28 +573,7 @@ class ClearingHouse {
576
573
  return false;
577
574
  }
578
575
  }
579
- async getSolWithdrawalIxs(marketIndex, amount) {
580
- const result = {
581
- ixs: [],
582
- signers: [],
583
- pubkey: web3_js_1.PublicKey.default,
584
- };
585
- // Create a temporary wrapped SOL account to store the SOL that we're withdrawing
586
- const authority = this.wallet.publicKey;
587
- const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(amount);
588
- result.pubkey = pubkey;
589
- ixs.forEach((ix) => {
590
- result.ixs.push(ix);
591
- });
592
- signers.forEach((ix) => {
593
- result.signers.push(ix);
594
- });
595
- const withdrawIx = await this.getWithdrawIx(amount, marketIndex, pubkey, true);
596
- result.ixs.push(withdrawIx);
597
- result.ixs.push(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, pubkey, authority, authority, []));
598
- return result;
599
- }
600
- async getWrappedSolAccountCreationIxs(amount) {
576
+ async getWrappedSolAccountCreationIxs(amount, isDeposit) {
601
577
  const wrappedSolAccount = new web3_js_1.Keypair();
602
578
  const result = {
603
579
  ixs: [],
@@ -605,12 +581,14 @@ class ClearingHouse {
605
581
  pubkey: wrappedSolAccount.publicKey,
606
582
  };
607
583
  const rentSpaceLamports = new anchor_1.BN(web3_js_1.LAMPORTS_PER_SOL / 100);
608
- const depositAmountLamports = amount.add(rentSpaceLamports);
584
+ const lamports = isDeposit
585
+ ? amount.add(rentSpaceLamports)
586
+ : rentSpaceLamports;
609
587
  const authority = this.wallet.publicKey;
610
588
  result.ixs.push(web3_js_1.SystemProgram.createAccount({
611
589
  fromPubkey: authority,
612
590
  newAccountPubkey: wrappedSolAccount.publicKey,
613
- lamports: depositAmountLamports.toNumber(),
591
+ lamports: lamports.toNumber(),
614
592
  space: 165,
615
593
  programId: spl_token_1.TOKEN_PROGRAM_ID,
616
594
  }));
@@ -632,7 +610,7 @@ class ClearingHouse {
632
610
  * @param fromUserId
633
611
  * @returns
634
612
  */
635
- async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = new anchor_1.BN(0), userId = 0, name = userName_1.DEFAULT_USER_NAME, fromUserId, referrerInfo) {
613
+ async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, userId = 0, name = userName_1.DEFAULT_USER_NAME, fromUserId, referrerInfo) {
636
614
  const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(userId, name, referrerInfo);
637
615
  const additionalSigners = [];
638
616
  const spotMarket = this.getSpotMarketAccount(marketIndex);
@@ -641,7 +619,7 @@ class ClearingHouse {
641
619
  const authority = this.wallet.publicKey;
642
620
  const createWSOLTokenAccount = isSolMarket && userTokenAccount.equals(authority);
643
621
  if (createWSOLTokenAccount) {
644
- const { ixs: startIxs, signers, pubkey, } = await this.getWrappedSolAccountCreationIxs(amount);
622
+ const { ixs: startIxs, signers, pubkey, } = await this.getWrappedSolAccountCreationIxs(amount, true);
645
623
  userTokenAccount = pubkey;
646
624
  startIxs.forEach((ix) => {
647
625
  tx.add(ix);
@@ -682,7 +660,7 @@ class ClearingHouse {
682
660
  const authority = this.wallet.publicKey;
683
661
  const createWSOLTokenAccount = isSolMarket && userTokenAccount.equals(authority);
684
662
  if (createWSOLTokenAccount) {
685
- const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(amount);
663
+ const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(amount, false);
686
664
  userTokenAccount = pubkey;
687
665
  ixs.forEach((ix) => {
688
666
  tx.add(ix);
@@ -776,7 +754,7 @@ class ClearingHouse {
776
754
  isWritable: true,
777
755
  isSigner: false,
778
756
  });
779
- if (marketIndex.eq(position.marketIndex)) {
757
+ if (marketIndex === position.marketIndex) {
780
758
  foundMarket = true;
781
759
  }
782
760
  }
@@ -803,7 +781,7 @@ class ClearingHouse {
803
781
  });
804
782
  if (sharesToBurn == undefined) {
805
783
  const userAccount = this.getUserAccount();
806
- const perpPosition = userAccount.perpPositions.filter((position) => position.marketIndex.eq(marketIndex))[0];
784
+ const perpPosition = userAccount.perpPositions.filter((position) => position.marketIndex === marketIndex)[0];
807
785
  sharesToBurn = perpPosition.lpShares;
808
786
  console.log('burning lp shares:', sharesToBurn.toString());
809
787
  }
@@ -818,7 +796,7 @@ class ClearingHouse {
818
796
  }
819
797
  async addLiquidity(amount, marketIndex) {
820
798
  const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getAddLiquidityIx(amount, marketIndex)), [], this.opts);
821
- this.marketLastSlotCache.set(marketIndex.toNumber(), slot);
799
+ this.marketLastSlotCache.set(marketIndex, slot);
822
800
  return txSig;
823
801
  }
824
802
  async getAddLiquidityIx(amount, marketIndex) {
@@ -871,12 +849,12 @@ class ClearingHouse {
871
849
  fillTx.feePayer = userAccount.authority;
872
850
  const [signedMarketOrderTx, signedFillTx] = await this.provider.wallet.signAllTransactions([marketOrderTx, fillTx]);
873
851
  const { txSig, slot } = await this.txSender.send(signedMarketOrderTx, [], this.opts, true);
874
- this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
852
+ this.marketLastSlotCache.set(orderParams.marketIndex, slot);
875
853
  return { txSig, signedFillTx };
876
854
  }
877
855
  async placeOrder(orderParams) {
878
856
  const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceOrderIx(orderParams)), [], this.opts);
879
- this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
857
+ this.marketLastSlotCache.set(orderParams.marketIndex, slot);
880
858
  return txSig;
881
859
  }
882
860
  getOrderParams(optionalOrderParams, marketType) {
@@ -906,12 +884,12 @@ class ClearingHouse {
906
884
  }
907
885
  async getUpdateAMMsIx(marketIndexes) {
908
886
  for (let i = marketIndexes.length; i < 5; i++) {
909
- marketIndexes.push(new anchor_1.BN(100));
887
+ marketIndexes.push(100);
910
888
  }
911
889
  const marketAccountInfos = [];
912
890
  const oracleAccountInfos = [];
913
891
  for (const marketIndex of marketIndexes) {
914
- if (!marketIndex.eq(new anchor_1.BN(100))) {
892
+ if (marketIndex !== 100) {
915
893
  const market = this.getPerpMarketAccount(marketIndex);
916
894
  marketAccountInfos.push({
917
895
  pubkey: market.pubkey,
@@ -1022,7 +1000,7 @@ class ClearingHouse {
1022
1000
  for (const spotPosition of userAccount.spotPositions) {
1023
1001
  if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
1024
1002
  const spotMarketAccount = this.getSpotMarketAccount(spotPosition.marketIndex);
1025
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
1003
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
1026
1004
  pubkey: spotMarketAccount.pubkey,
1027
1005
  isSigner: false,
1028
1006
  isWritable: false,
@@ -1038,9 +1016,9 @@ class ClearingHouse {
1038
1016
  }
1039
1017
  for (const position of userAccount.perpPositions) {
1040
1018
  if (!(0, position_1.positionIsAvailable)(position) &&
1041
- !position.marketIndex.eq(order.marketIndex)) {
1019
+ position.marketIndex !== order.marketIndex) {
1042
1020
  const market = this.getPerpMarketAccount(position.marketIndex);
1043
- perpMarketAccountMap.set(position.marketIndex.toNumber(), {
1021
+ perpMarketAccountMap.set(position.marketIndex, {
1044
1022
  pubkey: market.pubkey,
1045
1023
  isWritable: false,
1046
1024
  isSigner: false,
@@ -1052,7 +1030,7 @@ class ClearingHouse {
1052
1030
  });
1053
1031
  }
1054
1032
  }
1055
- perpMarketAccountMap.set(marketIndex.toNumber(), {
1033
+ perpMarketAccountMap.set(marketIndex, {
1056
1034
  pubkey: marketAccount.pubkey,
1057
1035
  isWritable: true,
1058
1036
  isSigner: false,
@@ -1142,7 +1120,7 @@ class ClearingHouse {
1142
1120
  for (const spotPosition of userAccount.spotPositions) {
1143
1121
  if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
1144
1122
  const spotMarket = this.getSpotMarketAccount(spotPosition.marketIndex);
1145
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
1123
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
1146
1124
  pubkey: spotMarket.pubkey,
1147
1125
  isSigner: false,
1148
1126
  isWritable: false,
@@ -1157,10 +1135,9 @@ class ClearingHouse {
1157
1135
  }
1158
1136
  }
1159
1137
  for (const position of userAccount.perpPositions) {
1160
- if (!(0, position_1.positionIsAvailable)(position) &&
1161
- !position.marketIndex.eq(order.marketIndex)) {
1138
+ if (!(0, position_1.positionIsAvailable)(position)) {
1162
1139
  const market = this.getPerpMarketAccount(position.marketIndex);
1163
- perpMarketAccountMap.set(position.marketIndex.toNumber(), {
1140
+ perpMarketAccountMap.set(position.marketIndex, {
1164
1141
  pubkey: market.pubkey,
1165
1142
  isWritable: false,
1166
1143
  isSigner: false,
@@ -1173,7 +1150,7 @@ class ClearingHouse {
1173
1150
  }
1174
1151
  }
1175
1152
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
1176
- spotMarketAccountMap.set(marketIndex.toNumber(), {
1153
+ spotMarketAccountMap.set(marketIndex, {
1177
1154
  pubkey: spotMarketAccount.pubkey,
1178
1155
  isWritable: true,
1179
1156
  isSigner: false,
@@ -1186,7 +1163,7 @@ class ClearingHouse {
1186
1163
  });
1187
1164
  }
1188
1165
  const quoteMarketAccount = this.getQuoteSpotMarketAccount();
1189
- spotMarketAccountMap.set(quoteMarketAccount.marketIndex.toNumber(), {
1166
+ spotMarketAccountMap.set(quoteMarketAccount.marketIndex, {
1190
1167
  pubkey: quoteMarketAccount.pubkey,
1191
1168
  isWritable: true,
1192
1169
  isSigner: false,
@@ -1298,6 +1275,11 @@ class ClearingHouse {
1298
1275
  isWritable: true,
1299
1276
  isSigner: false,
1300
1277
  });
1278
+ remainingAccounts.push({
1279
+ pubkey: this.getStateAccount().srmVault,
1280
+ isWritable: false,
1281
+ isSigner: false,
1282
+ });
1301
1283
  }
1302
1284
  return await this.program.instruction.fillSpotOrder(orderId, fulfillmentConfig ? fulfillmentConfig.fulfillmentType : null, makerOrderId, {
1303
1285
  accounts: {
@@ -1325,7 +1307,7 @@ class ClearingHouse {
1325
1307
  for (const spotPosition of userAccount.spotPositions) {
1326
1308
  if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
1327
1309
  const spotMarketAccount = this.getSpotMarketAccount(spotPosition.marketIndex);
1328
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
1310
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
1329
1311
  pubkey: spotMarketAccount.pubkey,
1330
1312
  isSigner: false,
1331
1313
  isWritable: false,
@@ -1341,9 +1323,9 @@ class ClearingHouse {
1341
1323
  }
1342
1324
  for (const position of userAccount.perpPositions) {
1343
1325
  if (!(0, position_1.positionIsAvailable)(position) &&
1344
- !position.marketIndex.eq(order.marketIndex)) {
1326
+ position.marketIndex !== order.marketIndex) {
1345
1327
  const market = this.getPerpMarketAccount(position.marketIndex);
1346
- perpMarketAccountMap.set(position.marketIndex.toNumber(), {
1328
+ perpMarketAccountMap.set(position.marketIndex, {
1347
1329
  pubkey: market.pubkey,
1348
1330
  isWritable: false,
1349
1331
  isSigner: false,
@@ -1355,7 +1337,7 @@ class ClearingHouse {
1355
1337
  });
1356
1338
  }
1357
1339
  }
1358
- perpMarketAccountMap.set(marketIndex.toNumber(), {
1340
+ perpMarketAccountMap.set(marketIndex, {
1359
1341
  pubkey: marketAccount.pubkey,
1360
1342
  isWritable: true,
1361
1343
  isSigner: false,
@@ -1395,7 +1377,7 @@ class ClearingHouse {
1395
1377
  for (const spotPosition of userAccount.spotPositions) {
1396
1378
  if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
1397
1379
  const spotMarketAccount = this.getSpotMarketAccount(spotPosition.marketIndex);
1398
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
1380
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
1399
1381
  pubkey: spotMarketAccount.pubkey,
1400
1382
  isSigner: false,
1401
1383
  isWritable: false,
@@ -1411,9 +1393,9 @@ class ClearingHouse {
1411
1393
  }
1412
1394
  for (const position of userAccount.perpPositions) {
1413
1395
  if (!(0, position_1.positionIsAvailable)(position) &&
1414
- !position.marketIndex.eq(order.marketIndex)) {
1396
+ position.marketIndex !== order.marketIndex) {
1415
1397
  const market = this.getPerpMarketAccount(position.marketIndex);
1416
- perpMarketAccountMap.set(position.marketIndex.toNumber(), {
1398
+ perpMarketAccountMap.set(position.marketIndex, {
1417
1399
  pubkey: market.pubkey,
1418
1400
  isWritable: false,
1419
1401
  isSigner: false,
@@ -1426,12 +1408,12 @@ class ClearingHouse {
1426
1408
  }
1427
1409
  }
1428
1410
  const quoteSpotMarket = this.getQuoteSpotMarketAccount();
1429
- spotMarketAccountMap.set(quoteSpotMarket.marketIndex.toNumber(), {
1411
+ spotMarketAccountMap.set(quoteSpotMarket.marketIndex, {
1430
1412
  pubkey: quoteSpotMarket.pubkey,
1431
1413
  isWritable: true,
1432
1414
  isSigner: false,
1433
1415
  });
1434
- spotMarketAccountMap.set(marketIndex.toNumber(), {
1416
+ spotMarketAccountMap.set(marketIndex, {
1435
1417
  pubkey: spotMarketAccount.pubkey,
1436
1418
  isWritable: false,
1437
1419
  isSigner: false,
@@ -1459,7 +1441,7 @@ class ClearingHouse {
1459
1441
  }
1460
1442
  async placeAndTake(orderParams, makerInfo, referrerInfo) {
1461
1443
  const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceAndTakeIx(orderParams, makerInfo, referrerInfo)), [], this.opts);
1462
- this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
1444
+ this.marketLastSlotCache.set(orderParams.marketIndex, slot);
1463
1445
  return txSig;
1464
1446
  }
1465
1447
  async getPlaceAndTakeIx(orderParams, makerInfo, referrerInfo) {
@@ -1508,7 +1490,7 @@ class ClearingHouse {
1508
1490
  }
1509
1491
  async placeAndMake(orderParams, takerInfo, referrerInfo) {
1510
1492
  const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceAndMakeIx(orderParams, takerInfo, referrerInfo)), [], this.opts);
1511
- this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
1493
+ this.marketLastSlotCache.set(orderParams.marketIndex, slot);
1512
1494
  return txSig;
1513
1495
  }
1514
1496
  async getPlaceAndMakeIx(orderParams, takerInfo, referrerInfo) {
@@ -1589,7 +1571,7 @@ class ClearingHouse {
1589
1571
  for (const position of settleeUserAccount.perpPositions) {
1590
1572
  if (!(0, position_1.positionIsAvailable)(position)) {
1591
1573
  const market = this.getPerpMarketAccount(position.marketIndex);
1592
- perpMarketAccountMap.set(position.marketIndex.toNumber(), {
1574
+ perpMarketAccountMap.set(position.marketIndex, {
1593
1575
  pubkey: market.pubkey,
1594
1576
  isWritable: false,
1595
1577
  isSigner: false,
@@ -1604,12 +1586,12 @@ class ClearingHouse {
1604
1586
  for (const spotPosition of settleeUserAccount.spotPositions) {
1605
1587
  if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
1606
1588
  const spotMarketAccount = this.getSpotMarketAccount(spotPosition.marketIndex);
1607
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
1589
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
1608
1590
  pubkey: spotMarketAccount.pubkey,
1609
1591
  isSigner: false,
1610
1592
  isWritable: false,
1611
1593
  });
1612
- if (!spotMarketAccount.marketIndex.eq(numericConstants_1.ZERO)) {
1594
+ if (spotMarketAccount.marketIndex !== 0) {
1613
1595
  oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
1614
1596
  pubkey: spotMarketAccount.oracle,
1615
1597
  isSigner: false,
@@ -1618,8 +1600,8 @@ class ClearingHouse {
1618
1600
  }
1619
1601
  }
1620
1602
  }
1621
- const marketAccount = this.getPerpMarketAccount(marketIndex.toNumber());
1622
- perpMarketAccountMap.set(marketIndex.toNumber(), {
1603
+ const marketAccount = this.getPerpMarketAccount(marketIndex);
1604
+ perpMarketAccountMap.set(marketIndex, {
1623
1605
  pubkey: marketAccount.pubkey,
1624
1606
  isSigner: false,
1625
1607
  isWritable: true,
@@ -1629,7 +1611,7 @@ class ClearingHouse {
1629
1611
  isSigner: false,
1630
1612
  isWritable: false,
1631
1613
  });
1632
- spotMarketAccountMap.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX.toNumber(), {
1614
+ spotMarketAccountMap.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX, {
1633
1615
  pubkey: this.getSpotMarketAccount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX).pubkey,
1634
1616
  isSigner: false,
1635
1617
  isWritable: true,
@@ -1659,7 +1641,7 @@ class ClearingHouse {
1659
1641
  for (const position of settleeUserAccount.perpPositions) {
1660
1642
  if (!(0, position_1.positionIsAvailable)(position)) {
1661
1643
  const market = this.getPerpMarketAccount(position.marketIndex);
1662
- marketAccountMap.set(position.marketIndex.toNumber(), {
1644
+ marketAccountMap.set(position.marketIndex, {
1663
1645
  pubkey: market.pubkey,
1664
1646
  isWritable: false,
1665
1647
  isSigner: false,
@@ -1674,12 +1656,12 @@ class ClearingHouse {
1674
1656
  for (const userBankBalance of settleeUserAccount.spotPositions) {
1675
1657
  if (!userBankBalance.balance.eq(numericConstants_1.ZERO)) {
1676
1658
  const bankAccount = this.getSpotMarketAccount(userBankBalance.marketIndex);
1677
- spotMarketAccountMap.set(userBankBalance.marketIndex.toNumber(), {
1659
+ spotMarketAccountMap.set(userBankBalance.marketIndex, {
1678
1660
  pubkey: bankAccount.pubkey,
1679
1661
  isSigner: false,
1680
1662
  isWritable: false,
1681
1663
  });
1682
- if (!bankAccount.marketIndex.eq(numericConstants_1.ZERO)) {
1664
+ if (bankAccount.marketIndex !== 0) {
1683
1665
  oracleAccountMap.set(bankAccount.oracle.toString(), {
1684
1666
  pubkey: bankAccount.oracle,
1685
1667
  isSigner: false,
@@ -1688,8 +1670,8 @@ class ClearingHouse {
1688
1670
  }
1689
1671
  }
1690
1672
  }
1691
- const marketAccount = this.getPerpMarketAccount(marketIndex.toNumber());
1692
- marketAccountMap.set(marketIndex.toNumber(), {
1673
+ const marketAccount = this.getPerpMarketAccount(marketIndex);
1674
+ marketAccountMap.set(marketIndex, {
1693
1675
  pubkey: marketAccount.pubkey,
1694
1676
  isSigner: false,
1695
1677
  isWritable: true,
@@ -1699,7 +1681,7 @@ class ClearingHouse {
1699
1681
  isSigner: false,
1700
1682
  isWritable: false,
1701
1683
  });
1702
- spotMarketAccountMap.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX.toNumber(), {
1684
+ spotMarketAccountMap.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX, {
1703
1685
  pubkey: this.getSpotMarketAccount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX).pubkey,
1704
1686
  isSigner: false,
1705
1687
  isWritable: true,
@@ -1720,7 +1702,7 @@ class ClearingHouse {
1720
1702
  }
1721
1703
  async liquidatePerp(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount) {
1722
1704
  const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getLiquidatePerpIx(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount)), [], this.opts);
1723
- this.marketLastSlotCache.set(marketIndex.toNumber(), slot);
1705
+ this.marketLastSlotCache.set(marketIndex, slot);
1724
1706
  return txSig;
1725
1707
  }
1726
1708
  async getLiquidatePerpIx(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount) {
@@ -1743,17 +1725,17 @@ class ClearingHouse {
1743
1725
  remainingAccounts: remainingAccounts,
1744
1726
  });
1745
1727
  }
1746
- async liquidateBorrow(userAccountPublicKey, userAccount, assetmarketIndex, liabilitymarketIndex, maxLiabilityTransfer) {
1747
- const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getLiquidateBorrowIx(userAccountPublicKey, userAccount, assetmarketIndex, liabilitymarketIndex, maxLiabilityTransfer)), [], this.opts);
1728
+ async liquidateBorrow(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer) {
1729
+ const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getLiquidateBorrowIx(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer)), [], this.opts);
1748
1730
  return txSig;
1749
1731
  }
1750
- async getLiquidateBorrowIx(userAccountPublicKey, userAccount, assetmarketIndex, liabilitymarketIndex, maxLiabilityTransfer) {
1732
+ async getLiquidateBorrowIx(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer) {
1751
1733
  const liquidatorPublicKey = await this.getUserAccountPublicKey();
1752
1734
  const remainingAccounts = this.getRemainingAccountsWithCounterparty({
1753
1735
  counterPartyUserAccount: userAccount,
1754
- writableSpotMarketIndexes: [liabilitymarketIndex, assetmarketIndex],
1736
+ writableSpotMarketIndexes: [liabilityMarketIndex, assetMarketIndex],
1755
1737
  });
1756
- return await this.program.instruction.liquidateBorrow(assetmarketIndex, liabilitymarketIndex, maxLiabilityTransfer, {
1738
+ return await this.program.instruction.liquidateBorrow(assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, {
1757
1739
  accounts: {
1758
1740
  state: await this.getStatePublicKey(),
1759
1741
  authority: this.wallet.publicKey,
@@ -1763,19 +1745,19 @@ class ClearingHouse {
1763
1745
  remainingAccounts: remainingAccounts,
1764
1746
  });
1765
1747
  }
1766
- async liquidateBorrowForPerpPnl(userAccountPublicKey, userAccount, perpMarketIndex, liabilitymarketIndex, maxLiabilityTransfer) {
1767
- const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilitymarketIndex, maxLiabilityTransfer)), [], this.opts);
1768
- this.marketLastSlotCache.set(perpMarketIndex.toNumber(), slot);
1748
+ async liquidateBorrowForPerpPnl(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer) {
1749
+ const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer)), [], this.opts);
1750
+ this.marketLastSlotCache.set(perpMarketIndex, slot);
1769
1751
  return txSig;
1770
1752
  }
1771
- async getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilitymarketIndex, maxLiabilityTransfer) {
1753
+ async getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer) {
1772
1754
  const liquidatorPublicKey = await this.getUserAccountPublicKey();
1773
1755
  const remainingAccounts = this.getRemainingAccountsWithCounterparty({
1774
1756
  counterPartyUserAccount: userAccount,
1775
1757
  writablePerpMarketIndex: perpMarketIndex,
1776
- writableSpotMarketIndexes: [liabilitymarketIndex],
1758
+ writableSpotMarketIndexes: [liabilityMarketIndex],
1777
1759
  });
1778
- return await this.program.instruction.liquidateBorrowForPerpPnl(perpMarketIndex, liabilitymarketIndex, maxLiabilityTransfer, {
1760
+ return await this.program.instruction.liquidateBorrowForPerpPnl(perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, {
1779
1761
  accounts: {
1780
1762
  state: await this.getStatePublicKey(),
1781
1763
  authority: this.wallet.publicKey,
@@ -1787,7 +1769,7 @@ class ClearingHouse {
1787
1769
  }
1788
1770
  async liquidatePerpPnlForDeposit(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer) {
1789
1771
  const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getLiquidatePerpPnlForDepositIx(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer)), [], this.opts);
1790
- this.marketLastSlotCache.set(perpMarketIndex.toNumber(), slot);
1772
+ this.marketLastSlotCache.set(perpMarketIndex, slot);
1791
1773
  return txSig;
1792
1774
  }
1793
1775
  async getLiquidatePerpPnlForDepositIx(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer) {
@@ -1866,7 +1848,7 @@ class ClearingHouse {
1866
1848
  for (const spotPosition of counterPartyUserAccount.spotPositions) {
1867
1849
  if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
1868
1850
  const spotMarket = this.getSpotMarketAccount(spotPosition.marketIndex);
1869
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
1851
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
1870
1852
  pubkey: spotMarket.pubkey,
1871
1853
  isSigner: false,
1872
1854
  isWritable: false,
@@ -1883,7 +1865,7 @@ class ClearingHouse {
1883
1865
  for (const position of counterPartyUserAccount.perpPositions) {
1884
1866
  if (!(0, position_1.positionIsAvailable)(position)) {
1885
1867
  const market = this.getPerpMarketAccount(position.marketIndex);
1886
- marketAccountMap.set(position.marketIndex.toNumber(), {
1868
+ marketAccountMap.set(position.marketIndex, {
1887
1869
  pubkey: market.pubkey,
1888
1870
  isWritable: false,
1889
1871
  isSigner: false,
@@ -1923,7 +1905,7 @@ class ClearingHouse {
1923
1905
  for (const spotPosition of userAccount.spotPositions) {
1924
1906
  if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
1925
1907
  const spotMarket = this.getSpotMarketAccount(spotPosition.marketIndex);
1926
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
1908
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
1927
1909
  pubkey: spotMarket.pubkey,
1928
1910
  isSigner: false,
1929
1911
  isWritable: false,
@@ -1940,7 +1922,7 @@ class ClearingHouse {
1940
1922
  for (const position of userAccount.perpPositions) {
1941
1923
  if (!(0, position_1.positionIsAvailable)(position)) {
1942
1924
  const market = this.getPerpMarketAccount(position.marketIndex);
1943
- marketAccountMap.set(position.marketIndex.toNumber(), {
1925
+ marketAccountMap.set(position.marketIndex, {
1944
1926
  pubkey: market.pubkey,
1945
1927
  isWritable: false,
1946
1928
  isSigner: false,
@@ -1952,9 +1934,9 @@ class ClearingHouse {
1952
1934
  });
1953
1935
  }
1954
1936
  }
1955
- if (params.writablePerpMarketIndex) {
1937
+ if (params.writablePerpMarketIndex !== undefined) {
1956
1938
  const market = this.getPerpMarketAccount(params.writablePerpMarketIndex);
1957
- marketAccountMap.set(market.marketIndex.toNumber(), {
1939
+ marketAccountMap.set(market.marketIndex, {
1958
1940
  pubkey: market.pubkey,
1959
1941
  isSigner: false,
1960
1942
  isWritable: true,
@@ -1965,10 +1947,10 @@ class ClearingHouse {
1965
1947
  isWritable: false,
1966
1948
  });
1967
1949
  }
1968
- if (params.writableSpotMarketIndexes) {
1950
+ if (params.writableSpotMarketIndexes !== undefined) {
1969
1951
  for (const writableSpotMarketIndex of params.writableSpotMarketIndexes) {
1970
1952
  const spotMarketAccount = this.getSpotMarketAccount(writableSpotMarketIndex);
1971
- spotMarketAccountMap.set(spotMarketAccount.marketIndex.toNumber(), {
1953
+ spotMarketAccountMap.set(spotMarketAccount.marketIndex, {
1972
1954
  pubkey: spotMarketAccount.pubkey,
1973
1955
  isSigner: false,
1974
1956
  isWritable: true,
@@ -1,7 +1,5 @@
1
- /// <reference types="bn.js" />
2
1
  import { ConfirmOptions, Connection, PublicKey } from '@solana/web3.js';
3
2
  import { IWallet } from './types';
4
- import { BN } from '@project-serum/anchor';
5
3
  import { OracleInfo } from './oracles/types';
6
4
  import { BulkAccountLoader } from './accounts/bulkAccountLoader';
7
5
  import { DriftEnv } from './config';
@@ -14,8 +12,8 @@ export declare type ClearingHouseConfig = {
14
12
  txSenderConfig?: TxSenderConfig;
15
13
  userIds?: number[];
16
14
  activeUserId?: number;
17
- perpMarketIndexes?: BN[];
18
- spotMarketIndexes?: BN[];
15
+ perpMarketIndexes?: number[];
16
+ spotMarketIndexes?: number[];
19
17
  oracleInfos?: OracleInfo[];
20
18
  env?: DriftEnv;
21
19
  userStats?: boolean;