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

Commit e9b23ec

Browse files
committed
fix: 🐛 implement ErrorCode type for sto and usd tieres sto
1 parent 22f991b commit e9b23ec

File tree

3 files changed

+107
-32
lines changed

3 files changed

+107
-32
lines changed

src/contract_wrappers/modules/sto/sto_wrapper.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TxParams, STOBaseContract, FundRaiseType, FULL_DECIMALS } from '../../../types';
1+
import { TxParams, STOBaseContract, FundRaiseType, FULL_DECIMALS, ErrorCode } from '../../../types';
22
import ModuleWrapper from '../module_wrapper';
33
import assert from '../../../utils/assert';
44
import { weiToValue } from '../../../utils/convert';
@@ -82,14 +82,22 @@ export default abstract class STOWrapper extends ModuleWrapper {
8282
};
8383

8484
public pause = async (params: TxParams) => {
85-
assert.assert(!(await this.paused()), 'Contract already paused');
86-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
85+
assert.assert(!(await this.paused()), ErrorCode.PreconditionRequired, 'Contract already paused');
86+
assert.assert(
87+
await this.isCallerTheSecurityTokenOwner(params.txData),
88+
ErrorCode.Unauthorized,
89+
'The caller must be the ST owner',
90+
);
8791
return (await this.contract).pause.sendTransactionAsync(params.txData, params.safetyFactor);
8892
};
8993

9094
public unpause = async (params: TxParams) => {
91-
assert.assert(await this.paused(), 'Contract is not paused');
92-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
95+
assert.assert(await this.paused(), ErrorCode.ContractPaused, 'Contract is not paused');
96+
assert.assert(
97+
await this.isCallerTheSecurityTokenOwner(params.txData),
98+
ErrorCode.Unauthorized,
99+
'The caller must be the ST owner',
100+
);
93101
return (await this.contract).unpause.sendTransactionAsync(params.txData, params.safetyFactor);
94102
};
95103
}

src/contract_wrappers/modules/sto/usd_tiered_sto_wrapper.ts

