@drift-labs/sdk 2.33.1-beta.1 → 2.33.1-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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.33.1-beta.1
1
+ 2.33.1-beta.2
package/lib/user.js CHANGED
@@ -1113,7 +1113,6 @@ class User {
1113
1113
  const outOraclePrice = this.getOracleDataForSpotMarket(outMarketIndex).price;
1114
1114
  const inPrecision = new _1.BN(10 ** inMarket.decimals);
1115
1115
  const outPrecision = new _1.BN(10 ** outMarket.decimals);
1116
- const outSaferThanIn = inMarket.initialAssetWeight < outMarket.initialAssetWeight;
1117
1116
  const inSpotPosition = this.getSpotPosition(inMarketIndex) ||
1118
1117
  this.getEmptySpotPosition(inMarketIndex);
1119
1118
  const outSpotPosition = this.getSpotPosition(outMarketIndex) ||
@@ -1137,6 +1136,12 @@ class User {
1137
1136
  let inSwap = numericConstants_1.ZERO;
1138
1137
  let outSwap = numericConstants_1.ZERO;
1139
1138
  const inTokenAmount = this.getTokenAmount(inMarketIndex);
1139
+ const outTokenAmount = this.getTokenAmount(outMarketIndex);
1140
+ const outSaferThanIn =
1141
+ // selling asset to close borrow
1142
+ (inTokenAmount.gt(numericConstants_1.ZERO) && outTokenAmount.lt(numericConstants_1.ZERO)) ||
1143
+ // buying asset with higher initial asset weight
1144
+ inMarket.initialAssetWeight < outMarket.initialAssetWeight;
1140
1145
  if (freeCollateral.lt(numericConstants_1.ONE)) {
1141
1146
  if (outSaferThanIn && inTokenAmount.gt(numericConstants_1.ZERO)) {
1142
1147
  inSwap = inTokenAmount;
@@ -1145,11 +1150,9 @@ class User {
1145
1150
  }
1146
1151
  else {
1147
1152
  let minSwap = numericConstants_1.ZERO;
1148
- let maxSwap = freeCollateral
1149
- .mul(inPrecision)
1150
- .mul(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
1151
- .div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.div(new _1.BN(100)))
1152
- .div(inOraclePrice); // just assume user can go 100x
1153
+ let maxSwap = _1.BN.max(freeCollateral.mul(inPrecision).mul(new _1.BN(100)).div(inOraclePrice), // 100x current free collateral
1154
+ inTokenAmount.abs().mul(new _1.BN(10)) // 10x current position
1155
+ );
1153
1156
  inSwap = maxSwap.div(numericConstants_1.TWO);
1154
1157
  const error = freeCollateral.div(new _1.BN(10000));
1155
1158
  let i = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.33.1-beta.1",
3
+ "version": "2.33.1-beta.2",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
package/src/user.ts CHANGED
@@ -2009,9 +2009,6 @@ export class User {
2009
2009
  const inPrecision = new BN(10 ** inMarket.decimals);
2010
2010
  const outPrecision = new BN(10 ** outMarket.decimals);
2011
2011
 
2012
- const outSaferThanIn =
2013
- inMarket.initialAssetWeight < outMarket.initialAssetWeight;
2014
-
2015
2012
  const inSpotPosition =
2016
2013
  this.getSpotPosition(inMarketIndex) ||
2017
2014
  this.getEmptySpotPosition(inMarketIndex);
@@ -2053,6 +2050,14 @@ export class User {
2053
2050
  let inSwap = ZERO;
2054
2051
  let outSwap = ZERO;
2055
2052
  const inTokenAmount = this.getTokenAmount(inMarketIndex);
2053
+ const outTokenAmount = this.getTokenAmount(outMarketIndex);
2054
+
2055
+ const outSaferThanIn =
2056
+ // selling asset to close borrow
2057
+ (inTokenAmount.gt(ZERO) && outTokenAmount.lt(ZERO)) ||
2058
+ // buying asset with higher initial asset weight
2059
+ inMarket.initialAssetWeight < outMarket.initialAssetWeight;
2060
+
2056
2061
  if (freeCollateral.lt(ONE)) {
2057
2062
  if (outSaferThanIn && inTokenAmount.gt(ZERO)) {
2058
2063
  inSwap = inTokenAmount;
@@ -2060,11 +2065,10 @@ export class User {
2060
2065
  }
2061
2066
  } else {
2062
2067
  let minSwap = ZERO;
2063
- let maxSwap = freeCollateral
2064
- .mul(inPrecision)
2065
- .mul(SPOT_MARKET_WEIGHT_PRECISION)
2066
- .div(SPOT_MARKET_WEIGHT_PRECISION.div(new BN(100)))
2067
- .div(inOraclePrice); // just assume user can go 100x
2068
+ let maxSwap = BN.max(
2069
+ freeCollateral.mul(inPrecision).mul(new BN(100)).div(inOraclePrice), // 100x current free collateral
2070
+ inTokenAmount.abs().mul(new BN(10)) // 10x current position
2071
+ );
2068
2072
  inSwap = maxSwap.div(TWO);
2069
2073
  const error = freeCollateral.div(new BN(10000));
2070
2074
 
@@ -22,10 +22,11 @@ import {
22
22
  ZERO,
23
23
  convertToNumber,
24
24
  QUOTE_PRECISION,
25
+ isVariant,
26
+ TWO
25
27
  } from '../../src';
26
28
 
27
29
  import { mockPerpMarkets, mockSpotMarkets, mockStateAccount } from './helpers';
28
- import { isVariant, TWO } from '../../lib';
29
30
  import { DLOBOrdersCoder } from '../../src/dlob/DLOBOrders';
30
31
 
31
32
  function insertOrderToDLOB(