@drift-labs/sdk 0.1.18-master.2 → 0.1.18-orders.0

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 (109) hide show
  1. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts +7 -4
  2. package/lib/accounts/defaultClearingHouseAccountSubscriber.js +27 -2
  3. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +28 -0
  4. package/lib/accounts/defaultHistoryAccountSubscriber.js +110 -0
  5. package/lib/accounts/defaultUserAccountSubscriber.d.ts +3 -2
  6. package/lib/accounts/defaultUserAccountSubscriber.js +14 -3
  7. package/lib/accounts/types.d.ts +32 -6
  8. package/lib/accounts/webSocketAccountSubscriber.d.ts +0 -1
  9. package/lib/addresses.d.ts +4 -1
  10. package/lib/addresses.js +28 -1
  11. package/lib/admin.d.ts +10 -5
  12. package/lib/admin.js +53 -30
  13. package/lib/assert/assert.d.ts +0 -1
  14. package/lib/clearingHouse.d.ts +22 -3
  15. package/lib/clearingHouse.js +246 -15
  16. package/lib/clearingHouseUser.d.ts +10 -17
  17. package/lib/clearingHouseUser.js +111 -97
  18. package/lib/config.d.ts +0 -1
  19. package/lib/config.js +1 -1
  20. package/lib/constants/markets.d.ts +0 -1
  21. package/lib/constants/numericConstants.d.ts +0 -1
  22. package/lib/examples/makeTradeExample.d.ts +0 -1
  23. package/lib/examples/makeTradeExample.js +6 -6
  24. package/lib/idl/clearing_house.json +895 -132
  25. package/lib/index.d.ts +3 -1
  26. package/lib/index.js +3 -0
  27. package/lib/math/amm.d.ts +1 -1
  28. package/lib/math/amm.js +38 -15
  29. package/lib/math/conversion.d.ts +0 -1
  30. package/lib/math/conversion.js +1 -1
  31. package/lib/math/funding.d.ts +0 -1
  32. package/lib/math/funding.js +2 -2
  33. package/lib/math/insuranceFund.d.ts +0 -1
  34. package/lib/math/market.d.ts +2 -2
  35. package/lib/math/market.js +12 -2
  36. package/lib/math/orders.d.ts +3 -0
  37. package/lib/math/orders.js +30 -0
  38. package/lib/math/position.d.ts +4 -2
  39. package/lib/math/position.js +19 -5
  40. package/lib/math/trade.d.ts +0 -1
  41. package/lib/math/trade.js +16 -16
  42. package/lib/math/utils.d.ts +0 -1
  43. package/lib/mockUSDCFaucet.d.ts +0 -1
  44. package/lib/orderParams.d.ts +7 -0
  45. package/lib/orderParams.js +88 -0
  46. package/lib/orders.d.ts +5 -0
  47. package/lib/orders.js +136 -0
  48. package/lib/pythClient.d.ts +0 -1
  49. package/lib/pythClient.js +1 -1
  50. package/lib/tx/defaultTxSender.d.ts +0 -1
  51. package/lib/tx/types.d.ts +0 -1
  52. package/lib/tx/utils.d.ts +0 -1
  53. package/lib/types.d.ts +140 -7
  54. package/lib/types.js +36 -1
  55. package/lib/util/computeUnits.d.ts +0 -1
  56. package/lib/util/tps.d.ts +0 -1
  57. package/lib/wallet.d.ts +0 -1
  58. package/package.json +1 -1
  59. package/src/accounts/defaultClearingHouseAccountSubscriber.ts +52 -5
  60. package/src/accounts/defaultHistoryAccountSubscriber.ts +176 -0
  61. package/src/accounts/defaultUserAccountSubscriber.ts +31 -4
  62. package/src/accounts/types.ts +42 -5
  63. package/src/addresses.ts +35 -0
  64. package/src/admin.ts +86 -33
  65. package/src/clearingHouse.ts +340 -7
  66. package/src/clearingHouseUser.ts +154 -102
  67. package/src/config.ts +1 -1
  68. package/src/idl/clearing_house.json +895 -132
  69. package/src/index.ts +3 -0
  70. package/src/math/amm.ts +47 -14
  71. package/src/math/funding.ts +4 -3
  72. package/src/math/market.ts +28 -2
  73. package/src/math/orders.ts +39 -0
  74. package/src/math/position.ts +23 -3
  75. package/src/orderParams.ts +128 -0
  76. package/src/orders.ts +230 -0
  77. package/src/types.ts +124 -6
  78. package/tsconfig.json +0 -1
  79. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
  80. package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
  81. package/lib/accounts/types.d.ts.map +0 -1
  82. package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
  83. package/lib/addresses.d.ts.map +0 -1
  84. package/lib/admin.d.ts.map +0 -1
  85. package/lib/assert/assert.d.ts.map +0 -1
  86. package/lib/clearingHouse.d.ts.map +0 -1
  87. package/lib/clearingHouseUser.d.ts.map +0 -1
  88. package/lib/config.d.ts.map +0 -1
  89. package/lib/constants/markets.d.ts.map +0 -1
  90. package/lib/constants/numericConstants.d.ts.map +0 -1
  91. package/lib/examples/makeTradeExample.d.ts.map +0 -1
  92. package/lib/index.d.ts.map +0 -1
  93. package/lib/math/amm.d.ts.map +0 -1
  94. package/lib/math/conversion.d.ts.map +0 -1
  95. package/lib/math/funding.d.ts.map +0 -1
  96. package/lib/math/insuranceFund.d.ts.map +0 -1
  97. package/lib/math/market.d.ts.map +0 -1
  98. package/lib/math/position.d.ts.map +0 -1
  99. package/lib/math/trade.d.ts.map +0 -1
  100. package/lib/math/utils.d.ts.map +0 -1
  101. package/lib/mockUSDCFaucet.d.ts.map +0 -1
  102. package/lib/pythClient.d.ts.map +0 -1
  103. package/lib/tx/defaultTxSender.d.ts.map +0 -1
  104. package/lib/tx/types.d.ts.map +0 -1
  105. package/lib/tx/utils.d.ts.map +0 -1
  106. package/lib/types.d.ts.map +0 -1
  107. package/lib/util/computeUnits.d.ts.map +0 -1
  108. package/lib/util/tps.d.ts.map +0 -1
  109. package/lib/wallet.d.ts.map +0 -1
