1
1
import ethUtil from 'ethereumjs-util' ;
2
2
import BigNumber from 'bignumber.js' ;
3
- import BN from " bn.js" ;
3
+ import BN from ' bn.js' ;
4
4
5
5
BigNumber . config ( {
6
- EXPONENTIAL_AT :20 ,
7
- RANGE :[ - 20 , 10000000 ] ,
8
- ROUNDING_MODE :1
6
+ EXPONENTIAL_AT : 20 ,
7
+ RANGE : [ - 20 , 10000000 ] ,
8
+ ROUNDING_MODE : 1
9
9
} ) ;
10
10
/**
11
11
*
12
12
* @param mixed Buffer|number|string (hex string must be with '0x' prefix)
13
13
* @returns {Buffer }
14
14
*/
15
- export function toBuffer ( mixed ) {
16
- if ( mixed instanceof Buffer ) {
17
- return mixed ;
18
- } else {
19
- return ethUtil . toBuffer ( mixed )
20
- }
15
+ export function toBuffer ( mixed )
16
+ {
17
+ if ( mixed instanceof Buffer )
18
+ {
19
+ return mixed ;
20
+ }
21
+ else
22
+ {
23
+ return ethUtil . toBuffer ( mixed ) ;
24
+ }
21
25
}
22
26
23
27
/**
24
28
*
25
29
* @param mixed number | BigNumber | BN | Buffer | string
26
30
* @returns {string }
27
31
*/
28
- export function toHex ( mixed ) {
29
-
30
- if ( typeof mixed === 'number' || mixed instanceof BigNumber || mixed instanceof BN ) {
31
- return addHexPrefix ( mixed . toString ( 16 ) )
32
- }
33
-
34
- if ( mixed instanceof Buffer ) {
35
- return addHexPrefix ( mixed . toString ( 'hex' ) )
36
- }
37
-
38
- if ( typeof mixed === 'string' ) {
39
- const regex = new RegExp ( / ^ 0 x [ 0 - 9 a - f A - F ] * $ / ) ;
40
- return regex . test ( mixed ) ? mixed : addHexPrefix ( toBuffer ( String ) . toString ( 'hex' ) )
41
- }
42
- throw new Error ( 'Unsupported type' )
32
+ export function toHex ( mixed )
33
+ {
34
+ if ( typeof mixed === 'number' || mixed instanceof BigNumber || mixed instanceof BN )
35
+ {
36
+ return addHexPrefix ( mixed . toString ( 16 ) ) ;
37
+ }
38
+
39
+ if ( mixed instanceof Buffer )
40
+ {
41
+ return addHexPrefix ( mixed . toString ( 'hex' ) ) ;
42
+ }
43
+
44
+ if ( typeof mixed === 'string' )
45
+ {
46
+ const regex = new RegExp ( / ^ 0 x [ 0 - 9 a - f A - F ] * $ / ) ;
47
+ return regex . test ( mixed ) ? mixed : addHexPrefix ( toBuffer ( String ) . toString ( 'hex' ) ) ;
48
+ }
49
+ throw new Error ( 'Unsupported type' ) ;
43
50
}
44
51
45
52
/**
46
53
*
47
54
* @param mixed number | BigNumber | BN | Buffer | string
48
55
* @returns {number }
49
56
*/
50
- export function toNumber ( mixed ) {
51
- if ( typeof mixed === 'number' ) {
52
- return mixed
53
- }
54
-
55
- if ( mixed instanceof BigNumber || mixed instanceof BN ) {
56
- return mixed . toNumber ( )
57
- }
58
-
59
- if ( typeof mixed === 'string' ) {
60
- return Number ( mixed )
61
- }
62
-
63
- throw new Error ( 'Unsupported type' )
57
+ export function toNumber ( mixed )
58
+ {
59
+ if ( typeof mixed === 'number' )
60
+ {
61
+ return mixed ;
62
+ }
63
+
64
+ if ( mixed instanceof BigNumber || mixed instanceof BN )
65
+ {
66
+ return mixed . toNumber ( ) ;
67
+ }
68
+
69
+ if ( typeof mixed === 'string' )
70
+ {
71
+ return Number ( mixed ) ;
72
+ }
73
+
74
+ throw new Error ( 'Unsupported type' ) ;
64
75
}
65
76
66
77
/**
67
78
*
68
79
* @param mixed number | BigNumber | BN | Buffer | string
69
80
* @returns {BigNumber }
70
81
*/
71
- export function toBig ( mixed ) {
72
-
73
- if ( mixed instanceof BigNumber ) {
74
- return mixed ;
75
- }
76
-
77
- if ( typeof mixed === 'number' ) {
78
-
79
- return new BigNumber ( mixed . toString ( ) )
80
- }
81
-
82
- if ( typeof mixed === 'string' ) {
83
-
84
- return new BigNumber ( mixed )
85
- }
86
-
87
- throw new Error ( 'Unsupported type' )
88
-
82
+ export function toBig ( mixed )
83
+ {
84
+ if ( mixed instanceof BigNumber )
85
+ {
86
+ return mixed ;
87
+ }
88
+
89
+ if ( typeof mixed === 'number' )
90
+ {
91
+ return new BigNumber ( mixed . toString ( ) ) ;
92
+ }
93
+
94
+ if ( typeof mixed === 'string' )
95
+ {
96
+ return new BigNumber ( mixed ) ;
97
+ }
98
+
99
+ throw new Error ( 'Unsupported type' ) ;
89
100
}
90
101
91
-
92
102
/**
93
103
*
94
104
* @param mixed number | BigNumber | BN | Buffer | string
95
105
* @returns {BN }
96
106
*/
97
- export function toBN ( mixed ) {
98
- return ( mixed instanceof BN ) ? mixed : new BN ( toBig ( mixed ) . toString ( 10 ) , 10 ) ;
107
+ export function toBN ( mixed )
108
+ {
109
+ return ( mixed instanceof BN ) ? mixed : new BN ( toBig ( mixed ) . toString ( 10 ) , 10 ) ;
99
110
}
100
111
101
112
/**
102
113
* Returns formatted hex string of a given private key
103
114
* @param mixed Buffer| string
104
115
* @returns {string }
105
116
*/
106
- export function formatKey ( mixed ) {
107
-
108
- if ( mixed instanceof Buffer ) {
109
- return mixed . toString ( 'hex' )
110
- }
111
-
112
- if ( typeof mixed === 'string' ) {
113
- return mixed . startsWith ( "0x" ) ? mixed : mixed
114
- }
115
- throw new Error ( 'Unsupported type' )
117
+ export function formatKey ( mixed )
118
+ {
119
+ if ( mixed instanceof Buffer )
120
+ {
121
+ return mixed . toString ( 'hex' ) ;
122
+ }
123
+
124
+ if ( typeof mixed === 'string' )
125
+ {
126
+ return mixed . startsWith ( '0x' ) ? mixed : mixed ;
127
+ }
128
+ throw new Error ( 'Unsupported type' ) ;
116
129
}
117
130
118
131
/**
119
132
* Returns hex string of a given address
120
133
* @param mixed Buffer | string
121
134
* @returns {string }
122
135
*/
123
- export function formatAddress ( mixed ) {
124
- if ( mixed instanceof Buffer ) {
125
- return ethUtil . toChecksumAddress ( '0x' + mixed . toString ( 'hex' ) )
126
- }
127
-
128
- if ( typeof mixed === 'string' ) {
129
- return ethUtil . toChecksumAddress ( mixed . startsWith ( "0x" ) ? mixed : "0x" + mixed )
130
- }
131
- throw new Error ( 'Unsupported type' )
132
-
136
+ export function formatAddress ( mixed )
137
+ {
138
+ if ( mixed instanceof Buffer )
139
+ {
140
+ return ethUtil . toChecksumAddress ( '0x' + mixed . toString ( 'hex' ) ) ;
141
+ }
142
+
143
+ if ( typeof mixed === 'string' )
144
+ {
145
+ return ethUtil . toChecksumAddress ( mixed . startsWith ( '0x' ) ? mixed : '0x' + mixed ) ;
146
+ }
147
+ throw new Error ( 'Unsupported type' ) ;
133
148
}
134
149
135
150
/**
136
151
* Returns hex string with '0x' prefix
137
152
* @param input
138
153
* @returns {string }
139
154
*/
140
- export function addHexPrefix ( input ) {
141
-
142
- if ( typeof input === 'string' ) {
143
- return input . startsWith ( '0x' ) ? input : "0x" + input ;
144
- }
145
- throw new Error ( 'Unsupported type' )
155
+ export function addHexPrefix ( input )
156
+ {
157
+ if ( typeof input === 'string' )
158
+ {
159
+ return input . startsWith ( '0x' ) ? input : '0x' + input ;
160
+ }
161
+ throw new Error ( 'Unsupported type' ) ;
146
162
}
147
163
148
164
/**
149
165
* Returns hex string without '0x' prefix
150
166
* @param input string
151
167
* @returns {string }
152
168
*/
153
- export function clearHexPrefix ( input ) {
154
- if ( typeof input === 'string' ) {
155
- return input . startsWith ( '0x' ) ? input . slice ( 2 ) : input ;
156
- }
157
- throw new Error ( 'Unsupported type' )
169
+ export function clearHexPrefix ( input )
170
+ {
171
+ if ( typeof input === 'string' )
172
+ {
173
+ return input . startsWith ( '0x' ) ? input . slice ( 2 ) : input ;
174
+ }
175
+ throw new Error ( 'Unsupported type' ) ;
158
176
}
159
177
160
178
/**
161
179
*
162
180
* @param hex
163
181
* @returns {string }
164
182
*/
165
- export function padLeftEven ( hex ) {
166
- return hex . length % 2 !== 0 ? `0${ hex } ` : hex ;
183
+ export function padLeftEven ( hex )
184
+ {
185
+ return hex . length % 2 !== 0 ? `0${ hex } ` : hex ;
167
186
}
168
187
169
188
/**
170
189
* Returns symbol of a given kind of currency
171
190
* @param settingsCurrency
172
191
* @returns {* }
173
192
*/
174
- export function getDisplaySymbol ( settingsCurrency ) {
175
- switch ( settingsCurrency ) {
176
- case 'CNY' :
177
- return '¥' ;
178
- case 'USD' :
179
- return '$' ;
180
- default :
181
- return ''
182
- }
193
+ export function getDisplaySymbol ( settingsCurrency )
194
+ {
195
+ switch ( settingsCurrency )
196
+ {
197
+ case 'CNY' :
198
+ return '¥' ;
199
+ case 'USD' :
200
+ return '$' ;
201
+ default :
202
+ return '' ;
203
+ }
183
204
}
184
205
185
206
/**
@@ -189,23 +210,21 @@ export function getDisplaySymbol(settingsCurrency) {
189
210
* @param ceil bool round up
190
211
* @returns {string }
191
212
*/
192
- export function toFixed ( number , precision , ceil ) {
193
- precision = precision || 0 ;
194
- ceil = ceil || false ;
195
- if ( number instanceof BigNumber ) {
196
- const rm = ceil ? 0 : 1 ;
197
- return number . toFixed ( precision , rm )
198
- }
199
-
200
- if ( typeof number === 'number' ) {
201
- return ceil ? ( Math . ceil ( number * Number ( '1e' + precision ) ) / Number ( '1e' + precision ) ) . toFixed ( precision ) :
202
- ( Math . floor ( number * Number ( '1e' + precision ) ) / Number ( '1e' + precision ) ) . toFixed ( precision ) ;
203
- }
204
-
205
- throw new Error ( 'Unsupported type' )
213
+ export function toFixed ( number , precision , ceil )
214
+ {
215
+ precision = precision || 0 ;
216
+ ceil = ceil || false ;
217
+ if ( number instanceof BigNumber )
218
+ {
219
+ const rm = ceil ? 0 : 1 ;
220
+ return number . toFixed ( precision , rm ) ;
221
+ }
222
+
223
+ if ( typeof number === 'number' )
224
+ {
225
+ return ceil ? ( Math . ceil ( number * Number ( '1e' + precision ) ) / Number ( '1e' + precision ) ) . toFixed ( precision )
226
+ : ( Math . floor ( number * Number ( '1e' + precision ) ) / Number ( '1e' + precision ) ) . toFixed ( precision ) ;
227
+ }
228
+
229
+ throw new Error ( 'Unsupported type' ) ;
206
230
}
207
-
208
-
209
-
210
-
211
-
0 commit comments