@velocity-exchange/sdk 0.0.2 → 0.0.3
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 +5 -6
- package/bunfig.toml +3 -0
- package/lib/browser/auctionSubscriber/auctionSubscriber.d.ts +4 -2
- package/lib/browser/auctionSubscriber/auctionSubscriber.js +9 -4
- package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.d.ts +4 -2
- package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.js +9 -4
- package/lib/browser/auctionSubscriber/types.d.ts +4 -2
- package/lib/browser/config.d.ts +3 -8
- package/lib/browser/config.js +14 -22
- package/lib/browser/constituentMap/constituentMap.d.ts +4 -2
- package/lib/browser/constituentMap/constituentMap.js +14 -10
- package/lib/browser/core/VelocityCore.d.ts +4 -0
- package/lib/browser/core/VelocityCore.js +3 -1
- package/lib/browser/dlob/DLOBSubscriber.d.ts +3 -1
- package/lib/browser/dlob/DLOBSubscriber.js +19 -10
- package/lib/browser/dlob/types.d.ts +3 -1
- package/lib/browser/idl/drift.d.ts +54 -1
- package/lib/browser/idl/drift.json +6046 -803
- package/lib/browser/orderSubscriber/OrderSubscriber.d.ts +3 -1
- package/lib/browser/orderSubscriber/OrderSubscriber.js +19 -11
- package/lib/browser/orderSubscriber/types.d.ts +3 -1
- package/lib/browser/priorityFee/priorityFeeSubscriber.d.ts +4 -2
- package/lib/browser/priorityFee/priorityFeeSubscriber.js +15 -11
- package/lib/browser/priorityFee/priorityFeeSubscriberMap.d.ts +11 -9
- package/lib/browser/priorityFee/priorityFeeSubscriberMap.js +18 -18
- package/lib/browser/priorityFee/types.d.ts +9 -5
- package/lib/browser/priorityFee/types.js +1 -1
- package/lib/browser/swift/grpcSignedMsgUserAccountSubscriber.d.ts +3 -12
- package/lib/browser/swift/grpcSignedMsgUserAccountSubscriber.js +3 -9
- package/lib/browser/swift/signedMsgUserAccountSubscriber.d.ts +11 -8
- package/lib/browser/swift/signedMsgUserAccountSubscriber.js +10 -5
- package/lib/browser/swift/swiftOrderSubscriber.d.ts +7 -4
- package/lib/browser/swift/swiftOrderSubscriber.js +19 -11
- package/lib/browser/user.d.ts +3 -1
- package/lib/browser/user.js +93 -86
- package/lib/browser/userConfig.d.ts +4 -2
- package/lib/browser/userMap/referrerMap.d.ts +5 -3
- package/lib/browser/userMap/referrerMap.js +18 -14
- package/lib/browser/userMap/revenueShareEscrowMap.d.ts +5 -3
- package/lib/browser/userMap/revenueShareEscrowMap.js +18 -14
- package/lib/browser/userMap/userMap.d.ts +3 -1
- package/lib/browser/userMap/userMap.js +24 -19
- package/lib/browser/userMap/userMapConfig.d.ts +4 -2
- package/lib/browser/userMap/userStatsMap.d.ts +5 -3
- package/lib/browser/userMap/userStatsMap.js +15 -11
- package/lib/browser/userStats.d.ts +3 -1
- package/lib/browser/userStats.js +21 -15
- package/lib/browser/userStatsConfig.d.ts +4 -2
- package/lib/browser/util/deprecatedAlias.d.ts +26 -0
- package/lib/browser/util/deprecatedAlias.js +10 -0
- package/lib/browser/velocityClient.js +5 -5
- package/lib/browser/velocityClientConfig.d.ts +7 -4
- package/lib/node/auctionSubscriber/auctionSubscriber.d.ts +4 -2
- package/lib/node/auctionSubscriber/auctionSubscriber.d.ts.map +1 -1
- package/lib/node/auctionSubscriber/auctionSubscriber.js +9 -4
- package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts +4 -2
- package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts.map +1 -1
- package/lib/node/auctionSubscriber/auctionSubscriberGrpc.js +9 -4
- package/lib/node/auctionSubscriber/types.d.ts +4 -2
- package/lib/node/auctionSubscriber/types.d.ts.map +1 -1
- package/lib/node/config.d.ts +3 -8
- package/lib/node/config.d.ts.map +1 -1
- package/lib/node/config.js +14 -22
- package/lib/node/constituentMap/constituentMap.d.ts +4 -2
- package/lib/node/constituentMap/constituentMap.d.ts.map +1 -1
- package/lib/node/constituentMap/constituentMap.js +14 -10
- package/lib/node/core/VelocityCore.d.ts +4 -0
- package/lib/node/core/VelocityCore.d.ts.map +1 -1
- package/lib/node/core/VelocityCore.js +3 -1
- package/lib/node/dlob/DLOBSubscriber.d.ts +3 -1
- package/lib/node/dlob/DLOBSubscriber.d.ts.map +1 -1
- package/lib/node/dlob/DLOBSubscriber.js +19 -10
- package/lib/node/dlob/types.d.ts +3 -1
- package/lib/node/dlob/types.d.ts.map +1 -1
- package/lib/node/idl/drift.d.ts +54 -1
- package/lib/node/idl/drift.d.ts.map +1 -1
- package/lib/node/idl/drift.json +6046 -803
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts +3 -1
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
- package/lib/node/orderSubscriber/OrderSubscriber.js +19 -11
- package/lib/node/orderSubscriber/types.d.ts +3 -1
- package/lib/node/orderSubscriber/types.d.ts.map +1 -1
- package/lib/node/priorityFee/priorityFeeSubscriber.d.ts +4 -2
- package/lib/node/priorityFee/priorityFeeSubscriber.d.ts.map +1 -1
- package/lib/node/priorityFee/priorityFeeSubscriber.js +15 -11
- package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts +11 -9
- package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts.map +1 -1
- package/lib/node/priorityFee/priorityFeeSubscriberMap.js +18 -18
- package/lib/node/priorityFee/types.d.ts +9 -5
- package/lib/node/priorityFee/types.d.ts.map +1 -1
- package/lib/node/priorityFee/types.js +1 -1
- package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.d.ts +3 -12
- package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.d.ts.map +1 -1
- package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.js +3 -9
- package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts +11 -8
- package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts.map +1 -1
- package/lib/node/swift/signedMsgUserAccountSubscriber.js +10 -5
- package/lib/node/swift/swiftOrderSubscriber.d.ts +7 -4
- package/lib/node/swift/swiftOrderSubscriber.d.ts.map +1 -1
- package/lib/node/swift/swiftOrderSubscriber.js +19 -11
- package/lib/node/user.d.ts +3 -1
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/user.js +93 -86
- package/lib/node/userConfig.d.ts +4 -2
- package/lib/node/userConfig.d.ts.map +1 -1
- package/lib/node/userMap/referrerMap.d.ts +5 -3
- package/lib/node/userMap/referrerMap.d.ts.map +1 -1
- package/lib/node/userMap/referrerMap.js +18 -14
- package/lib/node/userMap/revenueShareEscrowMap.d.ts +5 -3
- package/lib/node/userMap/revenueShareEscrowMap.d.ts.map +1 -1
- package/lib/node/userMap/revenueShareEscrowMap.js +18 -14
- package/lib/node/userMap/userMap.d.ts +3 -1
- package/lib/node/userMap/userMap.d.ts.map +1 -1
- package/lib/node/userMap/userMap.js +24 -19
- package/lib/node/userMap/userMapConfig.d.ts +4 -2
- package/lib/node/userMap/userMapConfig.d.ts.map +1 -1
- package/lib/node/userMap/userStatsMap.d.ts +5 -3
- package/lib/node/userMap/userStatsMap.d.ts.map +1 -1
- package/lib/node/userMap/userStatsMap.js +15 -11
- package/lib/node/userStats.d.ts +3 -1
- package/lib/node/userStats.d.ts.map +1 -1
- package/lib/node/userStats.js +21 -15
- package/lib/node/userStatsConfig.d.ts +4 -2
- package/lib/node/userStatsConfig.d.ts.map +1 -1
- package/lib/node/util/deprecatedAlias.d.ts +27 -0
- package/lib/node/util/deprecatedAlias.d.ts.map +1 -0
- package/lib/node/util/deprecatedAlias.js +10 -0
- package/lib/node/velocityClient.js +5 -5
- package/lib/node/velocityClientConfig.d.ts +7 -4
- package/lib/node/velocityClientConfig.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/auctionSubscriber/auctionSubscriber.ts +12 -6
- package/src/auctionSubscriber/auctionSubscriberGrpc.ts +12 -6
- package/src/auctionSubscriber/types.ts +5 -2
- package/src/config.ts +35 -27
- package/src/constituentMap/constituentMap.ts +17 -9
- package/src/core/VelocityCore.ts +6 -0
- package/src/dlob/DLOBSubscriber.ts +21 -11
- package/src/dlob/types.ts +3 -1
- package/src/idl/drift.json +23940 -18697
- package/src/idl/drift.ts +54 -1
- package/src/orderSubscriber/OrderSubscriber.ts +17 -7
- package/src/orderSubscriber/types.ts +3 -1
- package/src/priorityFee/priorityFeeSubscriber.ts +13 -9
- package/src/priorityFee/priorityFeeSubscriberMap.ts +23 -25
- package/src/priorityFee/types.ts +14 -9
- package/src/swift/grpcSignedMsgUserAccountSubscriber.ts +10 -23
- package/src/swift/signedMsgUserAccountSubscriber.ts +22 -14
- package/src/swift/swiftOrderSubscriber.ts +24 -16
- package/src/user.ts +93 -82
- package/src/userConfig.ts +5 -2
- package/src/userMap/referrerMap.ts +19 -15
- package/src/userMap/revenueShareEscrowMap.ts +20 -16
- package/src/userMap/userMap.ts +20 -15
- package/src/userMap/userMapConfig.ts +6 -3
- package/src/userMap/userStatsMap.ts +19 -15
- package/src/userStats.ts +13 -7
- package/src/userStatsConfig.ts +5 -2
- package/src/util/deprecatedAlias.ts +25 -0
- package/src/velocityClient.ts +6 -6
- package/src/velocityClientConfig.ts +25 -42
package/lib/browser/user.js
CHANGED
|
@@ -27,6 +27,10 @@ const strictOraclePrice_1 = require("./oracles/strictOraclePrice");
|
|
|
27
27
|
const grpcUserAccountSubscriber_1 = require("./accounts/grpcUserAccountSubscriber");
|
|
28
28
|
const marginCalculation_1 = require("./marginCalculation");
|
|
29
29
|
class User {
|
|
30
|
+
/** @deprecated Use `velocityClient` instead. `driftClient` will be removed in a future major. */
|
|
31
|
+
get driftClient() {
|
|
32
|
+
return this.velocityClient;
|
|
33
|
+
}
|
|
30
34
|
get isSubscribed() {
|
|
31
35
|
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
32
36
|
}
|
|
@@ -34,38 +38,40 @@ class User {
|
|
|
34
38
|
this._isSubscribed = val;
|
|
35
39
|
}
|
|
36
40
|
constructor(config) {
|
|
37
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
41
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
38
42
|
this._isSubscribed = false;
|
|
39
|
-
|
|
43
|
+
// Type-system guarantees at least one of the two is supplied.
|
|
44
|
+
const velocityClient = ((_a = config.velocityClient) !== null && _a !== void 0 ? _a : config.driftClient);
|
|
45
|
+
this.velocityClient = velocityClient;
|
|
40
46
|
this.userAccountPublicKey = config.userAccountPublicKey;
|
|
41
|
-
if (((
|
|
42
|
-
this.accountSubscriber = new pollingUserAccountSubscriber_1.PollingUserAccountSubscriber(
|
|
47
|
+
if (((_b = config.accountSubscription) === null || _b === void 0 ? void 0 : _b.type) === 'polling') {
|
|
48
|
+
this.accountSubscriber = new pollingUserAccountSubscriber_1.PollingUserAccountSubscriber(velocityClient.connection, config.userAccountPublicKey, config.accountSubscription.accountLoader, this.velocityClient.program.account.user.coder.accounts.decodeUnchecked.bind(this.velocityClient.program.account.user.coder.accounts));
|
|
43
49
|
}
|
|
44
|
-
else if (((
|
|
50
|
+
else if (((_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.type) === 'custom') {
|
|
45
51
|
this.accountSubscriber = config.accountSubscription.userAccountSubscriber;
|
|
46
52
|
}
|
|
47
|
-
else if (((
|
|
53
|
+
else if (((_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.type) === 'grpc') {
|
|
48
54
|
if (config.accountSubscription.grpcMultiUserAccountSubscriber) {
|
|
49
55
|
this.accountSubscriber =
|
|
50
56
|
config.accountSubscription.grpcMultiUserAccountSubscriber.forUser(config.userAccountPublicKey);
|
|
51
57
|
}
|
|
52
58
|
else {
|
|
53
|
-
this.accountSubscriber = new grpcUserAccountSubscriber_1.grpcUserAccountSubscriber(config.accountSubscription.grpcConfigs,
|
|
54
|
-
resubTimeoutMs: (
|
|
55
|
-
logResubMessages: (
|
|
59
|
+
this.accountSubscriber = new grpcUserAccountSubscriber_1.grpcUserAccountSubscriber(config.accountSubscription.grpcConfigs, velocityClient.program, config.userAccountPublicKey, {
|
|
60
|
+
resubTimeoutMs: (_e = config.accountSubscription) === null || _e === void 0 ? void 0 : _e.resubTimeoutMs,
|
|
61
|
+
logResubMessages: (_f = config.accountSubscription) === null || _f === void 0 ? void 0 : _f.logResubMessages,
|
|
56
62
|
});
|
|
57
63
|
}
|
|
58
64
|
}
|
|
59
65
|
else {
|
|
60
|
-
if (((
|
|
61
|
-
((
|
|
62
|
-
this.accountSubscriber = new websocketProgramUserAccountSubscriber_1.WebSocketProgramUserAccountSubscriber(
|
|
66
|
+
if (((_g = config.accountSubscription) === null || _g === void 0 ? void 0 : _g.type) === 'websocket' &&
|
|
67
|
+
((_h = config.accountSubscription) === null || _h === void 0 ? void 0 : _h.programUserAccountSubscriber)) {
|
|
68
|
+
this.accountSubscriber = new websocketProgramUserAccountSubscriber_1.WebSocketProgramUserAccountSubscriber(velocityClient.program, config.userAccountPublicKey, config.accountSubscription.programUserAccountSubscriber);
|
|
63
69
|
}
|
|
64
70
|
else {
|
|
65
|
-
this.accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(
|
|
66
|
-
resubTimeoutMs: (
|
|
67
|
-
logResubMessages: (
|
|
68
|
-
}, (
|
|
71
|
+
this.accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(velocityClient.program, config.userAccountPublicKey, {
|
|
72
|
+
resubTimeoutMs: (_j = config.accountSubscription) === null || _j === void 0 ? void 0 : _j.resubTimeoutMs,
|
|
73
|
+
logResubMessages: (_k = config.accountSubscription) === null || _k === void 0 ? void 0 : _k.logResubMessages,
|
|
74
|
+
}, (_l = config.accountSubscription) === null || _l === void 0 ? void 0 : _l.commitment);
|
|
69
75
|
}
|
|
70
76
|
}
|
|
71
77
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
@@ -166,7 +172,7 @@ class User {
|
|
|
166
172
|
if (spotPosition === undefined) {
|
|
167
173
|
return numericConstants_1.ZERO;
|
|
168
174
|
}
|
|
169
|
-
const spotMarket = this.
|
|
175
|
+
const spotMarket = this.velocityClient.getSpotMarketAccount(marketIndex);
|
|
170
176
|
return (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_2.getTokenAmount)(spotPosition.scaledBalance, spotMarket, spotPosition.balanceType), spotPosition.balanceType);
|
|
171
177
|
}
|
|
172
178
|
getEmptyPosition(marketIndex) {
|
|
@@ -195,8 +201,8 @@ class User {
|
|
|
195
201
|
const perpPosition = this.getPerpPosition(perpMarketIndex);
|
|
196
202
|
if (!perpPosition)
|
|
197
203
|
return numericConstants_1.ZERO;
|
|
198
|
-
const perpMarket = this.
|
|
199
|
-
const spotMarket = this.
|
|
204
|
+
const perpMarket = this.velocityClient.getPerpMarketAccount(perpMarketIndex);
|
|
205
|
+
const spotMarket = this.velocityClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
|
|
200
206
|
if (perpPosition === undefined) {
|
|
201
207
|
return numericConstants_1.ZERO;
|
|
202
208
|
}
|
|
@@ -212,8 +218,8 @@ class User {
|
|
|
212
218
|
if (!((_a = perpPosition.isolatedPositionScaledBalance) === null || _a === void 0 ? void 0 : _a.gt(numericConstants_1.ZERO))) {
|
|
213
219
|
return total;
|
|
214
220
|
}
|
|
215
|
-
const perpMarket = this.
|
|
216
|
-
const quoteSpotMarket = this.
|
|
221
|
+
const perpMarket = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
222
|
+
const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
|
|
217
223
|
const quoteOraclePriceData = this.getOracleDataForSpotMarket(perpMarket.quoteSpotMarketIndex);
|
|
218
224
|
const strictOracle = new strictOraclePrice_1.StrictOraclePrice(quoteOraclePriceData.price, quoteOraclePriceData.twap);
|
|
219
225
|
const tokenAmount = (0, spotBalance_2.getTokenAmount)(perpPosition.isolatedPositionScaledBalance, quoteSpotMarket, types_2.SpotBalanceType.DEPOSIT);
|
|
@@ -281,7 +287,7 @@ class User {
|
|
|
281
287
|
return this.userAccountPublicKey;
|
|
282
288
|
}
|
|
283
289
|
async exists() {
|
|
284
|
-
const userAccountRPCResponse = await this.
|
|
290
|
+
const userAccountRPCResponse = await this.velocityClient.connection.getParsedAccountInfo(this.userAccountPublicKey);
|
|
285
291
|
return userAccountRPCResponse.value !== null;
|
|
286
292
|
}
|
|
287
293
|
/**
|
|
@@ -301,7 +307,7 @@ class User {
|
|
|
301
307
|
*/
|
|
302
308
|
getPerpBuyingPower(marketIndex, collateralBuffer = numericConstants_1.ZERO, maxMarginRatio = undefined, positionType = 'cross') {
|
|
303
309
|
const perpPosition = this.getPerpPositionOrEmpty(marketIndex);
|
|
304
|
-
const perpMarket = this.
|
|
310
|
+
const perpMarket = this.velocityClient.getPerpMarketAccount(marketIndex);
|
|
305
311
|
const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
|
|
306
312
|
const worstCaseBaseAssetAmount = perpPosition
|
|
307
313
|
? (0, margin_2.calculateWorstCaseBaseAssetAmount)(perpPosition, perpMarket, oraclePriceData.price)
|
|
@@ -322,7 +328,7 @@ class User {
|
|
|
322
328
|
}
|
|
323
329
|
getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, baseAssetAmount, perpMarketMaxMarginRatio = undefined) {
|
|
324
330
|
const maxMarginRatio = Math.max(perpMarketMaxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
325
|
-
const marginRatio = (0, market_1.calculateMarketMarginRatio)(this.
|
|
331
|
+
const marginRatio = (0, market_1.calculateMarketMarginRatio)(this.velocityClient.getPerpMarketAccount(marketIndex), baseAssetAmount, 'Initial', maxMarginRatio);
|
|
326
332
|
return freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new anchor_1.BN(marginRatio));
|
|
327
333
|
}
|
|
328
334
|
/**
|
|
@@ -434,9 +440,9 @@ class User {
|
|
|
434
440
|
return this.getActivePerpPositions()
|
|
435
441
|
.filter((pos) => marketIndex !== undefined ? pos.marketIndex === marketIndex : true)
|
|
436
442
|
.reduce((unrealizedPnl, perpPosition) => {
|
|
437
|
-
const market = this.
|
|
443
|
+
const market = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
438
444
|
const oraclePriceData = this.getMMOracleDataForPerpMarket(market.marketIndex);
|
|
439
|
-
const quoteSpotMarket = this.
|
|
445
|
+
const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
|
|
440
446
|
const quoteOraclePriceData = this.getOracleDataForSpotMarket(market.quoteSpotMarketIndex);
|
|
441
447
|
let positionUnrealizedPnl = (0, position_2.calculatePositionPNL)(market, perpPosition, withFunding, oraclePriceData);
|
|
442
448
|
let quotePrice;
|
|
@@ -473,7 +479,7 @@ class User {
|
|
|
473
479
|
return this.getUserAccount()
|
|
474
480
|
.perpPositions.filter((pos) => marketIndex !== undefined ? pos.marketIndex === marketIndex : true)
|
|
475
481
|
.reduce((pnl, perpPosition) => {
|
|
476
|
-
const market = this.
|
|
482
|
+
const market = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
477
483
|
return pnl.add((0, position_1.calculateUnsettledFundingPnl)(market, perpPosition));
|
|
478
484
|
}, numericConstants_1.ZERO);
|
|
479
485
|
}
|
|
@@ -491,7 +497,7 @@ class User {
|
|
|
491
497
|
(!countForBase && !countForQuote)) {
|
|
492
498
|
continue;
|
|
493
499
|
}
|
|
494
|
-
const spotMarketAccount = this.
|
|
500
|
+
const spotMarketAccount = this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
|
|
495
501
|
const oraclePriceData = this.getOracleDataForSpotMarket(spotPosition.marketIndex);
|
|
496
502
|
let twap5min;
|
|
497
503
|
if (strict) {
|
|
@@ -617,12 +623,12 @@ class User {
|
|
|
617
623
|
getLiquidationBuffer() {
|
|
618
624
|
const liquidationBufferMap = new Map();
|
|
619
625
|
if (this.isBeingLiquidated()) {
|
|
620
|
-
liquidationBufferMap.set('cross', new anchor_1.BN(this.
|
|
626
|
+
liquidationBufferMap.set('cross', new anchor_1.BN(this.velocityClient.getStateAccount().liquidationMarginBufferRatio));
|
|
621
627
|
}
|
|
622
628
|
for (const position of this.getActivePerpPositions()) {
|
|
623
629
|
if (position.positionFlag &
|
|
624
630
|
(types_2.PositionFlag.BeingLiquidated | types_2.PositionFlag.Bankruptcy)) {
|
|
625
|
-
liquidationBufferMap.set(position.marketIndex, new anchor_1.BN(this.
|
|
631
|
+
liquidationBufferMap.set(position.marketIndex, new anchor_1.BN(this.velocityClient.getStateAccount().liquidationMarginBufferRatio));
|
|
626
632
|
}
|
|
627
633
|
}
|
|
628
634
|
return liquidationBufferMap;
|
|
@@ -667,7 +673,7 @@ class User {
|
|
|
667
673
|
return health;
|
|
668
674
|
}
|
|
669
675
|
calculateWeightedPerpPositionLiability(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict = false) {
|
|
670
|
-
const market = this.
|
|
676
|
+
const market = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
671
677
|
let valuationPrice = this.getOracleDataForPerpMarket(market.marketIndex).price;
|
|
672
678
|
if ((0, types_1.isVariant)(market.status, 'settlement')) {
|
|
673
679
|
valuationPrice = market.expiryPrice;
|
|
@@ -692,8 +698,8 @@ class User {
|
|
|
692
698
|
if ((0, types_1.isVariant)(market.status, 'settlement')) {
|
|
693
699
|
marginRatio = numericConstants_1.ZERO;
|
|
694
700
|
}
|
|
695
|
-
const quoteSpotMarket = this.
|
|
696
|
-
const quoteOraclePriceData = this.
|
|
701
|
+
const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
|
|
702
|
+
const quoteOraclePriceData = this.velocityClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
697
703
|
let quotePrice;
|
|
698
704
|
if (strict) {
|
|
699
705
|
quotePrice = anchor_1.BN.max(quoteOraclePriceData.price, quoteSpotMarket.historicalOracleData.lastOraclePriceTwap5Min);
|
|
@@ -736,7 +742,7 @@ class User {
|
|
|
736
742
|
*/
|
|
737
743
|
getPerpPositionValue(marketIndex, oraclePriceData, includeOpenOrders = false) {
|
|
738
744
|
const userPosition = this.getPerpPositionOrEmpty(marketIndex);
|
|
739
|
-
const market = this.
|
|
745
|
+
const market = this.velocityClient.getPerpMarketAccount(userPosition.marketIndex);
|
|
740
746
|
return (0, margin_2.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData, includeOpenOrders);
|
|
741
747
|
}
|
|
742
748
|
/**
|
|
@@ -745,7 +751,7 @@ class User {
|
|
|
745
751
|
*/
|
|
746
752
|
getPerpLiabilityValue(marketIndex, oraclePriceData, includeOpenOrders = false) {
|
|
747
753
|
const userPosition = this.getPerpPositionOrEmpty(marketIndex);
|
|
748
|
-
const market = this.
|
|
754
|
+
const market = this.velocityClient.getPerpMarketAccount(userPosition.marketIndex);
|
|
749
755
|
if (includeOpenOrders) {
|
|
750
756
|
return (0, margin_1.calculateWorstCasePerpLiabilityValue)(userPosition, market, oraclePriceData.price).worstCaseLiabilityValue;
|
|
751
757
|
}
|
|
@@ -769,7 +775,7 @@ class User {
|
|
|
769
775
|
* @returns : Precision PRICE_PRECISION
|
|
770
776
|
*/
|
|
771
777
|
getPositionEstimatedExitPriceAndPnl(position, amountToClose, useAMMClose = false) {
|
|
772
|
-
const market = this.
|
|
778
|
+
const market = this.velocityClient.getPerpMarketAccount(position.marketIndex);
|
|
773
779
|
const entryPrice = (0, position_1.calculateEntryPrice)(position);
|
|
774
780
|
const oraclePriceData = this.getMMOracleDataForPerpMarket(position.marketIndex);
|
|
775
781
|
if (amountToClose) {
|
|
@@ -825,9 +831,9 @@ class User {
|
|
|
825
831
|
if (perpMarketIndex) {
|
|
826
832
|
const perpPosition = this.getPerpPositionOrEmpty(perpMarketIndex);
|
|
827
833
|
const perpLiability = this.calculateWeightedPerpPositionLiability(perpPosition, marginCategory, undefined, includeOpenOrders);
|
|
828
|
-
const perpMarket = this.
|
|
834
|
+
const perpMarket = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
829
835
|
const oraclePriceData = this.getOracleDataForPerpMarket(perpPosition.marketIndex);
|
|
830
|
-
const quoteSpotMarket = this.
|
|
836
|
+
const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
|
|
831
837
|
const quoteOraclePriceData = this.getOracleDataForSpotMarket(perpMarket.quoteSpotMarketIndex);
|
|
832
838
|
const strictOracle = new strictOraclePrice_1.StrictOraclePrice(quoteOraclePriceData.price, quoteOraclePriceData.twap);
|
|
833
839
|
const positionUnrealizedPnl = (0, position_2.calculatePositionPNL)(perpMarket, perpPosition, true, oraclePriceData);
|
|
@@ -872,7 +878,7 @@ class User {
|
|
|
872
878
|
return false;
|
|
873
879
|
}
|
|
874
880
|
getSpotMarketAccountsWithDustPosition() {
|
|
875
|
-
const spotMarketAccounts = this.
|
|
881
|
+
const spotMarketAccounts = this.velocityClient.getSpotMarketAccounts();
|
|
876
882
|
const dustPositionAccounts = [];
|
|
877
883
|
for (const spotMarketAccount of spotMarketAccounts) {
|
|
878
884
|
const isDust = this.isDustDepositPosition(spotMarketAccount);
|
|
@@ -1095,7 +1101,7 @@ class User {
|
|
|
1095
1101
|
if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
1096
1102
|
continue;
|
|
1097
1103
|
}
|
|
1098
|
-
const market = this.
|
|
1104
|
+
const market = this.velocityClient.getPerpMarketAccount(userPosition.marketIndex);
|
|
1099
1105
|
if (market.amm.cumulativeFundingRateLong.eq(userPosition.lastCumulativeFundingRate) ||
|
|
1100
1106
|
market.amm.cumulativeFundingRateShort.eq(userPosition.lastCumulativeFundingRate)) {
|
|
1101
1107
|
continue;
|
|
@@ -1117,7 +1123,7 @@ class User {
|
|
|
1117
1123
|
const totalCollateral = this.getTotalCollateral('Maintenance');
|
|
1118
1124
|
const maintenanceMarginRequirement = this.getMaintenanceMarginRequirement();
|
|
1119
1125
|
const freeCollateral = anchor_1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(maintenanceMarginRequirement));
|
|
1120
|
-
const market = this.
|
|
1126
|
+
const market = this.velocityClient.getSpotMarketAccount(marketIndex);
|
|
1121
1127
|
let signedTokenAmount = (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_2.getTokenAmount)(currentSpotPosition.scaledBalance, market, currentSpotPosition.balanceType), currentSpotPosition.balanceType);
|
|
1122
1128
|
signedTokenAmount = signedTokenAmount.add(positionBaseSizeChange);
|
|
1123
1129
|
if (signedTokenAmount.eq(numericConstants_1.ZERO)) {
|
|
@@ -1125,10 +1131,10 @@ class User {
|
|
|
1125
1131
|
}
|
|
1126
1132
|
let freeCollateralDelta = this.calculateFreeCollateralDeltaForSpot(market, signedTokenAmount);
|
|
1127
1133
|
const oracle = market.oracle;
|
|
1128
|
-
const perpMarketWithSameOracle = this.
|
|
1134
|
+
const perpMarketWithSameOracle = this.velocityClient
|
|
1129
1135
|
.getPerpMarketAccounts()
|
|
1130
1136
|
.find((market) => market.amm.oracle.equals(oracle));
|
|
1131
|
-
const oraclePrice = this.
|
|
1137
|
+
const oraclePrice = this.velocityClient.getOracleDataForSpotMarket(marketIndex).price;
|
|
1132
1138
|
if (perpMarketWithSameOracle) {
|
|
1133
1139
|
const perpPosition = this.getPerpPositionOrEmpty(perpMarketWithSameOracle.marketIndex);
|
|
1134
1140
|
if (perpPosition) {
|
|
@@ -1165,9 +1171,9 @@ class User {
|
|
|
1165
1171
|
* @returns Precision : PRICE_PRECISION
|
|
1166
1172
|
*/
|
|
1167
1173
|
liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO, marginCategory = 'Maintenance', includeOpenOrders = false, offsetCollateral = numericConstants_1.ZERO, marginType) {
|
|
1168
|
-
const market = this.
|
|
1169
|
-
const oracle = this.
|
|
1170
|
-
const oraclePrice = this.
|
|
1174
|
+
const market = this.velocityClient.getPerpMarketAccount(marketIndex);
|
|
1175
|
+
const oracle = this.velocityClient.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
1176
|
+
const oraclePrice = this.velocityClient.getOracleDataForPerpMarket(marketIndex).price;
|
|
1171
1177
|
const currentPerpPosition = this.getPerpPositionOrEmpty(marketIndex);
|
|
1172
1178
|
if (marginType === 'Isolated') {
|
|
1173
1179
|
const marginCalculation = this.getMarginCalculation(marginCategory, {
|
|
@@ -1202,7 +1208,7 @@ class User {
|
|
|
1202
1208
|
if (!freeCollateralDelta) {
|
|
1203
1209
|
return new anchor_1.BN(-1);
|
|
1204
1210
|
}
|
|
1205
|
-
const spotMarketWithSameOracle = this.
|
|
1211
|
+
const spotMarketWithSameOracle = this.velocityClient
|
|
1206
1212
|
.getSpotMarketAccounts()
|
|
1207
1213
|
.find((market) => market.oracle.equals(oracle));
|
|
1208
1214
|
if (spotMarketWithSameOracle) {
|
|
@@ -1250,7 +1256,7 @@ class User {
|
|
|
1250
1256
|
freeCollateralChange = newPositionValue.sub(costBasis);
|
|
1251
1257
|
}
|
|
1252
1258
|
// assume worst fee tier
|
|
1253
|
-
const takerFeeTier = this.
|
|
1259
|
+
const takerFeeTier = this.velocityClient.getStateAccount().perpFeeStructure.feeTiers[0];
|
|
1254
1260
|
const takerFee = newPositionValue
|
|
1255
1261
|
.muln(takerFeeTier.feeNumerator)
|
|
1256
1262
|
.divn(takerFeeTier.feeDenominator);
|
|
@@ -1315,7 +1321,8 @@ class User {
|
|
|
1315
1321
|
calculateFreeCollateralDeltaForSpot(market, signedTokenAmount, marginCategory = 'Maintenance') {
|
|
1316
1322
|
const tokenPrecision = new anchor_1.BN(Math.pow(10, market.decimals));
|
|
1317
1323
|
if (signedTokenAmount.gt(numericConstants_1.ZERO)) {
|
|
1318
|
-
const assetWeight = (0, spotBalance_2.calculateAssetWeight)(signedTokenAmount, this.
|
|
1324
|
+
const assetWeight = (0, spotBalance_2.calculateAssetWeight)(signedTokenAmount, this.velocityClient.getOracleDataForSpotMarket(market.marketIndex)
|
|
1325
|
+
.price, market, marginCategory);
|
|
1319
1326
|
return numericConstants_1.QUOTE_PRECISION.mul(assetWeight)
|
|
1320
1327
|
.div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
|
|
1321
1328
|
.mul(signedTokenAmount)
|
|
@@ -1349,11 +1356,11 @@ class User {
|
|
|
1349
1356
|
}
|
|
1350
1357
|
getMarginUSDCRequiredForTrade(targetMarketIndex, baseSize, estEntryPrice, perpMarketMaxMarginRatio) {
|
|
1351
1358
|
const maxMarginRatio = Math.max(perpMarketMaxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
1352
|
-
return (0, margin_2.calculateMarginUSDCRequiredForTrade)(this.
|
|
1359
|
+
return (0, margin_2.calculateMarginUSDCRequiredForTrade)(this.velocityClient, targetMarketIndex, baseSize, maxMarginRatio, estEntryPrice);
|
|
1353
1360
|
}
|
|
1354
1361
|
getCollateralDepositRequiredForTrade(targetMarketIndex, baseSize, collateralIndex, perpMarketMaxMarginRatio) {
|
|
1355
1362
|
const maxMarginRatio = Math.max(perpMarketMaxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
1356
|
-
return (0, margin_2.calculateCollateralDepositRequiredForTrade)(this.
|
|
1363
|
+
return (0, margin_2.calculateCollateralDepositRequiredForTrade)(this.velocityClient, targetMarketIndex, baseSize, collateralIndex, maxMarginRatio);
|
|
1357
1364
|
}
|
|
1358
1365
|
/**
|
|
1359
1366
|
* Separates the max trade size into two parts:
|
|
@@ -1375,7 +1382,7 @@ class User {
|
|
|
1375
1382
|
? true
|
|
1376
1383
|
: targetSide === currentPositionSide;
|
|
1377
1384
|
const oracleData = this.getMMOracleDataForPerpMarket(targetMarketIndex);
|
|
1378
|
-
const marketAccount = this.
|
|
1385
|
+
const marketAccount = this.velocityClient.getPerpMarketAccount(targetMarketIndex);
|
|
1379
1386
|
// add any position we have on the opposite side of the current trade, because we can "flip" the size of this position without taking any extra leverage.
|
|
1380
1387
|
const oppositeSizeLiabilityValue = targetingSameSide
|
|
1381
1388
|
? numericConstants_1.ZERO
|
|
@@ -1446,8 +1453,8 @@ class User {
|
|
|
1446
1453
|
* @returns tradeSizeAllowed : Precision QUOTE_PRECISION
|
|
1447
1454
|
*/
|
|
1448
1455
|
getMaxTradeSizeUSDCForSpot(targetMarketIndex, direction, currentQuoteAssetValue, currentSpotMarketNetValue) {
|
|
1449
|
-
const market = this.
|
|
1450
|
-
const oraclePrice = this.
|
|
1456
|
+
const market = this.velocityClient.getSpotMarketAccount(targetMarketIndex);
|
|
1457
|
+
const oraclePrice = this.velocityClient.getOracleDataForSpotMarket(targetMarketIndex).price;
|
|
1451
1458
|
currentQuoteAssetValue = this.getSpotMarketAssetValue(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
1452
1459
|
currentSpotMarketNetValue =
|
|
1453
1460
|
currentSpotMarketNetValue !== null && currentSpotMarketNetValue !== void 0 ? currentSpotMarketNetValue : this.getSpotPositionValue(targetMarketIndex);
|
|
@@ -1489,8 +1496,8 @@ class User {
|
|
|
1489
1496
|
* @param iterationLimit how long to run appromixation before erroring out
|
|
1490
1497
|
*/
|
|
1491
1498
|
getMaxSwapAmount({ inMarketIndex, outMarketIndex, calculateSwap, iterationLimit = 1000, }) {
|
|
1492
|
-
const inMarket = this.
|
|
1493
|
-
const outMarket = this.
|
|
1499
|
+
const inMarket = this.velocityClient.getSpotMarketAccount(inMarketIndex);
|
|
1500
|
+
const outMarket = this.velocityClient.getSpotMarketAccount(outMarketIndex);
|
|
1494
1501
|
const inOraclePriceData = this.getOracleDataForSpotMarket(inMarketIndex);
|
|
1495
1502
|
const inOraclePrice = inOraclePriceData.price;
|
|
1496
1503
|
const outOraclePriceData = this.getOracleDataForSpotMarket(outMarketIndex);
|
|
@@ -1618,14 +1625,14 @@ class User {
|
|
|
1618
1625
|
}
|
|
1619
1626
|
calculateSpotPositionFreeCollateralContribution(spotPosition, strictOraclePrice) {
|
|
1620
1627
|
const marginCategory = 'Initial';
|
|
1621
|
-
const spotMarketAccount = this.
|
|
1628
|
+
const spotMarketAccount = this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
|
|
1622
1629
|
const { freeCollateralContribution } = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, strictOraclePrice, marginCategory, this.getUserAccount().maxMarginRatio);
|
|
1623
1630
|
return freeCollateralContribution;
|
|
1624
1631
|
}
|
|
1625
1632
|
calculateSpotPositionLeverageContribution(spotPosition, strictOraclePrice) {
|
|
1626
1633
|
let totalAssetValue = numericConstants_1.ZERO;
|
|
1627
1634
|
let totalLiabilityValue = numericConstants_1.ZERO;
|
|
1628
|
-
const spotMarketAccount = this.
|
|
1635
|
+
const spotMarketAccount = this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
|
|
1629
1636
|
const { tokenValue, ordersValue } = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, strictOraclePrice, 'Initial', this.getUserAccount().maxMarginRatio);
|
|
1630
1637
|
if (tokenValue.gte(numericConstants_1.ZERO)) {
|
|
1631
1638
|
totalAssetValue = tokenValue;
|
|
@@ -1652,8 +1659,8 @@ class User {
|
|
|
1652
1659
|
* @param outAmount
|
|
1653
1660
|
*/
|
|
1654
1661
|
accountLeverageAfterSwap({ inMarketIndex, outMarketIndex, inAmount, outAmount, }) {
|
|
1655
|
-
const inMarket = this.
|
|
1656
|
-
const outMarket = this.
|
|
1662
|
+
const inMarket = this.velocityClient.getSpotMarketAccount(inMarketIndex);
|
|
1663
|
+
const outMarket = this.velocityClient.getSpotMarketAccount(outMarketIndex);
|
|
1657
1664
|
const inOraclePriceData = this.getOracleDataForSpotMarket(inMarketIndex);
|
|
1658
1665
|
const inOraclePrice = inOraclePriceData.price;
|
|
1659
1666
|
const outOraclePriceData = this.getOracleDataForSpotMarket(outMarketIndex);
|
|
@@ -1740,7 +1747,7 @@ class User {
|
|
|
1740
1747
|
return newLeverage;
|
|
1741
1748
|
}
|
|
1742
1749
|
const currentPosition = this.getPerpPositionOrEmpty(targetMarketIndex);
|
|
1743
|
-
const perpMarket = this.
|
|
1750
|
+
const perpMarket = this.velocityClient.getPerpMarketAccount(targetMarketIndex);
|
|
1744
1751
|
const oracleData = this.getOracleDataForPerpMarket(targetMarketIndex);
|
|
1745
1752
|
let {
|
|
1746
1753
|
// eslint-disable-next-line prefer-const
|
|
@@ -1774,10 +1781,10 @@ class User {
|
|
|
1774
1781
|
return newLeverage;
|
|
1775
1782
|
}
|
|
1776
1783
|
getUserFeeTier(marketType, now) {
|
|
1777
|
-
const state = this.
|
|
1784
|
+
const state = this.velocityClient.getStateAccount();
|
|
1778
1785
|
const feeTierIndex = 0;
|
|
1779
1786
|
if ((0, types_1.isVariant)(marketType, 'perp')) {
|
|
1780
|
-
const userStatsAccount = this.
|
|
1787
|
+
const userStatsAccount = this.velocityClient
|
|
1781
1788
|
.getUserStats()
|
|
1782
1789
|
.getAccount();
|
|
1783
1790
|
const total30dVolume = (0, trade_1.getUser30dRollingVolumeEstimate)(userStatsAccount, now);
|
|
@@ -1829,7 +1836,7 @@ class User {
|
|
|
1829
1836
|
*/
|
|
1830
1837
|
calculateFeeForQuoteAmount(quoteAmount, marketIndex) {
|
|
1831
1838
|
if (marketIndex !== undefined) {
|
|
1832
|
-
const takerFeeMultiplier = this.
|
|
1839
|
+
const takerFeeMultiplier = this.velocityClient.getMarketFees(types_2.MarketType.PERP, marketIndex, this).takerFee;
|
|
1833
1840
|
const feeAmountNum = bigNum_1.BigNum.from(quoteAmount, numericConstants_1.QUOTE_PRECISION_EXP).toNum() *
|
|
1834
1841
|
takerFeeMultiplier;
|
|
1835
1842
|
return bigNum_1.BigNum.fromPrint(feeAmountNum.toString(), numericConstants_1.QUOTE_PRECISION_EXP).val;
|
|
@@ -1849,7 +1856,7 @@ class User {
|
|
|
1849
1856
|
*/
|
|
1850
1857
|
getWithdrawalLimit(marketIndex, reduceOnly) {
|
|
1851
1858
|
const nowTs = new anchor_1.BN(Math.floor(Date.now() / 1000));
|
|
1852
|
-
const spotMarket = this.
|
|
1859
|
+
const spotMarket = this.velocityClient.getSpotMarketAccount(marketIndex);
|
|
1853
1860
|
// eslint-disable-next-line prefer-const
|
|
1854
1861
|
let { borrowLimit, withdrawLimit } = (0, spotBalance_2.calculateWithdrawLimit)(spotMarket, nowTs);
|
|
1855
1862
|
const freeCollateral = this.getFreeCollateral();
|
|
@@ -1902,7 +1909,7 @@ class User {
|
|
|
1902
1909
|
}
|
|
1903
1910
|
}
|
|
1904
1911
|
canBypassWithdrawLimits(marketIndex) {
|
|
1905
|
-
const spotMarket = this.
|
|
1912
|
+
const spotMarket = this.velocityClient.getSpotMarketAccount(marketIndex);
|
|
1906
1913
|
const maxDepositAmount = spotMarket.withdrawGuardThreshold.div(new anchor_1.BN(10));
|
|
1907
1914
|
const position = this.getSpotPosition(marketIndex);
|
|
1908
1915
|
const netDeposits = this.getUserAccount().totalDeposits.sub(this.getUserAccount().totalWithdraws);
|
|
@@ -1983,9 +1990,9 @@ class User {
|
|
|
1983
1990
|
}
|
|
1984
1991
|
canBeDeleted(userStatsAccount, now) {
|
|
1985
1992
|
const userAccount = this.getUserAccount();
|
|
1986
|
-
const userStatsAccountToUse = userStatsAccount || this.
|
|
1993
|
+
const userStatsAccountToUse = userStatsAccount || this.velocityClient.getUserStats().getAccount();
|
|
1987
1994
|
const nowInSeconds = now || new anchor_1.BN(Math.floor(Date.now() / 1000));
|
|
1988
|
-
const stateAccount = this.
|
|
1995
|
+
const stateAccount = this.velocityClient.getStateAccount();
|
|
1989
1996
|
// Referrer cannot delete sub_account_id 0
|
|
1990
1997
|
const isReferrer = (userStatsAccountToUse.referrerStatus & types_1.ReferrerStatus.IsReferrer) > 0;
|
|
1991
1998
|
if (isReferrer && userAccount.subAccountId === 0) {
|
|
@@ -2034,13 +2041,13 @@ class User {
|
|
|
2034
2041
|
let safestPerpTier = 4;
|
|
2035
2042
|
let safestSpotTier = 4;
|
|
2036
2043
|
for (const perpPosition of this.getActivePerpPositions()) {
|
|
2037
|
-
safestPerpTier = Math.min(safestPerpTier, (0, tiers_1.getPerpMarketTierNumber)(this.
|
|
2044
|
+
safestPerpTier = Math.min(safestPerpTier, (0, tiers_1.getPerpMarketTierNumber)(this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex)));
|
|
2038
2045
|
}
|
|
2039
2046
|
for (const spotPosition of this.getActiveSpotPositions()) {
|
|
2040
2047
|
if ((0, types_1.isVariant)(spotPosition.balanceType, 'deposit')) {
|
|
2041
2048
|
continue;
|
|
2042
2049
|
}
|
|
2043
|
-
safestSpotTier = Math.min(safestSpotTier, (0, tiers_1.getSpotMarketTierNumber)(this.
|
|
2050
|
+
safestSpotTier = Math.min(safestSpotTier, (0, tiers_1.getSpotMarketTierNumber)(this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex)));
|
|
2044
2051
|
}
|
|
2045
2052
|
return {
|
|
2046
2053
|
perpTier: safestPerpTier,
|
|
@@ -2048,9 +2055,9 @@ class User {
|
|
|
2048
2055
|
};
|
|
2049
2056
|
}
|
|
2050
2057
|
getPerpPositionHealth({ marginCategory, perpPosition, oraclePriceData, quoteOraclePriceData, includeOpenOrders = true, }) {
|
|
2051
|
-
const perpMarket = this.
|
|
2058
|
+
const perpMarket = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
2052
2059
|
const _oraclePriceData = oraclePriceData ||
|
|
2053
|
-
this.
|
|
2060
|
+
this.velocityClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
|
|
2054
2061
|
const oraclePrice = _oraclePriceData.price;
|
|
2055
2062
|
let worstCaseBaseAmount;
|
|
2056
2063
|
let worstCaseLiabilityValue;
|
|
@@ -2066,7 +2073,7 @@ class User {
|
|
|
2066
2073
|
const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
2067
2074
|
const marginRatio = new anchor_1.BN((0, market_1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, userCustomMargin));
|
|
2068
2075
|
const _quoteOraclePriceData = quoteOraclePriceData ||
|
|
2069
|
-
this.
|
|
2076
|
+
this.velocityClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
2070
2077
|
let marginRequirement = worstCaseLiabilityValue
|
|
2071
2078
|
.mul(_quoteOraclePriceData.price)
|
|
2072
2079
|
.div(numericConstants_1.PRICE_PRECISION)
|
|
@@ -2089,16 +2096,16 @@ class User {
|
|
|
2089
2096
|
perpPnl: [],
|
|
2090
2097
|
};
|
|
2091
2098
|
for (const perpPosition of this.getActivePerpPositions()) {
|
|
2092
|
-
const perpMarket = this.
|
|
2093
|
-
const oraclePriceData = this.
|
|
2094
|
-
const quoteOraclePriceData = this.
|
|
2099
|
+
const perpMarket = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
2100
|
+
const oraclePriceData = this.velocityClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
|
|
2101
|
+
const quoteOraclePriceData = this.velocityClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
2095
2102
|
healthComponents.perpPositions.push(this.getPerpPositionHealth({
|
|
2096
2103
|
marginCategory,
|
|
2097
2104
|
perpPosition,
|
|
2098
2105
|
oraclePriceData,
|
|
2099
2106
|
quoteOraclePriceData,
|
|
2100
2107
|
}));
|
|
2101
|
-
const quoteSpotMarket = this.
|
|
2108
|
+
const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
|
|
2102
2109
|
const positionUnrealizedPnl = (0, position_2.calculatePositionPNL)(perpMarket, perpPosition, true, oraclePriceData);
|
|
2103
2110
|
let pnlWeight;
|
|
2104
2111
|
if (positionUnrealizedPnl.gt(numericConstants_1.ZERO)) {
|
|
@@ -2123,7 +2130,7 @@ class User {
|
|
|
2123
2130
|
}
|
|
2124
2131
|
let netQuoteValue = numericConstants_1.ZERO;
|
|
2125
2132
|
for (const spotPosition of this.getActiveSpotPositions()) {
|
|
2126
|
-
const spotMarketAccount = this.
|
|
2133
|
+
const spotMarketAccount = this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
|
|
2127
2134
|
const oraclePriceData = this.getOracleDataForSpotMarket(spotPosition.marketIndex);
|
|
2128
2135
|
const strictOraclePrice = new strictOraclePrice_1.StrictOraclePrice(oraclePriceData.price);
|
|
2129
2136
|
if (spotPosition.marketIndex === numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
|
|
@@ -2155,7 +2162,7 @@ class User {
|
|
|
2155
2162
|
}
|
|
2156
2163
|
}
|
|
2157
2164
|
if (!netQuoteValue.eq(numericConstants_1.ZERO)) {
|
|
2158
|
-
const spotMarketAccount = this.
|
|
2165
|
+
const spotMarketAccount = this.velocityClient.getQuoteSpotMarketAccount();
|
|
2159
2166
|
const oraclePriceData = this.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
2160
2167
|
const baseAssetValue = (0, spotBalance_1.getTokenValue)(netQuoteValue, spotMarketAccount.decimals, oraclePriceData);
|
|
2161
2168
|
const { weight, weightedTokenValue } = (0, spotPosition_1.calculateWeightedTokenValue)(netQuoteValue, baseAssetValue, oraclePriceData.price, spotMarketAccount, marginCategory, this.getUserAccount().maxMarginRatio);
|
|
@@ -2195,13 +2202,13 @@ class User {
|
|
|
2195
2202
|
return this.getTotalPerpPositionLiability(marginCategory, liquidationBuffer, includeOpenOrders).sub(currentPerpPositionValueUSDC);
|
|
2196
2203
|
}
|
|
2197
2204
|
getMMOracleDataForPerpMarket(marketIndex) {
|
|
2198
|
-
return this.
|
|
2205
|
+
return this.velocityClient.getMMOracleDataForPerpMarket(marketIndex);
|
|
2199
2206
|
}
|
|
2200
2207
|
getOracleDataForPerpMarket(marketIndex) {
|
|
2201
|
-
return this.
|
|
2208
|
+
return this.velocityClient.getOracleDataForPerpMarket(marketIndex);
|
|
2202
2209
|
}
|
|
2203
2210
|
getOracleDataForSpotMarket(marketIndex) {
|
|
2204
|
-
return this.
|
|
2211
|
+
return this.velocityClient.getOracleDataForSpotMarket(marketIndex);
|
|
2205
2212
|
}
|
|
2206
2213
|
/**
|
|
2207
2214
|
* Get the active perp and spot positions of the user.
|
|
@@ -2243,7 +2250,7 @@ class User {
|
|
|
2243
2250
|
if ((0, spotPosition_1.isSpotPositionAvailable)(spotPosition))
|
|
2244
2251
|
continue;
|
|
2245
2252
|
const isQuote = spotPosition.marketIndex === numericConstants_1.QUOTE_SPOT_MARKET_INDEX;
|
|
2246
|
-
const spotMarket = this.
|
|
2253
|
+
const spotMarket = this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
|
|
2247
2254
|
const oraclePriceData = this.getOracleDataForSpotMarket(spotPosition.marketIndex);
|
|
2248
2255
|
const twap5 = strict
|
|
2249
2256
|
? (0, oracles_1.calculateLiveOracleTwap)(spotMarket.historicalOracleData, oraclePriceData, new anchor_1.BN(Math.floor(Date.now() / 1000)), numericConstants_1.FIVE_MINUTE)
|
|
@@ -2292,8 +2299,8 @@ class User {
|
|
|
2292
2299
|
}
|
|
2293
2300
|
// PERP POSITIONS
|
|
2294
2301
|
for (const marketPosition of this.getActivePerpPositions()) {
|
|
2295
|
-
const market = this.
|
|
2296
|
-
const quoteSpotMarket = this.
|
|
2302
|
+
const market = this.velocityClient.getPerpMarketAccount(marketPosition.marketIndex);
|
|
2303
|
+
const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
|
|
2297
2304
|
const quoteOraclePriceData = this.getOracleDataForSpotMarket(market.quoteSpotMarketIndex);
|
|
2298
2305
|
const oraclePriceData = this.getMMOracleDataForPerpMarket(market.marketIndex);
|
|
2299
2306
|
const nonMmmOraclePriceData = this.getOracleDataForPerpMarket(market.marketIndex);
|
|
@@ -2346,7 +2353,7 @@ class User {
|
|
|
2346
2353
|
// derive isolated quote deposit value, mirroring on-chain logic
|
|
2347
2354
|
let depositValue = numericConstants_1.ZERO;
|
|
2348
2355
|
if ((_h = marketPosition.isolatedPositionScaledBalance) === null || _h === void 0 ? void 0 : _h.gt(numericConstants_1.ZERO)) {
|
|
2349
|
-
const quoteSpotMarket = this.
|
|
2356
|
+
const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
|
|
2350
2357
|
const quoteOraclePriceData = this.getOracleDataForSpotMarket(market.quoteSpotMarketIndex);
|
|
2351
2358
|
const strictQuote = new strictOraclePrice_1.StrictOraclePrice(quoteOraclePriceData.price, strict
|
|
2352
2359
|
? quoteSpotMarket.historicalOracleData.lastOraclePriceTwap5Min
|
|
@@ -5,11 +5,12 @@ import { GrpcConfigs, UserAccountSubscriber } from './accounts/types';
|
|
|
5
5
|
import { WebSocketProgramAccountSubscriber } from './accounts/webSocketProgramAccountSubscriber';
|
|
6
6
|
import { UserAccount } from './types';
|
|
7
7
|
import { grpcMultiUserAccountSubscriber } from './accounts/grpcMultiUserAccountSubscriber';
|
|
8
|
-
|
|
8
|
+
import { AtLeastOne } from './util/deprecatedAlias';
|
|
9
|
+
type UserConfigBase = {
|
|
9
10
|
accountSubscription?: UserSubscriptionConfig;
|
|
10
|
-
driftClient: VelocityClient;
|
|
11
11
|
userAccountPublicKey: PublicKey;
|
|
12
12
|
};
|
|
13
|
+
export type UserConfig = UserConfigBase & AtLeastOne<'velocityClient', 'driftClient', VelocityClient>;
|
|
13
14
|
export type UserSubscriptionConfig = {
|
|
14
15
|
type: 'grpc';
|
|
15
16
|
resubTimeoutMs?: number;
|
|
@@ -29,3 +30,4 @@ export type UserSubscriptionConfig = {
|
|
|
29
30
|
type: 'custom';
|
|
30
31
|
userAccountSubscriber: UserAccountSubscriber;
|
|
31
32
|
};
|
|
33
|
+
export {};
|
|
@@ -11,16 +11,18 @@ export declare class ReferrerMap {
|
|
|
11
11
|
* Will be undefined if the referrer is not in the map yet.
|
|
12
12
|
*/
|
|
13
13
|
private referrerReferrerInfoMap;
|
|
14
|
-
private
|
|
14
|
+
private velocityClient;
|
|
15
|
+
/** @deprecated Use `velocityClient` instead. `driftClient` will be removed in a future major. */
|
|
16
|
+
private get driftClient();
|
|
15
17
|
private parallelSync;
|
|
16
18
|
private fetchPromise?;
|
|
17
19
|
private fetchPromiseResolver;
|
|
18
20
|
/**
|
|
19
21
|
* Creates a new UserStatsMap instance.
|
|
20
22
|
*
|
|
21
|
-
* @param {VelocityClient}
|
|
23
|
+
* @param {VelocityClient} velocityClient - The VelocityClient instance.
|
|
22
24
|
*/
|
|
23
|
-
constructor(
|
|
25
|
+
constructor(velocityClient: VelocityClient, parallelSync?: boolean);
|
|
24
26
|
/**
|
|
25
27
|
* Subscribe to all UserStats accounts.
|
|
26
28
|
*/
|