@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
@@ -293,9 +293,8 @@ export class ClearingHouse {
293
293
  }
294
294
 
295
295
  public getPerpMarketAccount(
296
- marketIndex: BN | number
296
+ marketIndex: number
297
297
  ): PerpMarketAccount | undefined {
298
- marketIndex = marketIndex instanceof BN ? marketIndex : new BN(marketIndex);
299
298
  return this.accountSubscriber.getMarketAccountAndSlot(marketIndex)?.data;
300
299
  }
301
300
 
@@ -306,9 +305,8 @@ export class ClearingHouse {
306
305
  }
307
306
 
308
307
  public getSpotMarketAccount(
309
- marketIndex: BN | number
308
+ marketIndex: number
310
309
  ): SpotMarketAccount | undefined {
311
- marketIndex = marketIndex instanceof BN ? marketIndex : new BN(marketIndex);
312
310
  return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
313
311
  }
314
312
 
@@ -370,6 +368,7 @@ export class ClearingHouse {
370
368
  this.wallet = newWallet;
371
369
  this.provider = newProvider;
372
370
  this.program = newProgram;
371
+ this.authority = newWallet.publicKey;
373
372
 
374
373
  if (this.isSubscribed) {
375
374
  await Promise.all(this.unsubscribeUsers());
@@ -574,11 +573,9 @@ export class ClearingHouse {
574
573
  return this.getUser(userId).getUserAccountAndSlot();
575
574
  }
576
575
 
577
- public getSpotPosition(marketIndex: number | BN): SpotPosition | undefined {
578
- const marketIndexBN =
579
- marketIndex instanceof BN ? marketIndex : new BN(marketIndex);
580
- return this.getUserAccount().spotPositions.find((spotPosition) =>
581
- spotPosition.marketIndex.eq(marketIndexBN)
576
+ public getSpotPosition(marketIndex: number): SpotPosition | undefined {
577
+ return this.getUserAccount().spotPositions.find(
578
+ (spotPosition) => spotPosition.marketIndex === marketIndex
582
579
  );
583
580
  }
584
581
 
@@ -593,10 +590,10 @@ export class ClearingHouse {
593
590
  }
594
591
 
595
592
  getRemainingAccounts(params: {
596
- writablePerpMarketIndex?: BN;
597
- writableSpotMarketIndex?: BN;
598
- readablePerpMarketIndex?: BN;
599
- readableSpotMarketIndex?: BN;
593
+ writablePerpMarketIndex?: number;
594
+ writableSpotMarketIndex?: number;
595
+ readablePerpMarketIndex?: number;
596
+ readableSpotMarketIndex?: number;
600
597
  }): AccountMeta[] {
601
598
  const userAccountAndSlot = this.getUserAccountAndSlot();
602
599
  if (!userAccountAndSlot) {
@@ -632,9 +629,8 @@ export class ClearingHouse {
632
629
 
633
630
  for (const position of userAccount.perpPositions) {
634
631
  if (!positionIsAvailable(position)) {
635
- const marketIndexNum = position.marketIndex.toNumber();
636
- const marketAccount = this.getPerpMarketAccount(marketIndexNum);
637
- perpMarketAccountMap.set(marketIndexNum, {
632
+ const marketAccount = this.getPerpMarketAccount(position.marketIndex);
633
+ perpMarketAccountMap.set(position.marketIndex, {
638
634
  pubkey: marketAccount.pubkey,
639
635
  isSigner: false,
640
636
  isWritable: false,
@@ -647,11 +643,11 @@ export class ClearingHouse {
647
643
  }
648
644
  }
649
645
 
650
- if (params.readablePerpMarketIndex) {
646
+ if (params.readablePerpMarketIndex !== undefined) {
651
647
  const marketAccount = this.getPerpMarketAccount(
652
- params.readablePerpMarketIndex.toNumber()
648
+ params.readablePerpMarketIndex
653
649
  );
654
- perpMarketAccountMap.set(params.readablePerpMarketIndex.toNumber(), {
650
+ perpMarketAccountMap.set(params.readablePerpMarketIndex, {
655
651
  pubkey: marketAccount.pubkey,
656
652
  isSigner: false,
657
653
  isWritable: false,
@@ -663,11 +659,11 @@ export class ClearingHouse {
663
659
  });
664
660
  }
665
661
 
666
- if (params.writablePerpMarketIndex) {
662
+ if (params.writablePerpMarketIndex !== undefined) {
667
663
  const marketAccount = this.getPerpMarketAccount(
668
- params.writablePerpMarketIndex.toNumber()
664
+ params.writablePerpMarketIndex
669
665
  );
670
- perpMarketAccountMap.set(params.writablePerpMarketIndex.toNumber(), {
666
+ perpMarketAccountMap.set(params.writablePerpMarketIndex, {
671
667
  pubkey: marketAccount.pubkey,
672
668
  isSigner: false,
673
669
  isWritable: true,
@@ -684,12 +680,12 @@ export class ClearingHouse {
684
680
  const spotMarketAccount = this.getSpotMarketAccount(
685
681
  spotPosition.marketIndex
686
682
  );
687
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
683
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
688
684
  pubkey: spotMarketAccount.pubkey,
689
685
  isSigner: false,
690
686
  isWritable: false,
691
687
  });
692
- if (!spotMarketAccount.marketIndex.eq(ZERO)) {
688
+ if (spotMarketAccount.marketIndex !== 0) {
693
689
  oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
694
690
  pubkey: spotMarketAccount.oracle,
695
691
  isSigner: false,
@@ -699,16 +695,16 @@ export class ClearingHouse {
699
695
  }
700
696
  }
701
697
 
702
- if (params.readableSpotMarketIndex) {
698
+ if (params.readableSpotMarketIndex !== undefined) {
703
699
  const spotMarketAccount = this.getSpotMarketAccount(
704
700
  params.readableSpotMarketIndex
705
701
  );
706
- spotMarketAccountMap.set(params.readableSpotMarketIndex.toNumber(), {
702
+ spotMarketAccountMap.set(params.readableSpotMarketIndex, {
707
703
  pubkey: spotMarketAccount.pubkey,
708
704
  isSigner: false,
709
705
  isWritable: false,
710
706
  });
711
- if (!spotMarketAccount.marketIndex.eq(ZERO)) {
707
+ if (spotMarketAccount.marketIndex !== 0) {
712
708
  oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
713
709
  pubkey: spotMarketAccount.oracle,
714
710
  isSigner: false,
@@ -717,16 +713,16 @@ export class ClearingHouse {
717
713
  }
718
714
  }
719
715
 
720
- if (params.writableSpotMarketIndex) {
716
+ if (params.writableSpotMarketIndex !== undefined) {
721
717
  const spotMarketAccount = this.getSpotMarketAccount(
722
718
  params.writableSpotMarketIndex
723
719
  );
724
- spotMarketAccountMap.set(params.writableSpotMarketIndex.toNumber(), {
720
+ spotMarketAccountMap.set(params.writableSpotMarketIndex, {
725
721
  pubkey: spotMarketAccount.pubkey,
726
722
  isSigner: false,
727
723
  isWritable: true,
728
724
  });
729
- if (!spotMarketAccount.marketIndex.eq(ZERO)) {
725
+ if (spotMarketAccount.marketIndex !== 0) {
730
726
  oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
731
727
  pubkey: spotMarketAccount.oracle,
732
728
  isSigner: false,
@@ -757,7 +753,7 @@ export class ClearingHouse {
757
753
 
758
754
  public async deposit(
759
755
  amount: BN,
760
- marketIndex: BN,
756
+ marketIndex: number,
761
757
  collateralAccountPublicKey: PublicKey,
762
758
  userId?: number,
763
759
  reduceOnly = false
@@ -776,7 +772,7 @@ export class ClearingHouse {
776
772
 
777
773
  if (createWSOLTokenAccount) {
778
774
  const { ixs, signers, pubkey } =
779
- await this.getWrappedSolAccountCreationIxs(amount);
775
+ await this.getWrappedSolAccountCreationIxs(amount, true);
780
776
 
781
777
  collateralAccountPublicKey = pubkey;
782
778
 
@@ -821,7 +817,7 @@ export class ClearingHouse {
821
817
 
822
818
  async getDepositInstruction(
823
819
  amount: BN,
824
- marketIndex: BN,
820
+ marketIndex: number,
825
821
  userTokenAccount: PublicKey,
826
822
  userId?: number,
827
823
  reduceOnly = false,
@@ -888,63 +884,13 @@ export class ClearingHouse {
888
884
  }
889
885
  }
890
886
 
891
- private async getSolWithdrawalIxs(
892
- marketIndex: BN,
893
- amount: BN
887
+ private async getWrappedSolAccountCreationIxs(
888
+ amount: BN,
889
+ isDeposit?: boolean
894
890
  ): Promise<{
895
891
  ixs: anchor.web3.TransactionInstruction[];
896
892
  signers: Signer[];
897
893
  pubkey: PublicKey;
898
- }> {
899
- const result = {
900
- ixs: [],
901
- signers: [],
902
- pubkey: PublicKey.default,
903
- };
904
-
905
- // Create a temporary wrapped SOL account to store the SOL that we're withdrawing
906
-
907
- const authority = this.wallet.publicKey;
908
-
909
- const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(
910
- amount
911
- );
912
- result.pubkey = pubkey;
913
-
914
- ixs.forEach((ix) => {
915
- result.ixs.push(ix);
916
- });
917
-
918
- signers.forEach((ix) => {
919
- result.signers.push(ix);
920
- });
921
-
922
- const withdrawIx = await this.getWithdrawIx(
923
- amount,
924
- marketIndex,
925
- pubkey,
926
- true
927
- );
928
-
929
- result.ixs.push(withdrawIx);
930
-
931
- result.ixs.push(
932
- Token.createCloseAccountInstruction(
933
- TOKEN_PROGRAM_ID,
934
- pubkey,
935
- authority,
936
- authority,
937
- []
938
- )
939
- );
940
-
941
- return result;
942
- }
943
-
944
- private async getWrappedSolAccountCreationIxs(amount: BN): Promise<{
945
- ixs: anchor.web3.TransactionInstruction[];
946
- signers: Signer[];
947
- pubkey: PublicKey;
948
894
  }> {
949
895
  const wrappedSolAccount = new Keypair();
950
896
 
@@ -956,7 +902,9 @@ export class ClearingHouse {
956
902
 
957
903
  const rentSpaceLamports = new BN(LAMPORTS_PER_SOL / 100);
958
904
 
959
- const depositAmountLamports = amount.add(rentSpaceLamports);
905
+ const lamports = isDeposit
906
+ ? amount.add(rentSpaceLamports)
907
+ : rentSpaceLamports;
960
908
 
961
909
  const authority = this.wallet.publicKey;
962
910
 
@@ -964,7 +912,7 @@ export class ClearingHouse {
964
912
  SystemProgram.createAccount({
965
913
  fromPubkey: authority,
966
914
  newAccountPubkey: wrappedSolAccount.publicKey,
967
- lamports: depositAmountLamports.toNumber(),
915
+ lamports: lamports.toNumber(),
968
916
  space: 165,
969
917
  programId: TOKEN_PROGRAM_ID,
970
918
  })
@@ -1014,7 +962,7 @@ export class ClearingHouse {
1014
962
  public async initializeUserAccountAndDepositCollateral(
1015
963
  amount: BN,
1016
964
  userTokenAccount: PublicKey,
1017
- marketIndex = new BN(0),
965
+ marketIndex = 0,
1018
966
  userId = 0,
1019
967
  name = DEFAULT_USER_NAME,
1020
968
  fromUserId?: number,
@@ -1041,7 +989,7 @@ export class ClearingHouse {
1041
989
  ixs: startIxs,
1042
990
  signers,
1043
991
  pubkey,
1044
- } = await this.getWrappedSolAccountCreationIxs(amount);
992
+ } = await this.getWrappedSolAccountCreationIxs(amount, true);
1045
993
 
1046
994
  userTokenAccount = pubkey;
1047
995
 
@@ -1099,7 +1047,7 @@ export class ClearingHouse {
1099
1047
  public async initializeUserAccountForDevnet(
1100
1048
  userId = 0,
1101
1049
  name = DEFAULT_USER_NAME,
1102
- marketIndex: BN,
1050
+ marketIndex: number,
1103
1051
  tokenFaucet: TokenFaucet,
1104
1052
  amount: BN,
1105
1053
  referrerInfo?: ReferrerInfo
@@ -1136,7 +1084,7 @@ export class ClearingHouse {
1136
1084
 
1137
1085
  public async withdraw(
1138
1086
  amount: BN,
1139
- marketIndex: BN,
1087
+ marketIndex: number,
1140
1088
  userTokenAccount: PublicKey,
1141
1089
  reduceOnly = false
1142
1090
  ): Promise<TransactionSignature> {
@@ -1154,7 +1102,7 @@ export class ClearingHouse {
1154
1102
 
1155
1103
  if (createWSOLTokenAccount) {
1156
1104
  const { ixs, signers, pubkey } =
1157
- await this.getWrappedSolAccountCreationIxs(amount);
1105
+ await this.getWrappedSolAccountCreationIxs(amount, false);
1158
1106
 
1159
1107
  userTokenAccount = pubkey;
1160
1108
 
@@ -1209,7 +1157,7 @@ export class ClearingHouse {
1209
1157
 
1210
1158
  public async getWithdrawIx(
1211
1159
  amount: BN,
1212
- marketIndex: BN,
1160
+ marketIndex: number,
1213
1161
  userTokenAccount: PublicKey,
1214
1162
  reduceOnly = false
1215
1163
  ): Promise<TransactionInstruction> {
@@ -1244,7 +1192,7 @@ export class ClearingHouse {
1244
1192
 
1245
1193
  public async transferDeposit(
1246
1194
  amount: BN,
1247
- marketIndex: BN,
1195
+ marketIndex: number,
1248
1196
  fromUserId: number,
1249
1197
  toUserId: number
1250
1198
  ): Promise<TransactionSignature> {
@@ -1265,7 +1213,7 @@ export class ClearingHouse {
1265
1213
 
1266
1214
  public async getTransferDepositIx(
1267
1215
  amount: BN,
1268
- marketIndex: BN,
1216
+ marketIndex: number,
1269
1217
  fromUserId: number,
1270
1218
  toUserId: number
1271
1219
  ): Promise<TransactionInstruction> {
@@ -1297,7 +1245,7 @@ export class ClearingHouse {
1297
1245
  }
1298
1246
 
1299
1247
  public async updateSpotMarketCumulativeInterest(
1300
- marketIndex: BN
1248
+ marketIndex: number
1301
1249
  ): Promise<TransactionSignature> {
1302
1250
  const { txSig } = await this.txSender.send(
1303
1251
  wrapInTx(await this.updateSpotMarketCumulativeInterestIx(marketIndex)),
@@ -1308,7 +1256,7 @@ export class ClearingHouse {
1308
1256
  }
1309
1257
 
1310
1258
  public async updateSpotMarketCumulativeInterestIx(
1311
- marketIndex: BN
1259
+ marketIndex: number
1312
1260
  ): Promise<TransactionInstruction> {
1313
1261
  const spotMarket = this.getSpotMarketAccount(marketIndex);
1314
1262
  return await this.program.instruction.updateSpotMarketCumulativeInterest({
@@ -1320,7 +1268,7 @@ export class ClearingHouse {
1320
1268
 
1321
1269
  public async settleLP(
1322
1270
  settleeUserAccountPublicKey: PublicKey,
1323
- marketIndex: BN
1271
+ marketIndex: number
1324
1272
  ): Promise<TransactionSignature> {
1325
1273
  const { txSig } = await this.txSender.send(
1326
1274
  wrapInTx(await this.settleLPIx(settleeUserAccountPublicKey, marketIndex)),
@@ -1332,7 +1280,7 @@ export class ClearingHouse {
1332
1280
 
1333
1281
  public async settleLPIx(
1334
1282
  settleeUserAccountPublicKey: PublicKey,
1335
- marketIndex: BN
1283
+ marketIndex: number
1336
1284
  ): Promise<TransactionInstruction> {
1337
1285
  const settleeUserAccount = (await this.program.account.user.fetch(
1338
1286
  settleeUserAccountPublicKey
@@ -1353,7 +1301,7 @@ export class ClearingHouse {
1353
1301
  isSigner: false,
1354
1302
  });
1355
1303
 
1356
- if (marketIndex.eq(position.marketIndex)) {
1304
+ if (marketIndex === position.marketIndex) {
1357
1305
  foundMarket = true;
1358
1306
  }
1359
1307
  }
@@ -1375,7 +1323,7 @@ export class ClearingHouse {
1375
1323
  }
1376
1324
 
1377
1325
  public async removeLiquidity(
1378
- marketIndex: BN,
1326
+ marketIndex: number,
1379
1327
  sharesToBurn?: BN
1380
1328
  ): Promise<TransactionSignature> {
1381
1329
  const { txSig } = await this.txSender.send(
@@ -1387,7 +1335,7 @@ export class ClearingHouse {
1387
1335
  }
1388
1336
 
1389
1337
  public async getRemoveLiquidityIx(
1390
- marketIndex: BN,
1338
+ marketIndex: number,
1391
1339
  sharesToBurn?: BN
1392
1340
  ): Promise<TransactionInstruction> {
1393
1341
  const userAccountPublicKey = await this.getUserAccountPublicKey();
@@ -1398,8 +1346,8 @@ export class ClearingHouse {
1398
1346
 
1399
1347
  if (sharesToBurn == undefined) {
1400
1348
  const userAccount = this.getUserAccount();
1401
- const perpPosition = userAccount.perpPositions.filter((position) =>
1402
- position.marketIndex.eq(marketIndex)
1349
+ const perpPosition = userAccount.perpPositions.filter(
1350
+ (position) => position.marketIndex === marketIndex
1403
1351
  )[0];
1404
1352
  sharesToBurn = perpPosition.lpShares;
1405
1353
  console.log('burning lp shares:', sharesToBurn.toString());
@@ -1417,20 +1365,20 @@ export class ClearingHouse {
1417
1365
 
1418
1366
  public async addLiquidity(
1419
1367
  amount: BN,
1420
- marketIndex: BN
1368
+ marketIndex: number
1421
1369
  ): Promise<TransactionSignature> {
1422
1370
  const { txSig, slot } = await this.txSender.send(
1423
1371
  wrapInTx(await this.getAddLiquidityIx(amount, marketIndex)),
1424
1372
  [],
1425
1373
  this.opts
1426
1374
  );
1427
- this.marketLastSlotCache.set(marketIndex.toNumber(), slot);
1375
+ this.marketLastSlotCache.set(marketIndex, slot);
1428
1376
  return txSig;
1429
1377
  }
1430
1378
 
1431
1379
  public async getAddLiquidityIx(
1432
1380
  amount: BN,
1433
- marketIndex: BN
1381
+ marketIndex: number
1434
1382
  ): Promise<TransactionInstruction> {
1435
1383
  const userAccountPublicKey = await this.getUserAccountPublicKey();
1436
1384
  const remainingAccounts = this.getRemainingAccounts({
@@ -1450,7 +1398,7 @@ export class ClearingHouse {
1450
1398
  public async openPosition(
1451
1399
  direction: PositionDirection,
1452
1400
  amount: BN,
1453
- marketIndex: BN,
1401
+ marketIndex: number,
1454
1402
  limitPrice?: BN
1455
1403
  ): Promise<TransactionSignature> {
1456
1404
  return await this.placeAndTake({
@@ -1511,7 +1459,7 @@ export class ClearingHouse {
1511
1459
  true
1512
1460
  );
1513
1461
 
1514
- this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
1462
+ this.marketLastSlotCache.set(orderParams.marketIndex, slot);
1515
1463
 
1516
1464
  return { txSig, signedFillTx };
1517
1465
  }
@@ -1524,7 +1472,7 @@ export class ClearingHouse {
1524
1472
  [],
1525
1473
  this.opts
1526
1474
  );
1527
- this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
1475
+ this.marketLastSlotCache.set(orderParams.marketIndex, slot);
1528
1476
  return txSig;
1529
1477
  }
1530
1478
 
@@ -1558,7 +1506,9 @@ export class ClearingHouse {
1558
1506
  });
1559
1507
  }
1560
1508
 
1561
- public async updateAMMs(marketIndexes: BN[]): Promise<TransactionSignature> {
1509
+ public async updateAMMs(
1510
+ marketIndexes: number[]
1511
+ ): Promise<TransactionSignature> {
1562
1512
  const { txSig } = await this.txSender.send(
1563
1513
  wrapInTx(await this.getUpdateAMMsIx(marketIndexes)),
1564
1514
  [],
@@ -1568,15 +1518,15 @@ export class ClearingHouse {
1568
1518
  }
1569
1519
 
1570
1520
  public async getUpdateAMMsIx(
1571
- marketIndexes: BN[]
1521
+ marketIndexes: number[]
1572
1522
  ): Promise<TransactionInstruction> {
1573
1523
  for (let i = marketIndexes.length; i < 5; i++) {
1574
- marketIndexes.push(new BN(100));
1524
+ marketIndexes.push(100);
1575
1525
  }
1576
1526
  const marketAccountInfos = [];
1577
1527
  const oracleAccountInfos = [];
1578
1528
  for (const marketIndex of marketIndexes) {
1579
- if (!marketIndex.eq(new BN(100))) {
1529
+ if (marketIndex !== 100) {
1580
1530
  const market = this.getPerpMarketAccount(marketIndex);
1581
1531
  marketAccountInfos.push({
1582
1532
  pubkey: market.pubkey,
@@ -1602,7 +1552,7 @@ export class ClearingHouse {
1602
1552
  }
1603
1553
 
1604
1554
  public async settleExpiredMarket(
1605
- marketIndex: BN
1555
+ marketIndex: number
1606
1556
  ): Promise<TransactionSignature> {
1607
1557
  const { txSig } = await this.txSender.send(
1608
1558
  wrapInTx(await this.getSettleExpiredMarketIx(marketIndex)),
@@ -1613,7 +1563,7 @@ export class ClearingHouse {
1613
1563
  }
1614
1564
 
1615
1565
  public async getSettleExpiredMarketIx(
1616
- marketIndex: BN
1566
+ marketIndex: number
1617
1567
  ): Promise<TransactionInstruction> {
1618
1568
  const marketAccountInfos = [];
1619
1569
  const oracleAccountInfos = [];
@@ -1759,7 +1709,7 @@ export class ClearingHouse {
1759
1709
  const spotMarketAccount = this.getSpotMarketAccount(
1760
1710
  spotPosition.marketIndex
1761
1711
  );
1762
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
1712
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
1763
1713
  pubkey: spotMarketAccount.pubkey,
1764
1714
  isSigner: false,
1765
1715
  isWritable: false,
@@ -1778,10 +1728,10 @@ export class ClearingHouse {
1778
1728
  for (const position of userAccount.perpPositions) {
1779
1729
  if (
1780
1730
  !positionIsAvailable(position) &&
1781
- !position.marketIndex.eq(order.marketIndex)
1731
+ position.marketIndex !== order.marketIndex
1782
1732
  ) {
1783
1733
  const market = this.getPerpMarketAccount(position.marketIndex);
1784
- perpMarketAccountMap.set(position.marketIndex.toNumber(), {
1734
+ perpMarketAccountMap.set(position.marketIndex, {
1785
1735
  pubkey: market.pubkey,
1786
1736
  isWritable: false,
1787
1737
  isSigner: false,
@@ -1794,7 +1744,7 @@ export class ClearingHouse {
1794
1744
  }
1795
1745
  }
1796
1746
 
1797
- perpMarketAccountMap.set(marketIndex.toNumber(), {
1747
+ perpMarketAccountMap.set(marketIndex, {
1798
1748
  pubkey: marketAccount.pubkey,
1799
1749
  isWritable: true,
1800
1750
  isSigner: false,
@@ -1939,7 +1889,7 @@ export class ClearingHouse {
1939
1889
  for (const spotPosition of userAccount.spotPositions) {
1940
1890
  if (!isSpotPositionAvailable(spotPosition)) {
1941
1891
  const spotMarket = this.getSpotMarketAccount(spotPosition.marketIndex);
1942
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
1892
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
1943
1893
  pubkey: spotMarket.pubkey,
1944
1894
  isSigner: false,
1945
1895
  isWritable: false,
@@ -1956,12 +1906,9 @@ export class ClearingHouse {
1956
1906
  }
1957
1907
 
1958
1908
  for (const position of userAccount.perpPositions) {
1959
- if (
1960
- !positionIsAvailable(position) &&
1961
- !position.marketIndex.eq(order.marketIndex)
1962
- ) {
1909
+ if (!positionIsAvailable(position)) {
1963
1910
  const market = this.getPerpMarketAccount(position.marketIndex);
1964
- perpMarketAccountMap.set(position.marketIndex.toNumber(), {
1911
+ perpMarketAccountMap.set(position.marketIndex, {
1965
1912
  pubkey: market.pubkey,
1966
1913
  isWritable: false,
1967
1914
  isSigner: false,
@@ -1975,7 +1922,7 @@ export class ClearingHouse {
1975
1922
  }
1976
1923
 
1977
1924
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
1978
- spotMarketAccountMap.set(marketIndex.toNumber(), {
1925
+ spotMarketAccountMap.set(marketIndex, {
1979
1926
  pubkey: spotMarketAccount.pubkey,
1980
1927
  isWritable: true,
1981
1928
  isSigner: false,
@@ -1988,7 +1935,7 @@ export class ClearingHouse {
1988
1935
  });
1989
1936
  }
1990
1937
  const quoteMarketAccount = this.getQuoteSpotMarketAccount();
1991
- spotMarketAccountMap.set(quoteMarketAccount.marketIndex.toNumber(), {
1938
+ spotMarketAccountMap.set(quoteMarketAccount.marketIndex, {
1992
1939
  pubkey: quoteMarketAccount.pubkey,
1993
1940
  isWritable: true,
1994
1941
  isSigner: false,
@@ -2109,6 +2056,11 @@ export class ClearingHouse {
2109
2056
  isWritable: true,
2110
2057
  isSigner: false,
2111
2058
  });
2059
+ remainingAccounts.push({
2060
+ pubkey: this.getStateAccount().srmVault,
2061
+ isWritable: false,
2062
+ isSigner: false,
2063
+ });
2112
2064
  }
2113
2065
 
2114
2066
  return await this.program.instruction.fillSpotOrder(
@@ -2161,7 +2113,7 @@ export class ClearingHouse {
2161
2113
  const spotMarketAccount = this.getSpotMarketAccount(
2162
2114
  spotPosition.marketIndex
2163
2115
  );
2164
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
2116
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
2165
2117
  pubkey: spotMarketAccount.pubkey,
2166
2118
  isSigner: false,
2167
2119
  isWritable: false,
@@ -2180,10 +2132,10 @@ export class ClearingHouse {
2180
2132
  for (const position of userAccount.perpPositions) {
2181
2133
  if (
2182
2134
  !positionIsAvailable(position) &&
2183
- !position.marketIndex.eq(order.marketIndex)
2135
+ position.marketIndex !== order.marketIndex
2184
2136
  ) {
2185
2137
  const market = this.getPerpMarketAccount(position.marketIndex);
2186
- perpMarketAccountMap.set(position.marketIndex.toNumber(), {
2138
+ perpMarketAccountMap.set(position.marketIndex, {
2187
2139
  pubkey: market.pubkey,
2188
2140
  isWritable: false,
2189
2141
  isSigner: false,
@@ -2196,7 +2148,7 @@ export class ClearingHouse {
2196
2148
  }
2197
2149
  }
2198
2150
 
2199
- perpMarketAccountMap.set(marketIndex.toNumber(), {
2151
+ perpMarketAccountMap.set(marketIndex, {
2200
2152
  pubkey: marketAccount.pubkey,
2201
2153
  isWritable: true,
2202
2154
  isSigner: false,
@@ -2259,7 +2211,7 @@ export class ClearingHouse {
2259
2211
  const spotMarketAccount = this.getSpotMarketAccount(
2260
2212
  spotPosition.marketIndex
2261
2213
  );
2262
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
2214
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
2263
2215
  pubkey: spotMarketAccount.pubkey,
2264
2216
  isSigner: false,
2265
2217
  isWritable: false,
@@ -2278,10 +2230,10 @@ export class ClearingHouse {
2278
2230
  for (const position of userAccount.perpPositions) {
2279
2231
  if (
2280
2232
  !positionIsAvailable(position) &&
2281
- !position.marketIndex.eq(order.marketIndex)
2233
+ position.marketIndex !== order.marketIndex
2282
2234
  ) {
2283
2235
  const market = this.getPerpMarketAccount(position.marketIndex);
2284
- perpMarketAccountMap.set(position.marketIndex.toNumber(), {
2236
+ perpMarketAccountMap.set(position.marketIndex, {
2285
2237
  pubkey: market.pubkey,
2286
2238
  isWritable: false,
2287
2239
  isSigner: false,
@@ -2295,12 +2247,12 @@ export class ClearingHouse {
2295
2247
  }
2296
2248
 
2297
2249
  const quoteSpotMarket = this.getQuoteSpotMarketAccount();
2298
- spotMarketAccountMap.set(quoteSpotMarket.marketIndex.toNumber(), {
2250
+ spotMarketAccountMap.set(quoteSpotMarket.marketIndex, {
2299
2251
  pubkey: quoteSpotMarket.pubkey,
2300
2252
  isWritable: true,
2301
2253
  isSigner: false,
2302
2254
  });
2303
- spotMarketAccountMap.set(marketIndex.toNumber(), {
2255
+ spotMarketAccountMap.set(marketIndex, {
2304
2256
  pubkey: spotMarketAccount.pubkey,
2305
2257
  isWritable: false,
2306
2258
  isSigner: false,
@@ -2341,7 +2293,7 @@ export class ClearingHouse {
2341
2293
  [],
2342
2294
  this.opts
2343
2295
  );
2344
- this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
2296
+ this.marketLastSlotCache.set(orderParams.marketIndex, slot);
2345
2297
  return txSig;
2346
2298
  }
2347
2299
 
@@ -2415,7 +2367,7 @@ export class ClearingHouse {
2415
2367
  this.opts
2416
2368
  );
2417
2369
 
2418
- this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
2370
+ this.marketLastSlotCache.set(orderParams.marketIndex, slot);
2419
2371
 
2420
2372
  return txSig;
2421
2373
  }
@@ -2472,7 +2424,7 @@ export class ClearingHouse {
2472
2424
  * @returns
2473
2425
  */
2474
2426
  public async closePosition(
2475
- marketIndex: BN,
2427
+ marketIndex: number,
2476
2428
  limitPrice?: BN
2477
2429
  ): Promise<TransactionSignature> {
2478
2430
  const userPosition = this.getUser().getUserPosition(marketIndex);
@@ -2495,7 +2447,7 @@ export class ClearingHouse {
2495
2447
  settleeUserAccountPublicKey: PublicKey;
2496
2448
  settleeUserAccount: UserAccount;
2497
2449
  }[],
2498
- marketIndex: BN
2450
+ marketIndex: number
2499
2451
  ): Promise<TransactionSignature> {
2500
2452
  const ixs = [];
2501
2453
  for (const { settleeUserAccountPublicKey, settleeUserAccount } of users) {
@@ -2524,7 +2476,7 @@ export class ClearingHouse {
2524
2476
  public async settlePNL(
2525
2477
  settleeUserAccountPublicKey: PublicKey,
2526
2478
  settleeUserAccount: UserAccount,
2527
- marketIndex: BN
2479
+ marketIndex: number
2528
2480
  ): Promise<TransactionSignature> {
2529
2481
  const { txSig } = await this.txSender.send(
2530
2482
  wrapInTx(
@@ -2543,7 +2495,7 @@ export class ClearingHouse {
2543
2495
  public async settlePNLIx(
2544
2496
  settleeUserAccountPublicKey: PublicKey,
2545
2497
  settleeUserAccount: UserAccount,
2546
- marketIndex: BN
2498
+ marketIndex: number
2547
2499
  ): Promise<TransactionInstruction> {
2548
2500
  const perpMarketAccountMap = new Map<number, AccountMeta>();
2549
2501
  const oracleAccountMap = new Map<string, AccountMeta>();
@@ -2552,7 +2504,7 @@ export class ClearingHouse {
2552
2504
  for (const position of settleeUserAccount.perpPositions) {
2553
2505
  if (!positionIsAvailable(position)) {
2554
2506
  const market = this.getPerpMarketAccount(position.marketIndex);
2555
- perpMarketAccountMap.set(position.marketIndex.toNumber(), {
2507
+ perpMarketAccountMap.set(position.marketIndex, {
2556
2508
  pubkey: market.pubkey,
2557
2509
  isWritable: false,
2558
2510
  isSigner: false,
@@ -2570,12 +2522,12 @@ export class ClearingHouse {
2570
2522
  const spotMarketAccount = this.getSpotMarketAccount(
2571
2523
  spotPosition.marketIndex
2572
2524
  );
2573
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
2525
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
2574
2526
  pubkey: spotMarketAccount.pubkey,
2575
2527
  isSigner: false,
2576
2528
  isWritable: false,
2577
2529
  });
2578
- if (!spotMarketAccount.marketIndex.eq(ZERO)) {
2530
+ if (spotMarketAccount.marketIndex !== 0) {
2579
2531
  oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
2580
2532
  pubkey: spotMarketAccount.oracle,
2581
2533
  isSigner: false,
@@ -2585,8 +2537,8 @@ export class ClearingHouse {
2585
2537
  }
2586
2538
  }
2587
2539
 
2588
- const marketAccount = this.getPerpMarketAccount(marketIndex.toNumber());
2589
- perpMarketAccountMap.set(marketIndex.toNumber(), {
2540
+ const marketAccount = this.getPerpMarketAccount(marketIndex);
2541
+ perpMarketAccountMap.set(marketIndex, {
2590
2542
  pubkey: marketAccount.pubkey,
2591
2543
  isSigner: false,
2592
2544
  isWritable: true,
@@ -2597,7 +2549,7 @@ export class ClearingHouse {
2597
2549
  isWritable: false,
2598
2550
  });
2599
2551
 
2600
- spotMarketAccountMap.set(QUOTE_SPOT_MARKET_INDEX.toNumber(), {
2552
+ spotMarketAccountMap.set(QUOTE_SPOT_MARKET_INDEX, {
2601
2553
  pubkey: this.getSpotMarketAccount(QUOTE_SPOT_MARKET_INDEX).pubkey,
2602
2554
  isSigner: false,
2603
2555
  isWritable: true,
@@ -2622,7 +2574,7 @@ export class ClearingHouse {
2622
2574
  public async settleExpiredPosition(
2623
2575
  settleeUserAccountPublicKey: PublicKey,
2624
2576
  settleeUserAccount: UserAccount,
2625
- marketIndex: BN
2577
+ marketIndex: number
2626
2578
  ): Promise<TransactionSignature> {
2627
2579
  const { txSig } = await this.txSender.send(
2628
2580
  wrapInTx(
@@ -2641,7 +2593,7 @@ export class ClearingHouse {
2641
2593
  public async getSettleExpiredPositionIx(
2642
2594
  settleeUserAccountPublicKey: PublicKey,
2643
2595
  settleeUserAccount: UserAccount,
2644
- marketIndex: BN
2596
+ marketIndex: number
2645
2597
  ): Promise<TransactionInstruction> {
2646
2598
  const marketAccountMap = new Map<number, AccountMeta>();
2647
2599
  const oracleAccountMap = new Map<string, AccountMeta>();
@@ -2649,7 +2601,7 @@ export class ClearingHouse {
2649
2601
  for (const position of settleeUserAccount.perpPositions) {
2650
2602
  if (!positionIsAvailable(position)) {
2651
2603
  const market = this.getPerpMarketAccount(position.marketIndex);
2652
- marketAccountMap.set(position.marketIndex.toNumber(), {
2604
+ marketAccountMap.set(position.marketIndex, {
2653
2605
  pubkey: market.pubkey,
2654
2606
  isWritable: false,
2655
2607
  isSigner: false,
@@ -2667,12 +2619,12 @@ export class ClearingHouse {
2667
2619
  const bankAccount = this.getSpotMarketAccount(
2668
2620
  userBankBalance.marketIndex
2669
2621
  );
2670
- spotMarketAccountMap.set(userBankBalance.marketIndex.toNumber(), {
2622
+ spotMarketAccountMap.set(userBankBalance.marketIndex, {
2671
2623
  pubkey: bankAccount.pubkey,
2672
2624
  isSigner: false,
2673
2625
  isWritable: false,
2674
2626
  });
2675
- if (!bankAccount.marketIndex.eq(ZERO)) {
2627
+ if (bankAccount.marketIndex !== 0) {
2676
2628
  oracleAccountMap.set(bankAccount.oracle.toString(), {
2677
2629
  pubkey: bankAccount.oracle,
2678
2630
  isSigner: false,
@@ -2682,8 +2634,8 @@ export class ClearingHouse {
2682
2634
  }
2683
2635
  }
2684
2636
 
2685
- const marketAccount = this.getPerpMarketAccount(marketIndex.toNumber());
2686
- marketAccountMap.set(marketIndex.toNumber(), {
2637
+ const marketAccount = this.getPerpMarketAccount(marketIndex);
2638
+ marketAccountMap.set(marketIndex, {
2687
2639
  pubkey: marketAccount.pubkey,
2688
2640
  isSigner: false,
2689
2641
  isWritable: true,
@@ -2694,7 +2646,7 @@ export class ClearingHouse {
2694
2646
  isWritable: false,
2695
2647
  });
2696
2648
 
2697
- spotMarketAccountMap.set(QUOTE_SPOT_MARKET_INDEX.toNumber(), {
2649
+ spotMarketAccountMap.set(QUOTE_SPOT_MARKET_INDEX, {
2698
2650
  pubkey: this.getSpotMarketAccount(QUOTE_SPOT_MARKET_INDEX).pubkey,
2699
2651
  isSigner: false,
2700
2652
  isWritable: true,
@@ -2719,7 +2671,7 @@ export class ClearingHouse {
2719
2671
  public async liquidatePerp(
2720
2672
  userAccountPublicKey: PublicKey,
2721
2673
  userAccount: UserAccount,
2722
- marketIndex: BN,
2674
+ marketIndex: number,
2723
2675
  maxBaseAssetAmount: BN
2724
2676
  ): Promise<TransactionSignature> {
2725
2677
  const { txSig, slot } = await this.txSender.send(
@@ -2734,14 +2686,14 @@ export class ClearingHouse {
2734
2686
  [],
2735
2687
  this.opts
2736
2688
  );
2737
- this.marketLastSlotCache.set(marketIndex.toNumber(), slot);
2689
+ this.marketLastSlotCache.set(marketIndex, slot);
2738
2690
  return txSig;
2739
2691
  }
2740
2692
 
2741
2693
  public async getLiquidatePerpIx(
2742
2694
  userAccountPublicKey: PublicKey,
2743
2695
  userAccount: UserAccount,
2744
- marketIndex: BN,
2696
+ marketIndex: number,
2745
2697
  maxBaseAssetAmount: BN
2746
2698
  ): Promise<TransactionInstruction> {
2747
2699
  const userStatsPublicKey = getUserStatsAccountPublicKey(
@@ -2777,8 +2729,8 @@ export class ClearingHouse {
2777
2729
  public async liquidateBorrow(
2778
2730
  userAccountPublicKey: PublicKey,
2779
2731
  userAccount: UserAccount,
2780
- assetmarketIndex: BN,
2781
- liabilitymarketIndex: BN,
2732
+ assetMarketIndex: number,
2733
+ liabilityMarketIndex: number,
2782
2734
  maxLiabilityTransfer: BN
2783
2735
  ): Promise<TransactionSignature> {
2784
2736
  const { txSig } = await this.txSender.send(
@@ -2786,8 +2738,8 @@ export class ClearingHouse {
2786
2738
  await this.getLiquidateBorrowIx(
2787
2739
  userAccountPublicKey,
2788
2740
  userAccount,
2789
- assetmarketIndex,
2790
- liabilitymarketIndex,
2741
+ assetMarketIndex,
2742
+ liabilityMarketIndex,
2791
2743
  maxLiabilityTransfer
2792
2744
  )
2793
2745
  ),
@@ -2800,20 +2752,20 @@ export class ClearingHouse {
2800
2752
  public async getLiquidateBorrowIx(
2801
2753
  userAccountPublicKey: PublicKey,
2802
2754
  userAccount: UserAccount,
2803
- assetmarketIndex: BN,
2804
- liabilitymarketIndex: BN,
2755
+ assetMarketIndex: number,
2756
+ liabilityMarketIndex: number,
2805
2757
  maxLiabilityTransfer: BN
2806
2758
  ): Promise<TransactionInstruction> {
2807
2759
  const liquidatorPublicKey = await this.getUserAccountPublicKey();
2808
2760
 
2809
2761
  const remainingAccounts = this.getRemainingAccountsWithCounterparty({
2810
2762
  counterPartyUserAccount: userAccount,
2811
- writableSpotMarketIndexes: [liabilitymarketIndex, assetmarketIndex],
2763
+ writableSpotMarketIndexes: [liabilityMarketIndex, assetMarketIndex],
2812
2764
  });
2813
2765
 
2814
2766
  return await this.program.instruction.liquidateBorrow(
2815
- assetmarketIndex,
2816
- liabilitymarketIndex,
2767
+ assetMarketIndex,
2768
+ liabilityMarketIndex,
2817
2769
  maxLiabilityTransfer,
2818
2770
  {
2819
2771
  accounts: {
@@ -2830,8 +2782,8 @@ export class ClearingHouse {
2830
2782
  public async liquidateBorrowForPerpPnl(
2831
2783
  userAccountPublicKey: PublicKey,
2832
2784
  userAccount: UserAccount,
2833
- perpMarketIndex: BN,
2834
- liabilitymarketIndex: BN,
2785
+ perpMarketIndex: number,
2786
+ liabilityMarketIndex: number,
2835
2787
  maxLiabilityTransfer: BN
2836
2788
  ): Promise<TransactionSignature> {
2837
2789
  const { txSig, slot } = await this.txSender.send(
@@ -2840,22 +2792,22 @@ export class ClearingHouse {
2840
2792
  userAccountPublicKey,
2841
2793
  userAccount,
2842
2794
  perpMarketIndex,
2843
- liabilitymarketIndex,
2795
+ liabilityMarketIndex,
2844
2796
  maxLiabilityTransfer
2845
2797
  )
2846
2798
  ),
2847
2799
  [],
2848
2800
  this.opts
2849
2801
  );
2850
- this.marketLastSlotCache.set(perpMarketIndex.toNumber(), slot);
2802
+ this.marketLastSlotCache.set(perpMarketIndex, slot);
2851
2803
  return txSig;
2852
2804
  }
2853
2805
 
2854
2806
  public async getLiquidateBorrowForPerpPnlIx(
2855
2807
  userAccountPublicKey: PublicKey,
2856
2808
  userAccount: UserAccount,
2857
- perpMarketIndex: BN,
2858
- liabilitymarketIndex: BN,
2809
+ perpMarketIndex: number,
2810
+ liabilityMarketIndex: number,
2859
2811
  maxLiabilityTransfer: BN
2860
2812
  ): Promise<TransactionInstruction> {
2861
2813
  const liquidatorPublicKey = await this.getUserAccountPublicKey();
@@ -2863,12 +2815,12 @@ export class ClearingHouse {
2863
2815
  const remainingAccounts = this.getRemainingAccountsWithCounterparty({
2864
2816
  counterPartyUserAccount: userAccount,
2865
2817
  writablePerpMarketIndex: perpMarketIndex,
2866
- writableSpotMarketIndexes: [liabilitymarketIndex],
2818
+ writableSpotMarketIndexes: [liabilityMarketIndex],
2867
2819
  });
2868
2820
 
2869
2821
  return await this.program.instruction.liquidateBorrowForPerpPnl(
2870
2822
  perpMarketIndex,
2871
- liabilitymarketIndex,
2823
+ liabilityMarketIndex,
2872
2824
  maxLiabilityTransfer,
2873
2825
  {
2874
2826
  accounts: {
@@ -2885,8 +2837,8 @@ export class ClearingHouse {
2885
2837
  public async liquidatePerpPnlForDeposit(
2886
2838
  userAccountPublicKey: PublicKey,
2887
2839
  userAccount: UserAccount,
2888
- perpMarketIndex: BN,
2889
- assetMarketIndex: BN,
2840
+ perpMarketIndex: number,
2841
+ assetMarketIndex: number,
2890
2842
  maxPnlTransfer: BN
2891
2843
  ): Promise<TransactionSignature> {
2892
2844
  const { txSig, slot } = await this.txSender.send(
@@ -2902,15 +2854,15 @@ export class ClearingHouse {
2902
2854
  [],
2903
2855
  this.opts
2904
2856
  );
2905
- this.marketLastSlotCache.set(perpMarketIndex.toNumber(), slot);
2857
+ this.marketLastSlotCache.set(perpMarketIndex, slot);
2906
2858
  return txSig;
2907
2859
  }
2908
2860
 
2909
2861
  public async getLiquidatePerpPnlForDepositIx(
2910
2862
  userAccountPublicKey: PublicKey,
2911
2863
  userAccount: UserAccount,
2912
- perpMarketIndex: BN,
2913
- assetMarketIndex: BN,
2864
+ perpMarketIndex: number,
2865
+ assetMarketIndex: number,
2914
2866
  maxPnlTransfer: BN
2915
2867
  ): Promise<TransactionInstruction> {
2916
2868
  const liquidatorPublicKey = await this.getUserAccountPublicKey();
@@ -2940,7 +2892,7 @@ export class ClearingHouse {
2940
2892
  public async resolvePerpBankruptcy(
2941
2893
  userAccountPublicKey: PublicKey,
2942
2894
  userAccount: UserAccount,
2943
- marketIndex: BN
2895
+ marketIndex: number
2944
2896
  ): Promise<TransactionSignature> {
2945
2897
  const { txSig } = await this.txSender.send(
2946
2898
  wrapInTx(
@@ -2959,7 +2911,7 @@ export class ClearingHouse {
2959
2911
  public async getResolvePerpBankruptcyIx(
2960
2912
  userAccountPublicKey: PublicKey,
2961
2913
  userAccount: UserAccount,
2962
- marketIndex: BN
2914
+ marketIndex: number
2963
2915
  ): Promise<TransactionInstruction> {
2964
2916
  const liquidatorPublicKey = await this.getUserAccountPublicKey();
2965
2917
 
@@ -2993,7 +2945,7 @@ export class ClearingHouse {
2993
2945
  public async resolveBorrowBankruptcy(
2994
2946
  userAccountPublicKey: PublicKey,
2995
2947
  userAccount: UserAccount,
2996
- marketIndex: BN
2948
+ marketIndex: number
2997
2949
  ): Promise<TransactionSignature> {
2998
2950
  const { txSig } = await this.txSender.send(
2999
2951
  wrapInTx(
@@ -3012,7 +2964,7 @@ export class ClearingHouse {
3012
2964
  public async getResolveBorrowBankruptcyIx(
3013
2965
  userAccountPublicKey: PublicKey,
3014
2966
  userAccount: UserAccount,
3015
- marketIndex: BN
2967
+ marketIndex: number
3016
2968
  ): Promise<TransactionInstruction> {
3017
2969
  const liquidatorPublicKey = await this.getUserAccountPublicKey();
3018
2970
 
@@ -3040,8 +2992,8 @@ export class ClearingHouse {
3040
2992
 
3041
2993
  getRemainingAccountsWithCounterparty(params: {
3042
2994
  counterPartyUserAccount: UserAccount;
3043
- writablePerpMarketIndex?: BN;
3044
- writableSpotMarketIndexes?: BN[];
2995
+ writablePerpMarketIndex?: number;
2996
+ writableSpotMarketIndexes?: number[];
3045
2997
  }): AccountMeta[] {
3046
2998
  const counterPartyUserAccount = params.counterPartyUserAccount;
3047
2999
 
@@ -3051,7 +3003,7 @@ export class ClearingHouse {
3051
3003
  for (const spotPosition of counterPartyUserAccount.spotPositions) {
3052
3004
  if (!isSpotPositionAvailable(spotPosition)) {
3053
3005
  const spotMarket = this.getSpotMarketAccount(spotPosition.marketIndex);
3054
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
3006
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
3055
3007
  pubkey: spotMarket.pubkey,
3056
3008
  isSigner: false,
3057
3009
  isWritable: false,
@@ -3069,7 +3021,7 @@ export class ClearingHouse {
3069
3021
  for (const position of counterPartyUserAccount.perpPositions) {
3070
3022
  if (!positionIsAvailable(position)) {
3071
3023
  const market = this.getPerpMarketAccount(position.marketIndex);
3072
- marketAccountMap.set(position.marketIndex.toNumber(), {
3024
+ marketAccountMap.set(position.marketIndex, {
3073
3025
  pubkey: market.pubkey,
3074
3026
  isWritable: false,
3075
3027
  isSigner: false,
@@ -3113,7 +3065,7 @@ export class ClearingHouse {
3113
3065
  for (const spotPosition of userAccount.spotPositions) {
3114
3066
  if (!isSpotPositionAvailable(spotPosition)) {
3115
3067
  const spotMarket = this.getSpotMarketAccount(spotPosition.marketIndex);
3116
- spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
3068
+ spotMarketAccountMap.set(spotPosition.marketIndex, {
3117
3069
  pubkey: spotMarket.pubkey,
3118
3070
  isSigner: false,
3119
3071
  isWritable: false,
@@ -3131,7 +3083,7 @@ export class ClearingHouse {
3131
3083
  for (const position of userAccount.perpPositions) {
3132
3084
  if (!positionIsAvailable(position)) {
3133
3085
  const market = this.getPerpMarketAccount(position.marketIndex);
3134
- marketAccountMap.set(position.marketIndex.toNumber(), {
3086
+ marketAccountMap.set(position.marketIndex, {
3135
3087
  pubkey: market.pubkey,
3136
3088
  isWritable: false,
3137
3089
  isSigner: false,
@@ -3144,9 +3096,9 @@ export class ClearingHouse {
3144
3096
  }
3145
3097
  }
3146
3098
 
3147
- if (params.writablePerpMarketIndex) {
3099
+ if (params.writablePerpMarketIndex !== undefined) {
3148
3100
  const market = this.getPerpMarketAccount(params.writablePerpMarketIndex);
3149
- marketAccountMap.set(market.marketIndex.toNumber(), {
3101
+ marketAccountMap.set(market.marketIndex, {
3150
3102
  pubkey: market.pubkey,
3151
3103
  isSigner: false,
3152
3104
  isWritable: true,
@@ -3158,12 +3110,12 @@ export class ClearingHouse {
3158
3110
  });
3159
3111
  }
3160
3112
 
3161
- if (params.writableSpotMarketIndexes) {
3113
+ if (params.writableSpotMarketIndexes !== undefined) {
3162
3114
  for (const writableSpotMarketIndex of params.writableSpotMarketIndexes) {
3163
3115
  const spotMarketAccount = this.getSpotMarketAccount(
3164
3116
  writableSpotMarketIndex
3165
3117
  );
3166
- spotMarketAccountMap.set(spotMarketAccount.marketIndex.toNumber(), {
3118
+ spotMarketAccountMap.set(spotMarketAccount.marketIndex, {
3167
3119
  pubkey: spotMarketAccount.pubkey,
3168
3120
  isSigner: false,
3169
3121
  isWritable: true,
@@ -3187,7 +3139,7 @@ export class ClearingHouse {
3187
3139
 
3188
3140
  public async updateFundingRate(
3189
3141
  oracle: PublicKey,
3190
- marketIndex: BN
3142
+ marketIndex: number
3191
3143
  ): Promise<TransactionSignature> {
3192
3144
  const { txSig } = await this.txSender.send(
3193
3145
  wrapInTx(await this.getUpdateFundingRateIx(oracle, marketIndex)),
@@ -3199,7 +3151,7 @@ export class ClearingHouse {
3199
3151
 
3200
3152
  public async getUpdateFundingRateIx(
3201
3153
  oracle: PublicKey,
3202
- marketIndex: BN
3154
+ marketIndex: number
3203
3155
  ): Promise<TransactionInstruction> {
3204
3156
  return await this.program.instruction.updateFundingRate(marketIndex, {
3205
3157
  accounts: {
@@ -3258,14 +3210,14 @@ export class ClearingHouse {
3258
3210
  this.eventEmitter.emit(eventName, data);
3259
3211
  }
3260
3212
 
3261
- public getOracleDataForMarket(marketIndex: BN): OraclePriceData {
3213
+ public getOracleDataForMarket(marketIndex: number): OraclePriceData {
3262
3214
  const oracleKey = this.getPerpMarketAccount(marketIndex).amm.oracle;
3263
3215
  const oracleData = this.getOraclePriceDataAndSlot(oracleKey).data;
3264
3216
 
3265
3217
  return oracleData;
3266
3218
  }
3267
3219
 
3268
- public getOracleDataForSpotMarket(marketIndex: BN): OraclePriceData {
3220
+ public getOracleDataForSpotMarket(marketIndex: number): OraclePriceData {
3269
3221
  const oracleKey = this.getSpotMarketAccount(marketIndex).oracle;
3270
3222
  const oracleData = this.getOraclePriceDataAndSlot(oracleKey).data;
3271
3223
 
@@ -3273,7 +3225,7 @@ export class ClearingHouse {
3273
3225
  }
3274
3226
 
3275
3227
  public async initializeInsuranceFundStake(
3276
- marketIndex: BN
3228
+ marketIndex: number
3277
3229
  ): Promise<TransactionSignature> {
3278
3230
  const { txSig } = await this.txSender.send(
3279
3231
  wrapInTx(await this.getInitializeInsuranceFundStakeIx(marketIndex)),
@@ -3284,7 +3236,7 @@ export class ClearingHouse {
3284
3236
  }
3285
3237
 
3286
3238
  public async getInitializeInsuranceFundStakeIx(
3287
- marketIndex: BN
3239
+ marketIndex: number
3288
3240
  ): Promise<TransactionInstruction> {
3289
3241
  const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
3290
3242
  this.program.programId,
@@ -3310,7 +3262,7 @@ export class ClearingHouse {
3310
3262
  }
3311
3263
 
3312
3264
  public async addInsuranceFundStake(
3313
- marketIndex: BN,
3265
+ marketIndex: number,
3314
3266
  amount: BN,
3315
3267
  collateralAccountPublicKey: PublicKey
3316
3268
  ): Promise<TransactionSignature> {
@@ -3341,7 +3293,7 @@ export class ClearingHouse {
3341
3293
  }
3342
3294
 
3343
3295
  public async requestRemoveInsuranceFundStake(
3344
- marketIndex: BN,
3296
+ marketIndex: number,
3345
3297
  amount: BN
3346
3298
  ): Promise<TransactionSignature> {
3347
3299
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
@@ -3375,7 +3327,7 @@ export class ClearingHouse {
3375
3327
  }
3376
3328
 
3377
3329
  public async cancelRequestRemoveInsuranceFundStake(
3378
- marketIndex: BN
3330
+ marketIndex: number
3379
3331
  ): Promise<TransactionSignature> {
3380
3332
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
3381
3333
  const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
@@ -3407,7 +3359,7 @@ export class ClearingHouse {
3407
3359
  }
3408
3360
 
3409
3361
  public async removeInsuranceFundStake(
3410
- marketIndex: BN,
3362
+ marketIndex: number,
3411
3363
  collateralAccountPublicKey: PublicKey
3412
3364
  ): Promise<TransactionSignature> {
3413
3365
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
@@ -3438,7 +3390,7 @@ export class ClearingHouse {
3438
3390
  }
3439
3391
 
3440
3392
  public async settleRevenueToInsuranceFund(
3441
- marketIndex: BN
3393
+ marketIndex: number
3442
3394
  ): Promise<TransactionSignature> {
3443
3395
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
3444
3396
 
@@ -3460,8 +3412,8 @@ export class ClearingHouse {
3460
3412
  }
3461
3413
 
3462
3414
  public async resolvePerpPnlDeficit(
3463
- spotMarketIndex: BN,
3464
- perpMarketIndex: BN
3415
+ spotMarketIndex: number,
3416
+ perpMarketIndex: number
3465
3417
  ): Promise<TransactionSignature> {
3466
3418
  const { txSig } = await this.txSender.send(
3467
3419
  wrapInTx(
@@ -3474,8 +3426,8 @@ export class ClearingHouse {
3474
3426
  }
3475
3427
 
3476
3428
  public async getResolvePerpPnlDeficitIx(
3477
- spotMarketIndex: BN,
3478
- perpMarketIndex: BN
3429
+ spotMarketIndex: number,
3430
+ perpMarketIndex: number
3479
3431
  ): Promise<TransactionInstruction> {
3480
3432
  const remainingAccounts = this.getRemainingAccounts({
3481
3433
  writablePerpMarketIndex: perpMarketIndex,