@deeeed/hyperliquid-node20
Version:
Unofficial Hyperliquid API SDK for all major JS runtimes, written in TypeScript. Fork with Node.js 20.18.0+ compatibility.
55 lines (54 loc) • 2.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HyperliquidEventTarget = void 0;
const typescript_event_target_1 = require("typescript-event-target");
/** Listens for WebSocket messages and sends them as Hyperliquid typed events. */
class HyperliquidEventTarget extends typescript_event_target_1.TypedEventTarget {
constructor(socket) {
super();
socket.addEventListener("message", (event) => {
try {
const msg = JSON.parse(event.data);
if (isHyperliquidMsg(msg)) {
this.dispatchEvent(new CustomEvent(msg.channel, { detail: msg.data }));
}
else if (isExplorerBlockMsg(msg)) {
this.dispatchEvent(new CustomEvent("_explorerBlock", { detail: msg }));
}
else if (isExplorerTxsMsg(msg)) {
this.dispatchEvent(new CustomEvent("_explorerTxs", { detail: msg }));
}
}
catch {
// Ignore JSON parsing errors
}
});
}
}
exports.HyperliquidEventTarget = HyperliquidEventTarget;
/** Type guard for Hyperliquid messages. */
function isHyperliquidMsg(value) {
return typeof value === "object" && value !== null &&
"channel" in value && typeof value.channel === "string";
}
/** Type guard for explorer block messages. */
function isExplorerBlockMsg(value) {
return Array.isArray(value) && value.length > 0 &&
(typeof value[0] === "object" && value[0] !== null && !Array.isArray(value[0]) &&
"height" in value[0] && typeof value[0].height === "number" &&
"blockTime" in value[0] && typeof value[0].blockTime === "number" &&
"hash" in value[0] && typeof value[0].hash === "string" &&
"proposer" in value[0] && typeof value[0].proposer === "string" &&
"numTxs" in value[0] && typeof value[0].numTxs === "number");
}
/** Type guard for explorer transactions messages. */
function isExplorerTxsMsg(value) {
return Array.isArray(value) && value.length > 0 &&
(typeof value[0] === "object" && value[0] !== null && !Array.isArray(value[0]) &&
"action" in value[0] && typeof value[0].action === "object" && value[0].action !== null &&
"block" in value[0] && typeof value[0].block === "number" &&
"error" in value[0] && (typeof value[0].error === "string" || value[0].error === null) &&
"hash" in value[0] && typeof value[0].hash === "string" &&
"time" in value[0] && typeof value[0].time === "number" &&
"user" in value[0] && typeof value[0].user === "string");
}