@drift-labs/sdk 2.63.0-beta.0 → 2.63.0-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 +1 -1
- package/lib/math/amm.js +5 -1
- package/lib/math/exchangeStatus.d.ts +1 -0
- package/lib/math/exchangeStatus.js +44 -5
- package/package.json +1 -1
- package/src/math/amm.ts +13 -1
- package/src/math/exchangeStatus.ts +68 -3
- package/tests/dlob/helpers.ts +6 -0
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.63.0-beta.
|
|
1
|
+
2.63.0-beta.2
|
package/lib/math/amm.js
CHANGED
|
@@ -339,7 +339,11 @@ function calculateSpreadBN(baseSpread, lastOracleReservePriceSpreadPct, lastOrac
|
|
|
339
339
|
}
|
|
340
340
|
spreadTerms.longSpreadwPS = longSpread;
|
|
341
341
|
spreadTerms.shortSpreadwPS = shortSpread;
|
|
342
|
-
const
|
|
342
|
+
const maxSpreadBaseline = Math.min(Math.max(lastOracleReservePriceSpreadPct.abs().toNumber(), lastOracleConfPct.muln(2).toNumber(), anchor_1.BN.max(markStd, oracleStd)
|
|
343
|
+
.mul(numericConstants_1.PERCENTAGE_PRECISION)
|
|
344
|
+
.div(reservePrice)
|
|
345
|
+
.toNumber()), numericConstants_1.BID_ASK_SPREAD_PRECISION.toNumber());
|
|
346
|
+
const maxTargetSpread = Math.floor(Math.max(maxSpread, maxSpreadBaseline));
|
|
343
347
|
const inventorySpreadScale = calculateInventoryScale(baseAssetAmountWithAmm, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve, baseAssetAmountWithAmm.gt(numericConstants_1.ZERO) ? longSpread : shortSpread, maxTargetSpread);
|
|
344
348
|
if (baseAssetAmountWithAmm.gt(numericConstants_1.ZERO)) {
|
|
345
349
|
longSpread *= inventorySpreadScale;
|
|
@@ -3,3 +3,4 @@ export declare function exchangePaused(state: StateAccount): boolean;
|
|
|
3
3
|
export declare function fillPaused(state: StateAccount, market: PerpMarketAccount | SpotMarketAccount): boolean;
|
|
4
4
|
export declare function ammPaused(state: StateAccount, market: PerpMarketAccount | SpotMarketAccount): boolean;
|
|
5
5
|
export declare function isOperationPaused(pausedOperations: number, operation: PerpOperation | SpotOperation): boolean;
|
|
6
|
+
export declare function isAmmDrawdownPause(market: PerpMarketAccount): boolean;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isOperationPaused = exports.ammPaused = exports.fillPaused = exports.exchangePaused = void 0;
|
|
3
|
+
exports.isAmmDrawdownPause = exports.isOperationPaused = exports.ammPaused = exports.fillPaused = exports.exchangePaused = void 0;
|
|
4
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
4
5
|
const types_1 = require("../types");
|
|
6
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
5
7
|
function exchangePaused(state) {
|
|
6
8
|
return state.exchangeStatus !== types_1.ExchangeStatus.ACTIVE;
|
|
7
9
|
}
|
|
@@ -25,14 +27,51 @@ function ammPaused(state, market) {
|
|
|
25
27
|
return true;
|
|
26
28
|
}
|
|
27
29
|
if (market.hasOwnProperty('amm')) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const operationPaused = isOperationPaused(market.pausedOperations, types_1.PerpOperation.AMM_FILL);
|
|
31
|
+
if (operationPaused) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
if (isAmmDrawdownPause(market)) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
32
37
|
}
|
|
38
|
+
return false;
|
|
33
39
|
}
|
|
34
40
|
exports.ammPaused = ammPaused;
|
|
35
41
|
function isOperationPaused(pausedOperations, operation) {
|
|
36
42
|
return (pausedOperations & operation) > 0;
|
|
37
43
|
}
|
|
38
44
|
exports.isOperationPaused = isOperationPaused;
|
|
45
|
+
function isAmmDrawdownPause(market) {
|
|
46
|
+
let quoteDrawdownLimitBreached;
|
|
47
|
+
if ((0, types_1.isVariant)(market.contractTier, 'a') ||
|
|
48
|
+
(0, types_1.isVariant)(market.contractTier, 'b')) {
|
|
49
|
+
quoteDrawdownLimitBreached = market.amm.netRevenueSinceLastFunding.lte(numericConstants_1.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT.muln(400));
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
quoteDrawdownLimitBreached = market.amm.netRevenueSinceLastFunding.lte(numericConstants_1.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT.muln(200));
|
|
53
|
+
}
|
|
54
|
+
if (quoteDrawdownLimitBreached) {
|
|
55
|
+
const percentDrawdown = market.amm.netRevenueSinceLastFunding
|
|
56
|
+
.mul(numericConstants_1.PERCENTAGE_PRECISION)
|
|
57
|
+
.div(anchor_1.BN.max(market.amm.totalFeeMinusDistributions, numericConstants_1.ONE));
|
|
58
|
+
let percentDrawdownLimitBreached;
|
|
59
|
+
if ((0, types_1.isVariant)(market.contractTier, 'a')) {
|
|
60
|
+
percentDrawdownLimitBreached = percentDrawdown.lte(numericConstants_1.PERCENTAGE_PRECISION.divn(50).neg());
|
|
61
|
+
}
|
|
62
|
+
else if ((0, types_1.isVariant)(market.contractTier, 'b')) {
|
|
63
|
+
percentDrawdownLimitBreached = percentDrawdown.lte(numericConstants_1.PERCENTAGE_PRECISION.divn(33).neg());
|
|
64
|
+
}
|
|
65
|
+
else if ((0, types_1.isVariant)(market.contractTier, 'c')) {
|
|
66
|
+
percentDrawdownLimitBreached = percentDrawdown.lte(numericConstants_1.PERCENTAGE_PRECISION.divn(25).neg());
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
percentDrawdownLimitBreached = percentDrawdown.lte(numericConstants_1.PERCENTAGE_PRECISION.divn(20).neg());
|
|
70
|
+
}
|
|
71
|
+
if (percentDrawdownLimitBreached) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
exports.isAmmDrawdownPause = isAmmDrawdownPause;
|
package/package.json
CHANGED
package/src/math/amm.ts
CHANGED
|
@@ -658,8 +658,20 @@ export function calculateSpreadBN(
|
|
|
658
658
|
spreadTerms.longSpreadwPS = longSpread;
|
|
659
659
|
spreadTerms.shortSpreadwPS = shortSpread;
|
|
660
660
|
|
|
661
|
+
const maxSpreadBaseline = Math.min(
|
|
662
|
+
Math.max(
|
|
663
|
+
lastOracleReservePriceSpreadPct.abs().toNumber(),
|
|
664
|
+
lastOracleConfPct.muln(2).toNumber(),
|
|
665
|
+
BN.max(markStd, oracleStd)
|
|
666
|
+
.mul(PERCENTAGE_PRECISION)
|
|
667
|
+
.div(reservePrice)
|
|
668
|
+
.toNumber()
|
|
669
|
+
),
|
|
670
|
+
BID_ASK_SPREAD_PRECISION.toNumber()
|
|
671
|
+
);
|
|
672
|
+
|
|
661
673
|
const maxTargetSpread: number = Math.floor(
|
|
662
|
-
Math.max(maxSpread,
|
|
674
|
+
Math.max(maxSpread, maxSpreadBaseline)
|
|
663
675
|
);
|
|
664
676
|
|
|
665
677
|
const inventorySpreadScale = calculateInventoryScale(
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT,
|
|
3
|
+
PERCENTAGE_PRECISION,
|
|
4
|
+
ONE,
|
|
5
|
+
} from '../constants/numericConstants';
|
|
1
6
|
import {
|
|
2
7
|
ExchangeStatus,
|
|
3
8
|
PerpMarketAccount,
|
|
@@ -5,7 +10,9 @@ import {
|
|
|
5
10
|
SpotMarketAccount,
|
|
6
11
|
SpotOperation,
|
|
7
12
|
StateAccount,
|
|
13
|
+
isVariant,
|
|
8
14
|
} from '../types';
|
|
15
|
+
import { BN } from '@coral-xyz/anchor';
|
|
9
16
|
|
|
10
17
|
export function exchangePaused(state: StateAccount): boolean {
|
|
11
18
|
return state.exchangeStatus !== ExchangeStatus.ACTIVE;
|
|
@@ -41,10 +48,19 @@ export function ammPaused(
|
|
|
41
48
|
}
|
|
42
49
|
|
|
43
50
|
if (market.hasOwnProperty('amm')) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
const operationPaused = isOperationPaused(
|
|
52
|
+
market.pausedOperations,
|
|
53
|
+
PerpOperation.AMM_FILL
|
|
54
|
+
);
|
|
55
|
+
if (operationPaused) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
if (isAmmDrawdownPause(market as PerpMarketAccount)) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
47
61
|
}
|
|
62
|
+
|
|
63
|
+
return false;
|
|
48
64
|
}
|
|
49
65
|
|
|
50
66
|
export function isOperationPaused(
|
|
@@ -53,3 +69,52 @@ export function isOperationPaused(
|
|
|
53
69
|
): boolean {
|
|
54
70
|
return (pausedOperations & operation) > 0;
|
|
55
71
|
}
|
|
72
|
+
|
|
73
|
+
export function isAmmDrawdownPause(market: PerpMarketAccount): boolean {
|
|
74
|
+
let quoteDrawdownLimitBreached: boolean;
|
|
75
|
+
|
|
76
|
+
if (
|
|
77
|
+
isVariant(market.contractTier, 'a') ||
|
|
78
|
+
isVariant(market.contractTier, 'b')
|
|
79
|
+
) {
|
|
80
|
+
quoteDrawdownLimitBreached = market.amm.netRevenueSinceLastFunding.lte(
|
|
81
|
+
DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT.muln(400)
|
|
82
|
+
);
|
|
83
|
+
} else {
|
|
84
|
+
quoteDrawdownLimitBreached = market.amm.netRevenueSinceLastFunding.lte(
|
|
85
|
+
DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT.muln(200)
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (quoteDrawdownLimitBreached) {
|
|
90
|
+
const percentDrawdown = market.amm.netRevenueSinceLastFunding
|
|
91
|
+
.mul(PERCENTAGE_PRECISION)
|
|
92
|
+
.div(BN.max(market.amm.totalFeeMinusDistributions, ONE));
|
|
93
|
+
|
|
94
|
+
let percentDrawdownLimitBreached: boolean;
|
|
95
|
+
|
|
96
|
+
if (isVariant(market.contractTier, 'a')) {
|
|
97
|
+
percentDrawdownLimitBreached = percentDrawdown.lte(
|
|
98
|
+
PERCENTAGE_PRECISION.divn(50).neg()
|
|
99
|
+
);
|
|
100
|
+
} else if (isVariant(market.contractTier, 'b')) {
|
|
101
|
+
percentDrawdownLimitBreached = percentDrawdown.lte(
|
|
102
|
+
PERCENTAGE_PRECISION.divn(33).neg()
|
|
103
|
+
);
|
|
104
|
+
} else if (isVariant(market.contractTier, 'c')) {
|
|
105
|
+
percentDrawdownLimitBreached = percentDrawdown.lte(
|
|
106
|
+
PERCENTAGE_PRECISION.divn(25).neg()
|
|
107
|
+
);
|
|
108
|
+
} else {
|
|
109
|
+
percentDrawdownLimitBreached = percentDrawdown.lte(
|
|
110
|
+
PERCENTAGE_PRECISION.divn(20).neg()
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (percentDrawdownLimitBreached) {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return false;
|
|
120
|
+
}
|
package/tests/dlob/helpers.ts
CHANGED
|
@@ -181,6 +181,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
|
|
|
181
181
|
},
|
|
182
182
|
quoteSpotMarketIndex: 0,
|
|
183
183
|
feeAdjustment: 0,
|
|
184
|
+
pausedOperations: 0,
|
|
184
185
|
},
|
|
185
186
|
{
|
|
186
187
|
status: MarketStatus.INITIALIZED,
|
|
@@ -219,6 +220,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
|
|
|
219
220
|
},
|
|
220
221
|
quoteSpotMarketIndex: 0,
|
|
221
222
|
feeAdjustment: 0,
|
|
223
|
+
pausedOperations: 0,
|
|
222
224
|
},
|
|
223
225
|
{
|
|
224
226
|
status: MarketStatus.INITIALIZED,
|
|
@@ -257,6 +259,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
|
|
|
257
259
|
},
|
|
258
260
|
quoteSpotMarketIndex: 0,
|
|
259
261
|
feeAdjustment: 0,
|
|
262
|
+
pausedOperations: 0,
|
|
260
263
|
},
|
|
261
264
|
];
|
|
262
265
|
|
|
@@ -341,6 +344,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
341
344
|
lastIndexPriceTwap5Min: PRICE_PRECISION,
|
|
342
345
|
lastIndexPriceTwapTs: new BN(0),
|
|
343
346
|
},
|
|
347
|
+
pausedOperations: 0,
|
|
344
348
|
},
|
|
345
349
|
{
|
|
346
350
|
status: MarketStatus.ACTIVE,
|
|
@@ -422,6 +426,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
422
426
|
lastIndexPriceTwap5Min: new BN(0),
|
|
423
427
|
lastIndexPriceTwapTs: new BN(0),
|
|
424
428
|
},
|
|
429
|
+
pausedOperations: 0,
|
|
425
430
|
},
|
|
426
431
|
{
|
|
427
432
|
status: MarketStatus.ACTIVE,
|
|
@@ -503,6 +508,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
503
508
|
lastIndexPriceTwap5Min: new BN(0),
|
|
504
509
|
lastIndexPriceTwapTs: new BN(0),
|
|
505
510
|
},
|
|
511
|
+
pausedOperations: 0,
|
|
506
512
|
},
|
|
507
513
|
];
|
|
508
514
|
|