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

Commit a08335e

Browse files
committed
fix: 🐛 implement ErrorClass in VEW
1 parent 9379d20 commit a08335e

File tree

1 file changed

+140
-44
lines changed

1 file changed

+140
-44
lines changed

src/contract_wrappers/modules/wallet/vesting_escrow_wallet_wrapper.ts

Lines changed: 140 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
GetLogs,
3232
Perm,
3333
TransferStatusCode,
34+
ErrorCode,
3435
} from '../../../types';
3536
import {
3637
numberToBigNumber,
@@ -422,8 +423,12 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
422423
}
423424

424425
public unpause = async (params: TxParams) => {
425-
assert.assert(await this.paused(), 'Controller not currently paused');
426-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'Sender is not owner');
426+
assert.assert(await this.paused(), ErrorCode.PreconditionRequired, 'Controller not currently paused');
427+
assert.assert(
428+
await this.isCallerTheSecurityTokenOwner(params.txData),
429+
ErrorCode.Unauthorized,
430+
'Sender is not owner',
431+
);
427432
return (await this.contract).unpause.sendTransactionAsync(params.txData, params.safetyFactor);
428433
};
429434

@@ -432,8 +437,12 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
432437
};
433438

434439
public pause = async (params: TxParams) => {
435-
assert.assert(!(await this.paused()), 'Controller currently paused');
436-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'Sender is not owner');
440+
assert.assert(!(await this.paused()), ErrorCode.ContractPaused, 'Controller currently paused');
441+
assert.assert(
442+
await this.isCallerTheSecurityTokenOwner(params.txData),
443+
ErrorCode.Unauthorized,
444+
'Sender is not owner',
445+
);
437446
return (await this.contract).pause.sendTransactionAsync(params.txData, params.safetyFactor);
438447
};
439448

