@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.
- package/lib/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +3 -1
- package/lib/driftClient.d.ts +9 -0
- package/lib/driftClient.js +49 -0
- package/lib/idl/drift.json +1 -1
- package/lib/math/market.d.ts +5 -0
- package/lib/math/market.js +18 -1
- package/lib/math/oracles.d.ts +2 -2
- package/lib/math/oracles.js +20 -8
- package/lib/math/orders.js +1 -1
- package/lib/math/spotBalance.d.ts +5 -0
- package/lib/math/spotBalance.js +30 -3
- package/lib/math/trade.js +8 -2
- package/lib/user.d.ts +12 -6
- package/lib/user.js +124 -72
- package/package.json +1 -1
- package/src/constants/numericConstants.ts +2 -0
- package/src/driftClient.ts +64 -0
- package/src/idl/drift.json +1 -1
- package/src/math/market.ts +51 -0
- package/src/math/oracles.ts +30 -16
- package/src/math/orders.ts +2 -2
- package/src/math/spotBalance.ts +51 -3
- package/src/math/trade.ts +10 -2
- package/src/user.ts +267 -138
- package/tests/amm/test.ts +3 -2
package/src/math/orders.ts
CHANGED
|
@@ -165,12 +165,12 @@ export function isFillableByVAMM(
|
|
|
165
165
|
): boolean {
|
|
166
166
|
return (
|
|
167
167
|
(isAuctionComplete(order, slot) &&
|
|
168
|
-
|
|
168
|
+
calculateBaseAssetAmountForAmmToFulfill(
|
|
169
169
|
order,
|
|
170
170
|
market,
|
|
171
171
|
oraclePriceData,
|
|
172
172
|
slot
|
|
173
|
-
).
|
|
173
|
+
).gte(market.amm.minOrderSize)) ||
|
|
174
174
|
isOrderExpired(order, ts)
|
|
175
175
|
);
|
|
176
176
|
}
|
package/src/math/spotBalance.ts
CHANGED
|
@@ -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
|
-
): {
|
|
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
|
|
316
|
-
withdrawLimit
|
|
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
|
|
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
|
|
622
|
+
if (!limitOrder) {
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
if (usersToSkip.has(limitOrder.userAccount)) {
|
|
619
627
|
continue;
|
|
620
628
|
}
|
|
621
629
|
|