-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
89 lines (69 loc) · 2.07 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
const axios = require("axios");
const esploraUrl = "https://blockstream.info/liquid/api/";
const getVOuts = async (blockHash) => {
return axios.get(`${esploraUrl + "block/"}` + blockHash + "/txs/0").then((response) => {
const allTx = response.data;
const opReturnTxs = [];
allTx.forEach((tx) => {
if (tx.vout[0].scriptpubkey_type === "op_return")
opReturnTxs.push(
tx.vout.map((vo) => {
return {
scriptpubkey: vo.scriptpubkey,
value: vo.value,
asset: vo.asset,
};
})
);
});
return opReturnTxs;
});
};
const getLastBlockHeight = async () => {
return axios.get(`${esploraUrl + "blocks/tip/height"}`).then((response) => {
return response.data;
});
};
const getBlocks = async () => {
return axios.get(`${esploraUrl + "blocks"}`).then((response) => {
return response.data;
});
};
const getBlocksWithIndex = async (startHeight) => {
return axios
.get(`${esploraUrl}` + "blocks/" + startHeight)
.then((response) => {
return response.data;
})
.catch((err) => console.log(err));
};
const app = async () => {
let dummyInput = 1553646;
let blockPromises = [];
const lastBlockHeight = await getLastBlockHeight();
console.log(lastBlockHeight - dummyInput);
while (dummyInput <= lastBlockHeight) {
if (lastBlockHeight - dummyInput < 10) {
console.log("1");
blockPromises.push(getBlocks());
break;
} else {
console.log("2");
dummyInput += 9;
blockPromises.push(getBlocksWithIndex(dummyInput));
}
}
const unsortedBlocks = await Promise.all(blockPromises);
const mergedBlocks = [].concat.apply([], unsortedBlocks);
const blocks = mergedBlocks.sort((a, b) => a.height - b.height);
const txpromises = blocks.map(async (bl) => {
return getVOuts(bl.id);
});
const vouts = await Promise.all(txpromises);
const finalData = blocks.map((bl, index) => {
return { blockHeight: bl.height, txout: vouts[index] };
});
console.log(finalData);
return finalData;
};
app();