@drift-labs/sdk 2.33.1-beta.6 → 2.33.1-beta.8
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 +3 -1
- package/lib/driftClient.js +3 -1
- package/lib/math/spotBalance.js +1 -1
- package/lib/user.d.ts +14 -0
- package/lib/user.js +92 -9
- package/package.json +1 -1
- package/src/driftClient.ts +3 -1
- package/src/math/spotBalance.ts +1 -1
- package/src/user.ts +142 -13
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.33.1-beta.
|
|
1
|
+
2.33.1-beta.8
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -309,9 +309,11 @@ export declare class DriftClient {
|
|
|
309
309
|
* @param inMarketIndex the market index of the token you're selling
|
|
310
310
|
* @param outAssociatedTokenAccount the token account to receive the token being sold on jupiter
|
|
311
311
|
* @param inAssociatedTokenAccount the token account to
|
|
312
|
-
* @param amount the amount of
|
|
312
|
+
* @param amount the amount of TokenIn, regardless of swapMode
|
|
313
313
|
* @param slippageBps the max slippage passed to jupiter api
|
|
314
|
+
* @param swapMode jupiter swapMode (ExactIn or ExactOut), default is ExactIn
|
|
314
315
|
* @param route the jupiter route to use for the swap
|
|
316
|
+
* @param reduceOnly specify if In or Out token on the drift account must reduceOnly, checked at end of swap
|
|
315
317
|
* @param txParams
|
|
316
318
|
*/
|
|
317
319
|
swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, txParams, }: {
|
package/lib/driftClient.js
CHANGED
|
@@ -1940,9 +1940,11 @@ class DriftClient {
|
|
|
1940
1940
|
* @param inMarketIndex the market index of the token you're selling
|
|
1941
1941
|
* @param outAssociatedTokenAccount the token account to receive the token being sold on jupiter
|
|
1942
1942
|
* @param inAssociatedTokenAccount the token account to
|
|
1943
|
-
* @param amount the amount of
|
|
1943
|
+
* @param amount the amount of TokenIn, regardless of swapMode
|
|
1944
1944
|
* @param slippageBps the max slippage passed to jupiter api
|
|
1945
|
+
* @param swapMode jupiter swapMode (ExactIn or ExactOut), default is ExactIn
|
|
1945
1946
|
* @param route the jupiter route to use for the swap
|
|
1947
|
+
* @param reduceOnly specify if In or Out token on the drift account must reduceOnly, checked at end of swap
|
|
1946
1948
|
* @param txParams
|
|
1947
1949
|
*/
|
|
1948
1950
|
async swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, txParams, }) {
|
package/lib/math/spotBalance.js
CHANGED
|
@@ -286,7 +286,7 @@ function calculateTokenUtilizationLimits(depositTokenAmount, borrowTokenAmount,
|
|
|
286
286
|
// First, it determines a maximum withdrawal utilization from the market's target and historic utilization.
|
|
287
287
|
// Then, it deduces corresponding deposit/borrow amounts.
|
|
288
288
|
// Note: For deposit sizes below the guard threshold, withdrawals aren't blocked.
|
|
289
|
-
const maxWithdrawUtilization = anchor_1.BN.max(spotMarket.optimalUtilization, spotMarket.utilizationTwap.add(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(spotMarket.utilizationTwap).div(new anchor_1.BN(2))));
|
|
289
|
+
const maxWithdrawUtilization = anchor_1.BN.max(new anchor_1.BN(spotMarket.optimalUtilization), spotMarket.utilizationTwap.add(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(spotMarket.utilizationTwap).div(new anchor_1.BN(2))));
|
|
290
290
|
let minDepositTokensForUtilization = borrowTokenAmount
|
|
291
291
|
.mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
|
|
292
292
|
.div(maxWithdrawUtilization);
|
package/lib/user.d.ts
CHANGED
|
@@ -30,18 +30,22 @@ export declare class User {
|
|
|
30
30
|
getUserAccount(): UserAccount;
|
|
31
31
|
forceGetUserAccount(): Promise<UserAccount>;
|
|
32
32
|
getUserAccountAndSlot(): DataAndSlot<UserAccount> | undefined;
|
|
33
|
+
getPerpPositionForUserAccount(userAccount: UserAccount, marketIndex: number): PerpPosition | undefined;
|
|
33
34
|
/**
|
|
34
35
|
* Gets the user's current position for a given perp market. If the user has no position returns undefined
|
|
35
36
|
* @param marketIndex
|
|
36
37
|
* @returns userPerpPosition
|
|
37
38
|
*/
|
|
38
39
|
getPerpPosition(marketIndex: number): PerpPosition | undefined;
|
|
40
|
+
getPerpPositionAndSlot(marketIndex: number): DataAndSlot<PerpPosition | undefined>;
|
|
41
|
+
getSpotPositionForUserAccount(userAccount: UserAccount, marketIndex: number): SpotPosition | undefined;
|
|
39
42
|
/**
|
|
40
43
|
* Gets the user's current position for a given spot market. If the user has no position returns undefined
|
|
41
44
|
* @param marketIndex
|
|
42
45
|
* @returns userSpotPosition
|
|
43
46
|
*/
|
|
44
47
|
getSpotPosition(marketIndex: number): SpotPosition | undefined;
|
|
48
|
+
getSpotPositionAndSlot(marketIndex: number): DataAndSlot<SpotPosition | undefined>;
|
|
45
49
|
getEmptySpotPosition(marketIndex: number): SpotPosition;
|
|
46
50
|
/**
|
|
47
51
|
* Returns the token amount for a given market. The spot market precision is based on the token mint decimals.
|
|
@@ -52,17 +56,23 @@ export declare class User {
|
|
|
52
56
|
getTokenAmount(marketIndex: number): BN;
|
|
53
57
|
getEmptyPosition(marketIndex: number): PerpPosition;
|
|
54
58
|
getClonedPosition(position: PerpPosition): PerpPosition;
|
|
59
|
+
getOrderForUserAccount(userAccount: UserAccount, orderId: number): Order | undefined;
|
|
55
60
|
/**
|
|
56
61
|
* @param orderId
|
|
57
62
|
* @returns Order
|
|
58
63
|
*/
|
|
59
64
|
getOrder(orderId: number): Order | undefined;
|
|
65
|
+
getOrderAndSlot(orderId: number): DataAndSlot<Order | undefined>;
|
|
66
|
+
getOrderByUserIdForUserAccount(userAccount: UserAccount, userOrderId: number): Order | undefined;
|
|
60
67
|
/**
|
|
61
68
|
* @param userOrderId
|
|
62
69
|
* @returns Order
|
|
63
70
|
*/
|
|
64
71
|
getOrderByUserOrderId(userOrderId: number): Order | undefined;
|
|
72
|
+
getOrderByUserOrderIdAndSlot(userOrderId: number): DataAndSlot<Order | undefined>;
|
|
73
|
+
getOpenOrdersForUserAccount(userAccount?: UserAccount): Order[];
|
|
65
74
|
getOpenOrders(): Order[];
|
|
75
|
+
getOpenOrdersAndSlot(): DataAndSlot<Order[]>;
|
|
66
76
|
getUserAccountPublicKey(): PublicKey;
|
|
67
77
|
exists(): Promise<boolean>;
|
|
68
78
|
/**
|
|
@@ -107,8 +117,12 @@ export declare class User {
|
|
|
107
117
|
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
108
118
|
*/
|
|
109
119
|
getMaintenanceMarginRequirement(liquidationBuffer?: BN): BN;
|
|
120
|
+
getActivePerpPositionsForUserAccount(userAccount: UserAccount): PerpPosition[];
|
|
110
121
|
getActivePerpPositions(): PerpPosition[];
|
|
122
|
+
getActivePerpPositionsAndSlot(): DataAndSlot<PerpPosition[]>;
|
|
123
|
+
getActiveSpotPositionsForUserAccount(userAccount: UserAccount): SpotPosition[];
|
|
111
124
|
getActiveSpotPositions(): SpotPosition[];
|
|
125
|
+
getActiveSpotPositionsAndSlot(): DataAndSlot<SpotPosition[]>;
|
|
112
126
|
/**
|
|
113
127
|
* calculates unrealized position price pnl
|
|
114
128
|
* @returns : Precision QUOTE_PRECISION
|
package/lib/user.js
CHANGED
|
@@ -64,13 +64,28 @@ class User {
|
|
|
64
64
|
getUserAccountAndSlot() {
|
|
65
65
|
return this.accountSubscriber.getUserAccountAndSlot();
|
|
66
66
|
}
|
|
67
|
+
getPerpPositionForUserAccount(userAccount, marketIndex) {
|
|
68
|
+
return userAccount.perpPositions.find((position) => position.marketIndex === marketIndex);
|
|
69
|
+
}
|
|
67
70
|
/**
|
|
68
71
|
* Gets the user's current position for a given perp market. If the user has no position returns undefined
|
|
69
72
|
* @param marketIndex
|
|
70
73
|
* @returns userPerpPosition
|
|
71
74
|
*/
|
|
72
75
|
getPerpPosition(marketIndex) {
|
|
73
|
-
|
|
76
|
+
const userAccount = this.getUserAccount();
|
|
77
|
+
return this.getPerpPositionForUserAccount(userAccount, marketIndex);
|
|
78
|
+
}
|
|
79
|
+
getPerpPositionAndSlot(marketIndex) {
|
|
80
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
81
|
+
const perpPosition = this.getPerpPositionForUserAccount(userAccount.data, marketIndex);
|
|
82
|
+
return {
|
|
83
|
+
data: perpPosition,
|
|
84
|
+
slot: userAccount.slot,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
getSpotPositionForUserAccount(userAccount, marketIndex) {
|
|
88
|
+
return userAccount.spotPositions.find((position) => position.marketIndex === marketIndex);
|
|
74
89
|
}
|
|
75
90
|
/**
|
|
76
91
|
* Gets the user's current position for a given spot market. If the user has no position returns undefined
|
|
@@ -78,7 +93,16 @@ class User {
|
|
|
78
93
|
* @returns userSpotPosition
|
|
79
94
|
*/
|
|
80
95
|
getSpotPosition(marketIndex) {
|
|
81
|
-
|
|
96
|
+
const userAccount = this.getUserAccount();
|
|
97
|
+
return this.getSpotPositionForUserAccount(userAccount, marketIndex);
|
|
98
|
+
}
|
|
99
|
+
getSpotPositionAndSlot(marketIndex) {
|
|
100
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
101
|
+
const spotPosition = this.getSpotPositionForUserAccount(userAccount.data, marketIndex);
|
|
102
|
+
return {
|
|
103
|
+
data: spotPosition,
|
|
104
|
+
slot: userAccount.slot,
|
|
105
|
+
};
|
|
82
106
|
}
|
|
83
107
|
getEmptySpotPosition(marketIndex) {
|
|
84
108
|
return {
|
|
@@ -127,23 +151,58 @@ class User {
|
|
|
127
151
|
const clonedPosition = Object.assign({}, position);
|
|
128
152
|
return clonedPosition;
|
|
129
153
|
}
|
|
154
|
+
getOrderForUserAccount(userAccount, orderId) {
|
|
155
|
+
return userAccount.orders.find((order) => order.orderId === orderId);
|
|
156
|
+
}
|
|
130
157
|
/**
|
|
131
158
|
* @param orderId
|
|
132
159
|
* @returns Order
|
|
133
160
|
*/
|
|
134
161
|
getOrder(orderId) {
|
|
135
|
-
|
|
162
|
+
const userAccount = this.getUserAccount();
|
|
163
|
+
return this.getOrderForUserAccount(userAccount, orderId);
|
|
164
|
+
}
|
|
165
|
+
getOrderAndSlot(orderId) {
|
|
166
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
167
|
+
const order = this.getOrderForUserAccount(userAccount.data, orderId);
|
|
168
|
+
return {
|
|
169
|
+
data: order,
|
|
170
|
+
slot: userAccount.slot,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
getOrderByUserIdForUserAccount(userAccount, userOrderId) {
|
|
174
|
+
return userAccount.orders.find((order) => order.userOrderId === userOrderId);
|
|
136
175
|
}
|
|
137
176
|
/**
|
|
138
177
|
* @param userOrderId
|
|
139
178
|
* @returns Order
|
|
140
179
|
*/
|
|
141
180
|
getOrderByUserOrderId(userOrderId) {
|
|
142
|
-
|
|
181
|
+
const userAccount = this.getUserAccount();
|
|
182
|
+
return this.getOrderByUserIdForUserAccount(userAccount, userOrderId);
|
|
183
|
+
}
|
|
184
|
+
getOrderByUserOrderIdAndSlot(userOrderId) {
|
|
185
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
186
|
+
const order = this.getOrderByUserIdForUserAccount(userAccount.data, userOrderId);
|
|
187
|
+
return {
|
|
188
|
+
data: order,
|
|
189
|
+
slot: userAccount.slot,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
getOpenOrdersForUserAccount(userAccount) {
|
|
193
|
+
return userAccount === null || userAccount === void 0 ? void 0 : userAccount.orders.filter((order) => (0, types_1.isVariant)(order.status, 'open'));
|
|
143
194
|
}
|
|
144
195
|
getOpenOrders() {
|
|
145
|
-
|
|
146
|
-
return
|
|
196
|
+
const userAccount = this.getUserAccount();
|
|
197
|
+
return this.getOpenOrdersForUserAccount(userAccount);
|
|
198
|
+
}
|
|
199
|
+
getOpenOrdersAndSlot() {
|
|
200
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
201
|
+
const openOrders = this.getOpenOrdersForUserAccount(userAccount.data);
|
|
202
|
+
return {
|
|
203
|
+
data: openOrders,
|
|
204
|
+
slot: userAccount.slot,
|
|
205
|
+
};
|
|
147
206
|
}
|
|
148
207
|
getUserAccountPublicKey() {
|
|
149
208
|
return this.userAccountPublicKey;
|
|
@@ -316,14 +375,38 @@ class User {
|
|
|
316
375
|
getMaintenanceMarginRequirement(liquidationBuffer) {
|
|
317
376
|
return this.getMarginRequirement('Maintenance', liquidationBuffer);
|
|
318
377
|
}
|
|
319
|
-
|
|
320
|
-
return
|
|
378
|
+
getActivePerpPositionsForUserAccount(userAccount) {
|
|
379
|
+
return userAccount.perpPositions.filter((pos) => !pos.baseAssetAmount.eq(numericConstants_1.ZERO) ||
|
|
321
380
|
!pos.quoteAssetAmount.eq(numericConstants_1.ZERO) ||
|
|
322
381
|
!(pos.openOrders == 0) ||
|
|
323
382
|
!pos.lpShares.eq(numericConstants_1.ZERO));
|
|
324
383
|
}
|
|
384
|
+
getActivePerpPositions() {
|
|
385
|
+
const userAccount = this.getUserAccount();
|
|
386
|
+
return this.getActivePerpPositionsForUserAccount(userAccount);
|
|
387
|
+
}
|
|
388
|
+
getActivePerpPositionsAndSlot() {
|
|
389
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
390
|
+
const positions = this.getActivePerpPositionsForUserAccount(userAccount.data);
|
|
391
|
+
return {
|
|
392
|
+
data: positions,
|
|
393
|
+
slot: userAccount.slot,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
getActiveSpotPositionsForUserAccount(userAccount) {
|
|
397
|
+
return userAccount.spotPositions.filter((pos) => !(0, spotPosition_1.isSpotPositionAvailable)(pos));
|
|
398
|
+
}
|
|
325
399
|
getActiveSpotPositions() {
|
|
326
|
-
|
|
400
|
+
const userAccount = this.getUserAccount();
|
|
401
|
+
return this.getActiveSpotPositionsForUserAccount(userAccount);
|
|
402
|
+
}
|
|
403
|
+
getActiveSpotPositionsAndSlot() {
|
|
404
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
405
|
+
const positions = this.getActiveSpotPositionsForUserAccount(userAccount.data);
|
|
406
|
+
return {
|
|
407
|
+
data: positions,
|
|
408
|
+
slot: userAccount.slot,
|
|
409
|
+
};
|
|
327
410
|
}
|
|
328
411
|
/**
|
|
329
412
|
* calculates unrealized position price pnl
|
package/package.json
CHANGED
package/src/driftClient.ts
CHANGED
|
@@ -3353,9 +3353,11 @@ export class DriftClient {
|
|
|
3353
3353
|
* @param inMarketIndex the market index of the token you're selling
|
|
3354
3354
|
* @param outAssociatedTokenAccount the token account to receive the token being sold on jupiter
|
|
3355
3355
|
* @param inAssociatedTokenAccount the token account to
|
|
3356
|
-
* @param amount the amount of
|
|
3356
|
+
* @param amount the amount of TokenIn, regardless of swapMode
|
|
3357
3357
|
* @param slippageBps the max slippage passed to jupiter api
|
|
3358
|
+
* @param swapMode jupiter swapMode (ExactIn or ExactOut), default is ExactIn
|
|
3358
3359
|
* @param route the jupiter route to use for the swap
|
|
3360
|
+
* @param reduceOnly specify if In or Out token on the drift account must reduceOnly, checked at end of swap
|
|
3359
3361
|
* @param txParams
|
|
3360
3362
|
*/
|
|
3361
3363
|
public async swap({
|
package/src/math/spotBalance.ts
CHANGED
|
@@ -430,7 +430,7 @@ export function calculateTokenUtilizationLimits(
|
|
|
430
430
|
// Note: For deposit sizes below the guard threshold, withdrawals aren't blocked.
|
|
431
431
|
|
|
432
432
|
const maxWithdrawUtilization = BN.max(
|
|
433
|
-
spotMarket.optimalUtilization,
|
|
433
|
+
new BN(spotMarket.optimalUtilization),
|
|
434
434
|
spotMarket.utilizationTwap.add(
|
|
435
435
|
SPOT_MARKET_UTILIZATION_PRECISION.sub(spotMarket.utilizationTwap).div(
|
|
436
436
|
new BN(2)
|
package/src/user.ts
CHANGED
|
@@ -147,13 +147,44 @@ export class User {
|
|
|
147
147
|
return this.accountSubscriber.getUserAccountAndSlot();
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
+
public getPerpPositionForUserAccount(
|
|
151
|
+
userAccount: UserAccount,
|
|
152
|
+
marketIndex: number
|
|
153
|
+
): PerpPosition | undefined {
|
|
154
|
+
return userAccount.perpPositions.find(
|
|
155
|
+
(position) => position.marketIndex === marketIndex
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
150
159
|
/**
|
|
151
160
|
* Gets the user's current position for a given perp market. If the user has no position returns undefined
|
|
152
161
|
* @param marketIndex
|
|
153
162
|
* @returns userPerpPosition
|
|
154
163
|
*/
|
|
155
164
|
public getPerpPosition(marketIndex: number): PerpPosition | undefined {
|
|
156
|
-
|
|
165
|
+
const userAccount = this.getUserAccount();
|
|
166
|
+
return this.getPerpPositionForUserAccount(userAccount, marketIndex);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
public getPerpPositionAndSlot(
|
|
170
|
+
marketIndex: number
|
|
171
|
+
): DataAndSlot<PerpPosition | undefined> {
|
|
172
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
173
|
+
const perpPosition = this.getPerpPositionForUserAccount(
|
|
174
|
+
userAccount.data,
|
|
175
|
+
marketIndex
|
|
176
|
+
);
|
|
177
|
+
return {
|
|
178
|
+
data: perpPosition,
|
|
179
|
+
slot: userAccount.slot,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
public getSpotPositionForUserAccount(
|
|
184
|
+
userAccount: UserAccount,
|
|
185
|
+
marketIndex: number
|
|
186
|
+
): SpotPosition | undefined {
|
|
187
|
+
return userAccount.spotPositions.find(
|
|
157
188
|
(position) => position.marketIndex === marketIndex
|
|
158
189
|
);
|
|
159
190
|
}
|
|
@@ -164,9 +195,22 @@ export class User {
|
|
|
164
195
|
* @returns userSpotPosition
|
|
165
196
|
*/
|
|
166
197
|
public getSpotPosition(marketIndex: number): SpotPosition | undefined {
|
|
167
|
-
|
|
168
|
-
|
|
198
|
+
const userAccount = this.getUserAccount();
|
|
199
|
+
return this.getSpotPositionForUserAccount(userAccount, marketIndex);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public getSpotPositionAndSlot(
|
|
203
|
+
marketIndex: number
|
|
204
|
+
): DataAndSlot<SpotPosition | undefined> {
|
|
205
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
206
|
+
const spotPosition = this.getSpotPositionForUserAccount(
|
|
207
|
+
userAccount.data,
|
|
208
|
+
marketIndex
|
|
169
209
|
);
|
|
210
|
+
return {
|
|
211
|
+
data: spotPosition,
|
|
212
|
+
slot: userAccount.slot,
|
|
213
|
+
};
|
|
170
214
|
}
|
|
171
215
|
|
|
172
216
|
getEmptySpotPosition(marketIndex: number): SpotPosition {
|
|
@@ -227,13 +271,37 @@ export class User {
|
|
|
227
271
|
return clonedPosition;
|
|
228
272
|
}
|
|
229
273
|
|
|
274
|
+
public getOrderForUserAccount(
|
|
275
|
+
userAccount: UserAccount,
|
|
276
|
+
orderId: number
|
|
277
|
+
): Order | undefined {
|
|
278
|
+
return userAccount.orders.find((order) => order.orderId === orderId);
|
|
279
|
+
}
|
|
280
|
+
|
|
230
281
|
/**
|
|
231
282
|
* @param orderId
|
|
232
283
|
* @returns Order
|
|
233
284
|
*/
|
|
234
285
|
public getOrder(orderId: number): Order | undefined {
|
|
235
|
-
|
|
236
|
-
|
|
286
|
+
const userAccount = this.getUserAccount();
|
|
287
|
+
return this.getOrderForUserAccount(userAccount, orderId);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
public getOrderAndSlot(orderId: number): DataAndSlot<Order | undefined> {
|
|
291
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
292
|
+
const order = this.getOrderForUserAccount(userAccount.data, orderId);
|
|
293
|
+
return {
|
|
294
|
+
data: order,
|
|
295
|
+
slot: userAccount.slot,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
public getOrderByUserIdForUserAccount(
|
|
300
|
+
userAccount: UserAccount,
|
|
301
|
+
userOrderId: number
|
|
302
|
+
): Order | undefined {
|
|
303
|
+
return userAccount.orders.find(
|
|
304
|
+
(order) => order.userOrderId === userOrderId
|
|
237
305
|
);
|
|
238
306
|
}
|
|
239
307
|
|
|
@@ -242,17 +310,44 @@ export class User {
|
|
|
242
310
|
* @returns Order
|
|
243
311
|
*/
|
|
244
312
|
public getOrderByUserOrderId(userOrderId: number): Order | undefined {
|
|
245
|
-
|
|
246
|
-
|
|
313
|
+
const userAccount = this.getUserAccount();
|
|
314
|
+
return this.getOrderByUserIdForUserAccount(userAccount, userOrderId);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
public getOrderByUserOrderIdAndSlot(
|
|
318
|
+
userOrderId: number
|
|
319
|
+
): DataAndSlot<Order | undefined> {
|
|
320
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
321
|
+
const order = this.getOrderByUserIdForUserAccount(
|
|
322
|
+
userAccount.data,
|
|
323
|
+
userOrderId
|
|
247
324
|
);
|
|
325
|
+
return {
|
|
326
|
+
data: order,
|
|
327
|
+
slot: userAccount.slot,
|
|
328
|
+
};
|
|
248
329
|
}
|
|
249
330
|
|
|
250
|
-
public
|
|
251
|
-
return
|
|
331
|
+
public getOpenOrdersForUserAccount(userAccount?: UserAccount): Order[] {
|
|
332
|
+
return userAccount?.orders.filter((order) =>
|
|
252
333
|
isVariant(order.status, 'open')
|
|
253
334
|
);
|
|
254
335
|
}
|
|
255
336
|
|
|
337
|
+
public getOpenOrders(): Order[] {
|
|
338
|
+
const userAccount = this.getUserAccount();
|
|
339
|
+
return this.getOpenOrdersForUserAccount(userAccount);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
public getOpenOrdersAndSlot(): DataAndSlot<Order[]> {
|
|
343
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
344
|
+
const openOrders = this.getOpenOrdersForUserAccount(userAccount.data);
|
|
345
|
+
return {
|
|
346
|
+
data: openOrders,
|
|
347
|
+
slot: userAccount.slot,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
256
351
|
public getUserAccountPublicKey(): PublicKey {
|
|
257
352
|
return this.userAccountPublicKey;
|
|
258
353
|
}
|
|
@@ -502,8 +597,10 @@ export class User {
|
|
|
502
597
|
return this.getMarginRequirement('Maintenance', liquidationBuffer);
|
|
503
598
|
}
|
|
504
599
|
|
|
505
|
-
public
|
|
506
|
-
|
|
600
|
+
public getActivePerpPositionsForUserAccount(
|
|
601
|
+
userAccount: UserAccount
|
|
602
|
+
): PerpPosition[] {
|
|
603
|
+
return userAccount.perpPositions.filter(
|
|
507
604
|
(pos) =>
|
|
508
605
|
!pos.baseAssetAmount.eq(ZERO) ||
|
|
509
606
|
!pos.quoteAssetAmount.eq(ZERO) ||
|
|
@@ -512,12 +609,44 @@ export class User {
|
|
|
512
609
|
);
|
|
513
610
|
}
|
|
514
611
|
|
|
515
|
-
public
|
|
516
|
-
|
|
612
|
+
public getActivePerpPositions(): PerpPosition[] {
|
|
613
|
+
const userAccount = this.getUserAccount();
|
|
614
|
+
return this.getActivePerpPositionsForUserAccount(userAccount);
|
|
615
|
+
}
|
|
616
|
+
public getActivePerpPositionsAndSlot(): DataAndSlot<PerpPosition[]> {
|
|
617
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
618
|
+
const positions = this.getActivePerpPositionsForUserAccount(
|
|
619
|
+
userAccount.data
|
|
620
|
+
);
|
|
621
|
+
return {
|
|
622
|
+
data: positions,
|
|
623
|
+
slot: userAccount.slot,
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
public getActiveSpotPositionsForUserAccount(
|
|
628
|
+
userAccount: UserAccount
|
|
629
|
+
): SpotPosition[] {
|
|
630
|
+
return userAccount.spotPositions.filter(
|
|
517
631
|
(pos) => !isSpotPositionAvailable(pos)
|
|
518
632
|
);
|
|
519
633
|
}
|
|
520
634
|
|
|
635
|
+
public getActiveSpotPositions(): SpotPosition[] {
|
|
636
|
+
const userAccount = this.getUserAccount();
|
|
637
|
+
return this.getActiveSpotPositionsForUserAccount(userAccount);
|
|
638
|
+
}
|
|
639
|
+
public getActiveSpotPositionsAndSlot(): DataAndSlot<SpotPosition[]> {
|
|
640
|
+
const userAccount = this.getUserAccountAndSlot();
|
|
641
|
+
const positions = this.getActiveSpotPositionsForUserAccount(
|
|
642
|
+
userAccount.data
|
|
643
|
+
);
|
|
644
|
+
return {
|
|
645
|
+
data: positions,
|
|
646
|
+
slot: userAccount.slot,
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
|
|
521
650
|
/**
|
|
522
651
|
* calculates unrealized position price pnl
|
|
523
652
|
* @returns : Precision QUOTE_PRECISION
|