@drift-labs/sdk 2.60.0-beta.13 → 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.13
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
@@ -1640,14 +1640,14 @@ class User {
1640
1640
  .div(netAssetValue);
1641
1641
  return newLeverage;
1642
1642
  }
1643
- getUserFeeTier(marketType) {
1643
+ getUserFeeTier(marketType, now) {
1644
1644
  const state = this.driftClient.getStateAccount();
1645
1645
  let feeTierIndex = 0;
1646
1646
  if ((0, types_1.isVariant)(marketType, 'perp')) {
1647
1647
  const userStatsAccount = this.driftClient
1648
1648
  .getUserStats()
1649
1649
  .getAccount();
1650
- const total30dVolume = userStatsAccount.takerVolume30D.add(userStatsAccount.makerVolume30D); // todo: update using now and lastTs?
1650
+ const total30dVolume = (0, _1.getUser30dRollingVolumeEstimate)(userStatsAccount, now);
1651
1651
  const stakedQuoteAssetAmount = userStatsAccount.ifStakedQuoteAssetAmount;
1652
1652
  const volumeTiers = [
1653
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.13",
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,
@@ -3059,7 +3060,7 @@ export class User {
3059
3060
  return newLeverage;
3060
3061
  }
3061
3062
 
3062
- public getUserFeeTier(marketType: MarketType) {
3063
+ public getUserFeeTier(marketType: MarketType, now?: BN) {
3063
3064
  const state = this.driftClient.getStateAccount();
3064
3065
 
3065
3066
  let feeTierIndex = 0;
@@ -3068,9 +3069,10 @@ export class User {
3068
3069
  .getUserStats()
3069
3070
  .getAccount();
3070
3071
 
3071
- const total30dVolume = userStatsAccount.takerVolume30D.add(
3072
- userStatsAccount.makerVolume30D
3073
- ); // todo: update using now and lastTs?
3072
+ const total30dVolume = getUser30dRollingVolumeEstimate(
3073
+ userStatsAccount,
3074
+ now
3075
+ );
3074
3076
 
3075
3077
  const stakedQuoteAssetAmount = userStatsAccount.ifStakedQuoteAssetAmount;
3076
3078
  const volumeTiers = [