@drift-labs/sdk 0.2.0-master.31 → 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 +19 -11
- package/lib/dlob/DLOB.js +208 -107
- package/lib/dlob/DLOBNode.js +2 -10
- package/lib/dlob/NodeList.d.ts +1 -0
- package/lib/dlob/NodeList.js +6 -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 +70 -85
- package/lib/types.js +12 -11
- 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/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 +290 -120
- package/src/dlob/DLOBNode.ts +2 -14
- package/src/dlob/NodeList.ts +7 -1
- 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/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/types.ts +75 -61
- package/src/userMap/userMap.ts +25 -1
- package/src/userName.ts +2 -1
- package/tests/bn/test.ts +22 -1
- package/tests/dlob/helpers.ts +252 -81
- package/tests/dlob/test.ts +1115 -215
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) {
|
|
202
|
+
console.log(
|
|
203
|
+
`mkrnode: (mkt: ${isMarketOrder(n.makerNode.order)}, lim: ${isLimitOrder(
|
|
204
|
+
n.makerNode.order
|
|
205
|
+
)})`
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const printOrder = (o: Order) => {
|
|
194
210
|
console.log(
|
|
195
|
-
`
|
|
196
|
-
|
|
197
|
-
)},\texpired: ${
|
|
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()}`
|
|
198
218
|
);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
if (n.node.order) {
|
|
222
|
+
const t = n.node.order;
|
|
223
|
+
console.log(`Taker Order:`);
|
|
224
|
+
printOrder(t);
|
|
199
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 = {
|
|
@@ -284,13 +307,122 @@ describe('DLOB Tests', () => {
|
|
|
284
307
|
expect(foundBids).to.equal(0);
|
|
285
308
|
}
|
|
286
309
|
});
|
|
310
|
+
|
|
311
|
+
it('Can clear DLOB', () => {
|
|
312
|
+
const vAsk = new BN(15);
|
|
313
|
+
const vBid = new BN(10);
|
|
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
|
+
);
|
|
339
|
+
const marketIndex = 0;
|
|
340
|
+
|
|
341
|
+
const slot = 12;
|
|
342
|
+
const oracle = {
|
|
343
|
+
price: vBid.add(vAsk).div(new BN(2)),
|
|
344
|
+
slot: new BN(slot),
|
|
345
|
+
confidence: new BN(1),
|
|
346
|
+
hasSufficientNumberOfDataPoints: true,
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
insertOrderToDLOB(
|
|
350
|
+
dlob,
|
|
351
|
+
user0.publicKey,
|
|
352
|
+
OrderType.LIMIT,
|
|
353
|
+
MarketType.PERP,
|
|
354
|
+
0, // orderId
|
|
355
|
+
marketIndex,
|
|
356
|
+
new BN(9), // price
|
|
357
|
+
BASE_PRECISION, // quantity
|
|
358
|
+
PositionDirection.LONG,
|
|
359
|
+
vBid,
|
|
360
|
+
vAsk
|
|
361
|
+
);
|
|
362
|
+
insertOrderToDLOB(
|
|
363
|
+
dlob,
|
|
364
|
+
user1.publicKey,
|
|
365
|
+
OrderType.LIMIT,
|
|
366
|
+
MarketType.PERP,
|
|
367
|
+
1, // orderId
|
|
368
|
+
marketIndex,
|
|
369
|
+
new BN(8), // price
|
|
370
|
+
BASE_PRECISION, // quantity
|
|
371
|
+
PositionDirection.LONG,
|
|
372
|
+
vBid,
|
|
373
|
+
vAsk
|
|
374
|
+
);
|
|
375
|
+
insertOrderToDLOB(
|
|
376
|
+
dlob,
|
|
377
|
+
user2.publicKey,
|
|
378
|
+
OrderType.LIMIT,
|
|
379
|
+
MarketType.PERP,
|
|
380
|
+
2, // orderId
|
|
381
|
+
marketIndex,
|
|
382
|
+
new BN(8), // price
|
|
383
|
+
BASE_PRECISION, // quantity
|
|
384
|
+
PositionDirection.LONG,
|
|
385
|
+
vBid,
|
|
386
|
+
vAsk
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
const bids = dlob.getBids(
|
|
390
|
+
marketIndex,
|
|
391
|
+
undefined,
|
|
392
|
+
0,
|
|
393
|
+
MarketType.PERP,
|
|
394
|
+
oracle
|
|
395
|
+
);
|
|
396
|
+
let b = 0;
|
|
397
|
+
for (const _bid of bids) {
|
|
398
|
+
b++;
|
|
399
|
+
}
|
|
400
|
+
expect(b).to.equal(3);
|
|
401
|
+
|
|
402
|
+
dlob.clear();
|
|
403
|
+
let thrown = false;
|
|
404
|
+
try {
|
|
405
|
+
const bids1 = dlob.getBids(marketIndex, vBid, 0, MarketType.PERP, oracle);
|
|
406
|
+
bids1.next();
|
|
407
|
+
} catch (e) {
|
|
408
|
+
thrown = true;
|
|
409
|
+
}
|
|
410
|
+
expect(thrown, 'should throw after clearing').to.equal(true);
|
|
411
|
+
});
|
|
287
412
|
});
|
|
288
413
|
|
|
289
414
|
describe('DLOB Perp Tests', () => {
|
|
290
415
|
it('Test proper bids', () => {
|
|
291
416
|
const vAsk = new BN(15);
|
|
292
417
|
const vBid = new BN(10);
|
|
293
|
-
const
|
|
418
|
+
const mockUserMap = new MockUserMap();
|
|
419
|
+
const dlob = new DLOB(
|
|
420
|
+
mockPerpMarkets,
|
|
421
|
+
mockSpotMarkets,
|
|
422
|
+
mockStateAccount,
|
|
423
|
+
mockUserMap,
|
|
424
|
+
false
|
|
425
|
+
);
|
|
294
426
|
const marketIndex = 0;
|
|
295
427
|
|
|
296
428
|
const slot = 12;
|
|
@@ -371,9 +503,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
371
503
|
if (t.isVamm) {
|
|
372
504
|
continue;
|
|
373
505
|
}
|
|
506
|
+
|
|
507
|
+
const user = Keypair.generate();
|
|
508
|
+
const userAuth = Keypair.generate();
|
|
509
|
+
mockUserMap.addUserAccountAuthority(user.publicKey, userAuth.publicKey);
|
|
510
|
+
|
|
374
511
|
insertOrderToDLOB(
|
|
375
512
|
dlob,
|
|
376
|
-
|
|
513
|
+
user.publicKey,
|
|
377
514
|
t.orderType || OrderType.LIMIT,
|
|
378
515
|
MarketType.PERP,
|
|
379
516
|
t.orderId || 0, // orderId
|
|
@@ -393,7 +530,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
393
530
|
const bids = dlob.getBids(marketIndex, vBid, slot, MarketType.PERP, oracle);
|
|
394
531
|
|
|
395
532
|
console.log('The Book Bids:');
|
|
396
|
-
const gotBids = [];
|
|
533
|
+
const gotBids: Array<DLOBNode> = [];
|
|
397
534
|
let countBids = 0;
|
|
398
535
|
for (const bid of bids) {
|
|
399
536
|
gotBids.push(bid);
|
|
@@ -424,7 +561,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
424
561
|
it('Test proper bids on multiple markets', () => {
|
|
425
562
|
const vAsk = new BN(15);
|
|
426
563
|
const vBid = new BN(10);
|
|
427
|
-
const
|
|
564
|
+
const mockUserMap = new MockUserMap();
|
|
565
|
+
const dlob = new DLOB(
|
|
566
|
+
mockPerpMarkets,
|
|
567
|
+
mockSpotMarkets,
|
|
568
|
+
mockStateAccount,
|
|
569
|
+
mockUserMap,
|
|
570
|
+
false
|
|
571
|
+
);
|
|
428
572
|
const marketIndex0 = 0;
|
|
429
573
|
const marketIndex1 = 1;
|
|
430
574
|
|
|
@@ -505,9 +649,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
505
649
|
if (t.isVamm) {
|
|
506
650
|
continue;
|
|
507
651
|
}
|
|
652
|
+
|
|
653
|
+
const user = Keypair.generate();
|
|
654
|
+
const userAuth = Keypair.generate();
|
|
655
|
+
mockUserMap.addUserAccountAuthority(user.publicKey, userAuth.publicKey);
|
|
656
|
+
|
|
508
657
|
insertOrderToDLOB(
|
|
509
658
|
dlob,
|
|
510
|
-
|
|
659
|
+
user.publicKey,
|
|
511
660
|
t.orderType || OrderType.LIMIT,
|
|
512
661
|
MarketType.PERP,
|
|
513
662
|
t.orderId || 0, // orderId
|
|
@@ -567,7 +716,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
567
716
|
it('Test proper asks', () => {
|
|
568
717
|
const vAsk = new BN(15);
|
|
569
718
|
const vBid = new BN(10);
|
|
570
|
-
const
|
|
719
|
+
const mockUserMap = new MockUserMap();
|
|
720
|
+
const dlob = new DLOB(
|
|
721
|
+
mockPerpMarkets,
|
|
722
|
+
mockSpotMarkets,
|
|
723
|
+
mockStateAccount,
|
|
724
|
+
mockUserMap,
|
|
725
|
+
false
|
|
726
|
+
);
|
|
571
727
|
const marketIndex = 0;
|
|
572
728
|
|
|
573
729
|
const slot = 12;
|
|
@@ -648,9 +804,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
648
804
|
if (t.isVamm) {
|
|
649
805
|
continue;
|
|
650
806
|
}
|
|
807
|
+
|
|
808
|
+
const user = Keypair.generate();
|
|
809
|
+
const userAuth = Keypair.generate();
|
|
810
|
+
mockUserMap.addUserAccountAuthority(user.publicKey, userAuth.publicKey);
|
|
811
|
+
|
|
651
812
|
insertOrderToDLOB(
|
|
652
813
|
dlob,
|
|
653
|
-
|
|
814
|
+
user.publicKey,
|
|
654
815
|
t.orderType || OrderType.LIMIT,
|
|
655
816
|
MarketType.PERP,
|
|
656
817
|
t.orderId || 0, // orderId
|
|
@@ -701,7 +862,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
701
862
|
it('Test insert market orders', () => {
|
|
702
863
|
const vAsk = new BN(11);
|
|
703
864
|
const vBid = new BN(10);
|
|
704
|
-
const
|
|
865
|
+
const mockUserMap = new MockUserMap();
|
|
866
|
+
const dlob = new DLOB(
|
|
867
|
+
mockPerpMarkets,
|
|
868
|
+
mockSpotMarkets,
|
|
869
|
+
mockStateAccount,
|
|
870
|
+
mockUserMap,
|
|
871
|
+
false
|
|
872
|
+
);
|
|
705
873
|
const marketIndex = 0;
|
|
706
874
|
const oracle = {
|
|
707
875
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -712,9 +880,13 @@ describe('DLOB Perp Tests', () => {
|
|
|
712
880
|
|
|
713
881
|
// 3 mkt buys
|
|
714
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
|
+
|
|
715
887
|
insertOrderToDLOB(
|
|
716
888
|
dlob,
|
|
717
|
-
|
|
889
|
+
user.publicKey,
|
|
718
890
|
OrderType.MARKET,
|
|
719
891
|
MarketType.PERP,
|
|
720
892
|
i + 1,
|
|
@@ -729,9 +901,13 @@ describe('DLOB Perp Tests', () => {
|
|
|
729
901
|
|
|
730
902
|
// 3 mkt sells
|
|
731
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
|
+
|
|
732
908
|
insertOrderToDLOB(
|
|
733
909
|
dlob,
|
|
734
|
-
|
|
910
|
+
user.publicKey,
|
|
735
911
|
OrderType.MARKET,
|
|
736
912
|
MarketType.PERP,
|
|
737
913
|
i + 1,
|
|
@@ -798,11 +974,39 @@ describe('DLOB Perp Tests', () => {
|
|
|
798
974
|
confidence: new BN(1),
|
|
799
975
|
hasSufficientNumberOfDataPoints: true,
|
|
800
976
|
};
|
|
801
|
-
|
|
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
|
+
);
|
|
802
1006
|
const marketIndex = 0;
|
|
803
1007
|
insertOrderToDLOB(
|
|
804
1008
|
dlob,
|
|
805
|
-
|
|
1009
|
+
user0.publicKey,
|
|
806
1010
|
OrderType.LIMIT,
|
|
807
1011
|
MarketType.PERP,
|
|
808
1012
|
3, // orderId
|
|
@@ -816,7 +1020,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
816
1020
|
|
|
817
1021
|
insertOrderToDLOB(
|
|
818
1022
|
dlob,
|
|
819
|
-
|
|
1023
|
+
user1.publicKey,
|
|
820
1024
|
OrderType.LIMIT,
|
|
821
1025
|
MarketType.PERP,
|
|
822
1026
|
2,
|
|
@@ -830,7 +1034,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
830
1034
|
|
|
831
1035
|
insertOrderToDLOB(
|
|
832
1036
|
dlob,
|
|
833
|
-
|
|
1037
|
+
user2.publicKey,
|
|
834
1038
|
OrderType.LIMIT,
|
|
835
1039
|
MarketType.PERP,
|
|
836
1040
|
1, // orderId
|
|
@@ -844,7 +1048,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
844
1048
|
|
|
845
1049
|
insertOrderToDLOB(
|
|
846
1050
|
dlob,
|
|
847
|
-
|
|
1051
|
+
user3.publicKey,
|
|
848
1052
|
OrderType.LIMIT,
|
|
849
1053
|
MarketType.PERP,
|
|
850
1054
|
1, // orderId
|
|
@@ -858,7 +1062,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
858
1062
|
|
|
859
1063
|
insertOrderToDLOB(
|
|
860
1064
|
dlob,
|
|
861
|
-
|
|
1065
|
+
user4.publicKey,
|
|
862
1066
|
OrderType.LIMIT,
|
|
863
1067
|
MarketType.PERP,
|
|
864
1068
|
2, // orderId
|
|
@@ -872,7 +1076,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
872
1076
|
|
|
873
1077
|
insertOrderToDLOB(
|
|
874
1078
|
dlob,
|
|
875
|
-
|
|
1079
|
+
user5.publicKey,
|
|
876
1080
|
OrderType.LIMIT,
|
|
877
1081
|
MarketType.PERP,
|
|
878
1082
|
3, // orderId
|
|
@@ -942,13 +1146,41 @@ describe('DLOB Perp Tests', () => {
|
|
|
942
1146
|
confidence: new BN(1),
|
|
943
1147
|
hasSufficientNumberOfDataPoints: true,
|
|
944
1148
|
};
|
|
945
|
-
|
|
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
|
+
);
|
|
946
1178
|
const marketIndex = 0;
|
|
947
1179
|
|
|
948
1180
|
// insert floating bids
|
|
949
1181
|
insertOrderToDLOB(
|
|
950
1182
|
dlob,
|
|
951
|
-
|
|
1183
|
+
user0.publicKey,
|
|
952
1184
|
OrderType.LIMIT,
|
|
953
1185
|
MarketType.PERP,
|
|
954
1186
|
1, // orderId
|
|
@@ -959,12 +1191,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
959
1191
|
vBid,
|
|
960
1192
|
vAsk,
|
|
961
1193
|
new BN(slot),
|
|
962
|
-
|
|
1194
|
+
ZERO, // TiF
|
|
963
1195
|
new BN(-1).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
964
1196
|
);
|
|
965
1197
|
insertOrderToDLOB(
|
|
966
1198
|
dlob,
|
|
967
|
-
|
|
1199
|
+
user1.publicKey,
|
|
968
1200
|
OrderType.LIMIT,
|
|
969
1201
|
MarketType.PERP,
|
|
970
1202
|
3, // orderId
|
|
@@ -975,12 +1207,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
975
1207
|
vBid,
|
|
976
1208
|
vAsk,
|
|
977
1209
|
new BN(slot),
|
|
978
|
-
|
|
1210
|
+
ZERO, // TiF
|
|
979
1211
|
new BN(-3).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
980
1212
|
);
|
|
981
1213
|
insertOrderToDLOB(
|
|
982
1214
|
dlob,
|
|
983
|
-
|
|
1215
|
+
user2.publicKey,
|
|
984
1216
|
OrderType.LIMIT,
|
|
985
1217
|
MarketType.PERP,
|
|
986
1218
|
2, // orderId
|
|
@@ -991,14 +1223,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
991
1223
|
vBid,
|
|
992
1224
|
vAsk,
|
|
993
1225
|
new BN(slot),
|
|
994
|
-
|
|
1226
|
+
ZERO, // TiF
|
|
995
1227
|
new BN(-2).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
996
1228
|
);
|
|
997
1229
|
|
|
998
1230
|
// insert floating asks
|
|
999
1231
|
insertOrderToDLOB(
|
|
1000
1232
|
dlob,
|
|
1001
|
-
|
|
1233
|
+
user3.publicKey,
|
|
1002
1234
|
OrderType.LIMIT,
|
|
1003
1235
|
MarketType.PERP,
|
|
1004
1236
|
5, // orderId
|
|
@@ -1009,12 +1241,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1009
1241
|
vAsk,
|
|
1010
1242
|
vBid,
|
|
1011
1243
|
new BN(slot),
|
|
1012
|
-
|
|
1244
|
+
ZERO, // TiF
|
|
1013
1245
|
new BN(2).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
1014
1246
|
);
|
|
1015
1247
|
insertOrderToDLOB(
|
|
1016
1248
|
dlob,
|
|
1017
|
-
|
|
1249
|
+
user4.publicKey,
|
|
1018
1250
|
OrderType.LIMIT,
|
|
1019
1251
|
MarketType.PERP,
|
|
1020
1252
|
6, // orderId
|
|
@@ -1025,12 +1257,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1025
1257
|
vAsk,
|
|
1026
1258
|
vBid,
|
|
1027
1259
|
new BN(slot),
|
|
1028
|
-
|
|
1260
|
+
ZERO, // TiF
|
|
1029
1261
|
new BN(3).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
1030
1262
|
);
|
|
1031
1263
|
insertOrderToDLOB(
|
|
1032
1264
|
dlob,
|
|
1033
|
-
|
|
1265
|
+
user5.publicKey,
|
|
1034
1266
|
OrderType.LIMIT,
|
|
1035
1267
|
MarketType.PERP,
|
|
1036
1268
|
4, // orderId
|
|
@@ -1041,7 +1273,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1041
1273
|
vAsk,
|
|
1042
1274
|
vBid,
|
|
1043
1275
|
new BN(slot),
|
|
1044
|
-
|
|
1276
|
+
ZERO, // TiF
|
|
1045
1277
|
new BN(1).mul(PRICE_PRECISION) // oraclePriceOffset
|
|
1046
1278
|
);
|
|
1047
1279
|
|
|
@@ -1103,16 +1335,40 @@ describe('DLOB Perp Tests', () => {
|
|
|
1103
1335
|
expect(asks).to.equal(4); // vamm ask + 3 orders
|
|
1104
1336
|
});
|
|
1105
1337
|
|
|
1106
|
-
it('Test multiple market orders fill with multiple limit orders', () => {
|
|
1338
|
+
it('Test multiple market orders fill with multiple limit orders', async () => {
|
|
1107
1339
|
const vAsk = new BN(15);
|
|
1108
1340
|
const vBid = new BN(10);
|
|
1109
|
-
|
|
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
|
+
);
|
|
1110
1366
|
const marketIndex = 0;
|
|
1111
1367
|
|
|
1112
1368
|
// insert some limit buys above vamm bid, below ask
|
|
1113
1369
|
insertOrderToDLOB(
|
|
1114
1370
|
dlob,
|
|
1115
|
-
|
|
1371
|
+
user0.publicKey,
|
|
1116
1372
|
OrderType.LIMIT,
|
|
1117
1373
|
MarketType.PERP,
|
|
1118
1374
|
1, // orderId
|
|
@@ -1125,7 +1381,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1125
1381
|
);
|
|
1126
1382
|
insertOrderToDLOB(
|
|
1127
1383
|
dlob,
|
|
1128
|
-
|
|
1384
|
+
user1.publicKey,
|
|
1129
1385
|
OrderType.LIMIT,
|
|
1130
1386
|
MarketType.PERP,
|
|
1131
1387
|
2, // orderId
|
|
@@ -1138,7 +1394,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1138
1394
|
);
|
|
1139
1395
|
insertOrderToDLOB(
|
|
1140
1396
|
dlob,
|
|
1141
|
-
|
|
1397
|
+
user2.publicKey,
|
|
1142
1398
|
OrderType.LIMIT,
|
|
1143
1399
|
MarketType.PERP,
|
|
1144
1400
|
3, // orderId
|
|
@@ -1167,7 +1423,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1167
1423
|
// place two market sell order eating 2 of the limit orders
|
|
1168
1424
|
insertOrderToDLOB(
|
|
1169
1425
|
dlob,
|
|
1170
|
-
|
|
1426
|
+
user3.publicKey,
|
|
1171
1427
|
OrderType.MARKET,
|
|
1172
1428
|
MarketType.PERP,
|
|
1173
1429
|
4, // orderId
|
|
@@ -1180,7 +1436,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1180
1436
|
);
|
|
1181
1437
|
insertOrderToDLOB(
|
|
1182
1438
|
dlob,
|
|
1183
|
-
|
|
1439
|
+
user4.publicKey,
|
|
1184
1440
|
OrderType.MARKET,
|
|
1185
1441
|
MarketType.PERP,
|
|
1186
1442
|
5, // orderId
|
|
@@ -1221,13 +1477,34 @@ describe('DLOB Perp Tests', () => {
|
|
|
1221
1477
|
it('Test one market orders fills two limit orders', () => {
|
|
1222
1478
|
const vAsk = new BN(15);
|
|
1223
1479
|
const vBid = new BN(10);
|
|
1224
|
-
|
|
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
|
+
);
|
|
1225
1502
|
const marketIndex = 0;
|
|
1226
1503
|
|
|
1227
1504
|
// insert some limit sells below vAMM ask, above bid
|
|
1228
1505
|
insertOrderToDLOB(
|
|
1229
1506
|
dlob,
|
|
1230
|
-
|
|
1507
|
+
user0.publicKey,
|
|
1231
1508
|
OrderType.LIMIT,
|
|
1232
1509
|
MarketType.PERP,
|
|
1233
1510
|
1, // orderId
|
|
@@ -1240,7 +1517,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1240
1517
|
);
|
|
1241
1518
|
insertOrderToDLOB(
|
|
1242
1519
|
dlob,
|
|
1243
|
-
|
|
1520
|
+
user1.publicKey,
|
|
1244
1521
|
OrderType.LIMIT,
|
|
1245
1522
|
MarketType.PERP,
|
|
1246
1523
|
2, // orderId
|
|
@@ -1253,7 +1530,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1253
1530
|
);
|
|
1254
1531
|
insertOrderToDLOB(
|
|
1255
1532
|
dlob,
|
|
1256
|
-
|
|
1533
|
+
user2.publicKey,
|
|
1257
1534
|
OrderType.LIMIT,
|
|
1258
1535
|
MarketType.PERP,
|
|
1259
1536
|
3, // orderId
|
|
@@ -1265,14 +1542,17 @@ describe('DLOB Perp Tests', () => {
|
|
|
1265
1542
|
vAsk
|
|
1266
1543
|
);
|
|
1267
1544
|
|
|
1545
|
+
// auction over
|
|
1546
|
+
const endSlot = 12;
|
|
1547
|
+
|
|
1268
1548
|
// should have no crossing orders
|
|
1269
1549
|
const nodesToFillBefore = dlob.findCrossingNodesToFill(
|
|
1270
1550
|
marketIndex,
|
|
1271
|
-
|
|
1551
|
+
endSlot,
|
|
1272
1552
|
MarketType.PERP,
|
|
1273
1553
|
{
|
|
1274
1554
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
1275
|
-
slot: new BN(
|
|
1555
|
+
slot: new BN(endSlot),
|
|
1276
1556
|
confidence: new BN(1),
|
|
1277
1557
|
hasSufficientNumberOfDataPoints: true,
|
|
1278
1558
|
}
|
|
@@ -1282,7 +1562,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1282
1562
|
// place one market buy order eating 2 of the limit orders
|
|
1283
1563
|
insertOrderToDLOB(
|
|
1284
1564
|
dlob,
|
|
1285
|
-
|
|
1565
|
+
user3.publicKey,
|
|
1286
1566
|
OrderType.MARKET,
|
|
1287
1567
|
MarketType.PERP,
|
|
1288
1568
|
4, // orderId
|
|
@@ -1296,34 +1576,70 @@ describe('DLOB Perp Tests', () => {
|
|
|
1296
1576
|
|
|
1297
1577
|
const nodesToFillAfter = dlob.findCrossingNodesToFill(
|
|
1298
1578
|
marketIndex,
|
|
1299
|
-
|
|
1579
|
+
endSlot,
|
|
1300
1580
|
MarketType.PERP,
|
|
1301
1581
|
{
|
|
1302
1582
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
1303
|
-
slot: new BN(
|
|
1583
|
+
slot: new BN(endSlot),
|
|
1304
1584
|
confidence: new BN(1),
|
|
1305
1585
|
hasSufficientNumberOfDataPoints: true,
|
|
1306
1586
|
}
|
|
1307
1587
|
);
|
|
1308
1588
|
console.log(`Filled nodes: ${nodesToFillAfter.length}`);
|
|
1309
1589
|
for (const n of nodesToFillAfter) {
|
|
1310
|
-
printCrossedNodes(n,
|
|
1590
|
+
printCrossedNodes(n, endSlot);
|
|
1311
1591
|
}
|
|
1312
1592
|
expect(nodesToFillAfter.length).to.equal(2);
|
|
1313
1593
|
|
|
1314
1594
|
// taker should fill completely with best maker
|
|
1315
|
-
expect(
|
|
1316
|
-
|
|
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);
|
|
1317
1603
|
|
|
1318
1604
|
// taker should fill completely with second best maker
|
|
1319
|
-
expect(
|
|
1320
|
-
|
|
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);
|
|
1321
1613
|
});
|
|
1322
1614
|
|
|
1323
1615
|
it('Test two market orders to fill one limit order', () => {
|
|
1324
1616
|
const vAsk = new BN(15);
|
|
1325
1617
|
const vBid = new BN(8);
|
|
1326
|
-
|
|
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
|
+
);
|
|
1327
1643
|
const marketIndex = 0;
|
|
1328
1644
|
|
|
1329
1645
|
const slot = 12;
|
|
@@ -1337,7 +1653,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1337
1653
|
// insert some limit sells below vAMM ask, above bid
|
|
1338
1654
|
insertOrderToDLOB(
|
|
1339
1655
|
dlob,
|
|
1340
|
-
|
|
1656
|
+
user0.publicKey,
|
|
1341
1657
|
OrderType.LIMIT,
|
|
1342
1658
|
MarketType.PERP,
|
|
1343
1659
|
1, // orderId
|
|
@@ -1350,7 +1666,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1350
1666
|
);
|
|
1351
1667
|
insertOrderToDLOB(
|
|
1352
1668
|
dlob,
|
|
1353
|
-
|
|
1669
|
+
user1.publicKey,
|
|
1354
1670
|
OrderType.LIMIT,
|
|
1355
1671
|
MarketType.PERP,
|
|
1356
1672
|
2, // orderId
|
|
@@ -1363,7 +1679,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1363
1679
|
);
|
|
1364
1680
|
insertOrderToDLOB(
|
|
1365
1681
|
dlob,
|
|
1366
|
-
|
|
1682
|
+
user2.publicKey,
|
|
1367
1683
|
OrderType.LIMIT,
|
|
1368
1684
|
MarketType.PERP,
|
|
1369
1685
|
3, // orderId
|
|
@@ -1387,7 +1703,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1387
1703
|
// place two market buy orders to eat the best ask
|
|
1388
1704
|
insertOrderToDLOB(
|
|
1389
1705
|
dlob,
|
|
1390
|
-
|
|
1706
|
+
user3.publicKey,
|
|
1391
1707
|
OrderType.MARKET,
|
|
1392
1708
|
MarketType.PERP,
|
|
1393
1709
|
4, // orderId
|
|
@@ -1400,7 +1716,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1400
1716
|
);
|
|
1401
1717
|
insertOrderToDLOB(
|
|
1402
1718
|
dlob,
|
|
1403
|
-
|
|
1719
|
+
user4.publicKey,
|
|
1404
1720
|
OrderType.MARKET,
|
|
1405
1721
|
MarketType.PERP,
|
|
1406
1722
|
5, // orderId
|
|
@@ -1418,7 +1734,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1418
1734
|
MarketType.PERP,
|
|
1419
1735
|
oracle
|
|
1420
1736
|
);
|
|
1421
|
-
const mktNodes = dlob.
|
|
1737
|
+
const mktNodes = dlob.findExpiredNodesToFill(
|
|
1422
1738
|
marketIndex,
|
|
1423
1739
|
slot,
|
|
1424
1740
|
MarketType.PERP
|
|
@@ -1445,7 +1761,52 @@ describe('DLOB Perp Tests', () => {
|
|
|
1445
1761
|
it('Test trigger orders', () => {
|
|
1446
1762
|
const vAsk = new BN(15);
|
|
1447
1763
|
const vBid = new BN(8);
|
|
1448
|
-
|
|
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
|
+
);
|
|
1449
1810
|
const marketIndex = 0;
|
|
1450
1811
|
|
|
1451
1812
|
const slot = 20;
|
|
@@ -1462,7 +1823,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1462
1823
|
// should trigger limit buy with above condition
|
|
1463
1824
|
insertTriggerOrderToDLOB(
|
|
1464
1825
|
dlob,
|
|
1465
|
-
|
|
1826
|
+
user0.publicKey,
|
|
1466
1827
|
OrderType.TRIGGER_LIMIT,
|
|
1467
1828
|
MarketType.PERP,
|
|
1468
1829
|
1, //orderId
|
|
@@ -1478,7 +1839,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1478
1839
|
// should trigger limit sell with above condition
|
|
1479
1840
|
insertTriggerOrderToDLOB(
|
|
1480
1841
|
dlob,
|
|
1481
|
-
|
|
1842
|
+
user1.publicKey,
|
|
1482
1843
|
OrderType.TRIGGER_LIMIT,
|
|
1483
1844
|
MarketType.PERP,
|
|
1484
1845
|
2, //orderId
|
|
@@ -1494,7 +1855,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1494
1855
|
// should trigger market buy with above condition
|
|
1495
1856
|
insertTriggerOrderToDLOB(
|
|
1496
1857
|
dlob,
|
|
1497
|
-
|
|
1858
|
+
user2.publicKey,
|
|
1498
1859
|
OrderType.TRIGGER_MARKET,
|
|
1499
1860
|
MarketType.PERP,
|
|
1500
1861
|
3, //orderId
|
|
@@ -1510,7 +1871,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1510
1871
|
// should trigger market sell with above condition
|
|
1511
1872
|
insertTriggerOrderToDLOB(
|
|
1512
1873
|
dlob,
|
|
1513
|
-
|
|
1874
|
+
user3.publicKey,
|
|
1514
1875
|
OrderType.TRIGGER_MARKET,
|
|
1515
1876
|
MarketType.PERP,
|
|
1516
1877
|
4, //orderId
|
|
@@ -1526,7 +1887,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1526
1887
|
// should trigger limit buy with below condition
|
|
1527
1888
|
insertTriggerOrderToDLOB(
|
|
1528
1889
|
dlob,
|
|
1529
|
-
|
|
1890
|
+
user4.publicKey,
|
|
1530
1891
|
OrderType.TRIGGER_LIMIT,
|
|
1531
1892
|
MarketType.PERP,
|
|
1532
1893
|
5, //orderId
|
|
@@ -1542,7 +1903,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1542
1903
|
// should trigger limit sell with below condition
|
|
1543
1904
|
insertTriggerOrderToDLOB(
|
|
1544
1905
|
dlob,
|
|
1545
|
-
|
|
1906
|
+
user5.publicKey,
|
|
1546
1907
|
OrderType.TRIGGER_LIMIT,
|
|
1547
1908
|
MarketType.PERP,
|
|
1548
1909
|
6, //orderId
|
|
@@ -1558,7 +1919,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1558
1919
|
// should trigger market buy with below condition
|
|
1559
1920
|
insertTriggerOrderToDLOB(
|
|
1560
1921
|
dlob,
|
|
1561
|
-
|
|
1922
|
+
user6.publicKey,
|
|
1562
1923
|
OrderType.TRIGGER_MARKET,
|
|
1563
1924
|
MarketType.PERP,
|
|
1564
1925
|
7, //orderId
|
|
@@ -1574,7 +1935,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1574
1935
|
// should trigger market sell with below condition
|
|
1575
1936
|
insertTriggerOrderToDLOB(
|
|
1576
1937
|
dlob,
|
|
1577
|
-
|
|
1938
|
+
user7.publicKey,
|
|
1578
1939
|
OrderType.TRIGGER_MARKET,
|
|
1579
1940
|
MarketType.PERP,
|
|
1580
1941
|
8, //orderId
|
|
@@ -1591,7 +1952,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1591
1952
|
// should NOT trigger market sell with above condition
|
|
1592
1953
|
insertTriggerOrderToDLOB(
|
|
1593
1954
|
dlob,
|
|
1594
|
-
|
|
1955
|
+
user8.publicKey,
|
|
1595
1956
|
OrderType.TRIGGER_MARKET,
|
|
1596
1957
|
MarketType.PERP,
|
|
1597
1958
|
9, //orderId
|
|
@@ -1607,7 +1968,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1607
1968
|
// should NOT trigger market sell with below condition
|
|
1608
1969
|
insertTriggerOrderToDLOB(
|
|
1609
1970
|
dlob,
|
|
1610
|
-
|
|
1971
|
+
user9.publicKey,
|
|
1611
1972
|
OrderType.TRIGGER_MARKET,
|
|
1612
1973
|
MarketType.PERP,
|
|
1613
1974
|
10, //orderId
|
|
@@ -1623,7 +1984,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1623
1984
|
// should NOT trigger market buy with above condition
|
|
1624
1985
|
insertTriggerOrderToDLOB(
|
|
1625
1986
|
dlob,
|
|
1626
|
-
|
|
1987
|
+
user10.publicKey,
|
|
1627
1988
|
OrderType.TRIGGER_MARKET,
|
|
1628
1989
|
MarketType.PERP,
|
|
1629
1990
|
11, //orderId
|
|
@@ -1639,7 +2000,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1639
2000
|
// should NOT trigger market buy with below condition
|
|
1640
2001
|
insertTriggerOrderToDLOB(
|
|
1641
2002
|
dlob,
|
|
1642
|
-
|
|
2003
|
+
user11.publicKey,
|
|
1643
2004
|
OrderType.TRIGGER_MARKET,
|
|
1644
2005
|
MarketType.PERP,
|
|
1645
2006
|
12, //orderId
|
|
@@ -1669,16 +2030,32 @@ describe('DLOB Perp Tests', () => {
|
|
|
1669
2030
|
it('Test will return expired market orders to fill', () => {
|
|
1670
2031
|
const vAsk = new BN(15);
|
|
1671
2032
|
const vBid = new BN(8);
|
|
1672
|
-
|
|
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
|
+
);
|
|
1673
2049
|
const marketIndex = 0;
|
|
1674
2050
|
|
|
1675
2051
|
const slot = 20;
|
|
1676
|
-
const
|
|
2052
|
+
const ts = 20;
|
|
2053
|
+
const maxTs = new BN(30);
|
|
1677
2054
|
|
|
1678
2055
|
// non crossing bid
|
|
1679
2056
|
insertOrderToDLOB(
|
|
1680
2057
|
dlob,
|
|
1681
|
-
|
|
2058
|
+
user0.publicKey,
|
|
1682
2059
|
OrderType.MARKET,
|
|
1683
2060
|
MarketType.PERP,
|
|
1684
2061
|
255, // orderId
|
|
@@ -1689,11 +2066,11 @@ describe('DLOB Perp Tests', () => {
|
|
|
1689
2066
|
vBid,
|
|
1690
2067
|
vAsk,
|
|
1691
2068
|
new BN(slot),
|
|
1692
|
-
|
|
2069
|
+
maxTs
|
|
1693
2070
|
);
|
|
1694
2071
|
insertOrderToDLOB(
|
|
1695
2072
|
dlob,
|
|
1696
|
-
|
|
2073
|
+
user1.publicKey,
|
|
1697
2074
|
OrderType.MARKET,
|
|
1698
2075
|
MarketType.PERP,
|
|
1699
2076
|
2, // orderId
|
|
@@ -1704,16 +2081,18 @@ describe('DLOB Perp Tests', () => {
|
|
|
1704
2081
|
vAsk,
|
|
1705
2082
|
vBid,
|
|
1706
2083
|
new BN(slot),
|
|
1707
|
-
|
|
2084
|
+
maxTs
|
|
1708
2085
|
);
|
|
1709
2086
|
|
|
1710
2087
|
// order auction is not yet complete, and order is not expired.
|
|
1711
2088
|
const slot0 = slot;
|
|
2089
|
+
const ts0 = ts;
|
|
1712
2090
|
const nodesToFillBefore = dlob.findNodesToFill(
|
|
1713
2091
|
marketIndex,
|
|
1714
2092
|
vBid,
|
|
1715
2093
|
vAsk,
|
|
1716
2094
|
slot0,
|
|
2095
|
+
ts0,
|
|
1717
2096
|
MarketType.PERP,
|
|
1718
2097
|
{
|
|
1719
2098
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -1725,12 +2104,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
1725
2104
|
expect(nodesToFillBefore.length).to.equal(0);
|
|
1726
2105
|
|
|
1727
2106
|
// should get order to fill after timeInForce
|
|
1728
|
-
const slot1 = slot0 +
|
|
2107
|
+
const slot1 = slot0 + 20;
|
|
2108
|
+
const ts1 = ts0 + 20; // overshoots expiry
|
|
1729
2109
|
const nodesToFillAfter = dlob.findNodesToFill(
|
|
1730
2110
|
marketIndex,
|
|
1731
2111
|
vBid,
|
|
1732
2112
|
vAsk,
|
|
1733
2113
|
slot1, // auction is over, and order ix expired
|
|
2114
|
+
ts1,
|
|
1734
2115
|
MarketType.PERP,
|
|
1735
2116
|
{
|
|
1736
2117
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -1753,10 +2134,35 @@ describe('DLOB Perp Tests', () => {
|
|
|
1753
2134
|
it('Test skips vAMM and fills market buy order with floating limit order during auction', () => {
|
|
1754
2135
|
const vAsk = new BN(15).mul(PRICE_PRECISION);
|
|
1755
2136
|
const vBid = new BN(8).mul(PRICE_PRECISION);
|
|
1756
|
-
|
|
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
|
+
);
|
|
1757
2162
|
const marketIndex = 0;
|
|
1758
2163
|
|
|
1759
2164
|
const slot = 12;
|
|
2165
|
+
const ts = 12;
|
|
1760
2166
|
const oracle = {
|
|
1761
2167
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
1762
2168
|
slot: new BN(slot),
|
|
@@ -1767,7 +2173,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1767
2173
|
// insert some floating limit sells above vAMM ask
|
|
1768
2174
|
insertOrderToDLOB(
|
|
1769
2175
|
dlob,
|
|
1770
|
-
|
|
2176
|
+
user0.publicKey,
|
|
1771
2177
|
OrderType.LIMIT,
|
|
1772
2178
|
MarketType.PERP,
|
|
1773
2179
|
1, // orderId
|
|
@@ -1778,12 +2184,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1778
2184
|
vBid,
|
|
1779
2185
|
vAsk,
|
|
1780
2186
|
new BN(slot),
|
|
1781
|
-
|
|
2187
|
+
ZERO,
|
|
1782
2188
|
new BN(1).mul(PRICE_PRECISION)
|
|
1783
2189
|
);
|
|
1784
2190
|
insertOrderToDLOB(
|
|
1785
2191
|
dlob,
|
|
1786
|
-
|
|
2192
|
+
user1.publicKey,
|
|
1787
2193
|
OrderType.LIMIT,
|
|
1788
2194
|
MarketType.PERP,
|
|
1789
2195
|
2, // orderId
|
|
@@ -1794,12 +2200,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1794
2200
|
vBid,
|
|
1795
2201
|
vAsk,
|
|
1796
2202
|
new BN(slot),
|
|
1797
|
-
|
|
2203
|
+
ZERO,
|
|
1798
2204
|
new BN(1).mul(PRICE_PRECISION)
|
|
1799
2205
|
);
|
|
1800
2206
|
insertOrderToDLOB(
|
|
1801
2207
|
dlob,
|
|
1802
|
-
|
|
2208
|
+
user2.publicKey,
|
|
1803
2209
|
OrderType.LIMIT,
|
|
1804
2210
|
MarketType.PERP,
|
|
1805
2211
|
3, // orderId
|
|
@@ -1810,7 +2216,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1810
2216
|
vBid,
|
|
1811
2217
|
vAsk,
|
|
1812
2218
|
new BN(slot),
|
|
1813
|
-
|
|
2219
|
+
ZERO,
|
|
1814
2220
|
new BN(1).mul(PRICE_PRECISION)
|
|
1815
2221
|
);
|
|
1816
2222
|
|
|
@@ -1820,6 +2226,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1820
2226
|
vBid,
|
|
1821
2227
|
vAsk,
|
|
1822
2228
|
slot,
|
|
2229
|
+
ts,
|
|
1823
2230
|
MarketType.PERP,
|
|
1824
2231
|
{
|
|
1825
2232
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -1833,7 +2240,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1833
2240
|
// place two market buy orders to eat the best ask
|
|
1834
2241
|
insertOrderToDLOB(
|
|
1835
2242
|
dlob,
|
|
1836
|
-
|
|
2243
|
+
user3.publicKey,
|
|
1837
2244
|
OrderType.MARKET,
|
|
1838
2245
|
MarketType.PERP,
|
|
1839
2246
|
4, // orderId
|
|
@@ -1846,7 +2253,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1846
2253
|
);
|
|
1847
2254
|
insertOrderToDLOB(
|
|
1848
2255
|
dlob,
|
|
1849
|
-
|
|
2256
|
+
user4.publicKey,
|
|
1850
2257
|
OrderType.MARKET,
|
|
1851
2258
|
MarketType.PERP,
|
|
1852
2259
|
5, // orderId
|
|
@@ -1863,6 +2270,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1863
2270
|
vBid,
|
|
1864
2271
|
vAsk,
|
|
1865
2272
|
slot,
|
|
2273
|
+
ts,
|
|
1866
2274
|
MarketType.PERP,
|
|
1867
2275
|
{
|
|
1868
2276
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -1871,7 +2279,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1871
2279
|
hasSufficientNumberOfDataPoints: true,
|
|
1872
2280
|
}
|
|
1873
2281
|
);
|
|
1874
|
-
const mktNodes = dlob.
|
|
2282
|
+
const mktNodes = dlob.findExpiredNodesToFill(
|
|
1875
2283
|
marketIndex,
|
|
1876
2284
|
slot,
|
|
1877
2285
|
MarketType.PERP
|
|
@@ -1902,10 +2310,35 @@ describe('DLOB Perp Tests', () => {
|
|
|
1902
2310
|
it('Test fills market buy order with better priced vAMM after auction', () => {
|
|
1903
2311
|
const vAsk = new BN(15).mul(PRICE_PRECISION);
|
|
1904
2312
|
const vBid = new BN(8).mul(PRICE_PRECISION);
|
|
1905
|
-
|
|
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
|
+
);
|
|
1906
2338
|
const marketIndex = 0;
|
|
1907
2339
|
|
|
1908
2340
|
const slot = 12;
|
|
2341
|
+
const ts = 12;
|
|
1909
2342
|
const oracle = {
|
|
1910
2343
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
1911
2344
|
slot: new BN(slot),
|
|
@@ -1916,7 +2349,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1916
2349
|
// insert some floating limit sells above vAMM ask
|
|
1917
2350
|
insertOrderToDLOB(
|
|
1918
2351
|
dlob,
|
|
1919
|
-
|
|
2352
|
+
user0.publicKey,
|
|
1920
2353
|
OrderType.LIMIT,
|
|
1921
2354
|
MarketType.PERP,
|
|
1922
2355
|
1, // orderId
|
|
@@ -1927,12 +2360,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1927
2360
|
vBid,
|
|
1928
2361
|
vAsk,
|
|
1929
2362
|
new BN(slot),
|
|
1930
|
-
|
|
2363
|
+
ZERO,
|
|
1931
2364
|
new BN(3).mul(PRICE_PRECISION)
|
|
1932
2365
|
);
|
|
1933
2366
|
insertOrderToDLOB(
|
|
1934
2367
|
dlob,
|
|
1935
|
-
|
|
2368
|
+
user1.publicKey,
|
|
1936
2369
|
OrderType.LIMIT,
|
|
1937
2370
|
MarketType.PERP,
|
|
1938
2371
|
2, // orderId
|
|
@@ -1943,12 +2376,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
1943
2376
|
vBid,
|
|
1944
2377
|
vAsk,
|
|
1945
2378
|
new BN(slot),
|
|
1946
|
-
|
|
2379
|
+
ZERO,
|
|
1947
2380
|
new BN(4).mul(PRICE_PRECISION)
|
|
1948
2381
|
);
|
|
1949
2382
|
insertOrderToDLOB(
|
|
1950
2383
|
dlob,
|
|
1951
|
-
|
|
2384
|
+
user2.publicKey,
|
|
1952
2385
|
OrderType.LIMIT,
|
|
1953
2386
|
MarketType.PERP,
|
|
1954
2387
|
3, // orderId
|
|
@@ -1959,12 +2392,13 @@ describe('DLOB Perp Tests', () => {
|
|
|
1959
2392
|
vBid,
|
|
1960
2393
|
vAsk,
|
|
1961
2394
|
new BN(slot),
|
|
1962
|
-
|
|
2395
|
+
ZERO,
|
|
1963
2396
|
new BN(5).mul(PRICE_PRECISION)
|
|
1964
2397
|
);
|
|
1965
2398
|
|
|
1966
2399
|
// should have no crossing orders
|
|
1967
2400
|
const auctionOverSlot = slot * 10;
|
|
2401
|
+
const auctionOverTs = ts * 10;
|
|
1968
2402
|
const nodesToFillBefore = dlob.findCrossingNodesToFill(
|
|
1969
2403
|
marketIndex,
|
|
1970
2404
|
auctionOverSlot, // auction over
|
|
@@ -1976,7 +2410,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
1976
2410
|
// place two market buy orders
|
|
1977
2411
|
insertOrderToDLOB(
|
|
1978
2412
|
dlob,
|
|
1979
|
-
|
|
2413
|
+
user3.publicKey,
|
|
1980
2414
|
OrderType.MARKET,
|
|
1981
2415
|
MarketType.PERP,
|
|
1982
2416
|
4, // orderId
|
|
@@ -1987,11 +2421,11 @@ describe('DLOB Perp Tests', () => {
|
|
|
1987
2421
|
vBid,
|
|
1988
2422
|
vAsk,
|
|
1989
2423
|
new BN(slot),
|
|
1990
|
-
|
|
2424
|
+
ZERO
|
|
1991
2425
|
);
|
|
1992
2426
|
insertOrderToDLOB(
|
|
1993
2427
|
dlob,
|
|
1994
|
-
|
|
2428
|
+
user4.publicKey,
|
|
1995
2429
|
OrderType.MARKET,
|
|
1996
2430
|
MarketType.PERP,
|
|
1997
2431
|
5, // orderId
|
|
@@ -2002,14 +2436,15 @@ describe('DLOB Perp Tests', () => {
|
|
|
2002
2436
|
vBid,
|
|
2003
2437
|
vAsk,
|
|
2004
2438
|
new BN(slot),
|
|
2005
|
-
|
|
2439
|
+
ZERO
|
|
2006
2440
|
);
|
|
2007
2441
|
|
|
2008
2442
|
const nodesToFillAfter = dlob.findNodesToFill(
|
|
2009
2443
|
marketIndex,
|
|
2010
2444
|
vBid,
|
|
2011
2445
|
vAsk,
|
|
2012
|
-
|
|
2446
|
+
auctionOverTs, // auction in progress
|
|
2447
|
+
ts,
|
|
2013
2448
|
MarketType.PERP,
|
|
2014
2449
|
oracle
|
|
2015
2450
|
);
|
|
@@ -2032,7 +2467,31 @@ describe('DLOB Perp Tests', () => {
|
|
|
2032
2467
|
it('Test skips vAMM and fills market sell order with floating limit buys during auction', () => {
|
|
2033
2468
|
const vAsk = new BN(15).mul(PRICE_PRECISION);
|
|
2034
2469
|
const vBid = new BN(8).mul(PRICE_PRECISION);
|
|
2035
|
-
|
|
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
|
+
);
|
|
2036
2495
|
const marketIndex = 0;
|
|
2037
2496
|
|
|
2038
2497
|
const slot = 12;
|
|
@@ -2046,7 +2505,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2046
2505
|
// insert some floating limit buy below vAMM bid
|
|
2047
2506
|
insertOrderToDLOB(
|
|
2048
2507
|
dlob,
|
|
2049
|
-
|
|
2508
|
+
user0.publicKey,
|
|
2050
2509
|
OrderType.LIMIT,
|
|
2051
2510
|
MarketType.PERP,
|
|
2052
2511
|
2, // orderId
|
|
@@ -2057,12 +2516,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
2057
2516
|
vBid,
|
|
2058
2517
|
vAsk,
|
|
2059
2518
|
new BN(slot),
|
|
2060
|
-
|
|
2519
|
+
ZERO,
|
|
2061
2520
|
new BN(-4).mul(PRICE_PRECISION) // second best bid, but worse than vBid (8): 7.5
|
|
2062
2521
|
);
|
|
2063
2522
|
insertOrderToDLOB(
|
|
2064
2523
|
dlob,
|
|
2065
|
-
|
|
2524
|
+
user1.publicKey,
|
|
2066
2525
|
OrderType.LIMIT,
|
|
2067
2526
|
MarketType.PERP,
|
|
2068
2527
|
3, // orderId
|
|
@@ -2073,12 +2532,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
2073
2532
|
vBid,
|
|
2074
2533
|
vAsk,
|
|
2075
2534
|
new BN(slot),
|
|
2076
|
-
|
|
2535
|
+
ZERO,
|
|
2077
2536
|
new BN(-1).mul(PRICE_PRECISION) // best price: 10.5
|
|
2078
2537
|
);
|
|
2079
2538
|
insertOrderToDLOB(
|
|
2080
2539
|
dlob,
|
|
2081
|
-
|
|
2540
|
+
user2.publicKey,
|
|
2082
2541
|
OrderType.LIMIT,
|
|
2083
2542
|
MarketType.PERP,
|
|
2084
2543
|
1, // orderId
|
|
@@ -2089,7 +2548,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2089
2548
|
vBid,
|
|
2090
2549
|
vAsk,
|
|
2091
2550
|
new BN(slot),
|
|
2092
|
-
|
|
2551
|
+
ZERO,
|
|
2093
2552
|
new BN(-5).mul(PRICE_PRECISION) // third best bid, worse than vBid (8): 6.5
|
|
2094
2553
|
);
|
|
2095
2554
|
|
|
@@ -2105,7 +2564,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2105
2564
|
// place two market sell orders to eat the best bid
|
|
2106
2565
|
insertOrderToDLOB(
|
|
2107
2566
|
dlob,
|
|
2108
|
-
|
|
2567
|
+
user3.publicKey,
|
|
2109
2568
|
OrderType.MARKET,
|
|
2110
2569
|
MarketType.PERP,
|
|
2111
2570
|
4, // orderId
|
|
@@ -2118,7 +2577,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2118
2577
|
);
|
|
2119
2578
|
insertOrderToDLOB(
|
|
2120
2579
|
dlob,
|
|
2121
|
-
|
|
2580
|
+
user4.publicKey,
|
|
2122
2581
|
OrderType.MARKET,
|
|
2123
2582
|
MarketType.PERP,
|
|
2124
2583
|
5, // orderId
|
|
@@ -2136,7 +2595,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2136
2595
|
MarketType.PERP,
|
|
2137
2596
|
oracle
|
|
2138
2597
|
);
|
|
2139
|
-
const mktNodes = dlob.
|
|
2598
|
+
const mktNodes = dlob.findExpiredNodesToFill(
|
|
2140
2599
|
marketIndex,
|
|
2141
2600
|
slot,
|
|
2142
2601
|
MarketType.PERP
|
|
@@ -2185,10 +2644,35 @@ describe('DLOB Perp Tests', () => {
|
|
|
2185
2644
|
it('Test fills market sell order with better priced vAMM after auction', () => {
|
|
2186
2645
|
const vAsk = new BN(15).mul(PRICE_PRECISION);
|
|
2187
2646
|
const vBid = new BN(8).mul(PRICE_PRECISION);
|
|
2188
|
-
|
|
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
|
+
);
|
|
2189
2672
|
const marketIndex = 0;
|
|
2190
2673
|
|
|
2191
2674
|
const slot = 12;
|
|
2675
|
+
const ts = 12;
|
|
2192
2676
|
const oracle = {
|
|
2193
2677
|
price: vBid.add(vAsk).div(new BN(2)), // 11.5
|
|
2194
2678
|
slot: new BN(slot),
|
|
@@ -2199,7 +2683,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2199
2683
|
// insert some floating limit buy below vAMM bid
|
|
2200
2684
|
insertOrderToDLOB(
|
|
2201
2685
|
dlob,
|
|
2202
|
-
|
|
2686
|
+
user0.publicKey,
|
|
2203
2687
|
OrderType.LIMIT,
|
|
2204
2688
|
MarketType.PERP,
|
|
2205
2689
|
2, // orderId
|
|
@@ -2210,12 +2694,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
2210
2694
|
vBid,
|
|
2211
2695
|
vAsk,
|
|
2212
2696
|
new BN(slot),
|
|
2213
|
-
|
|
2697
|
+
ZERO,
|
|
2214
2698
|
new BN(-4).mul(PRICE_PRECISION) // second best bid, but worse than vBid (8): 7.5
|
|
2215
2699
|
);
|
|
2216
2700
|
insertOrderToDLOB(
|
|
2217
2701
|
dlob,
|
|
2218
|
-
|
|
2702
|
+
user1.publicKey,
|
|
2219
2703
|
OrderType.LIMIT,
|
|
2220
2704
|
MarketType.PERP,
|
|
2221
2705
|
3, // orderId
|
|
@@ -2226,12 +2710,12 @@ describe('DLOB Perp Tests', () => {
|
|
|
2226
2710
|
vBid,
|
|
2227
2711
|
vAsk,
|
|
2228
2712
|
new BN(slot),
|
|
2229
|
-
|
|
2713
|
+
ZERO,
|
|
2230
2714
|
new BN(-1).mul(PRICE_PRECISION) // best price: 10.5
|
|
2231
2715
|
);
|
|
2232
2716
|
insertOrderToDLOB(
|
|
2233
2717
|
dlob,
|
|
2234
|
-
|
|
2718
|
+
user2.publicKey,
|
|
2235
2719
|
OrderType.LIMIT,
|
|
2236
2720
|
MarketType.PERP,
|
|
2237
2721
|
1, // orderId
|
|
@@ -2242,7 +2726,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2242
2726
|
vBid,
|
|
2243
2727
|
vAsk,
|
|
2244
2728
|
new BN(slot),
|
|
2245
|
-
|
|
2729
|
+
ZERO,
|
|
2246
2730
|
new BN(-5).mul(PRICE_PRECISION) // third best bid, worse than vBid (8): 6.5
|
|
2247
2731
|
);
|
|
2248
2732
|
|
|
@@ -2254,6 +2738,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2254
2738
|
vBid,
|
|
2255
2739
|
vAsk,
|
|
2256
2740
|
slot,
|
|
2741
|
+
ts,
|
|
2257
2742
|
MarketType.PERP,
|
|
2258
2743
|
oracle
|
|
2259
2744
|
);
|
|
@@ -2262,7 +2747,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2262
2747
|
// place two market sell orders to eat the best bid
|
|
2263
2748
|
insertOrderToDLOB(
|
|
2264
2749
|
dlob,
|
|
2265
|
-
|
|
2750
|
+
user3.publicKey,
|
|
2266
2751
|
OrderType.MARKET,
|
|
2267
2752
|
MarketType.PERP,
|
|
2268
2753
|
4, // orderId
|
|
@@ -2276,7 +2761,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2276
2761
|
);
|
|
2277
2762
|
insertOrderToDLOB(
|
|
2278
2763
|
dlob,
|
|
2279
|
-
|
|
2764
|
+
user4.publicKey,
|
|
2280
2765
|
OrderType.MARKET,
|
|
2281
2766
|
MarketType.PERP,
|
|
2282
2767
|
5, // orderId
|
|
@@ -2292,6 +2777,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2292
2777
|
|
|
2293
2778
|
// auction ends, but order not expired
|
|
2294
2779
|
const afterAuctionSlot = slot + 11;
|
|
2780
|
+
const afterAuctionTs = ts + 11;
|
|
2295
2781
|
printBookState(dlob, marketIndex, vBid, vAsk, afterAuctionSlot, oracle);
|
|
2296
2782
|
|
|
2297
2783
|
const nodesToFillAfter = dlob.findNodesToFill(
|
|
@@ -2299,6 +2785,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2299
2785
|
vBid,
|
|
2300
2786
|
vAsk,
|
|
2301
2787
|
afterAuctionSlot,
|
|
2788
|
+
afterAuctionTs,
|
|
2302
2789
|
MarketType.PERP,
|
|
2303
2790
|
oracle
|
|
2304
2791
|
);
|
|
@@ -2334,10 +2821,29 @@ describe('DLOB Perp Tests', () => {
|
|
|
2334
2821
|
it('Test fills crossing bids with vAMM after auction ends', () => {
|
|
2335
2822
|
const vAsk = new BN(15).mul(PRICE_PRECISION);
|
|
2336
2823
|
const vBid = new BN(8).mul(PRICE_PRECISION);
|
|
2337
|
-
|
|
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
|
+
);
|
|
2338
2843
|
const marketIndex = 0;
|
|
2339
2844
|
|
|
2340
2845
|
const slot = 12;
|
|
2846
|
+
const ts = 12;
|
|
2341
2847
|
const oracle = {
|
|
2342
2848
|
price: vBid.add(vAsk).div(new BN(2)), // 11.5
|
|
2343
2849
|
slot: new BN(slot),
|
|
@@ -2348,7 +2854,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2348
2854
|
// insert some floating limit buy below vAMM bid
|
|
2349
2855
|
insertOrderToDLOB(
|
|
2350
2856
|
dlob,
|
|
2351
|
-
|
|
2857
|
+
user0.publicKey,
|
|
2352
2858
|
OrderType.LIMIT,
|
|
2353
2859
|
MarketType.PERP,
|
|
2354
2860
|
2, // orderId
|
|
@@ -2359,11 +2865,11 @@ describe('DLOB Perp Tests', () => {
|
|
|
2359
2865
|
vBid,
|
|
2360
2866
|
vAsk,
|
|
2361
2867
|
new BN(slot),
|
|
2362
|
-
|
|
2868
|
+
new BN(200)
|
|
2363
2869
|
);
|
|
2364
2870
|
insertOrderToDLOB(
|
|
2365
2871
|
dlob,
|
|
2366
|
-
|
|
2872
|
+
user1.publicKey,
|
|
2367
2873
|
OrderType.LIMIT,
|
|
2368
2874
|
MarketType.PERP,
|
|
2369
2875
|
3, // orderId
|
|
@@ -2374,11 +2880,11 @@ describe('DLOB Perp Tests', () => {
|
|
|
2374
2880
|
vBid,
|
|
2375
2881
|
vAsk,
|
|
2376
2882
|
new BN(slot),
|
|
2377
|
-
|
|
2883
|
+
new BN(200)
|
|
2378
2884
|
);
|
|
2379
2885
|
insertOrderToDLOB(
|
|
2380
2886
|
dlob,
|
|
2381
|
-
|
|
2887
|
+
user2.publicKey,
|
|
2382
2888
|
OrderType.LIMIT,
|
|
2383
2889
|
MarketType.PERP,
|
|
2384
2890
|
1, // orderId
|
|
@@ -2389,7 +2895,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2389
2895
|
vBid,
|
|
2390
2896
|
vAsk,
|
|
2391
2897
|
new BN(slot),
|
|
2392
|
-
|
|
2898
|
+
new BN(200)
|
|
2393
2899
|
);
|
|
2394
2900
|
console.log(`Book state before fill:`);
|
|
2395
2901
|
printBookState(dlob, marketIndex, vBid, vAsk, slot, oracle);
|
|
@@ -2399,6 +2905,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
2399
2905
|
vBid,
|
|
2400
2906
|
vAsk,
|
|
2401
2907
|
slot,
|
|
2908
|
+
ts,
|
|
2402
2909
|
MarketType.PERP,
|
|
2403
2910
|
oracle
|
|
2404
2911
|
);
|
|
@@ -2406,12 +2913,14 @@ describe('DLOB Perp Tests', () => {
|
|
|
2406
2913
|
|
|
2407
2914
|
// auction ends now
|
|
2408
2915
|
const afterAuctionSlot = 10 * slot;
|
|
2916
|
+
const afterAuctionTs = 10 * ts;
|
|
2409
2917
|
|
|
2410
2918
|
const nodesToFillAfter = dlob.findNodesToFill(
|
|
2411
2919
|
marketIndex,
|
|
2412
2920
|
vBid,
|
|
2413
2921
|
vAsk,
|
|
2414
2922
|
afterAuctionSlot,
|
|
2923
|
+
afterAuctionTs,
|
|
2415
2924
|
MarketType.PERP,
|
|
2416
2925
|
oracle
|
|
2417
2926
|
);
|
|
@@ -2446,13 +2955,194 @@ describe('DLOB Perp Tests', () => {
|
|
|
2446
2955
|
|
|
2447
2956
|
expect(nodesToFillAfter.length).to.equal(2);
|
|
2448
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
|
+
});
|
|
2449
3132
|
});
|
|
2450
3133
|
|
|
2451
3134
|
describe('DLOB Spot Tests', () => {
|
|
2452
3135
|
it('Test proper bids', () => {
|
|
2453
3136
|
const vAsk = new BN(115);
|
|
2454
3137
|
const vBid = new BN(100);
|
|
2455
|
-
const
|
|
3138
|
+
const mockUserMap = new MockUserMap();
|
|
3139
|
+
const dlob = new DLOB(
|
|
3140
|
+
mockPerpMarkets,
|
|
3141
|
+
mockSpotMarkets,
|
|
3142
|
+
mockStateAccount,
|
|
3143
|
+
mockUserMap,
|
|
3144
|
+
false
|
|
3145
|
+
);
|
|
2456
3146
|
const marketIndex = 0;
|
|
2457
3147
|
|
|
2458
3148
|
const slot = 12;
|
|
@@ -2515,9 +3205,13 @@ describe('DLOB Spot Tests', () => {
|
|
|
2515
3205
|
];
|
|
2516
3206
|
|
|
2517
3207
|
for (const t of testCases) {
|
|
3208
|
+
const user0 = Keypair.generate();
|
|
3209
|
+
const user0Auth = Keypair.generate();
|
|
3210
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
3211
|
+
|
|
2518
3212
|
insertOrderToDLOB(
|
|
2519
3213
|
dlob,
|
|
2520
|
-
|
|
3214
|
+
user0.publicKey,
|
|
2521
3215
|
t.orderType || OrderType.LIMIT,
|
|
2522
3216
|
MarketType.SPOT,
|
|
2523
3217
|
t.orderId || 0, // orderId
|
|
@@ -2566,7 +3260,14 @@ describe('DLOB Spot Tests', () => {
|
|
|
2566
3260
|
it('Test proper bids on multiple markets', () => {
|
|
2567
3261
|
const vAsk = new BN(15);
|
|
2568
3262
|
const vBid = new BN(10);
|
|
2569
|
-
const
|
|
3263
|
+
const mockUserMap = new MockUserMap();
|
|
3264
|
+
const dlob = new DLOB(
|
|
3265
|
+
mockPerpMarkets,
|
|
3266
|
+
mockSpotMarkets,
|
|
3267
|
+
mockStateAccount,
|
|
3268
|
+
mockUserMap,
|
|
3269
|
+
false
|
|
3270
|
+
);
|
|
2570
3271
|
const marketIndex0 = 0;
|
|
2571
3272
|
const marketIndex1 = 1;
|
|
2572
3273
|
|
|
@@ -2637,9 +3338,13 @@ describe('DLOB Spot Tests', () => {
|
|
|
2637
3338
|
];
|
|
2638
3339
|
|
|
2639
3340
|
for (const t of testCases) {
|
|
3341
|
+
const user0 = Keypair.generate();
|
|
3342
|
+
const user0Auth = Keypair.generate();
|
|
3343
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
3344
|
+
|
|
2640
3345
|
insertOrderToDLOB(
|
|
2641
3346
|
dlob,
|
|
2642
|
-
|
|
3347
|
+
user0.publicKey,
|
|
2643
3348
|
t.orderType || OrderType.LIMIT,
|
|
2644
3349
|
MarketType.SPOT,
|
|
2645
3350
|
t.orderId || 0, // orderId
|
|
@@ -2699,7 +3404,14 @@ describe('DLOB Spot Tests', () => {
|
|
|
2699
3404
|
it('Test proper asks', () => {
|
|
2700
3405
|
const vAsk = new BN(15);
|
|
2701
3406
|
const vBid = new BN(10);
|
|
2702
|
-
const
|
|
3407
|
+
const mockUserMap = new MockUserMap();
|
|
3408
|
+
const dlob = new DLOB(
|
|
3409
|
+
mockPerpMarkets,
|
|
3410
|
+
mockSpotMarkets,
|
|
3411
|
+
mockStateAccount,
|
|
3412
|
+
mockUserMap,
|
|
3413
|
+
false
|
|
3414
|
+
);
|
|
2703
3415
|
const marketIndex = 0;
|
|
2704
3416
|
|
|
2705
3417
|
const slot = 12;
|
|
@@ -2762,9 +3474,13 @@ describe('DLOB Spot Tests', () => {
|
|
|
2762
3474
|
];
|
|
2763
3475
|
|
|
2764
3476
|
for (const t of testCases) {
|
|
3477
|
+
const user0 = Keypair.generate();
|
|
3478
|
+
const user0Auth = Keypair.generate();
|
|
3479
|
+
mockUserMap.addUserAccountAuthority(user0.publicKey, user0Auth.publicKey);
|
|
3480
|
+
|
|
2765
3481
|
insertOrderToDLOB(
|
|
2766
3482
|
dlob,
|
|
2767
|
-
|
|
3483
|
+
user0.publicKey,
|
|
2768
3484
|
t.orderType || OrderType.LIMIT,
|
|
2769
3485
|
MarketType.SPOT,
|
|
2770
3486
|
t.orderId || 0, // orderId
|
|
@@ -2821,14 +3537,25 @@ describe('DLOB Spot Tests', () => {
|
|
|
2821
3537
|
confidence: new BN(1),
|
|
2822
3538
|
hasSufficientNumberOfDataPoints: true,
|
|
2823
3539
|
};
|
|
2824
|
-
const
|
|
3540
|
+
const mockUserMap = new MockUserMap();
|
|
3541
|
+
const dlob = new DLOB(
|
|
3542
|
+
mockPerpMarkets,
|
|
3543
|
+
mockSpotMarkets,
|
|
3544
|
+
mockStateAccount,
|
|
3545
|
+
mockUserMap,
|
|
3546
|
+
false
|
|
3547
|
+
);
|
|
2825
3548
|
const marketIndex = 0;
|
|
2826
3549
|
|
|
2827
3550
|
// 3 mkt buys
|
|
2828
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
|
+
|
|
2829
3556
|
insertOrderToDLOB(
|
|
2830
3557
|
dlob,
|
|
2831
|
-
|
|
3558
|
+
user0.publicKey,
|
|
2832
3559
|
OrderType.MARKET,
|
|
2833
3560
|
MarketType.SPOT,
|
|
2834
3561
|
i + 1,
|
|
@@ -2843,9 +3570,13 @@ describe('DLOB Spot Tests', () => {
|
|
|
2843
3570
|
|
|
2844
3571
|
// 3 mkt sells
|
|
2845
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
|
+
|
|
2846
3577
|
insertOrderToDLOB(
|
|
2847
3578
|
dlob,
|
|
2848
|
-
|
|
3579
|
+
user0.publicKey,
|
|
2849
3580
|
OrderType.MARKET,
|
|
2850
3581
|
MarketType.SPOT,
|
|
2851
3582
|
i + 1,
|
|
@@ -2907,11 +3638,39 @@ describe('DLOB Spot Tests', () => {
|
|
|
2907
3638
|
confidence: new BN(1),
|
|
2908
3639
|
hasSufficientNumberOfDataPoints: true,
|
|
2909
3640
|
};
|
|
2910
|
-
|
|
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
|
+
);
|
|
2911
3670
|
const marketIndex = 0;
|
|
2912
3671
|
insertOrderToDLOB(
|
|
2913
3672
|
dlob,
|
|
2914
|
-
|
|
3673
|
+
user0.publicKey,
|
|
2915
3674
|
OrderType.LIMIT,
|
|
2916
3675
|
MarketType.SPOT,
|
|
2917
3676
|
3, // orderId
|
|
@@ -2925,7 +3684,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
2925
3684
|
|
|
2926
3685
|
insertOrderToDLOB(
|
|
2927
3686
|
dlob,
|
|
2928
|
-
|
|
3687
|
+
user1.publicKey,
|
|
2929
3688
|
OrderType.LIMIT,
|
|
2930
3689
|
MarketType.SPOT,
|
|
2931
3690
|
2, // orderId
|
|
@@ -2939,7 +3698,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
2939
3698
|
|
|
2940
3699
|
insertOrderToDLOB(
|
|
2941
3700
|
dlob,
|
|
2942
|
-
|
|
3701
|
+
user2.publicKey,
|
|
2943
3702
|
OrderType.LIMIT,
|
|
2944
3703
|
MarketType.SPOT,
|
|
2945
3704
|
1, // orderId
|
|
@@ -2953,7 +3712,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
2953
3712
|
|
|
2954
3713
|
insertOrderToDLOB(
|
|
2955
3714
|
dlob,
|
|
2956
|
-
|
|
3715
|
+
user3.publicKey,
|
|
2957
3716
|
OrderType.LIMIT,
|
|
2958
3717
|
MarketType.SPOT,
|
|
2959
3718
|
1, // orderId
|
|
@@ -2967,7 +3726,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
2967
3726
|
|
|
2968
3727
|
insertOrderToDLOB(
|
|
2969
3728
|
dlob,
|
|
2970
|
-
|
|
3729
|
+
user4.publicKey,
|
|
2971
3730
|
OrderType.LIMIT,
|
|
2972
3731
|
MarketType.SPOT,
|
|
2973
3732
|
2, // orderId
|
|
@@ -2981,7 +3740,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
2981
3740
|
|
|
2982
3741
|
insertOrderToDLOB(
|
|
2983
3742
|
dlob,
|
|
2984
|
-
|
|
3743
|
+
user5.publicKey,
|
|
2985
3744
|
OrderType.LIMIT,
|
|
2986
3745
|
MarketType.SPOT,
|
|
2987
3746
|
3, // orderId
|
|
@@ -3043,13 +3802,41 @@ describe('DLOB Spot Tests', () => {
|
|
|
3043
3802
|
it('Test multiple market orders fill with multiple limit orders', () => {
|
|
3044
3803
|
const vAsk = new BN(15);
|
|
3045
3804
|
const vBid = new BN(10);
|
|
3046
|
-
|
|
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
|
+
);
|
|
3047
3834
|
const marketIndex = 0;
|
|
3048
3835
|
|
|
3049
3836
|
// insert some limit buys above vamm bid, below ask
|
|
3050
3837
|
insertOrderToDLOB(
|
|
3051
3838
|
dlob,
|
|
3052
|
-
|
|
3839
|
+
user0.publicKey,
|
|
3053
3840
|
OrderType.LIMIT,
|
|
3054
3841
|
MarketType.SPOT,
|
|
3055
3842
|
1, // orderId
|
|
@@ -3062,7 +3849,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3062
3849
|
);
|
|
3063
3850
|
insertOrderToDLOB(
|
|
3064
3851
|
dlob,
|
|
3065
|
-
|
|
3852
|
+
user1.publicKey,
|
|
3066
3853
|
OrderType.LIMIT,
|
|
3067
3854
|
MarketType.SPOT,
|
|
3068
3855
|
2, // orderId
|
|
@@ -3075,7 +3862,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3075
3862
|
);
|
|
3076
3863
|
insertOrderToDLOB(
|
|
3077
3864
|
dlob,
|
|
3078
|
-
|
|
3865
|
+
user2.publicKey,
|
|
3079
3866
|
OrderType.LIMIT,
|
|
3080
3867
|
MarketType.SPOT,
|
|
3081
3868
|
3, // orderId
|
|
@@ -3104,7 +3891,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3104
3891
|
// place two market sell order eating 2 of the limit orders
|
|
3105
3892
|
insertOrderToDLOB(
|
|
3106
3893
|
dlob,
|
|
3107
|
-
|
|
3894
|
+
user4.publicKey,
|
|
3108
3895
|
OrderType.MARKET,
|
|
3109
3896
|
MarketType.SPOT,
|
|
3110
3897
|
4, // orderId
|
|
@@ -3117,7 +3904,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3117
3904
|
);
|
|
3118
3905
|
insertOrderToDLOB(
|
|
3119
3906
|
dlob,
|
|
3120
|
-
|
|
3907
|
+
user5.publicKey,
|
|
3121
3908
|
OrderType.MARKET,
|
|
3122
3909
|
MarketType.SPOT,
|
|
3123
3910
|
5, // orderId
|
|
@@ -3159,13 +3946,35 @@ describe('DLOB Spot Tests', () => {
|
|
|
3159
3946
|
it('Test one market order fills two limit orders', () => {
|
|
3160
3947
|
const vAsk = new BN(15);
|
|
3161
3948
|
const vBid = new BN(10);
|
|
3162
|
-
|
|
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
|
+
);
|
|
3163
3972
|
const marketIndex = 0;
|
|
3164
3973
|
|
|
3165
3974
|
// insert some limit sells below vAMM ask, above bid
|
|
3166
3975
|
insertOrderToDLOB(
|
|
3167
3976
|
dlob,
|
|
3168
|
-
|
|
3977
|
+
user0.publicKey,
|
|
3169
3978
|
OrderType.LIMIT,
|
|
3170
3979
|
MarketType.SPOT,
|
|
3171
3980
|
1, // orderId
|
|
@@ -3178,7 +3987,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3178
3987
|
);
|
|
3179
3988
|
insertOrderToDLOB(
|
|
3180
3989
|
dlob,
|
|
3181
|
-
|
|
3990
|
+
user1.publicKey,
|
|
3182
3991
|
OrderType.LIMIT,
|
|
3183
3992
|
MarketType.SPOT,
|
|
3184
3993
|
2, // orderId
|
|
@@ -3191,7 +4000,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3191
4000
|
);
|
|
3192
4001
|
insertOrderToDLOB(
|
|
3193
4002
|
dlob,
|
|
3194
|
-
|
|
4003
|
+
user2.publicKey,
|
|
3195
4004
|
OrderType.LIMIT,
|
|
3196
4005
|
MarketType.SPOT,
|
|
3197
4006
|
3, // orderId
|
|
@@ -3220,7 +4029,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3220
4029
|
// place one market buy order eating 2 of the limit orders
|
|
3221
4030
|
insertOrderToDLOB(
|
|
3222
4031
|
dlob,
|
|
3223
|
-
|
|
4032
|
+
user3.publicKey,
|
|
3224
4033
|
OrderType.MARKET,
|
|
3225
4034
|
MarketType.SPOT,
|
|
3226
4035
|
4, // orderId
|
|
@@ -3262,7 +4071,32 @@ describe('DLOB Spot Tests', () => {
|
|
|
3262
4071
|
it('Test two market orders to fill one limit order', () => {
|
|
3263
4072
|
const vAsk = new BN(15);
|
|
3264
4073
|
const vBid = new BN(8);
|
|
3265
|
-
|
|
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
|
+
);
|
|
3266
4100
|
const marketIndex = 0;
|
|
3267
4101
|
|
|
3268
4102
|
const slot = 12;
|
|
@@ -3276,7 +4110,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3276
4110
|
// insert some limit sells below vAMM ask, above bid
|
|
3277
4111
|
insertOrderToDLOB(
|
|
3278
4112
|
dlob,
|
|
3279
|
-
|
|
4113
|
+
user0.publicKey,
|
|
3280
4114
|
OrderType.LIMIT,
|
|
3281
4115
|
MarketType.SPOT,
|
|
3282
4116
|
1, // orderId
|
|
@@ -3289,7 +4123,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3289
4123
|
);
|
|
3290
4124
|
insertOrderToDLOB(
|
|
3291
4125
|
dlob,
|
|
3292
|
-
|
|
4126
|
+
user1.publicKey,
|
|
3293
4127
|
OrderType.LIMIT,
|
|
3294
4128
|
MarketType.SPOT,
|
|
3295
4129
|
2, // orderId
|
|
@@ -3302,7 +4136,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3302
4136
|
);
|
|
3303
4137
|
insertOrderToDLOB(
|
|
3304
4138
|
dlob,
|
|
3305
|
-
|
|
4139
|
+
user2.publicKey,
|
|
3306
4140
|
OrderType.LIMIT,
|
|
3307
4141
|
MarketType.SPOT,
|
|
3308
4142
|
3, // orderId
|
|
@@ -3326,7 +4160,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3326
4160
|
// place two market buy orders to eat the best ask
|
|
3327
4161
|
insertOrderToDLOB(
|
|
3328
4162
|
dlob,
|
|
3329
|
-
|
|
4163
|
+
user3.publicKey,
|
|
3330
4164
|
OrderType.MARKET,
|
|
3331
4165
|
MarketType.SPOT,
|
|
3332
4166
|
4, // orderId
|
|
@@ -3339,7 +4173,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3339
4173
|
);
|
|
3340
4174
|
insertOrderToDLOB(
|
|
3341
4175
|
dlob,
|
|
3342
|
-
|
|
4176
|
+
user4.publicKey,
|
|
3343
4177
|
OrderType.MARKET,
|
|
3344
4178
|
MarketType.SPOT,
|
|
3345
4179
|
5, // orderId
|
|
@@ -3357,7 +4191,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3357
4191
|
MarketType.SPOT,
|
|
3358
4192
|
oracle
|
|
3359
4193
|
);
|
|
3360
|
-
const mktNodes = dlob.
|
|
4194
|
+
const mktNodes = dlob.findExpiredNodesToFill(
|
|
3361
4195
|
marketIndex,
|
|
3362
4196
|
slot,
|
|
3363
4197
|
MarketType.SPOT
|
|
@@ -3385,7 +4219,53 @@ describe('DLOB Spot Tests', () => {
|
|
|
3385
4219
|
it('Test trigger orders', () => {
|
|
3386
4220
|
const vAsk = new BN(15);
|
|
3387
4221
|
const vBid = new BN(8);
|
|
3388
|
-
|
|
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
|
+
);
|
|
3389
4269
|
const marketIndex = 0;
|
|
3390
4270
|
|
|
3391
4271
|
const slot = 20;
|
|
@@ -3402,7 +4282,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3402
4282
|
// should trigger limit buy with above condition
|
|
3403
4283
|
insertTriggerOrderToDLOB(
|
|
3404
4284
|
dlob,
|
|
3405
|
-
|
|
4285
|
+
user0.publicKey,
|
|
3406
4286
|
OrderType.TRIGGER_LIMIT,
|
|
3407
4287
|
MarketType.SPOT,
|
|
3408
4288
|
1, //orderId
|
|
@@ -3418,7 +4298,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3418
4298
|
// should trigger limit sell with above condition
|
|
3419
4299
|
insertTriggerOrderToDLOB(
|
|
3420
4300
|
dlob,
|
|
3421
|
-
|
|
4301
|
+
user1.publicKey,
|
|
3422
4302
|
OrderType.TRIGGER_LIMIT,
|
|
3423
4303
|
MarketType.SPOT,
|
|
3424
4304
|
2, //orderId
|
|
@@ -3434,7 +4314,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3434
4314
|
// should trigger market buy with above condition
|
|
3435
4315
|
insertTriggerOrderToDLOB(
|
|
3436
4316
|
dlob,
|
|
3437
|
-
|
|
4317
|
+
user2.publicKey,
|
|
3438
4318
|
OrderType.TRIGGER_MARKET,
|
|
3439
4319
|
MarketType.SPOT,
|
|
3440
4320
|
3, //orderId
|
|
@@ -3450,7 +4330,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3450
4330
|
// should trigger market sell with above condition
|
|
3451
4331
|
insertTriggerOrderToDLOB(
|
|
3452
4332
|
dlob,
|
|
3453
|
-
|
|
4333
|
+
user3.publicKey,
|
|
3454
4334
|
OrderType.TRIGGER_MARKET,
|
|
3455
4335
|
MarketType.SPOT,
|
|
3456
4336
|
4, //orderId
|
|
@@ -3466,7 +4346,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3466
4346
|
// should trigger limit buy with below condition
|
|
3467
4347
|
insertTriggerOrderToDLOB(
|
|
3468
4348
|
dlob,
|
|
3469
|
-
|
|
4349
|
+
user4.publicKey,
|
|
3470
4350
|
OrderType.TRIGGER_LIMIT,
|
|
3471
4351
|
MarketType.SPOT,
|
|
3472
4352
|
5, //orderId
|
|
@@ -3482,7 +4362,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3482
4362
|
// should trigger limit sell with below condition
|
|
3483
4363
|
insertTriggerOrderToDLOB(
|
|
3484
4364
|
dlob,
|
|
3485
|
-
|
|
4365
|
+
user5.publicKey,
|
|
3486
4366
|
OrderType.TRIGGER_LIMIT,
|
|
3487
4367
|
MarketType.SPOT,
|
|
3488
4368
|
6, //orderId
|
|
@@ -3498,7 +4378,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3498
4378
|
// should trigger market buy with below condition
|
|
3499
4379
|
insertTriggerOrderToDLOB(
|
|
3500
4380
|
dlob,
|
|
3501
|
-
|
|
4381
|
+
user6.publicKey,
|
|
3502
4382
|
OrderType.TRIGGER_MARKET,
|
|
3503
4383
|
MarketType.SPOT,
|
|
3504
4384
|
7, //orderId
|
|
@@ -3514,7 +4394,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3514
4394
|
// should trigger market sell with below condition
|
|
3515
4395
|
insertTriggerOrderToDLOB(
|
|
3516
4396
|
dlob,
|
|
3517
|
-
|
|
4397
|
+
user7.publicKey,
|
|
3518
4398
|
OrderType.TRIGGER_MARKET,
|
|
3519
4399
|
MarketType.SPOT,
|
|
3520
4400
|
8, //orderId
|
|
@@ -3531,7 +4411,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3531
4411
|
// should NOT trigger market sell with above condition
|
|
3532
4412
|
insertTriggerOrderToDLOB(
|
|
3533
4413
|
dlob,
|
|
3534
|
-
|
|
4414
|
+
user8.publicKey,
|
|
3535
4415
|
OrderType.TRIGGER_MARKET,
|
|
3536
4416
|
MarketType.SPOT,
|
|
3537
4417
|
9, //orderId
|
|
@@ -3547,7 +4427,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3547
4427
|
// should NOT trigger market sell with below condition
|
|
3548
4428
|
insertTriggerOrderToDLOB(
|
|
3549
4429
|
dlob,
|
|
3550
|
-
|
|
4430
|
+
user9.publicKey,
|
|
3551
4431
|
OrderType.TRIGGER_MARKET,
|
|
3552
4432
|
MarketType.SPOT,
|
|
3553
4433
|
10, //orderId
|
|
@@ -3563,7 +4443,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3563
4443
|
// should NOT trigger market buy with above condition
|
|
3564
4444
|
insertTriggerOrderToDLOB(
|
|
3565
4445
|
dlob,
|
|
3566
|
-
|
|
4446
|
+
user10.publicKey,
|
|
3567
4447
|
OrderType.TRIGGER_MARKET,
|
|
3568
4448
|
MarketType.SPOT,
|
|
3569
4449
|
11, //orderId
|
|
@@ -3579,7 +4459,7 @@ describe('DLOB Spot Tests', () => {
|
|
|
3579
4459
|
// should NOT trigger market buy with below condition
|
|
3580
4460
|
insertTriggerOrderToDLOB(
|
|
3581
4461
|
dlob,
|
|
3582
|
-
|
|
4462
|
+
user11.publicKey,
|
|
3583
4463
|
OrderType.TRIGGER_MARKET,
|
|
3584
4464
|
MarketType.SPOT,
|
|
3585
4465
|
12, //orderId
|
|
@@ -3609,16 +4489,32 @@ describe('DLOB Spot Tests', () => {
|
|
|
3609
4489
|
it('Test will return expired market orders to fill', () => {
|
|
3610
4490
|
const vAsk = new BN(15);
|
|
3611
4491
|
const vBid = new BN(8);
|
|
3612
|
-
|
|
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
|
+
);
|
|
3613
4508
|
const marketIndex = 0;
|
|
3614
4509
|
|
|
3615
4510
|
const slot = 20;
|
|
3616
|
-
const
|
|
4511
|
+
const ts = 20;
|
|
4512
|
+
const maxTs = new BN(30);
|
|
3617
4513
|
|
|
3618
4514
|
// non crossing bid
|
|
3619
4515
|
insertOrderToDLOB(
|
|
3620
4516
|
dlob,
|
|
3621
|
-
|
|
4517
|
+
user0.publicKey,
|
|
3622
4518
|
OrderType.MARKET,
|
|
3623
4519
|
MarketType.SPOT,
|
|
3624
4520
|
255, // orderId
|
|
@@ -3629,11 +4525,11 @@ describe('DLOB Spot Tests', () => {
|
|
|
3629
4525
|
vBid,
|
|
3630
4526
|
vAsk,
|
|
3631
4527
|
new BN(slot),
|
|
3632
|
-
|
|
4528
|
+
maxTs
|
|
3633
4529
|
);
|
|
3634
4530
|
insertOrderToDLOB(
|
|
3635
4531
|
dlob,
|
|
3636
|
-
|
|
4532
|
+
user1.publicKey,
|
|
3637
4533
|
OrderType.MARKET,
|
|
3638
4534
|
MarketType.SPOT,
|
|
3639
4535
|
2, // orderId
|
|
@@ -3644,16 +4540,18 @@ describe('DLOB Spot Tests', () => {
|
|
|
3644
4540
|
vAsk,
|
|
3645
4541
|
vBid,
|
|
3646
4542
|
new BN(slot),
|
|
3647
|
-
|
|
4543
|
+
maxTs
|
|
3648
4544
|
);
|
|
3649
4545
|
|
|
3650
4546
|
// order auction is not yet complete, and order is not expired.
|
|
3651
4547
|
const slot0 = slot;
|
|
4548
|
+
const ts0 = ts;
|
|
3652
4549
|
const nodesToFillBefore = dlob.findNodesToFill(
|
|
3653
4550
|
marketIndex,
|
|
3654
4551
|
vBid,
|
|
3655
4552
|
vAsk,
|
|
3656
4553
|
slot0,
|
|
4554
|
+
ts0,
|
|
3657
4555
|
MarketType.SPOT,
|
|
3658
4556
|
{
|
|
3659
4557
|
price: vBid.add(vAsk).div(new BN(2)),
|
|
@@ -3665,12 +4563,14 @@ describe('DLOB Spot Tests', () => {
|
|
|
3665
4563
|
expect(nodesToFillBefore.length).to.equal(0);
|
|
3666
4564
|
|
|
3667
4565
|
// should get order to fill after timeInForce
|
|
3668
|
-
const slot1 = slot0 +
|
|
4566
|
+
const slot1 = slot0 + slot * 2; // overshoots expiry
|
|
4567
|
+
const ts1 = ts + ts * 2;
|
|
3669
4568
|
const nodesToFillAfter = dlob.findNodesToFill(
|
|
3670
4569
|
marketIndex,
|
|
3671
4570
|
vBid,
|
|
3672
4571
|
vAsk,
|
|
3673
4572
|
slot1,
|
|
4573
|
+
ts1,
|
|
3674
4574
|
MarketType.SPOT,
|
|
3675
4575
|
{
|
|
3676
4576
|
price: vBid.add(vAsk).div(new BN(2)),
|