@drift-labs/sdk 0.2.0-master.0 → 0.2.0-master.11

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 (121) hide show
  1. package/lib/admin.d.ts +8 -5
  2. package/lib/admin.js +43 -11
  3. package/lib/clearingHouse.d.ts +28 -18
  4. package/lib/clearingHouse.js +366 -143
  5. package/lib/clearingHouseUser.d.ts +2 -2
  6. package/lib/clearingHouseUser.js +8 -17
  7. package/lib/config.js +1 -1
  8. package/lib/constants/banks.js +9 -2
  9. package/lib/constants/numericConstants.d.ts +2 -0
  10. package/lib/constants/numericConstants.js +3 -1
  11. package/lib/factory/bigNum.d.ts +8 -2
  12. package/lib/factory/bigNum.js +14 -6
  13. package/lib/idl/clearing_house.json +805 -202
  14. package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  15. package/lib/index.d.ts +3 -2
  16. package/lib/index.js +7 -2
  17. package/lib/math/amm.d.ts +6 -1
  18. package/lib/math/amm.js +124 -41
  19. package/lib/math/auction.js +4 -1
  20. package/lib/math/orders.d.ts +2 -2
  21. package/lib/math/orders.js +18 -11
  22. package/lib/math/position.js +3 -1
  23. package/lib/math/repeg.js +1 -1
  24. package/lib/math/trade.d.ts +1 -1
  25. package/lib/math/trade.js +7 -10
  26. package/lib/orderParams.d.ts +14 -5
  27. package/lib/orderParams.js +8 -96
  28. package/lib/orders.d.ts +1 -2
  29. package/lib/orders.js +6 -85
  30. package/lib/slot/SlotSubscriber.d.ts +7 -0
  31. package/lib/slot/SlotSubscriber.js +3 -0
  32. package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +7 -5
  33. package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +41 -40
  34. package/lib/tx/utils.js +1 -1
  35. package/lib/types.d.ts +137 -14
  36. package/lib/types.js +53 -1
  37. package/lib/util/computeUnits.js +1 -1
  38. package/package.json +3 -3
  39. package/src/accounts/bulkAccountLoader.js +197 -0
  40. package/src/accounts/bulkUserSubscription.js +33 -0
  41. package/src/accounts/fetch.js +29 -0
  42. package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
  43. package/src/accounts/pollingOracleSubscriber.js +93 -0
  44. package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
  45. package/src/accounts/pollingUserAccountSubscriber.js +132 -0
  46. package/src/accounts/types.js +10 -0
  47. package/src/accounts/utils.js +7 -0
  48. package/src/accounts/webSocketAccountSubscriber.js +93 -0
  49. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
  50. package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
  51. package/src/addresses/marketAddresses.js +26 -0
  52. package/src/addresses/pda.js +104 -0
  53. package/src/admin.ts +66 -14
  54. package/src/assert/assert.js +9 -0
  55. package/src/clearingHouse.ts +558 -230
  56. package/src/clearingHouseUser.ts +12 -23
  57. package/src/config.ts +1 -1
  58. package/src/constants/banks.ts +9 -2
  59. package/src/constants/numericConstants.ts +2 -0
  60. package/src/events/eventList.js +77 -0
  61. package/src/events/eventSubscriber.js +139 -0
  62. package/src/events/fetchLogs.js +50 -0
  63. package/src/events/pollingLogProvider.js +64 -0
  64. package/src/events/sort.js +44 -0
  65. package/src/events/txEventCache.js +71 -0
  66. package/src/events/types.js +20 -0
  67. package/src/events/webSocketLogProvider.js +41 -0
  68. package/src/examples/makeTradeExample.js +80 -0
  69. package/src/factory/bigNum.js +364 -0
  70. package/src/factory/bigNum.ts +26 -9
  71. package/src/factory/oracleClient.js +20 -0
  72. package/src/idl/clearing_house.json +805 -202
  73. package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  74. package/src/index.js +100 -0
  75. package/src/index.ts +3 -2
  76. package/src/math/amm.js +369 -0
  77. package/src/math/amm.ts +207 -52
  78. package/src/math/auction.js +42 -0
  79. package/src/math/auction.ts +5 -1
  80. package/src/math/bankBalance.js +75 -0
  81. package/src/math/conversion.js +11 -0
  82. package/src/math/funding.js +248 -0
  83. package/src/math/market.js +57 -0
  84. package/src/math/oracles.js +26 -0
  85. package/src/math/orders.js +110 -0
  86. package/src/math/orders.ts +17 -13
  87. package/src/math/position.js +140 -0
  88. package/src/math/position.ts +5 -1
  89. package/src/math/repeg.js +128 -0
  90. package/src/math/repeg.ts +2 -1
  91. package/src/math/state.js +15 -0
  92. package/src/math/trade.js +253 -0
  93. package/src/math/trade.ts +23 -25
  94. package/src/math/utils.js +0 -1
  95. package/src/mockUSDCFaucet.js +280 -0
  96. package/src/oracles/oracleClientCache.js +19 -0
  97. package/src/oracles/pythClient.js +46 -0
  98. package/src/oracles/quoteAssetOracleClient.js +32 -0
  99. package/src/oracles/switchboardClient.js +69 -0
  100. package/src/oracles/types.js +2 -0
  101. package/src/orderParams.js +20 -0
  102. package/src/orderParams.ts +20 -141
  103. package/src/orders.js +134 -0
  104. package/src/orders.ts +7 -131
  105. package/src/slot/SlotSubscriber.js +39 -0
  106. package/src/slot/SlotSubscriber.ts +11 -1
  107. package/src/token/index.js +38 -0
  108. package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +48 -59
  109. package/src/tx/retryTxSender.js +188 -0
  110. package/src/tx/types.js +2 -0
  111. package/src/tx/utils.js +17 -0
  112. package/src/tx/utils.ts +1 -1
  113. package/src/types.js +114 -0
  114. package/src/types.ts +132 -16
  115. package/src/userName.js +20 -0
  116. package/src/util/computeUnits.ts +1 -1
  117. package/src/util/promiseTimeout.js +14 -0
  118. package/src/util/tps.js +27 -0
  119. package/src/wallet.js +35 -0
  120. package/src/util/computeUnits.js +0 -17
  121. package/src/util/computeUnits.js.map +0 -1
