-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
78 lines (68 loc) · 1.71 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const { BAND_SYMBOL } = require('./const')
const remove0x = hex => {
if (hex.startsWith('0x')) {
return hex.substring(2)
}
return hex
}
const containBandData = cells => {
let result = false
for (let cell of cells) {
if (remove0x(cell.output_data).startsWith(BAND_SYMBOL)) {
result = true
break
}
}
return result
}
const intToHex = num => {
let hex = num.toString(16)
return hex.length % 2 === 1 ? `0${hex}` : hex
}
const intToU32 = num => {
if (typeof num !== 'number') {
throw new Error('Invalid data type')
}
const u32 = num.toString(16)
return `${'0'.repeat(8 - u32.length)}${u32}`
}
const u32ToInt = hex => {
if (typeof hex !== 'string') {
throw new Error('Invalid data type')
}
return parseInt(remove0x(hex), 16)
}
const intToU64 = num => {
if (typeof num !== 'number' && typeof num !== 'bigint') {
throw new Error('Invalid data type')
}
const u64 = num.toString(16)
return `${'0'.repeat(16 - u64.length)}${u64}`
}
const u64ToInt = hex => {
if (typeof hex !== 'string') {
throw new Error('Invalid data type')
}
return parseInt(remove0x(hex), 16)
}
const generateBandData = (price, index, timestamp) => {
return `0x${BAND_SYMBOL}${intToHex(index)}${intToU32(timestamp)}${intToU64(price)}`
}
const parseBandData = data => {
const temp = remove0x(data)
const start = BAND_SYMBOL.length
const index = parseInt(temp.substring(start, start + 2), 16)
const timestamp = u32ToInt(temp.substring(start + 2, start + 10))
const price = u64ToInt(temp.substring(start + 10))
return { index, timestamp, price }
}
module.exports = {
remove0x,
containBandData,
intToU32,
u32ToInt,
intToU64,
u64ToInt,
generateBandData,
parseBandData,
}