@drift-labs/sdk 2.13.0-beta.0 → 2.13.0-beta.1

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.
@@ -165,12 +165,12 @@ export function isFillableByVAMM(
165
165
  ): boolean {
166
166
  return (
167
167
  (isAuctionComplete(order, slot) &&
168
- !calculateBaseAssetAmountForAmmToFulfill(
168
+ calculateBaseAssetAmountForAmmToFulfill(
169
169
  order,
170
170
  market,
171
171
  oraclePriceData,
172
172
  slot
173
- ).eq(ZERO)) ||
173
+ ).gte(market.amm.minOrderSize)) ||
174
174
  isOrderExpired(order, ts)
175
175
  );
176
176
  }
@@ -67,6 +67,28 @@ export function getSignedTokenAmount(
67
67
  }
68
68
  }
69
69
 
70
+ export function getStrictTokenValue(
71
+ tokenAmount: BN,
72
+ spotDecimals: number,
73
+ oraclePriceData: OraclePriceData,
74
+ oraclePriceTwap: BN
75
+ ): BN {
76
+ if (tokenAmount.eq(ZERO)) {
77
+ return ZERO;
78
+ }
79
+
80
+ let price = oraclePriceData.price;
81
+ if (tokenAmount.gt(ZERO)) {
82
+ price = BN.min(oraclePriceData.price, oraclePriceTwap);
83
+ } else {
84
+ price = BN.max(oraclePriceData.price, oraclePriceTwap);
85
+ }
86
+
87
+ const precisionDecrease = TEN.pow(new BN(spotDecimals));
88
+
89
+ return tokenAmount.mul(price).div(precisionDecrease);
90
+ }
91
+
70
92
  export function getTokenValue(
71
93
  tokenAmount: BN,
72
94
  spotDecimals: number,
@@ -268,7 +290,14 @@ export function calculateInterestAccumulated(
268
290
  export function calculateWithdrawLimit(
269
291
  spotMarket: SpotMarketAccount,
270
292
  now: BN
271
- ): { borrowLimit: BN; withdrawLimit: BN } {
293
+ ): {
294
+ borrowLimit: BN;
295
+ withdrawLimit: BN;
296
+ minDepositAmount: BN;
297
+ maxBorrowAmount: BN;
298
+ currentDepositAmount;
299
+ currentBorrowAmount;
300
+ } {
272
301
  const marketDepositTokenAmount = getTokenAmount(
273
302
  spotMarket.depositBalance,
274
303
  spotMarket,
@@ -311,8 +340,27 @@ export function calculateWithdrawLimit(
311
340
  )
312
341
  );
313
342
 
343
+ let withdrawLimit = BN.max(
344
+ marketDepositTokenAmount.sub(minDepositTokens),
345
+ ZERO
346
+ );
347
+
348
+ let borrowLimit = BN.max(maxBorrowTokens.sub(marketBorrowTokenAmount), ZERO);
349
+
350
+ if (borrowLimit.eq(ZERO)) {
351
+ withdrawLimit = ZERO;
352
+ }
353
+
354
+ if (withdrawLimit.eq(ZERO)) {
355
+ borrowLimit = ZERO;
356
+ }
357
+
314
358
  return {
315
- borrowLimit: maxBorrowTokens.sub(marketBorrowTokenAmount),
316
- withdrawLimit: marketDepositTokenAmount.sub(minDepositTokens),
359
+ borrowLimit,
360
+ withdrawLimit,
361
+ maxBorrowAmount: maxBorrowTokens,
362
+ minDepositAmount: minDepositTokens,
363
+ currentDepositAmount: marketDepositTokenAmount,
364
+ currentBorrowAmount: marketBorrowTokenAmount,
317
365
  };
318
366
  }
package/src/math/trade.ts CHANGED
@@ -529,7 +529,11 @@ export function calculateEstimatedPerpEntryPrice(
529
529
  }
530
530
  }
531
531
 
532
- if (limitOrder && usersToSkip.has(limitOrder.userAccount)) {
532
+ if (!limitOrder) {
533
+ continue;
534
+ }
535
+
536
+ if (usersToSkip.has(limitOrder.userAccount)) {
533
537
  continue;
534
538
  }
535
539
 
@@ -615,7 +619,11 @@ export function calculateEstimatedPerpEntryPrice(
615
619
  }
616
620
  }
617
621
 
618
- if (limitOrder && usersToSkip.has(limitOrder.userAccount)) {
622
+ if (!limitOrder) {
623
+ continue;
624
+ }
625
+
626
+ if (usersToSkip.has(limitOrder.userAccount)) {
619
627
  continue;
620
628
  }
621
629