@drift-labs/sdk 0.1.19-master.2 → 0.1.21-master.4

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 (143) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +31 -0
  2. package/lib/accounts/bulkAccountLoader.js +177 -0
  3. package/lib/accounts/bulkUserSubscription.d.ts +7 -0
  4. package/lib/accounts/bulkUserSubscription.js +28 -0
  5. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +49 -0
  6. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +228 -0
  7. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +25 -0
  8. package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
  9. package/lib/accounts/pollingUserAccountSubscriber.d.ts +32 -0
  10. package/lib/accounts/pollingUserAccountSubscriber.js +136 -0
  11. package/lib/accounts/types.d.ts +38 -3
  12. package/lib/accounts/utils.d.ts +1 -0
  13. package/lib/accounts/utils.js +7 -0
  14. package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -3
  15. package/lib/accounts/webSocketAccountSubscriber.js +43 -12
  16. package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +8 -3
  17. package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +30 -4
  18. package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +6 -3
  19. package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +16 -4
  20. package/lib/addresses.d.ts +4 -1
  21. package/lib/addresses.js +28 -1
  22. package/lib/admin.d.ts +10 -4
  23. package/lib/admin.js +54 -17
  24. package/lib/assert/assert.d.ts +0 -1
  25. package/lib/clearingHouse.d.ts +39 -4
  26. package/lib/clearingHouse.js +334 -23
  27. package/lib/clearingHouseUser.d.ts +27 -6
  28. package/lib/clearingHouseUser.js +125 -45
  29. package/lib/config.d.ts +0 -1
  30. package/lib/constants/markets.d.ts +2 -2
  31. package/lib/constants/markets.js +28 -15
  32. package/lib/constants/numericConstants.d.ts +4 -2
  33. package/lib/constants/numericConstants.js +16 -17
  34. package/lib/examples/makeTradeExample.d.ts +0 -1
  35. package/lib/examples/makeTradeExample.js +6 -6
  36. package/lib/factory/clearingHouse.d.ts +25 -0
  37. package/lib/factory/clearingHouse.js +64 -0
  38. package/lib/factory/clearingHouseUser.d.ts +19 -0
  39. package/lib/factory/clearingHouseUser.js +34 -0
  40. package/lib/idl/clearing_house.json +1066 -39
  41. package/lib/index.d.ts +11 -3
  42. package/lib/index.js +12 -2
  43. package/lib/math/amm.d.ts +3 -1
  44. package/lib/math/amm.js +128 -15
  45. package/lib/math/conversion.d.ts +1 -2
  46. package/lib/math/conversion.js +1 -1
  47. package/lib/math/funding.d.ts +0 -1
  48. package/lib/math/funding.js +1 -1
  49. package/lib/math/insuranceFund.d.ts +2 -2
  50. package/lib/math/insuranceFund.js +3 -6
  51. package/lib/math/market.d.ts +2 -2
  52. package/lib/math/market.js +12 -2
  53. package/lib/math/orders.d.ts +3 -0
  54. package/lib/math/orders.js +32 -0
  55. package/lib/math/position.d.ts +6 -3
  56. package/lib/math/position.js +21 -10
  57. package/lib/math/trade.d.ts +0 -1
  58. package/lib/math/trade.js +16 -16
  59. package/lib/math/utils.d.ts +2 -2
  60. package/lib/math/utils.js +3 -3
  61. package/lib/mockUSDCFaucet.d.ts +2 -2
  62. package/lib/orderParams.d.ts +7 -0
  63. package/lib/orderParams.js +108 -0
  64. package/lib/orders.d.ts +6 -0
  65. package/lib/orders.js +136 -0
  66. package/lib/pythClient.d.ts +0 -1
  67. package/lib/pythClient.js +1 -1
  68. package/lib/token/index.d.ts +3 -0
  69. package/lib/token/index.js +38 -0
  70. package/lib/tx/defaultTxSender.d.ts +0 -1
  71. package/lib/tx/types.d.ts +0 -1
  72. package/lib/tx/utils.d.ts +0 -1
  73. package/lib/types.d.ts +147 -3
  74. package/lib/types.js +36 -1
  75. package/lib/util/computeUnits.d.ts +0 -1
  76. package/lib/util/tps.d.ts +0 -1
  77. package/lib/wallet.d.ts +0 -1
  78. package/package.json +11 -3
  79. package/src/accounts/bulkAccountLoader.ts +215 -0
  80. package/src/accounts/bulkUserSubscription.ts +28 -0
  81. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +326 -0
  82. package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
  83. package/src/accounts/pollingUserAccountSubscriber.ts +194 -0
  84. package/src/accounts/types.ts +48 -1
  85. package/src/accounts/utils.ts +3 -0
  86. package/src/accounts/webSocketAccountSubscriber.ts +67 -17
  87. package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +51 -1
  88. package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +33 -3
  89. package/src/addresses.ts +37 -0
  90. package/src/admin.ts +92 -24
  91. package/src/clearingHouse.ts +455 -22
  92. package/src/clearingHouseUser.ts +155 -18
  93. package/src/constants/markets.ts +17 -1
  94. package/src/constants/numericConstants.ts +3 -1
  95. package/src/examples/makeTradeExample.ts +4 -1
  96. package/src/factory/clearingHouse.ts +125 -0
  97. package/src/factory/clearingHouseUser.ts +73 -0
  98. package/src/idl/clearing_house.json +1066 -39
  99. package/src/index.ts +11 -2
  100. package/src/math/amm.ts +169 -14
  101. package/src/math/conversion.ts +1 -1
  102. package/src/math/insuranceFund.ts +1 -1
  103. package/src/math/market.ts +28 -2
  104. package/src/math/orders.ts +44 -0
  105. package/src/math/position.ts +24 -4
  106. package/src/math/utils.ts +1 -1
  107. package/src/mockUSDCFaucet.ts +1 -1
  108. package/src/orderParams.ts +151 -0
  109. package/src/orders.ts +236 -0
  110. package/src/token/index.ts +37 -0
  111. package/src/types.ts +130 -2
  112. package/tsconfig.json +0 -1
  113. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
  114. package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
  115. package/lib/accounts/types.d.ts.map +0 -1
  116. package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
  117. package/lib/addresses.d.ts.map +0 -1
  118. package/lib/admin.d.ts.map +0 -1
  119. package/lib/assert/assert.d.ts.map +0 -1
  120. package/lib/clearingHouse.d.ts.map +0 -1
  121. package/lib/clearingHouseUser.d.ts.map +0 -1
  122. package/lib/config.d.ts.map +0 -1
  123. package/lib/constants/markets.d.ts.map +0 -1
  124. package/lib/constants/numericConstants.d.ts.map +0 -1
  125. package/lib/examples/makeTradeExample.d.ts.map +0 -1
  126. package/lib/index.d.ts.map +0 -1
  127. package/lib/math/amm.d.ts.map +0 -1
  128. package/lib/math/conversion.d.ts.map +0 -1
  129. package/lib/math/funding.d.ts.map +0 -1
  130. package/lib/math/insuranceFund.d.ts.map +0 -1
  131. package/lib/math/market.d.ts.map +0 -1
  132. package/lib/math/position.d.ts.map +0 -1
  133. package/lib/math/trade.d.ts.map +0 -1
  134. package/lib/math/utils.d.ts.map +0 -1
  135. package/lib/mockUSDCFaucet.d.ts.map +0 -1
  136. package/lib/pythClient.d.ts.map +0 -1
  137. package/lib/tx/defaultTxSender.d.ts.map +0 -1
  138. package/lib/tx/types.d.ts.map +0 -1
  139. package/lib/tx/utils.d.ts.map +0 -1
  140. package/lib/types.d.ts.map +0 -1
  141. package/lib/util/computeUnits.d.ts.map +0 -1
  142. package/lib/util/tps.d.ts.map +0 -1
  143. package/lib/wallet.d.ts.map +0 -1
