@human-protocol/sdk 1.0.4 → 1.0.6

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.
@@ -1,1339 +0,0 @@
1
- import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest';
2
- import { ethers, BigNumber } from 'ethers';
3
- import EscrowClient from '../src/escrow';
4
- import {
5
- FAKE_ADDRESS,
6
- FAKE_HASH,
7
- FAKE_NETWORK,
8
- FAKE_URL,
9
- VALID_URL,
10
- } from './utils/constants';
11
- import {
12
- ErrorAmountMustBeGreaterThanZero,
13
- ErrorAmountsCannotBeEmptyArray,
14
- ErrorEscrowAddressIsNotProvidedByFactory,
15
- ErrorEscrowDoesNotHaveEnoughBalance,
16
- ErrorHashIsEmptyString,
17
- ErrorInvalidAddress,
18
- ErrorInvalidEscrowAddressProvided,
19
- ErrorInvalidRecordingOracleAddressProvided,
20
- ErrorInvalidReputationOracleAddressProvided,
21
- ErrorInvalidTokenAddress,
22
- ErrorInvalidUrl,
23
- ErrorListOfHandlersCannotBeEmpty,
24
- ErrorRecipientAndAmountsMustBeSameLength,
25
- ErrorRecipientCannotBeEmptyArray,
26
- ErrorTotalFeeMustBeLessThanHundred,
27
- ErrorUrlIsEmptyString,
28
- InvalidEthereumAddressError,
29
- } from '../src/error';
30
- import InitClient from '../src/init';
31
- import {
32
- EscrowFactory__factory,
33
- Escrow__factory,
34
- HMToken__factory,
35
- } from '@human-protocol/core/typechain-types';
36
- import { DEFAULT_TX_ID } from '../src/constants';
37
- import { EscrowStatus } from '../src/types';
38
-
39
- vi.mock('../src/init');
40
-
41
- describe('EscrowClient', () => {
42
- const provider = new ethers.providers.JsonRpcProvider();
43
- let escrowClient: any,
44
- mockSigner: any,
45
- mockEscrowContract: any,
46
- mockEscrowFactoryContract: any,
47
- mockTokenContract: any;
48
-
49
- beforeEach(async () => {
50
- mockSigner = {
51
- ...provider.getSigner(),
52
- getAddress: vi.fn().mockReturnValue(ethers.constants.AddressZero),
53
- };
54
-
55
- mockEscrowContract = {
56
- createEscrow: vi.fn(),
57
- setup: vi.fn(),
58
- createAndSetupEscrow: vi.fn(),
59
- fund: vi.fn(),
60
- storeResults: vi.fn(),
61
- complete: vi.fn(),
62
- bulkPayOut: vi.fn(),
63
- cancel: vi.fn(),
64
- abort: vi.fn(),
65
- addTrustedHandlers: vi.fn(),
66
- getBalance: vi.fn(),
67
- manifestUrl: vi.fn(),
68
- finalResultsUrl: vi.fn(),
69
- token: vi.fn(),
70
- status: vi.fn(),
71
- getLaunchedEscrows: vi.fn(),
72
- getEscrowsFiltered: vi.fn(),
73
- address: ethers.constants.AddressZero,
74
- };
75
-
76
- mockEscrowFactoryContract = {
77
- createEscrow: vi.fn(),
78
- hasEscrow: vi.fn(),
79
- lastEscrow: vi.fn(),
80
- };
81
-
82
- mockTokenContract = {
83
- allowance: vi.fn(),
84
- approve: vi.fn(),
85
- transfer: vi.fn(),
86
- };
87
-
88
- const getClientParamsMock = InitClient.getParams as jest.Mock;
89
- getClientParamsMock.mockResolvedValue({
90
- signerOrProvider: mockSigner,
91
- network: FAKE_NETWORK,
92
- });
93
-
94
- // Mock EscrowFactory__factory.connect to return the mock EscrowFactory
95
- vi.spyOn(EscrowFactory__factory, 'connect').mockReturnValue(
96
- mockEscrowFactoryContract
97
- );
98
-
99
- // Mock Escrow__factory.connect to return the mock Escrow
100
- vi.spyOn(Escrow__factory, 'connect').mockReturnValue(mockEscrowContract);
101
-
102
- // Mock HMToken__factory.connect to return the mock HMToken
103
- vi.spyOn(HMToken__factory, 'connect').mockReturnValue(mockTokenContract);
104
-
105
- escrowClient = new EscrowClient(await InitClient.getParams(mockSigner));
106
-
107
- escrowClient.escrowContract = mockEscrowContract;
108
- escrowClient.tokenContract = mockTokenContract;
109
- escrowClient.escrowFactoryContract = mockEscrowFactoryContract;
110
- });
111
-
112
- afterEach(() => {
113
- vi.restoreAllMocks();
114
- });
115
-
116
- describe('createEscrow', () => {
117
- test('should throw an error if tokenAddress is an invalid address', async () => {
118
- const invalidAddress = FAKE_ADDRESS;
119
-
120
- await expect(
121
- escrowClient.createEscrow(invalidAddress, [
122
- ethers.constants.AddressZero,
123
- ])
124
- ).rejects.toThrow(ErrorInvalidTokenAddress);
125
- });
126
-
127
- test('should throw an error if trustedHandlers contains an invalid address', async () => {
128
- await expect(
129
- escrowClient.createEscrow(ethers.constants.AddressZero, [FAKE_ADDRESS])
130
- ).rejects.toThrow(new InvalidEthereumAddressError(FAKE_ADDRESS));
131
- });
132
-
133
- test('should create an escrow and return its address', async () => {
134
- const tokenAddress = ethers.constants.AddressZero;
135
- const trustedHandlers = [ethers.constants.AddressZero];
136
- const expectedEscrowAddress = ethers.constants.AddressZero;
137
-
138
- // Create a spy object for the createEscrow method
139
- const createEscrowSpy = vi
140
- .spyOn(escrowClient.escrowFactoryContract, 'createEscrow')
141
- .mockImplementation(() => ({
142
- wait: async () => ({
143
- events: [
144
- {
145
- args: {
146
- 1: expectedEscrowAddress,
147
- },
148
- },
149
- ],
150
- }),
151
- }));
152
-
153
- const result = await escrowClient.createEscrow(
154
- tokenAddress,
155
- trustedHandlers
156
- );
157
-
158
- expect(createEscrowSpy).toHaveBeenCalledWith(
159
- tokenAddress,
160
- trustedHandlers
161
- );
162
- expect(result).toBe(expectedEscrowAddress);
163
- });
164
-
165
- test('should throw an error if the create an escrow fails', async () => {
166
- const tokenAddress = ethers.constants.AddressZero;
167
- const trustedHandlers = [ethers.constants.AddressZero];
168
-
169
- escrowClient.escrowFactoryContract.createEscrow.mockRejectedValueOnce(
170
- new Error()
171
- );
172
-
173
- await expect(
174
- escrowClient.createEscrow(tokenAddress, trustedHandlers)
175
- ).rejects.toThrow();
176
-
177
- expect(
178
- escrowClient.escrowFactoryContract.createEscrow
179
- ).toHaveBeenCalledWith(tokenAddress, trustedHandlers);
180
- });
181
- });
182
-
183
- describe('setup', () => {
184
- test('should throw an error if recordingOracle is an invalid address', async () => {
185
- const escrowConfig = {
186
- recordingOracle: FAKE_ADDRESS,
187
- reputationOracle: ethers.constants.AddressZero,
188
- recordingOracleFee: BigNumber.from(10),
189
- reputationOracleFee: BigNumber.from(10),
190
- manifestUrl: VALID_URL,
191
- hash: FAKE_HASH,
192
- };
193
-
194
- await expect(
195
- escrowClient.setup(ethers.constants.AddressZero, escrowConfig)
196
- ).rejects.toThrow(ErrorInvalidRecordingOracleAddressProvided);
197
- });
198
-
199
- test('should throw an error if reputationOracle is an invalid address', async () => {
200
- const escrowConfig = {
201
- recordingOracle: ethers.constants.AddressZero,
202
- reputationOracle: FAKE_ADDRESS,
203
- recordingOracleFee: BigNumber.from(10),
204
- reputationOracleFee: BigNumber.from(10),
205
- manifestUrl: VALID_URL,
206
- hash: FAKE_HASH,
207
- };
208
-
209
- await expect(
210
- escrowClient.setup(ethers.constants.AddressZero, escrowConfig)
211
- ).rejects.toThrow(ErrorInvalidReputationOracleAddressProvided);
212
- });
213
-
214
- test('should throw an error if reputationOracle is an invalid address', async () => {
215
- const escrowConfig = {
216
- recordingOracle: ethers.constants.AddressZero,
217
- reputationOracle: ethers.constants.AddressZero,
218
- recordingOracleFee: BigNumber.from(10),
219
- reputationOracleFee: BigNumber.from(10),
220
- manifestUrl: VALID_URL,
221
- hash: FAKE_HASH,
222
- };
223
-
224
- await expect(
225
- escrowClient.setup(FAKE_ADDRESS, escrowConfig)
226
- ).rejects.toThrow(ErrorInvalidEscrowAddressProvided);
227
- });
228
-
229
- test('should throw an error if hasEscrow returns false', async () => {
230
- const escrowConfig = {
231
- recordingOracle: ethers.constants.AddressZero,
232
- reputationOracle: ethers.constants.AddressZero,
233
- recordingOracleFee: BigNumber.from(10),
234
- reputationOracleFee: BigNumber.from(10),
235
- manifestUrl: VALID_URL,
236
- hash: FAKE_HASH,
237
- };
238
-
239
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
240
-
241
- await expect(
242
- escrowClient.setup(ethers.constants.AddressZero, escrowConfig)
243
- ).rejects.toThrow(ErrorEscrowAddressIsNotProvidedByFactory);
244
- });
245
-
246
- test('should throw an error if 0 <= recordingOracleFee or 0 <= reputationOracleFee', async () => {
247
- const escrowConfig = {
248
- recordingOracle: ethers.constants.AddressZero,
249
- reputationOracle: ethers.constants.AddressZero,
250
- recordingOracleFee: BigNumber.from(0),
251
- reputationOracleFee: BigNumber.from(0),
252
- manifestUrl: VALID_URL,
253
- hash: FAKE_HASH,
254
- };
255
-
256
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
257
-
258
- await expect(
259
- escrowClient.setup(ethers.constants.AddressZero, escrowConfig)
260
- ).rejects.toThrow(ErrorAmountMustBeGreaterThanZero);
261
- });
262
-
263
- test('should throw an error if recordingOracleFee > 100 or reputationOracleFee > 100', async () => {
264
- const escrowConfig = {
265
- recordingOracle: ethers.constants.AddressZero,
266
- reputationOracle: ethers.constants.AddressZero,
267
- recordingOracleFee: BigNumber.from(100),
268
- reputationOracleFee: BigNumber.from(100),
269
- manifestUrl: VALID_URL,
270
- hash: FAKE_HASH,
271
- };
272
-
273
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
274
-
275
- await expect(
276
- escrowClient.setup(ethers.constants.AddressZero, escrowConfig)
277
- ).rejects.toThrow(ErrorTotalFeeMustBeLessThanHundred);
278
- });
279
-
280
- test('should throw an error if manifestUrl is an empty string', async () => {
281
- const escrowConfig = {
282
- recordingOracle: ethers.constants.AddressZero,
283
- reputationOracle: ethers.constants.AddressZero,
284
- recordingOracleFee: BigNumber.from(50),
285
- reputationOracleFee: BigNumber.from(50),
286
- manifestUrl: '',
287
- hash: FAKE_HASH,
288
- };
289
-
290
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
291
-
292
- await expect(
293
- escrowClient.setup(ethers.constants.AddressZero, escrowConfig)
294
- ).rejects.toThrow(ErrorUrlIsEmptyString);
295
- });
296
-
297
- test('should throw an error if manifestUrl is an invalid url', async () => {
298
- const escrowConfig = {
299
- recordingOracle: ethers.constants.AddressZero,
300
- reputationOracle: ethers.constants.AddressZero,
301
- recordingOracleFee: BigNumber.from(50),
302
- reputationOracleFee: BigNumber.from(50),
303
- manifestUrl: FAKE_URL,
304
- hash: FAKE_HASH,
305
- };
306
-
307
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
308
-
309
- await expect(
310
- escrowClient.setup(ethers.constants.AddressZero, escrowConfig)
311
- ).rejects.toThrow(ErrorInvalidUrl);
312
- });
313
-
314
- test('should throw an error if hash is an empty string', async () => {
315
- const escrowConfig = {
316
- recordingOracle: ethers.constants.AddressZero,
317
- reputationOracle: ethers.constants.AddressZero,
318
- recordingOracleFee: BigNumber.from(50),
319
- reputationOracleFee: BigNumber.from(50),
320
- manifestUrl: VALID_URL,
321
- hash: '',
322
- };
323
-
324
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
325
-
326
- await expect(
327
- escrowClient.setup(ethers.constants.AddressZero, escrowConfig)
328
- ).rejects.toThrow(ErrorHashIsEmptyString);
329
- });
330
-
331
- test('should successfully setup escrow', async () => {
332
- const escrowConfig = {
333
- recordingOracle: ethers.constants.AddressZero,
334
- reputationOracle: ethers.constants.AddressZero,
335
- recordingOracleFee: BigNumber.from(50),
336
- reputationOracleFee: BigNumber.from(50),
337
- manifestUrl: VALID_URL,
338
- hash: FAKE_HASH,
339
- };
340
-
341
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
342
- escrowClient.escrowContract.setup.mockReturnValue(true);
343
-
344
- await escrowClient.setup(ethers.constants.AddressZero, escrowConfig);
345
-
346
- expect(escrowClient.escrowContract.setup).toHaveBeenCalledWith(
347
- ethers.constants.AddressZero,
348
- ethers.constants.AddressZero,
349
- BigNumber.from(50),
350
- BigNumber.from(50),
351
- VALID_URL,
352
- FAKE_HASH
353
- );
354
- });
355
-
356
- test('should throw an error if setup escrow fails', async () => {
357
- const escrowConfig = {
358
- recordingOracle: ethers.constants.AddressZero,
359
- reputationOracle: ethers.constants.AddressZero,
360
- recordingOracleFee: BigNumber.from(50),
361
- reputationOracleFee: BigNumber.from(50),
362
- manifestUrl: VALID_URL,
363
- hash: FAKE_HASH,
364
- };
365
-
366
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
367
- escrowClient.escrowContract.setup.mockRejectedValueOnce(new Error());
368
-
369
- await expect(
370
- escrowClient.setup(ethers.constants.AddressZero, escrowConfig)
371
- ).rejects.toThrow();
372
-
373
- expect(escrowClient.escrowContract.setup).toHaveBeenCalledWith(
374
- ethers.constants.AddressZero,
375
- ethers.constants.AddressZero,
376
- BigNumber.from(50),
377
- BigNumber.from(50),
378
- VALID_URL,
379
- FAKE_HASH
380
- );
381
- });
382
- });
383
-
384
- describe('createAndSetupEscrow', () => {
385
- test('should successfully create and setup escrow', async () => {
386
- const escrowAddress = ethers.constants.AddressZero;
387
- const tokenAddress = ethers.constants.AddressZero;
388
- const trustedHandlers = [ethers.constants.AddressZero];
389
- const escrowConfig = {
390
- recordingOracle: ethers.constants.AddressZero,
391
- reputationOracle: ethers.constants.AddressZero,
392
- recordingOracleFee: BigNumber.from(50),
393
- reputationOracleFee: BigNumber.from(50),
394
- manifestUrl: VALID_URL,
395
- hash: FAKE_HASH,
396
- };
397
-
398
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
399
- escrowClient.createEscrow = vi.fn().mockReturnValue(escrowAddress);
400
- escrowClient.escrowContract.setup.mockReturnValue(true);
401
-
402
- await escrowClient.createAndSetupEscrow(
403
- tokenAddress,
404
- trustedHandlers,
405
- escrowConfig
406
- );
407
-
408
- expect(escrowClient.createEscrow).toHaveBeenCalledWith(
409
- tokenAddress,
410
- trustedHandlers
411
- );
412
- expect(escrowClient.escrowContract.setup).toHaveBeenCalledWith(
413
- ethers.constants.AddressZero,
414
- ethers.constants.AddressZero,
415
- BigNumber.from(50),
416
- BigNumber.from(50),
417
- VALID_URL,
418
- FAKE_HASH
419
- );
420
- });
421
-
422
- test('should throw an error if setup escrow fails', async () => {
423
- const escrowConfig = {
424
- recordingOracle: ethers.constants.AddressZero,
425
- reputationOracle: ethers.constants.AddressZero,
426
- recordingOracleFee: BigNumber.from(50),
427
- reputationOracleFee: BigNumber.from(50),
428
- manifestUrl: VALID_URL,
429
- hash: FAKE_HASH,
430
- };
431
-
432
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
433
- escrowClient.escrowContract.setup.mockRejectedValueOnce(new Error());
434
-
435
- await expect(
436
- escrowClient.setup(ethers.constants.AddressZero, escrowConfig)
437
- ).rejects.toThrow();
438
-
439
- expect(escrowClient.escrowContract.setup).toHaveBeenCalledWith(
440
- ethers.constants.AddressZero,
441
- ethers.constants.AddressZero,
442
- BigNumber.from(50),
443
- BigNumber.from(50),
444
- VALID_URL,
445
- FAKE_HASH
446
- );
447
- });
448
- });
449
-
450
- describe('fund', () => {
451
- test('should throw an error if escrowAddress is an invalid address', async () => {
452
- const invalidAddress = FAKE_ADDRESS;
453
- const amount = BigNumber.from(10);
454
-
455
- await expect(escrowClient.fund(invalidAddress, amount)).rejects.toThrow(
456
- ErrorInvalidEscrowAddressProvided
457
- );
458
- });
459
-
460
- test('should throw an error if hasEscrow returns false', async () => {
461
- const escrowAddress = ethers.constants.AddressZero;
462
- const amount = BigNumber.from(10);
463
-
464
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
465
-
466
- await expect(escrowClient.fund(escrowAddress, amount)).rejects.toThrow(
467
- ErrorEscrowAddressIsNotProvidedByFactory
468
- );
469
- });
470
-
471
- test('should throw an error if 0 <= amount', async () => {
472
- const escrowAddress = ethers.constants.AddressZero;
473
- const invalidAmount = BigNumber.from(0);
474
-
475
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
476
-
477
- await expect(
478
- escrowClient.fund(escrowAddress, invalidAmount)
479
- ).rejects.toThrow(ErrorAmountMustBeGreaterThanZero);
480
- });
481
-
482
- test('should successfully fund escrow', async () => {
483
- const tokenAddress = ethers.constants.AddressZero;
484
- const escrowAddress = ethers.constants.AddressZero;
485
- const amount = BigNumber.from(10);
486
-
487
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
488
- escrowClient.escrowContract.token.mockReturnValue(tokenAddress);
489
-
490
- await escrowClient.fund(escrowAddress, amount);
491
-
492
- expect(escrowClient.escrowContract.token).toHaveBeenCalledWith();
493
- expect(escrowClient.tokenContract.transfer).toHaveBeenCalledWith(
494
- escrowAddress,
495
- amount
496
- );
497
- });
498
-
499
- test('should throw an error if setup escrow fails', async () => {
500
- const tokenAddress = ethers.constants.AddressZero;
501
- const escrowAddress = ethers.constants.AddressZero;
502
- const amount = BigNumber.from(10);
503
-
504
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
505
- escrowClient.escrowContract.token.mockReturnValue(tokenAddress);
506
- escrowClient.tokenContract.transfer.mockRejectedValueOnce(new Error());
507
-
508
- await expect(escrowClient.fund(escrowAddress, amount)).rejects.toThrow();
509
-
510
- expect(escrowClient.escrowContract.token).toHaveBeenCalledWith();
511
- });
512
- });
513
-
514
- describe('storeResults', () => {
515
- test('should throw an error if escrowAddress is an invalid address', async () => {
516
- const invalidAddress = FAKE_ADDRESS;
517
- const url = VALID_URL;
518
- const hash = FAKE_HASH;
519
-
520
- await expect(
521
- escrowClient.storeResults(invalidAddress, url, hash)
522
- ).rejects.toThrow(ErrorInvalidEscrowAddressProvided);
523
- });
524
-
525
- test('should throw an error if hasEscrow returns false', async () => {
526
- const escrowAddress = ethers.constants.AddressZero;
527
- const url = VALID_URL;
528
- const hash = FAKE_HASH;
529
-
530
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
531
-
532
- await expect(
533
- escrowClient.storeResults(escrowAddress, url, hash)
534
- ).rejects.toThrow(ErrorEscrowAddressIsNotProvidedByFactory);
535
- });
536
-
537
- test('should throw an error if url is an empty string', async () => {
538
- const escrowAddress = ethers.constants.AddressZero;
539
- const url = '';
540
- const hash = FAKE_HASH;
541
-
542
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
543
-
544
- await expect(
545
- escrowClient.storeResults(escrowAddress, url, hash)
546
- ).rejects.toThrow(ErrorUrlIsEmptyString);
547
- });
548
-
549
- test('should throw an error if results url is invalid url', async () => {
550
- const escrowAddress = ethers.constants.AddressZero;
551
- const url = FAKE_URL;
552
- const hash = FAKE_HASH;
553
-
554
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
555
-
556
- await expect(
557
- escrowClient.storeResults(escrowAddress, url, hash)
558
- ).rejects.toThrow(ErrorInvalidUrl);
559
- });
560
-
561
- test('should throw an error if hash is an empty string', async () => {
562
- const escrowAddress = ethers.constants.AddressZero;
563
- const url = VALID_URL;
564
- const hash = '';
565
-
566
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
567
-
568
- await expect(
569
- escrowClient.storeResults(escrowAddress, url, hash)
570
- ).rejects.toThrow(ErrorHashIsEmptyString);
571
- });
572
-
573
- test('should successfully store results', async () => {
574
- const escrowAddress = ethers.constants.AddressZero;
575
- const url = VALID_URL;
576
- const hash = FAKE_HASH;
577
-
578
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
579
-
580
- await escrowClient.storeResults(escrowAddress, url, hash);
581
-
582
- expect(escrowClient.escrowContract.storeResults).toHaveBeenCalledWith(
583
- url,
584
- hash
585
- );
586
- });
587
-
588
- test('should throw an error if the store results fails', async () => {
589
- const escrowAddress = ethers.constants.AddressZero;
590
- const url = VALID_URL;
591
- const hash = FAKE_HASH;
592
-
593
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
594
- escrowClient.escrowContract.storeResults.mockRejectedValueOnce(
595
- new Error()
596
- );
597
-
598
- await expect(
599
- escrowClient.storeResults(escrowAddress, url, hash)
600
- ).rejects.toThrow();
601
-
602
- expect(escrowClient.escrowContract.storeResults).toHaveBeenCalledWith(
603
- url,
604
- hash
605
- );
606
- });
607
- });
608
-
609
- describe('complete', () => {
610
- test('should throw an error if escrowAddress is an invalid address', async () => {
611
- const invalidAddress = FAKE_ADDRESS;
612
-
613
- await expect(escrowClient.complete(invalidAddress)).rejects.toThrow(
614
- ErrorInvalidEscrowAddressProvided
615
- );
616
- });
617
-
618
- test('should throw an error if hasEscrow returns false', async () => {
619
- const escrowAddress = ethers.constants.AddressZero;
620
-
621
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
622
-
623
- await expect(escrowClient.complete(escrowAddress)).rejects.toThrow(
624
- ErrorEscrowAddressIsNotProvidedByFactory
625
- );
626
- });
627
-
628
- test('should successfully complete escrow', async () => {
629
- const escrowAddress = ethers.constants.AddressZero;
630
-
631
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
632
-
633
- await escrowClient.complete(escrowAddress);
634
-
635
- expect(escrowClient.escrowContract.complete).toHaveBeenCalledWith();
636
- });
637
-
638
- test('should throw an error if the complete fails', async () => {
639
- const escrowAddress = ethers.constants.AddressZero;
640
-
641
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
642
- escrowClient.escrowContract.complete.mockRejectedValueOnce(new Error());
643
-
644
- await expect(escrowClient.complete(escrowAddress)).rejects.toThrow();
645
-
646
- expect(escrowClient.escrowContract.complete).toHaveBeenCalledWith();
647
- });
648
- });
649
-
650
- describe('bulkPayOut', () => {
651
- test('should throw an error if escrowAddress is an invalid address', async () => {
652
- const invalidAddress = FAKE_ADDRESS;
653
- const recipients = [ethers.constants.AddressZero];
654
- const amounts = [BigNumber.from(100)];
655
- const finalResultsUrl = VALID_URL;
656
- const finalResultsHash = FAKE_HASH;
657
-
658
- await expect(
659
- escrowClient.bulkPayOut(
660
- invalidAddress,
661
- recipients,
662
- amounts,
663
- finalResultsUrl,
664
- finalResultsHash
665
- )
666
- ).rejects.toThrow(ErrorInvalidEscrowAddressProvided);
667
- });
668
-
669
- test('should throw an error if hasEscrow returns false', async () => {
670
- const escrowAddress = ethers.constants.AddressZero;
671
- const recipients = [ethers.constants.AddressZero];
672
- const amounts = [BigNumber.from(100)];
673
- const finalResultsUrl = VALID_URL;
674
- const finalResultsHash = FAKE_HASH;
675
-
676
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
677
-
678
- await expect(
679
- escrowClient.bulkPayOut(
680
- escrowAddress,
681
- recipients,
682
- amounts,
683
- finalResultsUrl,
684
- finalResultsHash
685
- )
686
- ).rejects.toThrow(ErrorEscrowAddressIsNotProvidedByFactory);
687
- });
688
-
689
- test('should throw an error if recipients length is equal to 0', async () => {
690
- const escrowAddress = ethers.constants.AddressZero;
691
- const recipients = [];
692
- const amounts = [BigNumber.from(100)];
693
- const finalResultsUrl = VALID_URL;
694
- const finalResultsHash = FAKE_HASH;
695
-
696
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
697
-
698
- await expect(
699
- escrowClient.bulkPayOut(
700
- escrowAddress,
701
- recipients,
702
- amounts,
703
- finalResultsUrl,
704
- finalResultsHash
705
- )
706
- ).rejects.toThrow(ErrorRecipientCannotBeEmptyArray);
707
- });
708
-
709
- test('should throw an error if amounts length is equal to 0', async () => {
710
- const escrowAddress = ethers.constants.AddressZero;
711
- const recipients = [ethers.constants.AddressZero];
712
- const amounts = [];
713
- const finalResultsUrl = VALID_URL;
714
- const finalResultsHash = FAKE_HASH;
715
-
716
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
717
-
718
- await expect(
719
- escrowClient.bulkPayOut(
720
- escrowAddress,
721
- recipients,
722
- amounts,
723
- finalResultsUrl,
724
- finalResultsHash
725
- )
726
- ).rejects.toThrow(ErrorAmountsCannotBeEmptyArray);
727
- });
728
-
729
- test('should throw an error if recipients and amounts do not have the same length', async () => {
730
- const escrowAddress = ethers.constants.AddressZero;
731
- const recipients = [ethers.constants.AddressZero];
732
- const amounts = [
733
- BigNumber.from(100),
734
- BigNumber.from(100),
735
- BigNumber.from(100),
736
- ];
737
- const finalResultsUrl = VALID_URL;
738
- const finalResultsHash = FAKE_HASH;
739
-
740
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
741
-
742
- await expect(
743
- escrowClient.bulkPayOut(
744
- escrowAddress,
745
- recipients,
746
- amounts,
747
- finalResultsUrl,
748
- finalResultsHash
749
- )
750
- ).rejects.toThrow(ErrorRecipientAndAmountsMustBeSameLength);
751
- });
752
-
753
- test('should throw an error if recipients contains invalid addresses', async () => {
754
- const escrowAddress = ethers.constants.AddressZero;
755
- const recipients = [FAKE_ADDRESS];
756
- const amounts = [BigNumber.from(100)];
757
- const finalResultsUrl = VALID_URL;
758
- const finalResultsHash = FAKE_HASH;
759
-
760
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
761
-
762
- await expect(
763
- escrowClient.bulkPayOut(
764
- escrowAddress,
765
- recipients,
766
- amounts,
767
- finalResultsUrl,
768
- finalResultsHash
769
- )
770
- ).rejects.toThrow(new InvalidEthereumAddressError(FAKE_ADDRESS));
771
- });
772
-
773
- test('should throw an error if url is an empty string', async () => {
774
- const escrowAddress = ethers.constants.AddressZero;
775
- const recipients = [ethers.constants.AddressZero];
776
- const amounts = [BigNumber.from(100)];
777
- const finalResultsUrl = '';
778
- const finalResultsHash = FAKE_HASH;
779
-
780
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
781
-
782
- await expect(
783
- escrowClient.bulkPayOut(
784
- escrowAddress,
785
- recipients,
786
- amounts,
787
- finalResultsUrl,
788
- finalResultsHash
789
- )
790
- ).rejects.toThrow(ErrorUrlIsEmptyString);
791
- });
792
-
793
- test('should throw an error if final results url is an invalid url', async () => {
794
- const escrowAddress = ethers.constants.AddressZero;
795
- const recipients = [ethers.constants.AddressZero];
796
- const amounts = [BigNumber.from(100)];
797
- const finalResultsUrl = FAKE_URL;
798
- const finalResultsHash = FAKE_HASH;
799
-
800
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
801
-
802
- await expect(
803
- escrowClient.bulkPayOut(
804
- escrowAddress,
805
- recipients,
806
- amounts,
807
- finalResultsUrl,
808
- finalResultsHash
809
- )
810
- ).rejects.toThrow(ErrorInvalidUrl);
811
- });
812
-
813
- test('should throw an error if hash is an empty string', async () => {
814
- const escrowAddress = ethers.constants.AddressZero;
815
- const recipients = [ethers.constants.AddressZero];
816
- const amounts = [BigNumber.from(100)];
817
- const finalResultsUrl = VALID_URL;
818
- const finalResultsHash = '';
819
-
820
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
821
-
822
- await expect(
823
- escrowClient.bulkPayOut(
824
- escrowAddress,
825
- recipients,
826
- amounts,
827
- finalResultsUrl,
828
- finalResultsHash
829
- )
830
- ).rejects.toThrow(ErrorHashIsEmptyString);
831
- });
832
-
833
- test('should throw an error if escrow does not have enough balance', async () => {
834
- const escrowAddress = ethers.constants.AddressZero;
835
- const recipients = [
836
- ethers.constants.AddressZero,
837
- ethers.constants.AddressZero,
838
- ];
839
- const amounts = [BigNumber.from(90), BigNumber.from(20)];
840
- const finalResultsUrl = VALID_URL;
841
- const finalResultsHash = FAKE_HASH;
842
-
843
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
844
- escrowClient.getBalance = vi.fn().mockReturnValue(BigNumber.from(50));
845
-
846
- await expect(
847
- escrowClient.bulkPayOut(
848
- escrowAddress,
849
- recipients,
850
- amounts,
851
- finalResultsUrl,
852
- finalResultsHash
853
- )
854
- ).rejects.toThrow(ErrorEscrowDoesNotHaveEnoughBalance);
855
- });
856
-
857
- test('should successfully bulkPayOut escrow', async () => {
858
- const escrowAddress = ethers.constants.AddressZero;
859
- const recipients = [
860
- ethers.constants.AddressZero,
861
- ethers.constants.AddressZero,
862
- ];
863
- const amounts = [BigNumber.from(10), BigNumber.from(10)];
864
- const finalResultsUrl = VALID_URL;
865
- const finalResultsHash = FAKE_HASH;
866
-
867
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
868
- escrowClient.getBalance = vi.fn().mockReturnValue(BigNumber.from(100));
869
-
870
- await escrowClient.bulkPayOut(
871
- escrowAddress,
872
- recipients,
873
- amounts,
874
- finalResultsUrl,
875
- finalResultsHash
876
- );
877
-
878
- expect(escrowClient.escrowContract.bulkPayOut).toHaveBeenCalledWith(
879
- recipients,
880
- amounts,
881
- finalResultsUrl,
882
- finalResultsHash,
883
- DEFAULT_TX_ID
884
- );
885
- });
886
-
887
- test('should throw an error if bulkPayOut fails', async () => {
888
- const escrowAddress = ethers.constants.AddressZero;
889
-
890
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
891
- escrowClient.escrowContract.abort.mockRejectedValueOnce(new Error());
892
-
893
- await expect(escrowClient.abort(escrowAddress)).rejects.toThrow();
894
-
895
- expect(escrowClient.escrowContract.abort).toHaveBeenCalledWith();
896
- });
897
- });
898
-
899
- describe('cancel', () => {
900
- test('should throw an error if escrowAddress is an invalid address', async () => {
901
- const invalidAddress = FAKE_ADDRESS;
902
-
903
- await expect(escrowClient.cancel(invalidAddress)).rejects.toThrow(
904
- ErrorInvalidEscrowAddressProvided
905
- );
906
- });
907
-
908
- test('should throw an error if hasEscrow returns false', async () => {
909
- const escrowAddress = ethers.constants.AddressZero;
910
-
911
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
912
-
913
- await expect(escrowClient.cancel(escrowAddress)).rejects.toThrow(
914
- ErrorEscrowAddressIsNotProvidedByFactory
915
- );
916
- });
917
-
918
- test('should successfully cancel escrow', async () => {
919
- const escrowAddress = ethers.constants.AddressZero;
920
-
921
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
922
-
923
- await escrowClient.cancel(escrowAddress);
924
-
925
- expect(escrowClient.escrowContract.cancel).toHaveBeenCalledWith();
926
- });
927
-
928
- test('should throw an error if the cancel fails', async () => {
929
- const escrowAddress = ethers.constants.AddressZero;
930
-
931
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
932
- escrowClient.escrowContract.cancel.mockRejectedValueOnce(new Error());
933
-
934
- await expect(escrowClient.cancel(escrowAddress)).rejects.toThrow();
935
-
936
- expect(escrowClient.escrowContract.cancel).toHaveBeenCalledWith();
937
- });
938
- });
939
-
940
- describe('abort', () => {
941
- test('should throw an error if escrowAddress is an invalid address', async () => {
942
- const invalidAddress = FAKE_ADDRESS;
943
-
944
- await expect(escrowClient.abort(invalidAddress)).rejects.toThrow(
945
- ErrorInvalidEscrowAddressProvided
946
- );
947
- });
948
-
949
- test('should throw an error if hasEscrow returns false', async () => {
950
- const escrowAddress = ethers.constants.AddressZero;
951
-
952
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
953
-
954
- await expect(escrowClient.abort(escrowAddress)).rejects.toThrow(
955
- ErrorEscrowAddressIsNotProvidedByFactory
956
- );
957
- });
958
-
959
- test('should successfully abort escrow', async () => {
960
- const escrowAddress = ethers.constants.AddressZero;
961
-
962
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
963
-
964
- await escrowClient.abort(escrowAddress);
965
-
966
- expect(escrowClient.escrowContract.abort).toHaveBeenCalledWith();
967
- });
968
-
969
- test('should throw an error if abort fails', async () => {
970
- const escrowAddress = ethers.constants.AddressZero;
971
-
972
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
973
- escrowClient.escrowContract.abort.mockRejectedValueOnce(new Error());
974
-
975
- await expect(escrowClient.abort(escrowAddress)).rejects.toThrow();
976
-
977
- expect(escrowClient.escrowContract.abort).toHaveBeenCalledWith();
978
- });
979
- });
980
-
981
- describe('addTrustedHandlers', () => {
982
- test('should throw an error if escrowAddress is an invalid address', async () => {
983
- const escrowAddress = FAKE_ADDRESS;
984
- const trustedHandlers = [ethers.constants.AddressZero];
985
-
986
- await expect(
987
- escrowClient.addTrustedHandlers(escrowAddress, trustedHandlers)
988
- ).rejects.toThrow(ErrorInvalidEscrowAddressProvided);
989
- });
990
-
991
- test('should throw an error if hasEscrow returns false', async () => {
992
- const escrowAddress = ethers.constants.AddressZero;
993
- const trustedHandlers = [ethers.constants.AddressZero];
994
-
995
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
996
-
997
- await expect(
998
- escrowClient.addTrustedHandlers(escrowAddress, trustedHandlers)
999
- ).rejects.toThrow(ErrorEscrowAddressIsNotProvidedByFactory);
1000
- });
1001
-
1002
- test('should throw an error if trusted handlers length is equal to 0', async () => {
1003
- const escrowAddress = ethers.constants.AddressZero;
1004
- const trustedHandlers = [];
1005
-
1006
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1007
-
1008
- await expect(
1009
- escrowClient.addTrustedHandlers(escrowAddress, trustedHandlers)
1010
- ).rejects.toThrow(ErrorListOfHandlersCannotBeEmpty);
1011
- });
1012
-
1013
- test('should throw an error if trusted handlers contains invalid addresses', async () => {
1014
- const escrowAddress = ethers.constants.AddressZero;
1015
- const trustedHandlers = [FAKE_ADDRESS];
1016
-
1017
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1018
-
1019
- await expect(
1020
- escrowClient.addTrustedHandlers(escrowAddress, trustedHandlers)
1021
- ).rejects.toThrow(new InvalidEthereumAddressError(FAKE_ADDRESS));
1022
- });
1023
-
1024
- test('should successfully addTrustedHandlers', async () => {
1025
- const escrowAddress = ethers.constants.AddressZero;
1026
- const trustedHandlers = [ethers.constants.AddressZero];
1027
-
1028
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1029
-
1030
- await escrowClient.addTrustedHandlers(escrowAddress, trustedHandlers);
1031
-
1032
- expect(
1033
- escrowClient.escrowContract.addTrustedHandlers
1034
- ).toHaveBeenCalledWith(trustedHandlers);
1035
- });
1036
-
1037
- test('should throw an error if addTrustedHandlers fails', async () => {
1038
- const escrowAddress = ethers.constants.AddressZero;
1039
- const trustedHandlers = [ethers.constants.AddressZero];
1040
-
1041
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1042
- escrowClient.escrowContract.addTrustedHandlers.mockRejectedValueOnce(
1043
- new Error()
1044
- );
1045
-
1046
- await expect(
1047
- escrowClient.addTrustedHandlers(escrowAddress, trustedHandlers)
1048
- ).rejects.toThrow();
1049
-
1050
- expect(
1051
- escrowClient.escrowContract.addTrustedHandlers
1052
- ).toHaveBeenCalledWith(trustedHandlers);
1053
- });
1054
- });
1055
-
1056
- describe('getBalance', () => {
1057
- test('should throw an error if escrowAddress is an invalid address', async () => {
1058
- const escrowAddress = FAKE_ADDRESS;
1059
-
1060
- await expect(escrowClient.getBalance(escrowAddress)).rejects.toThrow(
1061
- ErrorInvalidEscrowAddressProvided
1062
- );
1063
- });
1064
-
1065
- test('should throw an error if hasEscrow returns false', async () => {
1066
- const escrowAddress = ethers.constants.AddressZero;
1067
-
1068
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
1069
-
1070
- await expect(escrowClient.getBalance(escrowAddress)).rejects.toThrow(
1071
- ErrorEscrowAddressIsNotProvidedByFactory
1072
- );
1073
- });
1074
-
1075
- test('should successfully getBalance escrow', async () => {
1076
- const escrowAddress = ethers.constants.AddressZero;
1077
- const amount = BigNumber.from(100);
1078
-
1079
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1080
- escrowClient.escrowContract.getBalance.mockReturnValue(amount);
1081
-
1082
- const balance = await escrowClient.getBalance(escrowAddress);
1083
-
1084
- expect(balance).toEqual(amount);
1085
- expect(escrowClient.escrowContract.getBalance).toHaveBeenCalledWith();
1086
- });
1087
-
1088
- test('should throw an error if the getBalance fails', async () => {
1089
- const escrowAddress = ethers.constants.AddressZero;
1090
-
1091
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1092
- escrowClient.escrowContract.getBalance.mockRejectedValueOnce(new Error());
1093
-
1094
- await expect(escrowClient.getBalance(escrowAddress)).rejects.toThrow();
1095
-
1096
- expect(escrowClient.escrowContract.getBalance).toHaveBeenCalledWith();
1097
- });
1098
- });
1099
-
1100
- describe('getManifestUrl', () => {
1101
- test('should throw an error if escrowAddress is an invalid address', async () => {
1102
- const escrowAddress = FAKE_ADDRESS;
1103
-
1104
- await expect(escrowClient.getManifestUrl(escrowAddress)).rejects.toThrow(
1105
- ErrorInvalidEscrowAddressProvided
1106
- );
1107
- });
1108
-
1109
- test('should throw an error if hasEscrow returns false', async () => {
1110
- const escrowAddress = ethers.constants.AddressZero;
1111
-
1112
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
1113
-
1114
- await expect(escrowClient.getManifestUrl(escrowAddress)).rejects.toThrow(
1115
- ErrorEscrowAddressIsNotProvidedByFactory
1116
- );
1117
- });
1118
-
1119
- test('should successfully getManifestUrl', async () => {
1120
- const escrowAddress = ethers.constants.AddressZero;
1121
- const url = FAKE_URL;
1122
-
1123
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1124
- escrowClient.escrowContract.manifestUrl.mockReturnValue(url);
1125
-
1126
- const manifestUrl = await escrowClient.getManifestUrl(escrowAddress);
1127
-
1128
- expect(manifestUrl).toEqual(url);
1129
- expect(escrowClient.escrowContract.manifestUrl).toHaveBeenCalledWith();
1130
- });
1131
-
1132
- test('should throw an error if getManifestUrl fails', async () => {
1133
- const escrowAddress = ethers.constants.AddressZero;
1134
-
1135
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1136
- escrowClient.escrowContract.manifestUrl.mockRejectedValueOnce(
1137
- new Error()
1138
- );
1139
-
1140
- await expect(
1141
- escrowClient.getManifestUrl(escrowAddress)
1142
- ).rejects.toThrow();
1143
-
1144
- expect(escrowClient.escrowContract.manifestUrl).toHaveBeenCalledWith();
1145
- });
1146
- });
1147
-
1148
- describe('getResultsUrl', () => {
1149
- test('should throw an error if escrowAddress is an invalid address', async () => {
1150
- const escrowAddress = FAKE_ADDRESS;
1151
-
1152
- await expect(escrowClient.getResultsUrl(escrowAddress)).rejects.toThrow(
1153
- ErrorInvalidEscrowAddressProvided
1154
- );
1155
- });
1156
-
1157
- test('should throw an error if hasEscrow returns false', async () => {
1158
- const escrowAddress = ethers.constants.AddressZero;
1159
-
1160
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
1161
-
1162
- await expect(escrowClient.getResultsUrl(escrowAddress)).rejects.toThrow(
1163
- ErrorEscrowAddressIsNotProvidedByFactory
1164
- );
1165
- });
1166
-
1167
- test('should successfully getResultsUrl', async () => {
1168
- const escrowAddress = ethers.constants.AddressZero;
1169
- const url = FAKE_URL;
1170
-
1171
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1172
- escrowClient.escrowContract.finalResultsUrl.mockReturnValue(url);
1173
-
1174
- const finalResultsUrl = await escrowClient.getResultsUrl(escrowAddress);
1175
-
1176
- expect(finalResultsUrl).toEqual(url);
1177
- expect(
1178
- escrowClient.escrowContract.finalResultsUrl
1179
- ).toHaveBeenCalledWith();
1180
- });
1181
-
1182
- test('should throw an error if getResultsUrl fails', async () => {
1183
- const escrowAddress = ethers.constants.AddressZero;
1184
-
1185
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1186
- escrowClient.escrowContract.finalResultsUrl.mockRejectedValueOnce(
1187
- new Error()
1188
- );
1189
-
1190
- await expect(escrowClient.getResultsUrl(escrowAddress)).rejects.toThrow();
1191
-
1192
- expect(
1193
- escrowClient.escrowContract.finalResultsUrl
1194
- ).toHaveBeenCalledWith();
1195
- });
1196
- });
1197
-
1198
- describe('getTokenAddress', () => {
1199
- test('should throw an error if escrowAddress is an invalid address', async () => {
1200
- const escrowAddress = FAKE_ADDRESS;
1201
-
1202
- await expect(escrowClient.getTokenAddress(escrowAddress)).rejects.toThrow(
1203
- ErrorInvalidEscrowAddressProvided
1204
- );
1205
- });
1206
-
1207
- test('should throw an error if hasEscrow returns false', async () => {
1208
- const escrowAddress = ethers.constants.AddressZero;
1209
-
1210
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
1211
-
1212
- await expect(escrowClient.getTokenAddress(escrowAddress)).rejects.toThrow(
1213
- ErrorEscrowAddressIsNotProvidedByFactory
1214
- );
1215
- });
1216
-
1217
- test('should successfully getTokenAddress', async () => {
1218
- const escrowAddress = ethers.constants.AddressZero;
1219
-
1220
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1221
- escrowClient.escrowContract.token.mockReturnValue(
1222
- ethers.constants.AddressZero
1223
- );
1224
-
1225
- const tokenAddress = await escrowClient.getTokenAddress(escrowAddress);
1226
-
1227
- expect(tokenAddress).toEqual(ethers.constants.AddressZero);
1228
- expect(escrowClient.escrowContract.token).toHaveBeenCalledWith();
1229
- });
1230
-
1231
- test('should throw an error if getTokenAddress fails', async () => {
1232
- const escrowAddress = ethers.constants.AddressZero;
1233
-
1234
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1235
- escrowClient.escrowContract.token.mockRejectedValueOnce(new Error());
1236
-
1237
- await expect(
1238
- escrowClient.getTokenAddress(escrowAddress)
1239
- ).rejects.toThrow();
1240
-
1241
- expect(escrowClient.escrowContract.token).toHaveBeenCalledWith();
1242
- });
1243
- });
1244
-
1245
- describe('getStatus', () => {
1246
- test('should throw an error if escrowAddress is an invalid address', async () => {
1247
- const escrowAddress = FAKE_ADDRESS;
1248
-
1249
- await expect(escrowClient.getStatus(escrowAddress)).rejects.toThrow(
1250
- ErrorInvalidEscrowAddressProvided
1251
- );
1252
- });
1253
-
1254
- test('should throw an error if hasEscrow returns false', async () => {
1255
- const escrowAddress = ethers.constants.AddressZero;
1256
-
1257
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(false);
1258
-
1259
- await expect(escrowClient.getStatus(escrowAddress)).rejects.toThrow(
1260
- ErrorEscrowAddressIsNotProvidedByFactory
1261
- );
1262
- });
1263
-
1264
- test('should successfully getStatus', async () => {
1265
- const escrowAddress = ethers.constants.AddressZero;
1266
-
1267
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1268
- escrowClient.escrowContract.status.mockReturnValue(EscrowStatus.Complete);
1269
-
1270
- const status = await escrowClient.getStatus(escrowAddress);
1271
-
1272
- expect(status).toEqual(EscrowStatus.Complete);
1273
- expect(escrowClient.escrowContract.status).toHaveBeenCalledWith();
1274
- });
1275
-
1276
- test('should throw an error if getStatus fails', async () => {
1277
- const escrowAddress = ethers.constants.AddressZero;
1278
-
1279
- escrowClient.escrowFactoryContract.hasEscrow.mockReturnValue(true);
1280
- escrowClient.escrowContract.status.mockRejectedValueOnce(new Error());
1281
-
1282
- await expect(escrowClient.getStatus(escrowAddress)).rejects.toThrow();
1283
-
1284
- expect(escrowClient.escrowContract.status).toHaveBeenCalledWith();
1285
- });
1286
- });
1287
-
1288
- describe('getLaunchedEscrows', () => {
1289
- test('should throw an error if requesterAddress is an invalid address', async () => {
1290
- const requesterAddress = FAKE_ADDRESS;
1291
-
1292
- await expect(
1293
- escrowClient.getLaunchedEscrows(requesterAddress)
1294
- ).rejects.toThrow(ErrorInvalidAddress);
1295
- });
1296
-
1297
- test('should successfully getLaunchedEscrows', async () => {
1298
- const requesterAddress = FAKE_ADDRESS;
1299
- const mockLaunchedEscrowsResult = { id: ethers.constants.AddressZero };
1300
-
1301
- vi.spyOn(escrowClient, 'getLaunchedEscrows').mockImplementation(() =>
1302
- Promise.resolve([mockLaunchedEscrowsResult, mockLaunchedEscrowsResult])
1303
- );
1304
-
1305
- const results = await escrowClient.getLaunchedEscrows(requesterAddress);
1306
-
1307
- expect(results).toEqual([
1308
- mockLaunchedEscrowsResult,
1309
- mockLaunchedEscrowsResult,
1310
- ]);
1311
- });
1312
- });
1313
-
1314
- describe('getEscrowsFiltered', () => {
1315
- test('should throw an error if requesterAddress is an invalid address', async () => {
1316
- const requesterAddress = FAKE_ADDRESS;
1317
-
1318
- await expect(
1319
- escrowClient.getEscrowsFiltered(requesterAddress)
1320
- ).rejects.toThrow(ErrorInvalidAddress);
1321
- });
1322
-
1323
- test('should successfully getEscrowsFiltered', async () => {
1324
- const requesterAddress = FAKE_ADDRESS;
1325
- const mockLaunchedEscrowsResult = { id: ethers.constants.AddressZero };
1326
-
1327
- vi.spyOn(escrowClient, 'getEscrowsFiltered').mockImplementation(() =>
1328
- Promise.resolve([mockLaunchedEscrowsResult, mockLaunchedEscrowsResult])
1329
- );
1330
-
1331
- const results = await escrowClient.getEscrowsFiltered(requesterAddress);
1332
-
1333
- expect(results).toEqual([
1334
- mockLaunchedEscrowsResult,
1335
- mockLaunchedEscrowsResult,
1336
- ]);
1337
- });
1338
- });
1339
- });