/
Payroll_payday.test.js
847 lines (649 loc) · 42.4 KB
/
Payroll_payday.test.js
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
const { assertRevert } = require('@aragon/test-helpers/assertThrow')
const { getEventArgument } = require('../helpers/events')
const { bn, bigExp, maxUint256 } = require('../helpers/numbers')(web3)
const { deployErc20TokenAndDeposit, deployContracts, createPayrollInstance, mockTimestamps } = require('../helpers/setup.js')(artifacts, web3)
contract('Payroll payday', ([owner, employee, anotherEmployee, anyone]) => {
let dao, payroll, payrollBase, finance, vault, priceFeed, denominationToken, anotherToken
const NOW = 1553703809 // random fixed timestamp in seconds
const ONE_MONTH = 60 * 60 * 24 * 31
const TWO_MONTHS = ONE_MONTH * 2
const RATE_EXPIRATION_TIME = TWO_MONTHS
const PCT_ONE = bigExp(1, 18)
const TOKEN_DECIMALS = 18
const PAYROLL_PAYMENT_TYPE = 0
before('setup base apps and tokens', async () => {
({ dao, finance, vault, priceFeed, payrollBase } = await deployContracts(owner))
anotherToken = await deployErc20TokenAndDeposit(owner, finance, vault, 'Another token', TOKEN_DECIMALS)
denominationToken = await deployErc20TokenAndDeposit(owner, finance, vault, 'Denomination Token', TOKEN_DECIMALS)
})
beforeEach('setup payroll instance', async () => {
payroll = await createPayrollInstance(dao, payrollBase, owner)
await mockTimestamps(payroll, priceFeed, NOW)
})
describe('payroll payday', () => {
context('when it has already been initialized', function () {
beforeEach('initialize payroll app', async () => {
await payroll.initialize(finance.address, denominationToken.address, priceFeed.address, RATE_EXPIRATION_TIME, { from: owner })
})
context('when the sender is an employee', () => {
let employeeId
const from = employee
context('when the employee has a reasonable salary', () => {
const salary = 100000
beforeEach('add employee', async () => {
const receipt = await payroll.addEmployeeNow(employee, salary, 'Boss')
employeeId = getEventArgument(receipt, 'AddEmployee', 'employeeId')
})
context('when the employee has already set some token allocations', () => {
const denominationTokenAllocation = 80
const anotherTokenAllocation = 20
beforeEach('set tokens allocation', async () => {
await payroll.addAllowedToken(anotherToken.address, { from: owner })
await payroll.addAllowedToken(denominationToken.address, { from: owner })
await payroll.determineAllocation([denominationToken.address, anotherToken.address], [denominationTokenAllocation, anotherTokenAllocation], { from })
})
const assertTransferredAmounts = (requestedAmount, expectedRequestedAmount = requestedAmount) => {
const requestedDenominationTokenAmount = Math.round(expectedRequestedAmount * denominationTokenAllocation / 100)
const requestedAnotherTokenAmount = Math.round(expectedRequestedAmount * anotherTokenAllocation / 100)
it('transfers the requested salary amount', async () => {
const previousDenominationTokenBalance = await denominationToken.balanceOf(employee)
const previousAnotherTokenBalance = await anotherToken.balanceOf(employee)
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const currentDenominationTokenBalance = await denominationToken.balanceOf(employee)
const expectedDenominationTokenBalance = previousDenominationTokenBalance.plus(requestedDenominationTokenAmount);
assert.equal(currentDenominationTokenBalance.toString(), expectedDenominationTokenBalance.toString(), 'current denomination token balance does not match')
const currentAnotherTokenBalance = await anotherToken.balanceOf(employee)
const anotherTokenRate = (await priceFeed.get(denominationToken.address, anotherToken.address))[0].div(PCT_ONE)
const expectedAnotherTokenBalance = anotherTokenRate.mul(requestedAnotherTokenAmount).plus(previousAnotherTokenBalance).trunc()
assert.equal(currentAnotherTokenBalance.toString(), expectedAnotherTokenBalance.toString(), 'current token balance does not match')
})
it('emits one event per allocated token', async () => {
const receipt = await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const events = receipt.logs.filter(l => l.event === 'SendPayment')
assert.equal(events.length, 2, 'should have emitted two events')
const denominationTokenEvent = events.find(e => e.args.token === denominationToken.address).args
assert.equal(denominationTokenEvent.employee, employee, 'employee address does not match')
assert.equal(denominationTokenEvent.token, denominationToken.address, 'denomination token address does not match')
assert.equal(denominationTokenEvent.amount.toString(), requestedDenominationTokenAmount, 'payment amount does not match')
assert.equal(denominationTokenEvent.paymentReference, 'Payroll', 'payment reference does not match')
const anotherTokenRate = (await priceFeed.get(denominationToken.address, anotherToken.address))[0].div(PCT_ONE)
const anotherTokenEvent = events.find(e => e.args.token === anotherToken.address).args
assert.equal(anotherTokenEvent.employee, employee, 'employee address does not match')
assert.equal(anotherTokenEvent.token, anotherToken.address, 'token address does not match')
assert.equal(anotherTokenEvent.amount.div(anotherTokenRate).trunc().toString(), requestedAnotherTokenAmount, 'payment amount does not match')
assert.equal(anotherTokenEvent.paymentReference, 'Payroll', 'payment reference does not match')
})
it('can be called multiple times between periods of time', async () => {
// terminate employee in the future to ensure we can request payroll multiple times
await payroll.terminateEmployee(employeeId, NOW + TWO_MONTHS + TWO_MONTHS, { from: owner })
const previousDenominationTokenBalance = await denominationToken.balanceOf(employee)
const previousAnotherTokenBalance = await anotherToken.balanceOf(employee)
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const newOwedAmount = salary * ONE_MONTH
const newDenominationTokenOwedAmount = Math.round(newOwedAmount * denominationTokenAllocation / 100)
const newAnotherTokenOwedAmount = Math.round(newOwedAmount * anotherTokenAllocation / 100)
await payroll.mockAddTimestamp(ONE_MONTH)
await priceFeed.mockAddTimestamp(ONE_MONTH)
await payroll.payday(PAYROLL_PAYMENT_TYPE, newOwedAmount, { from })
const currentDenominationTokenBalance = await denominationToken.balanceOf(employee)
const expectedDenominationTokenBalance = previousDenominationTokenBalance.plus(requestedDenominationTokenAmount + newDenominationTokenOwedAmount)
assert.equal(currentDenominationTokenBalance.toString(), expectedDenominationTokenBalance.toString(), 'current denomination token balance does not match')
const currentAnotherTokenBalance = await anotherToken.balanceOf(employee)
const anotherTokenRate = (await priceFeed.get(denominationToken.address, anotherToken.address))[0].div(PCT_ONE)
const expectedAnotherTokenBalance = anotherTokenRate.mul(requestedAnotherTokenAmount + newAnotherTokenOwedAmount).plus(previousAnotherTokenBalance)
assert.equal(currentAnotherTokenBalance.toString(), expectedAnotherTokenBalance.toString(), 'current token balance does not match')
})
}
const assertEmployeeIsUpdatedCorrectly = (requestedAmount, expectedRequestedAmount) => {
it('updates the accrued salary and the last payroll date', async () => {
let expectedLastPayrollDate, expectedAccruedSalary
const [previousAccruedSalary, previousPayrollDate] = (await payroll.getEmployee(employeeId)).slice(4, 6)
if (expectedRequestedAmount >= previousAccruedSalary) {
expectedAccruedSalary = 0
const remainder = expectedRequestedAmount - previousAccruedSalary
expectedLastPayrollDate = previousPayrollDate.plus(Math.ceil(remainder / salary))
} else {
expectedAccruedSalary = previousAccruedSalary.minus(expectedRequestedAmount).toString()
expectedLastPayrollDate = previousPayrollDate
}
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const [accruedSalary, lastPayrollDate] = (await payroll.getEmployee(employeeId)).slice(4, 6)
assert.equal(accruedSalary.toString(), expectedAccruedSalary, 'accrued salary does not match')
assert.equal(lastPayrollDate.toString(), expectedLastPayrollDate.toString(), 'last payroll date does not match')
})
it('does not remove the employee', async () => {
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const [address, employeeSalary] = await payroll.getEmployee(employeeId)
assert.equal(address, employee, 'employee address does not match')
assert.equal(employeeSalary, salary, 'employee salary does not match')
})
}
const itHandlesPayrollProperly = (requestedAmount, expectedRequestedAmount) => {
context('when exchange rates are not expired', () => {
assertTransferredAmounts(requestedAmount, expectedRequestedAmount)
assertEmployeeIsUpdatedCorrectly(requestedAmount, expectedRequestedAmount)
})
context('when exchange rates are expired', () => {
beforeEach('expire exchange rates', async () => {
await priceFeed.mockSetTimestamp(NOW - TWO_MONTHS)
})
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_EXCHANGE_RATE_ZERO')
})
})
}
const itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts = (requestedAmount, totalOwedAmount) => {
const expectedRequestedAmount = requestedAmount === 0 ? totalOwedAmount : requestedAmount
context('when the employee has some pending reimbursements', () => {
beforeEach('add accrued value', async () => {
await payroll.addAccruedValue(employeeId, 1000, { from: owner })
})
context('when the employee has a pending bonus', () => {
beforeEach('add bonus', async () => {
await payroll.addBonus(employeeId, 50000, {from: owner})
})
context('when the employee is not terminated', () => {
itHandlesPayrollProperly(requestedAmount, expectedRequestedAmount)
})
context('when the employee is terminated', () => {
beforeEach('terminate employee', async () => {
await payroll.terminateEmployeeNow(employeeId, { from: owner })
})
itHandlesPayrollProperly(requestedAmount, expectedRequestedAmount)
})
})
context('when the employee does not have a pending bonus', () => {
context('when the employee is not terminated', () => {
itHandlesPayrollProperly(requestedAmount, expectedRequestedAmount)
})
context('when the employee is terminated', () => {
beforeEach('terminate employee', async () => {
await payroll.terminateEmployeeNow(employeeId, { from: owner })
})
itHandlesPayrollProperly(requestedAmount, expectedRequestedAmount)
})
})
})
context('when the employee does not have pending reimbursements', () => {
context('when the employee has a pending bonus', () => {
beforeEach('add bonus', async () => {
await payroll.addBonus(employeeId, 50000, {from: owner})
})
context('when the employee is not terminated', () => {
itHandlesPayrollProperly(requestedAmount, expectedRequestedAmount)
})
context('when the employee is terminated', () => {
beforeEach('terminate employee', async () => {
await payroll.terminateEmployeeNow(employeeId, { from: owner })
})
itHandlesPayrollProperly(requestedAmount, expectedRequestedAmount)
})
})
context('when the employee does not have a pending bonus', () => {
context('when the employee is not terminated', () => {
itHandlesPayrollProperly(requestedAmount, expectedRequestedAmount)
})
context('when the employee is terminated', () => {
beforeEach('terminate employee', async () => {
await payroll.terminateEmployeeNow(employeeId, { from: owner })
})
if (requestedAmount === 0 || requestedAmount === totalOwedAmount) {
context('when exchange rates are not expired', () => {
assertTransferredAmounts(requestedAmount, expectedRequestedAmount)
it('removes the employee', async () => {
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
await assertRevert(payroll.getEmployee(employeeId), 'PAYROLL_EMPLOYEE_DOESNT_EXIST')
})
})
context('when exchange rates are expired', () => {
beforeEach('expire exchange rates', async () => {
await priceFeed.mockSetTimestamp(NOW - TWO_MONTHS)
})
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_EXCHANGE_RATE_ZERO')
})
})
}
else itHandlesPayrollProperly(requestedAmount, expectedRequestedAmount)
})
})
})
}
context('when the employee does not have accrued salary', () => {
context('when the employee has some pending salary', () => {
const currentOwedSalary = salary * ONE_MONTH
beforeEach('accumulate some pending salary', async () => {
await payroll.mockAddTimestamp(ONE_MONTH)
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, currentOwedSalary)
})
context('when the requested amount is lower than the total owed salary', () => {
context('when the requested amount is represents less than a second of the earnings', () => {
const requestedAmount = salary / 2
it('updates the last payroll date by one second', async () => {
const previousLastPayrollDate = (await payroll.getEmployee(employeeId))[5]
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const currentLastPayrollDate = (await payroll.getEmployee(employeeId))[5]
assert.equal(currentLastPayrollDate.toString(), previousLastPayrollDate.plus(1).toString(), 'last payroll date does not match')
})
})
context('when the requested amount is represents more than a second of the earnings', () => {
const requestedAmount = currentOwedSalary / 2
itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, currentOwedSalary)
})
})
context('when the requested amount is equal to the total owed salary', () => {
const requestedAmount = currentOwedSalary
itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, currentOwedSalary)
})
context('when the requested amount is greater than the total owed salary', () => {
const requestedAmount = currentOwedSalary + 1
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_INVALID_REQUESTED_AMT')
})
})
})
context('when the employee does not have pending salary', () => {
context('when the requested amount is greater than zero', () => {
const requestedAmount = 100
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_NOTHING_PAID')
})
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_NOTHING_PAID')
})
})
})
})
context('when the employee has some accrued salary', () => {
const previousSalary = 10
const previousOwedSalary = ONE_MONTH * previousSalary
beforeEach('accrue some salary', async () => {
await payroll.setEmployeeSalary(employeeId, previousSalary, { from: owner })
await payroll.mockAddTimestamp(ONE_MONTH)
await priceFeed.mockAddTimestamp(ONE_MONTH)
await payroll.setEmployeeSalary(employeeId, salary, { from: owner })
})
context('when the employee has some pending salary', () => {
const currentOwedSalary = salary * ONE_MONTH
const totalOwedSalary = previousOwedSalary + currentOwedSalary
beforeEach('accumulate some pending salary', async () => {
await payroll.mockAddTimestamp(ONE_MONTH)
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, totalOwedSalary)
})
context('when the requested amount is lower than the previous owed salary', () => {
const requestedAmount = previousOwedSalary - 10
itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, totalOwedSalary)
})
context('when the requested amount is greater than the previous owed salary but lower than the total owed', () => {
const requestedAmount = totalOwedSalary / 2
itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, totalOwedSalary)
})
context('when the requested amount is equal to the total owed salary', () => {
const requestedAmount = totalOwedSalary
itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, totalOwedSalary)
})
context('when the requested amount is greater than the total owed salary', () => {
const requestedAmount = totalOwedSalary + 1
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_INVALID_REQUESTED_AMT')
})
})
})
context('when the employee does not have pending salary', () => {
context('when the requested amount is zero', () => {
const requestedAmount = 0
itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, previousOwedSalary)
})
context('when the requested amount is lower than the previous owed salary', () => {
const requestedAmount = previousOwedSalary - 10
itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, previousOwedSalary)
})
context('when the requested amount is equal to the previous owed salary', () => {
const requestedAmount = previousOwedSalary
itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, previousOwedSalary)
})
context('when the requested amount is greater than the previous owed salary', () => {
const requestedAmount = previousOwedSalary + 1
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_INVALID_REQUESTED_AMT')
})
})
})
})
})
context('when the employee did not set any token allocations yet', () => {
context('when the employee has some pending salary', () => {
const owedSalary = salary * ONE_MONTH
beforeEach('accumulate some pending salary', async () => {
await payroll.mockAddTimestamp(ONE_MONTH)
})
context('when the requested amount is lower than the total owed salary', () => {
const requestedAmount = owedSalary - 10
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_NOTHING_PAID')
})
})
context('when the requested amount is equal to the total owed salary', () => {
const requestedAmount = owedSalary
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_NOTHING_PAID')
})
})
})
context('when the employee does not have pending salary', () => {
context('when the requested amount is greater than zero', () => {
const requestedAmount = 100
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_NOTHING_PAID')
})
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_NOTHING_PAID')
})
})
})
})
})
context('when the employee does not have a reasonable salary', () => {
const itReverts = (requestedAmount, reason) => {
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, {from}), reason)
})
}
const itRevertsHandlingExpiredRates = (requestedAmount, nonExpiredRatesReason, expiredRatesReason) => {
context('when exchange rates are not expired', () => {
itReverts(requestedAmount, nonExpiredRatesReason)
})
context('when exchange rates are expired', () => {
beforeEach('expire exchange rates', async () => {
await priceFeed.mockSetTimestamp(NOW - TWO_MONTHS)
})
itReverts(requestedAmount, expiredRatesReason)
})
}
const itRevertsToWithdrawPartialPayroll = (requestedAmount, nonExpiredRatesReason, expiredRatesReason) => {
context('when the employee has some pending reimbursements', () => {
beforeEach('add accrued value', async () => {
await payroll.addAccruedValue(employeeId, 1000, {from: owner})
})
context('when the employee is not terminated', () => {
itRevertsHandlingExpiredRates(requestedAmount, nonExpiredRatesReason, expiredRatesReason)
})
context('when the employee is terminated', () => {
beforeEach('terminate employee', async () => {
await payroll.terminateEmployeeNow(employeeId, {from: owner})
})
itRevertsHandlingExpiredRates(requestedAmount, nonExpiredRatesReason, expiredRatesReason)
})
})
context('when the employee does not have pending reimbursements', () => {
context('when the employee is not terminated', () => {
itRevertsHandlingExpiredRates(requestedAmount, nonExpiredRatesReason, expiredRatesReason)
})
context('when the employee is terminated', () => {
beforeEach('terminate employee', async () => {
await payroll.terminateEmployeeNow(employeeId, {from: owner})
})
itRevertsHandlingExpiredRates(requestedAmount, nonExpiredRatesReason, expiredRatesReason)
})
})
}
context('when the employee has a zero salary', () => {
const salary = 0
beforeEach('add employee', async () => {
const receipt = await payroll.addEmployeeNow(employee, salary, 'Boss')
employeeId = getEventArgument(receipt, 'AddEmployee', 'employeeId')
})
const itRevertsAnyAttemptToWithdrawPartialPayroll = () => {
context('when the employee has some pending salary', () => {
beforeEach('accumulate some pending salary', async () => {
await payroll.mockAddTimestamp(ONE_MONTH)
})
context('when the requested amount is greater than zero', () => {
const requestedAmount = 10000
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
})
context('when the employee does not have pending salary', () => {
context('when the requested amount is greater than zero', () => {
const requestedAmount = 1000
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
})
}
context('when the employee has already set some token allocations', () => {
const denominationTokenAllocation = 80
const anotherTokenAllocation = 20
beforeEach('set tokens allocation', async () => {
await payroll.addAllowedToken(anotherToken.address, {from: owner})
await payroll.addAllowedToken(denominationToken.address, {from: owner})
await payroll.determineAllocation([denominationToken.address, anotherToken.address], [denominationTokenAllocation, anotherTokenAllocation], {from})
})
itRevertsAnyAttemptToWithdrawPartialPayroll()
})
context('when the employee did not set any token allocations yet', () => {
itRevertsAnyAttemptToWithdrawPartialPayroll()
})
})
context('when the employee has a huge salary', () => {
const salary = maxUint256()
beforeEach('add employee', async () => {
const receipt = await payroll.addEmployeeNow(employee, salary, 'Boss')
employeeId = getEventArgument(receipt, 'AddEmployee', 'employeeId')
})
context('when the employee has already set some token allocations', () => {
const denominationTokenAllocation = 80
const anotherTokenAllocation = 20
beforeEach('set tokens allocation', async () => {
await payroll.addAllowedToken(anotherToken.address, {from: owner})
await payroll.addAllowedToken(denominationToken.address, {from: owner})
await payroll.determineAllocation([denominationToken.address, anotherToken.address], [denominationTokenAllocation, anotherTokenAllocation], {from})
})
context('when the employee has some pending salary', () => {
const owedSalary = maxUint256()
beforeEach('accumulate some pending salary', async () => {
await payroll.mockAddTimestamp(ONE_MONTH)
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
itRevertsToWithdrawPartialPayroll(requestedAmount, 'MATH_MUL_OVERFLOW', 'PAYROLL_EXCHANGE_RATE_ZERO')
})
context('when the requested amount is lower than the total owed salary', () => {
const requestedAmount = 10000
const assertTransferredAmounts = requestedAmount => {
const requestedDenominationTokenAmount = Math.round(requestedAmount * denominationTokenAllocation / 100)
const requestedAnotherTokenAmount = Math.round(requestedAmount * anotherTokenAllocation / 100)
it('transfers the requested salary amount', async () => {
const previousDenominationTokenBalance = await denominationToken.balanceOf(employee)
const previousAnotherTokenBalance = await anotherToken.balanceOf(employee)
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const currentDenominationTokenBalance = await denominationToken.balanceOf(employee)
const expectedDenominationTokenBalance = previousDenominationTokenBalance.plus(requestedDenominationTokenAmount);
assert.equal(currentDenominationTokenBalance.toString(), expectedDenominationTokenBalance.toString(), 'current denomination token balance does not match')
const currentAnotherTokenBalance = await anotherToken.balanceOf(employee)
const anotherTokenRate = (await priceFeed.get(denominationToken.address, anotherToken.address))[0].div(PCT_ONE)
const expectedAnotherTokenBalance = anotherTokenRate.mul(requestedAnotherTokenAmount).plus(previousAnotherTokenBalance).trunc()
assert.equal(currentAnotherTokenBalance.toString(), expectedAnotherTokenBalance.toString(), 'current token balance does not match')
})
it('emits one event per allocated token', async () => {
const receipt = await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const events = receipt.logs.filter(l => l.event === 'SendPayment')
assert.equal(events.length, 2, 'should have emitted two events')
const denominationTokenEvent = events.find(e => e.args.token === denominationToken.address).args
assert.equal(denominationTokenEvent.employee, employee, 'employee address does not match')
assert.equal(denominationTokenEvent.token, denominationToken.address, 'denomination token address does not match')
assert.equal(denominationTokenEvent.amount.toString(), requestedDenominationTokenAmount, 'payment amount does not match')
assert.equal(denominationTokenEvent.paymentReference, 'Payroll', 'payment reference does not match')
const anotherTokenRate = (await priceFeed.get(denominationToken.address, anotherToken.address))[0].div(PCT_ONE)
const anotherTokenEvent = events.find(e => e.args.token === anotherToken.address).args
assert.equal(anotherTokenEvent.employee, employee, 'employee address does not match')
assert.equal(anotherTokenEvent.token, anotherToken.address, 'token address does not match')
assert.equal(anotherTokenEvent.amount.div(anotherTokenRate).trunc().toString(), requestedAnotherTokenAmount, 'payment amount does not match')
assert.equal(anotherTokenEvent.paymentReference, 'Payroll', 'payment reference does not match')
})
it('can be called multiple times between periods of time', async () => {
// terminate employee in the future to ensure we can request payroll multiple times
await payroll.terminateEmployee(employeeId, NOW + TWO_MONTHS + TWO_MONTHS, { from: owner })
const previousDenominationTokenBalance = await denominationToken.balanceOf(employee)
const previousAnotherTokenBalance = await anotherToken.balanceOf(employee)
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
await payroll.mockAddTimestamp(ONE_MONTH)
await priceFeed.mockAddTimestamp(ONE_MONTH)
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const currentDenominationTokenBalance = await denominationToken.balanceOf(employee)
const expectedDenominationTokenBalance = previousDenominationTokenBalance.plus(requestedDenominationTokenAmount * 2)
assert.equal(currentDenominationTokenBalance.toString(), expectedDenominationTokenBalance.toString(), 'current denomination token balance does not match')
const currentAnotherTokenBalance = await anotherToken.balanceOf(employee)
const anotherTokenRate = (await priceFeed.get(denominationToken.address, anotherToken.address))[0].div(PCT_ONE)
const expectedAnotherTokenBalance = anotherTokenRate.mul(requestedAnotherTokenAmount * 2).plus(previousAnotherTokenBalance)
assert.equal(currentAnotherTokenBalance.toString(), expectedAnotherTokenBalance.toString(), 'current token balance does not match')
})
}
const assertEmployeeIsUpdated = requestedAmount => {
it('updates the last payroll date', async () => {
const timeDiff = 1 // should be bn(requestedAmount).div(salary).ceil() but BN cannot represent such a small number, hardcoding it to 1
const previousPayrollDate = (await payroll.getEmployee(employeeId))[5]
const expectedLastPayrollDate = previousPayrollDate.plus(timeDiff)
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const lastPayrollDate = (await payroll.getEmployee(employeeId))[5]
assert.equal(lastPayrollDate.toString(), expectedLastPayrollDate.toString(), 'last payroll date does not match')
})
it('does not remove the employee', async () => {
await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })
const [address, employeeSalary] = await payroll.getEmployee(employeeId)
assert.equal(address, employee, 'employee address does not match')
assert.equal(employeeSalary.toString(), salary.toString())
})
}
const itHandlesPayrollProperly = requestedAmount => {
context('when exchange rates are not expired', () => {
assertTransferredAmounts(requestedAmount)
assertEmployeeIsUpdated(requestedAmount)
})
context('when exchange rates are expired', () => {
beforeEach('expire exchange rates', async () => {
await priceFeed.mockSetTimestamp(NOW - TWO_MONTHS)
})
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_EXCHANGE_RATE_ZERO')
})
})
}
context('when the employee has some pending reimbursements', () => {
beforeEach('add accrued value', async () => {
await payroll.addAccruedValue(employeeId, 1000, { from: owner })
})
context('when the employee is not terminated', () => {
itHandlesPayrollProperly(requestedAmount)
})
context('when the employee is terminated', () => {
beforeEach('terminate employee', async () => {
await payroll.terminateEmployeeNow(employeeId, { from: owner })
})
itHandlesPayrollProperly(requestedAmount)
})
})
context('when the employee does not have pending reimbursements', () => {
context('when the employee is not terminated', () => {
itHandlesPayrollProperly(requestedAmount)
})
context('when the employee is terminated', () => {
beforeEach('terminate employee', async () => {
await payroll.terminateEmployeeNow(employeeId, { from: owner })
})
itHandlesPayrollProperly(requestedAmount)
})
})
})
context('when the requested amount is equal to the total owed salary', () => {
const requestedAmount = owedSalary
itRevertsToWithdrawPartialPayroll(requestedAmount, 'MATH_MUL_OVERFLOW', 'PAYROLL_EXCHANGE_RATE_ZERO')
})
})
context('when the employee does not have pending salary', () => {
context('when the requested amount is greater than zero', () => {
const requestedAmount = 100
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
})
})
context('when the employee did not set any token allocations yet', () => {
context('when the employee has some pending salary', () => {
const owedSalary = maxUint256()
beforeEach('accumulate some pending salary', async () => {
await payroll.mockAddTimestamp(ONE_MONTH)
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
context('when the requested amount is lower than the total owed salary', () => {
const requestedAmount = 10000
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
context('when the requested amount is equal to the total owed salary', () => {
const requestedAmount = owedSalary
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
})
context('when the employee does not have pending salary', () => {
context('when the requested amount is greater than zero', () => {
const requestedAmount = 100
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
itRevertsToWithdrawPartialPayroll(requestedAmount, 'PAYROLL_NOTHING_PAID', 'PAYROLL_NOTHING_PAID')
})
})
})
})
})
})
context('when the sender is not an employee', () => {
const from = anyone
context('when the requested amount is greater than zero', () => {
const requestedAmount = 100
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_EMPLOYEE_DOES_NOT_MATCH')
})
})
context('when the requested amount is zero', () => {
const requestedAmount = 0
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from }), 'PAYROLL_EMPLOYEE_DOES_NOT_MATCH')
})
})
})
})
context('when it has not been initialized yet', function () {
const requestedAmount = 0
it('reverts', async () => {
await assertRevert(payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from: employee }), 'PAYROLL_EMPLOYEE_DOES_NOT_MATCH')
})
})
})
})