@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
@@ -72,13 +72,13 @@ export function getUserStatsAccountPublicKey(
72
72
 
73
73
  export async function getMarketPublicKey(
74
74
  programId: PublicKey,
75
- marketIndex: BN
75
+ marketIndex: number
76
76
  ): Promise<PublicKey> {
77
77
  return (
78
78
  await anchor.web3.PublicKey.findProgramAddress(
79
79
  [
80
80
  Buffer.from(anchor.utils.bytes.utf8.encode('market')),
81
- marketIndex.toArrayLike(Buffer, 'le', 8),
81
+ new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
82
82
  ],
83
83
  programId
84
84
  )
@@ -87,13 +87,13 @@ export async function getMarketPublicKey(
87
87
 
88
88
  export async function getSpotMarketPublicKey(
89
89
  programId: PublicKey,
90
- marketIndex: BN
90
+ marketIndex: number
91
91
  ): Promise<PublicKey> {
92
92
  return (
93
93
  await anchor.web3.PublicKey.findProgramAddress(
94
94
  [
95
95
  Buffer.from(anchor.utils.bytes.utf8.encode('spot_market')),
96
- marketIndex.toArrayLike(Buffer, 'le', 8),
96
+ new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
97
97
  ],
98
98
  programId
99
99
  )
@@ -102,13 +102,13 @@ export async function getSpotMarketPublicKey(
102
102
 
103
103
  export async function getSpotMarketVaultPublicKey(
104
104
  programId: PublicKey,
105
- marketIndex: BN
105
+ marketIndex: number
106
106
  ): Promise<PublicKey> {
107
107
  return (
108
108
  await anchor.web3.PublicKey.findProgramAddress(
109
109
  [
110
110
  Buffer.from(anchor.utils.bytes.utf8.encode('spot_market_vault')),
111
- marketIndex.toArrayLike(Buffer, 'le', 8),
111
+ new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
112
112
  ],
113
113
  programId
114
114
  )
@@ -117,13 +117,13 @@ export async function getSpotMarketVaultPublicKey(
117
117
 
118
118
  export async function getInsuranceFundVaultPublicKey(
119
119
  programId: PublicKey,
120
- marketIndex: BN
120
+ marketIndex: number
121
121
  ): Promise<PublicKey> {
122
122
  return (
123
123
  await anchor.web3.PublicKey.findProgramAddress(
124
124
  [
125
125
  Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_vault')),
126
- marketIndex.toArrayLike(Buffer, 'le', 8),
126
+ new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
127
127
  ],
128
128
  programId
129
129
  )
@@ -133,13 +133,13 @@ export async function getInsuranceFundVaultPublicKey(
133
133
  export function getInsuranceFundStakeAccountPublicKey(
134
134
  programId: PublicKey,
135
135
  authority: PublicKey,
136
- marketIndex: BN
136
+ marketIndex: number
137
137
  ): PublicKey {
138
138
  return anchor.web3.PublicKey.findProgramAddressSync(
139
139
  [
140
140
  Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_stake')),
141
141
  authority.toBuffer(),
142
- marketIndex.toArrayLike(Buffer, 'le', 8),
142
+ new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
143
143
  ],
144
144
  programId
145
145
  )[0];
package/src/admin.ts CHANGED
@@ -39,11 +39,6 @@ export class Admin extends ClearingHouse {
39
39
  this.program.programId
40
40
  );
41
41
 
42
- const [insuranceVaultPublicKey] = await PublicKey.findProgramAddress(
43
- [Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))],
44
- this.program.programId
45
- );
46
-
47
42
  const initializeTx = await this.program.transaction.initialize(
48
43
  adminControlsPrices,
49
44
  {
@@ -52,7 +47,6 @@ export class Admin extends ClearingHouse {
52
47
  state: clearingHouseStatePublicKey,
53
48
  quoteAssetMint: usdcMint,
54
49
  rent: SYSVAR_RENT_PUBKEY,
55
- insuranceVault: insuranceVaultPublicKey,
56
50
  clearingHouseSigner: this.getSignerPublicKey(),
57
51
  systemProgram: anchor.web3.SystemProgram.programId,
58
52
  tokenProgram: TOKEN_PROGRAM_ID,
@@ -139,7 +133,7 @@ export class Admin extends ClearingHouse {
139
133
  }
140
134
 
141
135
  public async initializeSerumFulfillmentConfig(
142
- marketIndex: BN,
136
+ marketIndex: number,
143
137
  serumMarket: PublicKey,
144
138
  serumProgram: PublicKey
145
139
  ): Promise<TransactionSignature> {
@@ -227,7 +221,7 @@ export class Admin extends ClearingHouse {
227
221
  }
228
222
 
229
223
  public async moveAmmPrice(
230
- marketIndex: BN,
224
+ marketIndex: number,
231
225
  baseAssetReserve: BN,
232
226
  quoteAssetReserve: BN,
233
227
  sqrtK?: BN
@@ -257,7 +251,7 @@ export class Admin extends ClearingHouse {
257
251
 
258
252
  public async updateK(
259
253
  sqrtK: BN,
260
- marketIndex: BN
254
+ marketIndex: number
261
255
  ): Promise<TransactionSignature> {
262
256
  return await this.program.rpc.updateK(sqrtK, {
263
257
  accounts: {
@@ -270,7 +264,7 @@ export class Admin extends ClearingHouse {
270
264
  }
271
265
 
272
266
  public async updateConcentrationScale(
273
- marketIndex: BN,
267
+ marketIndex: number,
274
268
  concentrationScale: BN
275
269
  ): Promise<TransactionSignature> {
276
270
  return await this.program.rpc.updateConcentrationCoef(concentrationScale, {
@@ -283,7 +277,7 @@ export class Admin extends ClearingHouse {
283
277
  }
284
278
 
285
279
  public async moveAmmToPrice(
286
- perpMarketIndex: BN,
280
+ perpMarketIndex: number,
287
281
  targetPrice: BN
288
282
  ): Promise<TransactionSignature> {
289
283
  const market = this.getPerpMarketAccount(perpMarketIndex);
@@ -325,7 +319,7 @@ export class Admin extends ClearingHouse {
325
319
 
326
320
  public async repegAmmCurve(
327
321
  newPeg: BN,
328
- marketIndex: BN
322
+ marketIndex: number
329
323
  ): Promise<TransactionSignature> {
330
324
  const marketPublicKey = await getMarketPublicKey(
331
325
  this.program.programId,
@@ -344,7 +338,7 @@ export class Admin extends ClearingHouse {
344
338
  }
345
339
 
346
340
  public async updateAmmOracleTwap(
347
- marketIndex: BN
341
+ marketIndex: number
348
342
  ): Promise<TransactionSignature> {
349
343
  const ammData = this.getPerpMarketAccount(marketIndex).amm;
350
344
  const marketPublicKey = await getMarketPublicKey(
@@ -363,7 +357,7 @@ export class Admin extends ClearingHouse {
363
357
  }
364
358
 
365
359
  public async resetAmmOracleTwap(
366
- marketIndex: BN
360
+ marketIndex: number
367
361
  ): Promise<TransactionSignature> {
368
362
  const ammData = this.getPerpMarketAccount(marketIndex).amm;
369
363
  const marketPublicKey = await getMarketPublicKey(
@@ -381,62 +375,19 @@ export class Admin extends ClearingHouse {
381
375
  });
382
376
  }
383
377
 
384
- public async withdrawFromInsuranceVault(
378
+ public async depositIntoMarketFeePool(
379
+ marketIndex: number,
385
380
  amount: BN,
386
- recipient: PublicKey
381
+ sourceVault: PublicKey
387
382
  ): Promise<TransactionSignature> {
388
- const state = await this.getStateAccount();
389
383
  const spotMarket = this.getQuoteSpotMarketAccount();
390
- return await this.program.rpc.withdrawFromInsuranceVault(amount, {
391
- accounts: {
392
- admin: this.wallet.publicKey,
393
- state: await this.getStatePublicKey(),
394
- spotMarket: spotMarket.pubkey,
395
- insuranceVault: state.insuranceVault,
396
- clearingHouseSigner: this.getSignerPublicKey(),
397
- recipient: recipient,
398
- tokenProgram: TOKEN_PROGRAM_ID,
399
- },
400
- });
401
- }
402
384
 
403
- public async withdrawFromMarketToInsuranceVault(
404
- marketIndex: BN,
405
- amount: BN,
406
- recipient: PublicKey
407
- ): Promise<TransactionSignature> {
408
- const marketPublicKey = await getMarketPublicKey(
409
- this.program.programId,
410
- marketIndex
411
- );
412
- const spotMarket = this.getQuoteSpotMarketAccount();
413
- return await this.program.rpc.withdrawFromMarketToInsuranceVault(amount, {
414
- accounts: {
415
- admin: this.wallet.publicKey,
416
- state: await this.getStatePublicKey(),
417
- market: marketPublicKey,
418
- spotMarket: spotMarket.pubkey,
419
- spotMarketVault: spotMarket.vault,
420
- clearingHouseSigner: this.getSignerPublicKey(),
421
- recipient: recipient,
422
- tokenProgram: TOKEN_PROGRAM_ID,
423
- },
424
- });
425
- }
426
-
427
- public async withdrawFromInsuranceVaultToMarket(
428
- marketIndex: BN,
429
- amount: BN
430
- ): Promise<TransactionSignature> {
431
- const state = await this.getStateAccount();
432
- const spotMarket = this.getQuoteSpotMarketAccount();
433
-
434
- return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
385
+ return await this.program.rpc.depositIntoMarketFeePool(amount, {
435
386
  accounts: {
436
387
  admin: this.wallet.publicKey,
437
388
  state: await this.getStatePublicKey(),
438
389
  market: await getMarketPublicKey(this.program.programId, marketIndex),
439
- insuranceVault: state.insuranceVault,
390
+ sourceVault,
440
391
  clearingHouseSigner: this.getSignerPublicKey(),
441
392
  quoteSpotMarket: spotMarket.pubkey,
442
393
  spotMarketVault: spotMarket.vault,
@@ -455,7 +406,7 @@ export class Admin extends ClearingHouse {
455
406
  }
456
407
 
457
408
  public async updateCurveUpdateIntensity(
458
- marketIndex: BN,
409
+ marketIndex: number,
459
410
  curveUpdateIntensity: number
460
411
  ): Promise<TransactionSignature> {
461
412
  // assert(curveUpdateIntensity >= 0 && curveUpdateIntensity <= 100);
@@ -474,7 +425,7 @@ export class Admin extends ClearingHouse {
474
425
  }
475
426
 
476
427
  public async updateMarginRatio(
477
- marketIndex: BN,
428
+ marketIndex: number,
478
429
  marginRatioInitial: number,
479
430
  marginRatioMaintenance: number
480
431
  ): Promise<TransactionSignature> {
@@ -492,7 +443,7 @@ export class Admin extends ClearingHouse {
492
443
  }
493
444
 
494
445
  public async updateMarketBaseSpread(
495
- marketIndex: BN,
446
+ marketIndex: number,
496
447
  baseSpread: number
497
448
  ): Promise<TransactionSignature> {
498
449
  return await this.program.rpc.updateMarketBaseSpread(baseSpread, {
@@ -505,7 +456,7 @@ export class Admin extends ClearingHouse {
505
456
  }
506
457
 
507
458
  public async updateAmmJitIntensity(
508
- marketIndex: BN,
459
+ marketIndex: number,
509
460
  ammJitIntensity: number
510
461
  ): Promise<TransactionSignature> {
511
462
  return await this.program.rpc.updateAmmJitIntensity(ammJitIntensity, {
@@ -518,7 +469,7 @@ export class Admin extends ClearingHouse {
518
469
  }
519
470
 
520
471
  public async updateMarketMaxSpread(
521
- marketIndex: BN,
472
+ marketIndex: number,
522
473
  maxSpread: number
523
474
  ): Promise<TransactionSignature> {
524
475
  return await this.program.rpc.updateMarketMaxSpread(maxSpread, {
@@ -640,7 +591,7 @@ export class Admin extends ClearingHouse {
640
591
  }
641
592
 
642
593
  public async updateWithdrawGuardThreshold(
643
- marketIndex: BN,
594
+ marketIndex: number,
644
595
  withdrawGuardThreshold: BN
645
596
  ): Promise<TransactionSignature> {
646
597
  return await this.program.rpc.updateWithdrawGuardThreshold(
@@ -659,7 +610,7 @@ export class Admin extends ClearingHouse {
659
610
  }
660
611
 
661
612
  public async updateSpotMarketIfFactor(
662
- marketIndex: BN,
613
+ marketIndex: number,
663
614
  userIfFactor: BN,
664
615
  totalIfFactor: BN
665
616
  ): Promise<TransactionSignature> {
@@ -681,7 +632,7 @@ export class Admin extends ClearingHouse {
681
632
  }
682
633
 
683
634
  public async updateSpotMarketRevenueSettlePeriod(
684
- marketIndex: BN,
635
+ marketIndex: number,
685
636
  revenueSettlePeriod: BN
686
637
  ): Promise<TransactionSignature> {
687
638
  return await this.program.rpc.updateSpotMarketRevenueSettlePeriod(
@@ -700,7 +651,7 @@ export class Admin extends ClearingHouse {
700
651
  }
701
652
 
702
653
  public async updateInsuranceWithdrawEscrowPeriod(
703
- marketIndex: BN,
654
+ marketIndex: number,
704
655
  insuranceWithdrawEscrowPeriod: BN
705
656
  ): Promise<TransactionSignature> {
706
657
  return await this.program.rpc.updateInsuranceWithdrawEscrowPeriod(
@@ -719,7 +670,7 @@ export class Admin extends ClearingHouse {
719
670
  }
720
671
 
721
672
  public async updateLpCooldownTime(
722
- marketIndex: BN,
673
+ marketIndex: number,
723
674
  cooldownTime: BN
724
675
  ): Promise<TransactionSignature> {
725
676
  return await this.program.rpc.updateLpCooldownTime(cooldownTime, {
@@ -732,7 +683,7 @@ export class Admin extends ClearingHouse {
732
683
  }
733
684
 
734
685
  public async updateMarketOracle(
735
- marketIndex: BN,
686
+ marketIndex: number,
736
687
  oracle: PublicKey,
737
688
  oracleSource: OracleSource
738
689
  ): Promise<TransactionSignature> {
@@ -746,7 +697,7 @@ export class Admin extends ClearingHouse {
746
697
  }
747
698
 
748
699
  public async updateMarketMinimumQuoteAssetTradeSize(
749
- marketIndex: BN,
700
+ marketIndex: number,
750
701
  minimumTradeSize: BN
751
702
  ): Promise<TransactionSignature> {
752
703
  return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
@@ -762,7 +713,7 @@ export class Admin extends ClearingHouse {
762
713
  }
763
714
 
764
715
  public async updateMarketBaseAssetAmountStepSize(
765
- marketIndex: BN,
716
+ marketIndex: number,
766
717
  stepSize: BN
767
718
  ): Promise<TransactionSignature> {
768
719
  return await this.program.rpc.updateMarketBaseAssetAmountStepSize(
@@ -778,7 +729,7 @@ export class Admin extends ClearingHouse {
778
729
  }
779
730
 
780
731
  public async updateMarketExpiry(
781
- perpMarketIndex: BN,
732
+ perpMarketIndex: number,
782
733
  expiryTs: BN
783
734
  ): Promise<TransactionSignature> {
784
735
  return await this.program.rpc.updateMarketExpiry(expiryTs, {
@@ -883,7 +834,7 @@ export class Admin extends ClearingHouse {
883
834
  }
884
835
 
885
836
  public async updateMaxBaseAssetAmountRatio(
886
- marketIndex: BN,
837
+ marketIndex: number,
887
838
  maxBaseAssetAmountRatio: number
888
839
  ): Promise<TransactionSignature> {
889
840
  return await this.program.rpc.updateMaxBaseAssetAmountRatio(
@@ -899,7 +850,7 @@ export class Admin extends ClearingHouse {
899
850
  }
900
851
 
901
852
  public async updateMaxSlippageRatio(
902
- marketIndex: BN,
853
+ marketIndex: number,
903
854
  maxSlippageRatio: number
904
855
  ): Promise<TransactionSignature> {
905
856
  return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
@@ -912,7 +863,7 @@ export class Admin extends ClearingHouse {
912
863
  }
913
864
 
914
865
  public async updateMarketMaxImbalances(
915
- marketIndex: BN,
866
+ marketIndex: number,
916
867
  unrealizedMaxImbalance: BN,
917
868
  maxRevenueWithdrawPerPeriod: BN,
918
869
  quoteMaxInsurance: BN
@@ -930,4 +881,16 @@ export class Admin extends ClearingHouse {
930
881
  }
931
882
  );
932
883
  }
884
+
885
+ public async updateSerumVault(
886
+ srmVault: PublicKey
887
+ ): Promise<TransactionSignature> {
888
+ return await this.program.rpc.updateSerumVault({
889
+ accounts: {
890
+ admin: this.wallet.publicKey,
891
+ state: await this.getStatePublicKey(),
892
+ srmVault: srmVault,
893
+ },
894
+ });
895
+ }
933
896
  }