-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
168 lines (159 loc) · 3 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
79
80
81
82
83
84
85
86
87
88
89
90
91
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const { post } = require('axios')
const crypto = require('crypto')
const Promise = require('bluebird')
const nodeData = {
'zone-0-0': {
http: 8610,
ws: 8611
},
'zone-0-1': {
http: 8542,
ws: 8643
},
'zone-0-2': {
http: 8674,
ws: 8675
},
'zone-1-0': {
http: 8512,
ws: 8613
},
'zone-1-1': {
http: 8544,
ws: 8645
},
'zone-1-2': {
http: 8576,
ws: 8677
},
'zone-2-0': {
http: 8614,
ws: 8615
},
'zone-2-1': {
http: 8646,
ws: 8647
},
'zone-2-2': {
http: 8678,
ws: 8679
}
}
const QUAI_CONTEXTS = [
{
name: 'Cyprus One',
shard: 'zone-0-0',
context: 2,
byte: ['0x00', '0x1D']
},
{
name: 'Cyprus Two',
shard: 'zone-0-1',
context: 2,
byte: ['0x1E', '0x3A']
},
{
name: 'Cyprus Three',
shard: 'zone-0-2',
context: 2,
byte: ['0x3B', '0x57']
},
{
name: 'Paxos One',
shard: 'zone-1-0',
context: 2,
byte: ['0x58', '0x73']
},
{
name: 'Paxos Two',
shard: 'zone-1-1',
context: 2,
byte: ['0x74', '0x8F']
},
{
name: 'Paxos Three',
shard: 'zone-1-2',
context: 2,
byte: ['0x90', '0xAB']
},
{
name: 'Hydra One',
shard: 'zone-2-0',
context: 2,
byte: ['0xAC', '0xC7']
},
{
name: 'Hydra Two',
shard: 'zone-2-1',
context: 2,
byte: ['0xC8', '0xE3']
},
{
name: 'Hydra Three',
shard: 'zone-2-2',
context: 2,
byte: ['0xE4', '0xFF']
}
]
const networks = {
1337: 'default',
9000: 'colosseum',
12000: 'garden',
15000: 'orchard',
17000: 'galena'
}
async function lookupChainId (url) {
const result = await post(url, {
jsonrpc: '2.0',
method: 'eth_chainId',
id: 1
})
if (result.data?.error) {
throw new Error(result.data.error)
}
const chainId = Number(result.data?.result)
return chainId
}
async function lookupTxPending (url) {
const result = await post(url, {
jsonrpc: '2.0',
method: 'txpool_status',
id: 1
})
if (result.data?.error) {
throw new Error(result.data?.error)
}
const { pending, queued } = result.data.result
return { pending: Number(pending), queued: Number(queued) }
}
function generateRandomAddress () {
return `0x${crypto.randomBytes(20).toString('hex')}`
}
function generateRandomAddressInShard (shard) {
const start = Number(shard.byte[0])
const end = Number(shard.byte[1])
let prefix = Math.floor(Math.random() * (end - start + 1) + start).toString(16)
// if prefix is only 1 character, add a 0 to the front
if (prefix.length === 1) {
prefix = '0' + prefix
}
let randomAddress = generateRandomAddress()
// replace first 4 characters with random number between start and end
randomAddress = randomAddress.replace(
randomAddress.substring(2, 4),
prefix
)
return randomAddress
}
function sleep (s) {
return new Promise((resolve) => setTimeout(resolve, s))
}
module.exports = {
generateRandomAddressInShard,
lookupChainId,
lookupTxPending,
nodeData,
networks,
sleep,
QUAI_CONTEXTS
}