1 | const JSBI = require('jsbi');
|
2 | const Big = require('big.js');
|
3 | const lodash = require('lodash');
|
4 | const parser = require('./parser');
|
5 |
|
6 | const MAX_UINT = JSBI.BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
|
7 |
|
8 |
|
9 | JSBI.prototype.toJSON = function () {
|
10 | return this.toString();
|
11 | };
|
12 |
|
13 |
|
14 | function toHex(value) {
|
15 | let hex;
|
16 |
|
17 | if (lodash.isString(value)) {
|
18 | hex = value.toLowerCase();
|
19 | } else if (Number.isInteger(value) || (value instanceof JSBI)) {
|
20 | hex = `0x${value.toString(16)}`;
|
21 | } else if (Buffer.isBuffer(value)) {
|
22 | hex = `0x${value.toString('hex')}`;
|
23 | } else if (lodash.isBoolean(value)) {
|
24 | hex = value ? '0x01' : '0x00';
|
25 | } else if (value === null) {
|
26 | hex = '0x';
|
27 | } else {
|
28 | hex = `${value}`;
|
29 | }
|
30 |
|
31 | if (!/^0x[0-9a-f]*$/.test(hex)) {
|
32 | throw new Error(`${value} not match hex`);
|
33 | }
|
34 | return hex.length % 2 ? `0x0${hex.slice(2)}` : hex;
|
35 | }
|
36 |
|
37 | function toNumber(value) {
|
38 | if (value === null) {
|
39 | throw new Error(`${value} not match number`);
|
40 | } else if (Buffer.isBuffer(value)) {
|
41 | value = `0x${value.toString('hex')}`;
|
42 | }
|
43 | return Number(value);
|
44 | }
|
45 |
|
46 | function toBigInt(value) {
|
47 | if (lodash.isString(value)) {
|
48 | value = value.replace(/^(-?\d+)(.0+)?$/, '$1');
|
49 | } else if (lodash.isBoolean(value)) {
|
50 | throw new Error(`${value} not match BigInt`);
|
51 | } else if (Buffer.isBuffer(value)) {
|
52 | throw new Error(`${value} not match BigInt`);
|
53 | }
|
54 | return JSBI.BigInt(value);
|
55 | }
|
56 |
|
57 |
|
58 | const format = {};
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | format.any = parser(v => v);
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | format.hex = parser(toHex);
|
91 | format.hex64 = format.hex.$validate(v => v.length === 2 + 64, 'hex64');
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | format.uInt = parser(toNumber).$validate(v => Number.isSafeInteger(v) && v >= 0, 'uint');
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | format.bigInt = parser(toBigInt);
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | format.bigUInt = format.bigInt.$validate(v => v >= 0, 'bigUInt');
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | format.hexUInt = format.bigUInt
|
174 | .$after(v => `0x${v.toString(16)}`)
|
175 | .$validate(v => /^0x[0-9a-f]+$/.test(v), 'hexUInt');
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | format.riskNumber = format.bigUInt.$after(v => Number(Big(v).div(MAX_UINT)));
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | format.epochNumber = format.hexUInt
|
202 | .$or('earliest')
|
203 | .$or('latest_checkpoint')
|
204 | .$or('latest_confirmed')
|
205 | .$or('latest_state')
|
206 | .$or('latest_mined');
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | format.address = format.hex.$validate(v => v.length === 2 + 40, 'address');
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | format.publicKey = format.hex.$validate(v => v.length === 2 + 128, 'publicKey');
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | format.privateKey = format.hex64;
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | format.signature = format.hex.$validate(v => v.length === 2 + 130, 'signature');
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | format.blockHash = format.hex64;
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 | format.transactionHash = format.hex64;
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | format.buffer = format.hex.$after(v => Buffer.from(v.substr(2), 'hex'));
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | format.bytes = parser(v => (Buffer.isBuffer(v) ? v : Buffer.from(v)));
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | format.boolean = format.any.$validate(lodash.isBoolean, 'boolean');
|
319 |
|
320 |
|
321 | format.getLogs = parser({
|
322 | limit: format.hexUInt.$or(undefined),
|
323 | fromEpoch: format.epochNumber.$or(undefined),
|
324 | toEpoch: format.epochNumber.$or(undefined),
|
325 | blockHashes: format.blockHash.$or([format.blockHash]).$or(undefined),
|
326 | address: format.address.$or([format.address]).$or(undefined),
|
327 | topics: parser([format.hex64.$or([format.hex64]).$or(null)]).$or(undefined),
|
328 | }, true);
|
329 |
|
330 | format.signTx = parser({
|
331 | nonce: format.hexUInt.$after(format.buffer),
|
332 | gasPrice: format.hexUInt.$after(format.buffer),
|
333 | gas: format.hexUInt.$after(format.buffer),
|
334 | to: parser(format.address.$or(null).$default(null)).$after(format.buffer),
|
335 | value: format.hexUInt.$default(0).$after(format.buffer),
|
336 | storageLimit: format.hexUInt.$after(format.buffer),
|
337 | epochHeight: format.uInt.$after(format.buffer),
|
338 | chainId: format.uInt.$default(0).$after(format.buffer),
|
339 | data: format.hex.$default('0x').$after(format.buffer),
|
340 | r: (format.hexUInt.$after(format.buffer)).$or(undefined),
|
341 | s: (format.hexUInt.$after(format.buffer)).$or(undefined),
|
342 | v: (format.uInt.$after(format.buffer)).$or(undefined),
|
343 | }, true);
|
344 |
|
345 | format.sendTx = parser({
|
346 | from: format.address,
|
347 | nonce: format.hexUInt.$or(undefined),
|
348 | gasPrice: format.hexUInt,
|
349 | gas: format.hexUInt,
|
350 | to: format.address.$or(null).$or(undefined),
|
351 | value: format.hexUInt.$or(undefined),
|
352 | storageLimit: format.hexUInt,
|
353 | epochHeight: format.hexUInt.$or(undefined),
|
354 | chainId: format.hexUInt.$or(undefined),
|
355 | data: format.hex.$or(undefined),
|
356 | }, true);
|
357 |
|
358 | format.callTx = parser({
|
359 | from: format.address.$or(undefined),
|
360 | nonce: format.hexUInt.$or(undefined),
|
361 | gasPrice: format.hexUInt.$or(undefined),
|
362 | gas: format.hexUInt.$or(undefined),
|
363 | to: format.address.$or(null),
|
364 | value: format.hexUInt.$or(undefined),
|
365 | storageLimit: format.hexUInt.$or(undefined),
|
366 | epochHeight: format.uInt.$or(undefined),
|
367 | chainId: format.uInt.$or(undefined),
|
368 | data: format.hex.$or(undefined),
|
369 | }, true);
|
370 |
|
371 | format.estimateTx = parser({
|
372 | from: format.address.$or(undefined),
|
373 | nonce: format.hexUInt.$or(undefined),
|
374 | gasPrice: format.hexUInt.$or(undefined),
|
375 | gas: format.hexUInt.$or(undefined),
|
376 | to: format.address.$or(null).$or(undefined),
|
377 | value: format.hexUInt.$or(undefined),
|
378 | storageLimit: format.hexUInt.$or(undefined),
|
379 | epochHeight: format.uInt.$or(undefined),
|
380 | chainId: format.uInt.$or(undefined),
|
381 | data: format.hex.$or(undefined),
|
382 | }, true);
|
383 |
|
384 |
|
385 | format.status = parser({
|
386 | chainId: format.uInt,
|
387 | epochNumber: format.uInt,
|
388 | blockNumber: format.uInt,
|
389 | pendingTxNumber: format.uInt,
|
390 | });
|
391 |
|
392 | format.account = parser({
|
393 | accumulatedInterestReturn: format.bigUInt,
|
394 | balance: format.bigUInt,
|
395 | collateralForStorage: format.bigUInt,
|
396 | nonce: format.bigUInt,
|
397 | stakingBalance: format.bigUInt,
|
398 | });
|
399 |
|
400 | format.transaction = parser({
|
401 | nonce: format.bigUInt,
|
402 | value: format.bigUInt,
|
403 | gasPrice: format.bigUInt,
|
404 | gas: format.bigUInt,
|
405 | v: format.uInt,
|
406 | transactionIndex: format.uInt.$or(null),
|
407 | status: format.uInt.$or(null),
|
408 | storageLimit: format.bigUInt,
|
409 | chainId: format.uInt,
|
410 | epochHeight: format.uInt,
|
411 | });
|
412 |
|
413 | format.estimate = parser({
|
414 | gasUsed: format.bigUInt,
|
415 | storageCollateralized: format.bigUInt,
|
416 | });
|
417 |
|
418 | format.block = parser({
|
419 | epochNumber: format.uInt,
|
420 | blame: format.uInt,
|
421 | height: format.uInt,
|
422 | size: format.uInt,
|
423 | timestamp: format.uInt,
|
424 | gasLimit: format.bigUInt,
|
425 | difficulty: format.bigUInt,
|
426 | transactions: [(format.transaction).$or(format.transactionHash)],
|
427 | });
|
428 |
|
429 | format.receipt = parser({
|
430 | index: format.uInt,
|
431 | epochNumber: format.uInt,
|
432 | outcomeStatus: format.uInt.$or(null),
|
433 | gasUsed: format.bigUInt,
|
434 | gasFee: format.bigUInt,
|
435 | });
|
436 |
|
437 | format.logs = parser([
|
438 | {
|
439 | epochNumber: format.uInt,
|
440 | logIndex: format.uInt,
|
441 | transactionIndex: format.uInt,
|
442 | transactionLogIndex: format.uInt,
|
443 | },
|
444 | ]);
|
445 |
|
446 | format.sponsorInfo = parser({
|
447 | sponsorBalanceForCollateral: format.bigUInt,
|
448 | sponsorBalanceForGas: format.bigUInt,
|
449 | sponsorGasBound: format.bigUInt,
|
450 | });
|
451 |
|
452 | format.rewardInfo = parser([
|
453 | {
|
454 | baseReward: format.bigUInt,
|
455 | totalReward: format.bigUInt,
|
456 | txFee: format.bigUInt,
|
457 | },
|
458 | ]);
|
459 |
|
460 | module.exports = format;
|