-
Notifications
You must be signed in to change notification settings - Fork 14
/
dynamo-repository.test.ts
574 lines (505 loc) · 21.8 KB
/
dynamo-repository.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-nocheck
import { DocumentClient } from 'aws-sdk/clients/dynamodb'
import { ORDER_STATUS, SORT_FIELDS } from '../../lib/entities/Order'
import { DynamoOrdersRepository } from '../../lib/repositories/orders-repository'
import { generateRandomNonce } from '../../lib/util/nonce'
import { currentTimestampInSeconds } from '../../lib/util/time'
jest.mock('../../lib/util/time')
jest.mock('../../lib/util/nonce', () => {
const originalModule = jest.requireActual('../../lib/util/nonce')
return {
...originalModule,
generateRandomNonce: jest.fn(originalModule.generateRandomNonce),
}
})
const dynamoConfig = {
convertEmptyValues: true,
endpoint: 'localhost:8000',
region: 'local-env',
sslEnabled: false,
credentials: {
accessKeyId: 'fakeMyKeyId',
secretAccessKey: 'fakeSecretAccessKey',
},
}
const MOCK_ORDER_1 = {
orderHash: '0x1',
offerer: 'hayden.eth',
encodedOrder: 'order1',
signature: 'sig1',
nonce: '1',
chainId: 1,
orderStatus: ORDER_STATUS.OPEN,
}
const MOCK_ORDER_2 = {
orderHash: '0x2',
offerer: 'riley.eth',
encodedOrder: 'order2',
signature: 'sig2',
nonce: '1',
chainId: 137,
orderStatus: ORDER_STATUS.OPEN,
}
const MOCK_ORDER_3 = {
orderHash: '0x3',
offerer: 'riley.eth',
encodedOrder: 'order3',
signature: 'sig3',
nonce: '2',
chainId: 137,
orderStatus: ORDER_STATUS.FILLED,
}
const MOCK_ORDER_4 = {
orderHash: '0x4',
offerer: 'hayden.eth',
encodedOrder: 'order4',
signature: 'sig4',
nonce: '4',
chainId: 1,
orderStatus: ORDER_STATUS.OPEN,
}
const MOCK_ORDER_5 = {
orderHash: '0x5',
offerer: 'hayden.eth',
encodedOrder: 'order1',
signature: 'sig1',
nonce: '1',
chainId: 1,
orderStatus: ORDER_STATUS.OPEN,
}
const ADDITIONAL_FIELDS_ORDER_1 = {
...MOCK_ORDER_1,
filler: '0x1',
}
const ADDITIONAL_FIELDS_ORDER_2 = {
...MOCK_ORDER_2,
filler: '0x1',
}
const ADDITIONAL_FIELDS_ORDER_3 = {
...MOCK_ORDER_3,
filler: '0x3',
}
const ADDITIONAL_FIELDS_ORDER_4 = {
...MOCK_ORDER_4,
filler: '0x4',
}
const ADDITIONAL_FIELDS_ORDER_5 = {
...MOCK_ORDER_5,
filler: '0x1',
}
const mockedGetCurrentTime = jest.mocked(currentTimestampInSeconds)
const mockTime = (time: number) => {
mockedGetCurrentTime.mockImplementation(() => time.toString())
}
const documentClient = new DocumentClient(dynamoConfig)
const ordersRepository = DynamoOrdersRepository.create(documentClient)
beforeAll(async () => {
mockTime(1)
await ordersRepository.putOrderAndUpdateNonceTransaction(ADDITIONAL_FIELDS_ORDER_1)
mockTime(2)
await ordersRepository.putOrderAndUpdateNonceTransaction(ADDITIONAL_FIELDS_ORDER_2)
mockTime(3)
await ordersRepository.putOrderAndUpdateNonceTransaction(ADDITIONAL_FIELDS_ORDER_3)
})
describe('OrdersRepository put item test', () => {
it('should successfully put an item in table', async () => {
expect(() => {
mockTime(1)
ordersRepository.putOrderAndUpdateNonceTransaction(ADDITIONAL_FIELDS_ORDER_1)
}).not.toThrow()
})
})
describe('OrdersRepository getOrders test', () => {
it('should successfully get orders given an orderHash', async () => {
const orders = await ordersRepository.getOrders(10, { orderHash: MOCK_ORDER_2.orderHash })
expect(orders.orders.length).toEqual(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
it('should return no orders for orderHash', async () => {
const orders = await ordersRepository.getOrders(10, { orderHash: '0x6' })
expect(orders.orders).toEqual([])
})
it('should successfully get orders given orderHashes', async () => {
const orders = await ordersRepository.getOrders(10, {
orderHashes: [MOCK_ORDER_2.orderHash, MOCK_ORDER_3.orderHash],
})
expect(orders.orders.length).toEqual(2)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_3))
expect(orders.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
it('should return no orders for orderHashes', async () => {
const orders = await ordersRepository.getOrders(10, {
orderHashes: ['0x6', '0x7'],
})
expect(orders.orders).toEqual([])
})
it('should successfully get orders given an offerer', async () => {
const queryResult = await ordersRepository.getOrders(10, { offerer: MOCK_ORDER_2.offerer })
expect(queryResult.orders.length).toEqual(2)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_3))
})
it('should return no orders for offerer', async () => {
const orders = await ordersRepository.getOrders(10, { offerer: 'zach.eth' })
expect(orders.orders).toEqual([])
})
it('should successfully get orders given an orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, { orderStatus: ORDER_STATUS.OPEN })
expect(queryResult.orders.length).toEqual(2)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
it('should return no orders for orderStatus', async () => {
const orders = await ordersRepository.getOrders(10, { orderStatus: ORDER_STATUS.INSUFFICIENT_FUNDS })
expect(orders.orders).toEqual([])
})
it('should successfully get orders given an chainId', async () => {
const queryResult = await ordersRepository.getOrders(10, { chainId: 137 })
expect(queryResult.orders.length).toEqual(2)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_3))
})
it('should return no orders for chainId', async () => {
const orders = await ordersRepository.getOrders(10, { chainId: 5 })
expect(orders.orders).toEqual([])
})
it('should successfully get orders given an chainId and filler', async () => {
const queryResult = await ordersRepository.getOrders(10, { chainId: 137, filler: '0x1' })
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
it('should return no orders for chainId and filler', async () => {
const orders = await ordersRepository.getOrders(10, { chainId: 137, filler: '0x5' })
expect(orders.orders).toEqual([])
})
it('should successfully get orders given an chainId and orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, { chainId: 1, orderStatus: ORDER_STATUS.OPEN })
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
})
it('should return no orders for chainId and orderStatus', async () => {
const orders = await ordersRepository.getOrders(10, { chainId: 1, orderStatus: ORDER_STATUS.EXPIRED })
expect(orders.orders).toEqual([])
})
it('should successfully get orders given an chainId, orderStatus, and filler', async () => {
const queryResult = await ordersRepository.getOrders(10, {
chainId: 137,
orderStatus: ORDER_STATUS.FILLED,
filler: '0x3',
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_3))
})
it('should return no orders for chainId and orderStatus', async () => {
const orders = await ordersRepository.getOrders(10, { chainId: 137, orderStatus: ORDER_STATUS.OPEN, filler: '0x3' })
expect(orders.orders).toEqual([])
})
it('should successfully get orders given a filler', async () => {
const queryResult = await ordersRepository.getOrders(10, { filler: ADDITIONAL_FIELDS_ORDER_1.filler })
expect(queryResult.orders.length).toEqual(2)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
it('should return no orders for filler', async () => {
const queryResult = await ordersRepository.getOrders(10, { filler: 'corn' })
expect(queryResult.orders).toEqual([])
})
it('should successfully get orders given a filler and orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, {
filler: ADDITIONAL_FIELDS_ORDER_1.filler,
orderStatus: ORDER_STATUS.OPEN,
})
expect(queryResult.orders.length).toEqual(2)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
it('should return no orders for filler and orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, {
filler: ADDITIONAL_FIELDS_ORDER_1.filler,
orderStatus: ORDER_STATUS.INSUFFICIENT_FUNDS,
})
expect(queryResult.orders).toEqual([])
})
it('should successfully get orders given a filler and offerer', async () => {
const queryResult = await ordersRepository.getOrders(10, {
filler: ADDITIONAL_FIELDS_ORDER_3.filler,
offerer: ADDITIONAL_FIELDS_ORDER_3.offerer,
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_3))
})
it('should return no orders for filler and offerer', async () => {
const queryResult = await ordersRepository.getOrders(10, {
filler: ADDITIONAL_FIELDS_ORDER_4.filler,
offerer: MOCK_ORDER_2.offerer,
})
expect(queryResult.orders).toEqual([])
})
it('should successfully get orders given a filler, offerer, orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, {
filler: ADDITIONAL_FIELDS_ORDER_3.filler,
offerer: ADDITIONAL_FIELDS_ORDER_3.offerer,
orderStatus: ADDITIONAL_FIELDS_ORDER_3.orderStatus,
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders[0].orderHash).toEqual(MOCK_ORDER_3.orderHash)
expect(queryResult.orders[0].filler_offerer_orderStatus).toEqual(
`${ADDITIONAL_FIELDS_ORDER_3.filler}_${MOCK_ORDER_3.offerer}_${MOCK_ORDER_3.orderStatus}`
)
})
it('should return no orders for filler, offerer, orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, {
filler: ADDITIONAL_FIELDS_ORDER_3.filler,
offerer: ADDITIONAL_FIELDS_ORDER_3.offerer,
orderStatus: ADDITIONAL_FIELDS_ORDER_1.orderStatus,
})
expect(queryResult.orders).toEqual([])
})
it('should successfully get orders given an offerer and orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, {
orderStatus: ORDER_STATUS.OPEN,
offerer: MOCK_ORDER_2.offerer,
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders[0]).toEqual(
expect.objectContaining({
...MOCK_ORDER_2,
offerer_orderStatus: `${MOCK_ORDER_2.offerer}_${MOCK_ORDER_2.orderStatus}`,
})
)
})
it('should return no orders for offerer and orderStatus', async () => {
const orders = await ordersRepository.getOrders(10, {
orderStatus: ORDER_STATUS.INSUFFICIENT_FUNDS,
offerer: MOCK_ORDER_1.offerer,
})
expect(orders.orders).toEqual([])
})
it('should return orders for limit', async () => {
await expect(ordersRepository.getOrders(2, {})).rejects.toThrow(
'Invalid query, must query with one of the following params: [orderHash, orderHashes, chainId, orderStatus, swapper, filler]'
)
})
})
describe('OrdersRepository getOrders test with pagination', () => {
it('should successfully page through orders with offerer', async () => {
let orders = await ordersRepository.getOrders(1, { offerer: 'riley.eth' })
expect(orders.orders.length).toEqual(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
orders = await ordersRepository.getOrders(2, { offerer: 'riley.eth' }, orders.cursor)
expect(orders.orders.length).toEqual(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_3))
expect(orders.cursor).toEqual(undefined)
})
it('should successfully page through orders with orderStatus', async () => {
let orders = await ordersRepository.getOrders(1, { orderStatus: ORDER_STATUS.OPEN })
expect(orders.orders.length).toEqual(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
orders = await ordersRepository.getOrders(2, { orderStatus: ORDER_STATUS.OPEN }, orders.cursor)
expect(orders.orders.length).toEqual(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
expect(orders.cursor).toEqual(undefined)
})
it('should successfully page through orders with chainId', async () => {
let orders = await ordersRepository.getOrders(1, { chainId: 137 })
expect(orders.orders.length).toEqual(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
orders = await ordersRepository.getOrders(2, { chainId: 137 }, orders.cursor)
expect(orders.orders.length).toEqual(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_3))
expect(orders.cursor).toEqual(undefined)
})
it('should successfully page through orders with chainId, orderStatus', async () => {
await ordersRepository.putOrderAndUpdateNonceTransaction(ADDITIONAL_FIELDS_ORDER_5)
let orders = await ordersRepository.getOrders(1, { orderStatus: ORDER_STATUS.OPEN, chainId: 1 })
expect(orders.orders.length).toEqual(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_5))
orders = await ordersRepository.getOrders(2, { orderStatus: ORDER_STATUS.OPEN, chainId: 1 }, orders.cursor)
expect(orders.orders.length).toEqual(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(orders.cursor).toEqual(undefined)
})
it('should throw an Error for cursor with the wrong index', async () => {
const orders = await ordersRepository.getOrders(2, { orderStatus: ORDER_STATUS.OPEN })
expect(orders.orders.length).toEqual(2)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_5))
expect(orders.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(() => ordersRepository.getOrders(0, { offerer: 'riley.eth' }, orders.cursor)).rejects.toThrow(
Error('Invalid cursor.')
)
})
it('should throw an Error for cursor with the wrong cursor', async () => {
const orders = await ordersRepository.getOrders(2, { orderStatus: ORDER_STATUS.OPEN })
expect(orders.orders.length).toEqual(2)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_5))
expect(orders.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(() => ordersRepository.getOrders(0, { offerer: 'riley.eth' }, 'wrong_cursor')).rejects.toThrow(
Error('Invalid cursor.')
)
})
})
describe('OrdersRepository getOrders test with sorting', () => {
it('should successfully get order given an offerer and createdAt sort', async () => {
const queryResult = await ordersRepository.getOrders(10, {
offerer: MOCK_ORDER_2.offerer,
sortKey: SORT_FIELDS.CREATED_AT,
sort: 'lte(2)',
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
it('should return no order given an offerer and createdAt sort', async () => {
const queryResult = await ordersRepository.getOrders(10, {
offerer: MOCK_ORDER_2.offerer,
sortKey: SORT_FIELDS.CREATED_AT,
sort: 'lt(2)',
})
expect(queryResult.orders).toEqual([])
})
it('should successfully get orders given an orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, {
orderStatus: ORDER_STATUS.OPEN,
sortKey: SORT_FIELDS.CREATED_AT,
sort: 'gte(2)',
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
it('should return no orders for orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, {
orderStatus: ORDER_STATUS.INSUFFICIENT_FUNDS,
sortKey: SORT_FIELDS.CREATED_AT,
sort: 'gt(2)',
})
expect(queryResult.orders).toEqual([])
})
it('should return all orders for OPEN status and between 1,3 time', async () => {
const queryResult = await ordersRepository.getOrders(10, {
orderStatus: ORDER_STATUS.OPEN,
sortKey: SORT_FIELDS.CREATED_AT,
sort: 'between(1,3)',
desc: true,
})
expect(queryResult.orders.length).toEqual(3)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(queryResult.orders[2]).toEqual(expect.objectContaining(MOCK_ORDER_5))
})
it('should return all orders for OPEN status and between 1,3 time ascending order', async () => {
const queryResult = await ordersRepository.getOrders(10, {
orderStatus: ORDER_STATUS.OPEN,
sortKey: SORT_FIELDS.CREATED_AT,
sort: 'between(1,3)',
desc: false,
})
expect(queryResult.orders.length).toEqual(3)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_5))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(queryResult.orders[2]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
})
describe('OrdersRepository getByHash test', () => {
it('should successfully get an item from table', async () => {
const order1 = await ordersRepository.getByHash(MOCK_ORDER_1.orderHash)
// dynamodb-toolbox auto-generates 'created' and 'modified' fields
expect(order1).toEqual(expect.objectContaining(MOCK_ORDER_1))
const order2 = await ordersRepository.getByHash(MOCK_ORDER_2.orderHash)
expect(order2).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
it('should return undefined if item does not exist', async () => {
const res = await ordersRepository.getByHash('0x4')
expect(res).toBeUndefined()
})
})
describe('OrdersRepository get nonce test', () => {
it('should successfully get last posted nonce from table', async () => {
await ordersRepository.putOrderAndUpdateNonceTransaction({
...MOCK_ORDER_1,
nonce: '4',
})
const nonce = await ordersRepository.getNonceByAddressAndChain('hayden.eth', MOCK_ORDER_1.chainId)
expect(nonce).toEqual('4')
})
it('should get last used nonce when there are multiple orders with that nonce value in the DB', async () => {
await ordersRepository.putOrderAndUpdateNonceTransaction({
...MOCK_ORDER_1,
nonce: '2',
orderHash: '0x4',
})
// at this point, there are three orders in the DB, two with nonce 2
const nonce = await ordersRepository.getNonceByAddressAndChain('hayden.eth', MOCK_ORDER_1.chainId)
expect(nonce).toEqual('2')
})
it('should generate random nonce for new address', async () => {
const res = await ordersRepository.getNonceByAddressAndChain('random.eth')
expect(res).not.toBeUndefined()
expect(generateRandomNonce).toHaveBeenCalled()
})
it('should track nonce for the same address on different chains separately', async () => {
await ordersRepository.putOrderAndUpdateNonceTransaction({
...MOCK_ORDER_2,
nonce: '10',
})
await ordersRepository.putOrderAndUpdateNonceTransaction({
...MOCK_ORDER_2,
chainId: 1,
nonce: '20',
})
const nonce = await ordersRepository.getNonceByAddressAndChain(MOCK_ORDER_2.offerer, MOCK_ORDER_2.chainId)
expect(nonce).toEqual('10')
const nonce2 = await ordersRepository.getNonceByAddressAndChain(MOCK_ORDER_2.offerer, 1)
expect(nonce2).toEqual('20')
})
})
describe('OrdersRepository get order count by offerer test', () => {
it('should successfully return order count by existing offerer', async () => {
mockTime(4)
await ordersRepository.putOrderAndUpdateNonceTransaction(ADDITIONAL_FIELDS_ORDER_4)
expect(await ordersRepository.countOrdersByOffererAndStatus(MOCK_ORDER_4.offerer, ORDER_STATUS.OPEN)).toEqual(3)
})
it('should return 0 for nonexistent offerer', async () => {
expect(await ordersRepository.countOrdersByOffererAndStatus('nonexistent', ORDER_STATUS.OPEN)).toEqual(0)
})
})
describe('OrdersRepository update status test', () => {
it('should successfully update orderStatus of an order identified by orderHash', async () => {
await ordersRepository.updateOrderStatus('0x1', ORDER_STATUS.FILLED, 'txHash', [
{ tokenOut: '0x1', amountOut: '1' },
])
await expect(ordersRepository.getByHash('0x1')).resolves.toMatchObject({
orderStatus: ORDER_STATUS.FILLED,
offerer_orderStatus: `${MOCK_ORDER_1.offerer}_${ORDER_STATUS.FILLED}`,
chainId_orderStatus: `${MOCK_ORDER_1.chainId}_${ORDER_STATUS.FILLED}`,
chainId_orderStatus_filler: `${MOCK_ORDER_1.chainId}_${ORDER_STATUS.FILLED}_${MOCK_ORDER_1.filler}`,
txHash: 'txHash',
settledAmounts: [{ tokenOut: '0x1', amountOut: '1' }],
})
})
it('should throw error if order does not exist', async () => {
await expect(ordersRepository.updateOrderStatus('nonexistent', ORDER_STATUS.FILLED)).rejects.toEqual(
new Error('cannot find order by hash when updating order status')
)
})
})
describe('OrdersRepository delete test', () => {
it('should delete orders by list of orderHashes', async () => {
await ordersRepository.putOrderAndUpdateNonceTransaction({
...MOCK_ORDER_1,
nonce: '1',
})
await ordersRepository.putOrderAndUpdateNonceTransaction({
...MOCK_ORDER_2,
nonce: '2',
})
let order = await ordersRepository.getByHash(MOCK_ORDER_1.orderHash)
expect(order).toBeDefined()
order = await ordersRepository.getByHash(MOCK_ORDER_2.orderHash)
expect(order).toBeDefined()
await ordersRepository.deleteOrders([MOCK_ORDER_1.orderHash, MOCK_ORDER_2.orderHash])
order = await ordersRepository.getByHash(MOCK_ORDER_1.orderHash)
expect(order).not.toBeDefined()
order = await ordersRepository.getByHash(MOCK_ORDER_2.orderHash)
expect(order).not.toBeDefined()
})
})