-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathbitqueryTx.js
More file actions
93 lines (86 loc) · 2.03 KB
/
bitqueryTx.js
File metadata and controls
93 lines (86 loc) · 2.03 KB
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
import fetch from "node-fetch";
const BITQUERY_ENDPOINT = "https://graphql.bitquery.io";
export async function fetchBitcoinTransaction({
apiKey,
txHash,
network = "bitcoin",
limit = 10,
offset = 0
}) {
const query = `
query ($network: BitcoinNetwork!, $hash: String!, $limit: Int!, $offset: Int!) {
bitcoin(network: $network) {
transactions(txHash: {is: $hash}) {
block {
height
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
}
index
inputCount
outputCount
inputValue
input_value_usd: inputValue(in: USD)
outputValue
output_value_usd: outputValue(in: USD)
feeValue
fee_value_usd: feeValue(in: USD)
txSize
txVsize
txWeight
txVersion
}
outputs(
txHash: {is: $hash}
options: {asc: "outputIndex", limit: $limit, offset: $offset}
) {
outputIndex
address: outputAddress {
address
annotation
}
value
value_usd: value(in: USD)
outputDirection
}
inputs(
txHash: {is: $hash}
options: {asc: "inputIndex", limit: $limit, offset: $offset}
) {
inputIndex
address: inputAddress {
address
annotation
}
value
value_usd: value(in: USD)
}
}
}
`;
const res = await fetch(BITQUERY_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify({
query,
variables: {
network,
hash: txHash,
limit,
offset
}
})
});
if (!res.ok) {
throw new Error(`Bitquery API error: ${res.status}`);
}
const json = await res.json();
if (json.errors) {
throw new Error(JSON.stringify(json.errors, null, 2));
}
return json.data.bitcoin;
}