@drift-labs/sdk 2.63.0-beta.1 → 2.63.0-beta.3
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/driftClient.js +12 -10
- package/lib/math/exchangeStatus.d.ts +1 -0
- package/lib/math/exchangeStatus.js +44 -5
- package/package.json +1 -1
- package/src/driftClient.ts +14 -12
- 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.3
|
package/lib/driftClient.js
CHANGED
|
@@ -304,7 +304,7 @@ class DriftClient {
|
|
|
304
304
|
* @param includeDelegates
|
|
305
305
|
*/
|
|
306
306
|
async updateWallet(newWallet, subAccountIds, activeSubAccountId, includeDelegates, authoritySubaccountMap) {
|
|
307
|
-
var _a, _b;
|
|
307
|
+
var _a, _b, _c;
|
|
308
308
|
const newProvider = new anchor_1.AnchorProvider(this.connection,
|
|
309
309
|
// @ts-ignore
|
|
310
310
|
newWallet, this.opts);
|
|
@@ -337,18 +337,20 @@ class DriftClient {
|
|
|
337
337
|
: subAccountIds
|
|
338
338
|
? new Map([[this.authority.toString(), subAccountIds]])
|
|
339
339
|
: new Map();
|
|
340
|
+
/* Reset user stats account */
|
|
341
|
+
if ((_c = this.userStats) === null || _c === void 0 ? void 0 : _c.isSubscribed) {
|
|
342
|
+
await this.userStats.unsubscribe();
|
|
343
|
+
}
|
|
344
|
+
this.userStats = undefined;
|
|
345
|
+
this.userStats = new userStats_1.UserStats({
|
|
346
|
+
driftClient: this,
|
|
347
|
+
userStatsAccountPublicKey: this.getUserStatsAccountPublicKey(),
|
|
348
|
+
accountSubscription: this.userStatsAccountSubscriptionConfig,
|
|
349
|
+
});
|
|
350
|
+
await this.userStats.subscribe();
|
|
340
351
|
let success = true;
|
|
341
352
|
if (this.isSubscribed) {
|
|
342
353
|
await Promise.all(this.unsubscribeUsers());
|
|
343
|
-
if (this.userStats) {
|
|
344
|
-
await this.userStats.unsubscribe();
|
|
345
|
-
this.userStats = new userStats_1.UserStats({
|
|
346
|
-
driftClient: this,
|
|
347
|
-
userStatsAccountPublicKey: this.getUserStatsAccountPublicKey(),
|
|
348
|
-
accountSubscription: this.userStatsAccountSubscriptionConfig,
|
|
349
|
-
});
|
|
350
|
-
await this.userStats.subscribe();
|
|
351
|
-
}
|
|
352
354
|
this.users.clear();
|
|
353
355
|
success = await this.addAndSubscribeToUsers();
|
|
354
356
|
}
|
|
@@ -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/driftClient.ts
CHANGED
|
@@ -586,23 +586,25 @@ export class DriftClient {
|
|
|
586
586
|
? new Map([[this.authority.toString(), subAccountIds]])
|
|
587
587
|
: new Map<string, number[]>();
|
|
588
588
|
|
|
589
|
-
|
|
589
|
+
/* Reset user stats account */
|
|
590
|
+
if (this.userStats?.isSubscribed) {
|
|
591
|
+
await this.userStats.unsubscribe();
|
|
592
|
+
}
|
|
590
593
|
|
|
591
|
-
|
|
592
|
-
await Promise.all(this.unsubscribeUsers());
|
|
594
|
+
this.userStats = undefined;
|
|
593
595
|
|
|
594
|
-
|
|
595
|
-
|
|
596
|
+
this.userStats = new UserStats({
|
|
597
|
+
driftClient: this,
|
|
598
|
+
userStatsAccountPublicKey: this.getUserStatsAccountPublicKey(),
|
|
599
|
+
accountSubscription: this.userStatsAccountSubscriptionConfig,
|
|
600
|
+
});
|
|
596
601
|
|
|
597
|
-
|
|
598
|
-
driftClient: this,
|
|
599
|
-
userStatsAccountPublicKey: this.getUserStatsAccountPublicKey(),
|
|
600
|
-
accountSubscription: this.userStatsAccountSubscriptionConfig,
|
|
601
|
-
});
|
|
602
|
+
await this.userStats.subscribe();
|
|
602
603
|
|
|
603
|
-
|
|
604
|
-
}
|
|
604
|
+
let success = true;
|
|
605
605
|
|
|
606
|
+
if (this.isSubscribed) {
|
|
607
|
+
await Promise.all(this.unsubscribeUsers());
|
|
606
608
|
this.users.clear();
|
|
607
609
|
success = await this.addAndSubscribeToUsers();
|
|
608
610
|
}
|
|
@@ -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
|
|