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

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 (112) 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 +2 -27
  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 +5 -14
  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 +7 -10
  16. package/lib/admin.d.ts.map +1 -0
  17. package/lib/admin.js +64 -53
  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 +15 -246
  23. package/lib/clearingHouseUser.d.ts +17 -10
  24. package/lib/clearingHouseUser.d.ts.map +1 -0
  25. package/lib/clearingHouseUser.js +97 -111
  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/examples/makeTradeExample.js +6 -6
  37. package/lib/idl/clearing_house.json +233 -911
  38. package/lib/index.d.ts +1 -3
  39. package/lib/index.d.ts.map +1 -0
  40. package/lib/index.js +0 -3
  41. package/lib/math/amm.d.ts +1 -1
  42. package/lib/math/amm.d.ts.map +1 -0
  43. package/lib/math/amm.js +15 -38
  44. package/lib/math/conversion.d.ts +1 -0
  45. package/lib/math/conversion.d.ts.map +1 -0
  46. package/lib/math/conversion.js +1 -1
  47. package/lib/math/funding.d.ts +1 -0
  48. package/lib/math/funding.d.ts.map +1 -0
  49. package/lib/math/funding.js +5 -2
  50. package/lib/math/insuranceFund.d.ts +1 -0
  51. package/lib/math/insuranceFund.d.ts.map +1 -0
  52. package/lib/math/market.d.ts +2 -2
  53. package/lib/math/market.d.ts.map +1 -0
  54. package/lib/math/market.js +2 -12
  55. package/lib/math/position.d.ts +2 -4
  56. package/lib/math/position.d.ts.map +1 -0
  57. package/lib/math/position.js +5 -19
  58. package/lib/math/trade.d.ts +1 -0
  59. package/lib/math/trade.d.ts.map +1 -0
  60. package/lib/math/trade.js +16 -16
  61. package/lib/math/utils.d.ts +1 -0
  62. package/lib/math/utils.d.ts.map +1 -0
  63. package/lib/mockUSDCFaucet.d.ts +1 -0
  64. package/lib/mockUSDCFaucet.d.ts.map +1 -0
  65. package/lib/pythClient.d.ts +1 -0
  66. package/lib/pythClient.d.ts.map +1 -0
  67. package/lib/pythClient.js +1 -1
  68. package/lib/tx/defaultTxSender.d.ts +1 -0
  69. package/lib/tx/defaultTxSender.d.ts.map +1 -0
  70. package/lib/tx/types.d.ts +1 -0
  71. package/lib/tx/types.d.ts.map +1 -0
  72. package/lib/tx/utils.d.ts +1 -0
  73. package/lib/tx/utils.d.ts.map +1 -0
  74. package/lib/types.d.ts +9 -140
  75. package/lib/types.d.ts.map +1 -0
  76. package/lib/types.js +1 -36
  77. package/lib/util/computeUnits.d.ts +1 -0
  78. package/lib/util/computeUnits.d.ts.map +1 -0
  79. package/lib/util/tps.d.ts +1 -0
  80. package/lib/util/tps.d.ts.map +1 -0
  81. package/lib/wallet.d.ts +1 -0
  82. package/lib/wallet.d.ts.map +1 -0
  83. package/package.json +1 -1
  84. package/src/accounts/defaultClearingHouseAccountSubscriber.ts +5 -52
  85. package/src/accounts/defaultUserAccountSubscriber.ts +6 -31
  86. package/src/accounts/types.ts +5 -42
  87. package/src/addresses.ts +0 -35
  88. package/src/admin.ts +71 -86
  89. package/src/clearingHouse.ts +7 -340
  90. package/src/clearingHouseUser.ts +102 -154
  91. package/src/config.ts +1 -1
  92. package/src/constants/markets.ts +16 -0
  93. package/src/idl/clearing_house.json +233 -911
  94. package/src/index.ts +0 -3
  95. package/src/math/amm.ts +14 -47
  96. package/src/math/funding.ts +5 -1
  97. package/src/math/market.ts +2 -28
  98. package/src/math/position.ts +3 -23
  99. package/src/types.ts +8 -124
  100. package/tsconfig.json +1 -0
  101. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -28
  102. package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
  103. package/lib/math/orders.d.ts +0 -3
  104. package/lib/math/orders.js +0 -30
  105. package/lib/orderParams.d.ts +0 -7
  106. package/lib/orderParams.js +0 -88
  107. package/lib/orders.d.ts +0 -5
  108. package/lib/orders.js +0 -136
  109. package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -176
  110. package/src/math/orders.ts +0 -39
  111. package/src/orderParams.ts +0 -128
  112. package/src/orders.ts +0 -230