@@ -471,7 +480,11 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
471480
* Used to change the treasury wallet address
472481
*/
473482
public changeTreasuryWallet = async (params: ChangeTreasuryWalletParams) => {
474-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
483+
assert.assert(
484+
await this.isCallerTheSecurityTokenOwner(params.txData),
485+
ErrorCode.Unauthorized,
486+
'The caller must be the ST owner',
487+
);
475488
return (await this.contract).changeTreasuryWallet.sendTransactionAsync(
476489
params.newTreasuryWallet,
477490
params.txData,
@@ -483,17 +496,25 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
483496
* Used to deposit tokens from treasury wallet to the vesting escrow wallet
484497
*/
485498
public depositTokens = async (params: DepositTokensParams) => {
486-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
499+
assert.assert(
500+
await this.isCallerAllowed(params.txData, Perm.Admin),
501+
ErrorCode.Unauthorized,
502+
'Caller is not allowed',
503+
);
487504

488-
assert.assert(params.numberOfTokens.toNumber() > 0, 'Number of tokens should be > 0');
505+
assert.assert(params.numberOfTokens.toNumber() > 0, ErrorCode.InvalidData, 'Number of tokens should be > 0');
489506
const decimals = await (await this.securityTokenContract()).decimals.callAsync();
490507
const canTransferFromResult = await (await this.securityTokenContract()).canTransferFrom.callAsync(
491508
await this.getCallerAddress(params.txData),
492509
await this.address(),
493510
valueToWei(params.numberOfTokens, decimals),
494511
'0x00',
495512
);
496-
assert.assert(canTransferFromResult[0] === TransferStatusCode.TransferSuccess, 'Failed transferFrom');
513+
assert.assert(
514+
canTransferFromResult[0] === TransferStatusCode.TransferSuccess,
515+
ErrorCode.InvalidTransfer,
516+
'Failed transferFrom',
517+
);
497518

498519
return (await this.contract).depositTokens.sendTransactionAsync(
499520
valueToWei(params.numberOfTokens, decimals),
@@ -506,13 +527,18 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
506527
* Sends unassigned tokens to the treasury wallet
507528
*/
508529
public sendToTreasury = async (params: SendToTreasuryParams) => {
509-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Operator), 'Caller is not allowed');
510-
assert.assert(params.amount.toNumber() > 0, 'Amount cannot be zero');
530+
assert.assert(
531+
await this.isCallerAllowed(params.txData, Perm.Operator),
532+
ErrorCode.Unauthorized,
533+
'Caller is not allowed',
534+
);
535+
assert.assert(params.amount.toNumber() > 0, ErrorCode.InvalidData, 'Amount cannot be zero');
511536

512537
const unassignedTokens = await this.unassignedTokens();
513538
const decimals = await (await this.securityTokenContract()).decimals.callAsync();
514539
assert.assert(
515540
params.amount.isLessThanOrEqualTo(valueToWei(unassignedTokens, decimals)),
541+
ErrorCode.InvalidData,
516542
'Amount is greater than unassigned tokens',
517543
);
518544

@@ -521,7 +547,11 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
521547
valueToWei(params.amount, decimals),
522548
'0x00',
523549
);
524-
assert.assert(canTransferResult[0] === TransferStatusCode.TransferSuccess, 'Transfer failed');
550+
assert.assert(
551+
canTransferResult[0] === TransferStatusCode.TransferSuccess,
552+
ErrorCode.InvalidTransfer,
553+
'Transfer failed',
554+
);
525555

526556
return (await this.contract).sendToTreasury.sendTransactionAsync(
527557
valueToWei(params.amount, decimals),
@@ -541,7 +571,11 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
541571
* Pushes available tokens to the beneficiary's address
542572
*/
543573
public pushAvailableTokens = async (params: PushAvailableTokensParams) => {
544-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Operator), 'Caller is not allowed');
574+
assert.assert(
575+
await this.isCallerAllowed(params.txData, Perm.Operator),
576+
ErrorCode.Unauthorized,
577+
'Caller is not allowed',
578+
);
545579

546580
return (await this.contract).pushAvailableTokens.sendTransactionAsync(
547581
params.beneficiary,
@@ -554,17 +588,25 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
554588
* Used to withdraw available tokens by beneficiary
555589
*/
556590
public pullAvailableTokens = async (params: TxParams) => {
557-
assert.assert(!(await this.paused()), 'Contract currently paused');
591+
assert.assert(!(await this.paused()), ErrorCode.ContractPaused, 'Contract currently paused');
558592
return (await this.contract).pullAvailableTokens.sendTransactionAsync(params.txData, params.safetyFactor);
559593
};
560594

561595
/**
562596
* Adds template that can be used for creating schedule
563597
*/
564598
public addTemplate = async (params: AddTemplateParams) => {
565-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
566-
assert.assert(params.name !== '', 'Invalid name');
567-
assert.assert(!(await this.getAllTemplateNames()).includes(params.name), 'Template name already exists');
599+
assert.assert(
600+
await this.isCallerAllowed(params.txData, Perm.Admin),
601+
ErrorCode.Unauthorized,
602+
'Caller is not allowed',
603+
);
604+
assert.assert(params.name !== '', ErrorCode.InvalidData, 'Invalid name');
605+
assert.assert(
606+
!(await this.getAllTemplateNames()).includes(params.name),
607+
ErrorCode.AlreadyExists,
608+
'Template name already exists',
609+
);
568610
await this.validateTemplate(params.numberOfTokens, params.duration, params.frequency);
569611
const decimals = await (await this.securityTokenContract()).decimals.callAsync();
570612
return (await this.contract).addTemplate.sendTransactionAsync(
@@ -581,9 +623,13 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
581623
* Removes template with a given name
582624
*/
583625
public removeTemplate = async (params: RemoveTemplateParams) => {
584-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
585-
assert.assert(params.name !== '', 'Invalid name');
586-
assert.assert((await this.getAllTemplateNames()).includes(params.name), 'Template not found');
626+
assert.assert(
627+
await this.isCallerAllowed(params.txData, Perm.Admin),
628+
ErrorCode.Unauthorized,
629+
'Caller is not allowed',
630+
);
631+
assert.assert(params.name !== '', ErrorCode.InvalidData, 'Invalid name');
632+
assert.assert((await this.getAllTemplateNames()).includes(params.name), ErrorCode.NotFound, 'Template not found');
587633
// TODO 3.1: require(templateToUsers[_name].length == 0, "Template is used");
588634
return (await this.contract).removeTemplate.sendTransactionAsync(
589635
stringToBytes32(params.name),
@@ -614,9 +660,17 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
614660
* Adds a vesting schedule for the beneficiary address
615661
*/
616662
public addSchedule = async (params: AddScheduleParams) => {
617-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
618-
assert.assert(params.templateName !== '', 'Invalid name');
619-
assert.assert(!(await this.getAllTemplateNames()).includes(params.templateName), 'Template name already exists');
663+
assert.assert(
664+
await this.isCallerAllowed(params.txData, Perm.Admin),
665+
ErrorCode.Unauthorized,
666+
'Caller is not allowed',
667+
);
668+
assert.assert(params.templateName !== '', ErrorCode.InvalidData, 'Invalid name');
669+
assert.assert(
670+
!(await this.getAllTemplateNames()).includes(params.templateName),
671+
ErrorCode.AlreadyExists,
672+
'Template name already exists',
673+
);
620674
await this.validateTemplate(params.numberOfTokens, params.duration, params.frequency);
621675
await this.validateAddSchedule(params.beneficiary, params.templateName, params.startTime);
622676
const decimals = await (await this.securityTokenContract()).decimals.callAsync();
@@ -636,7 +690,11 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
636690
* Adds a vesting schedule for the beneficiary address from a template
637691
*/
638692
public addScheduleFromTemplate = async (params: AddScheduleFromTemplateParams) => {
639-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
693+
assert.assert(
694+
await this.isCallerAllowed(params.txData, Perm.Admin),
695+
ErrorCode.Unauthorized,
696+
'Caller is not allowed',
697+
);
640698
await this.validateAddScheduleFromTemplate(params.beneficiary, params.templateName, params.startTime);
641699
return (await this.contract).addScheduleFromTemplate.sendTransactionAsync(
642700
params.beneficiary,
@@ -651,9 +709,13 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
651709
* Modifies a vesting schedule for a beneficiary address
652710
*/
653711
public modifySchedule = async (params: ModifyScheduleParams) => {
654-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
712+
assert.assert(
713+
await this.isCallerAllowed(params.txData, Perm.Admin),
714+
ErrorCode.Unauthorized,
715+
'Caller is not allowed',
716+
);
655717
this.checkSchedule(params.beneficiary);
656-
assert.assert(params.startTime.getTime() > Date.now(), 'Date in the past');
718+
assert.assert(params.startTime.getTime() > Date.now(), ErrorCode.TooEarly, 'Date in the past');
657719
// TODO: require(now < schedule.startTime, "Schedule started");
658720
return (await this.contract).modifySchedule.sendTransactionAsync(
659721
params.beneficiary,
@@ -668,7 +730,11 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
668730
* Revokes a vesting schedule with a given template name for a given beneficiary
669731
*/
670732
public revokeSchedule = async (params: RevokeScheduleParams) => {
671-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
733+
assert.assert(
734+
await this.isCallerAllowed(params.txData, Perm.Admin),
735+
ErrorCode.Unauthorized,
736+
'Caller is not allowed',
737+
);
672738
this.checkSchedule(params.beneficiary);
673739
// TODO: _sendTokensPerSchedule assert
674740
return (await this.contract).revokeSchedule.sendTransactionAsync(
@@ -683,7 +749,11 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
683749
* Revokes all vesting schedules for a given beneficiary address
684750
*/
685751
public revokeAllSchedules = async (params: RevokeAllSchedulesParams) => {
686-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
752+
assert.assert(
753+
await this.isCallerAllowed(params.txData, Perm.Admin),
754+
ErrorCode.Unauthorized,
755+
'Caller is not allowed',
756+
);
687757
assert.isNonZeroETHAddressHex('beneficiary', params.beneficiary);
688758
return (await this.contract).revokeAllSchedules.sendTransactionAsync(
689759
params.beneficiary,
@@ -756,7 +826,11 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
756826
* Used to bulk add vesting schedules for each of the beneficiaries
757827
*/
758828
public pushAvailableTokensMulti = async (params: PushAvailableTokensMultiParams) => {
759-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Operator), 'Caller is not allowed');
829+
assert.assert(
830+
await this.isCallerAllowed(params.txData, Perm.Operator),
831+
ErrorCode.Unauthorized,
832+
'Caller is not allowed',
833+
);
760834
// TODO: require(_toIndex < beneficiaries.length, "Array out of bound");
761835
return (await this.contract).pushAvailableTokensMulti.sendTransactionAsync(
762836
numberToBigNumber(params.fromIndex),
@@ -770,21 +844,26 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
770844
* Used to bulk add vesting schedules for each of beneficiary
771845
*/
772846
public addScheduleMulti = async (params: AddScheduleMultiParams) => {
773-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
847+
assert.assert(
848+
await this.isCallerAllowed(params.txData, Perm.Admin),
849+
ErrorCode.Unauthorized,
850+
'Caller is not allowed',
851+
);
774852
assert.assert(
775853
params.beneficiaries.length === params.templateNames.length &&
776854
params.beneficiaries.length === params.numberOfTokens.length &&
777855
params.beneficiaries.length === params.durations.length &&
778856
params.beneficiaries.length === params.frequencies.length &&
779857
params.beneficiaries.length === params.startTimes.length,
858+
ErrorCode.MismatchedArrayLength,
780859
'Arrays sizes mismatch',
781860
);
782861

783862
const resultGetAllTemplatesNames = [];
784863
const resultValidateTemplate = [];
785864
const resultValidateAddScheduleFromTemplate = [];
786865
for (let i = 0; i < params.beneficiaries.length; i += 1) {
787-
assert.assert(params.templateNames[i] !== '', 'Invalid name');
866+
assert.assert(params.templateNames[i] !== '', ErrorCode.InvalidData, 'Invalid name');
788867
resultGetAllTemplatesNames.push(this.getAllTemplateNames());
789868
resultValidateTemplate.push(
790869
this.validateTemplate(params.numberOfTokens[i], params.durations[i], params.frequencies[i]),
@@ -795,7 +874,11 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
795874
}
796875
const getAllTemplatesNames = await Promise.all(resultGetAllTemplatesNames);
797876
getAllTemplatesNames.forEach((templateName, i) => {
798-
assert.assert(!templateName.includes(params.templateNames[i]), 'Template name already exists');
877+
assert.assert(
878+
!templateName.includes(params.templateNames[i]),
879+
ErrorCode.AlreadyExists,
880+
'Template name already exists',
881+
);
799882
});
800883
await Promise.all(resultValidateTemplate);
801884
await Promise.all(resultValidateAddScheduleFromTemplate);
@@ -826,7 +909,11 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
826909
* Used to bulk add vesting schedules from templates for each of the beneficiary addresses
827910
*/
828911
public addScheduleFromTemplateMulti = async (params: AddScheduleFromTemplateMultiParams) => {
829-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
912+
assert.assert(
913+
await this.isCallerAllowed(params.txData, Perm.Admin),
914+
ErrorCode.Unauthorized,
915+
'Caller is not allowed',
916+
);
830917
const resultsValidateAddScheduleFromTemplate = [];
831918
for (let i = 0; i < params.beneficiaries.length; i += 1) {
832919
resultsValidateAddScheduleFromTemplate.push(
@@ -851,7 +938,11 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
851938
* Used to bulk revoke vesting schedules for each of the beneficiaries
852939
*/
853940
public revokeSchedulesMulti = async (params: RevokeSchedulesMultiParams) => {
854-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
941+
assert.assert(
942+
await this.isCallerAllowed(params.txData, Perm.Admin),
943+
ErrorCode.Unauthorized,
944+
'Caller is not allowed',
945+
);
855946
params.beneficiaries.forEach(beneficiary => {
856947
assert.isNonZeroETHAddressHex('beneficiary', beneficiary);
857948
});
@@ -866,16 +957,21 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
866957
* Used to bulk modify vesting schedules for each of the beneficiaries
867958
*/
868959
public modifyScheduleMulti = async (params: ModifyScheduleMultiParams) => {
869-
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
960+
assert.assert(
961+
await this.isCallerAllowed(params.txData, Perm.Admin),
962+
ErrorCode.Unauthorized,
963+
'Caller is not allowed',
964+
);
870965
assert.assert(
871966
params.beneficiaries.length === params.templateNames.length &&
872967
params.beneficiaries.length === params.startTimes.length,
968+
ErrorCode.MismatchedArrayLength,
873969
'Arrays sizes mismatch',
874970
);
875971

876972
for (let i = 0; i < params.beneficiaries.length; i += 1) {
877973
this.checkSchedule(params.beneficiaries[i]);
878-
assert.assert(params.startTimes[i].getTime() > Date.now(), 'Date in the past');
974+
assert.assert(params.startTimes[i].getTime() > Date.now(), ErrorCode.TooEarly, 'Date in the past');
879975
}
880976

881977
return (await this.contract).modifyScheduleMulti.sendTransactionAsync(
@@ -892,26 +988,26 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
892988
};
893989

894990
private validateTemplate = async (numberOfTokens: BigNumber, duration: number, frequency: number) => {
895-
assert.assert(numberOfTokens.toNumber() > 0, 'Zero amount');
896-
assert.assert(duration % frequency === 0, 'Invalid frequency');
991+
assert.assert(numberOfTokens.toNumber() > 0, ErrorCode.InvalidData, 'Zero amount');
992+
assert.assert(duration % frequency === 0, ErrorCode.InvalidData, 'Invalid frequency');
897993
const periodCount = duration / frequency;
898-
assert.assert(numberOfTokens.toNumber() % periodCount === 0, 'Invalid period count');
994+
assert.assert(numberOfTokens.toNumber() % periodCount === 0, ErrorCode.InvalidData, 'Invalid period count');
899995
const amountPerPeriod = numberOfTokens.toNumber() / periodCount;
900996
const granularity = await (await this.securityTokenContract()).granularity.callAsync();
901-
assert.assert(amountPerPeriod % granularity.toNumber() === 0, 'Invalid granularity');
997+
assert.assert(amountPerPeriod % granularity.toNumber() === 0, ErrorCode.InvalidData, 'Invalid granularity');
902998
};
903999

9041000
private validateAddSchedule = async (beneficiary: string, templateName: string, startTime: Date) => {
9051001
assert.isNonZeroETHAddressHex('beneficiary', beneficiary);
906-
assert.assert((await this.getScheduleCount({ beneficiary })) === 0, 'Already added');
907-
assert.assert(startTime.getTime() > Date.now(), 'Date in the past');
1002+
assert.assert((await this.getScheduleCount({ beneficiary })) === 0, ErrorCode.AlreadyExists, 'Already added');
1003+
assert.assert(startTime.getTime() > Date.now(), ErrorCode.TooEarly, 'Date in the past');
9081004
};
9091005

9101006
private validateAddScheduleFromTemplate = async (beneficiary: string, templateName: string, startTime: Date) => {
9111007
assert.isNonZeroETHAddressHex('beneficiary', beneficiary);
912-
assert.assert((await this.getAllTemplateNames()).includes(templateName), 'Template not found');
913-
assert.assert((await this.getScheduleCount({ beneficiary })) === 0, 'Already added');
914-
assert.assert(startTime.getTime() > Date.now(), 'Date in the past');
1008+
assert.assert((await this.getAllTemplateNames()).includes(templateName), ErrorCode.NotFound, 'Template not found');
1009+
assert.assert((await this.getScheduleCount({ beneficiary })) === 0, ErrorCode.AlreadyExists, 'Already added');
1010+
assert.assert(startTime.getTime() > Date.now(), ErrorCode.TooEarly, 'Date in the past');
9151011
};
9161012

9171013
private checkSchedule = (beneficiary: string) => {

0 commit comments

Comments
 (0)