Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.

Commit 5bebc6b

Browse files
committed
fix: 🐛 implement ErrorCode type for dividend checkpoint wrapper
1 parent 0b4ba33 commit 5bebc6b

File tree

3 files changed

+148
-40
lines changed

3 files changed

+148
-40
lines changed

src/contract_wrappers/modules/checkpoint/dividend_checkpoint_wrapper.ts

Lines changed: 137 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BigNumber, TxData, ERC20DetailedContract } from '@polymathnetwork/abi-wrappers';
22
import ModuleWrapper from '../module_wrapper';
33
import assert from '../../../utils/assert';
4-
import { TxParams, DividendCheckpointBaseContract, Perm, PERCENTAGE_DECIMALS } from '../../../types';
4+
import { TxParams, DividendCheckpointBaseContract, Perm, PERCENTAGE_DECIMALS, ErrorCode } from '../../../types';
55
import {
66
numberToBigNumber,
77
dateToBigNumber,
@@ -207,20 +207,32 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
207207
};
208208

209209
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+
);
212216
return (await this.contract).pause.sendTransactionAsync(params.txData, params.safetyFactor);
213217
};
214218

215219
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+
);
218226
return (await this.contract).unpause.sendTransactionAsync(params.txData, params.safetyFactor);
219227
};
220228

221229
public changeWallet = async (params: ChangeWalletParams) => {
222230
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+
);
224236
return (await this.contract).changeWallet.sendTransactionAsync(params.wallet, params.txData, params.safetyFactor);
225237
};
226238

@@ -229,13 +241,25 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
229241
};
230242

231243
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+
);
233249
return (await this.contract).createCheckpoint.sendTransactionAsync(params.txData, params.safetyFactor);
234250
};
235251

236252
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+
);
239263
params.excluded.forEach(address => assert.isNonZeroETHAddressHex('excluded', address));
240264
assert.areThereDuplicatedStrings('excluded', params.excluded);
241265
return (await this.contract).setDefaultExcluded.sendTransactionAsync(
@@ -246,8 +270,16 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
246270
};
247271

248272
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+
);
251283
params.withholding.forEach(withholding => assert.isPercentage('withholding tax', withholding));
252284
return (await this.contract).setWithholding.sendTransactionAsync(
253285
params.investors,
@@ -258,7 +290,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
258290
};
259291

260292
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+
);
262298
assert.isPercentage('withholding tax', params.withholding);
263299
return (await this.contract).setWithholdingFixed.sendTransactionAsync(
264300
params.investors,
@@ -269,7 +305,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
269305
};
270306

271307
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+
);
273313
params.payees.forEach(address => assert.isNonZeroETHAddressHex('payees', address));
274314
await this.checkValidDividend(params.dividendIndex);
275315
return (await this.contract).pushDividendPaymentToAddresses.sendTransactionAsync(
@@ -281,7 +321,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
281321
};
282322

283323
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+
);
285329
await this.checkValidDividend(params.dividendIndex);
286330
return (await this.contract).pushDividendPayment.sendTransactionAsync(
287331
numberToBigNumber(params.dividendIndex),
@@ -294,18 +338,18 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
294338

295339
public pullDividendPayment = async (params: DividendIndexTxParams) => {
296340
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');
298342
const investor = await this.getCallerAddress(params.txData);
299343
const isClaimed = await (await this.contract).isClaimed.callAsync(
300344
investor,
301345
numberToBigNumber(params.dividendIndex),
302346
);
303-
assert.assert(!isClaimed, `${investor} has already claimed this dividend`);
347+
assert.assert(!isClaimed, ErrorCode.AlreadyClaimed, `${investor} has already claimed this dividend`);
304348
const isExcluded = await (await this.contract).isExcluded.callAsync(
305349
investor,
306350
numberToBigNumber(params.dividendIndex),
307351
);
308-
assert.assert(!isExcluded, `${investor} is excluded from dividend`);
352+
assert.assert(!isExcluded, ErrorCode.AddressExcluded, `${investor} is excluded from dividend`);
309353
return (await this.contract).pullDividendPayment.sendTransactionAsync(
310354
numberToBigNumber(params.dividendIndex),
311355
params.txData,
@@ -314,11 +358,19 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
314358
};
315359

316360
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+
);
319371
const dividend = await this.dividends(params);
320372
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');
322374
return (await this.contract).reclaimDividend.sendTransactionAsync(
323375
numberToBigNumber(params.dividendIndex),
324376
params.txData,
@@ -327,7 +379,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
327379
};
328380

329381
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+
);
331387
const result = await (await this.contract).calculateDividend.callAsync(
332388
numberToBigNumber(params.dividendIndex),
333389
params.payee,
@@ -345,8 +401,16 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
345401
};
346402

347403
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+
);
350414
return (await this.contract).withdrawWithholding.sendTransactionAsync(
351415
numberToBigNumber(params.dividendIndex),
352416
params.txData,
@@ -355,9 +419,17 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
355419
};
356420

357421
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');
361433
return (await this.contract).updateDividendDates.sendTransactionAsync(
362434
numberToBigNumber(params.dividendIndex),
363435
dateToBigNumber(params.maturity),
@@ -406,7 +478,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
406478
};
407479

