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

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 (136) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +32 -0
  2. package/lib/accounts/bulkAccountLoader.js +156 -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 +133 -0
  11. package/lib/accounts/types.d.ts +34 -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 +0 -1
  15. package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +8 -3
  16. package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +30 -4
  17. package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +6 -3
  18. package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +16 -4
  19. package/lib/addresses.d.ts +4 -1
  20. package/lib/addresses.js +28 -1
  21. package/lib/admin.d.ts +11 -4
  22. package/lib/admin.js +71 -17
  23. package/lib/assert/assert.d.ts +0 -1
  24. package/lib/clearingHouse.d.ts +35 -4
  25. package/lib/clearingHouse.js +294 -23
  26. package/lib/clearingHouseUser.d.ts +19 -18
  27. package/lib/clearingHouseUser.js +129 -101
  28. package/lib/config.d.ts +0 -1
  29. package/lib/config.js +1 -1
  30. package/lib/constants/markets.d.ts +0 -1
  31. package/lib/constants/markets.js +13 -5
  32. package/lib/constants/numericConstants.d.ts +0 -1
  33. package/lib/examples/makeTradeExample.d.ts +0 -1
  34. package/lib/examples/makeTradeExample.js +6 -6
  35. package/lib/factory/clearingHouse.d.ts +25 -0
  36. package/lib/factory/clearingHouse.js +64 -0
  37. package/lib/factory/clearingHouseUser.d.ts +19 -0
  38. package/lib/factory/clearingHouseUser.js +34 -0
  39. package/lib/idl/clearing_house.json +1077 -54
  40. package/lib/index.d.ts +12 -4
  41. package/lib/index.js +17 -4
  42. package/lib/math/amm.d.ts +1 -1
  43. package/lib/math/amm.js +38 -15
  44. package/lib/math/conversion.d.ts +0 -1
  45. package/lib/math/conversion.js +1 -1
  46. package/lib/math/funding.d.ts +0 -1
  47. package/lib/math/funding.js +1 -1
  48. package/lib/math/insuranceFund.d.ts +0 -1
  49. package/lib/math/market.d.ts +2 -2
  50. package/lib/math/market.js +12 -2
  51. package/lib/math/orders.d.ts +3 -0
  52. package/lib/math/orders.js +30 -0
  53. package/lib/math/position.d.ts +4 -2
  54. package/lib/math/position.js +19 -5
  55. package/lib/math/trade.d.ts +0 -1
  56. package/lib/math/trade.js +16 -16
  57. package/lib/math/utils.d.ts +2 -3
  58. package/lib/math/utils.js +6 -3
  59. package/lib/mockUSDCFaucet.d.ts +0 -1
  60. package/lib/orderParams.d.ts +7 -0
  61. package/lib/orderParams.js +108 -0
  62. package/lib/orders.d.ts +5 -0
  63. package/lib/orders.js +136 -0
  64. package/lib/pythClient.d.ts +0 -1
  65. package/lib/pythClient.js +1 -1
  66. package/lib/token/index.d.ts +3 -0
  67. package/lib/token/index.js +38 -0
  68. package/lib/tx/defaultTxSender.d.ts +0 -1
  69. package/lib/tx/types.d.ts +0 -1
  70. package/lib/tx/utils.d.ts +0 -1
  71. package/lib/types.d.ts +144 -2
  72. package/lib/types.js +36 -1
  73. package/lib/util/computeUnits.d.ts +0 -1
  74. package/lib/util/tps.d.ts +0 -1
  75. package/lib/wallet.d.ts +0 -1
  76. package/package.json +3 -3
  77. package/src/accounts/bulkAccountLoader.ts +195 -0
  78. package/src/accounts/bulkUserSubscription.ts +28 -0
  79. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +326 -0
  80. package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
  81. package/src/accounts/pollingUserAccountSubscriber.ts +186 -0
  82. package/src/accounts/types.ts +43 -1
  83. package/src/accounts/utils.ts +3 -0
  84. package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +51 -1
  85. package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +33 -3
  86. package/src/addresses.ts +37 -0
  87. package/src/admin.ts +111 -24
  88. package/src/clearingHouse.ts +399 -22
  89. package/src/clearingHouseUser.ts +179 -107
  90. package/src/config.ts +1 -1
  91. package/src/constants/markets.ts +13 -5
  92. package/src/factory/clearingHouse.ts +125 -0
  93. package/src/factory/clearingHouseUser.ts +73 -0
  94. package/src/idl/clearing_house.json +1077 -54
  95. package/src/index.ts +12 -3
  96. package/src/math/amm.ts +47 -14
  97. package/src/math/market.ts +28 -2
  98. package/src/math/orders.ts +39 -0
  99. package/src/math/position.ts +23 -3
  100. package/src/math/utils.ts +1 -1
  101. package/src/orderParams.ts +151 -0
  102. package/src/orders.ts +230 -0
  103. package/src/token/index.ts +37 -0
  104. package/src/types.ts +128 -1
  105. package/tsconfig.json +0 -1
  106. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
  107. package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
  108. package/lib/accounts/types.d.ts.map +0 -1
  109. package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
  110. package/lib/addresses.d.ts.map +0 -1
  111. package/lib/admin.d.ts.map +0 -1
  112. package/lib/assert/assert.d.ts.map +0 -1
  113. package/lib/clearingHouse.d.ts.map +0 -1
  114. package/lib/clearingHouseUser.d.ts.map +0 -1
  115. package/lib/config.d.ts.map +0 -1
  116. package/lib/constants/markets.d.ts.map +0 -1
  117. package/lib/constants/numericConstants.d.ts.map +0 -1
  118. package/lib/examples/makeTradeExample.d.ts.map +0 -1
  119. package/lib/index.d.ts.map +0 -1
  120. package/lib/math/amm.d.ts.map +0 -1
  121. package/lib/math/conversion.d.ts.map +0 -1
  122. package/lib/math/funding.d.ts.map +0 -1
  123. package/lib/math/insuranceFund.d.ts.map +0 -1
  124. package/lib/math/market.d.ts.map +0 -1
  125. package/lib/math/position.d.ts.map +0 -1
  126. package/lib/math/trade.d.ts.map +0 -1
  127. package/lib/math/utils.d.ts.map +0 -1
  128. package/lib/mockUSDCFaucet.d.ts.map +0 -1
  129. package/lib/pythClient.d.ts.map +0 -1
  130. package/lib/tx/defaultTxSender.d.ts.map +0 -1
  131. package/lib/tx/types.d.ts.map +0 -1
  132. package/lib/tx/utils.d.ts.map +0 -1
  133. package/lib/types.d.ts.map +0 -1
  134. package/lib/util/computeUnits.d.ts.map +0 -1
  135. package/lib/util/tps.d.ts.map +0 -1
  136. package/lib/wallet.d.ts.map +0 -1
