@drift-labs/sdk 0.1.18-orders.1 → 0.1.18

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 (108) hide show
  1. package/README.md +2 -1
  2. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts +4 -7
  3. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +1 -0
  4. package/lib/accounts/defaultClearingHouseAccountSubscriber.js +1 -26
  5. package/lib/accounts/defaultUserAccountSubscriber.d.ts +2 -3
  6. package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +1 -0
  7. package/lib/accounts/defaultUserAccountSubscriber.js +4 -13
  8. package/lib/accounts/types.d.ts +6 -32
  9. package/lib/accounts/types.d.ts.map +1 -0
  10. package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -0
  11. package/lib/accounts/webSocketAccountSubscriber.d.ts.map +1 -0
  12. package/lib/addresses.d.ts +1 -4
  13. package/lib/addresses.d.ts.map +1 -0
  14. package/lib/addresses.js +1 -28
  15. package/lib/admin.d.ts +6 -10
  16. package/lib/admin.d.ts.map +1 -0
  17. package/lib/admin.js +44 -50
  18. package/lib/assert/assert.d.ts +1 -0
  19. package/lib/assert/assert.d.ts.map +1 -0
  20. package/lib/clearingHouse.d.ts +3 -22
  21. package/lib/clearingHouse.d.ts.map +1 -0
  22. package/lib/clearingHouse.js +7 -238
  23. package/lib/clearingHouseUser.d.ts +17 -10
  24. package/lib/clearingHouseUser.d.ts.map +1 -0
  25. package/lib/clearingHouseUser.js +84 -98
  26. package/lib/config.d.ts +1 -0
  27. package/lib/config.d.ts.map +1 -0
  28. package/lib/config.js +1 -1
  29. package/lib/constants/markets.d.ts +1 -0
  30. package/lib/constants/markets.d.ts.map +1 -0
  31. package/lib/constants/markets.js +16 -0
  32. package/lib/constants/numericConstants.d.ts +1 -0
  33. package/lib/constants/numericConstants.d.ts.map +1 -0
  34. package/lib/examples/makeTradeExample.d.ts +1 -0
  35. package/lib/examples/makeTradeExample.d.ts.map +1 -0
  36. package/lib/idl/clearing_house.json +233 -911
  37. package/lib/index.d.ts +1 -4
  38. package/lib/index.d.ts.map +1 -0
  39. package/lib/index.js +0 -4
  40. package/lib/math/amm.d.ts +1 -1
  41. package/lib/math/amm.d.ts.map +1 -0
  42. package/lib/math/amm.js +8 -31
  43. package/lib/math/conversion.d.ts +1 -0
  44. package/lib/math/conversion.d.ts.map +1 -0
  45. package/lib/math/funding.d.ts +1 -0
  46. package/lib/math/funding.d.ts.map +1 -0
  47. package/lib/math/funding.js +4 -1
  48. package/lib/math/insuranceFund.d.ts +1 -0
  49. package/lib/math/insuranceFund.d.ts.map +1 -0
  50. package/lib/math/market.d.ts +2 -2
  51. package/lib/math/market.d.ts.map +1 -0
  52. package/lib/math/market.js +1 -11
  53. package/lib/math/position.d.ts +2 -4
  54. package/lib/math/position.d.ts.map +1 -0
  55. package/lib/math/position.js +4 -18
  56. package/lib/math/trade.d.ts +1 -0
  57. package/lib/math/trade.d.ts.map +1 -0
  58. package/lib/math/utils.d.ts +1 -0
  59. package/lib/math/utils.d.ts.map +1 -0
  60. package/lib/mockUSDCFaucet.d.ts +1 -0
  61. package/lib/mockUSDCFaucet.d.ts.map +1 -0
  62. package/lib/pythClient.d.ts +1 -0
  63. package/lib/pythClient.d.ts.map +1 -0
  64. package/lib/tx/defaultTxSender.d.ts +1 -0
  65. package/lib/tx/defaultTxSender.d.ts.map +1 -0
  66. package/lib/tx/types.d.ts +1 -0
  67. package/lib/tx/types.d.ts.map +1 -0
  68. package/lib/tx/utils.d.ts +1 -0
  69. package/lib/tx/utils.d.ts.map +1 -0
  70. package/lib/types.d.ts +8 -140
  71. package/lib/types.d.ts.map +1 -0
  72. package/lib/types.js +1 -36
  73. package/lib/util/computeUnits.d.ts +1 -0
  74. package/lib/util/computeUnits.d.ts.map +1 -0
  75. package/lib/util/tps.d.ts +1 -0
  76. package/lib/util/tps.d.ts.map +1 -0
  77. package/lib/wallet.d.ts +1 -0
  78. package/lib/wallet.d.ts.map +1 -0
  79. package/package.json +1 -1
  80. package/src/accounts/defaultClearingHouseAccountSubscriber.ts +5 -52
  81. package/src/accounts/defaultUserAccountSubscriber.ts +6 -31
  82. package/src/accounts/types.ts +5 -42
  83. package/src/addresses.ts +0 -35
  84. package/src/admin.ts +52 -86
  85. package/src/clearingHouse.ts +7 -340
  86. package/src/clearingHouseUser.ts +102 -154
  87. package/src/config.ts +1 -1
  88. package/src/constants/markets.ts +16 -0
  89. package/src/idl/clearing_house.json +233 -911
  90. package/src/index.ts +0 -4
  91. package/src/math/amm.ts +14 -47
  92. package/src/math/funding.ts +5 -1
  93. package/src/math/market.ts +2 -28
  94. package/src/math/position.ts +3 -23
  95. package/src/types.ts +7 -124
  96. package/tsconfig.json +1 -0
  97. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -28
  98. package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
  99. package/lib/math/orders.d.ts +0 -3
  100. package/lib/math/orders.js +0 -30
  101. package/lib/orderParams.d.ts +0 -7
  102. package/lib/orderParams.js +0 -88
  103. package/lib/orders.d.ts +0 -5
  104. package/lib/orders.js +0 -136
  105. package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -176
  106. package/src/math/orders.ts +0 -39
  107. package/src/orderParams.ts +0 -128
  108. package/src/orders.ts +0 -230