Lines changed: 90 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
Subscribe,
3535
SubscribeAsyncParams,
3636
TxParams,
37+
ErrorCode,
3738
} from '../../../types';
3839
import {
3940
bigNumberToDate,
@@ -441,9 +442,14 @@ export default class USDTieredSTOWrapper extends STOWrapper {
441442
};
442443

443444
public changeAllowBeneficialInvestments = async (params: ChangeAllowBeneficialInvestmentsParams) => {
444-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
445+
assert.assert(
446+
await this.isCallerTheSecurityTokenOwner(params.txData),
447+
ErrorCode.Unauthorized,
448+
'The caller must be the ST owner',
449+
);
445450
assert.assert(
446451
params.allowBeneficialInvestments !== (await this.allowBeneficialInvestments()),
452+
ErrorCode.PreconditionRequired,
447453
'The value must be different',
448454
);
449455
return (await this.contract).changeAllowBeneficialInvestments.sendTransactionAsync(
@@ -597,7 +603,7 @@ export default class USDTieredSTOWrapper extends STOWrapper {
597603

598604
public getTokensSoldByTier = async (params: TierIndexParams) => {
599605
const tiers = await this.getNumberOfTiers();
600-
assert.assert(params.tier < new BigNumber(tiers).toNumber(), 'Invalid tier');
606+
assert.assert(params.tier < new BigNumber(tiers).toNumber(), ErrorCode.InvalidData, 'Invalid tier');
601607
return weiToValue(
602608
await (await this.contract).getTokensSoldByTier.callAsync(numberToBigNumber(params.tier)),
603609
await (await this.securityTokenContract()).decimals.callAsync(),
@@ -711,7 +717,7 @@ export default class USDTieredSTOWrapper extends STOWrapper {
711717
*/
712718
public getTokensMintedByTier = async (params: TierIndexParams) => {
713719
const decimals = await (await this.securityTokenContract()).decimals.callAsync();
714-
assert.assert(params.tier < (await this.getNumberOfTiers()).toNumber(), 'Invalid tier');
720+
assert.assert(params.tier < (await this.getNumberOfTiers()).toNumber(), ErrorCode.InvalidData, 'Invalid tier');
715721
const result = await (await this.contract).getTokensMintedByTier.callAsync(numberToBigNumber(params.tier));
716722
const typedResult: MintedByTier = {
717723
mintedInETH: weiToValue(result[0], decimals),
@@ -759,8 +765,12 @@ export default class USDTieredSTOWrapper extends STOWrapper {
759765
* Reserve address must be whitelisted to successfully finalize
760766
*/
761767
public finalize = async (params: TxParams) => {
762-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
763-
assert.assert(!(await this.isFinalized()), 'STO is finalized');
768+
assert.assert(
769+
await this.isCallerTheSecurityTokenOwner(params.txData),
770+
ErrorCode.Unauthorized,
771+
'The caller must be the ST owner',
772+
);
773+
assert.assert(!(await this.isFinalized()), ErrorCode.PreconditionRequired, 'STO is finalized');
764774
// we can't execute mint to validate the method
765775
return (await this.contract).finalize.sendTransactionAsync(params.txData, params.safetyFactor);
766776
};
@@ -769,9 +779,17 @@ export default class USDTieredSTOWrapper extends STOWrapper {
769779
* Modifies the list of overrides for non-accredited limits in USD
770780
*/
771781
public changeNonAccreditedLimit = async (params: ChangeNonAccreditedLimitParams) => {
772-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
782+
assert.assert(
783+
await this.isCallerTheSecurityTokenOwner(params.txData),
784+
ErrorCode.Unauthorized,
785+
'The caller must be the ST owner',
786+
);
773787
params.investors.forEach(address => assert.isETHAddressHex('investors', address));
774-
assert.assert(params.investors.length === params.nonAccreditedLimit.length, 'Array length mismatch');
788+
assert.assert(
789+
params.investors.length === params.nonAccreditedLimit.length,
790+
ErrorCode.MismatchedArrayLength,
791+
'Array length mismatch',
792+
);
775793
return (await this.contract).changeNonAccreditedLimit.sendTransactionAsync(
776794
params.investors,
777795
valueArrayToWeiArray(params.nonAccreditedLimit, FULL_DECIMALS),
@@ -784,9 +802,13 @@ export default class USDTieredSTOWrapper extends STOWrapper {
784802
* Modifies STO start and end times
785803
*/
786804
public modifyTimes = async (params: ModifyTimesParams) => {
787-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
805+
assert.assert(
806+
await this.isCallerTheSecurityTokenOwner(params.txData),
807+
ErrorCode.Unauthorized,
808+
'The caller must be the ST owner',
809+
);
788810
assert.isFutureDate(bigNumberToDate(await this.startTime()), 'STO already started');
789-
assert.assert(params.endTime > params.startTime, 'Start date must be greater than end time');
811+
assert.assert(params.endTime > params.startTime, ErrorCode.TooEarly, 'Start date must be greater than end time');
790812
assert.isFutureDate(params.startTime, 'Start date must be in the future');
791813
return (await this.contract).modifyTimes.sendTransactionAsync(
792814
dateToBigNumber(params.startTime),
@@ -800,9 +822,14 @@ export default class USDTieredSTOWrapper extends STOWrapper {
800822
* Modifies oracle
801823
*/
802824
public modifyOracle = async (params: ModifyOracleParams) => {
803-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
825+
assert.assert(
826+
await this.isCallerTheSecurityTokenOwner(params.txData),
827+
ErrorCode.Unauthorized,
828+
'The caller must be the ST owner',
829+
);
804830
assert.assert(
805831
params.fundRaiseType === FundRaiseType.POLY || params.fundRaiseType === FundRaiseType.ETH,
832+
ErrorCode.InvalidData,
806833
'Invalid currency',
807834
);
808835
return (await this.contract).modifyOracle.sendTransactionAsync(
@@ -817,7 +844,11 @@ export default class USDTieredSTOWrapper extends STOWrapper {
817844
* Modifies max non accredited invets limit and overall minimum investment limit
818845
*/
819846
public modifyLimits = async (params: ModifyLimitsParams) => {
820-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
847+
assert.assert(
848+
await this.isCallerTheSecurityTokenOwner(params.txData),
849+
ErrorCode.Unauthorized,
850+
'The caller must be the ST owner',
851+
);
821852
assert.isFutureDate(bigNumberToDate(await this.startTime()), 'STO already started');
822853
return (await this.contract).modifyLimits.sendTransactionAsync(
823854
valueToWei(params.nonAccreditedLimitUSD, FULL_DECIMALS),
@@ -831,7 +862,11 @@ export default class USDTieredSTOWrapper extends STOWrapper {
831862
* Modifies fund raise types
832863
*/
833864
public modifyFunding = async (params: ModifyFundingParams) => {
834-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
865+
assert.assert(
866+
await this.isCallerTheSecurityTokenOwner(params.txData),
867+
ErrorCode.Unauthorized,
868+
'The caller must be the ST owner',
869+
);
835870
assert.isFutureDate(bigNumberToDate(await this.startTime()), 'STO already started');
836871
return (await this.contract).modifyFunding.sendTransactionAsync(
837872
params.fundRaiseTypes,
@@ -844,7 +879,11 @@ export default class USDTieredSTOWrapper extends STOWrapper {
844879
* Modifies addresses used as wallet, reserve wallet and usd token
845880
*/
846881
public modifyAddresses = async (params: ModifyAddressesParams) => {
847-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
882+
assert.assert(
883+
await this.isCallerTheSecurityTokenOwner(params.txData),
884+
ErrorCode.Unauthorized,
885+
'The caller must be the ST owner',
886+
);
848887
params.usdTokens.forEach(address => assert.isETHAddressHex('usdTokens', address));
849888
assert.isNonZeroETHAddressHex('wallet', params.wallet);
850889
assert.isNonZeroETHAddressHex('treasuryWallet', params.treasuryWallet);
@@ -861,23 +900,33 @@ export default class USDTieredSTOWrapper extends STOWrapper {
861900
* Modifiers STO tiers. All tiers must be passed, can not edit specific tiers.
862901
*/
863902
public modifyTiers = async (params: ModifyTiersParams) => {
864-
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
903+
assert.assert(
904+
await this.isCallerTheSecurityTokenOwner(params.txData),
905+
ErrorCode.Unauthorized,
906+
'The caller must be the ST owner',
907+
);
865908
assert.isFutureDate(bigNumberToDate(await this.startTime()), 'STO already started');
866-
assert.assert(params.tokensPerTierTotal.length > 0, 'No tiers provided');
909+
assert.assert(params.tokensPerTierTotal.length > 0, ErrorCode.MismatchedArrayLength, 'No tiers provided');
867910
assert.assert(
868911
params.ratePerTier.length === params.tokensPerTierTotal.length &&
869912
params.ratePerTierDiscountPoly.length === params.tokensPerTierTotal.length &&
870913
params.tokensPerTierDiscountPoly.length === params.tokensPerTierTotal.length,
914+
ErrorCode.MismatchedArrayLength,
871915
'Tier data arrays length mismatch',
872916
);
873917
for (let i = 0; i < params.tokensPerTierTotal.length; i += 1) {
874918
assert.isBigNumberGreaterThanZero(params.ratePerTier[i], 'Invalid rate');
875919
assert.isBigNumberGreaterThanZero(params.tokensPerTierTotal[i], 'Invalid token amount');
876920
assert.assert(
877921
params.tokensPerTierDiscountPoly[i].isLessThanOrEqualTo(params.tokensPerTierTotal[i]),
922+
ErrorCode.InvalidDiscount,
878923
'Too many discounted tokens',
879924
);
880-
assert.assert(params.ratePerTierDiscountPoly[i].isLessThanOrEqualTo(params.ratePerTier[i]), 'Invalid discount');
925+
assert.assert(
926+
params.ratePerTierDiscountPoly[i].isLessThanOrEqualTo(params.ratePerTier[i]),
927+
ErrorCode.InvalidDiscount,
928+
'Invalid discount',
929+
);
881930
}
882931
const decimals = await (await this.securityTokenContract()).decimals.callAsync();
883932
return (await this.contract).modifyTiers.sendTransactionAsync(
@@ -956,45 +1005,55 @@ export default class USDTieredSTOWrapper extends STOWrapper {
9561005
usdToken?: string,
9571006
) => {
9581007
assert.isETHAddressHex('beneficiary', beneficiary);
959-
assert.assert(!(await this.paused()), 'Contract is Paused');
960-
assert.assert(await this.isOpen(), 'STO not open');
1008+
assert.assert(!(await this.paused()), ErrorCode.PreconditionRequired, 'Contract is Paused');
1009+
assert.assert(await this.isOpen(), ErrorCode.STOClosed, 'STO not open');
9611010
assert.isBigNumberGreaterThanZero(investmentValue, 'No funds were sent');
9621011
const stoDetails = await this.getSTODetails();
9631012
switch (fundRaiseType) {
9641013
case FundRaiseType.ETH: {
965-
assert.assert(stoDetails.isRaisedInETH, 'ETH Not Allowed');
1014+
assert.assert(stoDetails.isRaisedInETH, ErrorCode.CoinNotAllowed, 'ETH Not Allowed');
9661015
const weiBalance = await this.web3Wrapper.getBalanceInWeiAsync(from);
967-
assert.assert(weiBalance.isGreaterThan(investmentValue), 'Insufficient ETH funds');
1016+
assert.assert(
1017+
weiBalance.isGreaterThan(investmentValue),
1018+
ErrorCode.InsufficientBalance,
1019+
'Insufficient ETH funds',
1020+
);
9681021
break;
9691022
}
9701023
case FundRaiseType.POLY: {
971-
assert.assert(stoDetails.isRaisedInPOLY, 'POLY Not Allowed');
1024+
assert.assert(stoDetails.isRaisedInPOLY, ErrorCode.CoinNotAllowed, 'POLY Not Allowed');
9721025
const polyTokenBalance = await (await this.polyTokenContract()).balanceOf.callAsync(from);
9731026
assert.assert(
9741027
polyTokenBalance.isGreaterThanOrEqualTo(investmentValue),
1028+
ErrorCode.InsufficientBalance,
9751029
'Budget less than amount unable to transfer fee',
9761030
);
9771031
break;
9781032
}
9791033
case FundRaiseType.StableCoin: {
980-
assert.assert(stoDetails.isRaisedInSC, 'USD Not Allowed');
981-
assert.assert(usdToken !== null, 'USD Token Address must exist');
1034+
assert.assert(stoDetails.isRaisedInSC, ErrorCode.CoinNotAllowed, 'USD Not Allowed');
1035+
assert.assert(usdToken !== null, ErrorCode.InvalidAddress, 'USD Token Address must exist');
9821036
if (usdToken) {
9831037
const scTokenBalance = await (await this.detailedERC20TokenContract(usdToken)).balanceOf.callAsync(from);
9841038
assert.assert(
9851039
scTokenBalance.isGreaterThanOrEqualTo(investmentValue),
1040+
ErrorCode.InsufficientBalance,
9861041
'Budget less than amount unable to transfer fee',
9871042
);
9881043
}
9891044
break;
9901045
}
9911046
default: {
992-
assert.assert(false, 'Missing fundraise type');
1047+
assert.assert(false, ErrorCode.InvalidData, 'Missing fundraise type');
9931048
break;
9941049
}
9951050
}
9961051
if (!(await this.allowBeneficialInvestments())) {
997-
assert.assert(functionsUtils.checksumAddressComparision(beneficiary, from), 'Beneficiary != funder');
1052+
assert.assert(
1053+
functionsUtils.checksumAddressComparision(beneficiary, from),
1054+
ErrorCode.Unauthorized,
1055+
'Beneficiary != funder',
1056+
);
9981057
}
9991058
const rate = await this.getRate({
10001059
fundRaiseType,
@@ -1004,7 +1063,11 @@ export default class USDTieredSTOWrapper extends STOWrapper {
10041063
investorAddress: beneficiary,
10051064
});
10061065
const minimumInvestmentUSD = await this.minimumInvestmentUSD();
1007-
assert.assert(investedUSD.plus(investorInvestedUSD).isGreaterThan(minimumInvestmentUSD), 'Investment < min');
1066+
assert.assert(
1067+
investedUSD.plus(investorInvestedUSD).isGreaterThan(minimumInvestmentUSD),
1068+
ErrorCode.InsufficientBalance,
1069+
'Investment < min',
1070+
);
10081071

10091072
const generalTMAddress = await (await this.securityTokenContract()).getModulesByName.callAsync(
10101073
stringToBytes32(ModuleName.GeneralTransferManager),
@@ -1026,7 +1089,7 @@ export default class USDTieredSTOWrapper extends STOWrapper {
10261089
!nonAccreditedLimit || nonAccreditedLimit.isEqualTo(BIG_NUMBER_ZERO)
10271090
? await this.nonAccreditedLimitUSD()
10281091
: nonAccreditedLimit;
1029-
assert.assert(investorInvestedUSD.isLessThan(nonAccreditedLimitUSD), 'Over investor limit');
1092+
assert.assert(investorInvestedUSD.isLessThan(nonAccreditedLimitUSD), ErrorCode.ErrorLimit, 'Over investor limit');
10301093
}
10311094
};
10321095
}

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,8 @@ export enum ErrorCode {
360360
InsufficientAllowance = 'InsufficientAllowance',
361361
DifferentMode = 'DifferentMode',
362362
InvalidTransfer = 'InvalidTransfer',
363+
InvalidDiscount = 'InvalidDiscount',
364+
STOClosed = 'STOClosed',
365+
CoinNotAllowed = 'CoinNotAllowed',
366+
ErrorLimit = 'ErrorLimit',
363367
}

0 commit comments

Comments
 (0)