-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockchair.js
79 lines (72 loc) · 1.86 KB
/
blockchair.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
const bcUrl = 'https://api.blockchair.com/'
const axios = require('axios')
let last_req_time = 0
async function api(network, endpoint, data = null)
{
let time = Date.now()
if (time - last_req_time < 60000) {
// blockchair without api key allows a daily average of 1 req/minute, reset at midnight utc
await new Promise(resolve => setTimeout(resolve, last_req_time + 60000 - time))
}
const url = bcUrl + network + endpoint
let res
while (true) {
try {
last_req_time = Date.now()
if (data) {
res = (await axios.post(url, data)).data
} else {
res = (await axios.get(url)).data
}
break
} catch(e) {
if (e.code == 'EAI_AGAIN') {
process.stderr.write('... network interruption ...\n')
await new Promise(resolve => setTimeout(resolve, 1000))
continue
}
if (e.response.data.context) {
res = e.response.data
break
}
let error = new Error(e)
error.code = 'UnknownError'
error.original = e
throw error
}
}
if (res.context.code === 200) { return res.data }
let error = new Error(res.context.error)
error.original = res.context
if (res.context.error.startsWith('Invalid transaction')) {
error.code = 'InvalidData'
} else {
error.code = 'UnknownError'
}
throw error
}
async function broadcast(tx, network = 'bitcoin-sv')
{
if (tx.toString().match(/[^0-9a-fA-F\s]/)) {
tx = tx.toString('hex')
} else {
tx = tx.toString()
}
let res = await api(network, '/push/transaction', 'data=' + tx)
return res.transaction_hash
}
async function priority(txid, network = 'bitcoin-sv')
{
let res = await api(network, '/dashboards/transaction/' + txid + '/priority')
return res[txid].priority
}
async function getTX(txid, network = 'bitcoin-sv')
{
let res = await api(network, '/raw/transaction/' + txid)
return res[txid].raw_transaction
}
module.exports = {
broadcast: broadcast,
priority: priority,
getTX: getTX
}