@drift-labs/sdk 2.1.0 → 2.1.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/math/amm.js +4 -4
- package/package.json +1 -1
- package/src/math/amm.ts +8 -4
- package/tests/amm/test.ts +35 -0
package/lib/math/amm.js
CHANGED
|
@@ -213,7 +213,7 @@ function calculateInventoryScale(baseAssetAmountWithAmm, baseAssetReserve, minBa
|
|
|
213
213
|
}
|
|
214
214
|
exports.calculateInventoryScale = calculateInventoryScale;
|
|
215
215
|
function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, reservePrice, totalFeeMinusDistributions) {
|
|
216
|
-
//
|
|
216
|
+
// vAMM skew
|
|
217
217
|
const netBaseAssetValue = quoteAssetReserve
|
|
218
218
|
.sub(terminalQuoteAssetReserve)
|
|
219
219
|
.mul(pegMultiplier)
|
|
@@ -221,8 +221,8 @@ function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuote
|
|
|
221
221
|
const localBaseAssetValue = netBaseAssetAmount
|
|
222
222
|
.mul(reservePrice)
|
|
223
223
|
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.PRICE_PRECISION));
|
|
224
|
-
const
|
|
225
|
-
|
|
224
|
+
const effectiveGap = Math.max(0, localBaseAssetValue.sub(netBaseAssetValue).toNumber());
|
|
225
|
+
const effectiveLeverage = effectiveGap / (Math.max(0, totalFeeMinusDistributions.toNumber()) + 1) +
|
|
226
226
|
1 / numericConstants_1.QUOTE_PRECISION.toNumber();
|
|
227
227
|
return effectiveLeverage;
|
|
228
228
|
}
|
|
@@ -309,7 +309,7 @@ function calculateSpreadBN(baseSpread, lastOracleReservePriceSpreadPct, lastOrac
|
|
|
309
309
|
spreadTerms.effectiveLeverageCapped = spreadScale;
|
|
310
310
|
if (baseAssetAmountWithAmm.gt(numericConstants_1.ZERO)) {
|
|
311
311
|
longSpread *= spreadScale;
|
|
312
|
-
|
|
312
|
+
longSpread = Math.floor(longSpread);
|
|
313
313
|
}
|
|
314
314
|
else {
|
|
315
315
|
shortSpread *= spreadScale;
|
package/package.json
CHANGED
package/src/math/amm.ts
CHANGED
|
@@ -392,7 +392,7 @@ export function calculateEffectiveLeverage(
|
|
|
392
392
|
reservePrice: BN,
|
|
393
393
|
totalFeeMinusDistributions: BN
|
|
394
394
|
): number {
|
|
395
|
-
//
|
|
395
|
+
// vAMM skew
|
|
396
396
|
const netBaseAssetValue = quoteAssetReserve
|
|
397
397
|
.sub(terminalQuoteAssetReserve)
|
|
398
398
|
.mul(pegMultiplier)
|
|
@@ -402,9 +402,13 @@ export function calculateEffectiveLeverage(
|
|
|
402
402
|
.mul(reservePrice)
|
|
403
403
|
.div(AMM_TO_QUOTE_PRECISION_RATIO.mul(PRICE_PRECISION));
|
|
404
404
|
|
|
405
|
+
const effectiveGap = Math.max(
|
|
406
|
+
0,
|
|
407
|
+
localBaseAssetValue.sub(netBaseAssetValue).toNumber()
|
|
408
|
+
);
|
|
409
|
+
|
|
405
410
|
const effectiveLeverage =
|
|
406
|
-
|
|
407
|
-
(Math.max(0, totalFeeMinusDistributions.toNumber()) + 1) +
|
|
411
|
+
effectiveGap / (Math.max(0, totalFeeMinusDistributions.toNumber()) + 1) +
|
|
408
412
|
1 / QUOTE_PRECISION.toNumber();
|
|
409
413
|
|
|
410
414
|
return effectiveLeverage;
|
|
@@ -580,7 +584,7 @@ export function calculateSpreadBN(
|
|
|
580
584
|
|
|
581
585
|
if (baseAssetAmountWithAmm.gt(ZERO)) {
|
|
582
586
|
longSpread *= spreadScale;
|
|
583
|
-
|
|
587
|
+
longSpread = Math.floor(longSpread);
|
|
584
588
|
} else {
|
|
585
589
|
shortSpread *= spreadScale;
|
|
586
590
|
shortSpread = Math.floor(shortSpread);
|
package/tests/amm/test.ts
CHANGED
|
@@ -149,6 +149,41 @@ describe('AMM Tests', () => {
|
|
|
149
149
|
|
|
150
150
|
});
|
|
151
151
|
|
|
152
|
+
it('Corner Case Spreads', () => {
|
|
153
|
+
|
|
154
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
155
|
+
// @ts-ignore
|
|
156
|
+
const terms2: AMMSpreadTerms = calculateSpreadBN(
|
|
157
|
+
1000,
|
|
158
|
+
new BN(5555),
|
|
159
|
+
new BN(1131),
|
|
160
|
+
20000,
|
|
161
|
+
new BN(1009967115003047),
|
|
162
|
+
new BN(1009811402660255),
|
|
163
|
+
new BN(13460124),
|
|
164
|
+
new BN(15328930153),
|
|
165
|
+
new BN(13667686),
|
|
166
|
+
new BN(1235066973),
|
|
167
|
+
new BN(88540713),
|
|
168
|
+
new BN(994097717724176),
|
|
169
|
+
new BN(974077854655784),
|
|
170
|
+
new BN(1014841945381208),
|
|
171
|
+
new BN(103320),
|
|
172
|
+
new BN(59975),
|
|
173
|
+
new BN(768323534),
|
|
174
|
+
new BN(243875031),
|
|
175
|
+
new BN(130017761029),
|
|
176
|
+
true
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
console.log(terms2);
|
|
180
|
+
assert(terms2.effectiveLeverageCapped<=1.000001);
|
|
181
|
+
assert(terms2.inventorySpreadScale==1.117371);
|
|
182
|
+
assert(terms2.longSpread==1263);
|
|
183
|
+
assert(terms2.shortSpread==6686);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
|
|
152
187
|
it('live update functions', () => {
|
|
153
188
|
|
|
154
189
|
const mockAmm = mockPerpMarkets[0].amm;
|