package/src/admin.ts CHANGED
@@ -5,18 +5,29 @@ import {
5
5
  SYSVAR_RENT_PUBKEY,
6
6
  TransactionSignature,
7
7
  } from '@solana/web3.js';
8
- import { FeeStructure, IWallet, OracleGuardRails, OracleSource } from './types';
9
- import { BN, Idl, Program, Provider } from '@project-serum/anchor';
8
+ import {
9
+ FeeStructure,
10
+ IWallet,
11
+ OracleGuardRails,
12
+ OracleSource,
13
+ OrderFillerRewardStructure,
14
+ } from './types';
15
+ import { BN, Provider } from '@project-serum/anchor';
10
16
  import * as anchor from '@project-serum/anchor';
11
- import { getClearingHouseStateAccountPublicKeyAndNonce } from './addresses';
17
+ import {
18
+ getClearingHouseStateAccountPublicKey,
19
+ getClearingHouseStateAccountPublicKeyAndNonce,
20
+ getOrderStateAccountPublicKeyAndNonce,
21
+ } from './addresses';
12
22
  import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
13
23
  import { ClearingHouse } from './clearingHouse';
14
24
  import { PEG_PRECISION } from './constants/numericConstants';
15
- import clearingHouseIDL from './idl/clearing_house.json';
16
- import { DefaultClearingHouseAccountSubscriber } from './accounts/defaultClearingHouseAccountSubscriber';
17
- import { DefaultTxSender } from './tx/defaultTxSender';
18
25
  import { calculateTargetPriceTrade } from './math/trade';
19
26
  import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
27
+ import {
28
+ getAdmin,
29
+ getWebSocketClearingHouseConfig,
30
+ } from './factory/clearingHouse';
20
31
 
