-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap-airdrops.js
104 lines (84 loc) · 2.64 KB
/
map-airdrops.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
'use strict';
const sqlite3 = require('sqlite3').verbose();
const AirdropProof = require('hsd/lib/primitives/airdropproof');
const { NodeClient } = require('hs-client');
// const START_HEIGHT = 2016;
// const END_HEIGHT = 2116;
const START_HEIGHT = 2016;
const END_HEIGHT = 93225;
const nodeOptions = {
port: 12037,
apiKey: 'api-key',
};
const nodeClient = new NodeClient(nodeOptions);
// Connect to Database and set up
// const db = new sqlite3.Database(':memory:');
const db = new sqlite3.Database('./airdrops.sqlite3');
db.serialize(function () {
db.run(
'CREATE TABLE IF NOT EXISTS airdrops (idx INTEGER, subidx INTEGER, type INTEGER, timestamp INTEGER, address TEXT, value INTEGER, block INTEGER, txid TEXT)'
);
});
let foundProofs = 0;
(async () => {
console.log(
`[*] Searching for airdrop proofs from block ${START_HEIGHT} to ${END_HEIGHT}.`
);
for (let height = START_HEIGHT; height <= END_HEIGHT; height++) {
console.log(`[${foundProofs}/217557] Processing block #${height}...`);
const block = await nodeClient.execute('getblockbyheight', [height]);
// console.log(block);
const cbTx = await nodeClient.getTX(block.tx[0]);
// console.log(cbTx);
if (cbTx.inputs.length === 1) continue;
for (const [inpIdx, input] of cbTx.inputs.entries()) {
// console.log(inpIdx, input);
const witness = input.witness?.[0];
if (witness?.length < 50) continue;
// console.log(witness);
try {
// Decode airdrop proof
let proof;
try {
proof = AirdropProof.decode(Buffer.from(witness, 'hex'));
if (!proof) continue;
} catch (error) {
console.log('Not an airdrop proof, moving on.');
continue;
}
const key = proof.getKey();
const value = proof.getValue();
console.log(
`Found proof for index ${proof.index} subindex ${proof.subindex}.`
);
foundProofs += 1;
// Save to Database
const airdropSQLStatement = db.prepare(
'INSERT INTO airdrops VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
);
airdropSQLStatement.run(
proof.index,
proof.subindex,
key.type,
block.time,
cbTx.outputs[inpIdx].address,
value,
height,
block.tx[0]
);
airdropSQLStatement.finalize();
} catch (error) {
console.error(error);
console.log(proof);
console.log(key);
throw error;
}
}
}
// db.all('SELECT * from airdrops', (err, rows) => {
// console.log(err);
// console.log(rows);
// });
db.close();
console.log('done.');
})();