@drift-labs/sdk 2.12.0-beta.2 → 2.12.0
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.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/webSocketAccountSubscriber.js +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +3 -3
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.js +1 -5
- package/lib/adminClient.js +57 -61
- package/lib/dlob/DLOB.d.ts +8 -8
- package/lib/dlob/DLOB.js +91 -84
- package/lib/dlob/DLOBNode.js +7 -7
- package/lib/dlob/NodeList.js +2 -2
- package/lib/driftClient.d.ts +3 -3
- package/lib/driftClient.js +83 -87
- package/lib/events/eventSubscriber.js +2 -2
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/examples/makeTradeExample.js +9 -9
- package/lib/factory/bigNum.js +9 -9
- package/lib/factory/oracleClient.js +2 -2
- package/lib/idl/drift.json +1 -1
- package/lib/index.js +1 -5
- package/lib/math/amm.js +23 -23
- 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 +5 -5
- package/lib/math/market.js +12 -12
- package/lib/math/oracles.js +1 -1
- package/lib/math/orders.js +23 -23
- package/lib/math/position.js +5 -5
- package/lib/math/repeg.js +1 -1
- package/lib/math/spotBalance.js +7 -7
- package/lib/math/spotPosition.js +3 -3
- package/lib/math/trade.d.ts +35 -3
- package/lib/math/trade.js +225 -47
- package/lib/oracles/oracleClientCache.js +1 -1
- package/lib/oracles/pythClient.js +1 -1
- package/lib/tokenFaucet.js +1 -5
- package/lib/tx/retryTxSender.js +1 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +7 -4
- package/lib/types.d.ts +4 -0
- package/lib/user.js +47 -47
- package/lib/userMap/userMap.js +1 -1
- package/lib/userMap/userStatsMap.js +3 -3
- package/lib/userStats.js +2 -2
- package/package.json +1 -1
- package/src/dlob/DLOB.ts +34 -26
- package/src/driftClient.ts +11 -4
- package/src/idl/drift.json +1 -1
- package/src/math/trade.ts +322 -16
- package/src/tx/utils.ts +11 -3
- package/src/types.ts +5 -0
- package/tests/dlob/test.ts +307 -38
package/lib/dlob/DLOB.js
CHANGED
|
@@ -78,10 +78,10 @@ class DLOB {
|
|
|
78
78
|
this.insertOrder(record.order, record.user);
|
|
79
79
|
}
|
|
80
80
|
handleOrderActionRecord(record) {
|
|
81
|
-
if (
|
|
81
|
+
if (__1.isOneOfVariant(record.action, ['place', 'expire'])) {
|
|
82
82
|
return;
|
|
83
83
|
}
|
|
84
|
-
if (
|
|
84
|
+
if (__1.isVariant(record.action, 'trigger')) {
|
|
85
85
|
if (record.taker !== null) {
|
|
86
86
|
const takerOrder = this.getOrder(record.takerOrderId, record.taker);
|
|
87
87
|
if (takerOrder) {
|
|
@@ -95,7 +95,7 @@ class DLOB {
|
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
else if (
|
|
98
|
+
else if (__1.isVariant(record.action, 'fill')) {
|
|
99
99
|
if (record.taker !== null) {
|
|
100
100
|
const takerOrder = this.getOrder(record.takerOrderId, record.taker);
|
|
101
101
|
if (takerOrder) {
|
|
@@ -109,7 +109,7 @@ class DLOB {
|
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
|
-
else if (
|
|
112
|
+
else if (__1.isVariant(record.action, 'cancel')) {
|
|
113
113
|
if (record.taker !== null) {
|
|
114
114
|
const takerOrder = this.getOrder(record.takerOrderId, record.taker);
|
|
115
115
|
if (takerOrder) {
|
|
@@ -126,20 +126,20 @@ class DLOB {
|
|
|
126
126
|
}
|
|
127
127
|
insertOrder(order, userAccount, onInsert) {
|
|
128
128
|
var _a;
|
|
129
|
-
if (
|
|
129
|
+
if (__1.isVariant(order.status, 'init')) {
|
|
130
130
|
return;
|
|
131
131
|
}
|
|
132
|
-
if (!
|
|
132
|
+
if (!__1.isOneOfVariant(order.orderType, SUPPORTED_ORDER_TYPES)) {
|
|
133
133
|
return;
|
|
134
134
|
}
|
|
135
|
-
const marketType =
|
|
135
|
+
const marketType = __1.getVariant(order.marketType);
|
|
136
136
|
if (!this.orderLists.get(marketType).has(order.marketIndex)) {
|
|
137
137
|
this.addOrderList(marketType, order.marketIndex);
|
|
138
138
|
}
|
|
139
|
-
if (
|
|
139
|
+
if (__1.isVariant(order.status, 'open')) {
|
|
140
140
|
this.openOrders
|
|
141
141
|
.get(marketType)
|
|
142
|
-
.add(
|
|
142
|
+
.add(NodeList_1.getOrderSignature(order.orderId, userAccount));
|
|
143
143
|
}
|
|
144
144
|
(_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.insert(order, marketType, userAccount);
|
|
145
145
|
if (onInsert) {
|
|
@@ -186,15 +186,15 @@ class DLOB {
|
|
|
186
186
|
}
|
|
187
187
|
trigger(order, userAccount, onTrigger) {
|
|
188
188
|
var _a;
|
|
189
|
-
if (
|
|
189
|
+
if (__1.isVariant(order.status, 'init')) {
|
|
190
190
|
return;
|
|
191
191
|
}
|
|
192
|
-
if (
|
|
192
|
+
if (__1.isTriggered(order)) {
|
|
193
193
|
return;
|
|
194
194
|
}
|
|
195
|
-
const marketType =
|
|
195
|
+
const marketType = __1.getVariant(order.marketType);
|
|
196
196
|
const triggerList = this.orderLists.get(marketType).get(order.marketIndex)
|
|
197
|
-
.trigger[
|
|
197
|
+
.trigger[__1.isVariant(order.triggerCondition, 'above') ? 'above' : 'below'];
|
|
198
198
|
triggerList.remove(order, userAccount);
|
|
199
199
|
(_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.insert(order, marketType, userAccount);
|
|
200
200
|
if (onTrigger) {
|
|
@@ -203,7 +203,7 @@ class DLOB {
|
|
|
203
203
|
}
|
|
204
204
|
delete(order, userAccount, onDelete) {
|
|
205
205
|
var _a;
|
|
206
|
-
if (
|
|
206
|
+
if (__1.isVariant(order.status, 'init')) {
|
|
207
207
|
return;
|
|
208
208
|
}
|
|
209
209
|
(_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.remove(order, userAccount);
|
|
@@ -212,12 +212,12 @@ class DLOB {
|
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
getListForOrder(order) {
|
|
215
|
-
const isInactiveTriggerOrder =
|
|
215
|
+
const isInactiveTriggerOrder = __1.mustBeTriggered(order) && !__1.isTriggered(order);
|
|
216
216
|
let type;
|
|
217
217
|
if (isInactiveTriggerOrder) {
|
|
218
218
|
type = 'trigger';
|
|
219
219
|
}
|
|
220
|
-
else if (
|
|
220
|
+
else if (__1.isOneOfVariant(order.orderType, ['market', 'triggerMarket', 'oracle'])) {
|
|
221
221
|
type = 'market';
|
|
222
222
|
}
|
|
223
223
|
else if (order.oraclePriceOffset !== 0) {
|
|
@@ -228,12 +228,12 @@ class DLOB {
|
|
|
228
228
|
}
|
|
229
229
|
let subType;
|
|
230
230
|
if (isInactiveTriggerOrder) {
|
|
231
|
-
subType =
|
|
231
|
+
subType = __1.isVariant(order.triggerCondition, 'above') ? 'above' : 'below';
|
|
232
232
|
}
|
|
233
233
|
else {
|
|
234
|
-
subType =
|
|
234
|
+
subType = __1.isVariant(order.direction, 'long') ? 'bid' : 'ask';
|
|
235
235
|
}
|
|
236
|
-
const marketType =
|
|
236
|
+
const marketType = __1.getVariant(order.marketType);
|
|
237
237
|
if (!this.orderLists.has(marketType)) {
|
|
238
238
|
return undefined;
|
|
239
239
|
}
|
|
@@ -249,10 +249,10 @@ class DLOB {
|
|
|
249
249
|
return undefined;
|
|
250
250
|
}
|
|
251
251
|
findNodesToFill(marketIndex, fallbackBid, fallbackAsk, slot, ts, marketType, oraclePriceData, stateAccount, marketAccount) {
|
|
252
|
-
if (
|
|
252
|
+
if (exchangeStatus_1.fillPaused(stateAccount, marketAccount)) {
|
|
253
253
|
return [];
|
|
254
254
|
}
|
|
255
|
-
const isAmmPaused =
|
|
255
|
+
const isAmmPaused = exchangeStatus_1.ammPaused(stateAccount, marketAccount);
|
|
256
256
|
const marketOrderNodesToFill = this.findMarketNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused, fallbackAsk, fallbackBid);
|
|
257
257
|
const limitOrderNodesToFill = this.findLimitOrderNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused, fallbackAsk, fallbackBid);
|
|
258
258
|
// get expired market nodes
|
|
@@ -288,9 +288,9 @@ class DLOB {
|
|
|
288
288
|
findMarketNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused, fallbackAsk, fallbackBid) {
|
|
289
289
|
const nodesToFill = new Array();
|
|
290
290
|
let marketOrderGenerator = this.getMarketAsks(marketIndex, marketType);
|
|
291
|
-
const marketAsksCrossingBids = this.findMarketNodesCrossingLimitNodes(marketIndex, slot, marketType, oraclePriceData, marketOrderGenerator, this.
|
|
291
|
+
const marketAsksCrossingBids = this.findMarketNodesCrossingLimitNodes(marketIndex, slot, marketType, oraclePriceData, marketOrderGenerator, this.getMakerLimitBids.bind(this), (takerPrice, makerPrice) => {
|
|
292
292
|
return takerPrice === undefined || takerPrice.lte(makerPrice);
|
|
293
|
-
});
|
|
293
|
+
}, fallbackAsk);
|
|
294
294
|
for (const marketAskCrossingBid of marketAsksCrossingBids) {
|
|
295
295
|
nodesToFill.push(marketAskCrossingBid);
|
|
296
296
|
}
|
|
@@ -304,9 +304,9 @@ class DLOB {
|
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
306
|
marketOrderGenerator = this.getMarketBids(marketIndex, marketType);
|
|
307
|
-
const marketBidsToFill = this.findMarketNodesCrossingLimitNodes(marketIndex, slot, marketType, oraclePriceData, marketOrderGenerator, this.
|
|
307
|
+
const marketBidsToFill = this.findMarketNodesCrossingLimitNodes(marketIndex, slot, marketType, oraclePriceData, marketOrderGenerator, this.getMakerLimitAsks.bind(this), (takerPrice, fallbackPrice) => {
|
|
308
308
|
return takerPrice === undefined || takerPrice.gte(fallbackPrice);
|
|
309
|
-
});
|
|
309
|
+
}, fallbackBid);
|
|
310
310
|
for (const marketBidToFill of marketBidsToFill) {
|
|
311
311
|
nodesToFill.push(marketBidToFill);
|
|
312
312
|
}
|
|
@@ -321,10 +321,10 @@ class DLOB {
|
|
|
321
321
|
}
|
|
322
322
|
return nodesToFill;
|
|
323
323
|
}
|
|
324
|
-
findMarketNodesCrossingLimitNodes(marketIndex, slot, marketType, oraclePriceData, takerNodeGenerator, makerNodeGeneratorFn, doesCross) {
|
|
324
|
+
findMarketNodesCrossingLimitNodes(marketIndex, slot, marketType, oraclePriceData, takerNodeGenerator, makerNodeGeneratorFn, doesCross, fallbackPrice) {
|
|
325
325
|
const nodesToFill = new Array();
|
|
326
326
|
for (const takerNode of takerNodeGenerator) {
|
|
327
|
-
const makerNodeGenerator = makerNodeGeneratorFn(marketIndex, slot, marketType, oraclePriceData);
|
|
327
|
+
const makerNodeGenerator = makerNodeGeneratorFn(marketIndex, slot, marketType, oraclePriceData, fallbackPrice);
|
|
328
328
|
for (const makerNode of makerNodeGenerator) {
|
|
329
329
|
// Can't match orders from the same user
|
|
330
330
|
const sameUser = takerNode.userAccount.equals(makerNode.userAccount);
|
|
@@ -369,15 +369,15 @@ class DLOB {
|
|
|
369
369
|
let nextNode = nodeGenerator.next();
|
|
370
370
|
while (!nextNode.done) {
|
|
371
371
|
const node = nextNode.value;
|
|
372
|
-
if (
|
|
372
|
+
if (__1.isVariant(marketType, 'spot') && ((_a = node.order) === null || _a === void 0 ? void 0 : _a.postOnly)) {
|
|
373
373
|
nextNode = nodeGenerator.next();
|
|
374
374
|
continue;
|
|
375
375
|
}
|
|
376
|
-
const nodePrice =
|
|
376
|
+
const nodePrice = __1.getLimitPrice(node.order, oraclePriceData, slot);
|
|
377
377
|
// order crosses if there is no limit price or it crosses fallback price
|
|
378
378
|
const crosses = doesCross(nodePrice, fallbackPrice);
|
|
379
379
|
// fallback is available if auction is complete or it's a spot order
|
|
380
|
-
const fallbackAvailable =
|
|
380
|
+
const fallbackAvailable = __1.isVariant(marketType, 'spot') || __1.isAuctionComplete(node.order, slot);
|
|
381
381
|
if (crosses && fallbackAvailable) {
|
|
382
382
|
nodesToFill.push({
|
|
383
383
|
node: node,
|
|
@@ -390,7 +390,7 @@ class DLOB {
|
|
|
390
390
|
}
|
|
391
391
|
findExpiredNodesToFill(marketIndex, ts, marketType) {
|
|
392
392
|
const nodesToFill = new Array();
|
|
393
|
-
const marketTypeStr =
|
|
393
|
+
const marketTypeStr = __1.getVariant(marketType);
|
|
394
394
|
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
395
395
|
if (!nodeLists) {
|
|
396
396
|
return nodesToFill;
|
|
@@ -408,7 +408,7 @@ class DLOB {
|
|
|
408
408
|
];
|
|
409
409
|
for (const bidGenerator of bidGenerators) {
|
|
410
410
|
for (const bid of bidGenerator) {
|
|
411
|
-
if (
|
|
411
|
+
if (__1.isOrderExpired(bid.order, ts)) {
|
|
412
412
|
nodesToFill.push({
|
|
413
413
|
node: bid,
|
|
414
414
|
});
|
|
@@ -417,7 +417,7 @@ class DLOB {
|
|
|
417
417
|
}
|
|
418
418
|
for (const askGenerator of askGenerators) {
|
|
419
419
|
for (const ask of askGenerator) {
|
|
420
|
-
if (
|
|
420
|
+
if (__1.isOrderExpired(ask.order, ts)) {
|
|
421
421
|
nodesToFill.push({
|
|
422
422
|
node: ask,
|
|
423
423
|
});
|
|
@@ -430,14 +430,14 @@ class DLOB {
|
|
|
430
430
|
const nodesToFill = new Array();
|
|
431
431
|
// Then see if there are orders still in JIT auction
|
|
432
432
|
for (const marketBid of this.getMarketBids(marketIndex, marketType)) {
|
|
433
|
-
if (!
|
|
433
|
+
if (!__1.isAuctionComplete(marketBid.order, slot)) {
|
|
434
434
|
nodesToFill.push({
|
|
435
435
|
node: marketBid,
|
|
436
436
|
});
|
|
437
437
|
}
|
|
438
438
|
}
|
|
439
439
|
for (const marketAsk of this.getMarketAsks(marketIndex, marketType)) {
|
|
440
|
-
if (!
|
|
440
|
+
if (!__1.isAuctionComplete(marketAsk.order, slot)) {
|
|
441
441
|
nodesToFill.push({
|
|
442
442
|
node: marketAsk,
|
|
443
443
|
});
|
|
@@ -446,7 +446,7 @@ class DLOB {
|
|
|
446
446
|
return nodesToFill;
|
|
447
447
|
}
|
|
448
448
|
*getMarketBids(marketIndex, marketType) {
|
|
449
|
-
const marketTypeStr =
|
|
449
|
+
const marketTypeStr = __1.getVariant(marketType);
|
|
450
450
|
const orderLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
451
451
|
if (!orderLists) {
|
|
452
452
|
return;
|
|
@@ -460,7 +460,7 @@ class DLOB {
|
|
|
460
460
|
}
|
|
461
461
|
}
|
|
462
462
|
*getMarketAsks(marketIndex, marketType) {
|
|
463
|
-
const marketTypeStr =
|
|
463
|
+
const marketTypeStr = __1.getVariant(marketType);
|
|
464
464
|
const orderLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
465
465
|
if (!orderLists) {
|
|
466
466
|
return;
|
|
@@ -492,10 +492,10 @@ class DLOB {
|
|
|
492
492
|
const bestValue = bestGenerator.next.value;
|
|
493
493
|
const currentValue = currentGenerator.next.value;
|
|
494
494
|
// always return the market orders first
|
|
495
|
-
if (bestValue.order &&
|
|
495
|
+
if (bestValue.order && __1.isMarketOrder(bestValue.order)) {
|
|
496
496
|
return bestGenerator;
|
|
497
497
|
}
|
|
498
|
-
if (currentValue.order &&
|
|
498
|
+
if (currentValue.order && __1.isMarketOrder(currentValue.order)) {
|
|
499
499
|
return currentGenerator;
|
|
500
500
|
}
|
|
501
501
|
const bestPrice = bestValue.getPrice(oraclePriceData, slot);
|
|
@@ -519,10 +519,10 @@ class DLOB {
|
|
|
519
519
|
}
|
|
520
520
|
}
|
|
521
521
|
*getLimitAsks(marketIndex, slot, marketType, oraclePriceData) {
|
|
522
|
-
if (
|
|
522
|
+
if (__1.isVariant(marketType, 'spot') && !oraclePriceData) {
|
|
523
523
|
throw new Error('Must provide OraclePriceData to get spot asks');
|
|
524
524
|
}
|
|
525
|
-
const marketTypeStr =
|
|
525
|
+
const marketTypeStr = __1.getVariant(marketType);
|
|
526
526
|
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
527
527
|
if (!nodeLists) {
|
|
528
528
|
return;
|
|
@@ -536,27 +536,30 @@ class DLOB {
|
|
|
536
536
|
});
|
|
537
537
|
}
|
|
538
538
|
/**
|
|
539
|
-
* Filters the limit asks that are post only
|
|
540
|
-
*
|
|
539
|
+
* Filters the limit asks that are post only, have been place for sufficiently long or are above the fallback bid
|
|
540
|
+
* Market orders can only fill against orders that meet this criteria
|
|
541
541
|
*
|
|
542
542
|
* @returns
|
|
543
543
|
*/
|
|
544
|
-
*
|
|
544
|
+
*getMakerLimitAsks(marketIndex, slot, marketType, oraclePriceData, fallbackBid) {
|
|
545
545
|
for (const node of this.getLimitAsks(marketIndex, slot, marketType, oraclePriceData)) {
|
|
546
|
-
if (this.isRestingLimitOrder(node.order, slot
|
|
546
|
+
if (this.isRestingLimitOrder(node.order, slot)) {
|
|
547
|
+
yield node;
|
|
548
|
+
}
|
|
549
|
+
else if (fallbackBid &&
|
|
550
|
+
node.getPrice(oraclePriceData, slot).gt(fallbackBid)) {
|
|
547
551
|
yield node;
|
|
548
552
|
}
|
|
549
553
|
}
|
|
550
554
|
}
|
|
551
|
-
isRestingLimitOrder(order, slot
|
|
552
|
-
return
|
|
553
|
-
new __1.BN(slot).sub(order.slot).gte(new __1.BN(minPerpAuctionDuration * 1.5)));
|
|
555
|
+
isRestingLimitOrder(order, slot) {
|
|
556
|
+
return order.postOnly || new __1.BN(slot).sub(order.slot).gte(new __1.BN(15));
|
|
554
557
|
}
|
|
555
558
|
*getLimitBids(marketIndex, slot, marketType, oraclePriceData) {
|
|
556
|
-
if (
|
|
559
|
+
if (__1.isVariant(marketType, 'spot') && !oraclePriceData) {
|
|
557
560
|
throw new Error('Must provide OraclePriceData to get spot bids');
|
|
558
561
|
}
|
|
559
|
-
const marketTypeStr =
|
|
562
|
+
const marketTypeStr = __1.getVariant(marketType);
|
|
560
563
|
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
561
564
|
if (!nodeLists) {
|
|
562
565
|
return;
|
|
@@ -570,45 +573,49 @@ class DLOB {
|
|
|
570
573
|
});
|
|
571
574
|
}
|
|
572
575
|
/**
|
|
573
|
-
* Filters the limit bids that are post only
|
|
574
|
-
*
|
|
576
|
+
* Filters the limit bids that are post only, have been place for sufficiently long or are below the fallback ask
|
|
577
|
+
* Market orders can only fill against orders that meet this criteria
|
|
575
578
|
*
|
|
576
579
|
* @returns
|
|
577
580
|
*/
|
|
578
|
-
*
|
|
581
|
+
*getMakerLimitBids(marketIndex, slot, marketType, oraclePriceData, fallbackAsk) {
|
|
579
582
|
for (const node of this.getLimitBids(marketIndex, slot, marketType, oraclePriceData)) {
|
|
580
|
-
if (this.isRestingLimitOrder(node.order, slot
|
|
583
|
+
if (this.isRestingLimitOrder(node.order, slot)) {
|
|
584
|
+
yield node;
|
|
585
|
+
}
|
|
586
|
+
else if (fallbackAsk &&
|
|
587
|
+
node.getPrice(oraclePriceData, slot).lt(fallbackAsk)) {
|
|
581
588
|
yield node;
|
|
582
589
|
}
|
|
583
590
|
}
|
|
584
591
|
}
|
|
585
592
|
*getAsks(marketIndex, fallbackAsk, slot, marketType, oraclePriceData) {
|
|
586
|
-
if (
|
|
593
|
+
if (__1.isVariant(marketType, 'spot') && !oraclePriceData) {
|
|
587
594
|
throw new Error('Must provide OraclePriceData to get spot asks');
|
|
588
595
|
}
|
|
589
596
|
const generatorList = [
|
|
590
597
|
this.getMarketAsks(marketIndex, marketType),
|
|
591
598
|
this.getLimitAsks(marketIndex, slot, marketType, oraclePriceData),
|
|
592
599
|
];
|
|
593
|
-
const marketTypeStr =
|
|
600
|
+
const marketTypeStr = __1.getVariant(marketType);
|
|
594
601
|
if (marketTypeStr === 'perp' && fallbackAsk) {
|
|
595
|
-
generatorList.push(
|
|
602
|
+
generatorList.push(NodeList_1.getVammNodeGenerator(fallbackAsk));
|
|
596
603
|
}
|
|
597
604
|
yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestPrice, currentPrice) => {
|
|
598
605
|
return bestPrice.lt(currentPrice);
|
|
599
606
|
});
|
|
600
607
|
}
|
|
601
608
|
*getBids(marketIndex, fallbackBid, slot, marketType, oraclePriceData) {
|
|
602
|
-
if (
|
|
609
|
+
if (__1.isVariant(marketType, 'spot') && !oraclePriceData) {
|
|
603
610
|
throw new Error('Must provide OraclePriceData to get spot bids');
|
|
604
611
|
}
|
|
605
612
|
const generatorList = [
|
|
606
613
|
this.getMarketBids(marketIndex, marketType),
|
|
607
614
|
this.getLimitBids(marketIndex, slot, marketType, oraclePriceData),
|
|
608
615
|
];
|
|
609
|
-
const marketTypeStr =
|
|
616
|
+
const marketTypeStr = __1.getVariant(marketType);
|
|
610
617
|
if (marketTypeStr === 'perp' && fallbackBid) {
|
|
611
|
-
generatorList.push(
|
|
618
|
+
generatorList.push(NodeList_1.getVammNodeGenerator(fallbackBid));
|
|
612
619
|
}
|
|
613
620
|
yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestPrice, currentPrice) => {
|
|
614
621
|
return bestPrice.gt(currentPrice);
|
|
@@ -633,10 +640,10 @@ class DLOB {
|
|
|
633
640
|
}
|
|
634
641
|
const { takerNode, makerNode } = this.determineMakerAndTaker(askNode, bidNode);
|
|
635
642
|
// extra guard against bad fills for limit orders where auction is incomplete
|
|
636
|
-
if (!
|
|
643
|
+
if (!__1.isAuctionComplete(takerNode.order, slot)) {
|
|
637
644
|
let bidPrice;
|
|
638
645
|
let askPrice;
|
|
639
|
-
if (
|
|
646
|
+
if (__1.isVariant(takerNode.order.direction, 'long')) {
|
|
640
647
|
bidPrice = __1.BN.min(takerNode.getPrice(oraclePriceData, slot), fallbackAsk || __1.BN_MAX);
|
|
641
648
|
askPrice = makerNode.getPrice(oraclePriceData, slot);
|
|
642
649
|
}
|
|
@@ -708,11 +715,11 @@ class DLOB {
|
|
|
708
715
|
.value.getPrice(oraclePriceData, slot);
|
|
709
716
|
}
|
|
710
717
|
findNodesToTrigger(marketIndex, slot, oraclePrice, marketType, stateAccount) {
|
|
711
|
-
if (
|
|
718
|
+
if (exchangeStatus_1.exchangePaused(stateAccount)) {
|
|
712
719
|
return [];
|
|
713
720
|
}
|
|
714
721
|
const nodesToTrigger = [];
|
|
715
|
-
const marketTypeStr =
|
|
722
|
+
const marketTypeStr = __1.getVariant(marketType);
|
|
716
723
|
const marketNodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
717
724
|
const triggerAboveList = marketNodeLists
|
|
718
725
|
? marketNodeLists.trigger.above
|
|
@@ -720,7 +727,7 @@ class DLOB {
|
|
|
720
727
|
if (triggerAboveList) {
|
|
721
728
|
for (const node of triggerAboveList.getGenerator()) {
|
|
722
729
|
if (oraclePrice.gt(node.order.triggerPrice)) {
|
|
723
|
-
if (
|
|
730
|
+
if (__1.isAuctionComplete(node.order, slot)) {
|
|
724
731
|
nodesToTrigger.push({
|
|
725
732
|
node: node,
|
|
726
733
|
});
|
|
@@ -737,7 +744,7 @@ class DLOB {
|
|
|
737
744
|
if (triggerBelowList) {
|
|
738
745
|
for (const node of triggerBelowList.getGenerator()) {
|
|
739
746
|
if (oraclePrice.lt(node.order.triggerPrice)) {
|
|
740
|
-
if (
|
|
747
|
+
if (__1.isAuctionComplete(node.order, slot)) {
|
|
741
748
|
nodesToTrigger.push({
|
|
742
749
|
node: node,
|
|
743
750
|
});
|
|
@@ -751,46 +758,46 @@ class DLOB {
|
|
|
751
758
|
return nodesToTrigger;
|
|
752
759
|
}
|
|
753
760
|
printTopOfOrderLists(sdkConfig, driftClient, slotSubscriber, marketIndex, marketType) {
|
|
754
|
-
if (
|
|
761
|
+
if (__1.isVariant(marketType, 'perp')) {
|
|
755
762
|
const market = driftClient.getPerpMarketAccount(marketIndex);
|
|
756
763
|
const slot = slotSubscriber.getSlot();
|
|
757
764
|
const oraclePriceData = driftClient.getOracleDataForPerpMarket(marketIndex);
|
|
758
|
-
const fallbackAsk =
|
|
759
|
-
const fallbackBid =
|
|
765
|
+
const fallbackAsk = __1.calculateAskPrice(market, oraclePriceData);
|
|
766
|
+
const fallbackBid = __1.calculateBidPrice(market, oraclePriceData);
|
|
760
767
|
const bestAsk = this.getBestAsk(marketIndex, fallbackAsk, slot, marketType, oraclePriceData);
|
|
761
768
|
const bestBid = this.getBestBid(marketIndex, fallbackBid, slot, marketType, oraclePriceData);
|
|
762
769
|
const mid = bestAsk.add(bestBid).div(new __1.BN(2));
|
|
763
|
-
const bidSpread = (
|
|
764
|
-
|
|
770
|
+
const bidSpread = (__1.convertToNumber(bestBid, __1.PRICE_PRECISION) /
|
|
771
|
+
__1.convertToNumber(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
765
772
|
1) *
|
|
766
773
|
100.0;
|
|
767
|
-
const askSpread = (
|
|
768
|
-
|
|
774
|
+
const askSpread = (__1.convertToNumber(bestAsk, __1.PRICE_PRECISION) /
|
|
775
|
+
__1.convertToNumber(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
769
776
|
1) *
|
|
770
777
|
100.0;
|
|
771
778
|
console.log(`Market ${sdkConfig.MARKETS[marketIndex].symbol} Orders`);
|
|
772
|
-
console.log(` Ask`,
|
|
773
|
-
console.log(` Mid`,
|
|
774
|
-
console.log(` Bid`,
|
|
779
|
+
console.log(` Ask`, __1.convertToNumber(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
|
|
780
|
+
console.log(` Mid`, __1.convertToNumber(mid, __1.PRICE_PRECISION).toFixed(3));
|
|
781
|
+
console.log(` Bid`, __1.convertToNumber(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
|
|
775
782
|
}
|
|
776
|
-
else if (
|
|
783
|
+
else if (__1.isVariant(marketType, 'spot')) {
|
|
777
784
|
const slot = slotSubscriber.getSlot();
|
|
778
785
|
const oraclePriceData = driftClient.getOracleDataForPerpMarket(marketIndex);
|
|
779
786
|
const bestAsk = this.getBestAsk(marketIndex, undefined, slot, marketType, oraclePriceData);
|
|
780
787
|
const bestBid = this.getBestBid(marketIndex, undefined, slot, marketType, oraclePriceData);
|
|
781
788
|
const mid = bestAsk.add(bestBid).div(new __1.BN(2));
|
|
782
|
-
const bidSpread = (
|
|
783
|
-
|
|
789
|
+
const bidSpread = (__1.convertToNumber(bestBid, __1.PRICE_PRECISION) /
|
|
790
|
+
__1.convertToNumber(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
784
791
|
1) *
|
|
785
792
|
100.0;
|
|
786
|
-
const askSpread = (
|
|
787
|
-
|
|
793
|
+
const askSpread = (__1.convertToNumber(bestAsk, __1.PRICE_PRECISION) /
|
|
794
|
+
__1.convertToNumber(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
788
795
|
1) *
|
|
789
796
|
100.0;
|
|
790
797
|
console.log(`Market ${sdkConfig.MARKETS[marketIndex].symbol} Orders`);
|
|
791
|
-
console.log(` Ask`,
|
|
792
|
-
console.log(` Mid`,
|
|
793
|
-
console.log(` Bid`,
|
|
798
|
+
console.log(` Ask`, __1.convertToNumber(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
|
|
799
|
+
console.log(` Mid`, __1.convertToNumber(mid, __1.PRICE_PRECISION).toFixed(3));
|
|
800
|
+
console.log(` Bid`, __1.convertToNumber(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
|
|
794
801
|
}
|
|
795
802
|
}
|
|
796
803
|
getDLOBOrders() {
|
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 ${
|
|
17
|
-
msg += ` ${
|
|
18
|
-
msg += `${
|
|
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)}`;
|
|
19
19
|
if (this.order.price.gt(__1.ZERO)) {
|
|
20
|
-
msg += ` @ ${
|
|
20
|
+
msg += ` @ ${__1.convertToNumber(this.order.price, __1.PRICE_PRECISION).toFixed(3)}`;
|
|
21
21
|
}
|
|
22
22
|
if (this.order.triggerPrice.gt(__1.ZERO)) {
|
|
23
|
-
msg += ` ${
|
|
24
|
-
msg += ` ${
|
|
23
|
+
msg += ` ${__1.isVariant(this.order.triggerCondition, 'below') ? 'BELOW' : 'ABOVE'}`;
|
|
24
|
+
msg += ` ${__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
|
|
29
|
+
return __1.getLimitPrice(this.order, oraclePriceData, slot);
|
|
30
30
|
}
|
|
31
31
|
isBaseFilled() {
|
|
32
32
|
return this.order.baseAssetAmountFilled.eq(this.order.baseAssetAmount);
|
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 (
|
|
23
|
+
if (__1.isVariant(order.status, 'init')) {
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
|
-
const newNode =
|
|
26
|
+
const newNode = 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;
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="bn.js" />
|
|
3
3
|
import { AnchorProvider, BN, Program } from '@project-serum/anchor';
|
|
4
|
-
import { StateAccount, IWallet, PositionDirection, UserAccount, PerpMarketAccount, OrderParams, Order, SpotMarketAccount, SpotPosition, MakerInfo, TakerInfo, OptionalOrderParams, ReferrerInfo, MarketType, SerumV3FulfillmentConfigAccount } from './types';
|
|
4
|
+
import { StateAccount, IWallet, PositionDirection, UserAccount, PerpMarketAccount, OrderParams, Order, SpotMarketAccount, SpotPosition, MakerInfo, TakerInfo, OptionalOrderParams, ReferrerInfo, MarketType, TxParams, SerumV3FulfillmentConfigAccount } from './types';
|
|
5
5
|
import * as anchor from '@project-serum/anchor';
|
|
6
6
|
import { Connection, PublicKey, TransactionSignature, ConfirmOptions, Transaction, TransactionInstruction, AccountMeta, Signer } from '@solana/web3.js';
|
|
7
7
|
import { TokenFaucet } from './tokenFaucet';
|
|
@@ -188,7 +188,7 @@ export declare class DriftClient {
|
|
|
188
188
|
getFillPerpOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Pick<Order, 'marketIndex' | 'orderId'>, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
189
189
|
placeSpotOrder(orderParams: OptionalOrderParams): Promise<TransactionSignature>;
|
|
190
190
|
getPlaceSpotOrderIx(orderParams: OptionalOrderParams): Promise<TransactionInstruction>;
|
|
191
|
-
fillSpotOrder(userAccountPublicKey: PublicKey, user: UserAccount, order?: Order, fulfillmentConfig?: SerumV3FulfillmentConfigAccount, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionSignature>;
|
|
191
|
+
fillSpotOrder(userAccountPublicKey: PublicKey, user: UserAccount, order?: Order, fulfillmentConfig?: SerumV3FulfillmentConfigAccount, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<TransactionSignature>;
|
|
192
192
|
getFillSpotOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order?: Order, fulfillmentConfig?: SerumV3FulfillmentConfigAccount, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
193
193
|
addSpotFulfillmentAccounts(marketIndex: number, remainingAccounts: AccountMeta[], fulfillmentConfig?: SerumV3FulfillmentConfigAccount): void;
|
|
194
194
|
addSerumRemainingAccounts(marketIndex: number, remainingAccounts: AccountMeta[], fulfillmentConfig: SerumV3FulfillmentConfigAccount): void;
|
|
@@ -200,7 +200,7 @@ export declare class DriftClient {
|
|
|
200
200
|
getPlaceAndTakePerpOrderIx(orderParams: OptionalOrderParams, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
201
201
|
placeAndMakePerpOrder(orderParams: OptionalOrderParams, takerInfo: TakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionSignature>;
|
|
202
202
|
getPlaceAndMakePerpOrderIx(orderParams: OptionalOrderParams, takerInfo: TakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
203
|
-
placeAndTakeSpotOrder(orderParams: OptionalOrderParams, fulfillmentConfig?: SerumV3FulfillmentConfigAccount, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionSignature>;
|
|
203
|
+
placeAndTakeSpotOrder(orderParams: OptionalOrderParams, fulfillmentConfig?: SerumV3FulfillmentConfigAccount, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<TransactionSignature>;
|
|
204
204
|
getPlaceAndTakeSpotOrderIx(orderParams: OptionalOrderParams, fulfillmentConfig?: SerumV3FulfillmentConfigAccount, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
205
205
|
placeAndMakeSpotOrder(orderParams: OptionalOrderParams, takerInfo: TakerInfo, fulfillmentConfig?: SerumV3FulfillmentConfigAccount, referrerInfo?: ReferrerInfo): Promise<TransactionSignature>;
|
|
206
206
|
getPlaceAndMakeSpotOrderIx(orderParams: OptionalOrderParams, takerInfo: TakerInfo, fulfillmentConfig?: SerumV3FulfillmentConfigAccount, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|