1
1
import { assert as sharedAssert } from '@0x/assert' ;
2
2
import { BigNumber } from '@polymathnetwork/abi-wrappers' ;
3
- import { Partition } from '../types' ;
3
+ import { Partition , ErrorCode } from '../types' ;
4
+ import { PolymathError } from '../PolymathError' ;
4
5
5
6
const ZERO = '0x0000000000000000000000000000000000000000' ;
6
7
const MAX_64_BYTES_DATE = new Date ( 18446744073709 ) ;
@@ -9,61 +10,75 @@ const BIG_NUMBER_ZERO = new BigNumber(0);
9
10
10
11
const assert = {
11
12
...sharedAssert ,
13
+ assert ( condition : boolean , code : ErrorCode , message ?: string ) : void {
14
+ if ( ! condition ) {
15
+ throw new PolymathError ( { message, code } ) ;
16
+ }
17
+ } ,
12
18
isValidSubscriptionToken ( variableName : string , subscriptionToken : string ) : void {
13
19
const uuidRegex = new RegExp ( '^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$' ) ;
14
20
const isValid = uuidRegex . test ( subscriptionToken ) ;
15
- sharedAssert . assert ( isValid , `Expected ${ variableName } to be a valid subscription token` ) ;
21
+ this . assert (
22
+ isValid ,
23
+ ErrorCode . InvalidSubscriptionToken ,
24
+ `Expected ${ variableName } to be a valid subscription token` ,
25
+ ) ;
16
26
} ,
17
27
isNonZeroETHAddressHex ( variableName : string , address : string ) : void {
18
28
sharedAssert . isETHAddressHex ( variableName , address ) ;
19
- sharedAssert . assert ( address !== ZERO , `'${ variableName } ' is not expected to be 0x0` ) ;
29
+ this . assert ( address !== ZERO , ErrorCode . InvalidAddress , `'${ variableName } ' is not expected to be 0x0` ) ;
20
30
} ,
21
31
areThereDuplicatedStrings ( variableName : string , addresses : string [ ] ) : void {
22
32
const result = addresses . length === new Set ( addresses ) . size ;
23
- sharedAssert . assert ( result , `There are duplicates in ${ variableName } array.` ) ;
33
+ this . assert ( result , ErrorCode . DuplicatedStrings , `There are duplicates in ${ variableName } array.` ) ;
24
34
} ,
25
35
isLessThanMax64BytesDate ( variableName : string , value : Date ) : void {
26
- sharedAssert . assert ( value <= MAX_64_BYTES_DATE , `${ variableName } date is too far in the future` ) ;
36
+ this . assert ( value <= MAX_64_BYTES_DATE , ErrorCode . TooFar , `${ variableName } date is too far in the future` ) ;
27
37
} ,
28
38
isPercentage ( variableName : string , value : BigNumber ) : void {
29
- sharedAssert . assert (
39
+ this . assert (
30
40
value . isLessThanOrEqualTo ( MAX_PERCENTAGE ) ,
41
+ ErrorCode . InvalidData ,
31
42
`${ variableName } is not expected to be greater than 100%` ,
32
43
) ;
33
44
} ,
34
45
isFutureDate ( value : Date , message : string ) : void {
35
- sharedAssert . assert ( value >= new Date ( ) , message ) ;
46
+ this . assert ( value >= new Date ( ) , ErrorCode . TooEarly , message ) ;
36
47
} ,
37
48
isPastDate ( value : Date , message : string ) : void {
38
- sharedAssert . assert ( value <= new Date ( ) , message ) ;
49
+ this . assert ( value <= new Date ( ) , ErrorCode . TooFar , message ) ;
39
50
} ,
40
51
isNotDateZero ( value : Date , message : string ) : void {
41
- sharedAssert . assert ( value !== new Date ( 0 ) , message ) ;
52
+ this . assert ( value !== new Date ( 0 ) , ErrorCode . InvalidData , message ) ;
42
53
} ,
43
54
isBigNumberZero ( value : BigNumber , message : string ) : void {
44
- sharedAssert . assert ( value . isEqualTo ( BIG_NUMBER_ZERO ) , message ) ;
55
+ this . assert ( value . isEqualTo ( BIG_NUMBER_ZERO ) , ErrorCode . InvalidData , message ) ;
45
56
} ,
46
57
isBigNumberGreaterThanZero ( value : BigNumber , message : string ) : void {
47
- sharedAssert . assert ( value >= BIG_NUMBER_ZERO , message ) ;
48
- } ,
49
- areValidArrayLengths ( value : any [ ] [ ] , message : string ) { // eslint-disable-line
50
- sharedAssert . assert (
51
- value . every ( ( x : any [ ] ) => { // eslint-disable-line
58
+ this . assert ( value >= BIG_NUMBER_ZERO , ErrorCode . InvalidData , message ) ;
59
+ } ,
60
+ areValidArrayLengths ( value : any [ ] [ ] , message : string ) {
61
+ // eslint-disable-line
62
+ this . assert (
63
+ value . every ( ( x : any [ ] ) => {
64
+ // eslint-disable-line
52
65
return x . length === value [ 0 ] . length ;
53
66
} ) ,
67
+ ErrorCode . MismatchedArrayLength ,
54
68
message ,
55
69
) ;
56
70
} ,
57
71
// eslint-enable no-any
58
72
isValidVersion ( version : string ) {
59
- sharedAssert . assert (
73
+ this . assert (
60
74
/ ^ ( \d + \. ) ( \d + \. ) ( \d + ) $ / . test ( version ) ,
75
+ ErrorCode . InvalidVersion ,
61
76
'Invalid package version. Right format: major.minor.patch' ,
62
77
) ;
63
78
} ,
64
79
isValidPartition ( partition : Partition ) {
65
- sharedAssert . assert ( partition === Partition . Unlocked , 'Invalid Partition' ) ;
66
- }
80
+ this . assert ( partition === Partition . Unlocked , ErrorCode . InvalidPartition , 'Invalid Partition' ) ;
81
+ } ,
67
82
} ;
68
83
69
84
export default assert ;
0 commit comments