-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript_unlocked_listener.js
84 lines (56 loc) · 1.77 KB
/
script_unlocked_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
require('dotenv').config()
const { Listener } = require("bsv-spv");
const { getChannel } = require('rabbi')
const name = "powco-unlocks";
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 });
async function publish(exchange, routingkey, json) {
const channel = await getChannel()
channel.publish(exchange, routingkey, Buffer.from(JSON.stringify(json)))
}
const onBlock = async ({
header,
started,
finished,
size,
height,
txCount,
transactions,
startDate,
}) => {
for (const [index, transaction, pos, len] of transactions) {
for (let input of transaction.inputs) {
const outpoint = `${input.prevTxId.toString('hex')}_${input.vout}`
const inpoint = `${transaction.getTxid()}_${input.vin}`
const unlock = {
lock: outpoint,
unlock: inpoint
}
await publish('powco', 'tx.unlock', unlock)
await publish('powco', `tx.unlock.${outpoint}`, unlock)
}
}
};
listener.on("mempool_tx", async ({ transaction, size }) => {
for (let input of tx.inputs) {
const outpoint = `${input.prevTxId.toString('hex')}_${input.vout}`
const inpoint = `${transaction.getTxid()}_${input.vin}`
const unlock = {
lock: outpoint,
unlock: inpoint
}
await publish('powco', 'tx.unlock', unlock)
await publish('powco', `tx.unlock.${outpoint}`, unlock)
}
});
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 });