@drift-labs/sdk 0.2.0-master.32 → 0.2.0-master.33
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 +2 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +2 -2
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +8 -8
- package/lib/accounts/types.d.ts +1 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +2 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +7 -7
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.d.ts +4 -4
- package/lib/addresses/pda.js +23 -22
- package/lib/admin.d.ts +30 -32
- package/lib/admin.js +111 -119
- package/lib/clearingHouse.d.ts +43 -34
- package/lib/clearingHouse.js +353 -193
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +26 -5
- package/lib/clearingHouseUser.js +151 -51
- package/lib/config.d.ts +2 -0
- package/lib/config.js +5 -1
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.js +3 -2
- package/lib/dlob/DLOB.d.ts +18 -11
- package/lib/dlob/DLOB.js +179 -107
- package/lib/dlob/DLOBNode.js +2 -10
- package/lib/dlob/NodeList.js +1 -1
- package/lib/events/eventSubscriber.d.ts +1 -0
- package/lib/events/eventSubscriber.js +11 -4
- package/lib/events/fetchLogs.d.ts +3 -1
- package/lib/events/fetchLogs.js +13 -5
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/events/types.d.ts +1 -1
- package/lib/events/webSocketLogProvider.js +1 -1
- package/lib/factory/bigNum.d.ts +5 -4
- package/lib/factory/bigNum.js +36 -6
- package/lib/idl/clearing_house.json +1527 -1242
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/math/amm.js +9 -9
- package/lib/math/exchangeStatus.d.ts +4 -0
- package/lib/math/exchangeStatus.js +18 -0
- package/lib/math/funding.js +10 -10
- package/lib/math/margin.js +6 -1
- package/lib/math/market.js +9 -9
- package/lib/math/orders.d.ts +7 -3
- package/lib/math/orders.js +39 -31
- package/lib/math/repeg.js +3 -3
- package/lib/math/spotBalance.js +3 -3
- package/lib/math/spotPosition.js +2 -2
- package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/serum/serumSubscriber.d.ts +4 -0
- package/lib/serum/serumSubscriber.js +16 -1
- package/lib/types.d.ts +60 -75
- package/lib/types.js +2 -1
- package/lib/userMap/userMap.d.ts +17 -1
- package/lib/userMap/userMap.js +12 -0
- package/lib/userName.d.ts +1 -0
- package/lib/userName.js +3 -2
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +5 -1
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +9 -9
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +8 -8
- package/src/addresses/marketAddresses.ts +2 -2
- package/src/addresses/pda.ts +20 -20
- package/src/admin.ts +246 -221
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +556 -236
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.ts +237 -87
- package/src/config.ts +8 -1
- package/src/constants/numericConstants.ts +5 -1
- package/src/dlob/DLOB.ts +257 -120
- package/src/dlob/DLOBNode.ts +2 -14
- package/src/dlob/NodeList.ts +1 -1
- package/src/events/eventList.js +77 -0
- package/src/events/eventSubscriber.ts +18 -4
- package/src/events/fetchLogs.ts +20 -5
- package/src/events/pollingLogProvider.ts +1 -1
- package/src/events/types.ts +2 -1
- package/src/events/webSocketLogProvider.ts +1 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/factory/bigNum.ts +59 -6
- package/src/idl/clearing_house.json +1527 -1242
- package/src/idl/pyth.json +98 -2
- package/src/index.ts +3 -0
- package/src/math/amm.ts +9 -9
- package/src/math/exchangeStatus.ts +31 -0
- package/src/math/funding.ts +20 -10
- package/src/math/margin.ts +7 -1
- package/src/math/market.ts +9 -9
- package/src/math/orders.ts +44 -29
- package/src/math/repeg.ts +3 -3
- package/src/math/spotBalance.ts +4 -4
- package/src/math/spotPosition.ts +2 -2
- package/src/serum/serumFulfillmentConfigMap.ts +26 -0
- package/src/serum/serumSubscriber.ts +20 -1
- package/src/token/index.js +38 -0
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/types.ts +65 -51
- package/src/userMap/userMap.ts +25 -1
- package/src/userName.ts +2 -1
- package/src/util/computeUnits.js +27 -0
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/tests/bn/test.ts +22 -1
- package/tests/dlob/helpers.ts +252 -81
- package/tests/dlob/test.ts +1040 -219
package/tests/dlob/test.ts
CHANGED
|
@@ -16,9 +16,18 @@ import {
|
|
|
16
16
|
OraclePriceData,
|
|
17
17
|
NodeToFill,
|
|
18
18
|
isOrderExpired,
|
|
19
|
+
Order,
|
|
20
|
+
isMarketOrder,
|
|
21
|
+
isLimitOrder,
|
|
19
22
|
} from '../../src';
|
|
20
23
|
|
|
21
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
mockPerpMarkets,
|
|
26
|
+
mockSpotMarkets,
|
|
27
|
+
mockStateAccount,
|
|
28
|
+
MockUserMap,
|
|
29
|
+
} from './helpers';
|
|
30
|
+
import { ZERO } from '../../lib';
|
|
22
31
|
|
|
23
32
|
function insertOrderToDLOB(
|
|
24
33
|
dlob: DLOB,
|
|
@@ -33,7 +42,7 @@ function insertOrderToDLOB(
|
|
|
33
42
|
auctionStartPrice: BN,
|
|
34
43
|
auctionEndPrice: BN,
|
|
35
44
|
slot?: BN,
|
|
36
|
-
|
|
45
|
+
maxTs = ZERO,
|
|
37
46
|
oraclePriceOffset = new BN(0)
|
|
38
47
|
) {
|
|
39
48
|
dlob.insertOrder(
|
|
@@ -41,7 +50,6 @@ function insertOrderToDLOB(
|
|
|
41
50
|
status: OrderStatus.OPEN,
|
|
42
51
|
orderType,
|
|
43
52
|
marketType,
|
|
44
|
-
ts: new BN(getMockTimestamp()),
|
|
45
53
|
slot: slot || new BN(1),
|
|
46
54
|
orderId,
|
|
47
55
|
userOrderId: 0,
|
|
@@ -51,7 +59,6 @@ function insertOrderToDLOB(
|
|
|
51
59
|
baseAssetAmountFilled: new BN(0),
|
|
52
60
|
quoteAssetAmount: new BN(0),
|
|
53
61
|
quoteAssetAmountFilled: new BN(0),
|
|
54
|
-
fee: new BN(0),
|
|
55
62
|
direction,
|
|
56
63
|
reduceOnly: false,
|
|
57
64
|
triggerPrice: new BN(0),
|
|
@@ -59,12 +66,12 @@ function insertOrderToDLOB(
|
|
|
59
66
|
triggered: false,
|
|
60
67
|
existingPositionDirection: PositionDirection.LONG,
|
|
61
68
|
postOnly: false,
|
|
62
|
-
immediateOrCancel:
|
|
63
|
-
oraclePriceOffset,
|
|
69
|
+
immediateOrCancel: false,
|
|
70
|
+
oraclePriceOffset: oraclePriceOffset.toNumber(),
|
|
64
71
|
auctionDuration: 10,
|
|
65
72
|
auctionStartPrice,
|
|
66
73
|
auctionEndPrice,
|
|
67
|
-
|
|
74
|
+
maxTs,
|
|
68
75
|
},
|
|
69
76
|
userAccount
|
|
70
77
|
);
|
|
@@ -85,7 +92,7 @@ function insertTriggerOrderToDLOB(
|
|
|
85
92
|
auctionStartPrice: BN,
|
|
86
93
|
auctionEndPrice: BN,
|
|
87
94
|
slot?: BN,
|
|
88
|
-
|
|
95
|
+
maxTs = ZERO,
|
|
89
96
|
oraclePriceOffset = new BN(0)
|
|
90
97
|
) {
|
|
91
98
|
dlob.insertOrder(
|
|
@@ -93,7 +100,6 @@ function insertTriggerOrderToDLOB(
|
|
|
93
100
|
status: OrderStatus.OPEN,
|
|
94
101
|
orderType,
|
|
95
102
|
marketType,
|
|
96
|
-
ts: new BN(getMockTimestamp()),
|
|
97
103
|
slot: slot || new BN(1),
|
|
98
104
|
orderId,
|
|
99
105
|
userOrderId: 0,
|
|
@@ -103,7 +109,6 @@ function insertTriggerOrderToDLOB(
|
|
|
103
109
|
baseAssetAmountFilled: new BN(0),
|
|
104
110
|
quoteAssetAmount: new BN(0),
|
|
105
111
|
quoteAssetAmountFilled: new BN(0),
|
|
106
|
-
fee: new BN(0),
|
|
107
112
|
direction,
|
|
108
113
|
reduceOnly: false,
|
|
109
114
|
triggerPrice,
|
|
@@ -112,11 +117,11 @@ function insertTriggerOrderToDLOB(
|
|
|
112
117
|
existingPositionDirection: PositionDirection.LONG,
|
|
113
118
|
postOnly: false,
|
|
114
119
|
immediateOrCancel: true,
|
|
115
|
-
oraclePriceOffset,
|
|
120
|
+
oraclePriceOffset: oraclePriceOffset.toNumber(),
|
|
116
121
|
auctionDuration: 10,
|
|
117
122
|
auctionStartPrice,
|
|
118
123
|
auctionEndPrice,
|
|
119
|
-
|
|
124
|
+
maxTs,
|
|
120
125
|
},
|
|
121
126
|
userAccount
|
|
122
127
|
);
|
|
@@ -130,8 +135,8 @@ function printOrderNode(
|
|
|
130
135
|
console.log(
|
|
131
136
|
` . vAMMNode? ${node.isVammNode()},\t${
|
|
132
137
|
node.order ? getVariant(node.order?.orderType) : '~'
|
|
133
|
-
} ${node.order ? getVariant(node.order?.direction) : '~'}\t,
|
|
134
|
-
node.order?.
|
|
138
|
+
} ${node.order ? getVariant(node.order?.direction) : '~'}\t, slot: ${
|
|
139
|
+
node.order?.slot.toString() || '~'
|
|
135
140
|
}, orderId: ${node.order?.orderId.toString() || '~'},\tnode.getPrice: ${
|
|
136
141
|
oracle ? node.getPrice(oracle, slot!) : '~'
|
|
137
142
|
}, node.price: ${node.order?.price.toString() || '~'}, priceOffset: ${
|
|
@@ -184,44 +189,62 @@ function printBookState(
|
|
|
184
189
|
|
|
185
190
|
function printCrossedNodes(n: NodeToFill, slot: number) {
|
|
186
191
|
console.log(
|
|
187
|
-
`Cross Found,
|
|
192
|
+
`Cross Found, takerExists: ${n.node.order !== undefined}, makerExists: ${
|
|
188
193
|
n.makerNode !== undefined
|
|
189
194
|
}`
|
|
190
195
|
);
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
196
|
+
console.log(
|
|
197
|
+
`node: (mkt: ${isMarketOrder(n.node.order)}, lim: ${isLimitOrder(
|
|
198
|
+
n.node.order
|
|
199
|
+
)})`
|
|
200
|
+
);
|
|
201
|
+
if (n.makerNode) {
|
|
194
202
|
console.log(
|
|
195
|
-
`
|
|
196
|
-
|
|
197
|
-
)}
|
|
203
|
+
`mkrnode: (mkt: ${isMarketOrder(n.makerNode.order)}, lim: ${isLimitOrder(
|
|
204
|
+
n.makerNode.order
|
|
205
|
+
)})`
|
|
198
206
|
);
|
|
199
207
|
}
|
|
208
|
+
|
|
209
|
+
const printOrder = (o: Order) => {
|
|
210
|
+
console.log(
|
|
211
|
+
` orderId: ${o.orderId}, ${getVariant(o.orderType)}, ${getVariant(
|
|
212
|
+
o.direction
|
|
213
|
+
)},\texpired: ${isOrderExpired(o, slot)}, postOnly: ${
|
|
214
|
+
o.postOnly
|
|
215
|
+
}, reduceOnly: ${
|
|
216
|
+
o.reduceOnly
|
|
217
|
+
}, price: ${o.price.toString()}, priceOffset: ${o.oraclePriceOffset.toString()}, baseAmtFileld: ${o.baseAssetAmountFilled.toString()}/${o.baseAssetAmount.toString()}`
|
|
218
|
+
);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
if (n.node.order) {
|
|
222
|
+
const t = n.node.order;
|
|
223
|
+
console.log(`Taker Order:`);
|
|
224
|
+
printOrder(t);
|
|
225
|
+
}
|
|
200
226
|
if (n.makerNode) {
|
|
201
227
|
if (n.makerNode.isVammNode()) {
|
|
202
228
|
console.log(` maker is vAMM node`);
|
|
203
229
|
} else {
|
|
204
230
|
const m = n.makerNode.order!;
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
` maker orderId: ${m.orderId}, ${getVariant(
|
|
208
|
-
m.orderType
|
|
209
|
-
)}, ${getVariant(
|
|
210
|
-
m.direction
|
|
211
|
-
)},\texpired: ${exp}, price: ${m.price.toString()}, priceOffset: ${m.oraclePriceOffset.toString()}, baseAmtFileld: ${m.baseAssetAmountFilled.toString()}/${m.baseAssetAmount.toString()}`
|
|
212
|
-
);
|
|
231
|
+
console.log(`Maker Order:`);
|
|
232
|
+
printOrder(m);
|
|
213
233
|
}
|
|
214
234
|
}
|
|
215
235
|
}
|
|
216
236
|
|
|
217
|
-
let mockTs = 1;
|
|
218
|
-
function getMockTimestamp(): number {
|
|
219
|
-
return mockTs++;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
237
|
describe('DLOB Tests', () => {
|
|
223
238
|
it('Fresh DLOB is empty', () => {
|
|
224
|
-
const
|
|
239
|
+
const mockUserMap = new MockUserMap();
|
|
240
|
+
|
|
241
|
+
const dlob = new DLOB(
|
|
242
|
+
mockPerpMarkets,
|
|
243
|
+
mockSpotMarkets,
|
|
244
|
+
mockStateAccount,
|
|
245
|
+
mockUserMap,
|
|
246
|
+
false
|
|
247
|
+
);
|
|
225
248
|
const vAsk = new BN(11);
|
|
226
249
|
const vBid = new BN(10);
|
|
227
250
|
const oracle = {
|
|
@@ -288,7 +311,31 @@ describe('DLOB Tests', () => {
|
|
|
288
311
|
it('Can clear DLOB', () => {
|
|
289
312
|
const vAsk = new BN(15);
|
|
290
313
|
const vBid = new BN(10);
|
|
291
|
-
|
|
314
|
+
|
|
315
|
+
const mockUserMap = new MockUserMap();
|
|
316
|
+
const user0 = Keypair.generate();
|
|
317
|
+
const user0Auth = Keypair.generate();
|
|
318
|
+
const user1 = Keypair.generate();
|
|
319
|
+
const user1Auth = Keypair.generate();
|
|
320
|
+
const user2 = Keypair.generate();
|
|
321
|
+
const user2Auth = Keypair.generate();
|
|
322
|
+
const user3 = Keypair.generate();
|
|
323
|
+
const user3Auth = Keypair.generate();
|
|
324
|
+
const user4 = Keypair.generate();
|
|
325
|
+
const user4Auth = Keypair.generate();
|
|
326
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
327
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
328
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, user2Auth.publicKey);
|
|
329
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, user3Auth.publicKey);
|
|
330
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, user4Auth.publicKey);
|
|
331
|
+
|
|
332
|
+
const dlob = new DLOB(
|
|
333
|
+
mockPerpMarkets,
|
|
334
|
+
mockSpotMarkets,
|
|
335
|
+
mockStateAccount,
|
|
336
|
+
mockUserMap,
|
|
337
|
+
false
|
|
338
|
+
);
|
|
292
339
|
const marketIndex = 0;
|
|
293
340
|
|
|
294
341
|
const slot = 12;
|
|
@@ -301,7 +348,7 @@ describe('DLOB Tests', () => {
|
|
|
301
348
|
|
|
302
349
|
insertOrderToDLOB(
|
|
303
350
|
dlob,
|
|
304
|
-
|
|
351
|
+
user0.publicKey,
|
|
305
352
|
OrderType.LIMIT,
|
|
306
353
|
MarketType.PERP,
|
|
307
354
|
0, // orderId
|
|
@@ -314,7 +361,7 @@ describe('DLOB Tests', () => {
|
|
|
314
361
|
);
|
|
315
362
|
insertOrderToDLOB(
|
|
316
363
|
dlob,
|
|
317
|
-
|
|
364
|
+
user1.publicKey,
|
|
318
365
|
OrderType.LIMIT,
|
|
319
366
|
MarketType.PERP,
|
|
320
367
|
1, // orderId
|
|
@@ -327,7 +374,7 @@ describe('DLOB Tests', () => {
|
|
|
327
374
|
);
|
|
328
375
|
insertOrderToDLOB(
|
|
329
376
|
dlob,
|
|
330
|
-
|
|
377
|
+
user2.publicKey,
|
|
331
378
|
OrderType.LIMIT,
|
|
332
379
|
MarketType.PERP,
|
|
333
380
|
2, // orderId
|
|
@@ -358,7 +405,6 @@ describe('DLOB Tests', () => {
|
|
|
358
405
|
const bids1 = dlob.getBids(marketIndex, vBid, 0, MarketType.PERP, oracle);
|
|
359
406
|
bids1.next();
|
|
360
407
|
} catch (e) {
|
|
361
|
-
console.error(e);
|
|
362
408
|
thrown = true;
|
|
363
409
|
}
|
|
364
410
|
expect(thrown, 'should throw after clearing').to.equal(true);
|
|
@@ -369,7 +415,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
369
415
|
it('Test proper bids', () => {
|
|
370
416
|
const vAsk = new BN(15);
|
|
371
417
|
const vBid = new BN(10);
|
|
372
|
-
const
|
|
418
|
+
const mockUserMap = new MockUserMap();
|
|
419
|
+
const dlob = new DLOB(
|
|
420
|
+
mockPerpMarkets,
|
|
421
|
+
mockSpotMarkets,
|
|
422
|
+
mockStateAccount,
|
|
423
|
+
mockUserMap,
|
|
424
|
+
false
|
|
425
|
+
);
|
|
373
426
|
const marketIndex = 0;
|
|
374
427
|
|
|
375
428
|
const slot = 12;
|
|
@@ -450,9 +503,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
450
503
|
if (t.isVamm) {
|
|
451
504
|
continue;
|
|
452
505
|
}
|
|
506
|
+
|
|
507
|
+
const user = Keypair.generate();
|
|
508
|
+
const userAuth = Keypair.generate();
|
|
509
|
+
mockUserMap.addUserAccountAuthority(user.publicKey, userAuth.publicKey);
|
|
510
|
+
|
|
453
511
|
insertOrderToDLOB(
|
|
454
512
|
dlob,
|
|
455
|
-
|
|
513
|
+
user.publicKey,
|
|
456
514
|
t.orderType || OrderType.LIMIT,
|
|
457
515
|
MarketType.PERP,
|
|
458
516
|
t.orderId || 0, // orderId
|
|
@@ -503,7 +561,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
503
561
|
it('Test proper bids on multiple markets', () => {
|
|
504
562
|
const vAsk = new BN(15);
|
|
505
563
|
const vBid = new BN(10);
|
|
506
|
-
const
|
|
564
|
+
const mockUserMap = new MockUserMap();
|
|
565
|
+
const dlob = new DLOB(
|
|
566
|
+
mockPerpMarkets,
|
|
567
|
+
mockSpotMarkets,
|
|
568
|
+
mockStateAccount,
|
|
569
|
+
mockUserMap,
|
|
570
|
+
false
|
|
571
|
+
);
|
|
507
572
|
const marketIndex0 = 0;
|
|
508
573
|
const marketIndex1 = 1;
|
|
509
574
|
|
|
@@ -584,9 +649,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
584
649
|
if (t.isVamm) {
|
|
585
650
|
continue;
|
|
586
651
|
}
|
|
652
|
+
|
|
653
|
+
const user = Keypair.generate();
|
|
654
|
+
const userAuth = Keypair.generate();
|
|
655
|
+
mockUserMap.addUserAccountAuthority(user.publicKey, userAuth.publicKey);
|
|
656
|
+
|
|
587
657
|
insertOrderToDLOB(
|
|
588
658
|
dlob,
|
|
589
|
-
|
|
659
|
+
user.publicKey,
|
|
590
660
|
t.orderType || OrderType.LIMIT,
|
|
591
661
|
MarketType.PERP,
|
|
592
662
|
t.orderId || 0, // orderId
|
|
@@ -646,7 +716,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
646
716
|
it('Test proper asks', () => {
|
|
647
717
|
const vAsk = new BN(15);
|
|
648
718
|
const vBid = new BN(10);
|
|
649
|
-
const
|
|
719
|
+
const mockUserMap = new MockUserMap();
|
|
720
|
+
const dlob = new DLOB(
|
|
721
|
+
mockPerpMarkets,
|
|
722
|
+
mockSpotMarkets,
|
|
723
|
+
mockStateAccount,
|
|
724
|
+
mockUserMap,
|
|
725
|
+
false
|
|
726
|
+
);
|
|
650
727
|
const marketIndex = 0;
|
|
651
728
|
|
|
652
729
|
const slot = 12;
|
|
@@ -727,9 +804,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
727
804
|
if (t.isVamm) {
|
|
728
805
|
continue;
|
|
729
806
|
}
|
|
807
|
+
|
|
808
|
+
const user = Keypair.generate();
|
|
809
|
+
const userAuth = Keypair.generate();
|
|
810
|
+
mockUserMap.addUserAccountAuthority(user.publicKey, userAuth.publicKey);
|
|
811
|
+
|
|
730
812
|
insertOrderToDLOB(
|
|
731
813
|
dlob,
|
|
732
|
-
|
|
814
|
+
user.publicKey,
|
|
733
815
|
t.orderType || OrderType.LIMIT,
|
|
734
816
|
MarketType.PERP,
|
|
735
817
|
t.orderId || 0, // orderId
|
|
@@ -780,7 +862,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
780
862
|
it('Test insert market orders', () => {
|
|
781
863
|
const vAsk = new BN(11);
|
|
782
864
|
const vBid = new BN(10);
|
|
783
|
-
const
|
|
865
|
+
const mockUserMap = new MockUserMap();
|
|
866
|
+
const dlob = new DLOB(
|
|
867
|
+
mockPerpMarkets,
|
|
868
|
+
mockSpotMarkets,
|
|
869
|
+
mockStateAccount,
|
|
870
|
+
mockUserMap,
|
|
871
|
+
false
|
|
872
|
+
);
|
|
784
873
|
const marketIndex = 0;
|
|
785
874
|
const oracle = {
|
|
786
875
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -791,9 +880,13 @@ describe('DLOB Perp Tests', () => {
|
|
|
791
880
|
|
|
792
881
|
// 3 mkt buys
|
|
793
882
|
for (let i = 0; i < 3; i++) {
|
|
883
|
+
const user = Keypair.generate();
|
|
884
|
+
const userAuth = Keypair.generate();
|
|
885
|
+
mockUserMap.addUserAccountAuthority(user.publicKey, userAuth.publicKey);
|
|
886
|
+
|
|
794
887
|
insertOrderToDLOB(
|
|
795
888
|
dlob,
|
|
796
|
-
|
|
889
|
+
user.publicKey,
|
|
797
890
|
OrderType.MARKET,
|
|
798
891
|
MarketType.PERP,
|
|
799
892
|
i + 1,
|
|
@@ -808,9 +901,13 @@ describe('DLOB Perp Tests', () => {
|
|
|
808
901
|
|
|
809
902
|
// 3 mkt sells
|
|
810
903
|
for (let i = 0; i < 3; i++) {
|
|
904
|
+
const user = Keypair.generate();
|
|
905
|
+
const userAuth = Keypair.generate();
|
|
906
|
+
mockUserMap.addUserAccountAuthority(user.publicKey, userAuth.publicKey);
|
|
907
|
+
|
|
811
908
|
insertOrderToDLOB(
|
|
812
909
|
dlob,
|
|
813
|
-
|
|
910
|
+
user.publicKey,
|
|
814
911
|
OrderType.MARKET,
|
|
815
912
|
MarketType.PERP,
|
|
816
913
|
i + 1,
|
|
@@ -877,11 +974,39 @@ describe('DLOB Perp Tests', () => {
|
|
|
877
974
|
confidence: new BN(1),
|
|
878
975
|
hasSufficientNumberOfDataPoints: true,
|
|
879
976
|
};
|
|
880
|
-
|
|
977
|
+
|
|
978
|
+
const mockUserMap = new MockUserMap();
|
|
979
|
+
const user0 = Keypair.generate();
|
|
980
|
+
const userAuth0 = Keypair.generate();
|
|
981
|
+
const user1 = Keypair.generate();
|
|
982
|
+
const userAuth1 = Keypair.generate();
|
|
983
|
+
const user2 = Keypair.generate();
|
|
984
|
+
const userAuth2 = Keypair.generate();
|
|
985
|
+
const user3 = Keypair.generate();
|
|
986
|
+
const userAuth3 = Keypair.generate();
|
|
987
|
+
const user4 = Keypair.generate();
|
|
988
|
+
const userAuth4 = Keypair.generate();
|
|
989
|
+
const user5 = Keypair.generate();
|
|
990
|
+
const userAuth5 = Keypair.generate();
|
|
991
|
+
|
|
992
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, userAuth0.publicKey);
|
|
993
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, userAuth1.publicKey);
|
|
994
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, userAuth2.publicKey);
|
|
995
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, userAuth3.publicKey);
|
|
996
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, userAuth4.publicKey);
|
|
997
|
+
mockUserMap.addUserAccountAuthority(user5.publicKey, userAuth5.publicKey);
|
|
998
|
+
|
|
999
|
+
const dlob = new DLOB(
|
|
1000
|
+
mockPerpMarkets,
|
|
1001
|
+
mockSpotMarkets,
|
|
1002
|
+
mockStateAccount,
|
|
1003
|
+
mockUserMap,
|
|
1004
|
+
false
|
|
1005
|
+
);
|
|
881
1006
|
const marketIndex = 0;
|
|
882
1007
|
insertOrderToDLOB(
|
|
883
1008
|
dlob,
|
|
884
|
-
|
|
1009
|
+
user0.publicKey,
|
|
885
1010
|
OrderType.LIMIT,
|
|
886
1011
|
MarketType.PERP,
|
|
887
1012
|
3, // orderId
|
|
@@ -895,7 +1020,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
895
1020
|
|
|
896
1021
|
insertOrderToDLOB(
|
|
897
1022
|
dlob,
|
|
898
|
-
|
|
1023
|
+
user1.publicKey,
|
|
899
1024
|
OrderType.LIMIT,
|
|
900
1025
|
MarketType.PERP,
|
|
901
1026
|
2,
|
|
@@ -909,7 +1034,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
909
1034
|
|
|
910
1035
|
insertOrderToDLOB(
|
|
911
1036
|
dlob,
|
|
912
|
-
|
|
1037
|
+
user2.publicKey,
|
|
913
1038
|
OrderType.LIMIT,
|
|
914
1039
|
MarketType.PERP,
|
|
915
1040
|
1, // orderId
|
|
@@ -923,7 +1048,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
923
1048
|
|
|
924
1049
|
insertOrderToDLOB(
|
|
925
1050
|
dlob,
|
|
926
|
-
|
|
1051
|
+
user3.publicKey,
|
|
927
1052
|
OrderType.LIMIT,
|
|
928
1053
|
MarketType.PERP,
|
|
929
1054
|
1, // orderId
|
|
@@ -937,7 +1062,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
937
1062
|
|
|
938
1063
|
insertOrderToDLOB(
|
|
939
1064
|
dlob,
|
|
940
|
-
|
|
1065
|
+
user4.publicKey,
|
|
941
1066
|
OrderType.LIMIT,
|
|
942
1067
|
MarketType.PERP,
|
|
943
1068
|
2, // orderId
|
|
@@ -951,7 +1076,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
951
1076
|
|
|
952
1077
|
insertOrderToDLOB(
|
|
953
1078
|
dlob,
|
|
954
|
-
|
|
1079
|
+
user5.publicKey,
|
|
955
1080
|
OrderType.LIMIT,
|
|
956
1081
|
MarketType.PERP,
|
|
957
1082
|
3, // orderId
|
|
@@ -1021,13 +1146,41 @@ describe('DLOB Perp Tests', () => {
|
|
|
1021
1146
|
confidence: new BN(1),
|
|
1022
1147
|
hasSufficientNumberOfDataPoints: true,
|
|
1023
1148
|
};
|
|
1024
|
-
|
|
1149
|
+
|
|
1150
|
+
const mockUserMap = new MockUserMap();
|
|
1151
|
+
const user0 = Keypair.generate();
|
|
1152
|
+
const userAuth0 = Keypair.generate();
|
|
1153
|
+
const user1 = Keypair.generate();
|
|
1154
|
+
const userAuth1 = Keypair.generate();
|
|
1155
|
+
const user2 = Keypair.generate();
|
|
1156
|
+
const userAuth2 = Keypair.generate();
|
|
1157
|
+
const user3 = Keypair.generate();
|
|
1158
|
+
const userAuth3 = Keypair.generate();
|
|
1159
|
+
const user4 = Keypair.generate();
|
|
1160
|
+
const userAuth4 = Keypair.generate();
|
|
1161
|
+
const user5 = Keypair.generate();
|
|
1162
|
+
const userAuth5 = Keypair.generate();
|
|
1163
|
+
|
|
1164
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, userAuth0.publicKey);
|
|
1165
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, userAuth1.publicKey);
|
|
1166
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, userAuth2.publicKey);
|
|
1167
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, userAuth3.publicKey);
|
|
1168
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, userAuth4.publicKey);
|
|
1169
|
+
mockUserMap.addUserAccountAuthority(user5.publicKey, userAuth5.publicKey);
|
|
1170
|
+
|
|
1171
|
+
const dlob = new DLOB(
|
|
1172
|
+
mockPerpMarkets,
|
|
1173
|
+
mockSpotMarkets,
|
|
1174
|
+
mockStateAccount,
|
|
1175
|
+
mockUserMap,
|
|
1176
|
+
false
|
|
1177
|
+
);
|
|
1025
1178
|
const marketIndex = 0;
|
|
1026
1179
|
|
|
1027
1180
|
// insert floating bids
|
|
1028
1181
|
insertOrderToDLOB(
|
|
1029
1182
|
dlob,
|
|
1030
|
-
|
|
1183
|
+
user0.publicKey,
|
|
1031
1184
|
OrderType.LIMIT,
|
|
1032
1185
|
MarketType.PERP,
|
|
1033
1186
|
1, // orderId
|
|
@@ -1038,12 +1191,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1038
1191
|
vBid,
|
|
1039
1192
|
vAsk,
|
|
1040
1193
|
new BN(slot),
|
|
1041
|
-
|
|
1194
|
+
ZERO, // TiF
|
|
1042
1195
|
new BN(-1).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
1043
1196
|
);
|
|
1044
1197
|
insertOrderToDLOB(
|
|
1045
1198
|
dlob,
|
|
1046
|
-
|
|
1199
|
+
user1.publicKey,
|
|
1047
1200
|
OrderType.LIMIT,
|
|
1048
1201
|
MarketType.PERP,
|
|
1049
1202
|
3, // orderId
|
|
@@ -1054,12 +1207,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1054
1207
|
vBid,
|
|
1055
1208
|
vAsk,
|
|
1056
1209
|
new BN(slot),
|
|
1057
|
-
|
|
1210
|
+
ZERO, // TiF
|
|
1058
1211
|
new BN(-3).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
1059
1212
|
);
|
|
1060
1213
|
insertOrderToDLOB(
|
|
1061
1214
|
dlob,
|
|
1062
|
-
|
|
1215
|
+
user2.publicKey,
|
|
1063
1216
|
OrderType.LIMIT,
|
|
1064
1217
|
MarketType.PERP,
|
|
1065
1218
|
2, // orderId
|
|
@@ -1070,14 +1223,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
1070
1223
|
vBid,
|
|
1071
1224
|
vAsk,
|
|
1072
1225
|
new BN(slot),
|
|
1073
|
-
|
|
1226
|
+
ZERO, // TiF
|
|
1074
1227
|
new BN(-2).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
1075
1228
|
);
|
|
1076
1229
|
|
|
1077
1230
|
// insert floating asks
|
|
1078
1231
|
insertOrderToDLOB(
|
|
1079
1232
|
dlob,
|
|
1080
|
-
|
|
1233
|
+
user3.publicKey,
|
|
1081
1234
|
OrderType.LIMIT,
|
|
1082
1235
|
MarketType.PERP,
|
|
1083
1236
|
5, // orderId
|
|
@@ -1088,12 +1241,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1088
1241
|
vAsk,
|
|
1089
1242
|
vBid,
|
|
1090
1243
|
new BN(slot),
|
|
1091
|
-
|
|
1244
|
+
ZERO, // TiF
|
|
1092
1245
|
new BN(2).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
1093
1246
|
);
|
|
1094
1247
|
insertOrderToDLOB(
|
|
1095
1248
|
dlob,
|
|
1096
|
-
|
|
1249
|
+
user4.publicKey,
|
|
1097
1250
|
OrderType.LIMIT,
|
|
1098
1251
|
MarketType.PERP,
|
|
1099
1252
|
6, // orderId
|
|
@@ -1104,12 +1257,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1104
1257
|
vAsk,
|
|
1105
1258
|
vBid,
|
|
1106
1259
|
new BN(slot),
|
|
1107
|
-
|
|
1260
|
+
ZERO, // TiF
|
|
1108
1261
|
new BN(3).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
1109
1262
|
);
|
|
1110
1263
|
insertOrderToDLOB(
|
|
1111
1264
|
dlob,
|
|
1112
|
-
|
|
1265
|
+
user5.publicKey,
|
|
1113
1266
|
OrderType.LIMIT,
|
|
1114
1267
|
MarketType.PERP,
|
|
1115
1268
|
4, // orderId
|
|
@@ -1120,7 +1273,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1120
1273
|
vAsk,
|
|
1121
1274
|
vBid,
|
|
1122
1275
|
new BN(slot),
|
|
1123
|
-
|
|
1276
|
+
ZERO, // TiF
|
|
1124
1277
|
new BN(1).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
1125
1278
|
);
|
|
1126
1279
|
|
|
@@ -1182,16 +1335,40 @@ describe('DLOB Perp Tests', () => {
|
|
|
1182
1335
|
expect(asks).to.equal(4); // vamm ask + 3 orders
|
|
1183
1336
|
});
|
|
1184
1337
|
|
|
1185
|
-
it('Test multiple market orders fill with multiple limit orders', () => {
|
|
1338
|
+
it('Test multiple market orders fill with multiple limit orders', async () => {
|
|
1186
1339
|
const vAsk = new BN(15);
|
|
1187
1340
|
const vBid = new BN(10);
|
|
1188
|
-
|
|
1341
|
+
|
|
1342
|
+
const mockUserMap = new MockUserMap();
|
|
1343
|
+
const user0 = Keypair.generate();
|
|
1344
|
+
const user0Auth = Keypair.generate();
|
|
1345
|
+
const user1 = Keypair.generate();
|
|
1346
|
+
const user1Auth = Keypair.generate();
|
|
1347
|
+
const user2 = Keypair.generate();
|
|
1348
|
+
const user2Auth = Keypair.generate();
|
|
1349
|
+
const user3 = Keypair.generate();
|
|
1350
|
+
const user3Auth = Keypair.generate();
|
|
1351
|
+
const user4 = Keypair.generate();
|
|
1352
|
+
const user4Auth = Keypair.generate();
|
|
1353
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
1354
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
1355
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, user2Auth.publicKey);
|
|
1356
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, user3Auth.publicKey);
|
|
1357
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, user4Auth.publicKey);
|
|
1358
|
+
|
|
1359
|
+
const dlob = new DLOB(
|
|
1360
|
+
mockPerpMarkets,
|
|
1361
|
+
mockSpotMarkets,
|
|
1362
|
+
mockStateAccount,
|
|
1363
|
+
mockUserMap,
|
|
1364
|
+
false
|
|
1365
|
+
);
|
|
1189
1366
|
const marketIndex = 0;
|
|
1190
1367
|
|
|
1191
1368
|
// insert some limit buys above vamm bid, below ask
|
|
1192
1369
|
insertOrderToDLOB(
|
|
1193
1370
|
dlob,
|
|
1194
|
-
|
|
1371
|
+
user0.publicKey,
|
|
1195
1372
|
OrderType.LIMIT,
|
|
1196
1373
|
MarketType.PERP,
|
|
1197
1374
|
1, // orderId
|
|
@@ -1204,7 +1381,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1204
1381
|
);
|
|
1205
1382
|
insertOrderToDLOB(
|
|
1206
1383
|
dlob,
|
|
1207
|
-
|
|
1384
|
+
user1.publicKey,
|
|
1208
1385
|
OrderType.LIMIT,
|
|
1209
1386
|
MarketType.PERP,
|
|
1210
1387
|
2, // orderId
|
|
@@ -1217,7 +1394,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1217
1394
|
);
|
|
1218
1395
|
insertOrderToDLOB(
|
|
1219
1396
|
dlob,
|
|
1220
|
-
|
|
1397
|
+
user2.publicKey,
|
|
1221
1398
|
OrderType.LIMIT,
|
|
1222
1399
|
MarketType.PERP,
|
|
1223
1400
|
3, // orderId
|
|
@@ -1246,7 +1423,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1246
1423
|
// place two market sell order eating 2 of the limit orders
|
|
1247
1424
|
insertOrderToDLOB(
|
|
1248
1425
|
dlob,
|
|
1249
|
-
|
|
1426
|
+
user3.publicKey,
|
|
1250
1427
|
OrderType.MARKET,
|
|
1251
1428
|
MarketType.PERP,
|
|
1252
1429
|
4, // orderId
|
|
@@ -1259,7 +1436,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1259
1436
|
);
|
|
1260
1437
|
insertOrderToDLOB(
|
|
1261
1438
|
dlob,
|
|
1262
|
-
|
|
1439
|
+
user4.publicKey,
|
|
1263
1440
|
OrderType.MARKET,
|
|
1264
1441
|
MarketType.PERP,
|
|
1265
1442
|
5, // orderId
|
|
@@ -1300,13 +1477,34 @@ describe('DLOB Perp Tests', () => {
|
|
|
1300
1477
|
it('Test one market orders fills two limit orders', () => {
|
|
1301
1478
|
const vAsk = new BN(15);
|
|
1302
1479
|
const vBid = new BN(10);
|
|
1303
|
-
|
|
1480
|
+
|
|
1481
|
+
const mockUserMap = new MockUserMap();
|
|
1482
|
+
const user0 = Keypair.generate();
|
|
1483
|
+
const user0Auth = Keypair.generate();
|
|
1484
|
+
const user1 = Keypair.generate();
|
|
1485
|
+
const user1Auth = Keypair.generate();
|
|
1486
|
+
const user2 = Keypair.generate();
|
|
1487
|
+
const user2Auth = Keypair.generate();
|
|
1488
|
+
const user3 = Keypair.generate();
|
|
1489
|
+
const user3Auth = Keypair.generate();
|
|
1490
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
1491
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
1492
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, user2Auth.publicKey);
|
|
1493
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, user3Auth.publicKey);
|
|
1494
|
+
|
|
1495
|
+
const dlob = new DLOB(
|
|
1496
|
+
mockPerpMarkets,
|
|
1497
|
+
mockSpotMarkets,
|
|
1498
|
+
mockStateAccount,
|
|
1499
|
+
mockUserMap,
|
|
1500
|
+
false
|
|
1501
|
+
);
|
|
1304
1502
|
const marketIndex = 0;
|
|
1305
1503
|
|
|
1306
1504
|
// insert some limit sells below vAMM ask, above bid
|
|
1307
1505
|
insertOrderToDLOB(
|
|
1308
1506
|
dlob,
|
|
1309
|
-
|
|
1507
|
+
user0.publicKey,
|
|
1310
1508
|
OrderType.LIMIT,
|
|
1311
1509
|
MarketType.PERP,
|
|
1312
1510
|
1, // orderId
|
|
@@ -1319,7 +1517,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1319
1517
|
);
|
|
1320
1518
|
insertOrderToDLOB(
|
|
1321
1519
|
dlob,
|
|
1322
|
-
|
|
1520
|
+
user1.publicKey,
|
|
1323
1521
|
OrderType.LIMIT,
|
|
1324
1522
|
MarketType.PERP,
|
|
1325
1523
|
2, // orderId
|
|
@@ -1332,7 +1530,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1332
1530
|
);
|
|
1333
1531
|
insertOrderToDLOB(
|
|
1334
1532
|
dlob,
|
|
1335
|
-
|
|
1533
|
+
user2.publicKey,
|
|
1336
1534
|
OrderType.LIMIT,
|
|
1337
1535
|
MarketType.PERP,
|
|
1338
1536
|
3, // orderId
|
|
@@ -1344,14 +1542,17 @@ describe('DLOB Perp Tests', () => {
|
|
|
1344
1542
|
vAsk
|
|
1345
1543
|
);
|
|
1346
1544
|
|
|
1545
|
+
// auction over
|
|
1546
|
+
const endSlot = 12;
|
|
1547
|
+
|
|
1347
1548
|
// should have no crossing orders
|
|
1348
1549
|
const nodesToFillBefore = dlob.findCrossingNodesToFill(
|
|
1349
1550
|
marketIndex,
|
|
1350
|
-
|
|
1551
|
+
endSlot,
|
|
1351
1552
|
MarketType.PERP,
|
|
1352
1553
|
{
|
|
1353
1554
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
1354
|
-
slot: new BN(
|
|
1555
|
+
slot: new BN(endSlot),
|
|
1355
1556
|
confidence: new BN(1),
|
|
1356
1557
|
hasSufficientNumberOfDataPoints: true,
|
|
1357
1558
|
}
|
|
@@ -1361,7 +1562,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1361
1562
|
// place one market buy order eating 2 of the limit orders
|
|
1362
1563
|
insertOrderToDLOB(
|
|
1363
1564
|
dlob,
|
|
1364
|
-
|
|
1565
|
+
user3.publicKey,
|
|
1365
1566
|
OrderType.MARKET,
|
|
1366
1567
|
MarketType.PERP,
|
|
1367
1568
|
4, // orderId
|
|
@@ -1375,34 +1576,70 @@ describe('DLOB Perp Tests', () => {
|
|
|
1375
1576
|
|
|
1376
1577
|
const nodesToFillAfter = dlob.findCrossingNodesToFill(
|
|
1377
1578
|
marketIndex,
|
|
1378
|
-
|
|
1579
|
+
endSlot,
|
|
1379
1580
|
MarketType.PERP,
|
|
1380
1581
|
{
|
|
1381
1582
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
1382
|
-
slot: new BN(
|
|
1583
|
+
slot: new BN(endSlot),
|
|
1383
1584
|
confidence: new BN(1),
|
|
1384
1585
|
hasSufficientNumberOfDataPoints: true,
|
|
1385
1586
|
}
|
|
1386
1587
|
);
|
|
1387
1588
|
console.log(`Filled nodes: ${nodesToFillAfter.length}`);
|
|
1388
1589
|
for (const n of nodesToFillAfter) {
|
|
1389
|
-
printCrossedNodes(n,
|
|
1590
|
+
printCrossedNodes(n, endSlot);
|
|
1390
1591
|
}
|
|
1391
1592
|
expect(nodesToFillAfter.length).to.equal(2);
|
|
1392
1593
|
|
|
1393
1594
|
// taker should fill completely with best maker
|
|
1394
|
-
expect(
|
|
1395
|
-
|
|
1595
|
+
expect(
|
|
1596
|
+
nodesToFillAfter[0].node.order?.orderId,
|
|
1597
|
+
'wrong taker orderId'
|
|
1598
|
+
).to.equal(4);
|
|
1599
|
+
expect(
|
|
1600
|
+
nodesToFillAfter[0].makerNode?.order?.orderId,
|
|
1601
|
+
'wrong maker orderId'
|
|
1602
|
+
).to.equal(3);
|
|
1396
1603
|
|
|
1397
1604
|
// taker should fill completely with second best maker
|
|
1398
|
-
expect(
|
|
1399
|
-
|
|
1605
|
+
expect(
|
|
1606
|
+
nodesToFillAfter[1].node.order?.orderId,
|
|
1607
|
+
'wrong taker orderId'
|
|
1608
|
+
).to.equal(4);
|
|
1609
|
+
expect(
|
|
1610
|
+
nodesToFillAfter[1].makerNode?.order?.orderId,
|
|
1611
|
+
'wrong maker orderId'
|
|
1612
|
+
).to.equal(2);
|
|
1400
1613
|
});
|
|
1401
1614
|
|
|
1402
1615
|
it('Test two market orders to fill one limit order', () => {
|
|
1403
1616
|
const vAsk = new BN(15);
|
|
1404
1617
|
const vBid = new BN(8);
|
|
1405
|
-
|
|
1618
|
+
|
|
1619
|
+
const mockUserMap = new MockUserMap();
|
|
1620
|
+
const user0 = Keypair.generate();
|
|
1621
|
+
const user0Auth = Keypair.generate();
|
|
1622
|
+
const user1 = Keypair.generate();
|
|
1623
|
+
const user1Auth = Keypair.generate();
|
|
1624
|
+
const user2 = Keypair.generate();
|
|
1625
|
+
const user2Auth = Keypair.generate();
|
|
1626
|
+
const user3 = Keypair.generate();
|
|
1627
|
+
const user3Auth = Keypair.generate();
|
|
1628
|
+
const user4 = Keypair.generate();
|
|
1629
|
+
const user4Auth = Keypair.generate();
|
|
1630
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
1631
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
1632
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, user2Auth.publicKey);
|
|
1633
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, user3Auth.publicKey);
|
|
1634
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, user4Auth.publicKey);
|
|
1635
|
+
|
|
1636
|
+
const dlob = new DLOB(
|
|
1637
|
+
mockPerpMarkets,
|
|
1638
|
+
mockSpotMarkets,
|
|
1639
|
+
mockStateAccount,
|
|
1640
|
+
mockUserMap,
|
|
1641
|
+
false
|
|
1642
|
+
);
|
|
1406
1643
|
const marketIndex = 0;
|
|
1407
1644
|
|
|
1408
1645
|
const slot = 12;
|
|
@@ -1416,7 +1653,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1416
1653
|
// insert some limit sells below vAMM ask, above bid
|
|
1417
1654
|
insertOrderToDLOB(
|
|
1418
1655
|
dlob,
|
|
1419
|
-
|
|
1656
|
+
user0.publicKey,
|
|
1420
1657
|
OrderType.LIMIT,
|
|
1421
1658
|
MarketType.PERP,
|
|
1422
1659
|
1, // orderId
|
|
@@ -1429,7 +1666,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1429
1666
|
);
|
|
1430
1667
|
insertOrderToDLOB(
|
|
1431
1668
|
dlob,
|
|
1432
|
-
|
|
1669
|
+
user1.publicKey,
|
|
1433
1670
|
OrderType.LIMIT,
|
|
1434
1671
|
MarketType.PERP,
|
|
1435
1672
|
2, // orderId
|
|
@@ -1442,7 +1679,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1442
1679
|
);
|
|
1443
1680
|
insertOrderToDLOB(
|
|
1444
1681
|
dlob,
|
|
1445
|
-
|
|
1682
|
+
user2.publicKey,
|
|
1446
1683
|
OrderType.LIMIT,
|
|
1447
1684
|
MarketType.PERP,
|
|
1448
1685
|
3, // orderId
|
|
@@ -1466,7 +1703,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1466
1703
|
// place two market buy orders to eat the best ask
|
|
1467
1704
|
insertOrderToDLOB(
|
|
1468
1705
|
dlob,
|
|
1469
|
-
|
|
1706
|
+
user3.publicKey,
|
|
1470
1707
|
OrderType.MARKET,
|
|
1471
1708
|
MarketType.PERP,
|
|
1472
1709
|
4, // orderId
|
|
@@ -1479,7 +1716,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1479
1716
|
);
|
|
1480
1717
|
insertOrderToDLOB(
|
|
1481
1718
|
dlob,
|
|
1482
|
-
|
|
1719
|
+
user4.publicKey,
|
|
1483
1720
|
OrderType.MARKET,
|
|
1484
1721
|
MarketType.PERP,
|
|
1485
1722
|
5, // orderId
|
|
@@ -1497,7 +1734,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1497
1734
|
MarketType.PERP,
|
|
1498
1735
|
oracle
|
|
1499
1736
|
);
|
|
1500
|
-
const mktNodes = dlob.
|
|
1737
|
+
const mktNodes = dlob.findExpiredNodesToFill(
|
|
1501
1738
|
marketIndex,
|
|
1502
1739
|
slot,
|
|
1503
1740
|
MarketType.PERP
|
|
@@ -1524,7 +1761,52 @@ describe('DLOB Perp Tests', () => {
|
|
|
1524
1761
|
it('Test trigger orders', () => {
|
|
1525
1762
|
const vAsk = new BN(15);
|
|
1526
1763
|
const vBid = new BN(8);
|
|
1527
|
-
|
|
1764
|
+
|
|
1765
|
+
const mockUserMap = new MockUserMap();
|
|
1766
|
+
const user0 = Keypair.generate();
|
|
1767
|
+
const user0Auth = Keypair.generate();
|
|
1768
|
+
const user1 = Keypair.generate();
|
|
1769
|
+
const user1Auth = Keypair.generate();
|
|
1770
|
+
const user2 = Keypair.generate();
|
|
1771
|
+
const user2Auth = Keypair.generate();
|
|
1772
|
+
const user3 = Keypair.generate();
|
|
1773
|
+
const user3Auth = Keypair.generate();
|
|
1774
|
+
const user4 = Keypair.generate();
|
|
1775
|
+
const user4Auth = Keypair.generate();
|
|
1776
|
+
const user5 = Keypair.generate();
|
|
1777
|
+
const user5Auth = Keypair.generate();
|
|
1778
|
+
const user6 = Keypair.generate();
|
|
1779
|
+
const user6Auth = Keypair.generate();
|
|
1780
|
+
const user7 = Keypair.generate();
|
|
1781
|
+
const user7Auth = Keypair.generate();
|
|
1782
|
+
const user8 = Keypair.generate();
|
|
1783
|
+
const user8Auth = Keypair.generate();
|
|
1784
|
+
const user9 = Keypair.generate();
|
|
1785
|
+
const user9Auth = Keypair.generate();
|
|
1786
|
+
const user10 = Keypair.generate();
|
|
1787
|
+
const user10Auth = Keypair.generate();
|
|
1788
|
+
const user11 = Keypair.generate();
|
|
1789
|
+
const user11Auth = Keypair.generate();
|
|
1790
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
1791
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
1792
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, user2Auth.publicKey);
|
|
1793
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, user3Auth.publicKey);
|
|
1794
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, user4Auth.publicKey);
|
|
1795
|
+
mockUserMap.addUserAccountAuthority(user5.publicKey, user5Auth.publicKey);
|
|
1796
|
+
mockUserMap.addUserAccountAuthority(user6.publicKey, user6Auth.publicKey);
|
|
1797
|
+
mockUserMap.addUserAccountAuthority(user7.publicKey, user7Auth.publicKey);
|
|
1798
|
+
mockUserMap.addUserAccountAuthority(user8.publicKey, user8Auth.publicKey);
|
|
1799
|
+
mockUserMap.addUserAccountAuthority(user9.publicKey, user9Auth.publicKey);
|
|
1800
|
+
mockUserMap.addUserAccountAuthority(user10.publicKey, user10Auth.publicKey);
|
|
1801
|
+
mockUserMap.addUserAccountAuthority(user11.publicKey, user11Auth.publicKey);
|
|
1802
|
+
|
|
1803
|
+
const dlob = new DLOB(
|
|
1804
|
+
mockPerpMarkets,
|
|
1805
|
+
mockSpotMarkets,
|
|
1806
|
+
mockStateAccount,
|
|
1807
|
+
mockUserMap,
|
|
1808
|
+
false
|
|
1809
|
+
);
|
|
1528
1810
|
const marketIndex = 0;
|
|
1529
1811
|
|
|
1530
1812
|
const slot = 20;
|
|
@@ -1541,7 +1823,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1541
1823
|
// should trigger limit buy with above condition
|
|
1542
1824
|
insertTriggerOrderToDLOB(
|
|
1543
1825
|
dlob,
|
|
1544
|
-
|
|
1826
|
+
user0.publicKey,
|
|
1545
1827
|
OrderType.TRIGGER_LIMIT,
|
|
1546
1828
|
MarketType.PERP,
|
|
1547
1829
|
1, //orderId
|
|
@@ -1557,7 +1839,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1557
1839
|
// should trigger limit sell with above condition
|
|
1558
1840
|
insertTriggerOrderToDLOB(
|
|
1559
1841
|
dlob,
|
|
1560
|
-
|
|
1842
|
+
user1.publicKey,
|
|
1561
1843
|
OrderType.TRIGGER_LIMIT,
|
|
1562
1844
|
MarketType.PERP,
|
|
1563
1845
|
2, //orderId
|
|
@@ -1573,7 +1855,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1573
1855
|
// should trigger market buy with above condition
|
|
1574
1856
|
insertTriggerOrderToDLOB(
|
|
1575
1857
|
dlob,
|
|
1576
|
-
|
|
1858
|
+
user2.publicKey,
|
|
1577
1859
|
OrderType.TRIGGER_MARKET,
|
|
1578
1860
|
MarketType.PERP,
|
|
1579
1861
|
3, //orderId
|
|
@@ -1589,7 +1871,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1589
1871
|
// should trigger market sell with above condition
|
|
1590
1872
|
insertTriggerOrderToDLOB(
|
|
1591
1873
|
dlob,
|
|
1592
|
-
|
|
1874
|
+
user3.publicKey,
|
|
1593
1875
|
OrderType.TRIGGER_MARKET,
|
|
1594
1876
|
MarketType.PERP,
|
|
1595
1877
|
4, //orderId
|
|
@@ -1605,7 +1887,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1605
1887
|
// should trigger limit buy with below condition
|
|
1606
1888
|
insertTriggerOrderToDLOB(
|
|
1607
1889
|
dlob,
|
|
1608
|
-
|
|
1890
|
+
user4.publicKey,
|
|
1609
1891
|
OrderType.TRIGGER_LIMIT,
|
|
1610
1892
|
MarketType.PERP,
|
|
1611
1893
|
5, //orderId
|
|
@@ -1621,7 +1903,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1621
1903
|
// should trigger limit sell with below condition
|
|
1622
1904
|
insertTriggerOrderToDLOB(
|
|
1623
1905
|
dlob,
|
|
1624
|
-
|
|
1906
|
+
user5.publicKey,
|
|
1625
1907
|
OrderType.TRIGGER_LIMIT,
|
|
1626
1908
|
MarketType.PERP,
|
|
1627
1909
|
6, //orderId
|
|
@@ -1637,7 +1919,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1637
1919
|
// should trigger market buy with below condition
|
|
1638
1920
|
insertTriggerOrderToDLOB(
|
|
1639
1921
|
dlob,
|
|
1640
|
-
|
|
1922
|
+
user6.publicKey,
|
|
1641
1923
|
OrderType.TRIGGER_MARKET,
|
|
1642
1924
|
MarketType.PERP,
|
|
1643
1925
|
7, //orderId
|
|
@@ -1653,7 +1935,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1653
1935
|
// should trigger market sell with below condition
|
|
1654
1936
|
insertTriggerOrderToDLOB(
|
|
1655
1937
|
dlob,
|
|
1656
|
-
|
|
1938
|
+
user7.publicKey,
|
|
1657
1939
|
OrderType.TRIGGER_MARKET,
|
|
1658
1940
|
MarketType.PERP,
|
|
1659
1941
|
8, //orderId
|
|
@@ -1670,7 +1952,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1670
1952
|
// should NOT trigger market sell with above condition
|
|
1671
1953
|
insertTriggerOrderToDLOB(
|
|
1672
1954
|
dlob,
|
|
1673
|
-
|
|
1955
|
+
user8.publicKey,
|
|
1674
1956
|
OrderType.TRIGGER_MARKET,
|
|
1675
1957
|
MarketType.PERP,
|
|
1676
1958
|
9, //orderId
|
|
@@ -1686,7 +1968,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1686
1968
|
// should NOT trigger market sell with below condition
|
|
1687
1969
|
insertTriggerOrderToDLOB(
|
|
1688
1970
|
dlob,
|
|
1689
|
-
|
|
1971
|
+
user9.publicKey,
|
|
1690
1972
|
OrderType.TRIGGER_MARKET,
|
|
1691
1973
|
MarketType.PERP,
|
|
1692
1974
|
10, //orderId
|
|
@@ -1702,7 +1984,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1702
1984
|
// should NOT trigger market buy with above condition
|
|
1703
1985
|
insertTriggerOrderToDLOB(
|
|
1704
1986
|
dlob,
|
|
1705
|
-
|
|
1987
|
+
user10.publicKey,
|
|
1706
1988
|
OrderType.TRIGGER_MARKET,
|
|
1707
1989
|
MarketType.PERP,
|
|
1708
1990
|
11, //orderId
|
|
@@ -1718,7 +2000,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1718
2000
|
// should NOT trigger market buy with below condition
|
|
1719
2001
|
insertTriggerOrderToDLOB(
|
|
1720
2002
|
dlob,
|
|
1721
|
-
|
|
2003
|
+
user11.publicKey,
|
|
1722
2004
|
OrderType.TRIGGER_MARKET,
|
|
1723
2005
|
MarketType.PERP,
|
|
1724
2006
|
12, //orderId
|
|
@@ -1748,16 +2030,32 @@ describe('DLOB Perp Tests', () => {
|
|
|
1748
2030
|
it('Test will return expired market orders to fill', () => {
|
|
1749
2031
|
const vAsk = new BN(15);
|
|
1750
2032
|
const vBid = new BN(8);
|
|
1751
|
-
|
|
2033
|
+
|
|
2034
|
+
const mockUserMap = new MockUserMap();
|
|
2035
|
+
const user0 = Keypair.generate();
|
|
2036
|
+
const user0Auth = Keypair.generate();
|
|
2037
|
+
const user1 = Keypair.generate();
|
|
2038
|
+
const user1Auth = Keypair.generate();
|
|
2039
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
2040
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
2041
|
+
|
|
2042
|
+
const dlob = new DLOB(
|
|
2043
|
+
mockPerpMarkets,
|
|
2044
|
+
mockSpotMarkets,
|
|
2045
|
+
mockStateAccount,
|
|
2046
|
+
mockUserMap,
|
|
2047
|
+
false
|
|
2048
|
+
);
|
|
1752
2049
|
const marketIndex = 0;
|
|
1753
2050
|
|
|
1754
2051
|
const slot = 20;
|
|
1755
|
-
const
|
|
2052
|
+
const ts = 20;
|
|
2053
|
+
const maxTs = new BN(30);
|
|
1756
2054
|
|
|
1757
2055
|
// non crossing bid
|
|
1758
2056
|
insertOrderToDLOB(
|
|
1759
2057
|
dlob,
|
|
1760
|
-
|
|
2058
|
+
user0.publicKey,
|
|
1761
2059
|
OrderType.MARKET,
|
|
1762
2060
|
MarketType.PERP,
|
|
1763
2061
|
255, // orderId
|
|
@@ -1768,11 +2066,11 @@ describe('DLOB Perp Tests', () => {
|
|
|
1768
2066
|
vBid,
|
|
1769
2067
|
vAsk,
|
|
1770
2068
|
new BN(slot),
|
|
1771
|
-
|
|
2069
|
+
maxTs
|
|
1772
2070
|
);
|
|
1773
2071
|
insertOrderToDLOB(
|
|
1774
2072
|
dlob,
|
|
1775
|
-
|
|
2073
|
+
user1.publicKey,
|
|
1776
2074
|
OrderType.MARKET,
|
|
1777
2075
|
MarketType.PERP,
|
|
1778
2076
|
2, // orderId
|
|
@@ -1783,16 +2081,18 @@ describe('DLOB Perp Tests', () => {
|
|
|
1783
2081
|
vAsk,
|
|
1784
2082
|
vBid,
|
|
1785
2083
|
new BN(slot),
|
|
1786
|
-
|
|
2084
|
+
maxTs
|
|
1787
2085
|
);
|
|
1788
2086
|
|
|
1789
2087
|
// order auction is not yet complete, and order is not expired.
|
|
1790
2088
|
const slot0 = slot;
|
|
2089
|
+
const ts0 = ts;
|
|
1791
2090
|
const nodesToFillBefore = dlob.findNodesToFill(
|
|
1792
2091
|
marketIndex,
|
|
1793
2092
|
vBid,
|
|
1794
2093
|
vAsk,
|
|
1795
2094
|
slot0,
|
|
2095
|
+
ts0,
|
|
1796
2096
|
MarketType.PERP,
|
|
1797
2097
|
{
|
|
1798
2098
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -1804,12 +2104,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
1804
2104
|
expect(nodesToFillBefore.length).to.equal(0);
|
|
1805
2105
|
|
|
1806
2106
|
// should get order to fill after timeInForce
|
|
1807
|
-
const slot1 = slot0 +
|
|
2107
|
+
const slot1 = slot0 + 20;
|
|
2108
|
+
const ts1 = ts0 + 20; // overshoots expiry
|
|
1808
2109
|
const nodesToFillAfter = dlob.findNodesToFill(
|
|
1809
2110
|
marketIndex,
|
|
1810
2111
|
vBid,
|
|
1811
2112
|
vAsk,
|
|
1812
2113
|
slot1, // auction is over, and order ix expired
|
|
2114
|
+
ts1,
|
|
1813
2115
|
MarketType.PERP,
|
|
1814
2116
|
{
|
|
1815
2117
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -1832,10 +2134,35 @@ describe('DLOB Perp Tests', () => {
|
|
|
1832
2134
|
it('Test skips vAMM and fills market buy order with floating limit order during auction', () => {
|
|
1833
2135
|
const vAsk = new BN(15).mul(PRICE_PRECISION);
|
|
1834
2136
|
const vBid = new BN(8).mul(PRICE_PRECISION);
|
|
1835
|
-
|
|
2137
|
+
|
|
2138
|
+
const mockUserMap = new MockUserMap();
|
|
2139
|
+
const user0 = Keypair.generate();
|
|
2140
|
+
const user0Auth = Keypair.generate();
|
|
2141
|
+
const user1 = Keypair.generate();
|
|
2142
|
+
const user1Auth = Keypair.generate();
|
|
2143
|
+
const user2 = Keypair.generate();
|
|
2144
|
+
const user2Auth = Keypair.generate();
|
|
2145
|
+
const user3 = Keypair.generate();
|
|
2146
|
+
const user3Auth = Keypair.generate();
|
|
2147
|
+
const user4 = Keypair.generate();
|
|
2148
|
+
const user4Auth = Keypair.generate();
|
|
2149
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
2150
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
2151
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, user2Auth.publicKey);
|
|
2152
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, user3Auth.publicKey);
|
|
2153
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, user4Auth.publicKey);
|
|
2154
|
+
|
|
2155
|
+
const dlob = new DLOB(
|
|
2156
|
+
mockPerpMarkets,
|
|
2157
|
+
mockSpotMarkets,
|
|
2158
|
+
mockStateAccount,
|
|
2159
|
+
mockUserMap,
|
|
2160
|
+
false
|
|
2161
|
+
);
|
|
1836
2162
|
const marketIndex = 0;
|
|
1837
2163
|
|
|
1838
2164
|
const slot = 12;
|
|
2165
|
+
const ts = 12;
|
|
1839
2166
|
const oracle = {
|
|
1840
2167
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
1841
2168
|
slot: new BN(slot),
|
|
@@ -1846,7 +2173,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1846
2173
|
// insert some floating limit sells above vAMM ask
|
|
1847
2174
|
insertOrderToDLOB(
|
|
1848
2175
|
dlob,
|
|
1849
|
-
|
|
2176
|
+
user0.publicKey,
|
|
1850
2177
|
OrderType.LIMIT,
|
|
1851
2178
|
MarketType.PERP,
|
|
1852
2179
|
1, // orderId
|
|
@@ -1857,12 +2184,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1857
2184
|
vBid,
|
|
1858
2185
|
vAsk,
|
|
1859
2186
|
new BN(slot),
|
|
1860
|
-
|
|
2187
|
+
ZERO,
|
|
1861
2188
|
new BN(1).mul(PRICE_PRECISION)
|
|
1862
2189
|
);
|
|
1863
2190
|
insertOrderToDLOB(
|
|
1864
2191
|
dlob,
|
|
1865
|
-
|
|
2192
|
+
user1.publicKey,
|
|
1866
2193
|
OrderType.LIMIT,
|
|
1867
2194
|
MarketType.PERP,
|
|
1868
2195
|
2, // orderId
|
|
@@ -1873,12 +2200,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1873
2200
|
vBid,
|
|
1874
2201
|
vAsk,
|
|
1875
2202
|
new BN(slot),
|
|
1876
|
-
|
|
2203
|
+
ZERO,
|
|
1877
2204
|
new BN(1).mul(PRICE_PRECISION)
|
|
1878
2205
|
);
|
|
1879
2206
|
insertOrderToDLOB(
|
|
1880
2207
|
dlob,
|
|
1881
|
-
|
|
2208
|
+
user2.publicKey,
|
|
1882
2209
|
OrderType.LIMIT,
|
|
1883
2210
|
MarketType.PERP,
|
|
1884
2211
|
3, // orderId
|
|
@@ -1889,7 +2216,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1889
2216
|
vBid,
|
|
1890
2217
|
vAsk,
|
|
1891
2218
|
new BN(slot),
|
|
1892
|
-
|
|
2219
|
+
ZERO,
|
|
1893
2220
|
new BN(1).mul(PRICE_PRECISION)
|
|
1894
2221
|
);
|
|
1895
2222
|
|
|
@@ -1899,6 +2226,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1899
2226
|
vBid,
|
|
1900
2227
|
vAsk,
|
|
1901
2228
|
slot,
|
|
2229
|
+
ts,
|
|
1902
2230
|
MarketType.PERP,
|
|
1903
2231
|
{
|
|
1904
2232
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -1912,7 +2240,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1912
2240
|
// place two market buy orders to eat the best ask
|
|
1913
2241
|
insertOrderToDLOB(
|
|
1914
2242
|
dlob,
|
|
1915
|
-
|
|
2243
|
+
user3.publicKey,
|
|
1916
2244
|
OrderType.MARKET,
|
|
1917
2245
|
MarketType.PERP,
|
|
1918
2246
|
4, // orderId
|
|
@@ -1925,7 +2253,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1925
2253
|
);
|
|
1926
2254
|
insertOrderToDLOB(
|
|
1927
2255
|
dlob,
|
|
1928
|
-
|
|
2256
|
+
user4.publicKey,
|
|
1929
2257
|
OrderType.MARKET,
|
|
1930
2258
|
MarketType.PERP,
|
|
1931
2259
|
5, // orderId
|
|
@@ -1942,6 +2270,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1942
2270
|
vBid,
|
|
1943
2271
|
vAsk,
|
|
1944
2272
|
slot,
|
|
2273
|
+
ts,
|
|
1945
2274
|
MarketType.PERP,
|
|
1946
2275
|
{
|
|
1947
2276
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -1950,7 +2279,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1950
2279
|
hasSufficientNumberOfDataPoints: true,
|
|
1951
2280
|
}
|
|
1952
2281
|
);
|
|
1953
|
-
const mktNodes = dlob.
|
|
2282
|
+
const mktNodes = dlob.findExpiredNodesToFill(
|
|
1954
2283
|
marketIndex,
|
|
1955
2284
|
slot,
|
|
1956
2285
|
MarketType.PERP
|
|
@@ -1981,10 +2310,35 @@ describe('DLOB Perp Tests', () => {
|
|
|
1981
2310
|
it('Test fills market buy order with better priced vAMM after auction', () => {
|
|
1982
2311
|
const vAsk = new BN(15).mul(PRICE_PRECISION);
|
|
1983
2312
|
const vBid = new BN(8).mul(PRICE_PRECISION);
|
|
1984
|
-
|
|
2313
|
+
|
|
2314
|
+
const mockUserMap = new MockUserMap();
|
|
2315
|
+
const user0 = Keypair.generate();
|
|
2316
|
+
const user0Auth = Keypair.generate();
|
|
2317
|
+
const user1 = Keypair.generate();
|
|
2318
|
+
const user1Auth = Keypair.generate();
|
|
2319
|
+
const user2 = Keypair.generate();
|
|
2320
|
+
const user2Auth = Keypair.generate();
|
|
2321
|
+
const user3 = Keypair.generate();
|
|
2322
|
+
const user3Auth = Keypair.generate();
|
|
2323
|
+
const user4 = Keypair.generate();
|
|
2324
|
+
const user4Auth = Keypair.generate();
|
|
2325
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
2326
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
2327
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, user2Auth.publicKey);
|
|
2328
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, user3Auth.publicKey);
|
|
2329
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, user4Auth.publicKey);
|
|
2330
|
+
|
|
2331
|
+
const dlob = new DLOB(
|
|
2332
|
+
mockPerpMarkets,
|
|
2333
|
+
mockSpotMarkets,
|
|
2334
|
+
mockStateAccount,
|
|
2335
|
+
mockUserMap,
|
|
2336
|
+
false
|
|
2337
|
+
);
|
|
1985
2338
|
const marketIndex = 0;
|
|
1986
2339
|
|
|
1987
2340
|
const slot = 12;
|
|
2341
|
+
const ts = 12;
|
|
1988
2342
|
const oracle = {
|
|
1989
2343
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
1990
2344
|
slot: new BN(slot),
|
|
@@ -1995,7 +2349,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1995
2349
|
// insert some floating limit sells above vAMM ask
|
|
1996
2350
|
insertOrderToDLOB(
|
|
1997
2351
|
dlob,
|
|
1998
|
-
|
|
2352
|
+
user0.publicKey,
|
|
1999
2353
|
OrderType.LIMIT,
|
|
2000
2354
|
MarketType.PERP,
|
|
2001
2355
|
1, // orderId
|
|
@@ -2006,12 +2360,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
2006
2360
|
vBid,
|
|
2007
2361
|
vAsk,
|
|
2008
2362
|
new BN(slot),
|
|
2009
|
-
|
|
2363
|
+
ZERO,
|
|
2010
2364
|
new BN(3).mul(PRICE_PRECISION)
|
|
2011
2365
|
);
|
|
2012
2366
|
insertOrderToDLOB(
|
|
2013
2367
|
dlob,
|
|
2014
|
-
|
|
2368
|
+
user1.publicKey,
|
|
2015
2369
|
OrderType.LIMIT,
|
|
2016
2370
|
MarketType.PERP,
|
|
2017
2371
|
2, // orderId
|
|
@@ -2022,12 +2376,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
2022
2376
|
vBid,
|
|
2023
2377
|
vAsk,
|
|
2024
2378
|
new BN(slot),
|
|
2025
|
-
|
|
2379
|
+
ZERO,
|
|
2026
2380
|
new BN(4).mul(PRICE_PRECISION)
|
|
2027
2381
|
);
|
|
2028
2382
|
insertOrderToDLOB(
|
|
2029
2383
|
dlob,
|
|
2030
|
-
|
|
2384
|
+
user2.publicKey,
|
|
2031
2385
|
OrderType.LIMIT,
|
|
2032
2386
|
MarketType.PERP,
|
|
2033
2387
|
3, // orderId
|
|
@@ -2038,12 +2392,13 @@ describe('DLOB Perp Tests', () => {
|
|
|
2038
2392
|
vBid,
|
|
2039
2393
|
vAsk,
|
|
2040
2394
|
new BN(slot),
|
|
2041
|
-
|
|
2395
|
+
ZERO,
|
|
2042
2396
|
new BN(5).mul(PRICE_PRECISION)
|
|
2043
2397
|
);
|
|
2044
2398
|
|
|
2045
2399
|
// should have no crossing orders
|
|
2046
2400
|
const auctionOverSlot = slot * 10;
|
|
2401
|
+
const auctionOverTs = ts * 10;
|
|
2047
2402
|
const nodesToFillBefore = dlob.findCrossingNodesToFill(
|
|
2048
2403
|
marketIndex,
|
|
2049
2404
|
auctionOverSlot, // auction over
|
|
@@ -2055,7 +2410,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2055
2410
|
// place two market buy orders
|
|
2056
2411
|
insertOrderToDLOB(
|
|
2057
2412
|
dlob,
|
|
2058
|
-
|
|
2413
|
+
user3.publicKey,
|
|
2059
2414
|
OrderType.MARKET,
|
|
2060
2415
|
MarketType.PERP,
|
|
2061
2416
|
4, // orderId
|
|
@@ -2066,11 +2421,11 @@ describe('DLOB Perp Tests', () => {
|
|
|
2066
2421
|
vBid,
|
|
2067
2422
|
vAsk,
|
|
2068
2423
|
new BN(slot),
|
|
2069
|
-
|
|
2424
|
+
ZERO
|
|
2070
2425
|
);
|
|
2071
2426
|
insertOrderToDLOB(
|
|
2072
2427
|
dlob,
|
|
2073
|
-
|
|
2428
|
+
user4.publicKey,
|
|
2074
2429
|
OrderType.MARKET,
|
|
2075
2430
|
MarketType.PERP,
|
|
2076
2431
|
5, // orderId
|
|
@@ -2081,14 +2436,15 @@ describe('DLOB Perp Tests', () => {
|
|
|
2081
2436
|
vBid,
|
|
2082
2437
|
vAsk,
|
|
2083
2438
|
new BN(slot),
|
|
2084
|
-
|
|
2439
|
+
ZERO
|
|
2085
2440
|
);
|
|
2086
2441
|
|
|
2087
2442
|
const nodesToFillAfter = dlob.findNodesToFill(
|
|
2088
2443
|
marketIndex,
|
|
2089
2444
|
vBid,
|
|
2090
2445
|
vAsk,
|
|
2091
|
-
|
|
2446
|
+
auctionOverTs, // auction in progress
|
|
2447
|
+
ts,
|
|
2092
2448
|
MarketType.PERP,
|
|
2093
2449
|
oracle
|
|
2094
2450
|
);
|
|
@@ -2111,7 +2467,31 @@ describe('DLOB Perp Tests', () => {
|
|
|
2111
2467
|
it('Test skips vAMM and fills market sell order with floating limit buys during auction', () => {
|
|
2112
2468
|
const vAsk = new BN(15).mul(PRICE_PRECISION);
|
|
2113
2469
|
const vBid = new BN(8).mul(PRICE_PRECISION);
|
|
2114
|
-
|
|
2470
|
+
|
|
2471
|
+
const mockUserMap = new MockUserMap();
|
|
2472
|
+
const user0 = Keypair.generate();
|
|
2473
|
+
const user0Auth = Keypair.generate();
|
|
2474
|
+
const user1 = Keypair.generate();
|
|
2475
|
+
const user1Auth = Keypair.generate();
|
|
2476
|
+
const user2 = Keypair.generate();
|
|
2477
|
+
const user2Auth = Keypair.generate();
|
|
2478
|
+
const user3 = Keypair.generate();
|
|
2479
|
+
const user3Auth = Keypair.generate();
|
|
2480
|
+
const user4 = Keypair.generate();
|
|
2481
|
+
const user4Auth = Keypair.generate();
|
|
2482
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
2483
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
2484
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, user2Auth.publicKey);
|
|
2485
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, user3Auth.publicKey);
|
|
2486
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, user4Auth.publicKey);
|
|
2487
|
+
|
|
2488
|
+
const dlob = new DLOB(
|
|
2489
|
+
mockPerpMarkets,
|
|
2490
|
+
mockSpotMarkets,
|
|
2491
|
+
mockStateAccount,
|
|
2492
|
+
mockUserMap,
|
|
2493
|
+
false
|
|
2494
|
+
);
|
|
2115
2495
|
const marketIndex = 0;
|
|
2116
2496
|
|
|
2117
2497
|
const slot = 12;
|
|
@@ -2125,7 +2505,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2125
2505
|
// insert some floating limit buy below vAMM bid
|
|
2126
2506
|
insertOrderToDLOB(
|
|
2127
2507
|
dlob,
|
|
2128
|
-
|
|
2508
|
+
user0.publicKey,
|
|
2129
2509
|
OrderType.LIMIT,
|
|
2130
2510
|
MarketType.PERP,
|
|
2131
2511
|
2, // orderId
|
|
@@ -2136,12 +2516,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
2136
2516
|
vBid,
|
|
2137
2517
|
vAsk,
|
|
2138
2518
|
new BN(slot),
|
|
2139
|
-
|
|
2519
|
+
ZERO,
|
|
2140
2520
|
new BN(-4).mul(PRICE_PRECISION) // second best bid, but worse than vBid (8): 7.5
|
|
2141
2521
|
);
|
|
2142
2522
|
insertOrderToDLOB(
|
|
2143
2523
|
dlob,
|
|
2144
|
-
|
|
2524
|
+
user1.publicKey,
|
|
2145
2525
|
OrderType.LIMIT,
|
|
2146
2526
|
MarketType.PERP,
|
|
2147
2527
|
3, // orderId
|
|
@@ -2152,12 +2532,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
2152
2532
|
vBid,
|
|
2153
2533
|
vAsk,
|
|
2154
2534
|
new BN(slot),
|
|
2155
|
-
|
|
2535
|
+
ZERO,
|
|
2156
2536
|
new BN(-1).mul(PRICE_PRECISION) // best price: 10.5
|
|
2157
2537
|
);
|
|
2158
2538
|
insertOrderToDLOB(
|
|
2159
2539
|
dlob,
|
|
2160
|
-
|
|
2540
|
+
user2.publicKey,
|
|
2161
2541
|
OrderType.LIMIT,
|
|
2162
2542
|
MarketType.PERP,
|
|
2163
2543
|
1, // orderId
|
|
@@ -2168,7 +2548,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2168
2548
|
vBid,
|
|
2169
2549
|
vAsk,
|
|
2170
2550
|
new BN(slot),
|
|
2171
|
-
|
|
2551
|
+
ZERO,
|
|
2172
2552
|
new BN(-5).mul(PRICE_PRECISION) // third best bid, worse than vBid (8): 6.5
|
|
2173
2553
|
);
|
|
2174
2554
|
|
|
@@ -2184,7 +2564,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2184
2564
|
// place two market sell orders to eat the best bid
|
|
2185
2565
|
insertOrderToDLOB(
|
|
2186
2566
|
dlob,
|
|
2187
|
-
|
|
2567
|
+
user3.publicKey,
|
|
2188
2568
|
OrderType.MARKET,
|
|
2189
2569
|
MarketType.PERP,
|
|
2190
2570
|
4, // orderId
|
|
@@ -2197,7 +2577,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2197
2577
|
);
|
|
2198
2578
|
insertOrderToDLOB(
|
|
2199
2579
|
dlob,
|
|
2200
|
-
|
|
2580
|
+
user4.publicKey,
|
|
2201
2581
|
OrderType.MARKET,
|
|
2202
2582
|
MarketType.PERP,
|
|
2203
2583
|
5, // orderId
|
|
@@ -2215,7 +2595,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2215
2595
|
MarketType.PERP,
|
|
2216
2596
|
oracle
|
|
2217
2597
|
);
|
|
2218
|
-
const mktNodes = dlob.
|
|
2598
|
+
const mktNodes = dlob.findExpiredNodesToFill(
|
|
2219
2599
|
marketIndex,
|
|
2220
2600
|
slot,
|
|
2221
2601
|
MarketType.PERP
|
|
@@ -2264,10 +2644,35 @@ describe('DLOB Perp Tests', () => {
|
|
|
2264
2644
|
it('Test fills market sell order with better priced vAMM after auction', () => {
|
|
2265
2645
|
const vAsk = new BN(15).mul(PRICE_PRECISION);
|
|
2266
2646
|
const vBid = new BN(8).mul(PRICE_PRECISION);
|
|
2267
|
-
|
|
2647
|
+
|
|
2648
|
+
const mockUserMap = new MockUserMap();
|
|
2649
|
+
const user0 = Keypair.generate();
|
|
2650
|
+
const user0Auth = Keypair.generate();
|
|
2651
|
+
const user1 = Keypair.generate();
|
|
2652
|
+
const user1Auth = Keypair.generate();
|
|
2653
|
+
const user2 = Keypair.generate();
|
|
2654
|
+
const user2Auth = Keypair.generate();
|
|
2655
|
+
const user3 = Keypair.generate();
|
|
2656
|
+
const user3Auth = Keypair.generate();
|
|
2657
|
+
const user4 = Keypair.generate();
|
|
2658
|
+
const user4Auth = Keypair.generate();
|
|
2659
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
2660
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
2661
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, user2Auth.publicKey);
|
|
2662
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, user3Auth.publicKey);
|
|
2663
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, user4Auth.publicKey);
|
|
2664
|
+
|
|
2665
|
+
const dlob = new DLOB(
|
|
2666
|
+
mockPerpMarkets,
|
|
2667
|
+
mockSpotMarkets,
|
|
2668
|
+
mockStateAccount,
|
|
2669
|
+
mockUserMap,
|
|
2670
|
+
false
|
|
2671
|
+
);
|
|
2268
2672
|
const marketIndex = 0;
|
|
2269
2673
|
|
|
2270
2674
|
const slot = 12;
|
|
2675
|
+
const ts = 12;
|
|
2271
2676
|
const oracle = {
|
|
2272
2677
|
price: vBid.add(vAsk).div(new BN(2)), // 11.5
|
|
2273
2678
|
slot: new BN(slot),
|
|
@@ -2278,7 +2683,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2278
2683
|
// insert some floating limit buy below vAMM bid
|
|
2279
2684
|
insertOrderToDLOB(
|
|
2280
2685
|
dlob,
|
|
2281
|
-
|
|
2686
|
+
user0.publicKey,
|
|
2282
2687
|
OrderType.LIMIT,
|
|
2283
2688
|
MarketType.PERP,
|
|
2284
2689
|
2, // orderId
|
|
@@ -2289,12 +2694,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
2289
2694
|
vBid,
|
|
2290
2695
|
vAsk,
|
|
2291
2696
|
new BN(slot),
|
|
2292
|
-
|
|
2697
|
+
ZERO,
|
|
2293
2698
|
new BN(-4).mul(PRICE_PRECISION) // second best bid, but worse than vBid (8): 7.5
|
|
2294
2699
|
);
|
|
2295
2700
|
insertOrderToDLOB(
|
|
2296
2701
|
dlob,
|
|
2297
|
-
|
|
2702
|
+
user1.publicKey,
|
|
2298
2703
|
OrderType.LIMIT,
|
|
2299
2704
|
MarketType.PERP,
|
|
2300
2705
|
3, // orderId
|
|
@@ -2305,12 +2710,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
2305
2710
|
vBid,
|
|
2306
2711
|
vAsk,
|
|
2307
2712
|
new BN(slot),
|
|
2308
|
-
|
|
2713
|
+
ZERO,
|
|
2309
2714
|
new BN(-1).mul(PRICE_PRECISION) // best price: 10.5
|
|
2310
2715
|
);
|
|
2311
2716
|
insertOrderToDLOB(
|
|
2312
2717
|
dlob,
|
|
2313
|
-
|
|
2718
|
+
user2.publicKey,
|
|
2314
2719
|
OrderType.LIMIT,
|
|
2315
2720
|
MarketType.PERP,
|
|
2316
2721
|
1, // orderId
|
|
@@ -2321,7 +2726,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2321
2726
|
vBid,
|
|
2322
2727
|
vAsk,
|
|
2323
2728
|
new BN(slot),
|
|
2324
|
-
|
|
2729
|
+
ZERO,
|
|
2325
2730
|
new BN(-5).mul(PRICE_PRECISION) // third best bid, worse than vBid (8): 6.5
|
|
2326
2731
|
);
|
|
2327
2732
|
|
|
@@ -2333,6 +2738,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2333
2738
|
vBid,
|
|
2334
2739
|
vAsk,
|
|
2335
2740
|
slot,
|
|
2741
|
+
ts,
|
|
2336
2742
|
MarketType.PERP,
|
|
2337
2743
|
oracle
|
|
2338
2744
|
);
|
|
@@ -2341,7 +2747,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2341
2747
|
// place two market sell orders to eat the best bid
|
|
2342
2748
|
insertOrderToDLOB(
|
|
2343
2749
|
dlob,
|
|
2344
|
-
|
|
2750
|
+
user3.publicKey,
|
|
2345
2751
|
OrderType.MARKET,
|
|
2346
2752
|
MarketType.PERP,
|
|
2347
2753
|
4, // orderId
|
|
@@ -2355,7 +2761,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2355
2761
|
);
|
|
2356
2762
|
insertOrderToDLOB(
|
|
2357
2763
|
dlob,
|
|
2358
|
-
|
|
2764
|
+
user4.publicKey,
|
|
2359
2765
|
OrderType.MARKET,
|
|
2360
2766
|
MarketType.PERP,
|
|
2361
2767
|
5, // orderId
|
|
@@ -2371,6 +2777,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2371
2777
|
|
|
2372
2778
|
// auction ends, but order not expired
|
|
2373
2779
|
const afterAuctionSlot = slot + 11;
|
|
2780
|
+
const afterAuctionTs = ts + 11;
|
|
2374
2781
|
printBookState(dlob, marketIndex, vBid, vAsk, afterAuctionSlot, oracle);
|
|
2375
2782
|
|
|
2376
2783
|
const nodesToFillAfter = dlob.findNodesToFill(
|
|
@@ -2378,6 +2785,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2378
2785
|
vBid,
|
|
2379
2786
|
vAsk,
|
|
2380
2787
|
afterAuctionSlot,
|
|
2788
|
+
afterAuctionTs,
|
|
2381
2789
|
MarketType.PERP,
|
|
2382
2790
|
oracle
|
|
2383
2791
|
);
|
|
@@ -2413,10 +2821,29 @@ describe('DLOB Perp Tests', () => {
|
|
|
2413
2821
|
it('Test fills crossing bids with vAMM after auction ends', () => {
|
|
2414
2822
|
const vAsk = new BN(15).mul(PRICE_PRECISION);
|
|
2415
2823
|
const vBid = new BN(8).mul(PRICE_PRECISION);
|
|
2416
|
-
|
|
2824
|
+
|
|
2825
|
+
const mockUserMap = new MockUserMap();
|
|
2826
|
+
const user0 = Keypair.generate();
|
|
2827
|
+
const user0Auth = Keypair.generate();
|
|
2828
|
+
const user1 = Keypair.generate();
|
|
2829
|
+
const user1Auth = Keypair.generate();
|
|
2830
|
+
const user2 = Keypair.generate();
|
|
2831
|
+
const user2Auth = Keypair.generate();
|
|
2832
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
2833
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
2834
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, user2Auth.publicKey);
|
|
2835
|
+
|
|
2836
|
+
const dlob = new DLOB(
|
|
2837
|
+
mockPerpMarkets,
|
|
2838
|
+
mockSpotMarkets,
|
|
2839
|
+
mockStateAccount,
|
|
2840
|
+
mockUserMap,
|
|
2841
|
+
false
|
|
2842
|
+
);
|
|
2417
2843
|
const marketIndex = 0;
|
|
2418
2844
|
|
|
2419
2845
|
const slot = 12;
|
|
2846
|
+
const ts = 12;
|
|
2420
2847
|
const oracle = {
|
|
2421
2848
|
price: vBid.add(vAsk).div(new BN(2)), // 11.5
|
|
2422
2849
|
slot: new BN(slot),
|
|
@@ -2427,7 +2854,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2427
2854
|
// insert some floating limit buy below vAMM bid
|
|
2428
2855
|
insertOrderToDLOB(
|
|
2429
2856
|
dlob,
|
|
2430
|
-
|
|
2857
|
+
user0.publicKey,
|
|
2431
2858
|
OrderType.LIMIT,
|
|
2432
2859
|
MarketType.PERP,
|
|
2433
2860
|
2, // orderId
|
|
@@ -2438,11 +2865,11 @@ describe('DLOB Perp Tests', () => {
|
|
|
2438
2865
|
vBid,
|
|
2439
2866
|
vAsk,
|
|
2440
2867
|
new BN(slot),
|
|
2441
|
-
|
|
2868
|
+
new BN(200)
|
|
2442
2869
|
);
|
|
2443
2870
|
insertOrderToDLOB(
|
|
2444
2871
|
dlob,
|
|
2445
|
-
|
|
2872
|
+
user1.publicKey,
|
|
2446
2873
|
OrderType.LIMIT,
|
|
2447
2874
|
MarketType.PERP,
|
|
2448
2875
|
3, // orderId
|
|
@@ -2453,11 +2880,11 @@ describe('DLOB Perp Tests', () => {
|
|
|
2453
2880
|
vBid,
|
|
2454
2881
|
vAsk,
|
|
2455
2882
|
new BN(slot),
|
|
2456
|
-
|
|
2883
|
+
new BN(200)
|
|
2457
2884
|
);
|
|
2458
2885
|
insertOrderToDLOB(
|
|
2459
2886
|
dlob,
|
|
2460
|
-
|
|
2887
|
+
user2.publicKey,
|
|
2461
2888
|
OrderType.LIMIT,
|
|
2462
2889
|
MarketType.PERP,
|
|
2463
2890
|
1, // orderId
|
|
@@ -2468,7 +2895,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2468
2895
|
vBid,
|
|
2469
2896
|
vAsk,
|
|
2470
2897
|
new BN(slot),
|
|
2471
|
-
|
|
2898
|
+
new BN(200)
|
|
2472
2899
|
);
|
|
2473
2900
|
console.log(`Book state before fill:`);
|
|
2474
2901
|
printBookState(dlob, marketIndex, vBid, vAsk, slot, oracle);
|
|
@@ -2478,6 +2905,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2478
2905
|
vBid,
|
|
2479
2906
|
vAsk,
|
|
2480
2907
|
slot,
|
|
2908
|
+
ts,
|
|
2481
2909
|
MarketType.PERP,
|
|
2482
2910
|
oracle
|
|
2483
2911
|
);
|
|
@@ -2485,12 +2913,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
2485
2913
|
|
|
2486
2914
|
// auction ends now
|
|
2487
2915
|
const afterAuctionSlot = 10 * slot;
|
|
2916
|
+
const afterAuctionTs = 10 * ts;
|
|
2488
2917
|
|
|
2489
2918
|
const nodesToFillAfter = dlob.findNodesToFill(
|
|
2490
2919
|
marketIndex,
|
|
2491
2920
|
vBid,
|
|
2492
2921
|
vAsk,
|
|
2493
2922
|
afterAuctionSlot,
|
|
2923
|
+
afterAuctionTs,
|
|
2494
2924
|
MarketType.PERP,
|
|
2495
2925
|
oracle
|
|
2496
2926
|
);
|
|
@@ -2525,13 +2955,194 @@ describe('DLOB Perp Tests', () => {
|
|
|
2525
2955
|
|
|
2526
2956
|
expect(nodesToFillAfter.length).to.equal(2);
|
|
2527
2957
|
});
|
|
2958
|
+
|
|
2959
|
+
it('Test fills two limit orders better than vAmm', () => {
|
|
2960
|
+
const vAsk = new BN(20).mul(PRICE_PRECISION);
|
|
2961
|
+
const vBid = new BN(5).mul(PRICE_PRECISION);
|
|
2962
|
+
|
|
2963
|
+
const mockUserMap = new MockUserMap();
|
|
2964
|
+
const user0 = Keypair.generate();
|
|
2965
|
+
const user0Auth = Keypair.generate();
|
|
2966
|
+
const user1 = Keypair.generate();
|
|
2967
|
+
const user1Auth = Keypair.generate();
|
|
2968
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
2969
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user1Auth.publicKey);
|
|
2970
|
+
|
|
2971
|
+
const dlob = new DLOB(
|
|
2972
|
+
mockPerpMarkets,
|
|
2973
|
+
mockSpotMarkets,
|
|
2974
|
+
mockStateAccount,
|
|
2975
|
+
mockUserMap,
|
|
2976
|
+
false
|
|
2977
|
+
);
|
|
2978
|
+
const marketIndex = 0;
|
|
2979
|
+
|
|
2980
|
+
const slot = 12;
|
|
2981
|
+
const ts = 12;
|
|
2982
|
+
const oracle = {
|
|
2983
|
+
price: vBid.add(vAsk).div(new BN(2)), // 11.5
|
|
2984
|
+
slot: new BN(slot),
|
|
2985
|
+
confidence: new BN(1),
|
|
2986
|
+
hasSufficientNumberOfDataPoints: true,
|
|
2987
|
+
};
|
|
2988
|
+
|
|
2989
|
+
// insert a sell below the bid, but above vBid
|
|
2990
|
+
insertOrderToDLOB(
|
|
2991
|
+
dlob,
|
|
2992
|
+
user0.publicKey,
|
|
2993
|
+
OrderType.LIMIT,
|
|
2994
|
+
MarketType.PERP,
|
|
2995
|
+
3, // orderId
|
|
2996
|
+
marketIndex,
|
|
2997
|
+
new BN(10).mul(PRICE_PRECISION), // price; crosses bid
|
|
2998
|
+
new BN(1).mul(BASE_PRECISION), // quantity
|
|
2999
|
+
PositionDirection.SHORT,
|
|
3000
|
+
vAsk,
|
|
3001
|
+
vBid,
|
|
3002
|
+
new BN(slot),
|
|
3003
|
+
new BN(200)
|
|
3004
|
+
);
|
|
3005
|
+
// insert a buy above the vBid
|
|
3006
|
+
insertOrderToDLOB(
|
|
3007
|
+
dlob,
|
|
3008
|
+
user1.publicKey,
|
|
3009
|
+
OrderType.LIMIT,
|
|
3010
|
+
MarketType.PERP,
|
|
3011
|
+
2, // orderId
|
|
3012
|
+
marketIndex,
|
|
3013
|
+
new BN(15).mul(PRICE_PRECISION), // price,
|
|
3014
|
+
new BN(8768).mul(BASE_PRECISION).div(new BN(10000)), // quantity
|
|
3015
|
+
PositionDirection.LONG,
|
|
3016
|
+
vBid,
|
|
3017
|
+
vAsk,
|
|
3018
|
+
new BN(slot + 1), // later order becomes taker
|
|
3019
|
+
new BN(200)
|
|
3020
|
+
);
|
|
3021
|
+
|
|
3022
|
+
console.log(`Book state before fill:`);
|
|
3023
|
+
printBookState(dlob, marketIndex, vBid, vAsk, slot, oracle);
|
|
3024
|
+
|
|
3025
|
+
const nodesToFillBefore = dlob.findNodesToFill(
|
|
3026
|
+
marketIndex,
|
|
3027
|
+
vBid,
|
|
3028
|
+
vAsk,
|
|
3029
|
+
slot,
|
|
3030
|
+
ts,
|
|
3031
|
+
MarketType.PERP,
|
|
3032
|
+
oracle
|
|
3033
|
+
);
|
|
3034
|
+
console.log(`Filled nodes: ${nodesToFillBefore.length}`);
|
|
3035
|
+
for (const n of nodesToFillBefore) {
|
|
3036
|
+
printCrossedNodes(n, slot);
|
|
3037
|
+
}
|
|
3038
|
+
expect(nodesToFillBefore.length).to.equal(1);
|
|
3039
|
+
|
|
3040
|
+
// first order is maker, second is taker
|
|
3041
|
+
expect(
|
|
3042
|
+
nodesToFillBefore[0].node.order?.orderId,
|
|
3043
|
+
'wrong taker orderId'
|
|
3044
|
+
).to.equal(2);
|
|
3045
|
+
expect(
|
|
3046
|
+
nodesToFillBefore[0].makerNode?.order?.orderId,
|
|
3047
|
+
'wrong maker orderId'
|
|
3048
|
+
).to.equal(3);
|
|
3049
|
+
});
|
|
3050
|
+
|
|
3051
|
+
it('Test will not fill two limit orders by same authority', () => {
|
|
3052
|
+
const vAsk = new BN(20).mul(PRICE_PRECISION);
|
|
3053
|
+
const vBid = new BN(5).mul(PRICE_PRECISION);
|
|
3054
|
+
|
|
3055
|
+
const mockUserMap = new MockUserMap();
|
|
3056
|
+
const user0 = Keypair.generate();
|
|
3057
|
+
const user0Auth = Keypair.generate();
|
|
3058
|
+
const user1 = Keypair.generate();
|
|
3059
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
3060
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, user0Auth.publicKey);
|
|
3061
|
+
|
|
3062
|
+
const dlob = new DLOB(
|
|
3063
|
+
mockPerpMarkets,
|
|
3064
|
+
mockSpotMarkets,
|
|
3065
|
+
mockStateAccount,
|
|
3066
|
+
mockUserMap,
|
|
3067
|
+
false
|
|
3068
|
+
);
|
|
3069
|
+
const marketIndex = 0;
|
|
3070
|
+
|
|
3071
|
+
const slot = 12;
|
|
3072
|
+
const ts = 12;
|
|
3073
|
+
const oracle = {
|
|
3074
|
+
price: vBid.add(vAsk).div(new BN(2)), // 11.5
|
|
3075
|
+
slot: new BN(slot),
|
|
3076
|
+
confidence: new BN(1),
|
|
3077
|
+
hasSufficientNumberOfDataPoints: true,
|
|
3078
|
+
};
|
|
3079
|
+
|
|
3080
|
+
// insert a sell below the bid, but above vBid
|
|
3081
|
+
insertOrderToDLOB(
|
|
3082
|
+
dlob,
|
|
3083
|
+
user0.publicKey,
|
|
3084
|
+
OrderType.LIMIT,
|
|
3085
|
+
MarketType.PERP,
|
|
3086
|
+
3, // orderId
|
|
3087
|
+
marketIndex,
|
|
3088
|
+
new BN(10).mul(PRICE_PRECISION), // price; crosses bid
|
|
3089
|
+
new BN(1).mul(BASE_PRECISION), // quantity
|
|
3090
|
+
PositionDirection.SHORT,
|
|
3091
|
+
vAsk,
|
|
3092
|
+
vBid,
|
|
3093
|
+
new BN(slot),
|
|
3094
|
+
new BN(200)
|
|
3095
|
+
);
|
|
3096
|
+
// insert a buy above the vBid
|
|
3097
|
+
insertOrderToDLOB(
|
|
3098
|
+
dlob,
|
|
3099
|
+
user1.publicKey,
|
|
3100
|
+
OrderType.LIMIT,
|
|
3101
|
+
MarketType.PERP,
|
|
3102
|
+
2, // orderId
|
|
3103
|
+
marketIndex,
|
|
3104
|
+
new BN(15).mul(PRICE_PRECISION), // price,
|
|
3105
|
+
new BN(8768).mul(BASE_PRECISION).div(new BN(10000)), // quantity
|
|
3106
|
+
PositionDirection.LONG,
|
|
3107
|
+
vBid,
|
|
3108
|
+
vAsk,
|
|
3109
|
+
new BN(slot),
|
|
3110
|
+
new BN(200)
|
|
3111
|
+
);
|
|
3112
|
+
|
|
3113
|
+
console.log(`Book state before fill:`);
|
|
3114
|
+
printBookState(dlob, marketIndex, vBid, vAsk, slot, oracle);
|
|
3115
|
+
|
|
3116
|
+
const nodesToFillBefore = dlob.findNodesToFill(
|
|
3117
|
+
marketIndex,
|
|
3118
|
+
vBid,
|
|
3119
|
+
vAsk,
|
|
3120
|
+
slot,
|
|
3121
|
+
ts,
|
|
3122
|
+
MarketType.PERP,
|
|
3123
|
+
oracle
|
|
3124
|
+
);
|
|
3125
|
+
console.log(`Filled nodes: ${nodesToFillBefore.length}`);
|
|
3126
|
+
for (const n of nodesToFillBefore) {
|
|
3127
|
+
printCrossedNodes(n, slot);
|
|
3128
|
+
}
|
|
3129
|
+
|
|
3130
|
+
expect(nodesToFillBefore.length).to.equal(0);
|
|
3131
|
+
});
|
|
2528
3132
|
});
|
|
2529
3133
|
|
|
2530
3134
|
describe('DLOB Spot Tests', () => {
|
|
2531
3135
|
it('Test proper bids', () => {
|
|
2532
3136
|
const vAsk = new BN(115);
|
|
2533
3137
|
const vBid = new BN(100);
|
|
2534
|
-
const
|
|
3138
|
+
const mockUserMap = new MockUserMap();
|
|
3139
|
+
const dlob = new DLOB(
|
|
3140
|
+
mockPerpMarkets,
|
|
3141
|
+
mockSpotMarkets,
|
|
3142
|
+
mockStateAccount,
|
|
3143
|
+
mockUserMap,
|
|
3144
|
+
false
|
|
3145
|
+
);
|
|
2535
3146
|
const marketIndex = 0;
|
|
2536
3147
|
|
|
2537
3148
|
const slot = 12;
|
|
@@ -2594,9 +3205,13 @@ describe('DLOB Spot Tests', () => {
|
|
|
2594
3205
|
];
|
|
2595
3206
|
|
|
2596
3207
|
for (const t of testCases) {
|
|
3208
|
+
const user0 = Keypair.generate();
|
|
3209
|
+
const user0Auth = Keypair.generate();
|
|
3210
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
3211
|
+
|
|
2597
3212
|
insertOrderToDLOB(
|
|
2598
3213
|
dlob,
|
|
2599
|
-
|
|
3214
|
+
user0.publicKey,
|
|
2600
3215
|
t.orderType || OrderType.LIMIT,
|
|
2601
3216
|
MarketType.SPOT,
|
|
2602
3217
|
t.orderId || 0, // orderId
|
|
@@ -2645,7 +3260,14 @@ describe('DLOB Spot Tests', () => {
|
|
|
2645
3260
|
it('Test proper bids on multiple markets', () => {
|
|
2646
3261
|
const vAsk = new BN(15);
|
|
2647
3262
|
const vBid = new BN(10);
|
|
2648
|
-
const
|
|
3263
|
+
const mockUserMap = new MockUserMap();
|
|
3264
|
+
const dlob = new DLOB(
|
|
3265
|
+
mockPerpMarkets,
|
|
3266
|
+
mockSpotMarkets,
|
|
3267
|
+
mockStateAccount,
|
|
3268
|
+
mockUserMap,
|
|
3269
|
+
false
|
|
3270
|
+
);
|
|
2649
3271
|
const marketIndex0 = 0;
|
|
2650
3272
|
const marketIndex1 = 1;
|
|
2651
3273
|
|
|
@@ -2716,9 +3338,13 @@ describe('DLOB Spot Tests', () => {
|
|
|
2716
3338
|
];
|
|
2717
3339
|
|
|
2718
3340
|
for (const t of testCases) {
|
|
3341
|
+
const user0 = Keypair.generate();
|
|
3342
|
+
const user0Auth = Keypair.generate();
|
|
3343
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
3344
|
+
|
|
2719
3345
|
insertOrderToDLOB(
|
|
2720
3346
|
dlob,
|
|
2721
|
-
|
|
3347
|
+
user0.publicKey,
|
|
2722
3348
|
t.orderType || OrderType.LIMIT,
|
|
2723
3349
|
MarketType.SPOT,
|
|
2724
3350
|
t.orderId || 0, // orderId
|
|
@@ -2778,7 +3404,14 @@ describe('DLOB Spot Tests', () => {
|
|
|
2778
3404
|
it('Test proper asks', () => {
|
|
2779
3405
|
const vAsk = new BN(15);
|
|
2780
3406
|
const vBid = new BN(10);
|
|
2781
|
-
const
|
|
3407
|
+
const mockUserMap = new MockUserMap();
|
|
3408
|
+
const dlob = new DLOB(
|
|
3409
|
+
mockPerpMarkets,
|
|
3410
|
+
mockSpotMarkets,
|
|
3411
|
+
mockStateAccount,
|
|
3412
|
+
mockUserMap,
|
|
3413
|
+
false
|
|
3414
|
+
);
|
|
2782
3415
|
const marketIndex = 0;
|
|
2783
3416
|
|
|
2784
3417
|
const slot = 12;
|
|
@@ -2841,9 +3474,13 @@ describe('DLOB Spot Tests', () => {
|
|
|
2841
3474
|
];
|
|
2842
3475
|
|
|
2843
3476
|
for (const t of testCases) {
|
|
3477
|
+
const user0 = Keypair.generate();
|
|
3478
|
+
const user0Auth = Keypair.generate();
|
|
3479
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
3480
|
+
|
|
2844
3481
|
insertOrderToDLOB(
|
|
2845
3482
|
dlob,
|
|
2846
|
-
|
|
3483
|
+
user0.publicKey,
|
|
2847
3484
|
t.orderType || OrderType.LIMIT,
|
|
2848
3485
|
MarketType.SPOT,
|
|
2849
3486
|
t.orderId || 0, // orderId
|
|
@@ -2900,14 +3537,25 @@ describe('DLOB Spot Tests', () => {
|
|
|
2900
3537
|
confidence: new BN(1),
|
|
2901
3538
|
hasSufficientNumberOfDataPoints: true,
|
|
2902
3539
|
};
|
|
2903
|
-
const
|
|
3540
|
+
const mockUserMap = new MockUserMap();
|
|
3541
|
+
const dlob = new DLOB(
|
|
3542
|
+
mockPerpMarkets,
|
|
3543
|
+
mockSpotMarkets,
|
|
3544
|
+
mockStateAccount,
|
|
3545
|
+
mockUserMap,
|
|
3546
|
+
false
|
|
3547
|
+
);
|
|
2904
3548
|
const marketIndex = 0;
|
|
2905
3549
|
|
|
2906
3550
|
// 3 mkt buys
|
|
2907
3551
|
for (let i = 0; i < 3; i++) {
|
|
3552
|
+
const user0 = Keypair.generate();
|
|
3553
|
+
const user0Auth = Keypair.generate();
|
|
3554
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
3555
|
+
|
|
2908
3556
|
insertOrderToDLOB(
|
|
2909
3557
|
dlob,
|
|
2910
|
-
|
|
3558
|
+
user0.publicKey,
|
|
2911
3559
|
OrderType.MARKET,
|
|
2912
3560
|
MarketType.SPOT,
|
|
2913
3561
|
i + 1,
|
|
@@ -2922,9 +3570,13 @@ describe('DLOB Spot Tests', () => {
|
|
|
2922
3570
|
|
|
2923
3571
|
// 3 mkt sells
|
|
2924
3572
|
for (let i = 0; i < 3; i++) {
|
|
3573
|
+
const user0 = Keypair.generate();
|
|
3574
|
+
const user0Auth = Keypair.generate();
|
|
3575
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
3576
|
+
|
|
2925
3577
|
insertOrderToDLOB(
|
|
2926
3578
|
dlob,
|
|
2927
|
-
|
|
3579
|
+
user0.publicKey,
|
|
2928
3580
|
OrderType.MARKET,
|
|
2929
3581
|
MarketType.SPOT,
|
|
2930
3582
|
i + 1,
|
|
@@ -2986,11 +3638,39 @@ describe('DLOB Spot Tests', () => {
|
|
|
2986
3638
|
confidence: new BN(1),
|
|
2987
3639
|
hasSufficientNumberOfDataPoints: true,
|
|
2988
3640
|
};
|
|
2989
|
-
|
|
3641
|
+
|
|
3642
|
+
const mockUserMap = new MockUserMap();
|
|
3643
|
+
const user0 = Keypair.generate();
|
|
3644
|
+
const userAuth0 = Keypair.generate();
|
|
3645
|
+
const user1 = Keypair.generate();
|
|
3646
|
+
const userAuth1 = Keypair.generate();
|
|
3647
|
+
const user2 = Keypair.generate();
|
|
3648
|
+
const userAuth2 = Keypair.generate();
|
|
3649
|
+
const user3 = Keypair.generate();
|
|
3650
|
+
const userAuth3 = Keypair.generate();
|
|
3651
|
+
const user4 = Keypair.generate();
|
|
3652
|
+
const userAuth4 = Keypair.generate();
|
|
3653
|
+
const user5 = Keypair.generate();
|
|
3654
|
+
const userAuth5 = Keypair.generate();
|
|
3655
|
+
|
|
3656
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, userAuth0.publicKey);
|
|
3657
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, userAuth1.publicKey);
|
|
3658
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, userAuth2.publicKey);
|
|
3659
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, userAuth3.publicKey);
|
|
3660
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, userAuth4.publicKey);
|
|
3661
|
+
mockUserMap.addUserAccountAuthority(user5.publicKey, userAuth5.publicKey);
|
|
3662
|
+
|
|
3663
|
+
const dlob = new DLOB(
|
|
3664
|
+
mockPerpMarkets,
|
|
3665
|
+
mockSpotMarkets,
|
|
3666
|
+
mockStateAccount,
|
|
3667
|
+
mockUserMap,
|
|
3668
|
+
false
|
|
3669
|
+
);
|
|
2990
3670
|
const marketIndex = 0;
|
|
2991
3671
|
insertOrderToDLOB(
|
|
2992
3672
|
dlob,
|
|
2993
|
-
|
|
3673
|
+
user0.publicKey,
|
|
2994
3674
|
OrderType.LIMIT,
|
|
2995
3675
|
MarketType.SPOT,
|
|
2996
3676
|
3, // orderId
|
|
@@ -3004,7 +3684,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3004
3684
|
|
|
3005
3685
|
insertOrderToDLOB(
|
|
3006
3686
|
dlob,
|
|
3007
|
-
|
|
3687
|
+
user1.publicKey,
|
|
3008
3688
|
OrderType.LIMIT,
|
|
3009
3689
|
MarketType.SPOT,
|
|
3010
3690
|
2, // orderId
|
|
@@ -3018,7 +3698,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3018
3698
|
|
|
3019
3699
|
insertOrderToDLOB(
|
|
3020
3700
|
dlob,
|
|
3021
|
-
|
|
3701
|
+
user2.publicKey,
|
|
3022
3702
|
OrderType.LIMIT,
|
|
3023
3703
|
MarketType.SPOT,
|
|
3024
3704
|
1, // orderId
|
|
@@ -3032,7 +3712,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3032
3712
|
|
|
3033
3713
|
insertOrderToDLOB(
|
|
3034
3714
|
dlob,
|
|
3035
|
-
|
|
3715
|
+
user3.publicKey,
|
|
3036
3716
|
OrderType.LIMIT,
|
|
3037
3717
|
MarketType.SPOT,
|
|
3038
3718
|
1, // orderId
|
|
@@ -3046,7 +3726,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3046
3726
|
|
|
3047
3727
|
insertOrderToDLOB(
|
|
3048
3728
|
dlob,
|
|
3049
|
-
|
|
3729
|
+
user4.publicKey,
|
|
3050
3730
|
OrderType.LIMIT,
|
|
3051
3731
|
MarketType.SPOT,
|
|
3052
3732
|
2, // orderId
|
|
@@ -3060,7 +3740,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3060
3740
|
|
|
3061
3741
|
insertOrderToDLOB(
|
|
3062
3742
|
dlob,
|
|
3063
|
-
|
|
3743
|
+
user5.publicKey,
|
|
3064
3744
|
OrderType.LIMIT,
|
|
3065
3745
|
MarketType.SPOT,
|
|
3066
3746
|
3, // orderId
|
|
@@ -3122,13 +3802,41 @@ describe('DLOB Spot Tests', () => {
|
|
|
3122
3802
|
it('Test multiple market orders fill with multiple limit orders', () => {
|
|
3123
3803
|
const vAsk = new BN(15);
|
|
3124
3804
|
const vBid = new BN(10);
|
|
3125
|
-
|
|
3805
|
+
|
|
3806
|
+
const mockUserMap = new MockUserMap();
|
|
3807
|
+
const user0 = Keypair.generate();
|
|
3808
|
+
const userAuth0 = Keypair.generate();
|
|
3809
|
+
const user1 = Keypair.generate();
|
|
3810
|
+
const userAuth1 = Keypair.generate();
|
|
3811
|
+
const user2 = Keypair.generate();
|
|
3812
|
+
const userAuth2 = Keypair.generate();
|
|
3813
|
+
const user3 = Keypair.generate();
|
|
3814
|
+
const userAuth3 = Keypair.generate();
|
|
3815
|
+
const user4 = Keypair.generate();
|
|
3816
|
+
const userAuth4 = Keypair.generate();
|
|
3817
|
+
const user5 = Keypair.generate();
|
|
3818
|
+
const userAuth5 = Keypair.generate();
|
|
3819
|
+
|
|
3820
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, userAuth0.publicKey);
|
|
3821
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, userAuth1.publicKey);
|
|
3822
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, userAuth2.publicKey);
|
|
3823
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, userAuth3.publicKey);
|
|
3824
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, userAuth4.publicKey);
|
|
3825
|
+
mockUserMap.addUserAccountAuthority(user5.publicKey, userAuth5.publicKey);
|
|
3826
|
+
|
|
3827
|
+
const dlob = new DLOB(
|
|
3828
|
+
mockPerpMarkets,
|
|
3829
|
+
mockSpotMarkets,
|
|
3830
|
+
mockStateAccount,
|
|
3831
|
+
mockUserMap,
|
|
3832
|
+
false
|
|
3833
|
+
);
|
|
3126
3834
|
const marketIndex = 0;
|
|
3127
3835
|
|
|
3128
3836
|
// insert some limit buys above vamm bid, below ask
|
|
3129
3837
|
insertOrderToDLOB(
|
|
3130
3838
|
dlob,
|
|
3131
|
-
|
|
3839
|
+
user0.publicKey,
|
|
3132
3840
|
OrderType.LIMIT,
|
|
3133
3841
|
MarketType.SPOT,
|
|
3134
3842
|
1, // orderId
|
|
@@ -3141,7 +3849,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3141
3849
|
);
|
|
3142
3850
|
insertOrderToDLOB(
|
|
3143
3851
|
dlob,
|
|
3144
|
-
|
|
3852
|
+
user1.publicKey,
|
|
3145
3853
|
OrderType.LIMIT,
|
|
3146
3854
|
MarketType.SPOT,
|
|
3147
3855
|
2, // orderId
|
|
@@ -3154,7 +3862,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3154
3862
|
);
|
|
3155
3863
|
insertOrderToDLOB(
|
|
3156
3864
|
dlob,
|
|
3157
|
-
|
|
3865
|
+
user2.publicKey,
|
|
3158
3866
|
OrderType.LIMIT,
|
|
3159
3867
|
MarketType.SPOT,
|
|
3160
3868
|
3, // orderId
|
|
@@ -3183,7 +3891,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3183
3891
|
// place two market sell order eating 2 of the limit orders
|
|
3184
3892
|
insertOrderToDLOB(
|
|
3185
3893
|
dlob,
|
|
3186
|
-
|
|
3894
|
+
user4.publicKey,
|
|
3187
3895
|
OrderType.MARKET,
|
|
3188
3896
|
MarketType.SPOT,
|
|
3189
3897
|
4, // orderId
|
|
@@ -3196,7 +3904,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3196
3904
|
);
|
|
3197
3905
|
insertOrderToDLOB(
|
|
3198
3906
|
dlob,
|
|
3199
|
-
|
|
3907
|
+
user5.publicKey,
|
|
3200
3908
|
OrderType.MARKET,
|
|
3201
3909
|
MarketType.SPOT,
|
|
3202
3910
|
5, // orderId
|
|
@@ -3238,13 +3946,35 @@ describe('DLOB Spot Tests', () => {
|
|
|
3238
3946
|
it('Test one market order fills two limit orders', () => {
|
|
3239
3947
|
const vAsk = new BN(15);
|
|
3240
3948
|
const vBid = new BN(10);
|
|
3241
|
-
|
|
3949
|
+
|
|
3950
|
+
const mockUserMap = new MockUserMap();
|
|
3951
|
+
const user0 = Keypair.generate();
|
|
3952
|
+
const userAuth0 = Keypair.generate();
|
|
3953
|
+
const user1 = Keypair.generate();
|
|
3954
|
+
const userAuth1 = Keypair.generate();
|
|
3955
|
+
const user2 = Keypair.generate();
|
|
3956
|
+
const userAuth2 = Keypair.generate();
|
|
3957
|
+
const user3 = Keypair.generate();
|
|
3958
|
+
const userAuth3 = Keypair.generate();
|
|
3959
|
+
|
|
3960
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, userAuth0.publicKey);
|
|
3961
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, userAuth1.publicKey);
|
|
3962
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, userAuth2.publicKey);
|
|
3963
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, userAuth3.publicKey);
|
|
3964
|
+
|
|
3965
|
+
const dlob = new DLOB(
|
|
3966
|
+
mockPerpMarkets,
|
|
3967
|
+
mockSpotMarkets,
|
|
3968
|
+
mockStateAccount,
|
|
3969
|
+
mockUserMap,
|
|
3970
|
+
false
|
|
3971
|
+
);
|
|
3242
3972
|
const marketIndex = 0;
|
|
3243
3973
|
|
|
3244
3974
|
// insert some limit sells below vAMM ask, above bid
|
|
3245
3975
|
insertOrderToDLOB(
|
|
3246
3976
|
dlob,
|
|
3247
|
-
|
|
3977
|
+
user0.publicKey,
|
|
3248
3978
|
OrderType.LIMIT,
|
|
3249
3979
|
MarketType.SPOT,
|
|
3250
3980
|
1, // orderId
|
|
@@ -3257,7 +3987,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3257
3987
|
);
|
|
3258
3988
|
insertOrderToDLOB(
|
|
3259
3989
|
dlob,
|
|
3260
|
-
|
|
3990
|
+
user1.publicKey,
|
|
3261
3991
|
OrderType.LIMIT,
|
|
3262
3992
|
MarketType.SPOT,
|
|
3263
3993
|
2, // orderId
|
|
@@ -3270,7 +4000,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3270
4000
|
);
|
|
3271
4001
|
insertOrderToDLOB(
|
|
3272
4002
|
dlob,
|
|
3273
|
-
|
|
4003
|
+
user2.publicKey,
|
|
3274
4004
|
OrderType.LIMIT,
|
|
3275
4005
|
MarketType.SPOT,
|
|
3276
4006
|
3, // orderId
|
|
@@ -3299,7 +4029,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3299
4029
|
// place one market buy order eating 2 of the limit orders
|
|
3300
4030
|
insertOrderToDLOB(
|
|
3301
4031
|
dlob,
|
|
3302
|
-
|
|
4032
|
+
user3.publicKey,
|
|
3303
4033
|
OrderType.MARKET,
|
|
3304
4034
|
MarketType.SPOT,
|
|
3305
4035
|
4, // orderId
|
|
@@ -3341,7 +4071,32 @@ describe('DLOB Spot Tests', () => {
|
|
|
3341
4071
|
it('Test two market orders to fill one limit order', () => {
|
|
3342
4072
|
const vAsk = new BN(15);
|
|
3343
4073
|
const vBid = new BN(8);
|
|
3344
|
-
|
|
4074
|
+
|
|
4075
|
+
const mockUserMap = new MockUserMap();
|
|
4076
|
+
const user0 = Keypair.generate();
|
|
4077
|
+
const userAuth0 = Keypair.generate();
|
|
4078
|
+
const user1 = Keypair.generate();
|
|
4079
|
+
const userAuth1 = Keypair.generate();
|
|
4080
|
+
const user2 = Keypair.generate();
|
|
4081
|
+
const userAuth2 = Keypair.generate();
|
|
4082
|
+
const user3 = Keypair.generate();
|
|
4083
|
+
const userAuth3 = Keypair.generate();
|
|
4084
|
+
const user4 = Keypair.generate();
|
|
4085
|
+
const userAuth4 = Keypair.generate();
|
|
4086
|
+
|
|
4087
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, userAuth0.publicKey);
|
|
4088
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, userAuth1.publicKey);
|
|
4089
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, userAuth2.publicKey);
|
|
4090
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, userAuth3.publicKey);
|
|
4091
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, userAuth4.publicKey);
|
|
4092
|
+
|
|
4093
|
+
const dlob = new DLOB(
|
|
4094
|
+
mockPerpMarkets,
|
|
4095
|
+
mockSpotMarkets,
|
|
4096
|
+
mockStateAccount,
|
|
4097
|
+
mockUserMap,
|
|
4098
|
+
false
|
|
4099
|
+
);
|
|
3345
4100
|
const marketIndex = 0;
|
|
3346
4101
|
|
|
3347
4102
|
const slot = 12;
|
|
@@ -3355,7 +4110,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3355
4110
|
// insert some limit sells below vAMM ask, above bid
|
|
3356
4111
|
insertOrderToDLOB(
|
|
3357
4112
|
dlob,
|
|
3358
|
-
|
|
4113
|
+
user0.publicKey,
|
|
3359
4114
|
OrderType.LIMIT,
|
|
3360
4115
|
MarketType.SPOT,
|
|
3361
4116
|
1, // orderId
|
|
@@ -3368,7 +4123,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3368
4123
|
);
|
|
3369
4124
|
insertOrderToDLOB(
|
|
3370
4125
|
dlob,
|
|
3371
|
-
|
|
4126
|
+
user1.publicKey,
|
|
3372
4127
|
OrderType.LIMIT,
|
|
3373
4128
|
MarketType.SPOT,
|
|
3374
4129
|
2, // orderId
|
|
@@ -3381,7 +4136,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3381
4136
|
);
|
|
3382
4137
|
insertOrderToDLOB(
|
|
3383
4138
|
dlob,
|
|
3384
|
-
|
|
4139
|
+
user2.publicKey,
|
|
3385
4140
|
OrderType.LIMIT,
|
|
3386
4141
|
MarketType.SPOT,
|
|
3387
4142
|
3, // orderId
|
|
@@ -3405,7 +4160,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3405
4160
|
// place two market buy orders to eat the best ask
|
|
3406
4161
|
insertOrderToDLOB(
|
|
3407
4162
|
dlob,
|
|
3408
|
-
|
|
4163
|
+
user3.publicKey,
|
|
3409
4164
|
OrderType.MARKET,
|
|
3410
4165
|
MarketType.SPOT,
|
|
3411
4166
|
4, // orderId
|
|
@@ -3418,7 +4173,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3418
4173
|
);
|
|
3419
4174
|
insertOrderToDLOB(
|
|
3420
4175
|
dlob,
|
|
3421
|
-
|
|
4176
|
+
user4.publicKey,
|
|
3422
4177
|
OrderType.MARKET,
|
|
3423
4178
|
MarketType.SPOT,
|
|
3424
4179
|
5, // orderId
|
|
@@ -3436,7 +4191,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3436
4191
|
MarketType.SPOT,
|
|
3437
4192
|
oracle
|
|
3438
4193
|
);
|
|
3439
|
-
const mktNodes = dlob.
|
|
4194
|
+
const mktNodes = dlob.findExpiredNodesToFill(
|
|
3440
4195
|
marketIndex,
|
|
3441
4196
|
slot,
|
|
3442
4197
|
MarketType.SPOT
|
|
@@ -3464,7 +4219,53 @@ describe('DLOB Spot Tests', () => {
|
|
|
3464
4219
|
it('Test trigger orders', () => {
|
|
3465
4220
|
const vAsk = new BN(15);
|
|
3466
4221
|
const vBid = new BN(8);
|
|
3467
|
-
|
|
4222
|
+
|
|
4223
|
+
const mockUserMap = new MockUserMap();
|
|
4224
|
+
const user0 = Keypair.generate();
|
|
4225
|
+
const userAuth0 = Keypair.generate();
|
|
4226
|
+
const user1 = Keypair.generate();
|
|
4227
|
+
const userAuth1 = Keypair.generate();
|
|
4228
|
+
const user2 = Keypair.generate();
|
|
4229
|
+
const userAuth2 = Keypair.generate();
|
|
4230
|
+
const user3 = Keypair.generate();
|
|
4231
|
+
const userAuth3 = Keypair.generate();
|
|
4232
|
+
const user4 = Keypair.generate();
|
|
4233
|
+
const userAuth4 = Keypair.generate();
|
|
4234
|
+
const user5 = Keypair.generate();
|
|
4235
|
+
const userAuth5 = Keypair.generate();
|
|
4236
|
+
const user6 = Keypair.generate();
|
|
4237
|
+
const userAuth6 = Keypair.generate();
|
|
4238
|
+
const user7 = Keypair.generate();
|
|
4239
|
+
const userAuth7 = Keypair.generate();
|
|
4240
|
+
const user8 = Keypair.generate();
|
|
4241
|
+
const userAuth8 = Keypair.generate();
|
|
4242
|
+
const user9 = Keypair.generate();
|
|
4243
|
+
const userAuth9 = Keypair.generate();
|
|
4244
|
+
const user10 = Keypair.generate();
|
|
4245
|
+
const userAuth10 = Keypair.generate();
|
|
4246
|
+
const user11 = Keypair.generate();
|
|
4247
|
+
const userAuth11 = Keypair.generate();
|
|
4248
|
+
|
|
4249
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, userAuth0.publicKey);
|
|
4250
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, userAuth1.publicKey);
|
|
4251
|
+
mockUserMap.addUserAccountAuthority(user2.publicKey, userAuth2.publicKey);
|
|
4252
|
+
mockUserMap.addUserAccountAuthority(user3.publicKey, userAuth3.publicKey);
|
|
4253
|
+
mockUserMap.addUserAccountAuthority(user4.publicKey, userAuth4.publicKey);
|
|
4254
|
+
mockUserMap.addUserAccountAuthority(user5.publicKey, userAuth5.publicKey);
|
|
4255
|
+
mockUserMap.addUserAccountAuthority(user6.publicKey, userAuth6.publicKey);
|
|
4256
|
+
mockUserMap.addUserAccountAuthority(user7.publicKey, userAuth7.publicKey);
|
|
4257
|
+
mockUserMap.addUserAccountAuthority(user8.publicKey, userAuth8.publicKey);
|
|
4258
|
+
mockUserMap.addUserAccountAuthority(user9.publicKey, userAuth9.publicKey);
|
|
4259
|
+
mockUserMap.addUserAccountAuthority(user10.publicKey, userAuth10.publicKey);
|
|
4260
|
+
mockUserMap.addUserAccountAuthority(user11.publicKey, userAuth11.publicKey);
|
|
4261
|
+
|
|
4262
|
+
const dlob = new DLOB(
|
|
4263
|
+
mockPerpMarkets,
|
|
4264
|
+
mockSpotMarkets,
|
|
4265
|
+
mockStateAccount,
|
|
4266
|
+
mockUserMap,
|
|
4267
|
+
false
|
|
4268
|
+
);
|
|
3468
4269
|
const marketIndex = 0;
|
|
3469
4270
|
|
|
3470
4271
|
const slot = 20;
|
|
@@ -3481,7 +4282,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3481
4282
|
// should trigger limit buy with above condition
|
|
3482
4283
|
insertTriggerOrderToDLOB(
|
|
3483
4284
|
dlob,
|
|
3484
|
-
|
|
4285
|
+
user0.publicKey,
|
|
3485
4286
|
OrderType.TRIGGER_LIMIT,
|
|
3486
4287
|
MarketType.SPOT,
|
|
3487
4288
|
1, //orderId
|
|
@@ -3497,7 +4298,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3497
4298
|
// should trigger limit sell with above condition
|
|
3498
4299
|
insertTriggerOrderToDLOB(
|
|
3499
4300
|
dlob,
|
|
3500
|
-
|
|
4301
|
+
user1.publicKey,
|
|
3501
4302
|
OrderType.TRIGGER_LIMIT,
|
|
3502
4303
|
MarketType.SPOT,
|
|
3503
4304
|
2, //orderId
|
|
@@ -3513,7 +4314,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3513
4314
|
// should trigger market buy with above condition
|
|
3514
4315
|
insertTriggerOrderToDLOB(
|
|
3515
4316
|
dlob,
|
|
3516
|
-
|
|
4317
|
+
user2.publicKey,
|
|
3517
4318
|
OrderType.TRIGGER_MARKET,
|
|
3518
4319
|
MarketType.SPOT,
|
|
3519
4320
|
3, //orderId
|
|
@@ -3529,7 +4330,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3529
4330
|
// should trigger market sell with above condition
|
|
3530
4331
|
insertTriggerOrderToDLOB(
|
|
3531
4332
|
dlob,
|
|
3532
|
-
|
|
4333
|
+
user3.publicKey,
|
|
3533
4334
|
OrderType.TRIGGER_MARKET,
|
|
3534
4335
|
MarketType.SPOT,
|
|
3535
4336
|
4, //orderId
|
|
@@ -3545,7 +4346,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3545
4346
|
// should trigger limit buy with below condition
|
|
3546
4347
|
insertTriggerOrderToDLOB(
|
|
3547
4348
|
dlob,
|
|
3548
|
-
|
|
4349
|
+
user4.publicKey,
|
|
3549
4350
|
OrderType.TRIGGER_LIMIT,
|
|
3550
4351
|
MarketType.SPOT,
|
|
3551
4352
|
5, //orderId
|
|
@@ -3561,7 +4362,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3561
4362
|
// should trigger limit sell with below condition
|
|
3562
4363
|
insertTriggerOrderToDLOB(
|
|
3563
4364
|
dlob,
|
|
3564
|
-
|
|
4365
|
+
user5.publicKey,
|
|
3565
4366
|
OrderType.TRIGGER_LIMIT,
|
|
3566
4367
|
MarketType.SPOT,
|
|
3567
4368
|
6, //orderId
|
|
@@ -3577,7 +4378,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3577
4378
|
// should trigger market buy with below condition
|
|
3578
4379
|
insertTriggerOrderToDLOB(
|
|
3579
4380
|
dlob,
|
|
3580
|
-
|
|
4381
|
+
user6.publicKey,
|
|
3581
4382
|
OrderType.TRIGGER_MARKET,
|
|
3582
4383
|
MarketType.SPOT,
|
|
3583
4384
|
7, //orderId
|
|
@@ -3593,7 +4394,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3593
4394
|
// should trigger market sell with below condition
|
|
3594
4395
|
insertTriggerOrderToDLOB(
|
|
3595
4396
|
dlob,
|
|
3596
|
-
|
|
4397
|
+
user7.publicKey,
|
|
3597
4398
|
OrderType.TRIGGER_MARKET,
|
|
3598
4399
|
MarketType.SPOT,
|
|
3599
4400
|
8, //orderId
|
|
@@ -3610,7 +4411,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3610
4411
|
// should NOT trigger market sell with above condition
|
|
3611
4412
|
insertTriggerOrderToDLOB(
|
|
3612
4413
|
dlob,
|
|
3613
|
-
|
|
4414
|
+
user8.publicKey,
|
|
3614
4415
|
OrderType.TRIGGER_MARKET,
|
|
3615
4416
|
MarketType.SPOT,
|
|
3616
4417
|
9, //orderId
|
|
@@ -3626,7 +4427,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3626
4427
|
// should NOT trigger market sell with below condition
|
|
3627
4428
|
insertTriggerOrderToDLOB(
|
|
3628
4429
|
dlob,
|
|
3629
|
-
|
|
4430
|
+
user9.publicKey,
|
|
3630
4431
|
OrderType.TRIGGER_MARKET,
|
|
3631
4432
|
MarketType.SPOT,
|
|
3632
4433
|
10, //orderId
|
|
@@ -3642,7 +4443,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3642
4443
|
// should NOT trigger market buy with above condition
|
|
3643
4444
|
insertTriggerOrderToDLOB(
|
|
3644
4445
|
dlob,
|
|
3645
|
-
|
|
4446
|
+
user10.publicKey,
|
|
3646
4447
|
OrderType.TRIGGER_MARKET,
|
|
3647
4448
|
MarketType.SPOT,
|
|
3648
4449
|
11, //orderId
|
|
@@ -3658,7 +4459,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3658
4459
|
// should NOT trigger market buy with below condition
|
|
3659
4460
|
insertTriggerOrderToDLOB(
|
|
3660
4461
|
dlob,
|
|
3661
|
-
|
|
4462
|
+
user11.publicKey,
|
|
3662
4463
|
OrderType.TRIGGER_MARKET,
|
|
3663
4464
|
MarketType.SPOT,
|
|
3664
4465
|
12, //orderId
|
|
@@ -3688,16 +4489,32 @@ describe('DLOB Spot Tests', () => {
|
|
|
3688
4489
|
it('Test will return expired market orders to fill', () => {
|
|
3689
4490
|
const vAsk = new BN(15);
|
|
3690
4491
|
const vBid = new BN(8);
|
|
3691
|
-
|
|
4492
|
+
|
|
4493
|
+
const mockUserMap = new MockUserMap();
|
|
4494
|
+
const user0 = Keypair.generate();
|
|
4495
|
+
const userAuth0 = Keypair.generate();
|
|
4496
|
+
const user1 = Keypair.generate();
|
|
4497
|
+
const userAuth1 = Keypair.generate();
|
|
4498
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, userAuth0.publicKey);
|
|
4499
|
+
mockUserMap.addUserAccountAuthority(user1.publicKey, userAuth1.publicKey);
|
|
4500
|
+
|
|
4501
|
+
const dlob = new DLOB(
|
|
4502
|
+
mockPerpMarkets,
|
|
4503
|
+
mockSpotMarkets,
|
|
4504
|
+
mockStateAccount,
|
|
4505
|
+
mockUserMap,
|
|
4506
|
+
false
|
|
4507
|
+
);
|
|
3692
4508
|
const marketIndex = 0;
|
|
3693
4509
|
|
|
3694
4510
|
const slot = 20;
|
|
3695
|
-
const
|
|
4511
|
+
const ts = 20;
|
|
4512
|
+
const maxTs = new BN(30);
|
|
3696
4513
|
|
|
3697
4514
|
// non crossing bid
|
|
3698
4515
|
insertOrderToDLOB(
|
|
3699
4516
|
dlob,
|
|
3700
|
-
|
|
4517
|
+
user0.publicKey,
|
|
3701
4518
|
OrderType.MARKET,
|
|
3702
4519
|
MarketType.SPOT,
|
|
3703
4520
|
255, // orderId
|
|
@@ -3708,11 +4525,11 @@ describe('DLOB Spot Tests', () => {
|
|
|
3708
4525
|
vBid,
|
|
3709
4526
|
vAsk,
|
|
3710
4527
|
new BN(slot),
|
|
3711
|
-
|
|
4528
|
+
maxTs
|
|
3712
4529
|
);
|
|
3713
4530
|
insertOrderToDLOB(
|
|
3714
4531
|
dlob,
|
|
3715
|
-
|
|
4532
|
+
user1.publicKey,
|
|
3716
4533
|
OrderType.MARKET,
|
|
3717
4534
|
MarketType.SPOT,
|
|
3718
4535
|
2, // orderId
|
|
@@ -3723,16 +4540,18 @@ describe('DLOB Spot Tests', () => {
|
|
|
3723
4540
|
vAsk,
|
|
3724
4541
|
vBid,
|
|
3725
4542
|
new BN(slot),
|
|
3726
|
-
|
|
4543
|
+
maxTs
|
|
3727
4544
|
);
|
|
3728
4545
|
|
|
3729
4546
|
// order auction is not yet complete, and order is not expired.
|
|
3730
4547
|
const slot0 = slot;
|
|
4548
|
+
const ts0 = ts;
|
|
3731
4549
|
const nodesToFillBefore = dlob.findNodesToFill(
|
|
3732
4550
|
marketIndex,
|
|
3733
4551
|
vBid,
|
|
3734
4552
|
vAsk,
|
|
3735
4553
|
slot0,
|
|
4554
|
+
ts0,
|
|
3736
4555
|
MarketType.SPOT,
|
|
3737
4556
|
{
|
|
3738
4557
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -3744,12 +4563,14 @@ describe('DLOB Spot Tests', () => {
|
|
|
3744
4563
|
expect(nodesToFillBefore.length).to.equal(0);
|
|
3745
4564
|
|
|
3746
4565
|
// should get order to fill after timeInForce
|
|
3747
|
-
const slot1 = slot0 +
|
|
4566
|
+
const slot1 = slot0 + slot * 2; // overshoots expiry
|
|
4567
|
+
const ts1 = ts + ts * 2;
|
|
3748
4568
|
const nodesToFillAfter = dlob.findNodesToFill(
|
|
3749
4569
|
marketIndex,
|
|
3750
4570
|
vBid,
|
|
3751
4571
|
vAsk,
|
|
3752
4572
|
slot1,
|
|
4573
|
+
ts1,
|
|
3753
4574
|
MarketType.SPOT,
|
|
3754
4575
|
{
|
|
3755
4576
|
price: vBid.add(vAsk).div(new BN(2)),
|