package/src/admin.ts CHANGED
@@ -5,20 +5,10 @@ import {
5
5
  SYSVAR_RENT_PUBKEY,
6
6
  TransactionSignature,
7
7
  } from '@solana/web3.js';
8
- import {
9
- FeeStructure,
10
- IWallet,
11
- OracleGuardRails,
12
- OracleSource,
13
- OrderFillerRewardStructure,
14
- } from './types';
8
+ import { FeeStructure, IWallet, OracleGuardRails, OracleSource } from './types';
15
9
  import { BN, Idl, Program, Provider } from '@project-serum/anchor';
16
10
  import * as anchor from '@project-serum/anchor';
17
- import {
18
- getClearingHouseStateAccountPublicKey,
19
- getClearingHouseStateAccountPublicKeyAndNonce,
20
- getOrderStateAccountPublicKeyAndNonce,
21
- } from './addresses';
11
+ import { getClearingHouseStateAccountPublicKeyAndNonce } from './addresses';
22
12
  import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
23
13
  import { ClearingHouse } from './clearingHouse';
24
14
  import { PEG_PRECISION } from './constants/numericConstants';
@@ -58,9 +48,7 @@ export class Admin extends ClearingHouse {
58
48
  public async initialize(
59
49
  usdcMint: PublicKey,
60
50
  adminControlsPrices: boolean
61
- ): Promise<
62
- [TransactionSignature, TransactionSignature, TransactionSignature]
63
- > {
51
+ ): Promise<[TransactionSignature, TransactionSignature]> {
64
52
  const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
65
53
  await this.getStatePublicKey()
66
54
  );
@@ -165,7 +153,7 @@ export class Admin extends ClearingHouse {
165
153
  await this.program.account.depositHistory.createInstruction(
166
154
  depositHistory
167
155
  ),
168
- await this.program.account.curveHistory.createInstruction(
156
+ await this.program.account.extendedCurveHistory.createInstruction(
169
157
  curveHistory
170
158
  ),
171
159
  ],
@@ -184,40 +172,7 @@ export class Admin extends ClearingHouse {
184
172
  this.opts
185
173
  );
186
174
 
187
- const initializeOrderStateTxSig = await this.initializeOrderState();
188
-
189
- return [initializeTxSig, initializeHistoryTxSig, initializeOrderStateTxSig];
190
- }
191
-
192
- public async initializeOrderState(): Promise<TransactionSignature> {
193
- const orderHistory = anchor.web3.Keypair.generate();
194
- const [orderStatePublicKey, orderStateNonce] =
195
- await getOrderStateAccountPublicKeyAndNonce(this.program.programId);
196
- const clearingHouseStatePublicKey =
197
- await getClearingHouseStateAccountPublicKey(this.program.programId);
198
-
199
- const initializeOrderStateTx =
200
- await this.program.transaction.initializeOrderState(orderStateNonce, {
201
- accounts: {
202
- admin: this.wallet.publicKey,
203
- state: clearingHouseStatePublicKey,
204
- orderHistory: orderHistory.publicKey,
205
- orderState: orderStatePublicKey,
206
- rent: SYSVAR_RENT_PUBKEY,
207
- systemProgram: anchor.web3.SystemProgram.programId,
208
- },
209
- instructions: [
210
- await this.program.account.orderHistory.createInstruction(
211
- orderHistory
212
- ),
213
- ],
214
- });
215
-
216
- return await this.txSender.send(
217
- initializeOrderStateTx,
218
- [orderHistory],
219
- this.opts
220
- );
175
+ return [initializeTxSig, initializeHistoryTxSig];
221
176
  }
222
177
 
223
178
  public async initializeMarket(
@@ -275,13 +230,38 @@ export class Admin extends ClearingHouse {
275
230
  marketIndex: BN
276
231
  ): Promise<TransactionSignature> {
277
232
  const state = this.getStateAccount();
233
+ const markets = this.getMarketsAccount();
234
+ const marketData = markets.markets[marketIndex.toNumber()];
235
+ const ammData = marketData.amm;
236
+
278
237
  return await this.program.rpc.updateK(sqrtK, marketIndex, {
279
238
  accounts: {
280
239
  state: await this.getStatePublicKey(),
281
240
  admin: this.wallet.publicKey,
282
241
  markets: state.markets,
242
+ curveHistory: state.extendedCurveHistory,
243
+ oracle: ammData.oracle,
244
+ },
245
+ });
246
+ }
247
+
248
+ public async updateCurveHistory(): Promise<TransactionSignature> {
249
+ const extendedCurveHistory = anchor.web3.Keypair.generate();
250
+
251
+ const state = this.getStateAccount();
252
+ return await this.program.rpc.updateCurveHistory({
253
+ accounts: {
254
+ state: await this.getStatePublicKey(),
255
+ admin: this.wallet.publicKey,
283
256
  curveHistory: state.curveHistory,
257
+ extendedCurveHistory: extendedCurveHistory.publicKey,
284
258
  },
259
+ instructions: [
260
+ await this.program.account.extendedCurveHistory.createInstruction(
261
+ extendedCurveHistory
262
+ ),
263
+ ],
264
+ signers: [extendedCurveHistory],
285
265
  });
286
266
  }
287
267
 
@@ -334,7 +314,45 @@ export class Admin extends ClearingHouse {
334
314
  admin: this.wallet.publicKey,
335
315
  oracle: ammData.oracle,
336
316
  markets: state.markets,
337
- curveHistory: state.curveHistory,
317
+ curveHistory: state.extendedCurveHistory,
318
+ },
319
+ });
320
+ }
321
+
322
+ public async updateAmmOracleTwap(
323
+ marketIndex: BN
324
+ ): Promise<TransactionSignature> {
325
+ const state = this.getStateAccount();
326
+ const markets = this.getMarketsAccount();
327
+ const marketData = markets.markets[marketIndex.toNumber()];
328
+ const ammData = marketData.amm;
329
+
330
+ return await this.program.rpc.updateAmmOracleTwap(marketIndex, {
331
+ accounts: {
332
+ state: await this.getStatePublicKey(),
333
+ admin: this.wallet.publicKey,
334
+ oracle: ammData.oracle,
335
+ markets: state.markets,
336
+ curveHistory: state.extendedCurveHistory,
337
+ },
338
+ });
339
+ }
340
+
341
+ public async resetAmmOracleTwap(
342
+ marketIndex: BN
343
+ ): Promise<TransactionSignature> {
344
+ const state = this.getStateAccount();
345
+ const markets = this.getMarketsAccount();
346
+ const marketData = markets.markets[marketIndex.toNumber()];
347
+ const ammData = marketData.amm;
348
+
349
+ return await this.program.rpc.resetAmmOracleTwap(marketIndex, {
350
+ accounts: {
351
+ state: await this.getStatePublicKey(),
352
+ admin: this.wallet.publicKey,
353
+ oracle: ammData.oracle,
354
+ markets: state.markets,
355
+ curveHistory: state.extendedCurveHistory,
338
356
  },
339
357
  });
340
358
  }
