@drift-labs/sdk 0.2.0-master.37 → 0.2.0-master.39

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 (75) hide show
  1. package/README.md +14 -14
  2. package/lib/accounts/bulkUserStatsSubscription.d.ts +2 -2
  3. package/lib/accounts/bulkUserSubscription.d.ts +2 -2
  4. package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +3 -8
  5. package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +6 -13
  6. package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
  7. package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
  8. package/lib/accounts/types.d.ts +4 -4
  9. package/lib/accounts/{webSocketClearingHouseAccountSubscriber.d.ts → webSocketDriftClientAccountSubscriber.d.ts} +3 -3
  10. package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +4 -4
  11. package/lib/addresses/pda.d.ts +3 -3
  12. package/lib/addresses/pda.js +10 -10
  13. package/lib/{admin.d.ts → adminClient.d.ts} +12 -4
  14. package/lib/{admin.js → adminClient.js} +89 -15
  15. package/lib/config.js +1 -1
  16. package/lib/constants/numericConstants.d.ts +3 -0
  17. package/lib/constants/numericConstants.js +5 -2
  18. package/lib/dlob/DLOB.d.ts +2 -2
  19. package/lib/dlob/DLOB.js +4 -4
  20. package/lib/{clearingHouse.d.ts → driftClient.d.ts} +21 -20
  21. package/lib/{clearingHouse.js → driftClient.js} +48 -37
  22. package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +3 -3
  23. package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
  24. package/lib/examples/makeTradeExample.js +13 -13
  25. package/lib/idl/{clearing_house.json → drift.json} +717 -143
  26. package/lib/index.d.ts +10 -10
  27. package/lib/index.js +10 -10
  28. package/lib/math/orders.d.ts +4 -4
  29. package/lib/math/orders.js +3 -3
  30. package/lib/serum/serumFulfillmentConfigMap.d.ts +3 -3
  31. package/lib/serum/serumFulfillmentConfigMap.js +3 -3
  32. package/lib/types.d.ts +38 -2
  33. package/lib/types.js +2 -1
  34. package/lib/{clearingHouseUser.d.ts → user.d.ts} +16 -10
  35. package/lib/{clearingHouseUser.js → user.js} +54 -46
  36. package/lib/userConfig.d.ts +14 -0
  37. package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
  38. package/lib/userMap/userMap.d.ts +17 -17
  39. package/lib/userMap/userMap.js +15 -15
  40. package/lib/userMap/userStatsMap.d.ts +7 -7
  41. package/lib/userMap/userStatsMap.js +10 -10
  42. package/lib/{clearingHouseUserStats.d.ts → userStats.d.ts} +5 -5
  43. package/lib/{clearingHouseUserStats.js → userStats.js} +8 -8
  44. package/lib/userStatsConfig.d.ts +14 -0
  45. package/lib/{clearingHouseUserStatsConfig.js → userStatsConfig.js} +0 -0
  46. package/package.json +1 -1
  47. package/src/accounts/bulkUserStatsSubscription.ts +2 -2
  48. package/src/accounts/bulkUserSubscription.ts +2 -2
  49. package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +11 -25
  50. package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
  51. package/src/accounts/types.ts +4 -4
  52. package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +7 -7
  53. package/src/addresses/pda.ts +6 -8
  54. package/src/{admin.ts → adminClient.ts} +185 -18
  55. package/src/config.ts +1 -1
  56. package/src/constants/numericConstants.ts +4 -0
  57. package/src/dlob/DLOB.ts +5 -5
  58. package/src/{clearingHouse.ts → driftClient.ts} +85 -68
  59. package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +3 -3
  60. package/src/examples/makeTradeExample.ts +15 -17
  61. package/src/idl/{clearing_house.json → drift.json} +717 -143
  62. package/src/index.ts +10 -10
  63. package/src/math/orders.ts +7 -13
  64. package/src/serum/serumFulfillmentConfigMap.ts +5 -5
  65. package/src/types.ts +41 -2
  66. package/src/{clearingHouseUser.ts → user.ts} +60 -54
  67. package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
  68. package/src/userMap/userMap.ts +29 -29
  69. package/src/userMap/userStatsMap.ts +21 -23
  70. package/src/{clearingHouseUserStats.ts → userStats.ts} +10 -10
  71. package/src/userStatsConfig.ts +18 -0
  72. package/tests/dlob/helpers.ts +55 -13
  73. package/lib/clearingHouseUserConfig.d.ts +0 -14
  74. package/lib/clearingHouseUserStatsConfig.d.ts +0 -14
  75. package/src/clearingHouseUserStatsConfig.ts +0 -18
