@drift-labs/sdk 2.20.0-beta.0 → 2.20.0-beta.2
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/lib/accounts/bulkAccountLoader.d.ts +2 -1
- package/lib/accounts/bulkAccountLoader.js +7 -7
- package/lib/accounts/fetch.d.ts +1 -0
- package/lib/accounts/fetch.js +8 -4
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +7 -7
- package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +2 -2
- package/lib/accounts/types.d.ts +5 -4
- package/lib/accounts/webSocketAccountSubscriber.js +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +3 -3
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +13 -2
- package/lib/adminClient.js +61 -57
- package/lib/config.d.ts +2 -2
- package/lib/constants/perpMarkets.d.ts +1 -1
- package/lib/constants/spotMarkets.d.ts +1 -1
- package/lib/dlob/DLOB.d.ts +6 -5
- package/lib/dlob/DLOB.js +105 -75
- package/lib/dlob/DLOBNode.d.ts +2 -2
- package/lib/dlob/DLOBNode.js +7 -7
- package/lib/dlob/DLOBOrders.d.ts +2 -2
- package/lib/dlob/NodeList.d.ts +1 -1
- package/lib/dlob/NodeList.js +2 -2
- package/lib/driftClient.d.ts +5 -2
- package/lib/driftClient.js +137 -95
- package/lib/driftClientConfig.d.ts +3 -3
- package/lib/events/eventSubscriber.js +2 -2
- package/lib/events/fetchLogs.d.ts +2 -2
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/events/types.d.ts +14 -14
- package/lib/examples/loadDlob.js +2 -2
- package/lib/examples/makeTradeExample.js +9 -9
- package/lib/factory/bigNum.js +13 -13
- package/lib/factory/oracleClient.js +4 -4
- package/lib/idl/drift.json +82 -4
- package/lib/index.js +5 -1
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.js +23 -23
- package/lib/math/auction.js +6 -6
- package/lib/math/exchangeStatus.js +2 -2
- package/lib/math/funding.js +2 -2
- package/lib/math/margin.js +4 -4
- package/lib/math/market.js +15 -15
- package/lib/math/oracles.js +1 -1
- package/lib/math/orders.js +27 -24
- package/lib/math/position.js +5 -5
- package/lib/math/repeg.js +1 -1
- package/lib/math/spotBalance.js +9 -9
- package/lib/math/spotMarket.js +3 -3
- package/lib/math/spotPosition.js +3 -3
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +42 -42
- package/lib/math/utils.js +1 -1
- package/lib/oracles/oracleClientCache.js +1 -1
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/types.d.ts +2 -2
- package/lib/serum/types.d.ts +1 -1
- package/lib/slot/SlotSubscriber.d.ts +1 -1
- package/lib/tokenFaucet.js +5 -1
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +1 -1
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +71 -44
- package/lib/user.js +63 -63
- package/lib/userConfig.d.ts +2 -2
- package/lib/userMap/userMap.js +1 -1
- package/lib/userMap/userStatsMap.js +3 -3
- package/lib/userStats.js +2 -2
- package/lib/userStatsConfig.d.ts +2 -2
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +5 -5
- package/src/accounts/fetch.ts +8 -0
- package/src/addresses/pda.ts +13 -0
- package/src/dlob/DLOB.ts +55 -8
- package/src/driftClient.ts +77 -0
- package/src/idl/drift.json +82 -4
- package/src/math/orders.ts +5 -1
- package/src/types.ts +29 -0
- package/src/userMap/userStatsMap.ts +1 -4
- package/tests/amm/test.ts +24 -28
- package/tests/dlob/test.ts +67 -73
package/lib/user.js
CHANGED
|
@@ -13,6 +13,12 @@ const webSocketUserAccountSubscriber_1 = require("./accounts/webSocketUserAccoun
|
|
|
13
13
|
const spotPosition_1 = require("./math/spotPosition");
|
|
14
14
|
const oracles_1 = require("./math/oracles");
|
|
15
15
|
class User {
|
|
16
|
+
get isSubscribed() {
|
|
17
|
+
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
18
|
+
}
|
|
19
|
+
set isSubscribed(val) {
|
|
20
|
+
this._isSubscribed = val;
|
|
21
|
+
}
|
|
16
22
|
constructor(config) {
|
|
17
23
|
var _a;
|
|
18
24
|
this._isSubscribed = false;
|
|
@@ -26,12 +32,6 @@ class User {
|
|
|
26
32
|
}
|
|
27
33
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
28
34
|
}
|
|
29
|
-
get isSubscribed() {
|
|
30
|
-
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
31
|
-
}
|
|
32
|
-
set isSubscribed(val) {
|
|
33
|
-
this._isSubscribed = val;
|
|
34
|
-
}
|
|
35
35
|
/**
|
|
36
36
|
* Subscribe to User state accounts
|
|
37
37
|
* @returns SusbcriptionSuccess result
|
|
@@ -142,7 +142,7 @@ class User {
|
|
|
142
142
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
143
143
|
}
|
|
144
144
|
const market = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
145
|
-
const [marketOpenBids, marketOpenAsks] = amm_1.calculateMarketOpenBidAsk(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
|
|
145
|
+
const [marketOpenBids, marketOpenAsks] = (0, amm_1.calculateMarketOpenBidAsk)(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
|
|
146
146
|
const lpOpenBids = marketOpenBids
|
|
147
147
|
.mul(position.lpShares)
|
|
148
148
|
.div(market.amm.sqrtK);
|
|
@@ -245,13 +245,13 @@ class User {
|
|
|
245
245
|
getPerpBuyingPower(marketIndex) {
|
|
246
246
|
const perpPosition = this.getPerpPosition(marketIndex);
|
|
247
247
|
const worstCaseBaseAssetAmount = perpPosition
|
|
248
|
-
? margin_1.calculateWorstCaseBaseAssetAmount(perpPosition)
|
|
248
|
+
? (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition)
|
|
249
249
|
: numericConstants_1.ZERO;
|
|
250
250
|
const freeCollateral = this.getFreeCollateral();
|
|
251
251
|
return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, worstCaseBaseAssetAmount);
|
|
252
252
|
}
|
|
253
253
|
getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, baseAssetAmount) {
|
|
254
|
-
const marginRatio = _1.calculateMarketMarginRatio(this.driftClient.getPerpMarketAccount(marketIndex), baseAssetAmount, 'Initial');
|
|
254
|
+
const marginRatio = (0, _1.calculateMarketMarginRatio)(this.driftClient.getPerpMarketAccount(marketIndex), baseAssetAmount, 'Initial');
|
|
255
255
|
return freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new _1.BN(marginRatio));
|
|
256
256
|
}
|
|
257
257
|
/**
|
|
@@ -302,11 +302,11 @@ class User {
|
|
|
302
302
|
if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
|
|
303
303
|
perpPosition = this.getSettledLPPosition(perpPosition.marketIndex)[0];
|
|
304
304
|
}
|
|
305
|
-
let positionUnrealizedPnl = _1.calculatePositionPNL(market, perpPosition, withFunding, oraclePriceData);
|
|
305
|
+
let positionUnrealizedPnl = (0, _1.calculatePositionPNL)(market, perpPosition, withFunding, oraclePriceData);
|
|
306
306
|
if (withWeightMarginCategory !== undefined) {
|
|
307
307
|
if (positionUnrealizedPnl.gt(numericConstants_1.ZERO)) {
|
|
308
308
|
positionUnrealizedPnl = positionUnrealizedPnl
|
|
309
|
-
.mul(_1.calculateUnrealizedAssetWeight(market, quoteSpotMarket, positionUnrealizedPnl, withWeightMarginCategory, oraclePriceData))
|
|
309
|
+
.mul((0, _1.calculateUnrealizedAssetWeight)(market, quoteSpotMarket, positionUnrealizedPnl, withWeightMarginCategory, oraclePriceData))
|
|
310
310
|
.div(new _1.BN(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION));
|
|
311
311
|
}
|
|
312
312
|
}
|
|
@@ -322,7 +322,7 @@ class User {
|
|
|
322
322
|
.perpPositions.filter((pos) => marketIndex ? pos.marketIndex === marketIndex : true)
|
|
323
323
|
.reduce((pnl, perpPosition) => {
|
|
324
324
|
const market = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
325
|
-
return pnl.add(_1.calculatePositionFundingPNL(market, perpPosition));
|
|
325
|
+
return pnl.add((0, _1.calculatePositionFundingPNL)(market, perpPosition));
|
|
326
326
|
}, numericConstants_1.ZERO);
|
|
327
327
|
}
|
|
328
328
|
getSpotMarketAssetAndLiabilityValue(marketIndex, marginCategory, liquidationBuffer, includeOpenOrders, strict = false, now) {
|
|
@@ -335,15 +335,15 @@ class User {
|
|
|
335
335
|
const countForQuote = marketIndex === undefined ||
|
|
336
336
|
marketIndex === numericConstants_1.QUOTE_SPOT_MARKET_INDEX ||
|
|
337
337
|
(includeOpenOrders && spotPosition.openOrders !== 0);
|
|
338
|
-
if (spotPosition_1.isSpotPositionAvailable(spotPosition) ||
|
|
338
|
+
if ((0, spotPosition_1.isSpotPositionAvailable)(spotPosition) ||
|
|
339
339
|
(!countForBase && !countForQuote)) {
|
|
340
340
|
continue;
|
|
341
341
|
}
|
|
342
342
|
const spotMarketAccount = this.driftClient.getSpotMarketAccount(spotPosition.marketIndex);
|
|
343
343
|
if (spotPosition.marketIndex === numericConstants_1.QUOTE_SPOT_MARKET_INDEX &&
|
|
344
344
|
countForQuote) {
|
|
345
|
-
if (types_1.isVariant(spotPosition.balanceType, 'borrow')) {
|
|
346
|
-
const tokenAmount = spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
|
|
345
|
+
if ((0, types_1.isVariant)(spotPosition.balanceType, 'borrow')) {
|
|
346
|
+
const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
|
|
347
347
|
let weight = numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION;
|
|
348
348
|
if (marginCategory === 'Initial') {
|
|
349
349
|
weight = _1.BN.max(weight, new _1.BN(this.getUserAccount().maxMarginRatio));
|
|
@@ -355,27 +355,27 @@ class User {
|
|
|
355
355
|
continue;
|
|
356
356
|
}
|
|
357
357
|
else {
|
|
358
|
-
const tokenAmount = spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
|
|
358
|
+
const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
|
|
359
359
|
netQuoteValue = netQuoteValue.add(tokenAmount);
|
|
360
360
|
continue;
|
|
361
361
|
}
|
|
362
362
|
}
|
|
363
363
|
const oraclePriceData = this.getOracleDataForSpotMarket(spotPosition.marketIndex);
|
|
364
364
|
if (!includeOpenOrders && countForBase) {
|
|
365
|
-
if (types_1.isVariant(spotPosition.balanceType, 'borrow')) {
|
|
366
|
-
const tokenAmount = spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
|
|
365
|
+
if ((0, types_1.isVariant)(spotPosition.balanceType, 'borrow')) {
|
|
366
|
+
const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
|
|
367
367
|
const liabilityValue = this.getSpotLiabilityValue(tokenAmount, oraclePriceData, spotMarketAccount, marginCategory, liquidationBuffer, strict, now);
|
|
368
368
|
totalLiabilityValue = totalLiabilityValue.add(liabilityValue);
|
|
369
369
|
continue;
|
|
370
370
|
}
|
|
371
371
|
else {
|
|
372
|
-
const tokenAmount = spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
|
|
372
|
+
const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType);
|
|
373
373
|
const assetValue = this.getSpotAssetValue(tokenAmount, oraclePriceData, spotMarketAccount, marginCategory, strict, now);
|
|
374
374
|
totalAssetValue = totalAssetValue.add(assetValue);
|
|
375
375
|
continue;
|
|
376
376
|
}
|
|
377
377
|
}
|
|
378
|
-
const [worstCaseTokenAmount, worstCaseQuoteTokenAmount] = spotPosition_1.getWorstCaseTokenAmounts(spotPosition, spotMarketAccount, this.getOracleDataForSpotMarket(spotPosition.marketIndex));
|
|
378
|
+
const [worstCaseTokenAmount, worstCaseQuoteTokenAmount] = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, this.getOracleDataForSpotMarket(spotPosition.marketIndex));
|
|
379
379
|
if (worstCaseTokenAmount.gt(numericConstants_1.ZERO) && countForBase) {
|
|
380
380
|
const baseAssetValue = this.getSpotAssetValue(worstCaseTokenAmount, oraclePriceData, spotMarketAccount, marginCategory, strict, now);
|
|
381
381
|
totalAssetValue = totalAssetValue.add(baseAssetValue);
|
|
@@ -417,15 +417,15 @@ class User {
|
|
|
417
417
|
getSpotLiabilityValue(tokenAmount, oraclePriceData, spotMarketAccount, marginCategory, liquidationBuffer, strict = false, now) {
|
|
418
418
|
let liabilityValue = null;
|
|
419
419
|
if (strict && spotMarketAccount.marketIndex != numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
|
|
420
|
-
const estOracleTwap = oracles_1.calculateLiveOracleTwap(spotMarketAccount.historicalOracleData, oraclePriceData, now, numericConstants_1.FIVE_MINUTE // 5MIN
|
|
420
|
+
const estOracleTwap = (0, oracles_1.calculateLiveOracleTwap)(spotMarketAccount.historicalOracleData, oraclePriceData, now, numericConstants_1.FIVE_MINUTE // 5MIN
|
|
421
421
|
);
|
|
422
|
-
liabilityValue = _1.getStrictTokenValue(tokenAmount, spotMarketAccount.decimals, oraclePriceData, estOracleTwap);
|
|
422
|
+
liabilityValue = (0, _1.getStrictTokenValue)(tokenAmount, spotMarketAccount.decimals, oraclePriceData, estOracleTwap);
|
|
423
423
|
}
|
|
424
424
|
else {
|
|
425
|
-
liabilityValue = _1.getTokenValue(tokenAmount, spotMarketAccount.decimals, oraclePriceData);
|
|
425
|
+
liabilityValue = (0, _1.getTokenValue)(tokenAmount, spotMarketAccount.decimals, oraclePriceData);
|
|
426
426
|
}
|
|
427
427
|
if (marginCategory !== undefined) {
|
|
428
|
-
let weight = spotBalance_1.calculateLiabilityWeight(tokenAmount, spotMarketAccount, marginCategory);
|
|
428
|
+
let weight = (0, spotBalance_1.calculateLiabilityWeight)(tokenAmount, spotMarketAccount, marginCategory);
|
|
429
429
|
if (marginCategory === 'Initial') {
|
|
430
430
|
weight = _1.BN.max(weight, new _1.BN(this.getUserAccount().maxMarginRatio));
|
|
431
431
|
}
|
|
@@ -445,22 +445,22 @@ class User {
|
|
|
445
445
|
getSpotAssetValue(tokenAmount, oraclePriceData, spotMarketAccount, marginCategory, strict = false, now) {
|
|
446
446
|
let assetValue = null;
|
|
447
447
|
if (strict && spotMarketAccount.marketIndex != numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
|
|
448
|
-
const estOracleTwap = oracles_1.calculateLiveOracleTwap(spotMarketAccount.historicalOracleData, oraclePriceData, now, numericConstants_1.FIVE_MINUTE // 5MIN
|
|
448
|
+
const estOracleTwap = (0, oracles_1.calculateLiveOracleTwap)(spotMarketAccount.historicalOracleData, oraclePriceData, now, numericConstants_1.FIVE_MINUTE // 5MIN
|
|
449
449
|
);
|
|
450
|
-
assetValue = _1.getStrictTokenValue(tokenAmount, spotMarketAccount.decimals, oraclePriceData, estOracleTwap);
|
|
450
|
+
assetValue = (0, _1.getStrictTokenValue)(tokenAmount, spotMarketAccount.decimals, oraclePriceData, estOracleTwap);
|
|
451
451
|
}
|
|
452
452
|
else {
|
|
453
|
-
assetValue = _1.getTokenValue(tokenAmount, spotMarketAccount.decimals, oraclePriceData);
|
|
453
|
+
assetValue = (0, _1.getTokenValue)(tokenAmount, spotMarketAccount.decimals, oraclePriceData);
|
|
454
454
|
}
|
|
455
455
|
if (marginCategory !== undefined) {
|
|
456
|
-
const weight = spotBalance_1.calculateAssetWeight(tokenAmount, spotMarketAccount, marginCategory);
|
|
456
|
+
const weight = (0, spotBalance_1.calculateAssetWeight)(tokenAmount, spotMarketAccount, marginCategory);
|
|
457
457
|
assetValue = assetValue.mul(weight).div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
458
458
|
}
|
|
459
459
|
return assetValue;
|
|
460
460
|
}
|
|
461
461
|
getSpotTokenAmount(marketIndex) {
|
|
462
462
|
const spotPosition = this.getSpotPosition(marketIndex);
|
|
463
|
-
return spotBalance_1.getTokenAmount(spotPosition.scaledBalance, this.driftClient.getSpotMarketAccount(marketIndex), spotPosition.balanceType);
|
|
463
|
+
return (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, this.driftClient.getSpotMarketAccount(marketIndex), spotPosition.balanceType);
|
|
464
464
|
}
|
|
465
465
|
getSpotPositionValue(marketIndex, marginCategory, includeOpenOrders, strict = false, now) {
|
|
466
466
|
const { totalAssetValue, totalLiabilityValue } = this.getSpotMarketAssetAndLiabilityValue(marketIndex, marginCategory, undefined, includeOpenOrders, strict, now);
|
|
@@ -483,8 +483,8 @@ class User {
|
|
|
483
483
|
*/
|
|
484
484
|
getHealth() {
|
|
485
485
|
const userAccount = this.getUserAccount();
|
|
486
|
-
if (types_1.isVariant(userAccount.status, 'beingLiquidated') ||
|
|
487
|
-
types_1.isVariant(userAccount.status, 'bankrupt')) {
|
|
486
|
+
if ((0, types_1.isVariant)(userAccount.status, 'beingLiquidated') ||
|
|
487
|
+
(0, types_1.isVariant)(userAccount.status, 'bankrupt')) {
|
|
488
488
|
return 0;
|
|
489
489
|
}
|
|
490
490
|
const totalCollateral = this.getTotalCollateral('Maintenance');
|
|
@@ -530,25 +530,25 @@ class User {
|
|
|
530
530
|
perpPosition.openBids = totalOpenBids;
|
|
531
531
|
}
|
|
532
532
|
let valuationPrice = this.getOracleDataForPerpMarket(market.marketIndex).price;
|
|
533
|
-
if (types_1.isVariant(market.status, 'settlement')) {
|
|
533
|
+
if ((0, types_1.isVariant)(market.status, 'settlement')) {
|
|
534
534
|
valuationPrice = market.expiryPrice;
|
|
535
535
|
}
|
|
536
536
|
const baseAssetAmount = includeOpenOrders
|
|
537
|
-
? margin_1.calculateWorstCaseBaseAssetAmount(perpPosition)
|
|
537
|
+
? (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition)
|
|
538
538
|
: perpPosition.baseAssetAmount;
|
|
539
539
|
let baseAssetValue = baseAssetAmount
|
|
540
540
|
.abs()
|
|
541
541
|
.mul(valuationPrice)
|
|
542
542
|
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.PRICE_PRECISION));
|
|
543
543
|
if (marginCategory) {
|
|
544
|
-
let marginRatio = new _1.BN(_1.calculateMarketMarginRatio(market, baseAssetAmount.abs(), marginCategory));
|
|
544
|
+
let marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory));
|
|
545
545
|
if (marginCategory === 'Initial') {
|
|
546
546
|
marginRatio = _1.BN.max(marginRatio, new _1.BN(this.getUserAccount().maxMarginRatio));
|
|
547
547
|
}
|
|
548
548
|
if (liquidationBuffer !== undefined) {
|
|
549
549
|
marginRatio = marginRatio.add(liquidationBuffer);
|
|
550
550
|
}
|
|
551
|
-
if (types_1.isVariant(market.status, 'settlement')) {
|
|
551
|
+
if ((0, types_1.isVariant)(market.status, 'settlement')) {
|
|
552
552
|
marginRatio = numericConstants_1.ZERO;
|
|
553
553
|
}
|
|
554
554
|
baseAssetValue = baseAssetValue
|
|
@@ -568,7 +568,7 @@ class User {
|
|
|
568
568
|
getPerpPositionValue(marketIndex, oraclePriceData, includeOpenOrders = false) {
|
|
569
569
|
const userPosition = this.getPerpPosition(marketIndex) || this.getEmptyPosition(marketIndex);
|
|
570
570
|
const market = this.driftClient.getPerpMarketAccount(userPosition.marketIndex);
|
|
571
|
-
return margin_1.calculateBaseAssetValueWithOracle(market, userPosition, oraclePriceData, includeOpenOrders);
|
|
571
|
+
return (0, margin_1.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData, includeOpenOrders);
|
|
572
572
|
}
|
|
573
573
|
getPositionSide(currentPosition) {
|
|
574
574
|
if (currentPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
@@ -587,11 +587,11 @@ class User {
|
|
|
587
587
|
*/
|
|
588
588
|
getPositionEstimatedExitPriceAndPnl(position, amountToClose, useAMMClose = false) {
|
|
589
589
|
const market = this.driftClient.getPerpMarketAccount(position.marketIndex);
|
|
590
|
-
const entryPrice = position_1.calculateEntryPrice(position);
|
|
590
|
+
const entryPrice = (0, position_1.calculateEntryPrice)(position);
|
|
591
591
|
const oraclePriceData = this.getOracleDataForPerpMarket(position.marketIndex);
|
|
592
592
|
if (amountToClose) {
|
|
593
593
|
if (amountToClose.eq(numericConstants_1.ZERO)) {
|
|
594
|
-
return [_1.calculateReservePrice(market, oraclePriceData), numericConstants_1.ZERO];
|
|
594
|
+
return [(0, _1.calculateReservePrice)(market, oraclePriceData), numericConstants_1.ZERO];
|
|
595
595
|
}
|
|
596
596
|
position = {
|
|
597
597
|
baseAssetAmount: amountToClose,
|
|
@@ -602,10 +602,10 @@ class User {
|
|
|
602
602
|
}
|
|
603
603
|
let baseAssetValue;
|
|
604
604
|
if (useAMMClose) {
|
|
605
|
-
baseAssetValue = _1.calculateBaseAssetValue(market, position, oraclePriceData);
|
|
605
|
+
baseAssetValue = (0, _1.calculateBaseAssetValue)(market, position, oraclePriceData);
|
|
606
606
|
}
|
|
607
607
|
else {
|
|
608
|
-
baseAssetValue = margin_1.calculateBaseAssetValueWithOracle(market, position, oraclePriceData);
|
|
608
|
+
baseAssetValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, position, oraclePriceData);
|
|
609
609
|
}
|
|
610
610
|
if (position.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
611
611
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
@@ -667,7 +667,7 @@ class User {
|
|
|
667
667
|
return numericConstants_1.ZERO;
|
|
668
668
|
}
|
|
669
669
|
const totalLiabilityValue = perpLiabilityValue.add(spotLiabilityValue);
|
|
670
|
-
const marginRatio = _1.calculateMarketMarginRatio(market, numericConstants_1.ZERO, // todo
|
|
670
|
+
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, numericConstants_1.ZERO, // todo
|
|
671
671
|
category);
|
|
672
672
|
const freeCollateral = this.getFreeCollateral();
|
|
673
673
|
// how much more liabilities can be opened w remaining free collateral
|
|
@@ -702,14 +702,14 @@ class User {
|
|
|
702
702
|
const tradeQuoteAmount = this.getMaxTradeSizeUSDCForSpot(spotMarketIndex, direction, currentQuoteAssetValue, currentSpotMarketNetValue);
|
|
703
703
|
let assetValueToAdd = numericConstants_1.ZERO;
|
|
704
704
|
let liabilityValueToAdd = numericConstants_1.ZERO;
|
|
705
|
-
const newQuoteNetValue = types_1.isVariant(direction, 'short')
|
|
705
|
+
const newQuoteNetValue = (0, types_1.isVariant)(direction, 'short')
|
|
706
706
|
? currentQuoteValue.add(tradeQuoteAmount)
|
|
707
707
|
: currentQuoteValue.sub(tradeQuoteAmount);
|
|
708
708
|
const newQuoteAssetValue = _1.BN.max(newQuoteNetValue, numericConstants_1.ZERO);
|
|
709
709
|
const newQuoteLiabilityValue = _1.BN.min(newQuoteNetValue, numericConstants_1.ZERO).abs();
|
|
710
710
|
assetValueToAdd = assetValueToAdd.add(newQuoteAssetValue.sub(currentQuoteAssetValue));
|
|
711
711
|
liabilityValueToAdd = liabilityValueToAdd.add(newQuoteLiabilityValue.sub(currentQuoteLiabilityValue));
|
|
712
|
-
const newSpotMarketNetValue = types_1.isVariant(direction, 'long')
|
|
712
|
+
const newSpotMarketNetValue = (0, types_1.isVariant)(direction, 'long')
|
|
713
713
|
? currentSpotMarketNetValue.add(tradeQuoteAmount)
|
|
714
714
|
: currentSpotMarketNetValue.sub(tradeQuoteAmount);
|
|
715
715
|
const newSpotMarketAssetValue = _1.BN.max(newSpotMarketNetValue, numericConstants_1.ZERO);
|
|
@@ -740,7 +740,7 @@ class User {
|
|
|
740
740
|
const totalCollateral = this.getTotalCollateral('Maintenance');
|
|
741
741
|
// if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
|
|
742
742
|
let liquidationBuffer = undefined;
|
|
743
|
-
const isBeingLiquidated = types_1.isVariant(this.getUserAccount().status, 'beingLiquidated');
|
|
743
|
+
const isBeingLiquidated = (0, types_1.isVariant)(this.getUserAccount().status, 'beingLiquidated');
|
|
744
744
|
if (isBeingLiquidated) {
|
|
745
745
|
liquidationBuffer = new _1.BN(this.driftClient.getStateAccount().liquidationMarginBufferRatio);
|
|
746
746
|
}
|
|
@@ -748,13 +748,13 @@ class User {
|
|
|
748
748
|
return totalCollateral.lt(maintenanceRequirement);
|
|
749
749
|
}
|
|
750
750
|
isBeingLiquidated() {
|
|
751
|
-
return types_1.isOneOfVariant(this.getUserAccount().status, [
|
|
751
|
+
return (0, types_1.isOneOfVariant)(this.getUserAccount().status, [
|
|
752
752
|
'beingLiquidated',
|
|
753
753
|
'bankrupt',
|
|
754
754
|
]);
|
|
755
755
|
}
|
|
756
756
|
isBankrupt() {
|
|
757
|
-
return types_1.isVariant(this.getUserAccount().status, 'bankrupt');
|
|
757
|
+
return (0, types_1.isVariant)(this.getUserAccount().status, 'bankrupt');
|
|
758
758
|
}
|
|
759
759
|
/**
|
|
760
760
|
* Checks if any user position cumulative funding differs from respective market cumulative funding
|
|
@@ -788,7 +788,7 @@ class User {
|
|
|
788
788
|
const maintenanceMarginRequirement = this.getMaintenanceMarginRequirement();
|
|
789
789
|
const freeCollateral = _1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(maintenanceMarginRequirement));
|
|
790
790
|
const market = this.driftClient.getSpotMarketAccount(marketIndex);
|
|
791
|
-
let signedTokenAmount = _1.getSignedTokenAmount(spotBalance_1.getTokenAmount(currentSpotPosition.scaledBalance, market, currentSpotPosition.balanceType), currentSpotPosition.balanceType);
|
|
791
|
+
let signedTokenAmount = (0, _1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(currentSpotPosition.scaledBalance, market, currentSpotPosition.balanceType), currentSpotPosition.balanceType);
|
|
792
792
|
signedTokenAmount = signedTokenAmount.add(positionBaseSizeChange);
|
|
793
793
|
if (signedTokenAmount.eq(numericConstants_1.ZERO)) {
|
|
794
794
|
return new _1.BN(-1);
|
|
@@ -841,7 +841,7 @@ class User {
|
|
|
841
841
|
if (spotMarketWithSameOracle) {
|
|
842
842
|
const spotPosition = this.getSpotPosition(spotMarketWithSameOracle.marketIndex);
|
|
843
843
|
if (spotPosition) {
|
|
844
|
-
const signedTokenAmount = _1.getSignedTokenAmount(spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketWithSameOracle, spotPosition.balanceType), spotPosition.balanceType);
|
|
844
|
+
const signedTokenAmount = (0, _1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketWithSameOracle, spotPosition.balanceType), spotPosition.balanceType);
|
|
845
845
|
const spotFreeCollateralDelta = this.calculateFreeCollateralDeltaForSpot(spotMarketWithSameOracle, signedTokenAmount);
|
|
846
846
|
freeCollateralDelta = freeCollateralDelta.add(spotFreeCollateralDelta || numericConstants_1.ZERO);
|
|
847
847
|
}
|
|
@@ -861,11 +861,11 @@ class User {
|
|
|
861
861
|
}
|
|
862
862
|
calculateFreeCollateralDeltaForPerp(market, perpPosition, positionBaseSizeChange) {
|
|
863
863
|
const currentBaseAssetAmount = perpPosition.baseAssetAmount;
|
|
864
|
-
const worstCaseBaseAssetAmount = margin_1.calculateWorstCaseBaseAssetAmount(perpPosition);
|
|
864
|
+
const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
|
|
865
865
|
const orderBaseAssetAmount = worstCaseBaseAssetAmount.sub(currentBaseAssetAmount);
|
|
866
866
|
const proposedBaseAssetAmount = currentBaseAssetAmount.add(positionBaseSizeChange);
|
|
867
867
|
const proposedWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(positionBaseSizeChange);
|
|
868
|
-
const marginRatio = _1.calculateMarketMarginRatio(market, proposedWorstCaseBaseAssetAmount.abs(), 'Maintenance');
|
|
868
|
+
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedWorstCaseBaseAssetAmount.abs(), 'Maintenance');
|
|
869
869
|
const marginRatioQuotePrecision = new _1.BN(marginRatio)
|
|
870
870
|
.mul(numericConstants_1.QUOTE_PRECISION)
|
|
871
871
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
@@ -892,14 +892,14 @@ class User {
|
|
|
892
892
|
calculateFreeCollateralDeltaForSpot(market, signedTokenAmount) {
|
|
893
893
|
const tokenPrecision = new _1.BN(Math.pow(10, market.decimals));
|
|
894
894
|
if (signedTokenAmount.gt(numericConstants_1.ZERO)) {
|
|
895
|
-
const assetWeight = spotBalance_1.calculateAssetWeight(signedTokenAmount, market, 'Maintenance');
|
|
895
|
+
const assetWeight = (0, spotBalance_1.calculateAssetWeight)(signedTokenAmount, market, 'Maintenance');
|
|
896
896
|
return numericConstants_1.QUOTE_PRECISION.mul(assetWeight)
|
|
897
897
|
.div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
|
|
898
898
|
.mul(signedTokenAmount)
|
|
899
899
|
.div(tokenPrecision);
|
|
900
900
|
}
|
|
901
901
|
else {
|
|
902
|
-
const liabilityWeight = spotBalance_1.calculateLiabilityWeight(signedTokenAmount.abs(), market, 'Maintenance');
|
|
902
|
+
const liabilityWeight = (0, spotBalance_1.calculateLiabilityWeight)(signedTokenAmount.abs(), market, 'Maintenance');
|
|
903
903
|
return numericConstants_1.QUOTE_PRECISION.neg()
|
|
904
904
|
.mul(liabilityWeight)
|
|
905
905
|
.div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
|
|
@@ -949,7 +949,7 @@ class User {
|
|
|
949
949
|
getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide) {
|
|
950
950
|
const currentPosition = this.getPerpPosition(targetMarketIndex) ||
|
|
951
951
|
this.getEmptyPosition(targetMarketIndex);
|
|
952
|
-
const targetSide = types_1.isVariant(tradeSide, 'short') ? 'short' : 'long';
|
|
952
|
+
const targetSide = (0, types_1.isVariant)(tradeSide, 'short') ? 'short' : 'long';
|
|
953
953
|
const currentPositionSide = (currentPosition === null || currentPosition === void 0 ? void 0 : currentPosition.baseAssetAmount.isNeg())
|
|
954
954
|
? 'short'
|
|
955
955
|
: 'long';
|
|
@@ -1013,26 +1013,26 @@ class User {
|
|
|
1013
1013
|
currentSpotMarketNetValue =
|
|
1014
1014
|
currentSpotMarketNetValue !== null && currentSpotMarketNetValue !== void 0 ? currentSpotMarketNetValue : this.getSpotPositionValue(targetMarketIndex);
|
|
1015
1015
|
let freeCollateral = this.getFreeCollateral();
|
|
1016
|
-
const marginRatio = _1.calculateSpotMarketMarginRatio(market, 'Initial', numericConstants_1.ZERO, types_1.isVariant(direction, 'long')
|
|
1016
|
+
const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', numericConstants_1.ZERO, (0, types_1.isVariant)(direction, 'long')
|
|
1017
1017
|
? _1.SpotBalanceType.DEPOSIT
|
|
1018
1018
|
: _1.SpotBalanceType.BORROW);
|
|
1019
1019
|
let tradeAmount = numericConstants_1.ZERO;
|
|
1020
1020
|
if (this.getUserAccount().isMarginTradingEnabled) {
|
|
1021
1021
|
// if the user is buying/selling and already short/long, need to account for closing out short/long
|
|
1022
|
-
if (types_1.isVariant(direction, 'long') && currentSpotMarketNetValue.lt(numericConstants_1.ZERO)) {
|
|
1022
|
+
if ((0, types_1.isVariant)(direction, 'long') && currentSpotMarketNetValue.lt(numericConstants_1.ZERO)) {
|
|
1023
1023
|
tradeAmount = currentSpotMarketNetValue.abs();
|
|
1024
|
-
const marginRatio = _1.calculateSpotMarketMarginRatio(market, 'Initial', this.getSpotTokenAmount(targetMarketIndex), _1.SpotBalanceType.BORROW);
|
|
1024
|
+
const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', this.getSpotTokenAmount(targetMarketIndex), _1.SpotBalanceType.BORROW);
|
|
1025
1025
|
freeCollateral = freeCollateral.add(tradeAmount.mul(new _1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION));
|
|
1026
1026
|
}
|
|
1027
|
-
else if (types_1.isVariant(direction, 'short') &&
|
|
1027
|
+
else if ((0, types_1.isVariant)(direction, 'short') &&
|
|
1028
1028
|
currentSpotMarketNetValue.gt(numericConstants_1.ZERO)) {
|
|
1029
1029
|
tradeAmount = currentSpotMarketNetValue;
|
|
1030
|
-
const marginRatio = _1.calculateSpotMarketMarginRatio(market, 'Initial', this.getSpotTokenAmount(targetMarketIndex), _1.SpotBalanceType.DEPOSIT);
|
|
1030
|
+
const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', this.getSpotTokenAmount(targetMarketIndex), _1.SpotBalanceType.DEPOSIT);
|
|
1031
1031
|
freeCollateral = freeCollateral.add(tradeAmount.mul(new _1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION));
|
|
1032
1032
|
}
|
|
1033
1033
|
tradeAmount = tradeAmount.add(freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new _1.BN(marginRatio)));
|
|
1034
1034
|
}
|
|
1035
|
-
else if (types_1.isVariant(direction, 'long')) {
|
|
1035
|
+
else if ((0, types_1.isVariant)(direction, 'long')) {
|
|
1036
1036
|
tradeAmount = _1.BN.min(currentQuoteAssetValue, freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new _1.BN(marginRatio)));
|
|
1037
1037
|
}
|
|
1038
1038
|
else {
|
|
@@ -1051,7 +1051,7 @@ class User {
|
|
|
1051
1051
|
* @returns leverageRatio : Precision TEN_THOUSAND
|
|
1052
1052
|
*/
|
|
1053
1053
|
accountLeverageRatioAfterTrade(targetMarketIndex, targetMarketType, tradeQuoteAmount, tradeSide, includeOpenOrders = true) {
|
|
1054
|
-
const tradeIsPerp = types_1.isVariant(targetMarketType, 'perp');
|
|
1054
|
+
const tradeIsPerp = (0, types_1.isVariant)(targetMarketType, 'perp');
|
|
1055
1055
|
if (!tradeIsPerp) {
|
|
1056
1056
|
// calculate new asset/liability values for base and quote market to find new account leverage
|
|
1057
1057
|
const totalLiabilityValue = this.getTotalLiabilityValue();
|
|
@@ -1142,7 +1142,7 @@ class User {
|
|
|
1142
1142
|
const nowTs = new _1.BN(Math.floor(Date.now() / 1000));
|
|
1143
1143
|
const spotMarket = this.driftClient.getSpotMarketAccount(marketIndex);
|
|
1144
1144
|
// eslint-disable-next-line prefer-const
|
|
1145
|
-
let { borrowLimit, withdrawLimit } = spotBalance_1.calculateWithdrawLimit(spotMarket, nowTs);
|
|
1145
|
+
let { borrowLimit, withdrawLimit } = (0, spotBalance_1.calculateWithdrawLimit)(spotMarket, nowTs);
|
|
1146
1146
|
const freeCollateral = this.getFreeCollateral();
|
|
1147
1147
|
const oracleData = this.getOracleDataForSpotMarket(marketIndex);
|
|
1148
1148
|
const precisionIncrease = numericConstants_1.TEN.pow(new _1.BN(spotMarket.decimals - 6));
|
|
@@ -1188,7 +1188,7 @@ class User {
|
|
|
1188
1188
|
netDeposits,
|
|
1189
1189
|
};
|
|
1190
1190
|
}
|
|
1191
|
-
if (types_1.isVariant(position.balanceType, 'borrow')) {
|
|
1191
|
+
if ((0, types_1.isVariant)(position.balanceType, 'borrow')) {
|
|
1192
1192
|
return {
|
|
1193
1193
|
canBypass: false,
|
|
1194
1194
|
maxDepositAmount,
|
|
@@ -1196,7 +1196,7 @@ class User {
|
|
|
1196
1196
|
depositAmount: numericConstants_1.ZERO,
|
|
1197
1197
|
};
|
|
1198
1198
|
}
|
|
1199
|
-
const depositAmount = spotBalance_1.getTokenAmount(position.scaledBalance, spotMarket, _1.SpotBalanceType.DEPOSIT);
|
|
1199
|
+
const depositAmount = (0, spotBalance_1.getTokenAmount)(position.scaledBalance, spotMarket, _1.SpotBalanceType.DEPOSIT);
|
|
1200
1200
|
if (netDeposits.lt(numericConstants_1.ZERO)) {
|
|
1201
1201
|
return {
|
|
1202
1202
|
canBypass: false,
|
package/lib/userConfig.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { DriftClient } from './driftClient';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
|
-
export
|
|
4
|
+
export type UserConfig = {
|
|
5
5
|
accountSubscription?: UserSubscriptionConfig;
|
|
6
6
|
driftClient: DriftClient;
|
|
7
7
|
userAccountPublicKey: PublicKey;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export type UserSubscriptionConfig = {
|
|
10
10
|
type: 'websocket';
|
|
11
11
|
} | {
|
|
12
12
|
type: 'polling';
|
package/lib/userMap/userMap.js
CHANGED
|
@@ -26,7 +26,7 @@ class UserMap {
|
|
|
26
26
|
userAccountArray.push(programUserAccount.account);
|
|
27
27
|
}
|
|
28
28
|
if (this.accountSubscription.type === 'polling') {
|
|
29
|
-
await __1.bulkPollingUserSubscribe(userArray, this.accountSubscription.accountLoader);
|
|
29
|
+
await (0, __1.bulkPollingUserSubscribe)(userArray, this.accountSubscription.accountLoader);
|
|
30
30
|
}
|
|
31
31
|
else {
|
|
32
32
|
await Promise.all(userArray.map((user, i) => user.subscribe(userAccountArray[i])));
|
|
@@ -23,14 +23,14 @@ class UserStatsMap {
|
|
|
23
23
|
}
|
|
24
24
|
const chUserStat = new __1.UserStats({
|
|
25
25
|
driftClient: this.driftClient,
|
|
26
|
-
userStatsAccountPublicKey:
|
|
26
|
+
userStatsAccountPublicKey: programUserAccount.publicKey,
|
|
27
27
|
accountSubscription: this.accountSubscription,
|
|
28
28
|
});
|
|
29
29
|
userStatArray.push(chUserStat);
|
|
30
30
|
userStatsAccountArray.push(userStat);
|
|
31
31
|
}
|
|
32
32
|
if (this.accountSubscription.type === 'polling') {
|
|
33
|
-
await __1.bulkPollingUserStatsSubscribe(userStatArray, this.accountSubscription.accountLoader);
|
|
33
|
+
await (0, __1.bulkPollingUserStatsSubscribe)(userStatArray, this.accountSubscription.accountLoader);
|
|
34
34
|
}
|
|
35
35
|
else {
|
|
36
36
|
await Promise.all(userStatArray.map((userStat, i) => userStat.subscribe(userStatsAccountArray[i])));
|
|
@@ -42,7 +42,7 @@ class UserStatsMap {
|
|
|
42
42
|
async addUserStat(authority) {
|
|
43
43
|
const userStat = new __1.UserStats({
|
|
44
44
|
driftClient: this.driftClient,
|
|
45
|
-
userStatsAccountPublicKey: __1.getUserStatsAccountPublicKey(this.driftClient.program.programId, authority),
|
|
45
|
+
userStatsAccountPublicKey: (0, __1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, authority),
|
|
46
46
|
accountSubscription: this.accountSubscription,
|
|
47
47
|
});
|
|
48
48
|
await userStat.subscribe();
|
package/lib/userStats.js
CHANGED
|
@@ -40,8 +40,8 @@ class UserStats {
|
|
|
40
40
|
}
|
|
41
41
|
else {
|
|
42
42
|
return {
|
|
43
|
-
referrer: pda_1.getUserAccountPublicKeySync(this.driftClient.program.programId, this.getAccount().referrer, 0),
|
|
44
|
-
referrerStats: pda_1.getUserStatsAccountPublicKey(this.driftClient.program.programId, this.getAccount().referrer),
|
|
43
|
+
referrer: (0, pda_1.getUserAccountPublicKeySync)(this.driftClient.program.programId, this.getAccount().referrer, 0),
|
|
44
|
+
referrerStats: (0, pda_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, this.getAccount().referrer),
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
47
|
}
|
package/lib/userStatsConfig.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { DriftClient } from './driftClient';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
|
-
export
|
|
4
|
+
export type UserStatsConfig = {
|
|
5
5
|
accountSubscription?: UserStatsSubscriptionConfig;
|
|
6
6
|
driftClient: DriftClient;
|
|
7
7
|
userStatsAccountPublicKey: PublicKey;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export type UserStatsSubscriptionConfig = {
|
|
10
10
|
type: 'websocket';
|
|
11
11
|
} | {
|
|
12
12
|
type: 'polling';
|
package/package.json
CHANGED
|
@@ -183,9 +183,13 @@ export class BulkAccountLoader {
|
|
|
183
183
|
const accountsToLoad = accountsToLoadChunks[i];
|
|
184
184
|
for (const j in accountsToLoad) {
|
|
185
185
|
const accountToLoad = accountsToLoad[j];
|
|
186
|
-
const key = accountToLoad.publicKey.
|
|
186
|
+
const key = accountToLoad.publicKey.toBase58();
|
|
187
187
|
const oldRPCResponse = this.bufferAndSlotMap.get(key);
|
|
188
188
|
|
|
189
|
+
if (oldRPCResponse && newSlot <= oldRPCResponse.slot) {
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
|
|
189
193
|
let newBuffer: Buffer | undefined = undefined;
|
|
190
194
|
if (rpcResponse.result.value[j]) {
|
|
191
195
|
const raw: string = rpcResponse.result.value[j].data[0];
|
|
@@ -202,10 +206,6 @@ export class BulkAccountLoader {
|
|
|
202
206
|
continue;
|
|
203
207
|
}
|
|
204
208
|
|
|
205
|
-
if (newSlot <= oldRPCResponse.slot) {
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
209
|
const oldBuffer = oldRPCResponse.buffer;
|
|
210
210
|
if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
|
|
211
211
|
this.bufferAndSlotMap.set(key, {
|
package/src/accounts/fetch.ts
CHANGED
|
@@ -19,6 +19,14 @@ export async function fetchUserAccounts(
|
|
|
19
19
|
);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
return fetchUserAccountsUsingKeys(connection, program, userAccountPublicKeys);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function fetchUserAccountsUsingKeys(
|
|
26
|
+
connection: Connection,
|
|
27
|
+
program: Program,
|
|
28
|
+
userAccountPublicKeys: PublicKey[]
|
|
29
|
+
): Promise<(UserAccount | undefined)[]> {
|
|
22
30
|
const accountInfos = await connection.getMultipleAccountsInfo(
|
|
23
31
|
userAccountPublicKeys,
|
|
24
32
|
'confirmed'
|
package/src/addresses/pda.ts
CHANGED
|
@@ -188,3 +188,16 @@ export function getSerumFulfillmentConfigPublicKey(
|
|
|
188
188
|
programId
|
|
189
189
|
)[0];
|
|
190
190
|
}
|
|
191
|
+
|
|
192
|
+
export function getReferrerNamePublicKeySync(
|
|
193
|
+
programId: PublicKey,
|
|
194
|
+
nameBuffer: number[]
|
|
195
|
+
): PublicKey {
|
|
196
|
+
return PublicKey.findProgramAddressSync(
|
|
197
|
+
[
|
|
198
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('referrer_name')),
|
|
199
|
+
Buffer.from(nameBuffer),
|
|
200
|
+
],
|
|
201
|
+
programId
|
|
202
|
+
)[0];
|
|
203
|
+
}
|