@@ -500,21 +518,6 @@ export class Admin extends ClearingHouse {
500
518
  );
501
519
  }
502
520
 
503
- public async updateOrderFillerRewardStructure(
504
- orderFillerRewardStructure: OrderFillerRewardStructure
505
- ): Promise<TransactionSignature> {
506
- return await this.program.rpc.updateOrderFillerRewardStructure(
507
- orderFillerRewardStructure,
508
- {
509
- accounts: {
510
- admin: this.wallet.publicKey,
511
- state: await this.getStatePublicKey(),
512
- orderState: await this.getOrderStatePublicKey(),
513
- },
514
- }
515
- );
516
- }
517
-
518
521
  public async updateFee(fees: FeeStructure): Promise<TransactionSignature> {
519
522
  return await this.program.rpc.updateFee(fees, {
520
523
  accounts: {
@@ -555,30 +558,12 @@ export class Admin extends ClearingHouse {
555
558
  );
556
559
  }
557
560
 
558
- public async updateMarketMinimumQuoteAssetTradeSize(
559
- marketIndex: BN,
560
- minimumTradeSize: BN
561
- ): Promise<TransactionSignature> {
562
- const state = this.getStateAccount();
563
- return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
564
- marketIndex,
565
- minimumTradeSize,
566
- {
567
- accounts: {
568
- admin: this.wallet.publicKey,
569
- state: await this.getStatePublicKey(),
570
- markets: state.markets,
571
- },
572
- }
573
- );
574
- }
575
-
576
- public async updateMarketMinimumBaseAssetTradeSize(
561
+ public async updateMarketMinimumTradeSize(
577
562
  marketIndex: BN,
578
563
  minimumTradeSize: BN
579
564
  ): Promise<TransactionSignature> {
580
565
  const state = this.getStateAccount();
581
- return await this.program.rpc.updateMarketMinimumBaseAssetTradeSize(
566
+ return await this.program.rpc.updateMarketMinimumTradeSize(
582
567
  marketIndex,
583
568
  minimumTradeSize,
584
569
  {
@@ -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