-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmempool.js
60 lines (49 loc) · 1.3 KB
/
mempool.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
/* eslint-disable import/first */
/* eslint-disable import/order */
const { OuroborosClient } = require("@stricahq/ouroboros-network-js");
const cbors = require("@stricahq/cbors");
const BabbageParser = require("@stricahq/cardano-codec").babbage;
const client = new OuroborosClient({
protocolId: 32781,
protocolMagic: 764824073,
});
// connect to cardano node
// client.connect();
function acquireSnapshot() {
client.LocalTxMonitor.acquireSnapshot();
}
function requestNextTx() {
client.LocalTxMonitor.requestNextTx();
}
client.on("connect", () => {
acquireSnapshot();
});
client.on("disconnect", () => {
console.log("socket disconnected");
process.exit();
});
async function onData(data) {
if (data.acquired) {
requestNextTx();
}
if (data.await) {
console.log("Snapshot awaiting");
}
if (data.nextTx) {
try {
const decodedTx = cbors.Decoder.decode(data.nextTx).value;
const txBody = decodedTx[0];
const transaction = BabbageParser.parseTransaction(txBody, data.nextTx);
console.log(transaction.hash);
console.log(transaction);
} catch (error) {
console.log(error);
}
requestNextTx();
}
if (data.nextTx == null) {
console.log("Requesting next snapshot");
acquireSnapshot();
}
}
client.LocalTxMonitor.on("data", onData);