@drift-labs/sdk 0.2.0-master.38 → 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.
- package/README.md +14 -14
- package/lib/accounts/bulkUserStatsSubscription.d.ts +2 -2
- package/lib/accounts/bulkUserSubscription.d.ts +2 -2
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +3 -8
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +6 -13
- package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
- package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
- package/lib/accounts/types.d.ts +4 -4
- package/lib/accounts/{webSocketClearingHouseAccountSubscriber.d.ts → webSocketDriftClientAccountSubscriber.d.ts} +3 -3
- package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +4 -4
- package/lib/addresses/pda.d.ts +3 -3
- package/lib/addresses/pda.js +10 -10
- package/lib/{admin.d.ts → adminClient.d.ts} +9 -4
- package/lib/{admin.js → adminClient.js} +62 -15
- package/lib/config.js +1 -1
- package/lib/constants/numericConstants.d.ts +3 -0
- package/lib/constants/numericConstants.js +5 -2
- package/lib/dlob/DLOB.d.ts +2 -2
- package/lib/dlob/DLOB.js +4 -4
- package/lib/{clearingHouse.d.ts → driftClient.d.ts} +20 -20
- package/lib/{clearingHouse.js → driftClient.js} +28 -28
- package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +3 -3
- package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
- package/lib/examples/makeTradeExample.js +13 -13
- package/lib/idl/{clearing_house.json → drift.json} +603 -119
- package/lib/index.d.ts +10 -10
- package/lib/index.js +10 -10
- package/lib/math/orders.d.ts +4 -4
- package/lib/math/orders.js +3 -3
- package/lib/serum/serumFulfillmentConfigMap.d.ts +3 -3
- package/lib/serum/serumFulfillmentConfigMap.js +3 -3
- package/lib/types.d.ts +38 -2
- package/lib/types.js +2 -1
- package/lib/{clearingHouseUser.d.ts → user.d.ts} +16 -10
- package/lib/{clearingHouseUser.js → user.js} +53 -45
- package/lib/userConfig.d.ts +14 -0
- package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
- package/lib/userMap/userMap.d.ts +17 -17
- package/lib/userMap/userMap.js +15 -15
- package/lib/userMap/userStatsMap.d.ts +7 -7
- package/lib/userMap/userStatsMap.js +10 -10
- package/lib/{clearingHouseUserStats.d.ts → userStats.d.ts} +5 -5
- package/lib/{clearingHouseUserStats.js → userStats.js} +8 -8
- package/lib/userStatsConfig.d.ts +14 -0
- package/lib/{clearingHouseUserStatsConfig.js → userStatsConfig.js} +0 -0
- package/package.json +1 -1
- package/src/accounts/bulkUserStatsSubscription.ts +2 -2
- package/src/accounts/bulkUserSubscription.ts +2 -2
- package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +11 -25
- package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
- package/src/accounts/types.ts +4 -4
- package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +7 -7
- package/src/addresses/pda.ts +6 -8
- package/src/{admin.ts → adminClient.ts} +125 -18
- package/src/config.ts +1 -1
- package/src/constants/numericConstants.ts +4 -0
- package/src/dlob/DLOB.ts +5 -5
- package/src/{clearingHouse.ts → driftClient.ts} +45 -47
- package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +3 -3
- package/src/examples/makeTradeExample.ts +15 -17
- package/src/idl/{clearing_house.json → drift.json} +603 -119
- package/src/index.ts +10 -10
- package/src/math/orders.ts +7 -13
- package/src/serum/serumFulfillmentConfigMap.ts +5 -5
- package/src/types.ts +41 -2
- package/src/{clearingHouseUser.ts → user.ts} +59 -53
- package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
- package/src/userMap/userMap.ts +29 -29
- package/src/userMap/userStatsMap.ts +21 -23
- package/src/{clearingHouseUserStats.ts → userStats.ts} +10 -10
- package/src/userStatsConfig.ts +18 -0
- package/tests/dlob/helpers.ts +55 -13
- package/lib/clearingHouseUserConfig.d.ts +0 -14
- package/lib/clearingHouseUserStatsConfig.d.ts +0 -14
- 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
|
|
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
|
-
|
|
51
|
-
|
|
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
|
-
|
|
63
|
-
|
|
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 {
|
|
74
|
-
import {
|
|
75
|
-
import {
|
|
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 {
|
|
78
|
-
import {
|
|
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 {
|
|
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
|
-
* #
|
|
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
|
|
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,
|
|
104
|
-
userStats?:
|
|
103
|
+
users = new Map<number, User>();
|
|
104
|
+
userStats?: UserStats;
|
|
105
105
|
activeSubAccountId: number;
|
|
106
|
-
userAccountSubscriptionConfig:
|
|
107
|
-
accountSubscriber:
|
|
108
|
-
eventEmitter: StrictEventEmitter<EventEmitter,
|
|
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:
|
|
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
|
-
|
|
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
|
|
153
|
-
|
|
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
|
|
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
|
|
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:
|
|
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:
|
|
218
|
-
):
|
|
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
|
|
226
|
-
|
|
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
|
|
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 =
|
|
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
|
|
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
|
-
|
|
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):
|
|
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():
|
|
597
|
+
public getUsers(): User[] {
|
|
600
598
|
return [...this.users.values()];
|
|
601
599
|
}
|
|
602
600
|
|
|
603
|
-
public getUserStats():
|
|
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
|
-
|
|
1321
|
+
driftSigner: this.getSignerPublicKey(),
|
|
1324
1322
|
user: userAccountPublicKey,
|
|
1325
1323
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
1326
1324
|
userTokenAccount: userTokenAccount,
|
|
@@ -2690,7 +2688,7 @@ export class ClearingHouse {
|
|
|
2690
2688
|
marketIndex: number,
|
|
2691
2689
|
limitPrice?: BN
|
|
2692
2690
|
): Promise<TransactionSignature> {
|
|
2693
|
-
const userPosition = this.getUser().
|
|
2691
|
+
const userPosition = this.getUser().getPerpPosition(marketIndex);
|
|
2694
2692
|
if (!userPosition) {
|
|
2695
2693
|
throw Error(`No position in market ${marketIndex.toString()}`);
|
|
2696
2694
|
}
|
|
@@ -3145,7 +3143,7 @@ export class ClearingHouse {
|
|
|
3145
3143
|
liquidatorStats: liquidatorStatsPublicKey,
|
|
3146
3144
|
spotMarketVault: spotMarket.vault,
|
|
3147
3145
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
3148
|
-
|
|
3146
|
+
driftSigner: this.getSignerPublicKey(),
|
|
3149
3147
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
3150
3148
|
},
|
|
3151
3149
|
remainingAccounts: remainingAccounts,
|
|
@@ -3202,7 +3200,7 @@ export class ClearingHouse {
|
|
|
3202
3200
|
liquidator: liquidatorPublicKey,
|
|
3203
3201
|
spotMarketVault: spotMarket.vault,
|
|
3204
3202
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
3205
|
-
|
|
3203
|
+
driftSigner: this.getSignerPublicKey(),
|
|
3206
3204
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
3207
3205
|
},
|
|
3208
3206
|
remainingAccounts: remainingAccounts,
|
|
@@ -3277,7 +3275,7 @@ export class ClearingHouse {
|
|
|
3277
3275
|
});
|
|
3278
3276
|
}
|
|
3279
3277
|
|
|
3280
|
-
public triggerEvent(eventName: keyof
|
|
3278
|
+
public triggerEvent(eventName: keyof DriftClientAccountEvents, data?: any) {
|
|
3281
3279
|
this.eventEmitter.emit(eventName, data);
|
|
3282
3280
|
}
|
|
3283
3281
|
|
|
@@ -3359,7 +3357,7 @@ export class ClearingHouse {
|
|
|
3359
3357
|
authority: this.wallet.publicKey,
|
|
3360
3358
|
spotMarketVault: spotMarket.vault,
|
|
3361
3359
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
3362
|
-
|
|
3360
|
+
driftSigner: this.getSignerPublicKey(),
|
|
3363
3361
|
userTokenAccount: collateralAccountPublicKey,
|
|
3364
3362
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
3365
3363
|
},
|
|
@@ -3458,7 +3456,7 @@ export class ClearingHouse {
|
|
|
3458
3456
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
3459
3457
|
authority: this.wallet.publicKey,
|
|
3460
3458
|
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
3461
|
-
|
|
3459
|
+
driftSigner: this.getSignerPublicKey(),
|
|
3462
3460
|
userTokenAccount: collateralAccountPublicKey,
|
|
3463
3461
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
3464
3462
|
},
|
|
@@ -3482,7 +3480,7 @@ export class ClearingHouse {
|
|
|
3482
3480
|
state: await this.getStatePublicKey(),
|
|
3483
3481
|
spotMarket: spotMarketAccount.pubkey,
|
|
3484
3482
|
spotMarketVault: spotMarketAccount.vault,
|
|
3485
|
-
|
|
3483
|
+
driftSigner: this.getSignerPublicKey(),
|
|
3486
3484
|
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
3487
3485
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
3488
3486
|
},
|
|
@@ -3526,7 +3524,7 @@ export class ClearingHouse {
|
|
|
3526
3524
|
authority: this.wallet.publicKey,
|
|
3527
3525
|
spotMarketVault: spotMarket.vault,
|
|
3528
3526
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
3529
|
-
|
|
3527
|
+
driftSigner: this.getSignerPublicKey(),
|
|
3530
3528
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
3531
3529
|
},
|
|
3532
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
|
|
7
|
+
export type DriftClientConfig = {
|
|
8
8
|
connection: Connection;
|
|
9
9
|
wallet: IWallet;
|
|
10
10
|
programID: PublicKey;
|
|
11
|
-
accountSubscription?:
|
|
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
|
|
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
|
-
|
|
8
|
-
|
|
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
|
|
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
|
|
73
|
+
const driftClient = new DriftClient({
|
|
76
74
|
connection,
|
|
77
75
|
wallet: provider.wallet,
|
|
78
|
-
programID:
|
|
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
|
|
86
|
+
await driftClient.subscribe();
|
|
89
87
|
|
|
90
88
|
// Set up Clearing House user client
|
|
91
|
-
const user = new
|
|
92
|
-
|
|
93
|
-
userAccountPublicKey: await
|
|
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
|
|
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
|
-
|
|
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 =
|
|
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
|
|
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
|
|
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
|
|
168
|
+
await driftClient.closePosition(solMarketInfo.marketIndex);
|
|
171
169
|
};
|
|
172
170
|
|
|
173
171
|
main();
|