@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,640 +0,0 @@
1
- import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest';
2
- import { ethers, BigNumber } from 'ethers';
3
- import StakingClient from '../src/staking';
4
- import {
5
- FAKE_AMOUNT,
6
- FAKE_BLOCK_NUMBER,
7
- FAKE_NEGATIVE_AMOUNT,
8
- FAKE_NETWORK,
9
- FAKE_TRANSACTION_CONFIRMATIONS,
10
- FAKE_TRANSACTION_HASH,
11
- } from './utils/constants';
12
- import {
13
- ErrorInvalidEscrowAddressProvided,
14
- ErrorInvalidSlasherAddressProvided,
15
- ErrorInvalidStakerAddressProvided,
16
- ErrorInvalidStakingValueSign,
17
- ErrorInvalidStakingValueType,
18
- } from '../src/error';
19
- import InitClient from '../src/init';
20
- import { IAllocation, IReward, IStaker } from '../src/interfaces';
21
-
22
- vi.mock('../src/init');
23
-
24
- describe('StakingClient', () => {
25
- const provider = new ethers.providers.JsonRpcProvider();
26
- let stakingClient: any,
27
- mockSigner: any,
28
- mockStakingContract: any,
29
- mockRewardPoolContract: any,
30
- mockEscrowFactoryContract: any,
31
- mockTokenContract: any;
32
-
33
- beforeEach(async () => {
34
- mockSigner = {
35
- ...provider.getSigner(),
36
- getAddress: vi.fn().mockReturnValue(ethers.constants.AddressZero),
37
- };
38
-
39
- mockStakingContract = {
40
- stake: vi.fn(),
41
- unstake: vi.fn(),
42
- withdraw: vi.fn(),
43
- slash: vi.fn(),
44
- allocate: vi.fn(),
45
- closeAllocation: vi.fn(),
46
- distributeRewards: vi.fn(),
47
- getRewards: vi.fn(),
48
- getStaker: vi.fn(),
49
- getListOfStakers: vi.fn(),
50
- getAllocation: vi.fn(),
51
- rewardPool: vi.fn().mockResolvedValueOnce(ethers.constants.AddressZero),
52
- address: FAKE_NETWORK.stakingAddress,
53
- };
54
-
55
- mockRewardPoolContract = {
56
- distributeRewards: vi.fn(),
57
- getRewards: vi.fn(),
58
- address: ethers.constants.AddressZero,
59
- };
60
-
61
- mockEscrowFactoryContract = {
62
- hasEscrow: vi.fn(),
63
- };
64
-
65
- mockTokenContract = {
66
- allowance: vi.fn(),
67
- approve: vi.fn(),
68
- };
69
-
70
- const getClientParamsMock = InitClient.getParams as jest.Mock;
71
- getClientParamsMock.mockResolvedValue({
72
- signerOrProvider: mockSigner,
73
- network: FAKE_NETWORK,
74
- });
75
-
76
- stakingClient = new StakingClient(await InitClient.getParams(mockSigner));
77
-
78
- stakingClient.stakingContract = mockStakingContract;
79
- stakingClient.tokenContract = mockTokenContract;
80
- stakingClient.escrowFactoryContract = mockEscrowFactoryContract;
81
- });
82
-
83
- afterEach(() => {
84
- vi.restoreAllMocks();
85
- });
86
-
87
- describe('approveStake', () => {
88
- const amount = BigNumber.from(FAKE_AMOUNT);
89
- const negativeAmount = BigNumber.from(FAKE_NEGATIVE_AMOUNT);
90
-
91
- test('should throw an error if the amount is not a BigNumber', async () => {
92
- await expect(stakingClient.approveStake('foo')).rejects.toThrow(
93
- ErrorInvalidStakingValueType
94
- );
95
- expect(mockTokenContract.approve).toHaveBeenCalledTimes(0);
96
- });
97
-
98
- test('should throw an error if the amount is negative', async () => {
99
- await expect(stakingClient.approveStake(negativeAmount)).rejects.toThrow(
100
- ErrorInvalidStakingValueSign
101
- );
102
- expect(mockTokenContract.approve).toHaveBeenCalledTimes(0);
103
- });
104
-
105
- test('should not fail and return void if the allowance is sufficient and the approval is successful', async () => {
106
- stakingClient.isAllowance = vi.fn().mockResolvedValue(true);
107
-
108
- mockTokenContract.approve = vi.fn().mockResolvedValue({
109
- hash: FAKE_TRANSACTION_HASH,
110
- blockNumber: FAKE_BLOCK_NUMBER,
111
- confirmations: FAKE_TRANSACTION_CONFIRMATIONS,
112
- });
113
-
114
- await expect(stakingClient.approveStake(amount)).resolves.toBeUndefined();
115
- expect(mockTokenContract.approve).toBeCalledWith(
116
- ethers.constants.AddressZero,
117
- amount
118
- );
119
- expect(mockTokenContract.approve).toHaveBeenCalledTimes(1);
120
- });
121
-
122
- test('should throw an error if the approval fails', async () => {
123
- stakingClient.isAllowance = vi.fn().mockResolvedValue(true);
124
-
125
- mockTokenContract.approve = vi.fn().mockRejectedValue(new Error());
126
-
127
- await expect(stakingClient.approveStake(amount)).rejects.toThrow();
128
- expect(mockTokenContract.approve).toBeCalledWith(
129
- ethers.constants.AddressZero,
130
- amount
131
- );
132
- expect(mockTokenContract.approve).toHaveBeenCalledTimes(1);
133
- });
134
- });
135
-
136
- describe('stake', () => {
137
- const amount = BigNumber.from(FAKE_AMOUNT);
138
- const negativeAmount = BigNumber.from(FAKE_NEGATIVE_AMOUNT);
139
-
140
- test('should throw an error if amount is not a BigNumber', async () => {
141
- await expect(stakingClient.stake('foo')).rejects.toThrow(
142
- ErrorInvalidStakingValueType
143
- );
144
- expect(mockStakingContract.stake).toHaveBeenCalledTimes(0);
145
- });
146
-
147
- test('should throw an error if amount is negative', async () => {
148
- await expect(stakingClient.stake(negativeAmount)).rejects.toThrow(
149
- ErrorInvalidStakingValueSign
150
- );
151
- expect(mockStakingContract.stake).toHaveBeenCalledTimes(0);
152
- });
153
-
154
- test('should call the stake function on the staking contract with the given amount', async () => {
155
- mockTokenContract.allowance.mockResolvedValueOnce(amount);
156
-
157
- await stakingClient.stake(amount);
158
-
159
- expect(mockStakingContract.stake).toHaveBeenCalledWith(amount);
160
- expect(mockStakingContract.stake).toHaveBeenCalledTimes(1);
161
- });
162
-
163
- test('should throw an error if the stake function on the staking contract fails', async () => {
164
- mockTokenContract.allowance.mockResolvedValueOnce(amount);
165
-
166
- mockStakingContract.stake.mockRejectedValueOnce(new Error());
167
-
168
- await expect(stakingClient.stake(amount)).rejects.toThrow();
169
- expect(mockStakingContract.stake).toHaveBeenCalledWith(amount);
170
- expect(mockStakingContract.stake).toHaveBeenCalledTimes(1);
171
- });
172
- });
173
-
174
- describe('unstake', () => {
175
- const amount = BigNumber.from(FAKE_AMOUNT);
176
- const negativeAmount = BigNumber.from(FAKE_NEGATIVE_AMOUNT);
177
-
178
- test('should throw an error if amount is not a BigNumber', async () => {
179
- await expect(stakingClient.unstake('foo')).rejects.toThrow(
180
- ErrorInvalidStakingValueType
181
- );
182
- expect(mockStakingContract.unstake).toHaveBeenCalledTimes(0);
183
- });
184
-
185
- test('should throw an error if amount is negative', async () => {
186
- await expect(stakingClient.unstake(negativeAmount)).rejects.toThrow(
187
- ErrorInvalidStakingValueSign
188
- );
189
- expect(mockStakingContract.unstake).toHaveBeenCalledTimes(0);
190
- });
191
-
192
- test('should call the unstake function on the staking contract with the given amount', async () => {
193
- await stakingClient.unstake(amount);
194
-
195
- expect(mockStakingContract.unstake).toHaveBeenCalledWith(amount);
196
- expect(mockStakingContract.unstake).toHaveBeenCalledTimes(1);
197
- });
198
-
199
- test('should throw an error if the unstake function on the staking contract fails', async () => {
200
- mockStakingContract.unstake.mockRejectedValueOnce(new Error());
201
-
202
- await expect(stakingClient.unstake(amount)).rejects.toThrow();
203
- expect(mockStakingContract.unstake).toHaveBeenCalledWith(amount);
204
- expect(mockStakingContract.unstake).toHaveBeenCalledTimes(1);
205
- });
206
- });
207
-
208
- describe('withdraw', () => {
209
- test('should call the withdraw method with the correct parameters', async () => {
210
- mockStakingContract.withdraw.mockResolvedValueOnce();
211
-
212
- await stakingClient.withdraw();
213
-
214
- expect(mockStakingContract.withdraw).toHaveBeenCalledTimes(1);
215
- });
216
-
217
- test('should throw an error if the withdraw method of the staking contract fails', async () => {
218
- mockStakingContract.withdraw.mockRejectedValueOnce(new Error());
219
-
220
- await expect(stakingClient.withdraw()).rejects.toThrow();
221
- expect(mockStakingContract.withdraw).toHaveBeenCalledTimes(1);
222
- });
223
- });
224
-
225
- describe('slash', () => {
226
- const amount = BigNumber.from(FAKE_AMOUNT);
227
- const negativeAmount = BigNumber.from(FAKE_NEGATIVE_AMOUNT);
228
- const invalidAddress = 'InvalidAddress';
229
-
230
- test('throws an error if amount is not a BigNumber', async () => {
231
- await expect(
232
- stakingClient.slash(
233
- ethers.constants.AddressZero,
234
- ethers.constants.AddressZero,
235
- ethers.constants.AddressZero,
236
- 'foo'
237
- )
238
- ).rejects.toThrow(ErrorInvalidStakingValueType);
239
- expect(mockStakingContract.slash).toHaveBeenCalledTimes(0);
240
- });
241
-
242
- test('throws an error if amount is negative', async () => {
243
- await expect(
244
- stakingClient.slash(
245
- ethers.constants.AddressZero,
246
- ethers.constants.AddressZero,
247
- ethers.constants.AddressZero,
248
- negativeAmount
249
- )
250
- ).rejects.toThrow(ErrorInvalidStakingValueSign);
251
- expect(mockStakingContract.slash).toHaveBeenCalledTimes(0);
252
- });
253
-
254
- test('throws an error if slasher address is invalid', async () => {
255
- await expect(
256
- stakingClient.slash(
257
- invalidAddress,
258
- ethers.constants.AddressZero,
259
- ethers.constants.AddressZero,
260
- amount
261
- )
262
- ).rejects.toThrow(ErrorInvalidSlasherAddressProvided);
263
- expect(mockStakingContract.slash).toHaveBeenCalledTimes(0);
264
- });
265
-
266
- test('throws an error if staker address is invalid', async () => {
267
- await expect(
268
- stakingClient.slash(
269
- ethers.constants.AddressZero,
270
- invalidAddress,
271
- ethers.constants.AddressZero,
272
- amount
273
- )
274
- ).rejects.toThrow(ErrorInvalidStakerAddressProvided);
275
- expect(mockStakingContract.slash).toHaveBeenCalledTimes(0);
276
- });
277
-
278
- test('throws an error if escrow address is invalid', async () => {
279
- await expect(
280
- stakingClient.slash(
281
- ethers.constants.AddressZero,
282
- ethers.constants.AddressZero,
283
- invalidAddress,
284
- amount
285
- )
286
- ).rejects.toThrow(ErrorInvalidEscrowAddressProvided);
287
- expect(mockStakingContract.slash).toHaveBeenCalledTimes(0);
288
- });
289
-
290
- test('throws an error if escrow address is not provided by the factory', async () => {
291
- mockEscrowFactoryContract.hasEscrow.mockRejectedValueOnce(new Error());
292
-
293
- await expect(
294
- stakingClient.slash(
295
- invalidAddress,
296
- ethers.constants.AddressZero,
297
- ethers.constants.AddressZero,
298
- amount
299
- )
300
- ).rejects.toThrow(ErrorInvalidSlasherAddressProvided);
301
- expect(mockStakingContract.slash).toHaveBeenCalledTimes(0);
302
- });
303
-
304
- test('throws an error if slashing fails', async () => {
305
- mockEscrowFactoryContract.hasEscrow.mockResolvedValueOnce(true);
306
- mockStakingContract.slash.mockRejectedValueOnce(new Error());
307
-
308
- await expect(
309
- stakingClient.slash(
310
- ethers.constants.AddressZero,
311
- ethers.constants.AddressZero,
312
- ethers.constants.AddressZero,
313
- amount
314
- )
315
- ).rejects.toThrow();
316
-
317
- expect(mockStakingContract.slash).toHaveBeenCalledWith(
318
- ethers.constants.AddressZero,
319
- ethers.constants.AddressZero,
320
- ethers.constants.AddressZero,
321
- amount
322
- );
323
- expect(mockStakingContract.slash).toHaveBeenCalledTimes(1);
324
- });
325
-
326
- test('calls the staking contract to slash the given amount', async () => {
327
- mockEscrowFactoryContract.hasEscrow.mockResolvedValueOnce(true);
328
- mockStakingContract.slash.mockResolvedValueOnce();
329
-
330
- await stakingClient.slash(
331
- ethers.constants.AddressZero,
332
- ethers.constants.AddressZero,
333
- ethers.constants.AddressZero,
334
- amount
335
- );
336
-
337
- expect(mockStakingContract.slash).toHaveBeenCalledWith(
338
- ethers.constants.AddressZero,
339
- ethers.constants.AddressZero,
340
- ethers.constants.AddressZero,
341
- amount
342
- );
343
- expect(mockStakingContract.slash).toHaveBeenCalledTimes(1);
344
- });
345
- });
346
-
347
- describe('allocate', () => {
348
- const amount = BigNumber.from(FAKE_AMOUNT);
349
- const negativeAmount = BigNumber.from(FAKE_NEGATIVE_AMOUNT);
350
- const invalidAddress = 'InvalidAddress';
351
-
352
- test('throws an error if escrow address is invalid', async () => {
353
- await expect(
354
- stakingClient.allocate(invalidAddress, amount)
355
- ).rejects.toThrow(ErrorInvalidEscrowAddressProvided);
356
- expect(mockStakingContract.allocate).toHaveBeenCalledTimes(0);
357
- });
358
-
359
- test('throws an error if amount is not a BigNumber', async () => {
360
- await expect(
361
- stakingClient.allocate(ethers.constants.AddressZero, 'foo')
362
- ).rejects.toThrow(ErrorInvalidStakingValueType);
363
- expect(mockStakingContract.allocate).toHaveBeenCalledTimes(0);
364
- });
365
-
366
- test('throws an error if amount is negative', async () => {
367
- await expect(
368
- stakingClient.allocate(ethers.constants.AddressZero, negativeAmount)
369
- ).rejects.toThrow(ErrorInvalidStakingValueSign);
370
- expect(mockStakingContract.allocate).toHaveBeenCalledTimes(0);
371
- });
372
-
373
- test('throws an error if escrow address is not provided by the factory', async () => {
374
- mockEscrowFactoryContract.hasEscrow.mockRejectedValueOnce(new Error());
375
-
376
- await expect(
377
- stakingClient.allocate(ethers.constants.AddressZero, amount)
378
- ).rejects.toThrow();
379
- expect(mockStakingContract.allocate).toHaveBeenCalledTimes(0);
380
- });
381
-
382
- test('should call the allocate method with the correct parameters', async () => {
383
- mockEscrowFactoryContract.hasEscrow.mockResolvedValueOnce(true);
384
- await stakingClient.allocate(ethers.constants.AddressZero, amount);
385
-
386
- expect(mockStakingContract.allocate).toHaveBeenCalledWith(
387
- ethers.constants.AddressZero,
388
- amount
389
- );
390
- expect(mockStakingContract.allocate).toHaveBeenCalledTimes(1);
391
- });
392
-
393
- test('should throw an error if the allocate method fails', async () => {
394
- mockEscrowFactoryContract.hasEscrow.mockResolvedValueOnce(true);
395
- mockStakingContract.allocate.mockRejectedValueOnce(new Error());
396
-
397
- await expect(
398
- stakingClient.allocate(ethers.constants.AddressZero, amount)
399
- ).rejects.toThrow();
400
- expect(mockStakingContract.allocate).toHaveBeenCalledWith(
401
- ethers.constants.AddressZero,
402
- amount
403
- );
404
- expect(mockStakingContract.allocate).toHaveBeenCalledTimes(1);
405
- });
406
- });
407
-
408
- describe('closeAllocation', () => {
409
- const invalidAddress = 'InvalidAddress';
410
-
411
- test('should throws an error if escrow address is invalid', async () => {
412
- await expect(
413
- stakingClient.closeAllocation(invalidAddress)
414
- ).rejects.toThrow(ErrorInvalidEscrowAddressProvided);
415
- expect(mockStakingContract.closeAllocation).toHaveBeenCalledTimes(0);
416
- });
417
-
418
- test('throws an error if escrow address is not provided by the factory', async () => {
419
- mockEscrowFactoryContract.hasEscrow.mockRejectedValueOnce(new Error());
420
-
421
- await expect(
422
- stakingClient.closeAllocation(ethers.constants.AddressZero)
423
- ).rejects.toThrow();
424
- expect(mockStakingContract.closeAllocation).toHaveBeenCalledTimes(0);
425
- });
426
-
427
- test('should throw an error when stakingContract.closeAllocation throws an error', async () => {
428
- mockEscrowFactoryContract.hasEscrow.mockResolvedValueOnce(true);
429
- mockStakingContract.closeAllocation.mockRejectedValueOnce(new Error());
430
-
431
- await expect(
432
- stakingClient.closeAllocation(ethers.constants.AddressZero)
433
- ).rejects.toThrow();
434
-
435
- expect(mockStakingContract.closeAllocation).toHaveBeenCalledWith(
436
- ethers.constants.AddressZero
437
- );
438
- expect(mockStakingContract.closeAllocation).toHaveBeenCalledTimes(1);
439
- });
440
-
441
- test('should call the closeAllocation method with the correct parameters', async () => {
442
- mockEscrowFactoryContract.hasEscrow.mockResolvedValueOnce(true);
443
- mockStakingContract.closeAllocation.mockResolvedValueOnce();
444
-
445
- await stakingClient.closeAllocation(ethers.constants.AddressZero);
446
-
447
- expect(mockStakingContract.closeAllocation).toHaveBeenCalledWith(
448
- ethers.constants.AddressZero
449
- );
450
- expect(mockStakingContract.closeAllocation).toHaveBeenCalledTimes(1);
451
- });
452
- });
453
-
454
- describe('distributeRewards', () => {
455
- const invalidAddress = 'InvalidAddress';
456
-
457
- test('should throw an error if an invalid escrow address is provided', async () => {
458
- await expect(
459
- stakingClient.distributeRewards(invalidAddress)
460
- ).rejects.toThrow(ErrorInvalidEscrowAddressProvided);
461
- expect(mockStakingContract.distributeRewards).toHaveBeenCalledTimes(0);
462
- });
463
-
464
- test('throws an error if escrow address is not provided by the factory', async () => {
465
- mockEscrowFactoryContract.hasEscrow.mockRejectedValueOnce(new Error());
466
-
467
- await expect(
468
- stakingClient.distributeRewards(ethers.constants.AddressZero)
469
- ).rejects.toThrow();
470
- expect(mockStakingContract.distributeRewards).toHaveBeenCalledTimes(0);
471
- });
472
-
473
- test('should call distributeReward on the reward pool contract', async () => {
474
- mockEscrowFactoryContract.hasEscrow.mockResolvedValueOnce(true);
475
- vi.spyOn(stakingClient, 'distributeRewards').mockImplementation(() =>
476
- Promise.resolve(undefined)
477
- );
478
-
479
- const results = await stakingClient.distributeRewards(
480
- ethers.constants.AddressZero
481
- );
482
-
483
- expect(results).toBeUndefined();
484
- });
485
- });
486
-
487
- describe('getStaker', () => {
488
- const stakerAddress = ethers.constants.AddressZero;
489
- const invalidAddress = 'InvalidAddress';
490
-
491
- test('should return staker information', async () => {
492
- const mockStaker: IStaker = {
493
- tokensStaked: ethers.utils.parseEther('100'),
494
- tokensAllocated: ethers.utils.parseEther('50'),
495
- tokensLocked: ethers.utils.parseEther('25'),
496
- tokensLockedUntil: ethers.BigNumber.from(0),
497
- tokensAvailable: ethers.utils.parseEther('25'),
498
- };
499
- mockStakingContract.getStaker.mockResolvedValueOnce(mockStaker);
500
-
501
- const result = await stakingClient.getStaker(stakerAddress);
502
- expect(result).toEqual(mockStaker);
503
- expect(mockStakingContract.getStaker).toHaveBeenCalledWith(stakerAddress);
504
- expect(mockStakingContract.getStaker).toHaveBeenCalledTimes(1);
505
- });
506
-
507
- test('should throw an error for an invalid staker address', async () => {
508
- await expect(stakingClient.getStaker(invalidAddress)).rejects.toThrow(
509
- ErrorInvalidStakerAddressProvided
510
- );
511
- expect(mockStakingContract.getStaker).toHaveBeenCalledTimes(0);
512
- });
513
-
514
- test('should throw an error if the staking contract call fails', async () => {
515
- mockStakingContract.getStaker.mockRejectedValue(new Error());
516
-
517
- await expect(stakingClient.getStaker(stakerAddress)).rejects.toThrow();
518
- expect(mockStakingContract.getStaker).toHaveBeenCalledWith(stakerAddress);
519
- expect(mockStakingContract.getStaker).toHaveBeenCalledTimes(1);
520
- });
521
- });
522
-
523
- describe('getAllStakers()', () => {
524
- const mockStaker: IStaker = {
525
- tokensStaked: ethers.utils.parseEther('100'),
526
- tokensAllocated: ethers.utils.parseEther('50'),
527
- tokensLocked: ethers.utils.parseEther('25'),
528
- tokensLockedUntil: ethers.BigNumber.from(0),
529
- tokensAvailable: ethers.utils.parseEther('25'),
530
- };
531
- const stakerAddress = ethers.constants.AddressZero;
532
-
533
- test('should return an array of stakers', async () => {
534
- mockStakingContract.getListOfStakers.mockResolvedValueOnce([
535
- [stakerAddress, stakerAddress],
536
- [mockStaker, mockStaker],
537
- ]);
538
-
539
- const stakers = await stakingClient.getAllStakers();
540
-
541
- expect(stakers).toEqual([mockStaker, mockStaker]);
542
- expect(mockStakingContract.getListOfStakers).toHaveBeenCalledTimes(1);
543
- });
544
-
545
- test('should throw an error if no stakers are found', async () => {
546
- mockStakingContract.getListOfStakers.mockResolvedValue([[], []]);
547
- await expect(stakingClient.getAllStakers()).rejects.toThrow();
548
- expect(mockStakingContract.getListOfStakers).toHaveBeenCalledTimes(1);
549
- });
550
-
551
- test('should throw an error if there is an error in getting stakers', async () => {
552
- mockStakingContract.getListOfStakers.mockRejectedValueOnce(new Error());
553
- await expect(stakingClient.getAllStakers()).rejects.toThrow();
554
- expect(mockStakingContract.getListOfStakers).toHaveBeenCalledTimes(1);
555
- });
556
- });
557
-
558
- describe('getAllocation', () => {
559
- const invalidAddress = 'InvalidAddress';
560
-
561
- test('should throw an error for invalid escrow address', async () => {
562
- await expect(stakingClient.getAllocation(invalidAddress)).rejects.toThrow(
563
- ErrorInvalidEscrowAddressProvided
564
- );
565
- expect(mockStakingContract.getAllocation).toHaveBeenCalledTimes(0);
566
- });
567
-
568
- test('throws an error if escrow address is not provided by the factory', async () => {
569
- mockEscrowFactoryContract.hasEscrow.mockRejectedValueOnce(new Error());
570
-
571
- await expect(
572
- stakingClient.getAllocation(ethers.constants.AddressZero)
573
- ).rejects.toThrow();
574
- expect(mockStakingContract.getAllocation).toHaveBeenCalledTimes(0);
575
- });
576
-
577
- test('should return allocation information', async () => {
578
- const mockAllocation: IAllocation = {
579
- escrowAddress: ethers.constants.AddressZero,
580
- staker: ethers.constants.AddressZero,
581
- tokens: ethers.utils.parseEther('100'),
582
- createdAt: ethers.utils.parseEther('100'),
583
- closedAt: ethers.utils.parseEther('100'),
584
- };
585
- mockEscrowFactoryContract.hasEscrow.mockResolvedValueOnce(true);
586
- mockStakingContract.getAllocation.mockResolvedValueOnce(mockAllocation);
587
-
588
- const result = await stakingClient.getAllocation(
589
- ethers.constants.AddressZero
590
- );
591
- expect(result).toEqual(mockAllocation);
592
- expect(mockStakingContract.getAllocation).toHaveBeenCalledWith(
593
- ethers.constants.AddressZero
594
- );
595
- expect(mockStakingContract.getAllocation).toHaveBeenCalledTimes(1);
596
- });
597
-
598
- test('should throw an error if the allocation data cannot be retrieved', async () => {
599
- mockEscrowFactoryContract.hasEscrow.mockResolvedValueOnce(true);
600
- mockStakingContract.getAllocation.mockRejectedValue(new Error());
601
- await expect(
602
- stakingClient.getAllocation(ethers.constants.AddressZero)
603
- ).rejects.toThrow();
604
- });
605
- });
606
-
607
- describe('getRewards', () => {
608
- const invalidAddress = 'InvalidAddress';
609
- const gqlRawResult = {
610
- rewardAddedEvents: {
611
- escrow: ethers.constants.AddressZero,
612
- amount: ethers.utils.parseEther('100'),
613
- },
614
- };
615
-
616
- const mockReward: IReward = {
617
- escrowAddress: ethers.constants.AddressZero,
618
- amount: ethers.utils.parseEther('100'),
619
- };
620
-
621
- test('should throw an error if an invalid escrow address is provided', async () => {
622
- await expect(stakingClient.getRewards(invalidAddress)).rejects.toThrow(
623
- ErrorInvalidSlasherAddressProvided
624
- );
625
- expect(mockStakingContract.getRewards).toHaveBeenCalledTimes(0);
626
- });
627
-
628
- test('should return an array of rewards', async () => {
629
- vi.spyOn(stakingClient, 'getRewards').mockImplementation(() =>
630
- Promise.resolve([mockReward, mockReward])
631
- );
632
-
633
- const results = await stakingClient.getRewards(
634
- ethers.constants.AddressZero
635
- );
636
-
637
- expect(results).toEqual([mockReward, mockReward]);
638
- });
639
- });
640
- });