@drift-labs/sdk 2.31.1-beta.24 → 2.31.1-beta.25

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.31.1-beta.24
1
+ 2.31.1-beta.25
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.31.1-beta.23",
2
+ "version": "2.31.1-beta.24",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/lib/user.d.ts CHANGED
@@ -127,7 +127,6 @@ export declare class User {
127
127
  getSpotLiabilityValue(tokenAmount: BN, oraclePriceData: OraclePriceData, spotMarketAccount: SpotMarketAccount, marginCategory?: MarginCategory, liquidationBuffer?: BN, strict?: boolean, now?: BN): BN;
128
128
  getSpotMarketAssetValue(marketIndex?: number, marginCategory?: MarginCategory, includeOpenOrders?: boolean, strict?: boolean, now?: BN): BN;
129
129
  getSpotAssetValue(tokenAmount: BN, oraclePriceData: OraclePriceData, spotMarketAccount: SpotMarketAccount, marginCategory?: MarginCategory, strict?: boolean, now?: BN): BN;
130
- getSpotTokenAmount(marketIndex: number): BN;
131
130
  getSpotPositionValue(marketIndex: number, marginCategory?: MarginCategory, includeOpenOrders?: boolean, strict?: boolean, now?: BN): BN;
132
131
  getNetSpotMarketValue(withWeightMarginCategory?: MarginCategory): BN;
133
132
  /**
package/lib/user.js CHANGED
@@ -502,11 +502,6 @@ class User {
502
502
  }
503
503
  return assetValue;
504
504
  }
505
- getSpotTokenAmount(marketIndex) {
506
- var _a;
507
- const spotPosition = (_a = this.getSpotPosition(marketIndex)) !== null && _a !== void 0 ? _a : this.getEmptySpotPosition(marketIndex);
508
- return (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, this.driftClient.getSpotMarketAccount(marketIndex), spotPosition.balanceType);
509
- }
510
505
  getSpotPositionValue(marketIndex, marginCategory, includeOpenOrders, strict = false, now) {
511
506
  const { totalAssetValue, totalLiabilityValue } = this.getSpotMarketAssetAndLiabilityValue(marketIndex, marginCategory, undefined, includeOpenOrders, strict, now);
512
507
  return totalAssetValue.sub(totalLiabilityValue);
@@ -1083,13 +1078,13 @@ class User {
1083
1078
  // if the user is buying/selling and already short/long, need to account for closing out short/long
1084
1079
  if ((0, types_1.isVariant)(direction, 'long') && currentSpotMarketNetValue.lt(numericConstants_1.ZERO)) {
1085
1080
  tradeAmount = currentSpotMarketNetValue.abs();
1086
- const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', this.getSpotTokenAmount(targetMarketIndex), _1.SpotBalanceType.BORROW);
1081
+ const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', this.getTokenAmount(targetMarketIndex).abs(), _1.SpotBalanceType.BORROW);
1087
1082
  freeCollateral = freeCollateral.add(tradeAmount.mul(new _1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION));
1088
1083
  }
1089
1084
  else if ((0, types_1.isVariant)(direction, 'short') &&
1090
1085
  currentSpotMarketNetValue.gt(numericConstants_1.ZERO)) {
1091
1086
  tradeAmount = currentSpotMarketNetValue;
1092
- const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', this.getSpotTokenAmount(targetMarketIndex), _1.SpotBalanceType.DEPOSIT);
1087
+ const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', this.getTokenAmount(targetMarketIndex), _1.SpotBalanceType.DEPOSIT);
1093
1088
  freeCollateral = freeCollateral.add(tradeAmount.mul(new _1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION));
1094
1089
  }
1095
1090
  tradeAmount = tradeAmount.add(freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new _1.BN(marginRatio)));
@@ -1141,9 +1136,9 @@ class User {
1141
1136
  }
1142
1137
  let inSwap = numericConstants_1.ZERO;
1143
1138
  let outSwap = numericConstants_1.ZERO;
1144
- const inTokenAmount = this.getSpotTokenAmount(inMarketIndex);
1139
+ const inTokenAmount = this.getTokenAmount(inMarketIndex);
1145
1140
  if (freeCollateral.lt(numericConstants_1.ONE)) {
1146
- if (outSaferThanIn) {
1141
+ if (outSaferThanIn && inTokenAmount.gt(numericConstants_1.ZERO)) {
1147
1142
  inSwap = inTokenAmount;
1148
1143
  outSwap = calculateSwap(inSwap);
1149
1144
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.31.1-beta.24",
3
+ "version": "2.31.1-beta.25",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.31.1-beta.24",
2
+ "version": "2.31.1-beta.25",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/src/user.ts CHANGED
@@ -934,17 +934,6 @@ export class User {
934
934
  return assetValue;
935
935
  }
936
936
 
937
- public getSpotTokenAmount(marketIndex: number): BN {
938
- const spotPosition =
939
- this.getSpotPosition(marketIndex) ??
940
- this.getEmptySpotPosition(marketIndex);
941
- return getTokenAmount(
942
- spotPosition.scaledBalance,
943
- this.driftClient.getSpotMarketAccount(marketIndex),
944
- spotPosition.balanceType
945
- );
946
- }
947
-
948
937
  public getSpotPositionValue(
949
938
  marketIndex: number,
950
939
  marginCategory?: MarginCategory,
@@ -1953,7 +1942,7 @@ export class User {
1953
1942
  const marginRatio = calculateSpotMarketMarginRatio(
1954
1943
  market,
1955
1944
  'Initial',
1956
- this.getSpotTokenAmount(targetMarketIndex),
1945
+ this.getTokenAmount(targetMarketIndex).abs(),
1957
1946
  SpotBalanceType.BORROW
1958
1947
  );
1959
1948
  freeCollateral = freeCollateral.add(
@@ -1967,7 +1956,7 @@ export class User {
1967
1956
  const marginRatio = calculateSpotMarketMarginRatio(
1968
1957
  market,
1969
1958
  'Initial',
1970
- this.getSpotTokenAmount(targetMarketIndex),
1959
+ this.getTokenAmount(targetMarketIndex),
1971
1960
  SpotBalanceType.DEPOSIT
1972
1961
  );
1973
1962
  freeCollateral = freeCollateral.add(
@@ -2063,9 +2052,9 @@ export class User {
2063
2052
 
2064
2053
  let inSwap = ZERO;
2065
2054
  let outSwap = ZERO;
2066
- const inTokenAmount = this.getSpotTokenAmount(inMarketIndex);
2055
+ const inTokenAmount = this.getTokenAmount(inMarketIndex);
2067
2056
  if (freeCollateral.lt(ONE)) {
2068
- if (outSaferThanIn) {
2057
+ if (outSaferThanIn && inTokenAmount.gt(ZERO)) {
2069
2058
  inSwap = inTokenAmount;
2070
2059
  outSwap = calculateSwap(inSwap);
2071
2060
  }