@drift-labs/sdk 2.60.0-beta.12 → 2.60.0-beta.14

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 CHANGED
@@ -1 +1 @@
1
- 2.60.0-beta.12
1
+ 2.60.0-beta.14
@@ -1,5 +1,5 @@
1
1
  /// <reference types="bn.js" />
2
- import { PerpMarketAccount, PositionDirection, SpotMarketAccount } from '../types';
2
+ import { PerpMarketAccount, PositionDirection, SpotMarketAccount, UserStatsAccount } from '../types';
3
3
  import { BN } from '@coral-xyz/anchor';
4
4
  import { AssetType } from './amm';
5
5
  import { OraclePriceData } from '../oracles/types';
@@ -114,3 +114,4 @@ export declare function calculateEstimatedEntryPriceWithL2(assetType: AssetType,
114
114
  baseFilled: BN;
115
115
  quoteFilled: BN;
116
116
  };
117
+ export declare function getUser30dRollingVolumeEstimate(userStatsAccount: UserStatsAccount, now?: BN): BN;
package/lib/math/trade.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateEstimatedEntryPriceWithL2 = exports.calculateEstimatedSpotEntryPrice = exports.calculateEstimatedPerpEntryPrice = exports.calculateTargetPriceTrade = exports.calculateTradeAcquiredAmounts = exports.calculateTradeSlippage = void 0;
3
+ exports.getUser30dRollingVolumeEstimate = exports.calculateEstimatedEntryPriceWithL2 = exports.calculateEstimatedSpotEntryPrice = exports.calculateEstimatedPerpEntryPrice = exports.calculateTargetPriceTrade = exports.calculateTradeAcquiredAmounts = exports.calculateTradeSlippage = void 0;
4
4
  const types_1 = require("../types");
5
5
  const anchor_1 = require("@coral-xyz/anchor");
6
6
  const assert_1 = require("../assert/assert");
@@ -621,3 +621,17 @@ function calculateEstimatedEntryPriceWithL2(assetType, amount, direction, basePr
621
621
  };
622
622
  }
623
623
  exports.calculateEstimatedEntryPriceWithL2 = calculateEstimatedEntryPriceWithL2;
