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

Commit 29b825b

Browse files
committed
fix: 🐛 ErrorCode implementation in poly token and ST wrapper
1 parent 9ffbcf1 commit 29b825b

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

src/contract_wrappers/tokens/poly_token_faucet_wrapper.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PolyTokenFaucetContract, Web3Wrapper, BigNumber } from '@polymathnetwork/abi-wrappers';
22
import ContractWrapper from '../contract_wrapper';
3-
import { TxParams } from '../../types';
3+
import { TxParams, ErrorCode } from '../../types';
44
import assert from '../../utils/assert';
55
import { valueToWei } from '../../utils/convert';
66

@@ -29,7 +29,11 @@ export default class PolyTokenFaucetWrapper extends ContractWrapper {
2929

3030
public getTokens = async (params: GetTokensParams) => {
3131
assert.isNonZeroETHAddressHex('recipient', params.recipient);
32-
assert.assert(params.amount.isLessThanOrEqualTo(MAX_TOKEN_AMOUNT), 'Amount cannot exceed 1 million tokens');
32+
assert.assert(
33+
params.amount.isLessThanOrEqualTo(MAX_TOKEN_AMOUNT),
34+
ErrorCode.InvalidData,
35+
'Amount cannot exceed 1 million tokens',
36+
);
3337

3438
return (await this.contract).getTokens.sendTransactionAsync(
3539
valueToWei(params.amount, await (await this.contract).decimals.callAsync()),

src/contract_wrappers/tokens/security_token_wrapper.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
TxParams,
7474
CappedSTOFundRaiseType,
7575
TransferStatusCode,
76+
ErrorCode
7677
} from '../../types';
7778
import {
7879
bigNumberToDate,
@@ -1120,7 +1121,7 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
11201121

11211122
public changeName = async (params: ChangeNameParams) => {
11221123
await this.checkOnlyOwner(params.txData);
1123-
assert.assert(params.name.length > 0, 'Name required');
1124+
assert.assert(params.name.length > 0, ErrorCode.InvalidData, 'Name required');
11241125
return (await this.contract).changeName.sendTransactionAsync(params.name, params.txData, params.safetyFactor);
11251126
};
11261127

@@ -1177,13 +1178,13 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
11771178

11781179
public freezeTransfers = async (params: TxParams) => {
11791180
await this.checkOnlyOwner(params.txData);
1180-
assert.assert(!(await this.transfersFrozen()), 'Transfers already frozen');
1181+
assert.assert(!(await this.transfersFrozen()), ErrorCode.PreconditionRequired, 'Transfers already frozen');
11811182
return (await this.contract).freezeTransfers.sendTransactionAsync(params.txData, params.safetyFactor);
11821183
};
11831184

11841185
public unfreezeTransfers = async (params: TxParams) => {
11851186
await this.checkOnlyOwner(params.txData);
1186-
assert.assert(await this.transfersFrozen(), 'Transfers are not frozen');
1187+
assert.assert(await this.transfersFrozen(), ErrorCode.PreconditionRequired, 'Transfers are not frozen');
11871188
return (await this.contract).unfreezeTransfers.sendTransactionAsync(params.txData, params.safetyFactor);
11881189
};
11891190

@@ -1212,12 +1213,13 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
12121213
};
12131214

12141215
public freezeIssuance = async (params: FreezeIssuanceParams) => {
1215-
assert.assert(await this.isIssuable(), 'Issuance frozen');
1216+
assert.assert(await this.isIssuable(), ErrorCode.PreconditionRequired, 'Issuance frozen');
12161217
assert.assert(
12171218
functionsUtils.checksumAddressComparision(
12181219
await this.owner(),
12191220
(await this.web3Wrapper.getAvailableAddressesAsync())[0],
12201221
),
1222+
ErrorCode.Unauthorized,
12211223
'Msg sender must be owner',
12221224
);
12231225
return (await this.contract).freezeIssuance.sendTransactionAsync(
@@ -1230,14 +1232,15 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
12301232
public issue = async (params: IssueParams) => {
12311233
assert.isNonZeroETHAddressHex('investor', params.investor);
12321234
await this.checkOnlyOwner(params.txData);
1233-
assert.assert(await this.isIssuable(), 'Issuance frozen');
1235+
assert.assert(await this.isIssuable(), ErrorCode.PreconditionRequired, 'Issuance frozen');
12341236
const canTransfer = await this.canTransfer({
12351237
to: params.investor,
12361238
value: params.value,
12371239
data: params.data || '0x00',
12381240
});
12391241
assert.assert(
12401242
canTransfer.statusCode !== TransferStatusCode.TransferFailure,
1243+
ErrorCode.InvalidTransfer,
12411244
`Transfer Status: ${canTransfer.statusCode}`,
12421245
);
12431246
return (await this.contract).issue.sendTransactionAsync(
@@ -1252,7 +1255,7 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
12521255
public issueByPartition = async (params: IssueByPartitionParams) => {
12531256
assert.isNonZeroETHAddressHex('investor', params.investor);
12541257
await this.checkOnlyOwner(params.txData);
1255-
assert.assert(await this.isIssuable(), 'Issuance frozen');
1258+
assert.assert(await this.isIssuable(), ErrorCode.PreconditionRequired, 'Issuance frozen');
12561259
assert.isValidPartition(params.partition);
12571260
return (await this.contract).issueByPartition.sendTransactionAsync(
12581261
params.partition,
@@ -1268,9 +1271,10 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
12681271
params.investors.forEach(address => assert.isNonZeroETHAddressHex('investors', address));
12691272
assert.assert(
12701273
params.investors.length === params.values.length,
1274+
ErrorCode.MismatchedLength,
12711275
'Number of investors passed in must be equivalent to number of values',
12721276
);
1273-
assert.assert(await this.isIssuable(), 'Issuance frozen');
1277+
assert.assert(await this.isIssuable(), ErrorCode.PreconditionRequired, 'Issuance frozen');
12741278
await this.checkOnlyOwner(params.txData);
12751279
return (await this.contract).issueMulti.sendTransactionAsync(
12761280
params.investors,
@@ -1315,7 +1319,7 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
13151319
public operatorRedeemByPartition = async (params: OperatorRedeemByPartitionParams) => {
13161320
await this.checkBalanceFromGreaterThanValue((await this.web3Wrapper.getAvailableAddressesAsync())[0], params.value);
13171321
assert.isNonZeroETHAddressHex('TokenHolder', params.tokenHolder);
1318-
assert.assert(params.operatorData.length > 0, 'Operator data cannot be 0');
1322+
assert.assert(params.operatorData.length > 0, ErrorCode.InvalidData, 'Operator data cannot be 0');
13191323
assert.isValidPartition(params.partition);
13201324
return (await this.contract).operatorRedeemByPartition.sendTransactionAsync(
13211325
params.partition,
@@ -1335,6 +1339,7 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
13351339
owner: params.from,
13361340
spender: (await this.web3Wrapper.getAvailableAddressesAsync())[0],
13371341
})).isGreaterThanOrEqualTo(params.value),
1342+
ErrorCode.InsufficientAllowance,
13381343
'Insufficient allowance for inputted burn value',
13391344
);
13401345
assert.isETHAddressHex('from', params.from);
@@ -1351,6 +1356,7 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
13511356
await this.checkOnlyOwner(params.txData);
13521357
assert.assert(
13531358
(await this.currentCheckpointId()).isLessThan(MAX_CHECKPOINT_NUMBER),
1359+
ErrorCode.PreconditionRequired,
13541360
'Reached maximum checkpoint number',
13551361
);
13561362
return (await this.contract).createCheckpoint.sendTransactionAsync(params.txData, params.safetyFactor);
@@ -1365,6 +1371,7 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
13651371
public totalSupplyAt = async (params: CheckpointIdParams) => {
13661372
assert.assert(
13671373
(await this.currentCheckpointId()).isGreaterThanOrEqualTo(params.checkpointId),
1374+
ErrorCode.InvalidData,
13681375
'Checkpoint id must be less than or equal to currentCheckpoint',
13691376
);
13701377
return weiToValue(
@@ -1377,6 +1384,7 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
13771384
assert.isETHAddressHex('investor', params.investor);
13781385
assert.assert(
13791386
(await this.currentCheckpointId()).isGreaterThanOrEqualTo(params.checkpointId),
1387+
ErrorCode.InvalidData,
13801388
'Checkpoint id must be less than or equal to currentCheckpoint',
13811389
);
13821390
return weiToValue(
@@ -1449,7 +1457,7 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
14491457
public operatorTransferByPartition = async (params: OperatorTransferByPartitionParams) => {
14501458
assert.isETHAddressHex('To', params.to);
14511459
assert.isETHAddressHex('From', params.from);
1452-
assert.assert(params.operatorData.length > 0, 'Operator data cannot be 0');
1460+
assert.assert(params.operatorData.length > 0, ErrorCode.InvalidData, 'Operator data cannot be 0');
14531461
assert.isValidPartition(params.partition);
14541462
return (await this.contract).operatorTransferByPartition.sendTransactionAsync(
14551463
stringToBytes32(params.partition),
@@ -1701,8 +1709,8 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
17011709
};
17021710

17031711
public setDocument = async (params: SetDocumentParams) => {
1704-
assert.assert(params.name.length > 0, 'Bad name, cannot be empty');
1705-
assert.assert(params.uri.length > 0, 'Bad uri, cannot be empty');
1712+
assert.assert(params.name.length > 0, ErrorCode.InvalidData, 'Bad name, cannot be empty');
1713+
assert.assert(params.uri.length > 0, ErrorCode.InvalidData, 'Bad uri, cannot be empty');
17061714
await this.checkOnlyOwner(params.txData);
17071715
return (await this.contract).setDocument.sendTransactionAsync(
17081716
stringToBytes32(params.name),
@@ -1716,7 +1724,7 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
17161724
public removeDocument = async (params: DocumentParams) => {
17171725
await this.checkOnlyOwner(params.txData);
17181726
const document = await this.getDocument({ name: params.name });
1719-
assert.assert(document.documentUri.length !== 0, 'Document does not exist');
1727+
assert.assert(document.documentUri.length !== 0, ErrorCode.NotFound, 'Document does not exist');
17201728
return (await this.contract).removeDocument.sendTransactionAsync(
17211729
stringToBytes32(params.name),
17221730
params.txData,
@@ -1781,15 +1789,15 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
17811789
};
17821790

17831791
private checkModuleExists = async (moduleAddress: string) => {
1784-
assert.assert((await this.getModule({ moduleAddress })).name !== '', 'Module does not exist');
1792+
assert.assert((await this.getModule({ moduleAddress })).name !== '', ErrorCode.NotFound, 'Module does not exist');
17851793
};
17861794

17871795
private checkIsArchived = async (moduleAddress: string) => {
1788-
assert.assert((await this.getModule({ moduleAddress })).archived, 'Module is not yet archived');
1796+
assert.assert((await this.getModule({ moduleAddress })).archived, ErrorCode.PreconditionRequired, 'Module is not yet archived');
17891797
};
17901798

17911799
private checkIsNotArchived = async (moduleAddress: string) => {
1792-
assert.assert(!(await this.getModule({ moduleAddress })).archived, 'Module is archived');
1800+
assert.assert(!(await this.getModule({ moduleAddress })).archived, ErrorCode.PreconditionRequired, 'Module is archived');
17931801
};
17941802

17951803
private checkModuleStructAddressIsNotZero = async (moduleAddress: string) => {
@@ -1802,17 +1810,19 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
18021810
(await this.getModule({ moduleAddress })).address,
18031811
'0x0000000000000000000000000000000000000000',
18041812
),
1813+
ErrorCode.AlreadyExists,
18051814
'Module already exists at that address',
18061815
);
18071816
};
18081817

18091818
private checkIsControllable = async () => {
1810-
assert.assert(await this.isControllable(), 'Controller currently disabled');
1819+
assert.assert(await this.isControllable(), ErrorCode.PreconditionRequired, 'Controller currently disabled');
18111820
};
18121821

18131822
private checkBalanceFromGreaterThanValue = async (from: string, value: BigNumber) => {
18141823
assert.assert(
18151824
(await this.balanceOf({ owner: from })).isGreaterThanOrEqualTo(value),
1825+
ErrorCode.InsufficientBalance,
18161826
'Insufficient balance for inputted value',
18171827
);
18181828
};
@@ -1821,25 +1831,29 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
18211831
const moduleCost = await (await this.moduleFactoryContract(moduleFactory)).setupCostInPoly.callAsync();
18221832
assert.assert(
18231833
maxCost.isGreaterThanOrEqualTo(moduleCost),
1834+
ErrorCode.InsufficientBalance,
18241835
'Insufficient max cost to cover module factory setup cost',
18251836
);
18261837
const polyTokenBalance = await (await this.polyTokenContract()).balanceOf.callAsync(await this.address());
18271838
assert.assert(
18281839
polyTokenBalance.isGreaterThanOrEqualTo(moduleCost),
1840+
ErrorCode.InsufficientBalance,
18291841
'Insufficient poly token balance for module cost',
18301842
);
18311843
};
18321844

18331845
private checkOnlyOwner = async (txData: Partial<TxData> | undefined) => {
18341846
assert.assert(
18351847
functionsUtils.checksumAddressComparision(await this.owner(), await this.getCallerAddress(txData)),
1848+
ErrorCode.Unauthorized,
18361849
'Msg sender must be owner',
18371850
);
18381851
};
18391852

18401853
private checkMsgSenderIsController = async (txData: Partial<TxData> | undefined) => {
18411854
assert.assert(
18421855
(await this.isControllable()) && (await this.controller()) === (await this.getCallerAddress(txData)),
1856+
ErrorCode.Unauthorized,
18431857
'Msg sender must be controller',
18441858
);
18451859
};
@@ -1849,12 +1863,13 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
18491863
const isOwner = (await (await this.moduleFactoryContract(address)).owner.callAsync()) === (await this.owner());
18501864
assert.assert(
18511865
(await this.checkForRegisteredModule(address)) || isOwner,
1866+
ErrorCode.Unauthorized,
18521867
'ModuleFactory must be verified or SecurityToken owner must be ModuleFactory owner',
18531868
);
18541869
} else {
1855-
assert.assert(await this.checkForRegisteredModule(address), 'ModuleFactory must be verified');
1870+
assert.assert(await this.checkForRegisteredModule(address), ErrorCode.Unauthorized, 'ModuleFactory must be verified');
18561871
}
1857-
assert.assert(await this.isCompatibleModule(address), 'Version should within the compatible range of ST');
1872+
assert.assert(await this.isCompatibleModule(address), ErrorCode.InvalidVersion, 'Version should within the compatible range of ST');
18581873
};
18591874

18601875
private checkForRegisteredModule = async (moduleAddress: string) => {
@@ -1899,14 +1914,14 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
18991914
assert.isBigNumberGreaterThanZero(data.rate, 'Rate of token should be greater than 0');
19001915
assert.isNonZeroETHAddressHex('Funds Receiver', data.fundsReceiver);
19011916
assert.isFutureDate(data.startTime, 'Start time date not valid');
1902-
assert.assert(data.endTime > data.startTime, 'End time not valid');
1917+
assert.assert(data.endTime > data.startTime, ErrorCode.TooEarly, 'End time not valid');
19031918
assert.isBigNumberGreaterThanZero(data.cap, 'Cap should be greater than 0');
19041919
};
19051920

19061921
private usdTieredSTOAssertions = async (data: USDTieredSTOData) => {
19071922
assert.isFutureDate(data.startTime, 'Start time date not valid');
1908-
assert.assert(data.endTime > data.startTime, 'End time not valid');
1909-
assert.assert(data.tokensPerTierTotal.length > 0, 'No tiers provided');
1923+
assert.assert(data.endTime > data.startTime, ErrorCode.TooEarly, 'End time not valid');
1924+
assert.assert(data.tokensPerTierTotal.length > 0, ErrorCode.InvalidData, 'No tiers provided');
19101925
assert.areValidArrayLengths(
19111926
[data.ratePerTier, data.tokensPerTierTotal, data.ratePerTierDiscountPoly, data.tokensPerTierDiscountPoly],
19121927
'Tier data length mismatch',
@@ -1916,11 +1931,12 @@ export default class SecurityTokenWrapper extends ERC20TokenWrapper {
19161931
assert.isBigNumberGreaterThanZero(data.tokensPerTierTotal[i], 'Invalid token amount');
19171932
assert.assert(
19181933
data.tokensPerTierDiscountPoly[i].isLessThanOrEqualTo(data.tokensPerTierTotal[i]),
1934+
ErrorCode.InvalidData,
19191935
'Too many discounted tokens',
19201936
);
1921-
assert.assert(data.ratePerTierDiscountPoly[i].isLessThanOrEqualTo(data.ratePerTier[i]), 'Invalid discount');
1937+
assert.assert(data.ratePerTierDiscountPoly[i].isLessThanOrEqualTo(data.ratePerTier[i]), ErrorCode.InvalidData, 'Invalid discount');
19221938
}
1923-
assert.assert(data.fundRaiseTypes.length > 0 && data.fundRaiseTypes.length <= 3, 'Raise type is not specified');
1939+
assert.assert(data.fundRaiseTypes.length > 0 && data.fundRaiseTypes.length <= 3, ErrorCode.InvalidData, 'Raise type is not specified');
19241940
assert.isNonZeroETHAddressHex('Wallet', data.wallet);
19251941
assert.isNonZeroETHAddressHex('ReserveWallet', data.treasuryWallet);
19261942
};

0 commit comments

Comments
 (0)