forked from xihucuyu888/get-block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
303 lines (285 loc) · 7.57 KB
/
index.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const axios = require('axios')
const moment = require('moment')
const Web3 = require('web3')
const { ApiPromise, WsProvider } = require('@polkadot/api')
const config = require('./config.json');
const BTCURL = config.BTCURL
const ETHURL = config.ETHURL
const STXURL = config.STXURL
const AVAXURL = config.AVAXURL
const MATICURL = config.MATICURL
const ATOMURL = config.ATOMURL
const LTCURL = config.LTCURL
const OPURL = config.OPURL
const ARBURL = config.ARBURL
const DOTURL = config.DOTURL
const XRPURL = config.XRPURL
let dotApi
const sleepSeconds = (s) => {
return new Promise((resolve) => setTimeout(resolve, s * 1000))
}
const initPolkadot = async () => {
if (!dotApi) {
const wsProvider = new WsProvider(DOTURL);
dotApi = await ApiPromise.create({ provider: wsProvider });
}
else {
return dotApi
}
}
const getDOTBlockResult = async (indexOrHash) => {
await initPolkadot()
const blockHash = (await dotApi.rpc.chain.getBlockHash(indexOrHash)).toString()
const result = await dotApi.rpc.chain.getBlock(blockHash)
const timestamp = parseInt(moment(result.block.extrinsics[0].method.args[0]).format('x'))
return timestamp
}
const getDOTlatestBlock = async () => {
await initPolkadot()
const header = await dotApi.rpc.chain.getHeader()
const block = header.number.toNumber()
return block
}
const getXRPblockResult = async (indexOrHash) => {
const data = {
"method": "ledger",
"params": [
{
"ledger_index": indexOrHash,
"accounts": false,
"full": false,
"transactions": false,
"expand": false,
"owner_funds": false
}
]
}
const res = (await axios.post(`${XRPURL}`,data)).data
const close_time_human = res.result.ledger.close_time_human
const timestamp = moment.utc(close_time_human, "YYYY-MMM-DD HH:mm:ss.SSSSSSSSS Z").valueOf();
return timestamp
}
const getXRPlatestBlock = async() => {
const data = {
"method": "server_state",
"params": [
{}
]
}
const res = (await axios.post(`${XRPURL}`,data)).data
const block = res.result.state.validated_ledger.seq
return block
}
const getATOMBlockResult = async (indexOrHash) => {
let data, timestamp
data = (await axios.get(`${ATOMURL}/blocks/${indexOrHash}`)).data
timestamp = parseInt(moment(data.block.header.time).format('x'))
return timestamp
}
const getATOMlatestBlock = async () => {
const re = await axios.get(`${ATOMURL}/blocks/latest`)
const block = re?.data.block.header.height
return block
}
const getBTCBlockResult = async (chain,indexOrHash) => {
let data, timestamp
let URL
switch (chain) {
case 'BTC':
URL = BTCURL
break
case 'LTC':
URL = LTCURL
break
}
if (typeof indexOrHash === 'number') {
data = (await axios.get(`${URL}/block/${indexOrHash}`)).data
timestamp = parseInt(moment(data.time).format('x'))
}
return {
hash: data.hash,
timestamp: timestamp
}
}
const getBTClatestBlock = async (chain) => {
let URL
switch (chain) {
case 'BTC':
URL = BTCURL
break
case 'LTC':
URL = LTCURL
break
}
const re = await axios.get(`${URL}/block/tip`)
const block = re?.data.height
return block
}
const getSTXlatestBlock = async () => {
const re = await axios.get(`${STXURL}/extended/v1/block?limit=1`)
console.log(re?.data.results[0].height)
return re?.data.results[0].height
}
const getSTXBlockResult = async (indexOrHash) => {
const re = await axios.get(`${STXURL}/extended/v1/block/by_height/${indexOrHash}`)
const block = re?.data
const timestamp = block.burn_block_time * 1000
return timestamp
}
const getETHBlockResult = async (chain, indexOrHash) => {
let URL
switch (chain) {
case 'ARB':
URL = ARBURL
break
case 'OP':
URL = OPURL
break
case 'AVAX':
URL = AVAXURL
break
case 'MATIC':
URL = MATICURL
break
case 'ETH':
URL = ETHURL
break
}
const web3 = new Web3(URL)
const block = await web3.eth.getBlock(indexOrHash, true)
const timestamp = block.timestamp * 1000
return timestamp
}
const getETHlatestBlock = async (chain) => {
let URL
switch (chain) {
case 'ARB':
URL = ARBURL
break
case 'OP':
URL = OPURL
break
case 'AVAX':
URL = AVAXURL
break
case 'MATIC':
URL = MATICURL
break
case 'ETH':
URL = ETHURL
break
}
const payload = {
jsonrpc: '2.0',
id: '1',
method: 'eth_getBlockByNumber',
params: [
'latest',
false
]
}
const re = await axios.post(URL, payload)
const block = re?.data.result.number
return parseInt(block)
}
const getLatestBlock = async (chain) => {
let blockNumber
switch (chain) {
case 'OP':
case 'ARB':
case 'AVAX':
case 'MATIC':
case 'ETH':
blockNumber = await getETHlatestBlock(chain)
break
case 'BTC':
case 'LTC':
blockNumber = await getBTClatestBlock(chain)
break
case 'STX':
blockNumber = await getSTXlatestBlock()
break
case 'ATOM':
blockNumber = await getATOMlatestBlock()
break
case 'DOT':
blockNumber = await getDOTlatestBlock()
break
case 'XRP':
blockNumber = await getXRPlatestBlock()
break
}
return blockNumber
}
const getBlock = async (chain, block) => {
let timestamp
switch (chain) {
case 'STX':
timestamp = await getSTXBlockResult(block)
break
case 'BTC':
case 'LTC':
timestamp = (await getBTCBlockResult(chain,block)).timestamp
break
case 'ATOM':
timestamp = (await getATOMBlockResult(block))
break
case 'OP':
case 'ARB':
case 'AVAX':
case 'MATIC':
case 'ETH':
timestamp = await getETHBlockResult(chain, block)
break
case 'DOT':
timestamp = await getDOTBlockResult(block)
break
case 'XRP':
timestamp = await getXRPblockResult(block)
break
}
return timestamp
}
const getBlockNumber = async (chain, timestamp, left = 100000, right) => {
right = right || await getLatestBlock(chain)
while (left <= right) {
const mid = left + ((right - left) >> 1)
await sleepSeconds(0.01)
const midTime = await getBlock(chain, mid)
if (midTime === timestamp) {
const midDay = moment(midTime).format('YYYY-MM-DD HH:mm:ss')
console.log(chain, `mid=${mid}`, `time=${midDay}`)
return mid
} else if (midTime > timestamp) {
right = mid - 1
} else {
left = mid + 1
}
}
const leftTime = await getBlock(chain, left)
const rightTime = await getBlock(chain, right)
const leftDay = moment(leftTime).format('YYYY-MM-DD HH:mm:ss')
const rightDay = moment(rightTime).format('YYYY-MM-DD HH:mm:ss')
console.log(chain, `${left}`, `time=${leftDay}`)
console.log(chain, `${right}`, `time=${rightDay}`)
return left
}
if (require.main === module) {
const test = async () => {
const date = new Date('2023-11-17 13:00:00')
const timestamp = date.getTime()
console.log('timestamp', timestamp)
//await getBlockNumber('ETH', timestamp, 33907882)
//await getBlockNumber('AVAX', timestamp, 27669731)
//await getBlockNumber('MATIC', timestamp, 39963387)
//await getBlockNumber('BTC', timestamp, 33907882)
//await getBlockNumber('LTC', timestamp, 2941117)
//await getBlockNumber('STX', timestamp, 10000)
//await getBlockNumber('ATOM', timestamp, 18755300)
//await getBlockNumber('DOT', timestamp, 18044162)
//await getBlockNumber('ARB', timestamp, 10000)
await getBlockNumber('XRP', timestamp, 42954049)
}
test()
}
// ('AVAX', timestamp, 20956326) 填入timestamp之前的blockNumber