408480
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+
);
410486
const result = await (await this.contract).getDividendProgress.callAsync(numberToBigNumber(params.dividendIndex));
411487
const typedResult: Promise<DividendProgress>[] = [];
412488
for (let i = 0; i < result[0].length; i += 1) {
@@ -433,7 +509,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
433509

434510
public getCheckpointData = async (params: CheckpointIdParams) => {
435511
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+
);
437517
const result = await (await this.contract).getCheckpointData.callAsync(numberToBigNumber(params.checkpointId));
438518
const typedResult: Promise<CheckpointData>[] = [];
439519
for (let i = 0; i < result[0].length; i += 1) {
@@ -456,13 +536,21 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
456536

457537
public isClaimed = async (params: InvestorStatus) => {
458538
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+
);
460544
return (await this.contract).isClaimed.callAsync(params.investor, numberToBigNumber(params.dividendIndex));
461545
};
462546

463547
public isExcluded = async (params: InvestorStatus) => {
464548
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+
);
466554
return (await this.contract).isExcluded.callAsync(params.investor, numberToBigNumber(params.dividendIndex));
467555
};
468556

@@ -472,10 +560,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
472560
};
473561

474562
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');
476564
const dividend = await this.getDividendData({ dividendIndex });
477565
assert.assert(
478566
!dividend.claimedAmount.isGreaterThan(0),
567+
ErrorCode.InvalidData,
479568
'Dividend claimed amount greater than 0, dividend reclaimed',
480569
);
481570
assert.isPastDate(dividend.maturity, 'Dividend maturity in future');
@@ -494,17 +583,21 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
494583
) => {
495584
excluded.forEach(address => assert.isNonZeroETHAddressHex('Excluded address', address));
496585
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');
499592
assert.isFutureDate(expiry, 'Expiry in past');
500593
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');
502595

503596
const stContract = await this.securityTokenContract();
504597

505598
if (checkpointId) {
506599
const currentCheckpointId = await stContract.currentCheckpointId.callAsync();
507-
assert.assert(checkpointId <= currentCheckpointId.toNumber(), 'Invalid checkpoint');
600+
assert.assert(checkpointId <= currentCheckpointId.toNumber(), ErrorCode.InvalidCheckpoint, 'Invalid checkpoint');
508601
}
509602

510603
const callerAddress = await this.getCallerAddress(txData);
@@ -515,15 +608,18 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
515608
const erc20TokenAllowance = await erc20Detailed.allowance.callAsync(callerAddress, await this.address());
516609
assert.assert(
517610
erc20TokenAllowance.isGreaterThanOrEqualTo(amount),
611+
ErrorCode.InvalidData,
518612
'Your allowance is less than dividend amount',
519613
);
520614
assert.assert(
521615
erc20TokenBalance.isGreaterThanOrEqualTo(amount),
616+
ErrorCode.InvalidData,
522617
'Your balance is less than dividend amount',
523618
);
524619
} else {
525620
assert.assert(
526621
(await this.web3Wrapper.getBalanceInWeiAsync(callerAddress)).isGreaterThanOrEqualTo(amount.valueOf()),
622+
ErrorCode.PreconditionRequired,
527623
'Caller Address ETH Balance does not meet amount needed to create dividend',
528624
);
529625
}
@@ -541,7 +637,11 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
541637
currentSupply = await stContract.totalSupply.callAsync();
542638
}
543639

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+
);
545645

546646
// that hardcoded 0 is necessary for the sum function not
547647
// to return NaN if the excluded addresses array is empty
@@ -550,6 +650,7 @@ export default abstract class DividendCheckpointWrapper extends ModuleWrapper {
550650

551651
assert.assert(
552652
currentSupply.isGreaterThan(totalExcludedSupply),
653+
ErrorCode.InvalidData,
553654
'Invalid supply, current supply must be greater than excluded supply',
554655
);
555656
};

src/types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,22 @@ export const PERCENTAGE_DECIMALS = new BigNumber(16);
337337
export const FULL_DECIMALS = new BigNumber(18);
338338

339339
export enum ErrorCode {
340-
UserDeniedAccess = 'UserDeniedAccess',
340+
Unauthorized = 'Unauthorized',
341341
InvalidAddress = 'InvalidAddress',
342342
InsufficientBalance = 'InsufficientBalance',
343343
InvalidSubscriptionToken = 'InvalidSubscriptionToken',
344344
DuplicatedStrings = 'DuplicatedStrings',
345-
TooFar = 'TooFar',
345+
TooLate = 'TooLate',
346346
TooEarly = 'TooEarly',
347347
InvalidData = 'InvalidData',
348348
MismatchedArrayLength = 'MismatchedArrayLength',
349349
InvalidVersion = 'InvalidVersion',
350350
InvalidPartition = 'InvalidPartition',
351+
ContractPaused = 'ContractPaused',
352+
PreconditionRequired = 'PreconditionRequired',
353+
AlreadyClaimed = 'AlreadyClaimed',
354+
AddressExcluded = 'AddressExcluded',
355+
InvalidDividend = 'InvalidDividend',
356+
InvalidCheckpoint = 'InvalidCheckpoint',
357+
InvalidLenghtLimit = 'InvalidLenghtLimit',
351358
}

0 commit comments

Comments
 (0)