@@ -7,6 +7,7 @@ import {
7
7
  import {
8
8
  MarketsAccount,
9
9
  StateAccount,
10
+ CurveHistoryAccount,
10
11
  DepositHistoryAccount,
11
12
  FundingPaymentHistoryAccount,
12
13
  FundingRateHistoryAccount,
@@ -16,7 +17,10 @@ import {
16
17
  TradeHistoryAccount,
17
18
  UserAccount,
18
19
  Market,
19
- ExtendedCurveHistoryAccount,
20
+ OrderHistoryAccount,
21
+ OrderStateAccount,
22
+ OrderParams,
23
+ Order,
20
24
  } from './types';
21
25
  import * as anchor from '@project-serum/anchor';
22
26
  import clearingHouseIDL from './idl/clearing_house.json';
@@ -36,8 +40,11 @@ import { EventEmitter } from 'events';
36
40
  import StrictEventEmitter from 'strict-event-emitter-types';
37
41
  import {
38
42
  getClearingHouseStateAccountPublicKey,
43
+ getOrderStateAccountPublicKey,
39
44
  getUserAccountPublicKey,
40
45
  getUserAccountPublicKeyAndNonce,
46
+ getUserOrdersAccountPublicKey,
47
+ getUserOrdersAccountPublicKeyAndNonce,
41
48
  } from './addresses';
42
49
  import {
43
50
  ClearingHouseAccountSubscriber,
@@ -136,6 +143,7 @@ export class ClearingHouse {
136
143
  'fundingRateHistoryAccount',
137
144
  'liquidationHistoryAccount',
138
145
  'tradeHistoryAccount',
146
+ 'orderHistoryAccount',
139
147
  ]);
140
148
  }
141
149
 
@@ -200,10 +208,29 @@ export class ClearingHouse {
200
208
  return this.accountSubscriber.getDepositHistoryAccount();
201
209
  }
202
210
 
203
- public getCurveHistoryAccount(): ExtendedCurveHistoryAccount {
211
+ public getCurveHistoryAccount(): CurveHistoryAccount {
204
212
  return this.accountSubscriber.getCurveHistoryAccount();
205
213
  }
206
214
 
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
+
207
234
  /**
208
235
  * Update the wallet to use for clearing house transactions and linked user account
209
236
  * @param newWallet
@@ -232,9 +259,12 @@ export class ClearingHouse {
232
259
  userPositionsAccount,
233
260
  userAccountPublicKey,
234
261
  initializeUserAccountIx,
262
+ initializeUserOrdersAccountIx,
235
263
  ] = await this.getInitializeUserInstructions();
236
264
 
237
- const tx = new Transaction().add(initializeUserAccountIx);
265
+ const tx = new Transaction()
266
+ .add(initializeUserAccountIx)
267
+ .add(initializeUserOrdersAccountIx);
238
268
  const txSig = await this.txSender.send(
239
269
  tx,
240
270
  [userPositionsAccount],
@@ -244,9 +274,9 @@ export class ClearingHouse {
244
274
  }
245
275
 
246
276
  async getInitializeUserInstructions(): Promise<
247
- [Keypair, PublicKey, TransactionInstruction]
277
+ [Keypair, PublicKey, TransactionInstruction, TransactionInstruction]
248
278
  > {
249
- const [userPublicKey, userAccountNonce] =
279
+ const [userAccountPublicKey, userAccountNonce] =
250
280
  await getUserAccountPublicKeyAndNonce(
251
281
  this.program.programId,
252
282
  this.wallet.publicKey
@@ -280,7 +310,7 @@ export class ClearingHouse {
280
310
  optionalAccounts,
281
311
  {
282
312
  accounts: {
283
- user: userPublicKey,
313
+ user: userAccountPublicKey,
284
314
  authority: this.wallet.publicKey,
285
315
  rent: anchor.web3.SYSVAR_RENT_PUBKEY,
286
316
  systemProgram: anchor.web3.SystemProgram.programId,
@@ -290,7 +320,44 @@ export class ClearingHouse {
290
320
  remainingAccounts: remainingAccounts,
291
321
  }
292
322
  );
293
- return [userPositions, userPublicKey, initializeUserAccountIx];
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
+ );
294
361
  }
295
362
 
296
363
  userAccountPublicKey?: PublicKey;
@@ -322,6 +389,23 @@ export class ClearingHouse {
322
389
  return this.userAccount;
323
390
  }
324
391
 
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
+
325
409
  public async depositCollateral(
326
410
  amount: BN,
327
411
  collateralAccountPublicKey: PublicKey,
@@ -379,6 +463,7 @@ export class ClearingHouse {
379
463
  userPositionsAccount,
380
464
  userAccountPublicKey,
381
465
  initializeUserAccountIx,
466
+ initializeUserOrdersAccountIx,
382
467
  ] = await this.getInitializeUserInstructions();
383
468
 
384
469
  const depositCollateralIx = await this.getDepositCollateralInstruction(
@@ -389,6 +474,7 @@ export class ClearingHouse {
389
474
 
390
475
  const tx = new Transaction()
391
476
  .add(initializeUserAccountIx)
477
+ .add(initializeUserOrdersAccountIx)
392
478
  .add(depositCollateralIx);
393
479
 
394
480
  const txSig = await this.program.provider.send(tx, [userPositionsAccount]);
@@ -410,6 +496,7 @@ export class ClearingHouse {
410
496
  userPositionsAccount,
411
497
  userAccountPublicKey,
412
498
  initializeUserAccountIx,
499
+ initializeUserOrdersAccountIx,
413
500
  ] = await this.getInitializeUserInstructions();
414
501
 
415
502
  const depositCollateralIx = await this.getDepositCollateralInstruction(
@@ -422,6 +509,7 @@ export class ClearingHouse {
422
509
  .add(createAssociatedAccountIx)
423
510
  .add(mintToIx)
424
511
  .add(initializeUserAccountIx)
512
+ .add(initializeUserOrdersAccountIx)
425
513
  .add(depositCollateralIx);
426
514
 
427
515
  const txSig = await this.program.provider.send(tx, [userPositionsAccount]);
@@ -572,6 +660,251 @@ export class ClearingHouse {
572
660
  );
573
661
  }
574
662
 
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
+
575
908
  /**
576
909
  * Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
577
910
  * @param marketIndex