@@ -7,7 +7,6 @@ import {
7
7
  import {
8
8
  MarketsAccount,
9
9
  StateAccount,
10
- CurveHistoryAccount,
11
10
  DepositHistoryAccount,
12
11
  FundingPaymentHistoryAccount,
13
12
  FundingRateHistoryAccount,
@@ -17,10 +16,7 @@ import {
17
16
  TradeHistoryAccount,
18
17
  UserAccount,
19
18
  Market,
20
- OrderHistoryAccount,
21
- OrderStateAccount,
22
- OrderParams,
23
- Order,
19
+ ExtendedCurveHistoryAccount,
24
20
  } from './types';
25
21
  import * as anchor from '@project-serum/anchor';
26
22
  import clearingHouseIDL from './idl/clearing_house.json';
@@ -40,11 +36,8 @@ import { EventEmitter } from 'events';
40
36
  import StrictEventEmitter from 'strict-event-emitter-types';
41
37
  import {
42
38
  getClearingHouseStateAccountPublicKey,
43
- getOrderStateAccountPublicKey,
44
39
  getUserAccountPublicKey,
45
40
  getUserAccountPublicKeyAndNonce,
46
- getUserOrdersAccountPublicKey,
47
- getUserOrdersAccountPublicKeyAndNonce,
48
41
  } from './addresses';
49
42
  import {
50
43
  ClearingHouseAccountSubscriber,
@@ -143,7 +136,6 @@ export class ClearingHouse {
143
136
  'fundingRateHistoryAccount',
144
137
  'liquidationHistoryAccount',
145
138
  'tradeHistoryAccount',
146
- 'orderHistoryAccount',
147
139
  ]);
148
140
  }
149
141
 
@@ -208,29 +200,10 @@ export class ClearingHouse {
208
200
  return this.accountSubscriber.getDepositHistoryAccount();
209
201
  }
210
202
 
211
- public getCurveHistoryAccount(): CurveHistoryAccount {
203
+ public getCurveHistoryAccount(): ExtendedCurveHistoryAccount {
212
204
  return this.accountSubscriber.getCurveHistoryAccount();
213
205
  }
214
206
 
215
- public getOrderHistoryAccount(): OrderHistoryAccount {
216
- return this.accountSubscriber.getOrderHistoryAccount();
217
- }
218
-
219
- orderStatePublicKey?: PublicKey;
220
- public async getOrderStatePublicKey(): Promise<PublicKey> {
221
- if (this.orderStatePublicKey) {
222
- return this.orderStatePublicKey;
223
- }
224
- this.orderStatePublicKey = await getOrderStateAccountPublicKey(
225
- this.program.programId
226
- );
227
- return this.orderStatePublicKey;
228
- }
229
-
230
- public getOrderStateAccount(): OrderStateAccount {
231
- return this.accountSubscriber.getOrderStateAccount();
232
- }
233
-
234
207
  /**
235
208
  * Update the wallet to use for clearing house transactions and linked user account
236
209
  * @param newWallet
@@ -259,12 +232,9 @@ export class ClearingHouse {
259
232
  userPositionsAccount,
260
233
  userAccountPublicKey,
261
234
  initializeUserAccountIx,
262
- initializeUserOrdersAccountIx,
263
235
  ] = await this.getInitializeUserInstructions();
264
236
 
265
- const tx = new Transaction()
266
- .add(initializeUserAccountIx)
267
- .add(initializeUserOrdersAccountIx);
237
+ const tx = new Transaction().add(initializeUserAccountIx);
268
238
  const txSig = await this.txSender.send(
269
239
  tx,
270
240
  [userPositionsAccount],
@@ -274,9 +244,9 @@ export class ClearingHouse {
274
244
  }
275
245
 
276
246
  async getInitializeUserInstructions(): Promise<
277
- [Keypair, PublicKey, TransactionInstruction, TransactionInstruction]
247
+ [Keypair, PublicKey, TransactionInstruction]
278
248
  > {
279
- const [userAccountPublicKey, userAccountNonce] =
249
+ const [userPublicKey, userAccountNonce] =
280
250
  await getUserAccountPublicKeyAndNonce(
281
251
  this.program.programId,
282
252
  this.wallet.publicKey
@@ -310,7 +280,7 @@ export class ClearingHouse {
310
280
  optionalAccounts,
311
281
  {
312
282
  accounts: {
313
- user: userAccountPublicKey,
283
+ user: userPublicKey,
314
284
  authority: this.wallet.publicKey,
315
285
  rent: anchor.web3.SYSVAR_RENT_PUBKEY,
316
286
  systemProgram: anchor.web3.SystemProgram.programId,
@@ -320,44 +290,7 @@ export class ClearingHouse {
320
290
  remainingAccounts: remainingAccounts,
321
291
  }
322
292
  );
323
-
324
- const initializeUserOrdersAccountIx =
325
- await this.getInitializeUserOrdersInstruction(userAccountPublicKey);
326
-
327
- return [
328
- userPositions,
329
- userAccountPublicKey,
330
- initializeUserAccountIx,
331
- initializeUserOrdersAccountIx,
332
- ];
333
- }
334
-
335
- async getInitializeUserOrdersInstruction(
336
- userAccountPublicKey?: PublicKey
337
- ): Promise<TransactionInstruction> {
338
- const [userOrdersAccountPublicKey, userOrdersAccountNonce] =
339
- await getUserOrdersAccountPublicKeyAndNonce(
340
- this.program.programId,
341
- this.wallet.publicKey
342
- );
343
-
344
- if (!userAccountPublicKey) {
345
- userAccountPublicKey = await this.getUserAccountPublicKey();
346
- }
347
-
348
- return await this.program.instruction.initializeUserOrders(
349
- userOrdersAccountNonce,
350
- {
351
- accounts: {
352
- user: userAccountPublicKey,
353
- authority: this.wallet.publicKey,
354
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
355
- systemProgram: anchor.web3.SystemProgram.programId,
356
- userOrders: userOrdersAccountPublicKey,
357
- state: await this.getStatePublicKey(),
358
- },
359
- }
360
- );
293
+ return [userPositions, userPublicKey, initializeUserAccountIx];
361
294
  }
362
295
 
363
296
  userAccountPublicKey?: PublicKey;
@@ -389,23 +322,6 @@ export class ClearingHouse {
389
322
  return this.userAccount;
390
323
  }
391
324
 
392
- userOrdersAccountPublicKey?: PublicKey;
393
- /**
394
- * Get the address for the Clearing House User Order's account. NOT the user's wallet address.
395
- * @returns
396
- */
397
- public async getUserOrdersAccountPublicKey(): Promise<PublicKey> {
398
- if (this.userOrdersAccountPublicKey) {
399
- return this.userOrdersAccountPublicKey;
400
- }
401
-
402
- this.userOrdersAccountPublicKey = await getUserOrdersAccountPublicKey(
403
- this.program.programId,
404
- this.wallet.publicKey
405
- );
406
- return this.userOrdersAccountPublicKey;
407
- }
408
-
409
325
  public async depositCollateral(
410
326
  amount: BN,
411
327
  collateralAccountPublicKey: PublicKey,
@@ -463,7 +379,6 @@ export class ClearingHouse {
463
379
  userPositionsAccount,
464
380
  userAccountPublicKey,
465
381
  initializeUserAccountIx,
466
- initializeUserOrdersAccountIx,
467
382
  ] = await this.getInitializeUserInstructions();
468
383
 
469
384
  const depositCollateralIx = await this.getDepositCollateralInstruction(
@@ -474,7 +389,6 @@ export class ClearingHouse {
474
389
 
475
390
  const tx = new Transaction()
476
391
  .add(initializeUserAccountIx)
477
- .add(initializeUserOrdersAccountIx)
478
392
  .add(depositCollateralIx);
479
393
 
480
394
  const txSig = await this.program.provider.send(tx, [userPositionsAccount]);
@@ -496,7 +410,6 @@ export class ClearingHouse {
496
410
  userPositionsAccount,
497
411
  userAccountPublicKey,
498
412
  initializeUserAccountIx,
499
- initializeUserOrdersAccountIx,
500
413
  ] = await this.getInitializeUserInstructions();
501
414
 
502
415
  const depositCollateralIx = await this.getDepositCollateralInstruction(
@@ -509,7 +422,6 @@ export class ClearingHouse {
509
422
  .add(createAssociatedAccountIx)
510
423
  .add(mintToIx)
511
424
  .add(initializeUserAccountIx)
512
- .add(initializeUserOrdersAccountIx)
513
425
  .add(depositCollateralIx);
514
426
 
515
427
  const txSig = await this.program.provider.send(tx, [userPositionsAccount]);
@@ -660,251 +572,6 @@ export class ClearingHouse {
660
572
  );
661
573
  }
662
574
 
663
- public async placeOrder(
664
- orderParams: OrderParams,
665
- discountToken?: PublicKey,
666
- referrer?: PublicKey
667
- ): Promise<TransactionSignature> {
668
- return await this.txSender.send(
669
- wrapInTx(
670
- await this.getPlaceOrderIx(orderParams, discountToken, referrer)
671
- ),
672
- [],
673
- this.opts
674
- );
675
- }
676
-
677
- public async getPlaceOrderIx(
678
- orderParams: OrderParams,
679
- discountToken?: PublicKey,
680
- referrer?: PublicKey
681
- ): Promise<TransactionInstruction> {
682
- const userAccountPublicKey = await this.getUserAccountPublicKey();
683
- const userAccount = await this.getUserAccount();
684
-
685
- const priceOracle =
686
- this.getMarketsAccount().markets[orderParams.marketIndex.toNumber()].amm
687
- .oracle;
688
-
689
- const remainingAccounts = [];
690
- if (orderParams.optionalAccounts.discountToken) {
691
- if (!discountToken) {
692
- throw Error(
693
- 'Optional accounts specified discount token but no discount token present'
694
- );
695
- }
696
-
697
- remainingAccounts.push({
698
- pubkey: discountToken,
699
- isWritable: false,
700
- isSigner: false,
701
- });
702
- }
703
-
704
- if (orderParams.optionalAccounts.referrer) {
705
- if (!referrer) {
706
- throw Error(
707
- 'Optional accounts specified referrer but no referrer present'
708
- );
709
- }
710
-
711
- remainingAccounts.push({
712
- pubkey: referrer,
713
- isWritable: false,
714
- isSigner: false,
715
- });
716
- }
717
-
718
- const state = this.getStateAccount();
719
- const orderState = this.getOrderStateAccount();
720
- return await this.program.instruction.placeOrder(orderParams, {
721
- accounts: {
722
- state: await this.getStatePublicKey(),
723
- user: userAccountPublicKey,
724
- authority: this.wallet.publicKey,
725
- markets: state.markets,
726
- userOrders: await this.getUserOrdersAccountPublicKey(),
727
- userPositions: userAccount.positions,
728
- fundingPaymentHistory: state.fundingPaymentHistory,
729
- fundingRateHistory: state.fundingRateHistory,
730
- orderState: await this.getOrderStatePublicKey(),
731
- orderHistory: orderState.orderHistory,
732
- oracle: priceOracle,
733
- },
734
- remainingAccounts,
735
- });
736
- }
737
-
738
- public async cancelOrder(orderId: BN): Promise<TransactionSignature> {
739
- return await this.txSender.send(
740
- wrapInTx(await this.getCancelOrderIx(orderId)),
741
- [],
742
- this.opts
743
- );
744
- }
745
-
746
- public async getCancelOrderIx(orderId: BN): Promise<TransactionInstruction> {
747
- const userAccountPublicKey = await this.getUserAccountPublicKey();
748
- const userAccount = await this.getUserAccount();
749
-
750
- const state = this.getStateAccount();
751
- const orderState = this.getOrderStateAccount();
752
- return await this.program.instruction.cancelOrder(orderId, {
753
- accounts: {
754
- state: await this.getStatePublicKey(),
755
- user: userAccountPublicKey,
756
- authority: this.wallet.publicKey,
757
- markets: state.markets,
758
- userOrders: await this.getUserOrdersAccountPublicKey(),
759
- userPositions: userAccount.positions,
760
- fundingPaymentHistory: state.fundingPaymentHistory,
761
- fundingRateHistory: state.fundingRateHistory,
762
- orderState: await this.getOrderStatePublicKey(),
763
- orderHistory: orderState.orderHistory,
764
- },
765
- });
766
- }
767
-
768
- public async fillOrder(
769
- userAccountPublicKey: PublicKey,
770
- userOrdersAccountPublicKey: PublicKey,
771
- order: Order
772
- ): Promise<TransactionSignature> {
773
- return await this.txSender.send(
774
- wrapInTx(
775
- await this.getFillOrderIx(
776
- userAccountPublicKey,
777
- userOrdersAccountPublicKey,
778
- order
779
- )
780
- ),
781
- [],
782
- this.opts
783
- );
784
- }
785
-
786
- public async getFillOrderIx(
787
- userAccountPublicKey: PublicKey,
788
- userOrdersAccountPublicKey: PublicKey,
789
- order: Order
790
- ): Promise<TransactionInstruction> {
791
- const fillerPublicKey = await this.getUserAccountPublicKey();
792
- const userAccount: any = await this.program.account.user.fetch(
793
- userAccountPublicKey
794
- );
795
-
796
- const marketIndex = order.marketIndex;
797
- const oracle = this.getMarket(marketIndex).amm.oracle;
798
-
799
- const state = this.getStateAccount();
800
- const orderState = this.getOrderStateAccount();
801
-
802
- const remainingAccounts = [];
803
- if (!order.referrer.equals(PublicKey.default)) {
804
- remainingAccounts.push({
805
- pubkey: order.referrer,
806
- isWritable: true,
807
- isSigner: false,
808
- });
809
- }
810
-
811
- const orderId = order.orderId;
812
- return await this.program.instruction.fillOrder(orderId, {
813
- accounts: {
814
- state: await this.getStatePublicKey(),
815
- filler: fillerPublicKey,
816
- user: userAccountPublicKey,
817
- authority: this.wallet.publicKey,
818
- markets: state.markets,
819
- userPositions: userAccount.positions,
820
- userOrders: userOrdersAccountPublicKey,
821
- tradeHistory: state.tradeHistory,
822
- fundingPaymentHistory: state.fundingPaymentHistory,
823
- fundingRateHistory: state.fundingRateHistory,
824
- orderState: await this.getOrderStatePublicKey(),
825
- orderHistory: orderState.orderHistory,
826
- oracle: oracle,
827
- },
828
- remainingAccounts,
829
- });
830
- }
831
-
832
- public async placeAndFillOrder(
833
- orderParams: OrderParams,
834
- discountToken?: PublicKey,
835
- referrer?: PublicKey
836
- ): Promise<TransactionSignature> {
837
- return await this.txSender.send(
838
- wrapInTx(
839
- await this.getPlaceAndFillOrderIx(orderParams, discountToken, referrer)
840
- ),
841
- [],
842
- this.opts
843
- );
844
- }
845
-
846
- public async getPlaceAndFillOrderIx(
847
- orderParams: OrderParams,
848
- discountToken?: PublicKey,
849
- referrer?: PublicKey
850
- ): Promise<TransactionInstruction> {
851
- const userAccountPublicKey = await this.getUserAccountPublicKey();
852
- const userAccount = await this.getUserAccount();
853
-
854
- const priceOracle =
855
- this.getMarketsAccount().markets[orderParams.marketIndex.toNumber()].amm
856
- .oracle;
857
-
858
- const remainingAccounts = [];
859
- if (orderParams.optionalAccounts.discountToken) {
860
- if (!discountToken) {
861
- throw Error(
862
- 'Optional accounts specified discount token but no discount token present'
863
- );
864
- }
865
-
866
- remainingAccounts.push({
867
- pubkey: discountToken,
868
- isWritable: false,
869
- isSigner: false,
870
- });
871
- }
872
-
873
- if (orderParams.optionalAccounts.referrer) {
874
- if (!referrer) {
875
- throw Error(
876
- 'Optional accounts specified referrer but no referrer present'
877
- );
878
- }
879
-
880
- remainingAccounts.push({
881
- pubkey: referrer,
882
- isWritable: true,
883
- isSigner: false,
884
- });
885
- }
886
-
887
- const state = this.getStateAccount();
888
- const orderState = this.getOrderStateAccount();
889
- return await this.program.instruction.placeAndFillOrder(orderParams, {
890
- accounts: {
891
- state: await this.getStatePublicKey(),
892
- user: userAccountPublicKey,
893
- authority: this.wallet.publicKey,
894
- markets: state.markets,
895
- userOrders: await this.getUserOrdersAccountPublicKey(),
896
- userPositions: userAccount.positions,
897
- tradeHistory: state.tradeHistory,
898
- fundingPaymentHistory: state.fundingPaymentHistory,
899
- fundingRateHistory: state.fundingRateHistory,
900
- orderState: await this.getOrderStatePublicKey(),
901
- orderHistory: orderState.orderHistory,
902
- oracle: priceOracle,
903
- },
904
- remainingAccounts,
905
- });
906
- }
907
-
908
575
  /**
909
576
  * Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
910
577
  * @param marketIndex