-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboostpow_listener.js
89 lines (61 loc) · 1.77 KB
/
boostpow_listener.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 { Listener } = require("bsv-spv");
const axios = require('axios')
const name = "boostpow-listener-best";
const ticker = "BSV";
const blockHeight = -6 * 24 * 7; // Number. If negative then it's number from the tip.
const dataDir = __dirname;
const port = 8080; // Same as Masters port above
const listener = new Listener({ name, ticker, blockHeight, dataDir });
const {BoostPowJob, BoostPowJobProof} = require('boostpow')
async function handleTransaction(tx) {
let hex = tx.toHex()
const job = BoostPowJob.fromRawTransaction(hex)
if (job) {
console.log('boostpow.job.discovered', job)
const { data } = await axios.get(`https://pow.co/api/v1/boost/jobs/${job.txid}`)
//
console.log(data)
}
const proof = BoostPowJobProof.fromRawTransaction(hex)
if (proof) {
console.log('boostpow.proof.discovered', proof)
const { data } = await axios.post(`https://pow.co/api/v1/boost/proofs/${proof.txid}`)
//
console.log(data)
}
}
const onBlock = ({
header,
started,
finished,
size,
height,
txCount,
transactions,
startDate,
}) => {
for (const [index, tx, pos, len] of transactions) {
handleTransaction(tx)
}
};
listener.on("mempool_tx", ({ transaction, size }) => {
handleTransaction(transaction)
/*let hex = transaction.toHex()
const job = BoostPowJob.fromRawTransaction(hex)
if (job) {
console.log('boostpow.job.discovered', job)
}
const proof = BoostPowJobProof.fromRawTransaction(hex)
if (proof) {
console.log('boostpow.proof.discovered', proof)
}
*/
});
listener.on("block_reorg", ({ height, hash }) => {
// Re-org after height
});
listener.on("block_saved", ({ height, hash }) => {
listener.syncBlocks(onBlock);
});
listener.syncBlocks(onBlock);
listener.connect({ port });