package/src/admin.ts CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  } from './addresses/pda';
21
21
  import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
22
22
  import { ClearingHouse } from './clearingHouse';
23
- import { PEG_PRECISION } from './constants/numericConstants';
23
+ import { PEG_PRECISION, ZERO } from './constants/numericConstants';
24
24
  import { calculateTargetPriceTrade } from './math/trade';
25
25
  import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
26
26
 
@@ -85,7 +85,9 @@ export class Admin extends ClearingHouse {
85
85
  initialAssetWeight: BN,
86
86
  maintenanceAssetWeight: BN,
87
87
  initialLiabilityWeight: BN,
88
- maintenanceLiabilityWeight: BN
88
+ maintenanceLiabilityWeight: BN,
89
+ imfFactor = new BN(0),
90
+ liquidationFee = ZERO
89
91
  ): Promise<TransactionSignature> {
90
92
  const bankIndex = this.getStateAccount().numberOfBanks;
91
93
  const bank = await getBankPublicKey(this.program.programId, bankIndex);
@@ -109,6 +111,8 @@ export class Admin extends ClearingHouse {
109
111
  maintenanceAssetWeight,
110
112
  initialLiabilityWeight,
111
113
  maintenanceLiabilityWeight,
114
+ imfFactor,
115
+ liquidationFee,
112
116
  {
113
117
  accounts: {
114
118
  admin: this.wallet.publicKey,
@@ -144,8 +148,8 @@ export class Admin extends ClearingHouse {
144
148
  pegMultiplier: BN = PEG_PRECISION,
145
149
  oracleSource: OracleSource = OracleSource.PYTH,
146
150
  marginRatioInitial = 2000,
147
- marginRatioPartial = 625,
148
- marginRatioMaintenance = 500
151
+ marginRatioMaintenance = 500,
152
+ liquidationFee = ZERO
149
153
  ): Promise<TransactionSignature> {
150
154
  const marketPublicKey = await getMarketPublicKey(
151
155
  this.program.programId,
@@ -159,8 +163,8 @@ export class Admin extends ClearingHouse {
159
163
  pegMultiplier,
160
164
  oracleSource,
161
165
  marginRatioInitial,
162
- marginRatioPartial,
163
166
  marginRatioMaintenance,
167
+ liquidationFee,
164
168
  {
165
169
  accounts: {
166
170
  state: await this.getStatePublicKey(),
@@ -329,10 +333,12 @@ export class Admin extends ClearingHouse {
329
333
  recipient: PublicKey
330
334
  ): Promise<TransactionSignature> {
331
335
  const state = await this.getStateAccount();
336
+ const bank = this.getQuoteAssetBankAccount();
332
337
  return await this.program.rpc.withdrawFromInsuranceVault(amount, {
333
338
  accounts: {
334
339
  admin: this.wallet.publicKey,
335
340
  state: await this.getStatePublicKey(),
341
+ bank: bank.pubkey,
336
342
  insuranceVault: state.insuranceVault,
337
343
  insuranceVaultAuthority: state.insuranceVaultAuthority,
338
344
  recipient: recipient,
@@ -341,7 +347,7 @@ export class Admin extends ClearingHouse {
341
347
  });
342
348
  }
343
349
 
344
- public async withdrawFees(
350
+ public async withdrawFromMarketToInsuranceVault(
345
351
  marketIndex: BN,
346
352
  amount: BN,
347
353
  recipient: PublicKey
@@ -351,7 +357,7 @@ export class Admin extends ClearingHouse {
351
357
  marketIndex
352
358
  );
353
359
  const bank = this.getQuoteAssetBankAccount();
354
- return await this.program.rpc.withdrawFees(amount, {
360
+ return await this.program.rpc.withdrawFromMarketToInsuranceVault(amount, {
355
361
  accounts: {
356
362
  admin: this.wallet.publicKey,
357
363
  state: await this.getStatePublicKey(),
@@ -370,6 +376,8 @@ export class Admin extends ClearingHouse {
370
376
  amount: BN
371
377
  ): Promise<TransactionSignature> {
372
378
  const state = await this.getStateAccount();
379
+ const bank = this.getQuoteAssetBankAccount();
380
+
373
381
  return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
374
382
  accounts: {
375
383
  admin: this.wallet.publicKey,
@@ -377,7 +385,9 @@ export class Admin extends ClearingHouse {
377
385
  market: await getMarketPublicKey(this.program.programId, marketIndex),
378
386
  insuranceVault: state.insuranceVault,
379
387
  insuranceVaultAuthority: state.insuranceVaultAuthority,
380
- bankVault: this.getQuoteAssetBankAccount().vault,
388
+ bank: bank.pubkey,
389
+ bankVault: bank.vault,
390
+ bankVaultAuthority: bank.vaultAuthority,
381
391
  tokenProgram: TOKEN_PROGRAM_ID,
382
392
  },
383
393
  });
@@ -414,12 +424,10 @@ export class Admin extends ClearingHouse {
414
424
  public async updateMarginRatio(
415
425
  marketIndex: BN,
416
426
  marginRatioInitial: number,
417
- marginRatioPartial: number,
418
427
  marginRatioMaintenance: number
419
428
  ): Promise<TransactionSignature> {
420
429
  return await this.program.rpc.updateMarginRatio(
421
430
  marginRatioInitial,
422
- marginRatioPartial,
423
431
  marginRatioMaintenance,
424
432
  {
425
433
  accounts: {
@@ -444,6 +452,19 @@ export class Admin extends ClearingHouse {
444
452
  });
445
453
  }
446
454
 
455
+ public async updateMarketMaxSpread(
456
+ marketIndex: BN,
457
+ maxSpread: number
458
+ ): Promise<TransactionSignature> {
459
+ return await this.program.rpc.updateMarketMaxSpread(maxSpread, {
460
+ accounts: {
461
+ admin: this.wallet.publicKey,
462
+ state: await this.getStatePublicKey(),
463
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
464
+ },
465
+ });
466
+ }
467
+
447
468
  public async updatePartialLiquidationClosePercentage(
448
469
  numerator: BN,
449
470
  denominator: BN
@@ -662,11 +683,13 @@ export class Admin extends ClearingHouse {
662
683
  });
663
684
  }
664
685
 
665
- public async updateOrderAuctionTime(
666
- time: BN | number
686
+ public async updateAuctionDuration(
687
+ minDuration: BN | number,
688
+ maxDuration: BN | number
667
689
  ): Promise<TransactionSignature> {
668
- return await this.program.rpc.updateOrderAuctionTime(
669
- typeof time === 'number' ? time : time.toNumber,
690
+ return await this.program.rpc.updateAuctionDuration(
691
+ typeof minDuration === 'number' ? minDuration : minDuration.toNumber(),
692
+ typeof maxDuration === 'number' ? maxDuration : maxDuration.toNumber(),
670
693
  {
671
694
  accounts: {
672
695
  admin: this.wallet.publicKey,
@@ -675,4 +698,33 @@ export class Admin extends ClearingHouse {
675
698
  }
676
699
  );
677
700
  }
701
+
702
+ public async updateMaxBaseAssetAmountRatio(
703
+ marketIndex: BN,
704
+ maxBaseAssetAmountRatio: number
705
+ ): Promise<TransactionSignature> {
706
+ return await this.program.rpc.updateMaxBaseAssetAmountRatio(
707
+ maxBaseAssetAmountRatio,
708
+ {
709
+ accounts: {
710
+ admin: this.wallet.publicKey,
711
+ state: await this.getStatePublicKey(),
712
+ market: this.getMarketAccount(marketIndex).pubkey,
713
+ },
714
+ }
715
+ );
716
+ }
717
+
718
+ public async updateMaxSlippageRatio(
719
+ marketIndex: BN,
720
+ maxSlippageRatio: number
721
+ ): Promise<TransactionSignature> {
722
+ return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
723
+ accounts: {
724
+ admin: this.wallet.publicKey,
725
+ state: await this.getStatePublicKey(),
726
+ market: this.getMarketAccount(marketIndex).pubkey,
727
+ },
728
+ });
729
+ }
678
730
  }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assert = void 0;
4
+ function assert(condition, error) {
5
+ if (!condition) {
6
+ throw new Error(error || 'Unspecified AssertionError');
7
+ }
8
+ }
9
+ exports.assert = assert;