-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_listener.js
87 lines (57 loc) · 1.56 KB
/
run_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
require('dotenv').config()
const { Listener } = require("bsv-spv");
const name = "run-listener";
const ticker = "BSV";
const blockHeight = -10; // 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 { metadata } = require('run-sdk').util
const { connect } = require('amqplib')
var amqp;
async function startAmqp() {
const connection = await connect(process.env.amqp_url)
amqp = await connection.createChannel()
await amqp.assertExchange('powco')
}
startAmqp()
const onBlock = ({
header,
started,
finished,
size,
height,
txCount,
transactions,
startDate,
}) => {
for (const [index, tx, pos, len] of transactions) {
handleTransaction(tx)
}
};
listener.on("mempool_tx", async ({ transaction, size }) => {
handleTransaction(transaction)
})
async function handleTransaction(transaction) {
try {
const hex = transaction.toHex()
const txid = transaction.getTxid()
const runMetadata = metadata(hex)
if (runMetadata) {
console.log({ txid })
console.log(JSON.stringify(runMetadata))
}
amqp.publish('powco', 'run.transaction.discovered', Buffer.from(
JSON.stringify({ txid, runMetadata })
))
} catch(error) {
}
}
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 });