@drift-labs/sdk 0.1.32 → 0.1.33

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.
@@ -59,14 +59,15 @@ function isOrderReduceOnly(user, order) {
59
59
  const position = user.getUserPosition(order.marketIndex) ||
60
60
  user.getEmptyPosition(order.marketIndex);
61
61
  // if position is long and order is long
62
- if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && (0, types_1.isVariant)(order.direction, 'long')) {
62
+ if (position.baseAssetAmount.gte(numericConstants_1.ZERO) &&
63
+ (0, types_1.isVariant)(order.direction, 'long')) {
63
64
  return false;
64
65
  }
65
66
  // if position is short and order is short
66
- if (position.baseAssetAmount.lt(numericConstants_1.ZERO) &&
67
+ if (position.baseAssetAmount.lte(numericConstants_1.ZERO) &&
67
68
  (0, types_1.isVariant)(order.direction, 'short')) {
68
69
  return false;
69
70
  }
70
- return order.baseAssetAmount.abs().lte(position.baseAssetAmount.abs());
71
+ return true;
71
72
  }
72
73
  exports.isOrderReduceOnly = isOrderReduceOnly;
package/lib/orders.js CHANGED
@@ -174,6 +174,20 @@ function calculateBaseAssetAmountUserCanExecute(market, order, user) {
174
174
  const [_, baseAssetReservesAfter] = (0, _1.calculateAmmReservesAfterSwap)(market.amm, 'quote', quoteAssetAmount, (0, types_1.isVariant)(order.direction, 'long')
175
175
  ? types_1.SwapDirection.ADD
176
176
  : types_1.SwapDirection.REMOVE);
177
- return baseAssetReservesBefore.sub(baseAssetReservesAfter).abs();
177
+ let baseAssetAmount = baseAssetReservesBefore.sub(baseAssetReservesAfter).abs();
178
+ if (order.reduceOnly) {
179
+ const position = user.getUserPosition(order.marketIndex) ||
180
+ user.getEmptyPosition(order.marketIndex);
181
+ if ((0, types_1.isVariant)(order.direction, 'long') && position.baseAssetAmount.gte(numericConstants_1.ZERO)) {
182
+ baseAssetAmount = numericConstants_1.ZERO;
183
+ }
184
+ else if ((0, types_1.isVariant)(order.direction, 'short') && position.baseAssetAmount.lte(numericConstants_1.ZERO)) {
185
+ baseAssetAmount = numericConstants_1.ZERO;
186
+ }
187
+ else {
188
+ _1.BN.min(baseAssetAmount, position.baseAssetAmount.abs());
189
+ }
190
+ }
191
+ return baseAssetAmount;
178
192
  }
179
193
  exports.calculateBaseAssetAmountUserCanExecute = calculateBaseAssetAmountUserCanExecute;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.1.32",
3
+ "version": "0.1.33",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -89,17 +89,20 @@ export function isOrderReduceOnly(
89
89
  user.getEmptyPosition(order.marketIndex);
90
90
 
91
91
  // if position is long and order is long
92
- if (position.baseAssetAmount.gt(ZERO) && isVariant(order.direction, 'long')) {
92
+ if (
93
+ position.baseAssetAmount.gte(ZERO) &&
94
+ isVariant(order.direction, 'long')
95
+ ) {
93
96
  return false;
94
97
  }
95
98
 
96
99
  // if position is short and order is short
97
100
  if (
98
- position.baseAssetAmount.lt(ZERO) &&
101
+ position.baseAssetAmount.lte(ZERO) &&
99
102
  isVariant(order.direction, 'short')
100
103
  ) {
101
104
  return false;
102
105
  }
103
106
 
104
- return order.baseAssetAmount.abs().lte(position.baseAssetAmount.abs());
107
+ return true;
105
108
  }
package/src/orders.ts CHANGED
@@ -305,5 +305,19 @@ export function calculateBaseAssetAmountUserCanExecute(
305
305
  : SwapDirection.REMOVE
306
306
  );
307
307
 
308
- return baseAssetReservesBefore.sub(baseAssetReservesAfter).abs();
308
+ let baseAssetAmount = baseAssetReservesBefore.sub(baseAssetReservesAfter).abs();
309
+ if (order.reduceOnly) {
310
+ const position =
311
+ user.getUserPosition(order.marketIndex) ||
312
+ user.getEmptyPosition(order.marketIndex);
313
+ if (isVariant(order.direction, 'long') && position.baseAssetAmount.gte(ZERO)) {
314
+ baseAssetAmount = ZERO;
315
+ } else if (isVariant(order.direction, 'short') && position.baseAssetAmount.lte(ZERO)) {
316
+ baseAssetAmount = ZERO;
317
+ } else {
318
+ BN.min(baseAssetAmount, position.baseAssetAmount.abs());
319
+ }
320
+ }
321
+
322
+ return baseAssetAmount;
309
323
  }