@drift-labs/sdk 2.18.0 → 2.19.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/accounts/bulkAccountLoader.d.ts +2 -1
- package/lib/accounts/bulkAccountLoader.js +3 -3
- package/lib/accounts/fetch.js +2 -2
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +7 -7
- package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +2 -2
- package/lib/accounts/types.d.ts +5 -4
- package/lib/accounts/webSocketAccountSubscriber.js +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +3 -3
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.js +5 -1
- package/lib/adminClient.js +61 -57
- package/lib/config.d.ts +2 -2
- package/lib/constants/perpMarkets.d.ts +1 -1
- package/lib/constants/perpMarkets.js +33 -3
- package/lib/constants/spotMarkets.d.ts +1 -1
- package/lib/dlob/DLOB.d.ts +4 -4
- package/lib/dlob/DLOB.js +89 -76
- package/lib/dlob/DLOBNode.d.ts +2 -2
- package/lib/dlob/DLOBNode.js +7 -7
- package/lib/dlob/DLOBOrders.d.ts +2 -2
- package/lib/dlob/NodeList.d.ts +2 -2
- package/lib/dlob/NodeList.js +4 -4
- package/lib/driftClient.d.ts +1 -1
- package/lib/driftClient.js +99 -95
- package/lib/driftClientConfig.d.ts +3 -3
- package/lib/events/eventSubscriber.js +2 -2
- package/lib/events/fetchLogs.d.ts +2 -2
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/events/types.d.ts +14 -14
- package/lib/examples/loadDlob.js +2 -2
- package/lib/examples/makeTradeExample.js +9 -9
- package/lib/factory/bigNum.js +13 -13
- package/lib/factory/oracleClient.js +6 -3
- package/lib/idl/drift.json +7 -4
- package/lib/index.js +5 -1
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.js +33 -38
- package/lib/math/auction.js +6 -6
- package/lib/math/exchangeStatus.js +2 -2
- package/lib/math/funding.js +2 -2
- package/lib/math/margin.js +4 -4
- package/lib/math/market.js +15 -15
- package/lib/math/oracles.js +1 -1
- package/lib/math/orders.js +24 -24
- package/lib/math/position.js +5 -5
- package/lib/math/repeg.js +1 -1
- package/lib/math/spotBalance.js +9 -9
- package/lib/math/spotMarket.js +3 -3
- package/lib/math/spotPosition.js +3 -3
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +43 -43
- package/lib/math/utils.js +1 -1
- package/lib/oracles/oracleClientCache.js +1 -1
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/types.d.ts +2 -2
- package/lib/serum/serumSubscriber.d.ts +4 -3
- package/lib/serum/serumSubscriber.js +40 -11
- package/lib/serum/types.d.ts +3 -1
- package/lib/slot/SlotSubscriber.d.ts +1 -1
- package/lib/tokenFaucet.js +5 -1
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +1 -1
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +49 -46
- package/lib/types.js +2 -1
- package/lib/user.js +63 -63
- package/lib/userConfig.d.ts +2 -2
- package/lib/userMap/userMap.js +1 -1
- package/lib/userMap/userStatsMap.js +3 -3
- package/lib/userStats.js +2 -2
- package/lib/userStatsConfig.d.ts +2 -2
- package/package.json +1 -1
- package/src/constants/perpMarkets.ts +33 -3
- package/src/dlob/DLOB.ts +17 -5
- package/src/dlob/NodeList.ts +2 -5
- package/src/factory/oracleClient.ts +5 -1
- package/src/idl/drift.json +7 -4
- package/src/math/amm.ts +22 -23
- package/src/math/trade.ts +1 -1
- package/src/serum/serumSubscriber.ts +62 -20
- package/src/serum/types.ts +9 -5
- package/src/types.ts +2 -1
- package/tests/amm/test.ts +177 -5
- package/tests/dlob/test.ts +2 -1
package/lib/dlob/DLOB.js
CHANGED
|
@@ -80,10 +80,10 @@ class DLOB {
|
|
|
80
80
|
this.insertOrder(record.order, record.user, slot);
|
|
81
81
|
}
|
|
82
82
|
handleOrderActionRecord(record, slot) {
|
|
83
|
-
if (__1.isOneOfVariant(record.action, ['place', 'expire'])) {
|
|
83
|
+
if ((0, __1.isOneOfVariant)(record.action, ['place', 'expire'])) {
|
|
84
84
|
return;
|
|
85
85
|
}
|
|
86
|
-
if (__1.isVariant(record.action, 'trigger')) {
|
|
86
|
+
if ((0, __1.isVariant)(record.action, 'trigger')) {
|
|
87
87
|
if (record.taker !== null) {
|
|
88
88
|
const takerOrder = this.getOrder(record.takerOrderId, record.taker);
|
|
89
89
|
if (takerOrder) {
|
|
@@ -97,7 +97,7 @@ class DLOB {
|
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
else if (__1.isVariant(record.action, 'fill')) {
|
|
100
|
+
else if ((0, __1.isVariant)(record.action, 'fill')) {
|
|
101
101
|
if (record.taker !== null) {
|
|
102
102
|
const takerOrder = this.getOrder(record.takerOrderId, record.taker);
|
|
103
103
|
if (takerOrder) {
|
|
@@ -111,7 +111,7 @@ class DLOB {
|
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
|
-
else if (__1.isVariant(record.action, 'cancel')) {
|
|
114
|
+
else if ((0, __1.isVariant)(record.action, 'cancel')) {
|
|
115
115
|
if (record.taker !== null) {
|
|
116
116
|
const takerOrder = this.getOrder(record.takerOrderId, record.taker);
|
|
117
117
|
if (takerOrder) {
|
|
@@ -128,20 +128,20 @@ class DLOB {
|
|
|
128
128
|
}
|
|
129
129
|
insertOrder(order, userAccount, slot, onInsert) {
|
|
130
130
|
var _a;
|
|
131
|
-
if (__1.isVariant(order.status, 'init')) {
|
|
131
|
+
if ((0, __1.isVariant)(order.status, 'init')) {
|
|
132
132
|
return;
|
|
133
133
|
}
|
|
134
|
-
if (!__1.isOneOfVariant(order.orderType, SUPPORTED_ORDER_TYPES)) {
|
|
134
|
+
if (!(0, __1.isOneOfVariant)(order.orderType, SUPPORTED_ORDER_TYPES)) {
|
|
135
135
|
return;
|
|
136
136
|
}
|
|
137
|
-
const marketType = __1.getVariant(order.marketType);
|
|
137
|
+
const marketType = (0, __1.getVariant)(order.marketType);
|
|
138
138
|
if (!this.orderLists.get(marketType).has(order.marketIndex)) {
|
|
139
139
|
this.addOrderList(marketType, order.marketIndex);
|
|
140
140
|
}
|
|
141
|
-
if (__1.isVariant(order.status, 'open')) {
|
|
141
|
+
if ((0, __1.isVariant)(order.status, 'open')) {
|
|
142
142
|
this.openOrders
|
|
143
143
|
.get(marketType)
|
|
144
|
-
.add(NodeList_1.getOrderSignature(order.orderId, userAccount));
|
|
144
|
+
.add((0, NodeList_1.getOrderSignature)(order.orderId, userAccount));
|
|
145
145
|
}
|
|
146
146
|
(_a = this.getListForOrder(order, slot)) === null || _a === void 0 ? void 0 : _a.insert(order, marketType, userAccount);
|
|
147
147
|
if (onInsert) {
|
|
@@ -193,16 +193,16 @@ class DLOB {
|
|
|
193
193
|
}
|
|
194
194
|
trigger(order, userAccount, slot, onTrigger) {
|
|
195
195
|
var _a;
|
|
196
|
-
if (__1.isVariant(order.status, 'init')) {
|
|
196
|
+
if ((0, __1.isVariant)(order.status, 'init')) {
|
|
197
197
|
return;
|
|
198
198
|
}
|
|
199
199
|
this.updateRestingLimitOrders(slot);
|
|
200
|
-
if (__1.isTriggered(order)) {
|
|
200
|
+
if ((0, __1.isTriggered)(order)) {
|
|
201
201
|
return;
|
|
202
202
|
}
|
|
203
|
-
const marketType = __1.getVariant(order.marketType);
|
|
203
|
+
const marketType = (0, __1.getVariant)(order.marketType);
|
|
204
204
|
const triggerList = this.orderLists.get(marketType).get(order.marketIndex)
|
|
205
|
-
.trigger[__1.isVariant(order.triggerCondition, 'above') ? 'above' : 'below'];
|
|
205
|
+
.trigger[(0, __1.isVariant)(order.triggerCondition, 'above') ? 'above' : 'below'];
|
|
206
206
|
triggerList.remove(order, userAccount);
|
|
207
207
|
(_a = this.getListForOrder(order, slot)) === null || _a === void 0 ? void 0 : _a.insert(order, marketType, userAccount);
|
|
208
208
|
if (onTrigger) {
|
|
@@ -211,7 +211,7 @@ class DLOB {
|
|
|
211
211
|
}
|
|
212
212
|
delete(order, userAccount, slot, onDelete) {
|
|
213
213
|
var _a;
|
|
214
|
-
if (__1.isVariant(order.status, 'init')) {
|
|
214
|
+
if ((0, __1.isVariant)(order.status, 'init')) {
|
|
215
215
|
return;
|
|
216
216
|
}
|
|
217
217
|
this.updateRestingLimitOrders(slot);
|
|
@@ -221,29 +221,29 @@ class DLOB {
|
|
|
221
221
|
}
|
|
222
222
|
}
|
|
223
223
|
getListForOrder(order, slot) {
|
|
224
|
-
const isInactiveTriggerOrder = __1.mustBeTriggered(order) && !__1.isTriggered(order);
|
|
224
|
+
const isInactiveTriggerOrder = (0, __1.mustBeTriggered)(order) && !(0, __1.isTriggered)(order);
|
|
225
225
|
let type;
|
|
226
226
|
if (isInactiveTriggerOrder) {
|
|
227
227
|
type = 'trigger';
|
|
228
228
|
}
|
|
229
|
-
else if (__1.isOneOfVariant(order.orderType, ['market', 'triggerMarket', 'oracle'])) {
|
|
229
|
+
else if ((0, __1.isOneOfVariant)(order.orderType, ['market', 'triggerMarket', 'oracle'])) {
|
|
230
230
|
type = 'market';
|
|
231
231
|
}
|
|
232
232
|
else if (order.oraclePriceOffset !== 0) {
|
|
233
233
|
type = 'floatingLimit';
|
|
234
234
|
}
|
|
235
235
|
else {
|
|
236
|
-
const isResting = __1.isRestingLimitOrder(order, slot);
|
|
236
|
+
const isResting = (0, __1.isRestingLimitOrder)(order, slot);
|
|
237
237
|
type = isResting ? 'restingLimit' : 'takingLimit';
|
|
238
238
|
}
|
|
239
239
|
let subType;
|
|
240
240
|
if (isInactiveTriggerOrder) {
|
|
241
|
-
subType = __1.isVariant(order.triggerCondition, 'above') ? 'above' : 'below';
|
|
241
|
+
subType = (0, __1.isVariant)(order.triggerCondition, 'above') ? 'above' : 'below';
|
|
242
242
|
}
|
|
243
243
|
else {
|
|
244
|
-
subType = __1.isVariant(order.direction, 'long') ? 'bid' : 'ask';
|
|
244
|
+
subType = (0, __1.isVariant)(order.direction, 'long') ? 'bid' : 'ask';
|
|
245
245
|
}
|
|
246
|
-
const marketType = __1.getVariant(order.marketType);
|
|
246
|
+
const marketType = (0, __1.getVariant)(order.marketType);
|
|
247
247
|
if (!this.orderLists.has(marketType)) {
|
|
248
248
|
return undefined;
|
|
249
249
|
}
|
|
@@ -261,7 +261,7 @@ class DLOB {
|
|
|
261
261
|
for (const [_, nodeLists] of this.orderLists.get(marketTypeStr)) {
|
|
262
262
|
const nodesToUpdate = [];
|
|
263
263
|
for (const node of nodeLists.takingLimit.ask.getGenerator()) {
|
|
264
|
-
if (!__1.isRestingLimitOrder(node.order, slot)) {
|
|
264
|
+
if (!(0, __1.isRestingLimitOrder)(node.order, slot)) {
|
|
265
265
|
continue;
|
|
266
266
|
}
|
|
267
267
|
nodesToUpdate.push({
|
|
@@ -270,7 +270,7 @@ class DLOB {
|
|
|
270
270
|
});
|
|
271
271
|
}
|
|
272
272
|
for (const node of nodeLists.takingLimit.bid.getGenerator()) {
|
|
273
|
-
if (!__1.isRestingLimitOrder(node.order, slot)) {
|
|
273
|
+
if (!(0, __1.isRestingLimitOrder)(node.order, slot)) {
|
|
274
274
|
continue;
|
|
275
275
|
}
|
|
276
276
|
nodesToUpdate.push({
|
|
@@ -286,8 +286,9 @@ class DLOB {
|
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
288
|
getOrder(orderId, userAccount) {
|
|
289
|
+
const orderSignature = (0, NodeList_1.getOrderSignature)(orderId, userAccount);
|
|
289
290
|
for (const nodeList of this.getNodeLists()) {
|
|
290
|
-
const node = nodeList.get(
|
|
291
|
+
const node = nodeList.get(orderSignature);
|
|
291
292
|
if (node) {
|
|
292
293
|
return node.order;
|
|
293
294
|
}
|
|
@@ -295,11 +296,11 @@ class DLOB {
|
|
|
295
296
|
return undefined;
|
|
296
297
|
}
|
|
297
298
|
findNodesToFill(marketIndex, fallbackBid, fallbackAsk, slot, ts, marketType, oraclePriceData, stateAccount, marketAccount) {
|
|
298
|
-
if (exchangeStatus_1.fillPaused(stateAccount, marketAccount)) {
|
|
299
|
+
if ((0, exchangeStatus_1.fillPaused)(stateAccount, marketAccount)) {
|
|
299
300
|
return [];
|
|
300
301
|
}
|
|
301
|
-
const isAmmPaused = exchangeStatus_1.ammPaused(stateAccount, marketAccount);
|
|
302
|
-
const minAuctionDuration = __1.isVariant(marketType, 'perp')
|
|
302
|
+
const isAmmPaused = (0, exchangeStatus_1.ammPaused)(stateAccount, marketAccount);
|
|
303
|
+
const minAuctionDuration = (0, __1.isVariant)(marketType, 'perp')
|
|
303
304
|
? stateAccount.minPerpAuctionDuration
|
|
304
305
|
: 0;
|
|
305
306
|
const restingLimitOrderNodesToFill = this.findRestingLimitOrderNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused, minAuctionDuration, fallbackAsk, fallbackBid);
|
|
@@ -338,7 +339,7 @@ class DLOB {
|
|
|
338
339
|
const nodesToFill = new Array();
|
|
339
340
|
let takingOrderGenerator = this.getTakingAsks(marketIndex, marketType, slot, oraclePriceData);
|
|
340
341
|
const takingAsksCrossingBids = this.findTakingNodesCrossingMakerNodes(marketIndex, slot, marketType, oraclePriceData, takingOrderGenerator, this.getMakerLimitBids.bind(this), (takerPrice, makerPrice) => {
|
|
341
|
-
if (__1.isVariant(marketType, 'spot')) {
|
|
342
|
+
if ((0, __1.isVariant)(marketType, 'spot')) {
|
|
342
343
|
if (takerPrice === undefined) {
|
|
343
344
|
return false;
|
|
344
345
|
}
|
|
@@ -362,7 +363,7 @@ class DLOB {
|
|
|
362
363
|
}
|
|
363
364
|
takingOrderGenerator = this.getTakingBids(marketIndex, marketType, slot, oraclePriceData);
|
|
364
365
|
const takingBidsToFill = this.findTakingNodesCrossingMakerNodes(marketIndex, slot, marketType, oraclePriceData, takingOrderGenerator, this.getMakerLimitAsks.bind(this), (takerPrice, makerPrice) => {
|
|
365
|
-
if (__1.isVariant(marketType, 'spot')) {
|
|
366
|
+
if ((0, __1.isVariant)(marketType, 'spot')) {
|
|
366
367
|
if (takerPrice === undefined) {
|
|
367
368
|
return false;
|
|
368
369
|
}
|
|
@@ -434,16 +435,16 @@ class DLOB {
|
|
|
434
435
|
let nextNode = nodeGenerator.next();
|
|
435
436
|
while (!nextNode.done) {
|
|
436
437
|
const node = nextNode.value;
|
|
437
|
-
if (__1.isVariant(marketType, 'spot') && ((_a = node.order) === null || _a === void 0 ? void 0 : _a.postOnly)) {
|
|
438
|
+
if ((0, __1.isVariant)(marketType, 'spot') && ((_a = node.order) === null || _a === void 0 ? void 0 : _a.postOnly)) {
|
|
438
439
|
nextNode = nodeGenerator.next();
|
|
439
440
|
continue;
|
|
440
441
|
}
|
|
441
|
-
const nodePrice = __1.getLimitPrice(node.order, oraclePriceData, slot);
|
|
442
|
+
const nodePrice = (0, __1.getLimitPrice)(node.order, oraclePriceData, slot);
|
|
442
443
|
// order crosses if there is no limit price or it crosses fallback price
|
|
443
444
|
const crosses = doesCross(nodePrice, fallbackPrice);
|
|
444
445
|
// fallback is available if auction is complete or it's a spot order
|
|
445
|
-
const fallbackAvailable = __1.isVariant(marketType, 'spot') ||
|
|
446
|
-
__1.isFallbackAvailableLiquiditySource(node.order, minAuctionDuration, slot);
|
|
446
|
+
const fallbackAvailable = (0, __1.isVariant)(marketType, 'spot') ||
|
|
447
|
+
(0, __1.isFallbackAvailableLiquiditySource)(node.order, minAuctionDuration, slot);
|
|
447
448
|
if (crosses && fallbackAvailable) {
|
|
448
449
|
nodesToFill.push({
|
|
449
450
|
node: node,
|
|
@@ -456,7 +457,7 @@ class DLOB {
|
|
|
456
457
|
}
|
|
457
458
|
findExpiredNodesToFill(marketIndex, ts, marketType) {
|
|
458
459
|
const nodesToFill = new Array();
|
|
459
|
-
const marketTypeStr = __1.getVariant(marketType);
|
|
460
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
460
461
|
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
461
462
|
if (!nodeLists) {
|
|
462
463
|
return nodesToFill;
|
|
@@ -476,7 +477,7 @@ class DLOB {
|
|
|
476
477
|
];
|
|
477
478
|
for (const bidGenerator of bidGenerators) {
|
|
478
479
|
for (const bid of bidGenerator) {
|
|
479
|
-
if (__1.isOrderExpired(bid.order, ts)) {
|
|
480
|
+
if ((0, __1.isOrderExpired)(bid.order, ts)) {
|
|
480
481
|
nodesToFill.push({
|
|
481
482
|
node: bid,
|
|
482
483
|
});
|
|
@@ -485,7 +486,7 @@ class DLOB {
|
|
|
485
486
|
}
|
|
486
487
|
for (const askGenerator of askGenerators) {
|
|
487
488
|
for (const ask of askGenerator) {
|
|
488
|
-
if (__1.isOrderExpired(ask.order, ts)) {
|
|
489
|
+
if ((0, __1.isOrderExpired)(ask.order, ts)) {
|
|
489
490
|
nodesToFill.push({
|
|
490
491
|
node: ask,
|
|
491
492
|
});
|
|
@@ -510,7 +511,7 @@ class DLOB {
|
|
|
510
511
|
return nodesToFill;
|
|
511
512
|
}
|
|
512
513
|
*getTakingBids(marketIndex, marketType, slot, oraclePriceData) {
|
|
513
|
-
const marketTypeStr = __1.getVariant(marketType);
|
|
514
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
514
515
|
const orderLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
515
516
|
if (!orderLists) {
|
|
516
517
|
return;
|
|
@@ -525,7 +526,7 @@ class DLOB {
|
|
|
525
526
|
});
|
|
526
527
|
}
|
|
527
528
|
*getTakingAsks(marketIndex, marketType, slot, oraclePriceData) {
|
|
528
|
-
const marketTypeStr = __1.getVariant(marketType);
|
|
529
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
529
530
|
const orderLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
530
531
|
if (!orderLists) {
|
|
531
532
|
return;
|
|
@@ -576,11 +577,11 @@ class DLOB {
|
|
|
576
577
|
}
|
|
577
578
|
}
|
|
578
579
|
*getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData) {
|
|
579
|
-
if (__1.isVariant(marketType, 'spot') && !oraclePriceData) {
|
|
580
|
+
if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
|
|
580
581
|
throw new Error('Must provide OraclePriceData to get spot asks');
|
|
581
582
|
}
|
|
582
583
|
this.updateRestingLimitOrders(slot);
|
|
583
|
-
const marketTypeStr = __1.getVariant(marketType);
|
|
584
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
584
585
|
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
585
586
|
if (!nodeLists) {
|
|
586
587
|
return;
|
|
@@ -611,11 +612,11 @@ class DLOB {
|
|
|
611
612
|
}
|
|
612
613
|
}
|
|
613
614
|
*getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData) {
|
|
614
|
-
if (__1.isVariant(marketType, 'spot') && !oraclePriceData) {
|
|
615
|
+
if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
|
|
615
616
|
throw new Error('Must provide OraclePriceData to get spot bids');
|
|
616
617
|
}
|
|
617
618
|
this.updateRestingLimitOrders(slot);
|
|
618
|
-
const marketTypeStr = __1.getVariant(marketType);
|
|
619
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
619
620
|
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
620
621
|
if (!nodeLists) {
|
|
621
622
|
return;
|
|
@@ -646,23 +647,23 @@ class DLOB {
|
|
|
646
647
|
}
|
|
647
648
|
}
|
|
648
649
|
*getAsks(marketIndex, fallbackAsk, slot, marketType, oraclePriceData) {
|
|
649
|
-
if (__1.isVariant(marketType, 'spot') && !oraclePriceData) {
|
|
650
|
+
if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
|
|
650
651
|
throw new Error('Must provide OraclePriceData to get spot asks');
|
|
651
652
|
}
|
|
652
653
|
const generatorList = [
|
|
653
654
|
this.getTakingAsks(marketIndex, marketType, slot, oraclePriceData),
|
|
654
655
|
this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData),
|
|
655
656
|
];
|
|
656
|
-
const marketTypeStr = __1.getVariant(marketType);
|
|
657
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
657
658
|
if (marketTypeStr === 'perp' && fallbackAsk) {
|
|
658
|
-
generatorList.push(NodeList_1.getVammNodeGenerator(fallbackAsk));
|
|
659
|
+
generatorList.push((0, NodeList_1.getVammNodeGenerator)(fallbackAsk));
|
|
659
660
|
}
|
|
660
661
|
yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestNode, currentNode, slot, oraclePriceData) => {
|
|
661
662
|
const bestNodeTaking = bestNode.order
|
|
662
|
-
? __1.isTakingOrder(bestNode.order, slot)
|
|
663
|
+
? (0, __1.isTakingOrder)(bestNode.order, slot)
|
|
663
664
|
: false;
|
|
664
665
|
const currentNodeTaking = currentNode.order
|
|
665
|
-
? __1.isTakingOrder(currentNode.order, slot)
|
|
666
|
+
? (0, __1.isTakingOrder)(currentNode.order, slot)
|
|
666
667
|
: false;
|
|
667
668
|
if (bestNodeTaking && currentNodeTaking) {
|
|
668
669
|
return bestNode.order.slot.lt(currentNode.order.slot);
|
|
@@ -679,23 +680,23 @@ class DLOB {
|
|
|
679
680
|
});
|
|
680
681
|
}
|
|
681
682
|
*getBids(marketIndex, fallbackBid, slot, marketType, oraclePriceData) {
|
|
682
|
-
if (__1.isVariant(marketType, 'spot') && !oraclePriceData) {
|
|
683
|
+
if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
|
|
683
684
|
throw new Error('Must provide OraclePriceData to get spot bids');
|
|
684
685
|
}
|
|
685
686
|
const generatorList = [
|
|
686
687
|
this.getTakingBids(marketIndex, marketType, slot, oraclePriceData),
|
|
687
688
|
this.getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData),
|
|
688
689
|
];
|
|
689
|
-
const marketTypeStr = __1.getVariant(marketType);
|
|
690
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
690
691
|
if (marketTypeStr === 'perp' && fallbackBid) {
|
|
691
|
-
generatorList.push(NodeList_1.getVammNodeGenerator(fallbackBid));
|
|
692
|
+
generatorList.push((0, NodeList_1.getVammNodeGenerator)(fallbackBid));
|
|
692
693
|
}
|
|
693
694
|
yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestNode, currentNode, slot, oraclePriceData) => {
|
|
694
695
|
const bestNodeTaking = bestNode.order
|
|
695
|
-
? __1.isTakingOrder(bestNode.order, slot)
|
|
696
|
+
? (0, __1.isTakingOrder)(bestNode.order, slot)
|
|
696
697
|
: false;
|
|
697
698
|
const currentNodeTaking = currentNode.order
|
|
698
|
-
? __1.isTakingOrder(currentNode.order, slot)
|
|
699
|
+
? (0, __1.isTakingOrder)(currentNode.order, slot)
|
|
699
700
|
: false;
|
|
700
701
|
if (bestNodeTaking && currentNodeTaking) {
|
|
701
702
|
return bestNode.order.slot.lt(currentNode.order.slot);
|
|
@@ -761,20 +762,32 @@ class DLOB {
|
|
|
761
762
|
determineMakerAndTaker(askNode, bidNode) {
|
|
762
763
|
const askSlot = askNode.order.slot.add(new __1.BN(askNode.order.auctionDuration));
|
|
763
764
|
const bidSlot = bidNode.order.slot.add(new __1.BN(bidNode.order.auctionDuration));
|
|
764
|
-
if (
|
|
765
|
+
if (bidNode.order.postOnly && askNode.order.postOnly) {
|
|
766
|
+
return undefined;
|
|
767
|
+
}
|
|
768
|
+
else if (bidNode.order.postOnly) {
|
|
769
|
+
return {
|
|
770
|
+
takerNode: askNode,
|
|
771
|
+
makerNode: bidNode,
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
else if (askNode.order.postOnly) {
|
|
765
775
|
return {
|
|
766
776
|
takerNode: bidNode,
|
|
767
777
|
makerNode: askNode,
|
|
768
778
|
};
|
|
769
779
|
}
|
|
770
|
-
else if (
|
|
780
|
+
else if (askSlot.lte(bidSlot)) {
|
|
771
781
|
return {
|
|
772
|
-
takerNode:
|
|
773
|
-
makerNode:
|
|
782
|
+
takerNode: bidNode,
|
|
783
|
+
makerNode: askNode,
|
|
774
784
|
};
|
|
775
785
|
}
|
|
776
786
|
else {
|
|
777
|
-
return
|
|
787
|
+
return {
|
|
788
|
+
takerNode: askNode,
|
|
789
|
+
makerNode: bidNode,
|
|
790
|
+
};
|
|
778
791
|
}
|
|
779
792
|
}
|
|
780
793
|
getBestAsk(marketIndex, fallbackAsk, slot, marketType, oraclePriceData) {
|
|
@@ -788,11 +801,11 @@ class DLOB {
|
|
|
788
801
|
.value.getPrice(oraclePriceData, slot);
|
|
789
802
|
}
|
|
790
803
|
findNodesToTrigger(marketIndex, slot, oraclePrice, marketType, stateAccount) {
|
|
791
|
-
if (exchangeStatus_1.exchangePaused(stateAccount)) {
|
|
804
|
+
if ((0, exchangeStatus_1.exchangePaused)(stateAccount)) {
|
|
792
805
|
return [];
|
|
793
806
|
}
|
|
794
807
|
const nodesToTrigger = [];
|
|
795
|
-
const marketTypeStr = __1.getVariant(marketType);
|
|
808
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
796
809
|
const marketNodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
797
810
|
const triggerAboveList = marketNodeLists
|
|
798
811
|
? marketNodeLists.trigger.above
|
|
@@ -827,46 +840,46 @@ class DLOB {
|
|
|
827
840
|
return nodesToTrigger;
|
|
828
841
|
}
|
|
829
842
|
printTopOfOrderLists(sdkConfig, driftClient, slotSubscriber, marketIndex, marketType) {
|
|
830
|
-
if (__1.isVariant(marketType, 'perp')) {
|
|
843
|
+
if ((0, __1.isVariant)(marketType, 'perp')) {
|
|
831
844
|
const market = driftClient.getPerpMarketAccount(marketIndex);
|
|
832
845
|
const slot = slotSubscriber.getSlot();
|
|
833
846
|
const oraclePriceData = driftClient.getOracleDataForPerpMarket(marketIndex);
|
|
834
|
-
const fallbackAsk = __1.calculateAskPrice(market, oraclePriceData);
|
|
835
|
-
const fallbackBid = __1.calculateBidPrice(market, oraclePriceData);
|
|
847
|
+
const fallbackAsk = (0, __1.calculateAskPrice)(market, oraclePriceData);
|
|
848
|
+
const fallbackBid = (0, __1.calculateBidPrice)(market, oraclePriceData);
|
|
836
849
|
const bestAsk = this.getBestAsk(marketIndex, fallbackAsk, slot, marketType, oraclePriceData);
|
|
837
850
|
const bestBid = this.getBestBid(marketIndex, fallbackBid, slot, marketType, oraclePriceData);
|
|
838
851
|
const mid = bestAsk.add(bestBid).div(new __1.BN(2));
|
|
839
|
-
const bidSpread = (__1.convertToNumber(bestBid, __1.PRICE_PRECISION) /
|
|
840
|
-
__1.convertToNumber(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
852
|
+
const bidSpread = ((0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION) /
|
|
853
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
841
854
|
1) *
|
|
842
855
|
100.0;
|
|
843
|
-
const askSpread = (__1.convertToNumber(bestAsk, __1.PRICE_PRECISION) /
|
|
844
|
-
__1.convertToNumber(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
856
|
+
const askSpread = ((0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION) /
|
|
857
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
845
858
|
1) *
|
|
846
859
|
100.0;
|
|
847
860
|
console.log(`Market ${sdkConfig.MARKETS[marketIndex].symbol} Orders`);
|
|
848
|
-
console.log(` Ask`, __1.convertToNumber(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
|
|
849
|
-
console.log(` Mid`, __1.convertToNumber(mid, __1.PRICE_PRECISION).toFixed(3));
|
|
850
|
-
console.log(` Bid`, __1.convertToNumber(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
|
|
861
|
+
console.log(` Ask`, (0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
|
|
862
|
+
console.log(` Mid`, (0, __1.convertToNumber)(mid, __1.PRICE_PRECISION).toFixed(3));
|
|
863
|
+
console.log(` Bid`, (0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
|
|
851
864
|
}
|
|
852
|
-
else if (__1.isVariant(marketType, 'spot')) {
|
|
865
|
+
else if ((0, __1.isVariant)(marketType, 'spot')) {
|
|
853
866
|
const slot = slotSubscriber.getSlot();
|
|
854
867
|
const oraclePriceData = driftClient.getOracleDataForPerpMarket(marketIndex);
|
|
855
868
|
const bestAsk = this.getBestAsk(marketIndex, undefined, slot, marketType, oraclePriceData);
|
|
856
869
|
const bestBid = this.getBestBid(marketIndex, undefined, slot, marketType, oraclePriceData);
|
|
857
870
|
const mid = bestAsk.add(bestBid).div(new __1.BN(2));
|
|
858
|
-
const bidSpread = (__1.convertToNumber(bestBid, __1.PRICE_PRECISION) /
|
|
859
|
-
__1.convertToNumber(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
871
|
+
const bidSpread = ((0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION) /
|
|
872
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
860
873
|
1) *
|
|
861
874
|
100.0;
|
|
862
|
-
const askSpread = (__1.convertToNumber(bestAsk, __1.PRICE_PRECISION) /
|
|
863
|
-
__1.convertToNumber(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
875
|
+
const askSpread = ((0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION) /
|
|
876
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
864
877
|
1) *
|
|
865
878
|
100.0;
|
|
866
879
|
console.log(`Market ${sdkConfig.MARKETS[marketIndex].symbol} Orders`);
|
|
867
|
-
console.log(` Ask`, __1.convertToNumber(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
|
|
868
|
-
console.log(` Mid`, __1.convertToNumber(mid, __1.PRICE_PRECISION).toFixed(3));
|
|
869
|
-
console.log(` Bid`, __1.convertToNumber(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
|
|
880
|
+
console.log(` Ask`, (0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
|
|
881
|
+
console.log(` Mid`, (0, __1.convertToNumber)(mid, __1.PRICE_PRECISION).toFixed(3));
|
|
882
|
+
console.log(` Bid`, (0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
|
|
870
883
|
}
|
|
871
884
|
}
|
|
872
885
|
getDLOBOrders() {
|
package/lib/dlob/DLOBNode.d.ts
CHANGED
|
@@ -47,12 +47,12 @@ export declare class TriggerOrderNode extends OrderNode {
|
|
|
47
47
|
previous?: TriggerOrderNode;
|
|
48
48
|
getSortValue(order: Order): BN;
|
|
49
49
|
}
|
|
50
|
-
export
|
|
50
|
+
export type DLOBNodeMap = {
|
|
51
51
|
restingLimit: RestingLimitOrderNode;
|
|
52
52
|
takingLimit: TakingLimitOrderNode;
|
|
53
53
|
floatingLimit: FloatingLimitOrderNode;
|
|
54
54
|
market: MarketOrderNode;
|
|
55
55
|
trigger: TriggerOrderNode;
|
|
56
56
|
};
|
|
57
|
-
export
|
|
57
|
+
export type DLOBNodeType = 'restingLimit' | 'takingLimit' | 'floatingLimit' | 'market' | ('trigger' & keyof DLOBNodeMap);
|
|
58
58
|
export declare function createNode<T extends DLOBNodeType>(nodeType: T, order: Order, userAccount: PublicKey): DLOBNodeMap[T];
|
package/lib/dlob/DLOBNode.js
CHANGED
|
@@ -13,20 +13,20 @@ class OrderNode {
|
|
|
13
13
|
this.sortValue = this.getSortValue(order);
|
|
14
14
|
}
|
|
15
15
|
getLabel() {
|
|
16
|
-
let msg = `Order ${NodeList_1.getOrderSignature(this.order.orderId, this.userAccount)}`;
|
|
17
|
-
msg += ` ${__1.isVariant(this.order.direction, 'long') ? 'LONG' : 'SHORT'} `;
|
|
18
|
-
msg += `${__1.convertToNumber(this.order.baseAssetAmount, __1.AMM_RESERVE_PRECISION).toFixed(3)}`;
|
|
16
|
+
let msg = `Order ${(0, NodeList_1.getOrderSignature)(this.order.orderId, this.userAccount)}`;
|
|
17
|
+
msg += ` ${(0, __1.isVariant)(this.order.direction, 'long') ? 'LONG' : 'SHORT'} `;
|
|
18
|
+
msg += `${(0, __1.convertToNumber)(this.order.baseAssetAmount, __1.AMM_RESERVE_PRECISION).toFixed(3)}`;
|
|
19
19
|
if (this.order.price.gt(__1.ZERO)) {
|
|
20
|
-
msg += ` @ ${__1.convertToNumber(this.order.price, __1.PRICE_PRECISION).toFixed(3)}`;
|
|
20
|
+
msg += ` @ ${(0, __1.convertToNumber)(this.order.price, __1.PRICE_PRECISION).toFixed(3)}`;
|
|
21
21
|
}
|
|
22
22
|
if (this.order.triggerPrice.gt(__1.ZERO)) {
|
|
23
|
-
msg += ` ${__1.isVariant(this.order.triggerCondition, 'below') ? 'BELOW' : 'ABOVE'}`;
|
|
24
|
-
msg += ` ${__1.convertToNumber(this.order.triggerPrice, __1.PRICE_PRECISION).toFixed(3)}`;
|
|
23
|
+
msg += ` ${(0, __1.isVariant)(this.order.triggerCondition, 'below') ? 'BELOW' : 'ABOVE'}`;
|
|
24
|
+
msg += ` ${(0, __1.convertToNumber)(this.order.triggerPrice, __1.PRICE_PRECISION).toFixed(3)}`;
|
|
25
25
|
}
|
|
26
26
|
return msg;
|
|
27
27
|
}
|
|
28
28
|
getPrice(oraclePriceData, slot) {
|
|
29
|
-
return __1.getLimitPrice(this.order, oraclePriceData, slot);
|
|
29
|
+
return (0, __1.getLimitPrice)(this.order, oraclePriceData, slot);
|
|
30
30
|
}
|
|
31
31
|
isBaseFilled() {
|
|
32
32
|
return this.order.baseAssetAmountFilled.eq(this.order.baseAssetAmount);
|
package/lib/dlob/DLOBOrders.d.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { Idl } from '@project-serum/anchor';
|
|
4
4
|
import { Order } from '../types';
|
|
5
|
-
export
|
|
5
|
+
export type DLOBOrder = {
|
|
6
6
|
user: PublicKey;
|
|
7
7
|
order: Order;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export type DLOBOrders = DLOBOrder[];
|
|
10
10
|
export declare class DLOBOrdersCoder {
|
|
11
11
|
private idl;
|
|
12
12
|
constructor(idl: Idl);
|
package/lib/dlob/NodeList.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { BN, MarketTypeStr, Order } from '..';
|
|
3
3
|
import { PublicKey } from '@solana/web3.js';
|
|
4
4
|
import { DLOBNode, DLOBNodeMap } from './DLOBNode';
|
|
5
|
-
export
|
|
5
|
+
export type SortDirection = 'asc' | 'desc';
|
|
6
6
|
export declare function getOrderSignature(orderId: number, userAccount: PublicKey): string;
|
|
7
7
|
export interface DLOBNodeGenerator {
|
|
8
8
|
getGenerator(): Generator<DLOBNode>;
|
|
@@ -21,7 +21,7 @@ export declare class NodeList<NodeType extends keyof DLOBNodeMap> implements DLO
|
|
|
21
21
|
remove(order: Order, userAccount: PublicKey): void;
|
|
22
22
|
getGenerator(): Generator<DLOBNode>;
|
|
23
23
|
has(order: Order, userAccount: PublicKey): boolean;
|
|
24
|
-
get(
|
|
24
|
+
get(orderSignature: string): DLOBNodeMap[NodeType] | undefined;
|
|
25
25
|
print(): void;
|
|
26
26
|
printTop(): void;
|
|
27
27
|
}
|
package/lib/dlob/NodeList.js
CHANGED
|
@@ -20,10 +20,10 @@ class NodeList {
|
|
|
20
20
|
this.nodeMap.clear();
|
|
21
21
|
}
|
|
22
22
|
insert(order, marketType, userAccount) {
|
|
23
|
-
if (__1.isVariant(order.status, 'init')) {
|
|
23
|
+
if ((0, __1.isVariant)(order.status, 'init')) {
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
|
-
const newNode = DLOBNode_1.createNode(this.nodeType, order, userAccount);
|
|
26
|
+
const newNode = (0, DLOBNode_1.createNode)(this.nodeType, order, userAccount);
|
|
27
27
|
const orderSignature = getOrderSignature(order.orderId, userAccount);
|
|
28
28
|
if (this.nodeMap.has(orderSignature)) {
|
|
29
29
|
return;
|
|
@@ -104,8 +104,8 @@ class NodeList {
|
|
|
104
104
|
has(order, userAccount) {
|
|
105
105
|
return this.nodeMap.has(getOrderSignature(order.orderId, userAccount));
|
|
106
106
|
}
|
|
107
|
-
get(
|
|
108
|
-
return this.nodeMap.get(
|
|
107
|
+
get(orderSignature) {
|
|
108
|
+
return this.nodeMap.get(orderSignature);
|
|
109
109
|
}
|
|
110
110
|
print() {
|
|
111
111
|
let currentNode = this.head;
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ import { DriftClientConfig } from './driftClientConfig';
|
|
|
14
14
|
import { User } from './user';
|
|
15
15
|
import { UserSubscriptionConfig } from './userConfig';
|
|
16
16
|
import { UserStats } from './userStats';
|
|
17
|
-
|
|
17
|
+
type RemainingAccountParams = {
|
|
18
18
|
userAccounts: UserAccount[];
|
|
19
19
|
writablePerpMarketIndexes?: number[];
|
|
20
20
|
writableSpotMarketIndexes?: number[];
|