-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (105 loc) · 3.67 KB
/
index.js
File metadata and controls
124 lines (105 loc) · 3.67 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const fs = require("node:fs");
const path = require("node:path");
const xrpl = require("xrpl");
const { normalizeTransactionMessage } = require("./lib/normalizeTransactionMessage");
const client = new xrpl.Client("wss://honeycluster.io");
async function handleTransaction(transaction) {
if (!normalizeTransactionMessage(transaction)) {
return;
}
const txType = transaction.tx_json.TransactionType;
const filePath = path.join(__dirname, "TransactionTypes", `${txType}.js`);
try {
// Check if a handler file exists for this transaction type
if (fs.existsSync(filePath)) {
// Dynamically require the file
const transactionHandler = require(filePath);
// Call the handler function and pass the transaction data
if (typeof transactionHandler === "function") {
const response = await transactionHandler(transaction, client);
console.log(JSON.stringify(response, null, 2));
} else {
console.error(
`Handler for ${txType} does not export a function`,
);
}
} else {
// console.debug(`No handler file found for transaction type: ${txType}`);
}
} catch (error) {
console.error(`Error processing ${txType} transaction:`, {
error: error.message,
transaction: transaction.tx_json,
stack: error.stack,
});
}
}
async function startClient() {
try {
await client.connect();
console.log("Connected to XRPL WebSocket");
// Subscribe to transactions
await client.request({
command: "subscribe",
streams: ["transactions"],
});
// Event listener for incoming transactions
client.on("transaction", handleTransaction);
} catch (error) {
console.error("Connection error: ", error);
reconnectClient();
}
}
// Add graceful shutdown handling
async function gracefulShutdown(signal) {
console.log(`Received ${signal}. Starting graceful shutdown...`);
try {
// Disconnect from XRPL
if (client.isConnected()) {
await client.disconnect();
console.log("Disconnected from XRPL");
}
process.exit(0);
} catch (error) {
console.error("Error during shutdown:", error);
process.exit(1);
}
}
// Enhanced reconnect client function
async function reconnectClient() {
console.log("Initiating reconnection to XRPL...");
try {
if (client.isConnected()) {
await client.disconnect();
console.debug(
"Successfully disconnected before reconnection attempt",
);
}
} catch (error) {
console.warn("Error during disconnect before reconnection:", error);
}
setTimeout(() => startClient(), 5000);
}
// Add these event listeners at the end of the file
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
process.on("SIGINT", () => gracefulShutdown("SIGINT"));
// Handle uncaught exceptions and unhandled rejections
process.on("uncaughtException", (error) => {
console.error("Uncaught Exception:", error);
gracefulShutdown("uncaughtException");
});
process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason);
});
// Start the client
startClient();
// Handle disconnects
client.on("disconnected", (code) => {
console.error(`Disconnected from XRPL with code: ${code}`);
reconnectClient();
});
// Handle any error events
client.on("error", (error) => {
console.error("WebSocket error:", error);
reconnectClient();
});