21
32
  export class Admin extends ClearingHouse {
22
33
  public static from(
@@ -25,30 +36,21 @@ export class Admin extends ClearingHouse {
25
36
  clearingHouseProgramId: PublicKey,
26
37
  opts: ConfirmOptions = Provider.defaultOptions()
27
38
  ): Admin {
28
- const provider = new Provider(connection, wallet, opts);
29
- const program = new Program(
30
- clearingHouseIDL as Idl,
31
- clearingHouseProgramId,
32
- provider
33
- );
34
- const accountSubscriber = new DefaultClearingHouseAccountSubscriber(
35
- program
36
- );
37
- const txSender = new DefaultTxSender(provider);
38
- return new Admin(
39
+ const config = getWebSocketClearingHouseConfig(
39
40
  connection,
40
41
  wallet,
41
- program,
42
- accountSubscriber,
43
- txSender,
42
+ clearingHouseProgramId,
44
43
  opts
45
44
  );
45
+ return getAdmin(config);
46
46
  }
47
47
 
48
48
  public async initialize(
49
49
  usdcMint: PublicKey,
50
50
  adminControlsPrices: boolean
51
- ): Promise<[TransactionSignature, TransactionSignature]> {
51
+ ): Promise<
52
+ [TransactionSignature, TransactionSignature, TransactionSignature]
53
+ > {
52
54
  const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
53
55
  await this.getStatePublicKey()
54
56
  );
@@ -172,7 +174,40 @@ export class Admin extends ClearingHouse {
172
174
  this.opts
173
175
  );
174
176
 
175
- return [initializeTxSig, initializeHistoryTxSig];
177
+ const initializeOrderStateTxSig = await this.initializeOrderState();
178
+
179
+ return [initializeTxSig, initializeHistoryTxSig, initializeOrderStateTxSig];
180
+ }
181
+
182
+ public async initializeOrderState(): Promise<TransactionSignature> {
183
+ const orderHistory = anchor.web3.Keypair.generate();
184
+ const [orderStatePublicKey, orderStateNonce] =
185
+ await getOrderStateAccountPublicKeyAndNonce(this.program.programId);
186
+ const clearingHouseStatePublicKey =
187
+ await getClearingHouseStateAccountPublicKey(this.program.programId);
188
+
189
+ const initializeOrderStateTx =
190
+ await this.program.transaction.initializeOrderState(orderStateNonce, {
191
+ accounts: {
192
+ admin: this.wallet.publicKey,
193
+ state: clearingHouseStatePublicKey,
194
+ orderHistory: orderHistory.publicKey,
195
+ orderState: orderStatePublicKey,
196
+ rent: SYSVAR_RENT_PUBKEY,
197
+ systemProgram: anchor.web3.SystemProgram.programId,
198
+ },
199
+ instructions: [
200
+ await this.program.account.orderHistory.createInstruction(
201
+ orderHistory
202
+ ),
203
+ ],
204
+ });
205
+
206
+ return await this.txSender.send(
207
+ initializeOrderStateTx,
208
+ [orderHistory],
209
+ this.opts
210
+ );
176
211
  }
177
212
 
178
213
  public async initializeMarket(
@@ -518,6 +553,21 @@ export class Admin extends ClearingHouse {
518
553
  );
519
554
  }
520
555
 
556
+ public async updateOrderFillerRewardStructure(
557
+ orderFillerRewardStructure: OrderFillerRewardStructure
558
+ ): Promise<TransactionSignature> {
559
+ return await this.program.rpc.updateOrderFillerRewardStructure(
560
+ orderFillerRewardStructure,
561
+ {
562
+ accounts: {
563
+ admin: this.wallet.publicKey,
564
+ state: await this.getStatePublicKey(),
565
+ orderState: await this.getOrderStatePublicKey(),
566
+ },
567
+ }
568
+ );
569
+ }
570
+
521
571
  public async updateFee(fees: FeeStructure): Promise<TransactionSignature> {
522
572
  return await this.program.rpc.updateFee(fees, {
523
573
  accounts: {
@@ -558,12 +608,30 @@ export class Admin extends ClearingHouse {
558
608
  );
559
609
  }
560
610
 
561
- public async updateMarketMinimumTradeSize(
611
+ public async updateMarketMinimumQuoteAssetTradeSize(
612
+ marketIndex: BN,
613
+ minimumTradeSize: BN
614
+ ): Promise<TransactionSignature> {
615
+ const state = this.getStateAccount();
616
+ return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
617
+ marketIndex,
618
+ minimumTradeSize,
619
+ {
620
+ accounts: {
621
+ admin: this.wallet.publicKey,
622
+ state: await this.getStatePublicKey(),
623
+ markets: state.markets,
624
+ },
625
+ }
626
+ );
627
+ }
628
+
629
+ public async updateMarketMinimumBaseAssetTradeSize(
562
630
  marketIndex: BN,
563
631
  minimumTradeSize: BN
564
632
  ): Promise<TransactionSignature> {
565
633
  const state = this.getStateAccount();
566
- return await this.program.rpc.updateMarketMinimumTradeSize(
634
+ return await this.program.rpc.updateMarketMinimumBaseAssetTradeSize(
567
635
  marketIndex,
568
636
  minimumTradeSize,
569
637
  {