@@ -16,6 +16,10 @@ import {
16
16
  TradeHistoryAccount,
17
17
  UserAccount,
18
18
  Market,
19
+ OrderHistoryAccount,
20
+ OrderStateAccount,
21
+ OrderParams,
22
+ Order,
19
23
  ExtendedCurveHistoryAccount,
20
24
  } from './types';
21
25
  import * as anchor from '@project-serum/anchor';
@@ -36,24 +40,30 @@ 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,
44
51
  ClearingHouseAccountEvents,
45
52
  ClearingHouseAccountTypes,
46
53
  } from './accounts/types';
47
- import { DefaultClearingHouseAccountSubscriber } from './accounts/defaultClearingHouseAccountSubscriber';
48
54
  import { TxSender } from './tx/types';
49
55
  import { DefaultTxSender } from './tx/defaultTxSender';
50
56
  import { wrapInTx } from './tx/utils';
57
+ import {
58
+ getClearingHouse,
59
+ getWebSocketClearingHouseConfig,
60
+ } from './factory/clearingHouse';
51
61
 
52
62
  /**
53
63
  * # ClearingHouse
54
64
  * This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
55
65
  *
56
- * The default way to construct a ClearingHouse instance is using the {@link from} method. This will create an instance using the static {@link DefaultClearingHouseAccountSubscriber}, which will use a websocket for each state account subscription.
66
+ * The default way to construct a ClearingHouse instance is using the {@link from} method. This will create an instance using the static {@link WebSocketClearingHouseAccountSubscriber}, which will use a websocket for each state account subscription.
57
67
  * Alternatively, if you want to implement your own method of subscribing to the state accounts on the blockchain, you can implement a {@link ClearingHouseAccountSubscriber} and use it in the {@link ClearingHouse.constructor}
58
68
  */
