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

Commit 9ffbcf1

Browse files
committed
fix: 🐛 ErrorClass implementation for STR wrapper
1 parent d3239a9 commit 9ffbcf1

File tree

2 files changed

+89
-36
lines changed

2 files changed

+89
-36
lines changed

src/contract_wrappers/registries/security_token_registry_wrapper.ts

Lines changed: 88 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
GetLogs,
3737
FULL_DECIMALS,
3838
FeeType,
39+
ErrorCode,
3940
} from '../../types';
4041
import {
4142
bigNumberToDate,
@@ -560,7 +561,7 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
560561
*/
561562
public changeFeesAmountAndCurrency = async (params: ChangeFeesAmountAndCurrencyParams) => {
562563
const isOldFeesInPoly = await this.getIsFeeInPoly();
563-
assert.assert(isOldFeesInPoly !== params.isFeeInPoly, 'Currency unchanged');
564+
assert.assert(isOldFeesInPoly !== params.isFeeInPoly, ErrorCode.PreconditionRequired, 'Currency unchanged');
564565
return (await this.contract).changeFeesAmountAndCurrency.sendTransactionAsync(
565566
valueToWei(params.tickerRegFee, FULL_DECIMALS),
566567
valueToWei(params.stLaunchFee, FULL_DECIMALS),
@@ -576,17 +577,17 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
576577
*/
577578
public refreshSecurityToken = async (params: RefreshSecurityTokenParams) => {
578579
await this.checkWhenNotPausedOrOwner();
579-
assert.assert(params.name.length > 0, 'Name cannot be an empty string');
580-
assert.assert(params.ticker.length > 0, 'Ticker cannot be an empty string');
580+
assert.assert(params.name.length > 0, ErrorCode.InvalidData, 'Name cannot be an empty string');
581+
assert.assert(params.ticker.length > 0, ErrorCode.InvalidData, 'Ticker cannot be an empty string');
581582
assert.isNonZeroETHAddressHex('treasuryWallet', params.treasuryWallet);
582583
const tickerDetails = await this.getTickerDetails({
583584
ticker: params.ticker,
584585
});
585-
assert.assert(tickerDetails.status, 'Not deployed');
586+
assert.assert(tickerDetails.status, ErrorCode.NotFound, 'Not deployed');
586587
const isFrozen = await (await this.securityTokenContract(
587588
await this.getSecurityTokenAddress({ ticker: params.ticker }),
588589
)).transfersFrozen.callAsync();
589-
assert.assert(isFrozen, 'Transfers not frozen');
590+
assert.assert(isFrozen, ErrorCode.PreconditionRequired, 'Transfers not frozen');
590591
return (await this.contract).refreshSecurityToken.sendTransactionAsync(
591592
params.name,
592593
params.ticker,
@@ -610,7 +611,11 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
610611
const minor = new BigNumber(splitVersion[1]);
611612
const patch = new BigNumber(splitVersion[2]);
612613
const VERSION = [major, minor, patch];
613-
assert.assert(JSON.stringify(LATEST_VERSION) === JSON.stringify(VERSION), 'Cannot remove latestVersion');
614+
assert.assert(
615+
JSON.stringify(LATEST_VERSION) === JSON.stringify(VERSION),
616+
ErrorCode.PreconditionRequired,
617+
'Cannot remove latestVersion',
618+
);
614619
return (await this.contract).removeProtocolFactory.sendTransactionAsync(
615620
major,
616621
minor,
@@ -651,17 +656,25 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
651656
};
652657

653658
public generateNewSecurityToken = async (params: NewSecurityTokenParams) => {
654-
assert.assert(params.ticker.length > 0, 'Ticker cannot be an empty string');
655-
assert.assert(params.name.length > 0, 'Name cannot be an empty string');
659+
assert.assert(params.ticker.length > 0, ErrorCode.InvalidData, 'Ticker cannot be an empty string');
660+
assert.assert(params.name.length > 0, ErrorCode.InvalidData, 'Name cannot be an empty string');
656661
assert.isNonZeroETHAddressHex('treasuryWallet', params.treasuryWallet);
657662
await this.checkWhenNotPausedOrOwner();
658663
const tickerDetails = await this.getTickerDetails({
659664
ticker: params.ticker,
660665
});
661-
assert.assert(!tickerDetails.status, 'Ticker already deployed');
666+
assert.assert(!tickerDetails.status, ErrorCode.AlreadyExists, 'Ticker already deployed');
662667
const address = (await this.web3Wrapper.getAvailableAddressesAsync())[0];
663-
assert.assert(functionsUtils.checksumAddressComparision(address, tickerDetails.owner), 'Not authorised');
664-
assert.assert(tickerDetails.expiryDate.getTime() >= Date.now(), 'Ticker reservation is expired');
668+
assert.assert(
669+
functionsUtils.checksumAddressComparision(address, tickerDetails.owner),
670+
ErrorCode.Unauthorized,
671+
'Not authorised',
672+
);
673+
assert.assert(
674+
tickerDetails.expiryDate.getTime() >= Date.now(),
675+
ErrorCode.TickerExpired,
676+
'Ticker reservation is expired',
677+
);
665678

666679
// Check PolyToken allowance
667680
const securityTokenLaunchFee = await this.getSecurityTokenLaunchFee();
@@ -670,7 +683,11 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
670683
await (await this.polyTokenContract()).balanceOf.callAsync(address),
671684
FULL_DECIMALS,
672685
);
673-
assert.assert(polyBalance.isGreaterThanOrEqualTo(securityTokenLaunchFee), 'Insufficient poly token balance');
686+
assert.assert(
687+
polyBalance.isGreaterThanOrEqualTo(securityTokenLaunchFee),
688+
ErrorCode.InsufficientBalance,
689+
'Insufficient poly token balance',
690+
);
674691
}
675692

676693
const version = params.protocolVersion;
@@ -715,9 +732,13 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
715732
*/
716733
public modifyExistingTicker = async (params: ModifyExistingTickerParams) => {
717734
await this.checkOnlyOwner();
718-
assert.assert(params.ticker.length > 0 && params.ticker.length <= 10, 'Bad ticker');
719-
assert.assert(params.expiryDate.getTime() > new Date(0).getTime(), 'Bad expiry date');
720-
assert.assert(params.registrationDate.getTime() > new Date(0).getTime(), 'Bad registration date');
735+
assert.assert(params.ticker.length > 0 && params.ticker.length <= 10, ErrorCode.InvalidData, 'Bad ticker');
736+
assert.assert(params.expiryDate.getTime() > new Date(0).getTime(), ErrorCode.TooEarly, 'Bad expiry date');
737+
assert.assert(
738+
params.registrationDate.getTime() > new Date(0).getTime(),
739+
ErrorCode.TooEarly,
740+
'Bad registration date',
741+
);
721742
assert.isNonZeroETHAddressHex('owner', params.owner);
722743
if (params.status) {
723744
const address = await this.getSecurityTokenAddress({ ticker: params.ticker });
@@ -792,12 +813,20 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
792813
ticker: params.ticker,
793814
});
794815
const address = await this.getCallerAddress(params.txData);
795-
assert.assert(functionsUtils.checksumAddressComparision(address, tickerDetails.owner), 'Not authorised');
816+
assert.assert(
817+
functionsUtils.checksumAddressComparision(address, tickerDetails.owner),
818+
ErrorCode.Unauthorized,
819+
'Not authorised',
820+
);
796821
if (tickerDetails.status) {
797822
const securityTokenOwner = await (await this.securityTokenContract(
798823
await this.getSecurityTokenAddress({ ticker: params.ticker }),
799824
)).owner.callAsync();
800-
assert.assert(securityTokenOwner === params.newOwner, 'New owner does not match token owner');
825+
assert.assert(
826+
securityTokenOwner === params.newOwner,
827+
ErrorCode.PreconditionRequired,
828+
'New owner does not match token owner',
829+
);
801830
}
802831
return (await this.contract).transferTickerOwnership.sendTransactionAsync(
803832
params.newOwner,
@@ -811,16 +840,20 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
811840
* Deploys an instance of a new Security Token and records it to the registry
812841
*/
813842
public generateSecurityToken = async (params: GenerateSecurityTokenParams) => {
814-
assert.assert(params.ticker.length > 0, 'Ticker cannot be an empty string');
815-
assert.assert(params.name.length > 0, 'Name cannot be an empty string');
843+
assert.assert(params.ticker.length > 0, ErrorCode.InvalidData, 'Ticker cannot be an empty string');
844+
assert.assert(params.name.length > 0, ErrorCode.InvalidData, 'Name cannot be an empty string');
816845
await this.checkWhenNotPausedOrOwner();
817846
const tickerDetails = await this.getTickerDetails({
818847
ticker: params.ticker,
819848
});
820-
assert.assert(!tickerDetails.status, 'Ticker already deployed');
849+
assert.assert(!tickerDetails.status, ErrorCode.AlreadyExists, 'Ticker already deployed');
821850
const address = (await this.web3Wrapper.getAvailableAddressesAsync())[0];
822-
assert.assert(functionsUtils.checksumAddressComparision(address, tickerDetails.owner), 'Not authorised');
823-
assert.assert(tickerDetails.expiryDate.getTime() >= Date.now(), 'Ticker gets expired');
851+
assert.assert(
852+
functionsUtils.checksumAddressComparision(address, tickerDetails.owner),
853+
ErrorCode.Unauthorized,
854+
'Not authorised',
855+
);
856+
assert.assert(tickerDetails.expiryDate.getTime() >= Date.now(), ErrorCode.TooEarly, 'Ticker gets expired');
824857

825858
// Check PolyToken allowance
826859
const securityTokenLaunchFee = await this.getSecurityTokenLaunchFee();
@@ -829,7 +862,11 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
829862
await (await this.polyTokenContract()).balanceOf.callAsync(address),
830863
FULL_DECIMALS,
831864
);
832-
assert.assert(polyBalance.isGreaterThanOrEqualTo(securityTokenLaunchFee), 'Insufficient Poly token allowance');
865+
assert.assert(
866+
polyBalance.isGreaterThanOrEqualTo(securityTokenLaunchFee),
867+
ErrorCode.InsufficientBalance,
868+
'Insufficient Poly token allowance',
869+
);
833870
}
834871

835872
return (await this.contract).generateSecurityToken.sendTransactionAsync(
@@ -891,11 +928,12 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
891928
* Modifies the ticker details. Only Polymath has the ability to do so.
892929
*/
893930
public modifyTicker = async (params: ModifyTickerParams) => {
894-
assert.assert(params.ticker.length > 0, 'Ticker cannot be an empty string');
895-
assert.assert(params.ticker.length <= 10, 'Ticker length can not be greater than 10');
931+
assert.assert(params.ticker.length > 0, ErrorCode.InvalidData, 'Ticker cannot be an empty string');
932+
assert.assert(params.ticker.length <= 10, ErrorCode.InvalidData, 'Ticker length can not be greater than 10');
896933
await this.checkOnlyOwner();
897934
assert.assert(
898935
params.registrationDate.getTime() <= params.expiryDate.getTime(),
936+
ErrorCode.TooLate,
899937
'Registration date should < expiry date',
900938
);
901939
assert.isETHAddressHex('owner', params.owner);
@@ -932,7 +970,7 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
932970
*/
933971
public changeExpiryLimit = async (params: ChangeExpiryLimitParams) => {
934972
await this.checkOnlyOwner();
935-
assert.assert(params.newExpiry.toNumber() >= 86400, 'Expiry should >= 1 day');
973+
assert.assert(params.newExpiry.toNumber() >= 86400, ErrorCode.TooEarly, 'Expiry should >= 1 day');
936974
return (await this.contract).changeExpiryLimit.sendTransactionAsync(
937975
params.newExpiry,
938976
params.txData,
@@ -982,7 +1020,7 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
9821020
* Called by the owner to pause, triggers stopped state
9831021
*/
9841022
public pause = async (params: TxParams) => {
985-
assert.assert(!(await this.isPaused()), 'Contract is paused');
1023+
assert.assert(!(await this.isPaused()), ErrorCode.ContractPaused, 'Contract is paused');
9861024
await this.checkOnlyOwner();
9871025
return (await this.contract).pause.sendTransactionAsync(params.txData, params.safetyFactor);
9881026
};
@@ -991,7 +1029,7 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
9911029
* Called by the owner to unpause, returns to normal state
9921030
*/
9931031
public unpause = async (params: TxParams) => {
994-
assert.assert(await this.isPaused(), 'Contract is already not paused');
1032+
assert.assert(await this.isPaused(), ErrorCode.PreconditionRequired, 'Contract is already not paused');
9951033
await this.checkOnlyOwner();
9961034
return (await this.contract).unpause.sendTransactionAsync(params.txData, params.safetyFactor);
9971035
};
@@ -1002,7 +1040,7 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
10021040
public changeTickerRegistrationFee = async (params: ChangeFeeParams) => {
10031041
await this.checkOnlyOwner();
10041042
const actualFee = await this.getTickerRegistrationFee();
1005-
assert.assert(!actualFee.eq(params.newFee), 'Fee not changed');
1043+
assert.assert(!actualFee.eq(params.newFee), ErrorCode.PreconditionRequired, 'Fee not changed');
10061044
return (await this.contract).changeTickerRegistrationFee.sendTransactionAsync(
10071045
valueToWei(params.newFee, FULL_DECIMALS),
10081046
params.txData,
@@ -1016,7 +1054,7 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
10161054
public changeSecurityLaunchFee = async (params: ChangeFeeParams) => {
10171055
await this.checkOnlyOwner();
10181056
const actualFee = await this.getSecurityTokenLaunchFee();
1019-
assert.assert(!actualFee.eq(params.newFee), 'Fee not changed');
1057+
assert.assert(!actualFee.eq(params.newFee), ErrorCode.PreconditionRequired, 'Fee not changed');
10201058
return (await this.contract).changeSecurityLaunchFee.sendTransactionAsync(
10211059
valueToWei(params.newFee, FULL_DECIMALS),
10221060
params.txData,
@@ -1144,7 +1182,7 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
11441182
break;
11451183
}
11461184
default: {
1147-
assert.assert(false, 'Missing fee type');
1185+
assert.assert(false, ErrorCode.InvalidData, 'Missing fee type');
11481186
break;
11491187
}
11501188
}
@@ -1229,7 +1267,11 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
12291267
(await this.web3Wrapper.getAvailableAddressesAsync())[0],
12301268
)
12311269
) {
1232-
assert.assert(!(await this.isPaused()), 'Msg sender is not owner and the contract is paused');
1270+
assert.assert(
1271+
!(await this.isPaused()),
1272+
ErrorCode.Unauthorized,
1273+
'Msg sender is not owner and the contract is paused',
1274+
);
12331275
}
12341276
};
12351277

@@ -1239,32 +1281,42 @@ export default class SecurityTokenRegistryWrapper extends ContractWrapper {
12391281
await this.owner(),
12401282
(await this.web3Wrapper.getAvailableAddressesAsync())[0],
12411283
),
1284+
ErrorCode.Unauthorized,
12421285
'Msg sender must be owner',
12431286
);
12441287
};
12451288

12461289
private checkRegisterTickerRequirements = async (ticker: string, owner: string) => {
12471290
assert.isETHAddressHex('owner', owner);
1248-
assert.assert(ticker.length > 0 && ticker.length <= 10, 'Bad ticker, must be 1 to 10 characters');
1291+
assert.assert(
1292+
ticker.length > 0 && ticker.length <= 10,
1293+
ErrorCode.InvalidData,
1294+
'Bad ticker, must be 1 to 10 characters',
1295+
);
12491296
assert.assert(
12501297
await this.tickerAvailable({
12511298
ticker,
12521299
}),
1300+
ErrorCode.AlreadyExists,
12531301
'Ticker is not available',
12541302
);
12551303

12561304
// Check poly token allowance
12571305
const tickerRegistrationFee = await this.getTickerRegistrationFee();
12581306
if (tickerRegistrationFee.isGreaterThan(BIG_NUMBER_ZERO)) {
12591307
const polyBalance = weiToValue(await (await this.polyTokenContract()).balanceOf.callAsync(owner), FULL_DECIMALS);
1260-
assert.assert(polyBalance.isGreaterThanOrEqualTo(tickerRegistrationFee), 'Insufficient poly token balance');
1308+
assert.assert(
1309+
polyBalance.isGreaterThanOrEqualTo(tickerRegistrationFee),
1310+
ErrorCode.InsufficientBalance,
1311+
'Insufficient poly token balance',
1312+
);
12611313
}
12621314
};
12631315

12641316
private checkModifyST = async (ticker: string, deployedAt: Date, owner: string, securityToken: string) => {
12651317
await this.checkOnlyOwner();
1266-
assert.assert(ticker.length > 0 && ticker.length <= 10, 'Bad ticker');
1267-
assert.assert(deployedAt.getTime() > new Date(0).getTime(), 'Bad deployed date');
1318+
assert.assert(ticker.length > 0 && ticker.length <= 10, ErrorCode.InvalidData, 'Bad ticker');
1319+
assert.assert(deployedAt.getTime() > new Date(0).getTime(), ErrorCode.TooEarly, 'Bad deployed date');
12681320
assert.isNonZeroETHAddressHex('owner', owner);
12691321
assert.isNonZeroETHAddressHex('securityToken', securityToken);
12701322
assert.isNonZeroETHAddressHex(

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,5 @@ export enum ErrorCode {
365365
AlreadyExists = 'AlreadyExists',
366366
NotFound = 'NotFound',
367367
MismatchedLength = 'MismatchedLength',
368+
TickerExpired = 'TickerExpired',
368369
}

0 commit comments

Comments
 (0)