624
+ function getUser30dRollingVolumeEstimate(userStatsAccount, now) {
625
+ now = now || new anchor_1.BN(new Date().getTime() / 1000);
626
+ const sinceLastTaker = anchor_1.BN.max(now.sub(userStatsAccount.lastTakerVolume30DTs), numericConstants_1.ZERO);
627
+ const sinceLastMaker = anchor_1.BN.max(now.sub(userStatsAccount.lastMakerVolume30DTs), numericConstants_1.ZERO);
628
+ const thirtyDaysInSeconds = new anchor_1.BN(60 * 60 * 24 * 30);
629
+ const last30dVolume = userStatsAccount.takerVolume30D
630
+ .mul(anchor_1.BN.max(thirtyDaysInSeconds.sub(sinceLastTaker), numericConstants_1.ZERO))
631
+ .div(thirtyDaysInSeconds)
632
+ .add(userStatsAccount.makerVolume30D
633
+ .mul(anchor_1.BN.max(thirtyDaysInSeconds.sub(sinceLastMaker), numericConstants_1.ZERO))
634
+ .div(thirtyDaysInSeconds));
635
+ return last30dVolume;
636
+ }
637
+ exports.getUser30dRollingVolumeEstimate = getUser30dRollingVolumeEstimate;
package/lib/user.d.ts CHANGED
@@ -340,7 +340,7 @@ export declare class User {
340
340
  * @returns leverageRatio : Precision TEN_THOUSAND
341
341
  */
342
342
  accountLeverageRatioAfterTrade(targetMarketIndex: number, targetMarketType: MarketType, tradeQuoteAmount: BN, tradeSide: PositionDirection, includeOpenOrders?: boolean): BN;
343
- getUserFeeTier(marketType: MarketType): import("./types").FeeTier;
343
+ getUserFeeTier(marketType: MarketType, now?: BN): import("./types").FeeTier;
344
344
  /**
345
345
  * Calculates taker / maker fee (as a percentage, e.g. .001 = 10 basis points) for particular marketType
346
346
  * @param marketType
package/lib/user.js CHANGED
@@ -1136,6 +1136,8 @@ class User {
1136
1136
  freeCollateralChange = costBasis.sub(newPositionValue);
1137
1137
  }
1138
1138
  else {
1139
+ console.log('newPositionValue', newPositionValue.toString());
1140
+ console.log('costBasis', costBasis.toString());
1139
1141
  freeCollateralChange = newPositionValue.sub(costBasis);
1140
1142
  }
1141
1143
  // assume worst fee tier
@@ -1146,17 +1148,12 @@ class User {
1146
1148
  freeCollateralChange = freeCollateralChange.sub(takerFee);
1147
1149
  }
1148
1150
  const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
1149
- const marginRatioBefore = (0, _1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(), 'Maintenance');
1150
1151
  const newWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(positionBaseSizeChange);
1151
1152
  const newMarginRatio = (0, _1.calculateMarketMarginRatio)(market, newWorstCaseBaseAssetAmount.abs(), 'Maintenance');
1152
- // update free collateral to account for change in margin ratio from position change
1153
- freeCollateralChange = freeCollateralChange.sub(worstCaseBaseAssetAmount
1154
- .mul(oraclePrice)
1155
- .div(numericConstants_1.BASE_PRECISION)
1156
- .mul(new _1.BN(newMarginRatio - marginRatioBefore))
1157
- .div(numericConstants_1.MARGIN_PRECISION));
1158
1153
  // update free collateral to account for new margin requirement from position change
1159
- freeCollateralChange = freeCollateralChange.sub(positionBaseSizeChange
1154
+ freeCollateralChange = freeCollateralChange.sub(newWorstCaseBaseAssetAmount
1155
+ .abs()
1156
+ .sub(worstCaseBaseAssetAmount.abs())
1160
1157
  .mul(oraclePrice)
1161
1158
  .div(numericConstants_1.BASE_PRECISION)
1162
1159
  .mul(new _1.BN(newMarginRatio))
@@ -1643,14 +1640,14 @@ class User {
1643
1640
  .div(netAssetValue);
1644
1641
  return newLeverage;
1645
1642
  }
1646
- getUserFeeTier(marketType) {
1643
+ getUserFeeTier(marketType, now) {
1647
1644
  const state = this.driftClient.getStateAccount();
1648
1645
  let feeTierIndex = 0;
1649
1646
  if ((0, types_1.isVariant)(marketType, 'perp')) {
1650
1647
  const userStatsAccount = this.driftClient
1651
1648
  .getUserStats()
1652
1649
  .getAccount();
1653
- const total30dVolume = userStatsAccount.takerVolume30D.add(userStatsAccount.makerVolume30D); // todo: update using now and lastTs?
1650
+ const total30dVolume = (0, _1.getUser30dRollingVolumeEstimate)(userStatsAccount, now);
1654
1651
  const stakedQuoteAssetAmount = userStatsAccount.ifStakedQuoteAssetAmount;
1655
1652
  const volumeTiers = [
1656
1653
  new _1.BN(100000000).mul(numericConstants_1.QUOTE_PRECISION),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.60.0-beta.12",
3
+ "version": "2.60.0-beta.14",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
package/src/math/trade.ts CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  PerpMarketAccount,
4
4
  PositionDirection,
5
5
  SpotMarketAccount,
6
+ UserStatsAccount,
6
7
  } from '../types';
7
8
  import { BN } from '@coral-xyz/anchor';
8
9
  import { assert } from '../assert/assert';
@@ -964,3 +965,29 @@ export function calculateEstimatedEntryPriceWithL2(
964
965
  quoteFilled: cumulativeQuoteFilled,
965
966
  };
966
967
  }
968
+
969
+ export function getUser30dRollingVolumeEstimate(
970
+ userStatsAccount: UserStatsAccount,
971
+ now?: BN
972
+ ) {
973
+ now = now || new BN(new Date().getTime() / 1000);
974
+ const sinceLastTaker = BN.max(
975
+ now.sub(userStatsAccount.lastTakerVolume30DTs),
976
+ ZERO
977
+ );
978
+ const sinceLastMaker = BN.max(
979
+ now.sub(userStatsAccount.lastMakerVolume30DTs),
980
+ ZERO
981
+ );
982
+ const thirtyDaysInSeconds = new BN(60 * 60 * 24 * 30);
983
+ const last30dVolume = userStatsAccount.takerVolume30D
984
+ .mul(BN.max(thirtyDaysInSeconds.sub(sinceLastTaker), ZERO))
985
+ .div(thirtyDaysInSeconds)
986
+ .add(
987
+ userStatsAccount.makerVolume30D
988
+ .mul(BN.max(thirtyDaysInSeconds.sub(sinceLastMaker), ZERO))
989
+ .div(thirtyDaysInSeconds)
990
+ );
991
+
992
+ return last30dVolume;
993
+ }
package/src/user.ts CHANGED
@@ -55,6 +55,7 @@ import {
55
55
  getSignedTokenAmount,
56
56
  getStrictTokenValue,
57
57
  getTokenValue,
58
+ getUser30dRollingVolumeEstimate,
58
59
  MarketType,
59
60
  PositionDirection,
60
61
  sigNum,
@@ -2092,6 +2093,8 @@ export class User {
2092
2093
  if (positionBaseSizeChange.gt(ZERO)) {
2093
2094
  freeCollateralChange = costBasis.sub(newPositionValue);
2094
2095
  } else {
2096
+ console.log('newPositionValue', newPositionValue.toString());
2097
+ console.log('costBasis', costBasis.toString());
2095
2098
  freeCollateralChange = newPositionValue.sub(costBasis);
2096
2099
  }
2097
2100
 
@@ -2107,12 +2110,6 @@ export class User {
2107
2110
  const worstCaseBaseAssetAmount =
2108
2111
  calculateWorstCaseBaseAssetAmount(perpPosition);
2109
2112
 
2110
- const marginRatioBefore = calculateMarketMarginRatio(
2111
- market,
2112
- worstCaseBaseAssetAmount.abs(),
2113
- 'Maintenance'
2114
- );
2115
-
2116
2113
  const newWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(
2117
2114
  positionBaseSizeChange
2118
2115
  );
@@ -2123,18 +2120,11 @@ export class User {
2123
2120
  'Maintenance'
2124
2121
  );
2125
2122
 
2126
- // update free collateral to account for change in margin ratio from position change
2127
- freeCollateralChange = freeCollateralChange.sub(
2128
- worstCaseBaseAssetAmount
2129
- .mul(oraclePrice)
2130
- .div(BASE_PRECISION)
2131
- .mul(new BN(newMarginRatio - marginRatioBefore))
2132
- .div(MARGIN_PRECISION)
2133
- );
2134
-
2135
2123
  // update free collateral to account for new margin requirement from position change
2136
2124
  freeCollateralChange = freeCollateralChange.sub(
2137
- positionBaseSizeChange
2125
+ newWorstCaseBaseAssetAmount
2126
+ .abs()
2127
+ .sub(worstCaseBaseAssetAmount.abs())
2138
2128
  .mul(oraclePrice)
2139
2129
  .div(BASE_PRECISION)
2140
2130
  .mul(new BN(newMarginRatio))
@@ -3070,7 +3060,7 @@ export class User {
3070
3060
  return newLeverage;
3071
3061
  }
3072
3062
 
3073
- public getUserFeeTier(marketType: MarketType) {
3063
+ public getUserFeeTier(marketType: MarketType, now?: BN) {
3074
3064
  const state = this.driftClient.getStateAccount();
3075
3065
 
3076
3066
  let feeTierIndex = 0;
@@ -3079,9 +3069,10 @@ export class User {
3079
3069
  .getUserStats()
3080
3070
  .getAccount();
3081
3071
 
3082
- const total30dVolume = userStatsAccount.takerVolume30D.add(
3083
- userStatsAccount.makerVolume30D
3084
- ); // todo: update using now and lastTs?
3072
+ const total30dVolume = getUser30dRollingVolumeEstimate(
3073
+ userStatsAccount,
3074
+ now
3075
+ );
3085
3076
 
3086
3077
  const stakedQuoteAssetAmount = userStatsAccount.ifStakedQuoteAssetAmount;
3087
3078
  const volumeTiers = [