59
69
  export class ClearingHouse {
@@ -64,33 +74,38 @@ export class ClearingHouse {
64
74
  opts?: ConfirmOptions;
65
75
  accountSubscriber: ClearingHouseAccountSubscriber;
66
76
  eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
67
- isSubscribed = false;
77
+ _isSubscribed = false;
68
78
  txSender: TxSender;
69
79
 
80
+ public get isSubscribed() {
81
+ return this._isSubscribed && this.accountSubscriber.isSubscribed;
82
+ }
83
+
84
+ public set isSubscribed(val: boolean) {
85
+ this._isSubscribed = val;
86
+ }
87
+
88
+ /**
89
+ * @deprecated You should use the getClearingHouse factory method instead
90
+ * @param connection
91
+ * @param wallet
92
+ * @param clearingHouseProgramId
93
+ * @param opts
94
+ * @returns
95
+ */
70
96
  public static from(
71
97
  connection: Connection,
72
98
  wallet: IWallet,
73
99
  clearingHouseProgramId: PublicKey,
74
100
  opts: ConfirmOptions = Provider.defaultOptions()
75
101
  ): ClearingHouse {
76
- const provider = new Provider(connection, wallet, opts);
77
- const program = new Program(
78
- clearingHouseIDL as Idl,
79
- clearingHouseProgramId,
80
- provider
81
- );
82
- const accountSubscriber = new DefaultClearingHouseAccountSubscriber(
83
- program
84
- );
85
- const txSender = new DefaultTxSender(provider);
86
- return new ClearingHouse(
102
+ const config = getWebSocketClearingHouseConfig(
87
103
  connection,
88
104
  wallet,
89
- program,
90
- accountSubscriber,
91
- txSender,
105
+ clearingHouseProgramId,
92
106
  opts
93
107
  );
108
+ return getClearingHouse(config);
94
109
  }
95
110
 
96
111
  public constructor(
@@ -136,6 +151,7 @@ export class ClearingHouse {
136
151
  'fundingRateHistoryAccount',
137
152
  'liquidationHistoryAccount',
138
153
  'tradeHistoryAccount',
154
+ 'orderHistoryAccount',
139
155
  ]);
140
156
  }
141
157
 
@@ -204,6 +220,25 @@ export class ClearingHouse {
204
220
  return this.accountSubscriber.getCurveHistoryAccount();
205
221
  }
206
222
 
223
+ public getOrderHistoryAccount(): OrderHistoryAccount {
224
+ return this.accountSubscriber.getOrderHistoryAccount();
225
+ }
226
+
227
+ orderStatePublicKey?: PublicKey;
228
+ public async getOrderStatePublicKey(): Promise<PublicKey> {
229
+ if (this.orderStatePublicKey) {
230
+ return this.orderStatePublicKey;
231
+ }
232
+ this.orderStatePublicKey = await getOrderStateAccountPublicKey(
233
+ this.program.programId
234
+ );
235
+ return this.orderStatePublicKey;
236
+ }
237
+
238
+ public getOrderStateAccount(): OrderStateAccount {
239
+ return this.accountSubscriber.getOrderStateAccount();
240
+ }
241
+
207
242
  /**
208
243
  * Update the wallet to use for clearing house transactions and linked user account
209
244
  * @param newWallet
@@ -232,9 +267,12 @@ export class ClearingHouse {
232
267
  userPositionsAccount,
233
268
  userAccountPublicKey,
234
269
  initializeUserAccountIx,
270
+ initializeUserOrdersAccountIx,
235
271
  ] = await this.getInitializeUserInstructions();
236
272
 
237
- const tx = new Transaction().add(initializeUserAccountIx);
273
+ const tx = new Transaction()
274
+ .add(initializeUserAccountIx)
275
+ .add(initializeUserOrdersAccountIx);
238
276
  const txSig = await this.txSender.send(
239
277
  tx,
240
278
  [userPositionsAccount],
@@ -244,9 +282,9 @@ export class ClearingHouse {
244
282
  }
245
283
 
246
284
  async getInitializeUserInstructions(): Promise<
247
- [Keypair, PublicKey, TransactionInstruction]
285
+ [Keypair, PublicKey, TransactionInstruction, TransactionInstruction]
248
286
  > {
249
- const [userPublicKey, userAccountNonce] =
287
+ const [userAccountPublicKey, userAccountNonce] =
250
288
  await getUserAccountPublicKeyAndNonce(
251
289
  this.program.programId,
252
290
  this.wallet.publicKey
@@ -280,7 +318,7 @@ export class ClearingHouse {
280
318
  optionalAccounts,
281
319
  {
282
320
  accounts: {
283
- user: userPublicKey,
321
+ user: userAccountPublicKey,
284
322
  authority: this.wallet.publicKey,
285
323
  rent: anchor.web3.SYSVAR_RENT_PUBKEY,
286
324
  systemProgram: anchor.web3.SystemProgram.programId,
@@ -290,7 +328,44 @@ export class ClearingHouse {
290
328
  remainingAccounts: remainingAccounts,
291
329
  }
292
330
  );
293
- return [userPositions, userPublicKey, initializeUserAccountIx];
331
+
332
+ const initializeUserOrdersAccountIx =
333
+ await this.getInitializeUserOrdersInstruction(userAccountPublicKey);
334
+
335
+ return [
336
+ userPositions,
337
+ userAccountPublicKey,
338
+ initializeUserAccountIx,
339
+ initializeUserOrdersAccountIx,
340
+ ];
341
+ }
342
+
343
+ async getInitializeUserOrdersInstruction(
344
+ userAccountPublicKey?: PublicKey
345
+ ): Promise<TransactionInstruction> {
346
+ if (!userAccountPublicKey) {
347
+ userAccountPublicKey = await this.getUserAccountPublicKey();
348
+ }
349
+
350
+ const [userOrdersAccountPublicKey, userOrdersAccountNonce] =
351
+ await getUserOrdersAccountPublicKeyAndNonce(
352
+ this.program.programId,
353
+ userAccountPublicKey
354
+ );
355
+
356
+ return await this.program.instruction.initializeUserOrders(
357
+ userOrdersAccountNonce,
358
+ {
359
+ accounts: {
360
+ user: userAccountPublicKey,
361
+ authority: this.wallet.publicKey,
362
+ rent: anchor.web3.SYSVAR_RENT_PUBKEY,
363
+ systemProgram: anchor.web3.SystemProgram.programId,
364
+ userOrders: userOrdersAccountPublicKey,
365
+ state: await this.getStatePublicKey(),
366
+ },
367
+ }
368
+ );
294
369
  }
295
370
 
296
371
  userAccountPublicKey?: PublicKey;
@@ -322,6 +397,23 @@ export class ClearingHouse {
322
397
  return this.userAccount;
323
398
  }
324
399
 
400
+ userOrdersAccountPublicKey?: PublicKey;
401
+ /**
402
+ * Get the address for the Clearing House User Order's account. NOT the user's wallet address.
403
+ * @returns
404
+ */
405
+ public async getUserOrdersAccountPublicKey(): Promise<PublicKey> {
406
+ if (this.userOrdersAccountPublicKey) {
407
+ return this.userOrdersAccountPublicKey;
408
+ }
409
+
410
+ this.userOrdersAccountPublicKey = await getUserOrdersAccountPublicKey(
411
+ this.program.programId,
412
+ await this.getUserAccountPublicKey()
413
+ );
414
+ return this.userOrdersAccountPublicKey;
415
+ }
416
+
325
417
  public async depositCollateral(
326
418
  amount: BN,
327
419
  collateralAccountPublicKey: PublicKey,
@@ -379,6 +471,7 @@ export class ClearingHouse {
379
471
  userPositionsAccount,
380
472
  userAccountPublicKey,
381
473
  initializeUserAccountIx,
474
+ initializeUserOrdersAccountIx,
382
475
  ] = await this.getInitializeUserInstructions();
383
476
 
384
477
  const depositCollateralIx = await this.getDepositCollateralInstruction(
@@ -389,6 +482,7 @@ export class ClearingHouse {
389
482
 
390
483
  const tx = new Transaction()
391
484
  .add(initializeUserAccountIx)
485
+ .add(initializeUserOrdersAccountIx)
392
486
  .add(depositCollateralIx);
393
487
 
394
488
  const txSig = await this.program.provider.send(tx, [userPositionsAccount]);
@@ -410,6 +504,7 @@ export class ClearingHouse {
410
504
  userPositionsAccount,
411
505
  userAccountPublicKey,
412
506
  initializeUserAccountIx,
507
+ initializeUserOrdersAccountIx,
413
508
  ] = await this.getInitializeUserInstructions();
414
509
 
415
510
  const depositCollateralIx = await this.getDepositCollateralInstruction(
@@ -422,6 +517,7 @@ export class ClearingHouse {
422
517
  .add(createAssociatedAccountIx)
423
518
  .add(mintToIx)
424
519
  .add(initializeUserAccountIx)
520
+ .add(initializeUserOrdersAccountIx)
425
521
  .add(depositCollateralIx);
426
522
 
427
523
  const txSig = await this.program.provider.send(tx, [userPositionsAccount]);
@@ -572,6 +668,287 @@ export class ClearingHouse {
572
668
  );
573
669
  }
574
670
 
671
+ public async placeOrder(
672
+ orderParams: OrderParams,
673
+ discountToken?: PublicKey,
674
+ referrer?: PublicKey
675
+ ): Promise<TransactionSignature> {
676
+ return await this.txSender.send(
677
+ wrapInTx(
678
+ await this.getPlaceOrderIx(orderParams, discountToken, referrer)
679
+ ),
680
+ [],
681
+ this.opts
682
+ );
683
+ }
684
+
685
+ public async getPlaceOrderIx(
686
+ orderParams: OrderParams,
687
+ discountToken?: PublicKey,
688
+ referrer?: PublicKey
689
+ ): Promise<TransactionInstruction> {
690
+ const userAccountPublicKey = await this.getUserAccountPublicKey();
691
+ const userAccount = await this.getUserAccount();
692
+
693
+ const priceOracle =
694
+ this.getMarketsAccount().markets[orderParams.marketIndex.toNumber()].amm
695
+ .oracle;
696
+
697
+ const remainingAccounts = [];
698
+ if (orderParams.optionalAccounts.discountToken) {
699
+ if (!discountToken) {
700
+ throw Error(
701
+ 'Optional accounts specified discount token but no discount token present'
702
+ );
703
+ }
704
+
705
+ remainingAccounts.push({
706
+ pubkey: discountToken,
707
+ isWritable: false,
708
+ isSigner: false,
709
+ });
710
+ }
711
+
712
+ if (orderParams.optionalAccounts.referrer) {
713
+ if (!referrer) {
714
+ throw Error(
715
+ 'Optional accounts specified referrer but no referrer present'
716
+ );
717
+ }
718
+
719
+ remainingAccounts.push({
720
+ pubkey: referrer,
721
+ isWritable: false,
722
+ isSigner: false,
723
+ });
724
+ }
725
+
726
+ const state = this.getStateAccount();
727
+ const orderState = this.getOrderStateAccount();
728
+ return await this.program.instruction.placeOrder(orderParams, {
729
+ accounts: {
730
+ state: await this.getStatePublicKey(),
731
+ user: userAccountPublicKey,
732
+ authority: this.wallet.publicKey,
733
+ markets: state.markets,
734
+ userOrders: await this.getUserOrdersAccountPublicKey(),
735
+ userPositions: userAccount.positions,
736
+ fundingPaymentHistory: state.fundingPaymentHistory,
737
+ fundingRateHistory: state.fundingRateHistory,
738
+ orderState: await this.getOrderStatePublicKey(),
739
+ orderHistory: orderState.orderHistory,
740
+ oracle: priceOracle,
741
+ },
742
+ remainingAccounts,
743
+ });
744
+ }
745
+
746
+ public async cancelOrder(orderId: BN): Promise<TransactionSignature> {
747
+ return await this.txSender.send(
748
+ wrapInTx(await this.getCancelOrderIx(orderId)),
749
+ [],
750
+ this.opts
751
+ );
752
+ }
753
+
754
+ public async getCancelOrderIx(orderId: BN): Promise<TransactionInstruction> {
755
+ const userAccountPublicKey = await this.getUserAccountPublicKey();
756
+ const userAccount = await this.getUserAccount();
757
+
758
+ const state = this.getStateAccount();
759
+ const orderState = this.getOrderStateAccount();
760
+ return await this.program.instruction.cancelOrder(orderId, {
761
+ accounts: {
762
+ state: await this.getStatePublicKey(),
763
+ user: userAccountPublicKey,
764
+ authority: this.wallet.publicKey,
765
+ markets: state.markets,
766
+ userOrders: await this.getUserOrdersAccountPublicKey(),
767
+ userPositions: userAccount.positions,
768
+ fundingPaymentHistory: state.fundingPaymentHistory,
769
+ fundingRateHistory: state.fundingRateHistory,
770
+ orderState: await this.getOrderStatePublicKey(),
771
+ orderHistory: orderState.orderHistory,
772
+ },
773
+ });
774
+ }
775
+
776
+ public async cancelOrderByUserId(
777
+ userOrderId: number
778
+ ): Promise<TransactionSignature> {
779
+ return await this.txSender.send(
780
+ wrapInTx(await this.getCancelOrderByUserIdIx(userOrderId)),
781
+ [],
782
+ this.opts
783
+ );
784
+ }
785
+
786
+ public async getCancelOrderByUserIdIx(
787
+ userOrderId: number
788
+ ): Promise<TransactionInstruction> {
789
+ const userAccountPublicKey = await this.getUserAccountPublicKey();
790
+ const userAccount = await this.getUserAccount();
791
+
792
+ const state = this.getStateAccount();
793
+ const orderState = this.getOrderStateAccount();
794
+ return await this.program.instruction.cancelOrderByUserId(userOrderId, {
795
+ accounts: {
796
+ state: await this.getStatePublicKey(),
797
+ user: userAccountPublicKey,
798
+ authority: this.wallet.publicKey,
799
+ markets: state.markets,
800
+ userOrders: await this.getUserOrdersAccountPublicKey(),
801
+ userPositions: userAccount.positions,
802
+ fundingPaymentHistory: state.fundingPaymentHistory,
803
+ fundingRateHistory: state.fundingRateHistory,
804
+ orderState: await this.getOrderStatePublicKey(),
805
+ orderHistory: orderState.orderHistory,
806
+ },
807
+ });
808
+ }
809
+
810
+ public async fillOrder(
811
+ userAccountPublicKey: PublicKey,
812
+ userOrdersAccountPublicKey: PublicKey,
813
+ order: Order
814
+ ): Promise<TransactionSignature> {
815
+ return await this.txSender.send(
816
+ wrapInTx(
817
+ await this.getFillOrderIx(
818
+ userAccountPublicKey,
819
+ userOrdersAccountPublicKey,
820
+ order
821
+ )
822
+ ),
823
+ [],
824
+ this.opts
825
+ );
826
+ }
827
+
828
+ public async getFillOrderIx(
829
+ userAccountPublicKey: PublicKey,
830
+ userOrdersAccountPublicKey: PublicKey,
831
+ order: Order
832
+ ): Promise<TransactionInstruction> {
833
+ const fillerPublicKey = await this.getUserAccountPublicKey();
834
+ const userAccount: any = await this.program.account.user.fetch(
835
+ userAccountPublicKey
836
+ );
837
+
838
+ const marketIndex = order.marketIndex;
839
+ const oracle = this.getMarket(marketIndex).amm.oracle;
840
+
841
+ const state = this.getStateAccount();
842
+ const orderState = this.getOrderStateAccount();
843
+
844
+ const remainingAccounts = [];
845
+ if (!order.referrer.equals(PublicKey.default)) {
846
+ remainingAccounts.push({
847
+ pubkey: order.referrer,
848
+ isWritable: true,
849
+ isSigner: false,
850
+ });
851
+ }
852
+
853
+ const orderId = order.orderId;
854
+ return await this.program.instruction.fillOrder(orderId, {
855
+ accounts: {
856
+ state: await this.getStatePublicKey(),
857
+ filler: fillerPublicKey,
858
+ user: userAccountPublicKey,
859
+ authority: this.wallet.publicKey,
860
+ markets: state.markets,
861
+ userPositions: userAccount.positions,
862
+ userOrders: userOrdersAccountPublicKey,
863
+ tradeHistory: state.tradeHistory,
864
+ fundingPaymentHistory: state.fundingPaymentHistory,
865
+ fundingRateHistory: state.fundingRateHistory,
866
+ orderState: await this.getOrderStatePublicKey(),
867
+ orderHistory: orderState.orderHistory,
868
+ extendedCurveHistory: state.extendedCurveHistory,
869
+ oracle: oracle,
870
+ },
871
+ remainingAccounts,
872
+ });
873
+ }
874
+
875
+ public async placeAndFillOrder(
876
+ orderParams: OrderParams,
877
+ discountToken?: PublicKey,
878
+ referrer?: PublicKey
879
+ ): Promise<TransactionSignature> {
880
+ return await this.txSender.send(
881
+ wrapInTx(
882
+ await this.getPlaceAndFillOrderIx(orderParams, discountToken, referrer)
883
+ ),
884
+ [],
885
+ this.opts
886
+ );
887
+ }
888
+
889
+ public async getPlaceAndFillOrderIx(
890
+ orderParams: OrderParams,
891
+ discountToken?: PublicKey,
892
+ referrer?: PublicKey
893
+ ): Promise<TransactionInstruction> {
894
+ const userAccountPublicKey = await this.getUserAccountPublicKey();
895
+ const userAccount = await this.getUserAccount();
896
+
897
+ const priceOracle =
898
+ this.getMarketsAccount().markets[orderParams.marketIndex.toNumber()].amm
899
+ .oracle;
900
+
901
+ const remainingAccounts = [];
902
+ if (orderParams.optionalAccounts.discountToken) {
903
+ if (!discountToken) {
904
+ throw Error(
905
+ 'Optional accounts specified discount token but no discount token present'
906
+ );
907
+ }
908
+
909
+ remainingAccounts.push({
910
+ pubkey: discountToken,
911
+ isWritable: false,
912
+ isSigner: false,
913
+ });
914
+ }
915
+
916
+ if (orderParams.optionalAccounts.referrer) {
917
+ if (!referrer) {
918
+ throw Error(
919
+ 'Optional accounts specified referrer but no referrer present'
920
+ );
921
+ }
922
+
923
+ remainingAccounts.push({
924
+ pubkey: referrer,
925
+ isWritable: true,
926
+ isSigner: false,
927
+ });
928
+ }
929
+
930
+ const state = this.getStateAccount();
931
+ const orderState = this.getOrderStateAccount();
932
+ return await this.program.instruction.placeAndFillOrder(orderParams, {
933
+ accounts: {
934
+ state: await this.getStatePublicKey(),
935
+ user: userAccountPublicKey,
936
+ authority: this.wallet.publicKey,
937
+ markets: state.markets,
938
+ userOrders: await this.getUserOrdersAccountPublicKey(),
939
+ userPositions: userAccount.positions,
940
+ tradeHistory: state.tradeHistory,
941
+ fundingPaymentHistory: state.fundingPaymentHistory,
942
+ fundingRateHistory: state.fundingRateHistory,
943
+ orderState: await this.getOrderStatePublicKey(),
944
+ orderHistory: orderState.orderHistory,
945
+ extendedCurveHistory: state.extendedCurveHistory,
946
+ oracle: priceOracle,
947
+ },
948
+ remainingAccounts,
949
+ });
950
+ }
951
+
575
952
  /**
576
953
  * 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
954
  * @param marketIndex