@@ -26,7 +26,7 @@ import {
26
26
  isVariant,
27
27
  } from './types';
28
28
  import * as anchor from '@project-serum/anchor';
29
- import clearingHouseIDL from './idl/clearing_house.json';
29
+ import driftIDL from './idl/drift.json';
30
30
 
31
31
  import {
32
32
  Connection,
@@ -47,8 +47,8 @@ import { TokenFaucet } from './tokenFaucet';
47
47
  import { EventEmitter } from 'events';
48
48
  import StrictEventEmitter from 'strict-event-emitter-types';
49
49
  import {
50
- getClearingHouseSignerPublicKey,
51
- getClearingHouseStateAccountPublicKey,
50
+ getDriftSignerPublicKey,
51
+ getDriftStateAccountPublicKey,
52
52
  getInsuranceFundStakeAccountPublicKey,
53
53
  getPerpMarketPublicKey,
54
54
  getSerumFulfillmentConfigPublicKey,
@@ -59,8 +59,8 @@ import {
59
59
  getUserStatsAccountPublicKey,
60
60
  } from './addresses/pda';
61
61
  import {
62
- ClearingHouseAccountSubscriber,
63
- ClearingHouseAccountEvents,
62
+ DriftClientAccountSubscriber,
63
+ DriftClientAccountEvents,
64
64
  DataAndSlot,
65
65
  } from './accounts/types';
66
66
  import { TxSender } from './tx/types';
@@ -70,15 +70,15 @@ import { findDirectionToClose, positionIsAvailable } from './math/position';
70
70
  import { getTokenAmount } from './math/spotBalance';
71
71
  import { DEFAULT_USER_NAME, encodeName } from './userName';
72
72
  import { OraclePriceData } from './oracles/types';
73
- import { ClearingHouseConfig } from './clearingHouseConfig';
74
- import { PollingClearingHouseAccountSubscriber } from './accounts/pollingClearingHouseAccountSubscriber';
75
- import { WebSocketClearingHouseAccountSubscriber } from './accounts/webSocketClearingHouseAccountSubscriber';
73
+ import { DriftClientConfig } from './driftClientConfig';
74
+ import { PollingDriftClientAccountSubscriber } from './accounts/pollingDriftClientAccountSubscriber';
75
+ import { WebSocketDriftClientAccountSubscriber } from './accounts/webSocketDriftClientAccountSubscriber';
76
76
  import { RetryTxSender } from './tx/retryTxSender';
77
- import { ClearingHouseUser } from './clearingHouseUser';
78
- import { ClearingHouseUserAccountSubscriptionConfig } from './clearingHouseUserConfig';
77
+ import { User } from './user';
78
+ import { UserSubscriptionConfig } from './userConfig';
79
79
  import { getMarketsAndOraclesForSubscription } from './config';
80
80
  import { WRAPPED_SOL_MINT } from './constants/spotMarkets';
81
- import { ClearingHouseUserStats } from './clearingHouseUserStats';
81
+ import { UserStats } from './userStats';
82
82
  import { isSpotPositionAvailable } from './math/spotPosition';
83
83
 
84
84
  type RemainingAccountParams = {
@@ -91,21 +91,21 @@ type RemainingAccountParams = {
91
91
  };
92
92
 
93
93
  /**
94
- * # ClearingHouse
94
+ * # DriftClient
95
95
  * 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.
96
96
  */
97
- export class ClearingHouse {
97
+ export class DriftClient {
98
98
  connection: Connection;
99
99
  wallet: IWallet;
100
100
  public program: Program;
101
101
  provider: AnchorProvider;
102
102
  opts?: ConfirmOptions;
103
- users = new Map<number, ClearingHouseUser>();
104
- userStats?: ClearingHouseUserStats;
103
+ users = new Map<number, User>();
104
+ userStats?: UserStats;
105
105
  activeSubAccountId: number;
106
- userAccountSubscriptionConfig: ClearingHouseUserAccountSubscriptionConfig;
107
- accountSubscriber: ClearingHouseAccountSubscriber;
108
- eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
106
+ userAccountSubscriptionConfig: UserSubscriptionConfig;
107
+ accountSubscriber: DriftClientAccountSubscriber;
108
+ eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
109
109
  _isSubscribed = false;
110
110
  txSender: TxSender;
111
111
  perpMarketLastSlotCache = new Map<number, number>();
@@ -120,7 +120,7 @@ export class ClearingHouse {
120
120
  this._isSubscribed = val;
121
121
  }
122
122
 
123
- public constructor(config: ClearingHouseConfig) {
123
+ public constructor(config: DriftClientConfig) {
124
124
  this.connection = config.connection;
125
125
  this.wallet = config.wallet;
126
126
  this.opts = config.opts || AnchorProvider.defaultOptions();
@@ -130,7 +130,7 @@ export class ClearingHouse {
130
130
  this.opts
131
131
  );
132
132
  this.program = new Program(
133
- clearingHouseIDL as Idl,
133
+ driftIDL as Idl,
134
134
  config.programID,
135
135
  this.provider
136
136
  );
@@ -149,8 +149,8 @@ export class ClearingHouse {
149
149
  };
150
150
  this.createUsers(subAccountIds, this.userAccountSubscriptionConfig);
151
151
  if (config.userStats) {
152
- this.userStats = new ClearingHouseUserStats({
153
- clearingHouse: this,
152
+ this.userStats = new UserStats({
153
+ driftClient: this,
154
154
  userStatsAccountPublicKey: getUserStatsAccountPublicKey(
155
155
  this.program.programId,
156
156
  this.authority
@@ -178,7 +178,7 @@ export class ClearingHouse {
178
178
  }
179
179
 
180
180
  if (config.accountSubscription?.type === 'polling') {
181
- this.accountSubscriber = new PollingClearingHouseAccountSubscriber(
181
+ this.accountSubscriber = new PollingDriftClientAccountSubscriber(
182
182
  this.program,
183
183
  config.accountSubscription.accountLoader,
184
184
  perpMarketIndexes ?? [],
@@ -186,7 +186,7 @@ export class ClearingHouse {
186
186
  oracleInfos ?? []
187
187
  );
188
188
  } else {
189
- this.accountSubscriber = new WebSocketClearingHouseAccountSubscriber(
189
+ this.accountSubscriber = new WebSocketDriftClientAccountSubscriber(
190
190
  this.program,
191
191
  config.perpMarketIndexes ?? [],
192
192
  config.spotMarketIndexes ?? [],
@@ -204,7 +204,7 @@ export class ClearingHouse {
204
204
 
205
205
  createUsers(
206
206
  subAccountIds: number[],
207
- accountSubscriptionConfig: ClearingHouseUserAccountSubscriptionConfig
207
+ accountSubscriptionConfig: UserSubscriptionConfig
208
208
  ): void {
209
209
  for (const subAccountId of subAccountIds) {
210
210
  const user = this.createUser(subAccountId, accountSubscriptionConfig);
@@ -214,16 +214,16 @@ export class ClearingHouse {
214
214
 
215
215
  createUser(
216
216
  subAccountId: number,
217
- accountSubscriptionConfig: ClearingHouseUserAccountSubscriptionConfig
218
- ): ClearingHouseUser {
217
+ accountSubscriptionConfig: UserSubscriptionConfig
218
+ ): User {
219
219
  const userAccountPublicKey = getUserAccountPublicKeySync(
220
220
  this.program.programId,
221
221
  this.authority,
222
222
  subAccountId
223
223
  );
224
224
 
225
- return new ClearingHouseUser({
226
- clearingHouse: this,
225
+ return new User({
226
+ driftClient: this,
227
227
  userAccountPublicKey,
228
228
  accountSubscription: accountSubscriptionConfig,
229
229
  });
@@ -279,7 +279,7 @@ export class ClearingHouse {
279
279
  if (this.statePublicKey) {
280
280
  return this.statePublicKey;
281
281
  }
282
- this.statePublicKey = await getClearingHouseStateAccountPublicKey(
282
+ this.statePublicKey = await getDriftStateAccountPublicKey(
283
283
  this.program.programId
284
284
  );
285
285
  return this.statePublicKey;
@@ -290,9 +290,7 @@ export class ClearingHouse {
290
290
  if (this.signerPublicKey) {
291
291
  return this.signerPublicKey;
292
292
  }
293
- this.signerPublicKey = getClearingHouseSignerPublicKey(
294
- this.program.programId
295
- );
293
+ this.signerPublicKey = getDriftSignerPublicKey(this.program.programId);
296
294
  return this.signerPublicKey;
297
295
  }
298
296
 
@@ -349,7 +347,7 @@ export class ClearingHouse {
349
347
  }
350
348
 
351
349
  /**
352
- * Update the wallet to use for clearing house transactions and linked user account
350
+ * Update the wallet to use for drift transactions and linked user account
353
351
  * @param newWallet
354
352
  * @param subAccountIds
355
353
  * @param activeSubAccountId
@@ -365,7 +363,7 @@ export class ClearingHouse {
365
363
  this.opts
366
364
  );
367
365
  const newProgram = new Program(
368
- clearingHouseIDL as Idl,
366
+ driftIDL as Idl,
369
367
  this.program.programId,
370
368
  newProvider
371
369
  );
@@ -588,7 +586,7 @@ export class ClearingHouse {
588
586
  return txSig;
589
587
  }
590
588
 
591
- public getUser(subAccountId?: number): ClearingHouseUser {
589
+ public getUser(subAccountId?: number): User {
592
590
  subAccountId = subAccountId ?? this.activeSubAccountId;
593
591
  if (!this.users.has(subAccountId)) {
594
592
  throw new Error(`Clearing House has no user for user id ${subAccountId}`);
@@ -596,11 +594,11 @@ export class ClearingHouse {
596
594
  return this.users.get(subAccountId);
597
595
  }
598
596
 
599
- public getUsers(): ClearingHouseUser[] {
597
+ public getUsers(): User[] {
600
598
  return [...this.users.values()];
601
599
  }
602
600
 
603
- public getUserStats(): ClearingHouseUserStats {
601
+ public getUserStats(): UserStats {
604
602
  return this.userStats;
605
603
  }
606
604
 
@@ -1320,7 +1318,7 @@ export class ClearingHouse {
1320
1318
  state: await this.getStatePublicKey(),
1321
1319
  spotMarket: spotMarketAccount.pubkey,
1322
1320
  spotMarketVault: spotMarketAccount.vault,
1323
- clearingHouseSigner: this.getSignerPublicKey(),
1321
+ driftSigner: this.getSignerPublicKey(),
1324
1322
  user: userAccountPublicKey,
1325
1323
  userStats: this.getUserStatsAccountPublicKey(),
1326
1324
  userTokenAccount: userTokenAccount,
@@ -2166,13 +2164,11 @@ export class ClearingHouse {
2166
2164
  const orderId = order.orderId;
2167
2165
  const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
2168
2166
 
2169
- if (fulfillmentConfig) {
2170
- this.addSerumRemainingAccounts(
2171
- marketIndex,
2172
- remainingAccounts,
2173
- fulfillmentConfig
2174
- );
2175
- }
2167
+ this.addSpotFulfillmentAccounts(
2168
+ marketIndex,
2169
+ remainingAccounts,
2170
+ fulfillmentConfig
2171
+ );
2176
2172
 
2177
2173
  return await this.program.instruction.fillSpotOrder(
2178
2174
  orderId,
@@ -2192,6 +2188,31 @@ export class ClearingHouse {
2192
2188
  );
2193
2189
  }
2194
2190
 
2191
+ addSpotFulfillmentAccounts(
2192
+ marketIndex: number,
2193
+ remainingAccounts: AccountMeta[],
2194
+ fulfillmentConfig?: SerumV3FulfillmentConfigAccount
2195
+ ) {
2196
+ if (fulfillmentConfig) {
2197
+ this.addSerumRemainingAccounts(
2198
+ marketIndex,
2199
+ remainingAccounts,
2200
+ fulfillmentConfig
2201
+ );
2202
+ } else {
2203
+ remainingAccounts.push({
2204
+ pubkey: this.getSpotMarketAccount(marketIndex).vault,
2205
+ isWritable: false,
2206
+ isSigner: false,
2207
+ });
2208
+ remainingAccounts.push({
2209
+ pubkey: this.getQuoteSpotMarketAccount().vault,
2210
+ isWritable: false,
2211
+ isSigner: false,
2212
+ });
2213
+ }
2214
+ }
2215
+
2195
2216
  addSerumRemainingAccounts(
2196
2217
  marketIndex: number,
2197
2218
  remainingAccounts: AccountMeta[],
@@ -2556,13 +2577,11 @@ export class ClearingHouse {
2556
2577
  });
2557
2578
  }
2558
2579
 
2559
- if (fulfillmentConfig) {
2560
- this.addSerumRemainingAccounts(
2561
- orderParams.marketIndex,
2562
- remainingAccounts,
2563
- fulfillmentConfig
2564
- );
2565
- }
2580
+ this.addSpotFulfillmentAccounts(
2581
+ orderParams.marketIndex,
2582
+ remainingAccounts,
2583
+ fulfillmentConfig
2584
+ );
2566
2585
 
2567
2586
  return await this.program.instruction.placeAndTakeSpotOrder(
2568
2587
  orderParams,
@@ -2635,13 +2654,11 @@ export class ClearingHouse {
2635
2654
  });
2636
2655
  }
2637
2656
 
2638
- if (fulfillmentConfig) {
2639
- this.addSerumRemainingAccounts(
2640
- orderParams.marketIndex,
2641
- remainingAccounts,
2642
- fulfillmentConfig
2643
- );
2644
- }
2657
+ this.addSpotFulfillmentAccounts(
2658
+ orderParams.marketIndex,
2659
+ remainingAccounts,
2660
+ fulfillmentConfig
2661
+ );
2645
2662
 
2646
2663
  const takerOrderId = takerInfo.order.orderId;
2647
2664
  return await this.program.instruction.placeAndMakeSpotOrder(
@@ -2671,7 +2688,7 @@ export class ClearingHouse {
2671
2688
  marketIndex: number,
2672
2689
  limitPrice?: BN
2673
2690
  ): Promise<TransactionSignature> {
2674
- const userPosition = this.getUser().getUserPosition(marketIndex);
2691
+ const userPosition = this.getUser().getPerpPosition(marketIndex);
2675
2692
  if (!userPosition) {
2676
2693
  throw Error(`No position in market ${marketIndex.toString()}`);
2677
2694
  }
@@ -3126,7 +3143,7 @@ export class ClearingHouse {
3126
3143
  liquidatorStats: liquidatorStatsPublicKey,
3127
3144
  spotMarketVault: spotMarket.vault,
3128
3145
  insuranceFundVault: spotMarket.insuranceFund.vault,
3129
- clearingHouseSigner: this.getSignerPublicKey(),
3146
+ driftSigner: this.getSignerPublicKey(),
3130
3147
  tokenProgram: TOKEN_PROGRAM_ID,
3131
3148
  },
3132
3149
  remainingAccounts: remainingAccounts,
@@ -3183,7 +3200,7 @@ export class ClearingHouse {
3183
3200
  liquidator: liquidatorPublicKey,
3184
3201
  spotMarketVault: spotMarket.vault,
3185
3202
  insuranceFundVault: spotMarket.insuranceFund.vault,
3186
- clearingHouseSigner: this.getSignerPublicKey(),
3203
+ driftSigner: this.getSignerPublicKey(),
3187
3204
  tokenProgram: TOKEN_PROGRAM_ID,
3188
3205
  },
3189
3206
  remainingAccounts: remainingAccounts,
@@ -3258,7 +3275,7 @@ export class ClearingHouse {
3258
3275
  });
3259
3276
  }
3260
3277
 
3261
- public triggerEvent(eventName: keyof ClearingHouseAccountEvents, data?: any) {
3278
+ public triggerEvent(eventName: keyof DriftClientAccountEvents, data?: any) {
3262
3279
  this.eventEmitter.emit(eventName, data);
3263
3280
  }
3264
3281
 
@@ -3340,7 +3357,7 @@ export class ClearingHouse {
3340
3357
  authority: this.wallet.publicKey,
3341
3358
  spotMarketVault: spotMarket.vault,
3342
3359
  insuranceFundVault: spotMarket.insuranceFund.vault,
3343
- clearingHouseSigner: this.getSignerPublicKey(),
3360
+ driftSigner: this.getSignerPublicKey(),
3344
3361
  userTokenAccount: collateralAccountPublicKey,
3345
3362
  tokenProgram: TOKEN_PROGRAM_ID,
3346
3363
  },
@@ -3439,7 +3456,7 @@ export class ClearingHouse {
3439
3456
  userStats: this.getUserStatsAccountPublicKey(),
3440
3457
  authority: this.wallet.publicKey,
3441
3458
  insuranceFundVault: spotMarketAccount.insuranceFund.vault,
3442
- clearingHouseSigner: this.getSignerPublicKey(),
3459
+ driftSigner: this.getSignerPublicKey(),
3443
3460
  userTokenAccount: collateralAccountPublicKey,
3444
3461
  tokenProgram: TOKEN_PROGRAM_ID,
3445
3462
  },
@@ -3463,7 +3480,7 @@ export class ClearingHouse {
3463
3480
  state: await this.getStatePublicKey(),
3464
3481
  spotMarket: spotMarketAccount.pubkey,
3465
3482
  spotMarketVault: spotMarketAccount.vault,
3466
- clearingHouseSigner: this.getSignerPublicKey(),
3483
+ driftSigner: this.getSignerPublicKey(),
3467
3484
  insuranceFundVault: spotMarketAccount.insuranceFund.vault,
3468
3485
  tokenProgram: TOKEN_PROGRAM_ID,
3469
3486
  },
@@ -3507,7 +3524,7 @@ export class ClearingHouse {
3507
3524
  authority: this.wallet.publicKey,
3508
3525
  spotMarketVault: spotMarket.vault,
3509
3526
  insuranceFundVault: spotMarket.insuranceFund.vault,
3510
- clearingHouseSigner: this.getSignerPublicKey(),
3527
+ driftSigner: this.getSignerPublicKey(),
3511
3528
  tokenProgram: TOKEN_PROGRAM_ID,
3512
3529
  },
3513
3530
  remainingAccounts: remainingAccounts,
@@ -4,11 +4,11 @@ import { OracleInfo } from './oracles/types';
4
4
  import { BulkAccountLoader } from './accounts/bulkAccountLoader';
5
5
  import { DriftEnv } from './config';
6
6
 
7
- export type ClearingHouseConfig = {
7
+ export type DriftClientConfig = {
8
8
  connection: Connection;
9
9
  wallet: IWallet;
10
10
  programID: PublicKey;
11
- accountSubscription?: ClearingHouseSubscriptionConfig;
11
+ accountSubscription?: DriftClientSubscriptionConfig;
12
12
  opts?: ConfirmOptions;
13
13
  txSenderConfig?: TxSenderConfig;
14
14
  subAccountIds?: number[];
@@ -21,7 +21,7 @@ export type ClearingHouseConfig = {
21
21
  authority?: PublicKey; // explicitly pass an authority if signer is delegate
22
22
  };
23
23
 
24
- type ClearingHouseSubscriptionConfig =
24
+ type DriftClientSubscriptionConfig =
25
25
  | {
26
26
  type: 'websocket';
27
27
  }
@@ -4,8 +4,8 @@ import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token';
4
4
  import { Connection, Keypair, PublicKey } from '@solana/web3.js';
5
5
  import {
6
6
  calculateReservePrice,
7
- ClearingHouse,
8
- ClearingHouseUser,
7
+ DriftClient,
8
+ User,
9
9
  initialize,
10
10
  PositionDirection,
11
11
  convertToNumber,
@@ -64,18 +64,16 @@ const main = async () => {
64
64
  );
65
65
 
66
66
  // Set up the Drift Clearing House
67
- const clearingHousePublicKey = new PublicKey(
68
- sdkConfig.CLEARING_HOUSE_PROGRAM_ID
69
- );
67
+ const driftPublicKey = new PublicKey(sdkConfig.CLEARING_HOUSE_PROGRAM_ID);
70
68
  const bulkAccountLoader = new BulkAccountLoader(
71
69
  connection,
72
70
  'confirmed',
73
71
  1000
74
72
  );
75
- const clearingHouse = new ClearingHouse({
73
+ const driftClient = new DriftClient({
76
74
  connection,
77
75
  wallet: provider.wallet,
78
- programID: clearingHousePublicKey,
76
+ programID: driftPublicKey,
79
77
  perpMarketIndexes: PerpMarkets[cluster].map((market) => market.marketIndex),
80
78
  spotMarketIndexes: SpotMarkets[cluster].map(
81
79
  (spotMarket) => spotMarket.marketIndex
@@ -85,12 +83,12 @@ const main = async () => {
85
83
  accountLoader: bulkAccountLoader,
86
84
  },
87
85
  });
88
- await clearingHouse.subscribe();
86
+ await driftClient.subscribe();
89
87
 
90
88
  // Set up Clearing House user client
91
- const user = new ClearingHouseUser({
92
- clearingHouse,
93
- userAccountPublicKey: await clearingHouse.getUserAccountPublicKey(),
89
+ const user = new User({
90
+ driftClient: driftClient,
91
+ userAccountPublicKey: await driftClient.getUserAccountPublicKey(),
94
92
  accountSubscription: {
95
93
  type: 'polling',
96
94
  accountLoader: bulkAccountLoader,
@@ -103,7 +101,7 @@ const main = async () => {
103
101
  if (!userAccountExists) {
104
102
  //// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
105
103
  const depositAmount = new BN(10000).mul(QUOTE_PRECISION);
106
- await clearingHouse.initializeUserAccountAndDepositCollateral(
104
+ await driftClient.initializeUserAccountAndDepositCollateral(
107
105
  depositAmount,
108
106
  await getTokenAddress(
109
107
  usdcTokenAddress.toString(),
@@ -121,7 +119,7 @@ const main = async () => {
121
119
  );
122
120
 
123
121
  const currentMarketPrice = calculateReservePrice(
124
- clearingHouse.getPerpMarketAccount(solMarketInfo.marketIndex),
122
+ driftClient.getPerpMarketAccount(solMarketInfo.marketIndex),
125
123
  undefined
126
124
  );
127
125
 
@@ -130,7 +128,7 @@ const main = async () => {
130
128
  console.log(`Current Market Price is $${formattedPrice}`);
131
129
 
132
130
  // Estimate the slippage for a $5000 LONG trade
133
- const solMarketAccount = clearingHouse.getPerpMarketAccount(
131
+ const solMarketAccount = driftClient.getPerpMarketAccount(
134
132
  solMarketInfo.marketIndex
135
133
  );
136
134
 
@@ -151,7 +149,7 @@ const main = async () => {
151
149
  );
152
150
 
153
151
  // Make a $5000 LONG trade
154
- await clearingHouse.openPosition(
152
+ await driftClient.openPosition(
155
153
  PositionDirection.LONG,
156
154
  longAmount,
157
155
  solMarketInfo.marketIndex
@@ -160,14 +158,14 @@ const main = async () => {
160
158
 
161
159
  // Reduce the position by $2000
162
160
  const reduceAmount = new BN(2000).mul(QUOTE_PRECISION);
163
- await clearingHouse.openPosition(
161
+ await driftClient.openPosition(
164
162
  PositionDirection.SHORT,
165
163
  reduceAmount,
166
164
  solMarketInfo.marketIndex
167
165
  );
168
166
 
169
167
  // Close the rest of the position
170
- await clearingHouse.closePosition(solMarketInfo.marketIndex);
168
+ await driftClient.closePosition(solMarketInfo.marketIndex);
171
169
  };
172
170
 
173
171
  main();