@drift-labs/sdk 2.31.1-beta.5 → 2.31.1-beta.7
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/VERSION +1 -1
- package/lib/driftClient.d.ts +2 -0
- package/lib/driftClient.js +26 -9
- package/lib/driftClientConfig.d.ts +2 -1
- package/lib/idl/drift.json +1 -1
- package/lib/user.js +6 -3
- package/package.json +1 -1
- package/src/config.ts +2 -2
- package/src/driftClient.ts +38 -2
- package/src/driftClientConfig.ts +2 -1
- package/src/idl/drift.json +1 -1
- package/src/user.ts +7 -3
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.31.1-beta.
|
|
1
|
+
2.31.1-beta.7
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -51,6 +51,7 @@ export declare class DriftClient {
|
|
|
51
51
|
authoritySubAccountMap?: Map<string, number[]>;
|
|
52
52
|
skipLoadUsers?: boolean;
|
|
53
53
|
txVersion: TransactionVersion;
|
|
54
|
+
txParams: TxParams;
|
|
54
55
|
get isSubscribed(): boolean;
|
|
55
56
|
set isSubscribed(val: boolean);
|
|
56
57
|
constructor(config: DriftClientConfig);
|
|
@@ -230,6 +231,7 @@ export declare class DriftClient {
|
|
|
230
231
|
getRemovePerpLpSharesIx(marketIndex: number, sharesToBurn?: BN): Promise<TransactionInstruction>;
|
|
231
232
|
addPerpLpShares(amount: BN, marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
232
233
|
getAddPerpLpSharesIx(amount: BN, marketIndex: number): Promise<TransactionInstruction>;
|
|
234
|
+
getQuoteValuePerLpShare(marketIndex: number): BN;
|
|
233
235
|
/**
|
|
234
236
|
* @deprecated use {@link placePerpOrder} or {@link placeAndTakePerpOrder} instead
|
|
235
237
|
*/
|
package/lib/driftClient.js
CHANGED
|
@@ -64,7 +64,7 @@ class DriftClient {
|
|
|
64
64
|
this._isSubscribed = val;
|
|
65
65
|
}
|
|
66
66
|
constructor(config) {
|
|
67
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
67
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
|
|
68
68
|
this.users = new Map();
|
|
69
69
|
this._isSubscribed = false;
|
|
70
70
|
this.perpMarketLastSlotCache = new Map();
|
|
@@ -78,6 +78,10 @@ class DriftClient {
|
|
|
78
78
|
this.activeSubAccountId = (_c = config.activeSubAccountId) !== null && _c !== void 0 ? _c : 0;
|
|
79
79
|
this.skipLoadUsers = (_d = config.skipLoadUsers) !== null && _d !== void 0 ? _d : false;
|
|
80
80
|
this.txVersion = (_e = config.txVersion) !== null && _e !== void 0 ? _e : 'legacy';
|
|
81
|
+
this.txParams = {
|
|
82
|
+
computeUnits: (_g = (_f = config.txParams) === null || _f === void 0 ? void 0 : _f.computeUnits) !== null && _g !== void 0 ? _g : 600000,
|
|
83
|
+
computeUnitsPrice: (_j = (_h = config.txParams) === null || _h === void 0 ? void 0 : _h.computeUnitsPrice) !== null && _j !== void 0 ? _j : 0,
|
|
84
|
+
};
|
|
81
85
|
if (config.includeDelegates && config.subAccountIds) {
|
|
82
86
|
throw new Error('Can only pass one of includeDelegates or subAccountIds. If you want to specify subaccount ids for multiple authorities, pass authoritySubaccountMap instead');
|
|
83
87
|
}
|
|
@@ -92,8 +96,8 @@ class DriftClient {
|
|
|
92
96
|
: config.subAccountIds
|
|
93
97
|
? new Map([[this.authority.toString(), config.subAccountIds]])
|
|
94
98
|
: new Map();
|
|
95
|
-
this.includeDelegates = (
|
|
96
|
-
if (((
|
|
99
|
+
this.includeDelegates = (_k = config.includeDelegates) !== null && _k !== void 0 ? _k : false;
|
|
100
|
+
if (((_l = config.accountSubscription) === null || _l === void 0 ? void 0 : _l.type) === 'polling') {
|
|
97
101
|
this.userAccountSubscriptionConfig = {
|
|
98
102
|
type: 'polling',
|
|
99
103
|
accountLoader: config.accountSubscription.accountLoader,
|
|
@@ -125,14 +129,14 @@ class DriftClient {
|
|
|
125
129
|
const noMarketsAndOraclesSpecified = config.perpMarketIndexes === undefined &&
|
|
126
130
|
config.spotMarketIndexes === undefined &&
|
|
127
131
|
config.oracleInfos === undefined;
|
|
128
|
-
if (((
|
|
129
|
-
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (
|
|
132
|
+
if (((_m = config.accountSubscription) === null || _m === void 0 ? void 0 : _m.type) === 'polling') {
|
|
133
|
+
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_o = config.perpMarketIndexes) !== null && _o !== void 0 ? _o : [], (_p = config.spotMarketIndexes) !== null && _p !== void 0 ? _p : [], (_q = config.oracleInfos) !== null && _q !== void 0 ? _q : [], noMarketsAndOraclesSpecified);
|
|
130
134
|
}
|
|
131
135
|
else {
|
|
132
|
-
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (
|
|
136
|
+
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_r = config.perpMarketIndexes) !== null && _r !== void 0 ? _r : [], (_s = config.spotMarketIndexes) !== null && _s !== void 0 ? _s : [], (_t = config.oracleInfos) !== null && _t !== void 0 ? _t : [], noMarketsAndOraclesSpecified);
|
|
133
137
|
}
|
|
134
138
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
135
|
-
this.txSender = (
|
|
139
|
+
this.txSender = (_u = config.txSender) !== null && _u !== void 0 ? _u : new retryTxSender_1.RetryTxSender(this.provider);
|
|
136
140
|
}
|
|
137
141
|
getUserMapKey(subAccountId, authority) {
|
|
138
142
|
return `${subAccountId}_${authority.toString()}`;
|
|
@@ -1358,6 +1362,19 @@ class DriftClient {
|
|
|
1358
1362
|
remainingAccounts: remainingAccounts,
|
|
1359
1363
|
});
|
|
1360
1364
|
}
|
|
1365
|
+
getQuoteValuePerLpShare(marketIndex) {
|
|
1366
|
+
const perpMarketAccount = this.getPerpMarketAccount(marketIndex);
|
|
1367
|
+
const openBids = anchor_1.BN.max(perpMarketAccount.amm.baseAssetReserve.sub(perpMarketAccount.amm.minBaseAssetReserve), numericConstants_1.ZERO);
|
|
1368
|
+
const openAsks = anchor_1.BN.max(perpMarketAccount.amm.maxBaseAssetReserve.sub(perpMarketAccount.amm.baseAssetReserve), numericConstants_1.ZERO);
|
|
1369
|
+
const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
|
|
1370
|
+
const maxOpenBidsAsks = anchor_1.BN.max(openBids, openAsks);
|
|
1371
|
+
const quoteValuePerLpShare = maxOpenBidsAsks
|
|
1372
|
+
.mul(oraclePriceData.price)
|
|
1373
|
+
.mul(numericConstants_1.QUOTE_PRECISION)
|
|
1374
|
+
.div(numericConstants_1.PRICE_PRECISION)
|
|
1375
|
+
.div(perpMarketAccount.amm.sqrtK);
|
|
1376
|
+
return quoteValuePerLpShare;
|
|
1377
|
+
}
|
|
1361
1378
|
/**
|
|
1362
1379
|
* @deprecated use {@link placePerpOrder} or {@link placeAndTakePerpOrder} instead
|
|
1363
1380
|
*/
|
|
@@ -3058,13 +3075,13 @@ class DriftClient {
|
|
|
3058
3075
|
async buildTransaction(instructions, txParams, txVersion, lookupTables) {
|
|
3059
3076
|
var _a, _b;
|
|
3060
3077
|
const allIx = [];
|
|
3061
|
-
const computeUnits = (_a = txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits) !== null && _a !== void 0 ? _a :
|
|
3078
|
+
const computeUnits = (_a = txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits) !== null && _a !== void 0 ? _a : this.txParams.computeUnits;
|
|
3062
3079
|
if (computeUnits !== 200000) {
|
|
3063
3080
|
allIx.push(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
|
|
3064
3081
|
units: computeUnits,
|
|
3065
3082
|
}));
|
|
3066
3083
|
}
|
|
3067
|
-
const computeUnitsPrice = (_b = txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice) !== null && _b !== void 0 ? _b :
|
|
3084
|
+
const computeUnitsPrice = (_b = txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice) !== null && _b !== void 0 ? _b : this.txParams.computeUnitsPrice;
|
|
3068
3085
|
if (computeUnitsPrice !== 0) {
|
|
3069
3086
|
allIx.push(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
3070
3087
|
microLamports: computeUnitsPrice,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ConfirmOptions, Connection, PublicKey, TransactionVersion } from '@solana/web3.js';
|
|
2
|
-
import { IWallet } from './types';
|
|
2
|
+
import { IWallet, TxParams } from './types';
|
|
3
3
|
import { OracleInfo } from './oracles/types';
|
|
4
4
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
5
5
|
import { DriftEnv } from './config';
|
|
@@ -24,6 +24,7 @@ export type DriftClientConfig = {
|
|
|
24
24
|
authoritySubAccountMap?: Map<string, number[]>;
|
|
25
25
|
skipLoadUsers?: boolean;
|
|
26
26
|
txVersion?: TransactionVersion;
|
|
27
|
+
txParams?: TxParams;
|
|
27
28
|
};
|
|
28
29
|
export type DriftClientSubscriptionConfig = {
|
|
29
30
|
type: 'websocket';
|
package/lib/idl/drift.json
CHANGED
package/lib/user.js
CHANGED
|
@@ -180,7 +180,9 @@ class User {
|
|
|
180
180
|
* @returns : pnl from settle
|
|
181
181
|
*/
|
|
182
182
|
getPerpPositionWithLPSettle(marketIndex, originalPosition) {
|
|
183
|
-
|
|
183
|
+
var _a;
|
|
184
|
+
originalPosition =
|
|
185
|
+
(_a = originalPosition !== null && originalPosition !== void 0 ? originalPosition : this.getPerpPosition(marketIndex)) !== null && _a !== void 0 ? _a : this.getEmptyPosition(marketIndex);
|
|
184
186
|
if (originalPosition.lpShares.eq(numericConstants_1.ZERO)) {
|
|
185
187
|
return [originalPosition, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
186
188
|
}
|
|
@@ -232,7 +234,7 @@ class User {
|
|
|
232
234
|
let pnl;
|
|
233
235
|
if (updateType == 'open' || updateType == 'increase') {
|
|
234
236
|
newQuoteEntry = position.quoteEntryAmount.add(deltaQaa);
|
|
235
|
-
pnl =
|
|
237
|
+
pnl = numericConstants_1.ZERO;
|
|
236
238
|
}
|
|
237
239
|
else if (updateType == 'reduce' || updateType == 'close') {
|
|
238
240
|
newQuoteEntry = position.quoteEntryAmount.sub(position.quoteEntryAmount
|
|
@@ -608,7 +610,8 @@ class User {
|
|
|
608
610
|
* @returns : Precision QUOTE_PRECISION
|
|
609
611
|
*/
|
|
610
612
|
getPerpPositionValue(marketIndex, oraclePriceData, includeOpenOrders = false) {
|
|
611
|
-
const userPosition = this.
|
|
613
|
+
const userPosition = this.getPerpPositionWithLPSettle(marketIndex)[0] ||
|
|
614
|
+
this.getEmptyPosition(marketIndex);
|
|
612
615
|
const market = this.driftClient.getPerpMarketAccount(userPosition.marketIndex);
|
|
613
616
|
return (0, margin_1.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData, includeOpenOrders);
|
|
614
617
|
}
|
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -131,7 +131,7 @@ export async function findAllMarketAndOracles(program: Program): Promise<{
|
|
|
131
131
|
(await program.account.spotMarket.all()) as ProgramAccount<SpotMarketAccount>[];
|
|
132
132
|
|
|
133
133
|
for (const perpMarketProgramAccount of perpMarketProgramAccounts) {
|
|
134
|
-
const perpMarket = perpMarketProgramAccount.account;
|
|
134
|
+
const perpMarket = perpMarketProgramAccount.account as PerpMarketAccount;
|
|
135
135
|
perpMarketIndexes.push(perpMarket.marketIndex);
|
|
136
136
|
oracleInfos.set(perpMarket.amm.oracle.toString(), {
|
|
137
137
|
publicKey: perpMarket.amm.oracle,
|
|
@@ -140,7 +140,7 @@ export async function findAllMarketAndOracles(program: Program): Promise<{
|
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
for (const spotMarketProgramAccount of spotMarketProgramAccounts) {
|
|
143
|
-
const spotMarket = spotMarketProgramAccount.account;
|
|
143
|
+
const spotMarket = spotMarketProgramAccount.account as SpotMarketAccount;
|
|
144
144
|
spotMarketIndexes.push(spotMarket.marketIndex);
|
|
145
145
|
oracleInfos.set(spotMarket.oracle.toString(), {
|
|
146
146
|
publicKey: spotMarket.oracle,
|
package/src/driftClient.ts
CHANGED
|
@@ -95,6 +95,7 @@ import {
|
|
|
95
95
|
PRICE_PRECISION,
|
|
96
96
|
QUOTE_SPOT_MARKET_INDEX,
|
|
97
97
|
ZERO,
|
|
98
|
+
QUOTE_PRECISION,
|
|
98
99
|
} from './constants/numericConstants';
|
|
99
100
|
import { findDirectionToClose, positionIsAvailable } from './math/position';
|
|
100
101
|
import { getSignedTokenAmount, getTokenAmount } from './math/spotBalance';
|
|
@@ -154,6 +155,7 @@ export class DriftClient {
|
|
|
154
155
|
authoritySubAccountMap?: Map<string, number[]>;
|
|
155
156
|
skipLoadUsers?: boolean;
|
|
156
157
|
txVersion: TransactionVersion;
|
|
158
|
+
txParams: TxParams;
|
|
157
159
|
|
|
158
160
|
public get isSubscribed() {
|
|
159
161
|
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
@@ -182,6 +184,10 @@ export class DriftClient {
|
|
|
182
184
|
this.activeSubAccountId = config.activeSubAccountId ?? 0;
|
|
183
185
|
this.skipLoadUsers = config.skipLoadUsers ?? false;
|
|
184
186
|
this.txVersion = config.txVersion ?? 'legacy';
|
|
187
|
+
this.txParams = {
|
|
188
|
+
computeUnits: config.txParams?.computeUnits ?? 600_000,
|
|
189
|
+
computeUnitsPrice: config.txParams?.computeUnitsPrice ?? 0,
|
|
190
|
+
};
|
|
185
191
|
|
|
186
192
|
if (config.includeDelegates && config.subAccountIds) {
|
|
187
193
|
throw new Error(
|
|
@@ -2301,6 +2307,35 @@ export class DriftClient {
|
|
|
2301
2307
|
});
|
|
2302
2308
|
}
|
|
2303
2309
|
|
|
2310
|
+
public getQuoteValuePerLpShare(marketIndex: number): BN {
|
|
2311
|
+
const perpMarketAccount = this.getPerpMarketAccount(marketIndex);
|
|
2312
|
+
|
|
2313
|
+
const openBids = BN.max(
|
|
2314
|
+
perpMarketAccount.amm.baseAssetReserve.sub(
|
|
2315
|
+
perpMarketAccount.amm.minBaseAssetReserve
|
|
2316
|
+
),
|
|
2317
|
+
ZERO
|
|
2318
|
+
);
|
|
2319
|
+
|
|
2320
|
+
const openAsks = BN.max(
|
|
2321
|
+
perpMarketAccount.amm.maxBaseAssetReserve.sub(
|
|
2322
|
+
perpMarketAccount.amm.baseAssetReserve
|
|
2323
|
+
),
|
|
2324
|
+
ZERO
|
|
2325
|
+
);
|
|
2326
|
+
|
|
2327
|
+
const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
|
|
2328
|
+
|
|
2329
|
+
const maxOpenBidsAsks = BN.max(openBids, openAsks);
|
|
2330
|
+
const quoteValuePerLpShare = maxOpenBidsAsks
|
|
2331
|
+
.mul(oraclePriceData.price)
|
|
2332
|
+
.mul(QUOTE_PRECISION)
|
|
2333
|
+
.div(PRICE_PRECISION)
|
|
2334
|
+
.div(perpMarketAccount.amm.sqrtK);
|
|
2335
|
+
|
|
2336
|
+
return quoteValuePerLpShare;
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2304
2339
|
/**
|
|
2305
2340
|
* @deprecated use {@link placePerpOrder} or {@link placeAndTakePerpOrder} instead
|
|
2306
2341
|
*/
|
|
@@ -5371,7 +5406,7 @@ export class DriftClient {
|
|
|
5371
5406
|
lookupTables?: AddressLookupTableAccount[]
|
|
5372
5407
|
): Promise<Transaction | VersionedTransaction> {
|
|
5373
5408
|
const allIx = [];
|
|
5374
|
-
const computeUnits = txParams?.computeUnits ??
|
|
5409
|
+
const computeUnits = txParams?.computeUnits ?? this.txParams.computeUnits;
|
|
5375
5410
|
if (computeUnits !== 200_000) {
|
|
5376
5411
|
allIx.push(
|
|
5377
5412
|
ComputeBudgetProgram.setComputeUnitLimit({
|
|
@@ -5379,7 +5414,8 @@ export class DriftClient {
|
|
|
5379
5414
|
})
|
|
5380
5415
|
);
|
|
5381
5416
|
}
|
|
5382
|
-
const computeUnitsPrice =
|
|
5417
|
+
const computeUnitsPrice =
|
|
5418
|
+
txParams?.computeUnitsPrice ?? this.txParams.computeUnitsPrice;
|
|
5383
5419
|
if (computeUnitsPrice !== 0) {
|
|
5384
5420
|
allIx.push(
|
|
5385
5421
|
ComputeBudgetProgram.setComputeUnitPrice({
|
package/src/driftClientConfig.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
PublicKey,
|
|
5
5
|
TransactionVersion,
|
|
6
6
|
} from '@solana/web3.js';
|
|
7
|
-
import { IWallet } from './types';
|
|
7
|
+
import { IWallet, TxParams } from './types';
|
|
8
8
|
import { OracleInfo } from './oracles/types';
|
|
9
9
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
10
10
|
import { DriftEnv } from './config';
|
|
@@ -30,6 +30,7 @@ export type DriftClientConfig = {
|
|
|
30
30
|
authoritySubAccountMap?: Map<string, number[]>; // if passed this will override subAccountIds and includeDelegates
|
|
31
31
|
skipLoadUsers?: boolean; // if passed to constructor, no user accounts will be loaded. they will load if updateWallet is called afterwards.
|
|
32
32
|
txVersion?: TransactionVersion; // which tx version to use
|
|
33
|
+
txParams?: TxParams; // default tx params to use
|
|
33
34
|
};
|
|
34
35
|
|
|
35
36
|
export type DriftClientSubscriptionConfig =
|
package/src/idl/drift.json
CHANGED
package/src/user.ts
CHANGED
|
@@ -304,7 +304,10 @@ export class User {
|
|
|
304
304
|
marketIndex: number,
|
|
305
305
|
originalPosition?: PerpPosition
|
|
306
306
|
): [PerpPosition, BN, BN] {
|
|
307
|
-
originalPosition =
|
|
307
|
+
originalPosition =
|
|
308
|
+
originalPosition ??
|
|
309
|
+
this.getPerpPosition(marketIndex) ??
|
|
310
|
+
this.getEmptyPosition(marketIndex);
|
|
308
311
|
|
|
309
312
|
if (originalPosition.lpShares.eq(ZERO)) {
|
|
310
313
|
return [originalPosition, ZERO, ZERO];
|
|
@@ -371,7 +374,7 @@ export class User {
|
|
|
371
374
|
let pnl;
|
|
372
375
|
if (updateType == 'open' || updateType == 'increase') {
|
|
373
376
|
newQuoteEntry = position.quoteEntryAmount.add(deltaQaa);
|
|
374
|
-
pnl =
|
|
377
|
+
pnl = ZERO;
|
|
375
378
|
} else if (updateType == 'reduce' || updateType == 'close') {
|
|
376
379
|
newQuoteEntry = position.quoteEntryAmount.sub(
|
|
377
380
|
position.quoteEntryAmount
|
|
@@ -1136,7 +1139,8 @@ export class User {
|
|
|
1136
1139
|
includeOpenOrders = false
|
|
1137
1140
|
): BN {
|
|
1138
1141
|
const userPosition =
|
|
1139
|
-
this.
|
|
1142
|
+
this.getPerpPositionWithLPSettle(marketIndex)[0] ||
|
|
1143
|
+
this.getEmptyPosition(marketIndex);
|
|
1140
1144
|
const market = this.driftClient.getPerpMarketAccount(
|
|
1141
1145
|
userPosition.marketIndex
|
|
1142
1146
|
);
|