Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions bitqueryTx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}