1
1
import { BigNumber , TxData , ERC20DetailedContract } from '@polymathnetwork/abi-wrappers' ;
2
2
import ModuleWrapper from '../module_wrapper' ;
3
3
import assert from '../../../utils/assert' ;
4
- import { TxParams , DividendCheckpointBaseContract , Perm , PERCENTAGE_DECIMALS } from '../../../types' ;
4
+ import { TxParams , DividendCheckpointBaseContract , Perm , PERCENTAGE_DECIMALS , ErrorCode } from '../../../types' ;
5
5
import {
6
6
numberToBigNumber ,
7
7
dateToBigNumber ,
@@ -207,20 +207,32 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
207
207
} ;
208
208
209
209
public pause = async ( params : TxParams ) => {
210
- assert . assert ( ! ( await this . paused ( ) ) , 'Contract currently paused' ) ;
211
- assert . assert ( await this . isCallerTheSecurityTokenOwner ( params . txData ) , 'The caller must be the ST owner' ) ;
210
+ assert . assert ( ! ( await this . paused ( ) ) , ErrorCode . ContractPaused , 'Contract currently paused' ) ;
211
+ assert . assert (
212
+ await this . isCallerTheSecurityTokenOwner ( params . txData ) ,
213
+ ErrorCode . Unauthorized ,
214
+ 'The caller must be the ST owner' ,
215
+ ) ;
212
216
return ( await this . contract ) . pause . sendTransactionAsync ( params . txData , params . safetyFactor ) ;
213
217
} ;
214
218
215
219
public unpause = async ( params : TxParams ) => {
216
- assert . assert ( await this . paused ( ) , 'Contract currently not paused' ) ;
217
- assert . assert ( await this . isCallerTheSecurityTokenOwner ( params . txData ) , 'The caller must be the ST owner' ) ;
220
+ assert . assert ( await this . paused ( ) , ErrorCode . PreconditionRequired , 'Contract currently not paused' ) ;
221
+ assert . assert (
222
+ await this . isCallerTheSecurityTokenOwner ( params . txData ) ,
223
+ ErrorCode . Unauthorized ,
224
+ 'The caller must be the ST owner' ,
225
+ ) ;
218
226
return ( await this . contract ) . unpause . sendTransactionAsync ( params . txData , params . safetyFactor ) ;
219
227
} ;
220
228
221
229
public changeWallet = async ( params : ChangeWalletParams ) => {
222
230
assert . isNonZeroETHAddressHex ( 'wallet' , params . wallet ) ;
223
- assert . assert ( await this . isCallerTheSecurityTokenOwner ( params . txData ) , 'The caller must be the ST owner' ) ;
231
+ assert . assert (
232
+ await this . isCallerTheSecurityTokenOwner ( params . txData ) ,
233
+ ErrorCode . Unauthorized ,
234
+ 'The caller must be the ST owner' ,
235
+ ) ;
224
236
return ( await this . contract ) . changeWallet . sendTransactionAsync ( params . wallet , params . txData , params . safetyFactor ) ;
225
237
} ;
226
238
@@ -229,13 +241,25 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
229
241
} ;
230
242
231
243
public createCheckpoint = async ( params : TxParams ) => {
232
- assert . assert ( await this . isCallerAllowed ( params . txData , Perm . Operator ) , 'Caller is not allowed' ) ;
244
+ assert . assert (
245
+ await this . isCallerAllowed ( params . txData , Perm . Operator ) ,
246
+ ErrorCode . Unauthorized ,
247
+ 'Caller is not allowed' ,
248
+ ) ;
233
249
return ( await this . contract ) . createCheckpoint . sendTransactionAsync ( params . txData , params . safetyFactor ) ;
234
250
} ;
235
251
236
252
public setDefaultExcluded = async ( params : SetDefaultExcludedParams ) => {
237
- assert . assert ( await this . isCallerAllowed ( params . txData , Perm . Admin ) , 'Caller is not allowed' ) ;
238
- assert . assert ( params . excluded . length <= EXCLUDED_ADDRESS_LIMIT , 'Too many excluded addresses' ) ;
253
+ assert . assert (
254
+ await this . isCallerAllowed ( params . txData , Perm . Admin ) ,
255
+ ErrorCode . Unauthorized ,
256
+ 'Caller is not allowed' ,
257
+ ) ;
258
+ assert . assert (
259
+ params . excluded . length <= EXCLUDED_ADDRESS_LIMIT ,
260
+ ErrorCode . InvalidData ,
261
+ 'Too many excluded addresses' ,
262
+ ) ;
239
263
params . excluded . forEach ( address => assert . isNonZeroETHAddressHex ( 'excluded' , address ) ) ;
240
264
assert . areThereDuplicatedStrings ( 'excluded' , params . excluded ) ;
241
265
return ( await this . contract ) . setDefaultExcluded . sendTransactionAsync (
@@ -246,8 +270,16 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
246
270
} ;
247
271
248
272
public setWithholding = async ( params : SetWithholdingParams ) => {
249
- assert . assert ( await this . isCallerAllowed ( params . txData , Perm . Admin ) , 'Caller is not allowed' ) ;
250
- assert . assert ( params . investors . length === params . withholding . length , 'Mismatched input lengths' ) ;
273
+ assert . assert (
274
+ await this . isCallerAllowed ( params . txData , Perm . Admin ) ,
275
+ ErrorCode . Unauthorized ,
276
+ 'Caller is not allowed' ,
277
+ ) ;
278
+ assert . assert (
279
+ params . investors . length === params . withholding . length ,
280
+ ErrorCode . InvalidData ,
281
+ 'Mismatched input lengths' ,
282
+ ) ;
251
283
params . withholding . forEach ( withholding => assert . isPercentage ( 'withholding tax' , withholding ) ) ;
252
284
return ( await this . contract ) . setWithholding . sendTransactionAsync (
253
285
params . investors ,
@@ -258,7 +290,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
258
290
} ;
259
291
260
292
public setWithholdingFixed = async ( params : SetWithholdingFixedParams ) => {
261
- assert . assert ( await this . isCallerAllowed ( params . txData , Perm . Admin ) , 'Caller is not allowed' ) ;
293
+ assert . assert (
294
+ await this . isCallerAllowed ( params . txData , Perm . Admin ) ,
295
+ ErrorCode . Unauthorized ,
296
+ 'Caller is not allowed' ,
297
+ ) ;
262
298
assert . isPercentage ( 'withholding tax' , params . withholding ) ;
263
299
return ( await this . contract ) . setWithholdingFixed . sendTransactionAsync (
264
300
params . investors ,
@@ -269,7 +305,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
269
305
} ;
270
306
271
307
public pushDividendPaymentToAddresses = async ( params : PushDividendPaymentToAddressesParams ) => {
272
- assert . assert ( await this . isCallerAllowed ( params . txData , Perm . Operator ) , 'Caller is not allowed' ) ;
308
+ assert . assert (
309
+ await this . isCallerAllowed ( params . txData , Perm . Operator ) ,
310
+ ErrorCode . Unauthorized ,
311
+ 'Caller is not allowed' ,
312
+ ) ;
273
313
params . payees . forEach ( address => assert . isNonZeroETHAddressHex ( 'payees' , address ) ) ;
274
314
await this . checkValidDividend ( params . dividendIndex ) ;
275
315
return ( await this . contract ) . pushDividendPaymentToAddresses . sendTransactionAsync (
@@ -281,7 +321,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
281
321
} ;
282
322
283
323
public pushDividendPayment = async ( params : PushDividendPaymentParams ) => {
284
- assert . assert ( await this . isCallerAllowed ( params . txData , Perm . Operator ) , 'Caller is not allowed' ) ;
324
+ assert . assert (
325
+ await this . isCallerAllowed ( params . txData , Perm . Operator ) ,
326
+ ErrorCode . Unauthorized ,
327
+ 'Caller is not allowed' ,
328
+ ) ;
285
329
await this . checkValidDividend ( params . dividendIndex ) ;
286
330
return ( await this . contract ) . pushDividendPayment . sendTransactionAsync (
287
331
numberToBigNumber ( params . dividendIndex ) ,
@@ -294,18 +338,18 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
294
338
295
339
public pullDividendPayment = async ( params : DividendIndexTxParams ) => {
296
340
await this . checkValidDividend ( params . dividendIndex ) ;
297
- assert . assert ( ! ( await this . paused ( ) ) , 'Contract currently paused' ) ;
341
+ assert . assert ( ! ( await this . paused ( ) ) , ErrorCode . PreconditionRequired , 'Contract currently paused' ) ;
298
342
const investor = await this . getCallerAddress ( params . txData ) ;
299
343
const isClaimed = await ( await this . contract ) . isClaimed . callAsync (
300
344
investor ,
301
345
numberToBigNumber ( params . dividendIndex ) ,
302
346
) ;
303
- assert . assert ( ! isClaimed , `${ investor } has already claimed this dividend` ) ;
347
+ assert . assert ( ! isClaimed , ErrorCode . AlreadyClaimed , `${ investor } has already claimed this dividend` ) ;
304
348
const isExcluded = await ( await this . contract ) . isExcluded . callAsync (
305
349
investor ,
306
350
numberToBigNumber ( params . dividendIndex ) ,
307
351
) ;
308
- assert . assert ( ! isExcluded , `${ investor } is excluded from dividend` ) ;
352
+ assert . assert ( ! isExcluded , ErrorCode . AddressExcluded , `${ investor } is excluded from dividend` ) ;
309
353
return ( await this . contract ) . pullDividendPayment . sendTransactionAsync (
310
354
numberToBigNumber ( params . dividendIndex ) ,
311
355
params . txData ,
@@ -314,11 +358,19 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
314
358
} ;
315
359
316
360
public reclaimDividend = async ( params : DividendIndexTxParams ) => {
317
- assert . assert ( await this . isCallerAllowed ( params . txData , Perm . Operator ) , 'Caller is not allowed' ) ;
318
- assert . assert ( await this . isValidDividendIndex ( params . dividendIndex ) , 'Invalid dividend index' ) ;
361
+ assert . assert (
362
+ await this . isCallerAllowed ( params . txData , Perm . Operator ) ,
363
+ ErrorCode . Unauthorized ,
364
+ 'Caller is not allowed' ,
365
+ ) ;
366
+ assert . assert (
367
+ await this . isValidDividendIndex ( params . dividendIndex ) ,
368
+ ErrorCode . InvalidData ,
369
+ 'Invalid dividend index' ,
370
+ ) ;
319
371
const dividend = await this . dividends ( params ) ;
320
372
assert . isPastDate ( dividend . expiry , 'Dividend expiry is in the future' ) ;
321
- assert . assert ( ! dividend . reclaimed , 'Dividend is already claimed' ) ;
373
+ assert . assert ( ! dividend . reclaimed , ErrorCode . AlreadyClaimed , 'Dividend is already claimed' ) ;
322
374
return ( await this . contract ) . reclaimDividend . sendTransactionAsync (
323
375
numberToBigNumber ( params . dividendIndex ) ,
324
376
params . txData ,
@@ -327,7 +379,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
327
379
} ;
328
380
329
381
public calculateDividend = async ( params : CalculateDividendParams ) => {
330
- assert . assert ( await this . isValidDividendIndex ( params . dividendIndex ) , 'Invalid dividend index' ) ;
382
+ assert . assert (
383
+ await this . isValidDividendIndex ( params . dividendIndex ) ,
384
+ ErrorCode . InvalidDividend ,
385
+ 'Invalid dividend index' ,
386
+ ) ;
331
387
const result = await ( await this . contract ) . calculateDividend . callAsync (
332
388
numberToBigNumber ( params . dividendIndex ) ,
333
389
params . payee ,
@@ -345,8 +401,16 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
345
401
} ;
346
402
347
403
public withdrawWithholding = async ( params : DividendIndexTxParams ) => {
348
- assert . assert ( await this . isCallerAllowed ( params . txData , Perm . Operator ) , 'Caller is not allowed' ) ;
349
- assert . assert ( await this . isValidDividendIndex ( params . dividendIndex ) , 'Invalid dividend index' ) ;
404
+ assert . assert (
405
+ await this . isCallerAllowed ( params . txData , Perm . Operator ) ,
406
+ ErrorCode . Unauthorized ,
407
+ 'Caller is not allowed' ,
408
+ ) ;
409
+ assert . assert (
410
+ await this . isValidDividendIndex ( params . dividendIndex ) ,
411
+ ErrorCode . InvalidDividend ,
412
+ 'Invalid dividend index' ,
413
+ ) ;
350
414
return ( await this . contract ) . withdrawWithholding . sendTransactionAsync (
351
415
numberToBigNumber ( params . dividendIndex ) ,
352
416
params . txData ,
@@ -355,9 +419,17 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
355
419
} ;
356
420
357
421
public updateDividendDates = async ( params : UpdateDividendDatesParams ) => {
358
- assert . assert ( await this . isCallerTheSecurityTokenOwner ( params . txData ) , 'The caller must be the ST owner' ) ;
359
- assert . assert ( await this . isValidDividendIndex ( params . dividendIndex ) , 'Invalid dividend index' ) ;
360
- assert . assert ( params . expiry > params . maturity , 'Expiry before maturity' ) ;
422
+ assert . assert (
423
+ await this . isCallerTheSecurityTokenOwner ( params . txData ) ,
424
+ ErrorCode . Unauthorized ,
425
+ 'The caller must be the ST owner' ,
426
+ ) ;
427
+ assert . assert (
428
+ await this . isValidDividendIndex ( params . dividendIndex ) ,
429
+ ErrorCode . InvalidDividend ,
430
+ 'Invalid dividend index' ,
431
+ ) ;
432
+ assert . assert ( params . expiry > params . maturity , ErrorCode . TooLate , 'Expiry before maturity' ) ;
361
433
return ( await this . contract ) . updateDividendDates . sendTransactionAsync (
362
434
numberToBigNumber ( params . dividendIndex ) ,
363
435
dateToBigNumber ( params . maturity ) ,
@@ -406,7 +478,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
406
478
} ;
407
479
408
480
public getDividendProgress = async ( params : DividendIndexParams ) => {
409
- assert . assert ( await this . isValidDividendIndex ( params . dividendIndex ) , 'Invalid dividend index' ) ;
481
+ assert . assert (
482
+ await this . isValidDividendIndex ( params . dividendIndex ) ,
483
+ ErrorCode . InvalidDividend ,
484
+ 'Invalid dividend index' ,
485
+ ) ;
410
486
const result = await ( await this . contract ) . getDividendProgress . callAsync ( numberToBigNumber ( params . dividendIndex ) ) ;
411
487
const typedResult : Promise < DividendProgress > [ ] = [ ] ;
412
488
for ( let i = 0 ; i < result [ 0 ] . length ; i += 1 ) {
@@ -433,7 +509,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
433
509
434
510
public getCheckpointData = async ( params : CheckpointIdParams ) => {
435
511
const currentCheckpointId = await ( await this . securityTokenContract ( ) ) . currentCheckpointId . callAsync ( ) ;
436
- assert . assert ( params . checkpointId <= new BigNumber ( currentCheckpointId ) . toNumber ( ) , 'Invalid checkpoint' ) ;
512
+ assert . assert (
513
+ params . checkpointId <= new BigNumber ( currentCheckpointId ) . toNumber ( ) ,
514
+ ErrorCode . InvalidCheckpoint ,
515
+ 'Invalid checkpoint' ,
516
+ ) ;
437
517
const result = await ( await this . contract ) . getCheckpointData . callAsync ( numberToBigNumber ( params . checkpointId ) ) ;
438
518
const typedResult : Promise < CheckpointData > [ ] = [ ] ;
439
519
for ( let i = 0 ; i < result [ 0 ] . length ; i += 1 ) {
@@ -456,13 +536,21 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
456
536
457
537
public isClaimed = async ( params : InvestorStatus ) => {
458
538
assert . isETHAddressHex ( 'investor' , params . investor ) ;
459
- assert . assert ( await this . isValidDividendIndex ( params . dividendIndex ) , 'Invalid dividend index' ) ;
539
+ assert . assert (
540
+ await this . isValidDividendIndex ( params . dividendIndex ) ,
541
+ ErrorCode . InvalidDividend ,
542
+ 'Invalid dividend index' ,
543
+ ) ;
460
544
return ( await this . contract ) . isClaimed . callAsync ( params . investor , numberToBigNumber ( params . dividendIndex ) ) ;
461
545
} ;
462
546
463
547
public isExcluded = async ( params : InvestorStatus ) => {
464
548
assert . isETHAddressHex ( 'investor' , params . investor ) ;
465
- assert . assert ( await this . isValidDividendIndex ( params . dividendIndex ) , 'Invalid dividend index' ) ;
549
+ assert . assert (
550
+ await this . isValidDividendIndex ( params . dividendIndex ) ,
551
+ ErrorCode . InvalidDividend ,
552
+ 'Invalid dividend index' ,
553
+ ) ;
466
554
return ( await this . contract ) . isExcluded . callAsync ( params . investor , numberToBigNumber ( params . dividendIndex ) ) ;
467
555
} ;
468
556
@@ -472,10 +560,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
472
560
} ;
473
561
474
562
private checkValidDividend = async ( dividendIndex : number ) => {
475
- assert . assert ( await this . isValidDividendIndex ( dividendIndex ) , 'Invalid dividend index' ) ;
563
+ assert . assert ( await this . isValidDividendIndex ( dividendIndex ) , ErrorCode . InvalidDividend , 'Invalid dividend index' ) ;
476
564
const dividend = await this . getDividendData ( { dividendIndex } ) ;
477
565
assert . assert (
478
566
! dividend . claimedAmount . isGreaterThan ( 0 ) ,
567
+ ErrorCode . InvalidData ,
479
568
'Dividend claimed amount greater than 0, dividend reclaimed' ,
480
569
) ;
481
570
assert . isPastDate ( dividend . maturity , 'Dividend maturity in future' ) ;
@@ -494,17 +583,21 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
494
583
) => {
495
584
excluded . forEach ( address => assert . isNonZeroETHAddressHex ( 'Excluded address' , address ) ) ;
496
585
assert . areThereDuplicatedStrings ( 'Excluded addresses' , excluded ) ;
497
- assert . assert ( excluded . length <= EXCLUDED_ADDRESS_LIMIT , 'Too many addresses excluded' ) ;
498
- assert . assert ( expiry > maturity , 'Expiry before maturity' ) ;
586
+ assert . assert (
587
+ excluded . length <= EXCLUDED_ADDRESS_LIMIT ,
588
+ ErrorCode . InvalidLenghtLimit ,
589
+ 'Too many addresses excluded' ,
590
+ ) ;
591
+ assert . assert ( expiry > maturity , ErrorCode . TooLate , 'Expiry before maturity' ) ;
499
592
assert . isFutureDate ( expiry , 'Expiry in past' ) ;
500
593
assert . isBigNumberGreaterThanZero ( amount , 'No dividend sent' ) ;
501
- assert . assert ( name . length > 0 , 'The name can not be empty' ) ;
594
+ assert . assert ( name . length > 0 , ErrorCode . InvalidData , 'The name can not be empty' ) ;
502
595
503
596
const stContract = await this . securityTokenContract ( ) ;
504
597
505
598
if ( checkpointId ) {
506
599
const currentCheckpointId = await stContract . currentCheckpointId . callAsync ( ) ;
507
- assert . assert ( checkpointId <= currentCheckpointId . toNumber ( ) , 'Invalid checkpoint' ) ;
600
+ assert . assert ( checkpointId <= currentCheckpointId . toNumber ( ) , ErrorCode . InvalidCheckpoint , 'Invalid checkpoint' ) ;
508
601
}
509
602
510
603
const callerAddress = await this . getCallerAddress ( txData ) ;
@@ -515,15 +608,18 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
515
608
const erc20TokenAllowance = await erc20Detailed . allowance . callAsync ( callerAddress , await this . address ( ) ) ;
516
609
assert . assert (
517
610
erc20TokenAllowance . isGreaterThanOrEqualTo ( amount ) ,
611
+ ErrorCode . InvalidData ,
518
612
'Your allowance is less than dividend amount' ,
519
613
) ;
520
614
assert . assert (
521
615
erc20TokenBalance . isGreaterThanOrEqualTo ( amount ) ,
616
+ ErrorCode . InvalidData ,
522
617
'Your balance is less than dividend amount' ,
523
618
) ;
524
619
} else {
525
620
assert . assert (
526
621
( await this . web3Wrapper . getBalanceInWeiAsync ( callerAddress ) ) . isGreaterThanOrEqualTo ( amount . valueOf ( ) ) ,
622
+ ErrorCode . PreconditionRequired ,
527
623
'Caller Address ETH Balance does not meet amount needed to create dividend' ,
528
624
) ;
529
625
}
@@ -541,7 +637,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
541
637
currentSupply = await stContract . totalSupply . callAsync ( ) ;
542
638
}
543
639
544
- assert . assert ( ! currentSupply . isZero ( ) , 'Invalid supply, current supply must be greater than 0' ) ;
640
+ assert . assert (
641
+ ! currentSupply . isZero ( ) ,
642
+ ErrorCode . InvalidData ,
643
+ 'Invalid supply, current supply must be greater than 0' ,
644
+ ) ;
545
645
546
646
// that hardcoded 0 is necessary for the sum function not
547
647
// to return NaN if the excluded addresses array is empty
@@ -550,6 +650,7 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
550
650
551
651
assert . assert (
552
652
currentSupply . isGreaterThan ( totalExcludedSupply ) ,
653
+ ErrorCode . InvalidData ,
553
654
'Invalid supply, current supply must be greater than excluded supply' ,
554
655
) ;
555
656
} ;
0 commit comments