@xapy/orderbook 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/exchanges/binance/index.cjs +605 -0
- package/dist/exchanges/binance/index.cjs.map +1 -0
- package/dist/exchanges/binance/index.d.cts +60 -0
- package/dist/exchanges/binance/index.d.ts +60 -0
- package/dist/exchanges/binance/index.js +601 -0
- package/dist/exchanges/binance/index.js.map +1 -0
- package/dist/exchanges/bingx/index.cjs +2 -1
- package/dist/exchanges/bingx/index.cjs.map +1 -1
- package/dist/exchanges/bingx/index.d.cts +1 -1
- package/dist/exchanges/bingx/index.d.ts +1 -1
- package/dist/exchanges/bingx/index.js +2 -1
- package/dist/exchanges/bingx/index.js.map +1 -1
- package/dist/exchanges/bitget/index.cjs +2 -1
- package/dist/exchanges/bitget/index.cjs.map +1 -1
- package/dist/exchanges/bitget/index.d.cts +1 -1
- package/dist/exchanges/bitget/index.d.ts +1 -1
- package/dist/exchanges/bitget/index.js +2 -1
- package/dist/exchanges/bitget/index.js.map +1 -1
- package/dist/exchanges/bybit/index.cjs +2 -1
- package/dist/exchanges/bybit/index.cjs.map +1 -1
- package/dist/exchanges/bybit/index.d.cts +1 -1
- package/dist/exchanges/bybit/index.d.ts +1 -1
- package/dist/exchanges/bybit/index.js +2 -1
- package/dist/exchanges/bybit/index.js.map +1 -1
- package/dist/exchanges/coinex/index.cjs +2 -1
- package/dist/exchanges/coinex/index.cjs.map +1 -1
- package/dist/exchanges/coinex/index.d.cts +1 -1
- package/dist/exchanges/coinex/index.d.ts +1 -1
- package/dist/exchanges/coinex/index.js +2 -1
- package/dist/exchanges/coinex/index.js.map +1 -1
- package/dist/exchanges/gate/index.cjs +2 -1
- package/dist/exchanges/gate/index.cjs.map +1 -1
- package/dist/exchanges/gate/index.d.cts +1 -1
- package/dist/exchanges/gate/index.d.ts +1 -1
- package/dist/exchanges/gate/index.js +2 -1
- package/dist/exchanges/gate/index.js.map +1 -1
- package/dist/exchanges/huobi/index.cjs +2 -1
- package/dist/exchanges/huobi/index.cjs.map +1 -1
- package/dist/exchanges/huobi/index.d.cts +1 -1
- package/dist/exchanges/huobi/index.d.ts +1 -1
- package/dist/exchanges/huobi/index.js +2 -1
- package/dist/exchanges/huobi/index.js.map +1 -1
- package/dist/exchanges/hyperliquid/index.cjs +536 -0
- package/dist/exchanges/hyperliquid/index.cjs.map +1 -0
- package/dist/exchanges/hyperliquid/index.d.cts +45 -0
- package/dist/exchanges/hyperliquid/index.d.ts +45 -0
- package/dist/exchanges/hyperliquid/index.js +532 -0
- package/dist/exchanges/hyperliquid/index.js.map +1 -0
- package/dist/exchanges/kucoin/index.cjs +614 -0
- package/dist/exchanges/kucoin/index.cjs.map +1 -0
- package/dist/exchanges/kucoin/index.d.cts +60 -0
- package/dist/exchanges/kucoin/index.d.ts +60 -0
- package/dist/exchanges/kucoin/index.js +609 -0
- package/dist/exchanges/kucoin/index.js.map +1 -0
- package/dist/exchanges/okx/index.cjs +2 -1
- package/dist/exchanges/okx/index.cjs.map +1 -1
- package/dist/exchanges/okx/index.d.cts +1 -1
- package/dist/exchanges/okx/index.d.ts +1 -1
- package/dist/exchanges/okx/index.js +2 -1
- package/dist/exchanges/okx/index.js.map +1 -1
- package/dist/index.cjs +627 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +625 -2
- package/dist/index.js.map +1 -1
- package/dist/{stream-BKpWmRYN.d.cts → stream-C73HPYqT.d.cts} +1 -1
- package/dist/{stream-BKpWmRYN.d.ts → stream-C73HPYqT.d.ts} +1 -1
- package/package.json +34 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { B as BookEvent, O as OrderbookLevel, M as Market } from './stream-
|
|
2
|
-
export { C as ClientOptions, E as ExchangeName, a as Orderbook, b as OrderbookOptions, c as OrderbookStream, S as Side, d as StreamEvents } from './stream-
|
|
1
|
+
import { B as BookEvent, O as OrderbookLevel, M as Market } from './stream-C73HPYqT.cjs';
|
|
2
|
+
export { C as ClientOptions, E as ExchangeName, a as Orderbook, b as OrderbookOptions, c as OrderbookStream, S as Side, d as StreamEvents } from './stream-C73HPYqT.cjs';
|
|
3
3
|
export { BybitClient } from './exchanges/bybit/index.cjs';
|
|
4
4
|
export { BitgetClient } from './exchanges/bitget/index.cjs';
|
|
5
5
|
export { GateClient } from './exchanges/gate/index.cjs';
|
|
@@ -7,6 +7,9 @@ export { BingxClient } from './exchanges/bingx/index.cjs';
|
|
|
7
7
|
export { HuobiClient } from './exchanges/huobi/index.cjs';
|
|
8
8
|
export { CoinexClient } from './exchanges/coinex/index.cjs';
|
|
9
9
|
export { OkxClient } from './exchanges/okx/index.cjs';
|
|
10
|
+
export { BinanceClient } from './exchanges/binance/index.cjs';
|
|
11
|
+
export { HyperliquidClient } from './exchanges/hyperliquid/index.cjs';
|
|
12
|
+
export { KucoinClient } from './exchanges/kucoin/index.cjs';
|
|
10
13
|
|
|
11
14
|
declare class ExchangeError extends Error {
|
|
12
15
|
readonly exchange: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { B as BookEvent, O as OrderbookLevel, M as Market } from './stream-
|
|
2
|
-
export { C as ClientOptions, E as ExchangeName, a as Orderbook, b as OrderbookOptions, c as OrderbookStream, S as Side, d as StreamEvents } from './stream-
|
|
1
|
+
import { B as BookEvent, O as OrderbookLevel, M as Market } from './stream-C73HPYqT.js';
|
|
2
|
+
export { C as ClientOptions, E as ExchangeName, a as Orderbook, b as OrderbookOptions, c as OrderbookStream, S as Side, d as StreamEvents } from './stream-C73HPYqT.js';
|
|
3
3
|
export { BybitClient } from './exchanges/bybit/index.js';
|
|
4
4
|
export { BitgetClient } from './exchanges/bitget/index.js';
|
|
5
5
|
export { GateClient } from './exchanges/gate/index.js';
|
|
@@ -7,6 +7,9 @@ export { BingxClient } from './exchanges/bingx/index.js';
|
|
|
7
7
|
export { HuobiClient } from './exchanges/huobi/index.js';
|
|
8
8
|
export { CoinexClient } from './exchanges/coinex/index.js';
|
|
9
9
|
export { OkxClient } from './exchanges/okx/index.js';
|
|
10
|
+
export { BinanceClient } from './exchanges/binance/index.js';
|
|
11
|
+
export { HyperliquidClient } from './exchanges/hyperliquid/index.js';
|
|
12
|
+
export { KucoinClient } from './exchanges/kucoin/index.js';
|
|
10
13
|
|
|
11
14
|
declare class ExchangeError extends Error {
|
|
12
15
|
readonly exchange: string;
|
package/dist/index.js
CHANGED
|
@@ -356,7 +356,8 @@ var WSClient = class extends TypedEmitter {
|
|
|
356
356
|
async connect() {
|
|
357
357
|
if (this.closed) return;
|
|
358
358
|
const WS = await getWebSocketCtor();
|
|
359
|
-
const
|
|
359
|
+
const url = typeof this.cfg.url === "function" ? await this.cfg.url() : this.cfg.url;
|
|
360
|
+
const ws = new WS(url);
|
|
360
361
|
ws.binaryType = "arraybuffer";
|
|
361
362
|
this.ws = ws;
|
|
362
363
|
ws.onopen = async () => {
|
|
@@ -1912,6 +1913,628 @@ var OkxClient = class {
|
|
|
1912
1913
|
}
|
|
1913
1914
|
};
|
|
1914
1915
|
|
|
1915
|
-
|
|
1916
|
+
// src/exchanges/binance/symbols.ts
|
|
1917
|
+
var QUOTES5 = [
|
|
1918
|
+
"USDT",
|
|
1919
|
+
"USDC",
|
|
1920
|
+
"FDUSD",
|
|
1921
|
+
"BUSD",
|
|
1922
|
+
"TUSD",
|
|
1923
|
+
"USD",
|
|
1924
|
+
"BTC",
|
|
1925
|
+
"ETH",
|
|
1926
|
+
"BNB"
|
|
1927
|
+
];
|
|
1928
|
+
function toBinanceSymbol(symbol) {
|
|
1929
|
+
const [base, quote] = symbol.split("/");
|
|
1930
|
+
if (!base || !quote) {
|
|
1931
|
+
throw new Error(`invalid symbol "${symbol}" \u2014 expected "BASE/QUOTE"`);
|
|
1932
|
+
}
|
|
1933
|
+
return `${base}${quote}`.toUpperCase();
|
|
1934
|
+
}
|
|
1935
|
+
function fromBinanceSymbol(s) {
|
|
1936
|
+
const upper = s.toUpperCase();
|
|
1937
|
+
for (const q of QUOTES5) {
|
|
1938
|
+
if (upper.endsWith(q) && upper.length > q.length) {
|
|
1939
|
+
return `${upper.slice(0, -q.length)}/${q}`;
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
return upper;
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
// src/exchanges/binance/rest.ts
|
|
1946
|
+
var BASE_SPOT = "https://api.binance.com";
|
|
1947
|
+
var BASE_FAPI = "https://fapi.binance.com";
|
|
1948
|
+
async function fetchBinanceOrderbook(opts) {
|
|
1949
|
+
const symbol = toBinanceSymbol(opts.symbol);
|
|
1950
|
+
const limit = opts.depth ?? 1e3;
|
|
1951
|
+
const url = opts.market === "spot" ? `${BASE_SPOT}/api/v3/depth?symbol=${symbol}&limit=${limit}` : `${BASE_FAPI}/fapi/v1/depth?symbol=${symbol}&limit=${limit}`;
|
|
1952
|
+
const data = await httpJson({
|
|
1953
|
+
url,
|
|
1954
|
+
timeoutMs: opts.timeoutMs,
|
|
1955
|
+
transformUrl: opts.transformUrl
|
|
1956
|
+
});
|
|
1957
|
+
return {
|
|
1958
|
+
exchange: "binance",
|
|
1959
|
+
symbol: fromBinanceSymbol(symbol),
|
|
1960
|
+
market: opts.market,
|
|
1961
|
+
bids: data.bids.map(([p, s]) => ({ price: Number(p), size: Number(s) })),
|
|
1962
|
+
asks: data.asks.map(([p, s]) => ({ price: Number(p), size: Number(s) })),
|
|
1963
|
+
timestamp: data.E ?? data.T ?? Date.now(),
|
|
1964
|
+
sequence: data.lastUpdateId
|
|
1965
|
+
};
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
// src/exchanges/binance/ws.ts
|
|
1969
|
+
var WS_SPOT6 = "wss://stream.binance.com:9443/ws";
|
|
1970
|
+
var WS_FUTURES3 = "wss://fstream.binance.com/ws";
|
|
1971
|
+
function streamBinanceOrderbook(opts) {
|
|
1972
|
+
const symbol = toBinanceSymbol(opts.symbol);
|
|
1973
|
+
const isSpot = opts.market === "spot";
|
|
1974
|
+
const interval = opts.interval ?? "100ms";
|
|
1975
|
+
const wsBase = isSpot ? WS_SPOT6 : WS_FUTURES3;
|
|
1976
|
+
const wsUrl = `${wsBase}/${symbol.toLowerCase()}@depth@${interval}`;
|
|
1977
|
+
const merger = new BookMerger();
|
|
1978
|
+
let snapshotId = -1;
|
|
1979
|
+
let lastU = -1;
|
|
1980
|
+
let snapshotInFlight = false;
|
|
1981
|
+
let snapshotDone = false;
|
|
1982
|
+
let buffered = [];
|
|
1983
|
+
let recovering = false;
|
|
1984
|
+
let ws = null;
|
|
1985
|
+
const stream = new OrderbookStream(() => ws?.close());
|
|
1986
|
+
const emit = (r) => {
|
|
1987
|
+
const book = {
|
|
1988
|
+
exchange: "binance",
|
|
1989
|
+
symbol: fromBinanceSymbol(symbol),
|
|
1990
|
+
market: opts.market,
|
|
1991
|
+
bids: r.bids,
|
|
1992
|
+
asks: r.asks,
|
|
1993
|
+
timestamp: r.timestamp,
|
|
1994
|
+
sequence: r.sequence
|
|
1995
|
+
};
|
|
1996
|
+
stream.emit("update", book);
|
|
1997
|
+
};
|
|
1998
|
+
const triggerSnapshot = () => {
|
|
1999
|
+
if (snapshotInFlight) return;
|
|
2000
|
+
snapshotInFlight = true;
|
|
2001
|
+
fetchBinanceOrderbook({
|
|
2002
|
+
symbol: opts.symbol,
|
|
2003
|
+
market: opts.market,
|
|
2004
|
+
depth: opts.depth ?? 1e3,
|
|
2005
|
+
timeoutMs: opts.timeoutMs,
|
|
2006
|
+
transformUrl: opts.transformUrl
|
|
2007
|
+
}).then((snap) => {
|
|
2008
|
+
snapshotId = snap.sequence;
|
|
2009
|
+
const r = merger.apply({
|
|
2010
|
+
kind: "snapshot",
|
|
2011
|
+
bids: snap.bids,
|
|
2012
|
+
asks: snap.asks,
|
|
2013
|
+
sequence: snap.sequence,
|
|
2014
|
+
timestamp: snap.timestamp
|
|
2015
|
+
});
|
|
2016
|
+
if (r.ok) emit(r);
|
|
2017
|
+
snapshotDone = true;
|
|
2018
|
+
lastU = -1;
|
|
2019
|
+
const pending2 = buffered;
|
|
2020
|
+
buffered = [];
|
|
2021
|
+
for (const ev of pending2) processDelta(ev);
|
|
2022
|
+
}).catch((err) => stream.emit("error", err)).finally(() => {
|
|
2023
|
+
snapshotInFlight = false;
|
|
2024
|
+
});
|
|
2025
|
+
};
|
|
2026
|
+
const resync = () => {
|
|
2027
|
+
if (recovering) return;
|
|
2028
|
+
recovering = true;
|
|
2029
|
+
merger.reset();
|
|
2030
|
+
snapshotId = -1;
|
|
2031
|
+
snapshotDone = false;
|
|
2032
|
+
lastU = -1;
|
|
2033
|
+
buffered = [];
|
|
2034
|
+
setTimeout(() => {
|
|
2035
|
+
recovering = false;
|
|
2036
|
+
triggerSnapshot();
|
|
2037
|
+
}, 100);
|
|
2038
|
+
};
|
|
2039
|
+
const processDelta = (ev) => {
|
|
2040
|
+
if (ev.u < snapshotId + 1) return;
|
|
2041
|
+
const bids = ev.b.map(([p, s]) => ({
|
|
2042
|
+
price: Number(p),
|
|
2043
|
+
size: Number(s)
|
|
2044
|
+
}));
|
|
2045
|
+
const asks = ev.a.map(([p, s]) => ({
|
|
2046
|
+
price: Number(p),
|
|
2047
|
+
size: Number(s)
|
|
2048
|
+
}));
|
|
2049
|
+
let prevSequence;
|
|
2050
|
+
if (lastU < 0) {
|
|
2051
|
+
const overlap = isSpot ? ev.U <= snapshotId + 1 : ev.U <= snapshotId;
|
|
2052
|
+
if (!overlap) {
|
|
2053
|
+
resync();
|
|
2054
|
+
return;
|
|
2055
|
+
}
|
|
2056
|
+
prevSequence = snapshotId;
|
|
2057
|
+
} else if (isSpot) {
|
|
2058
|
+
prevSequence = ev.U - 1;
|
|
2059
|
+
} else {
|
|
2060
|
+
if (ev.pu === void 0) {
|
|
2061
|
+
resync();
|
|
2062
|
+
return;
|
|
2063
|
+
}
|
|
2064
|
+
prevSequence = ev.pu;
|
|
2065
|
+
}
|
|
2066
|
+
const r = merger.apply({
|
|
2067
|
+
kind: "delta",
|
|
2068
|
+
bids,
|
|
2069
|
+
asks,
|
|
2070
|
+
sequence: ev.u,
|
|
2071
|
+
prevSequence,
|
|
2072
|
+
timestamp: ev.E
|
|
2073
|
+
});
|
|
2074
|
+
if (r.ok) {
|
|
2075
|
+
lastU = ev.u;
|
|
2076
|
+
emit(r);
|
|
2077
|
+
} else if (r.reason === "gap" || r.reason === "no-snapshot") {
|
|
2078
|
+
resync();
|
|
2079
|
+
}
|
|
2080
|
+
};
|
|
2081
|
+
const onMessage = (raw) => {
|
|
2082
|
+
if (typeof raw !== "string") return;
|
|
2083
|
+
let msg;
|
|
2084
|
+
try {
|
|
2085
|
+
msg = JSON.parse(raw);
|
|
2086
|
+
} catch {
|
|
2087
|
+
return;
|
|
2088
|
+
}
|
|
2089
|
+
if (msg.e !== "depthUpdate") return;
|
|
2090
|
+
if (!snapshotDone) {
|
|
2091
|
+
buffered.push(msg);
|
|
2092
|
+
if (!snapshotInFlight) triggerSnapshot();
|
|
2093
|
+
return;
|
|
2094
|
+
}
|
|
2095
|
+
processDelta(msg);
|
|
2096
|
+
};
|
|
2097
|
+
ws = new WSClient({
|
|
2098
|
+
url: wsUrl,
|
|
2099
|
+
onOpen: () => {
|
|
2100
|
+
merger.reset();
|
|
2101
|
+
snapshotId = -1;
|
|
2102
|
+
snapshotDone = false;
|
|
2103
|
+
lastU = -1;
|
|
2104
|
+
buffered = [];
|
|
2105
|
+
},
|
|
2106
|
+
onMessage
|
|
2107
|
+
// Binance server-initiated WS-level ping; auto-handled by the WS impl.
|
|
2108
|
+
});
|
|
2109
|
+
ws.on("open", () => stream.emit("connected"));
|
|
2110
|
+
ws.on("close", (r) => stream.emit("disconnected", r));
|
|
2111
|
+
ws.on("reconnecting", (a, w) => stream.emit("reconnecting", a, w));
|
|
2112
|
+
ws.on("error", (e) => stream.emit("error", e));
|
|
2113
|
+
ws.connect().catch((e) => stream.emit("error", e));
|
|
2114
|
+
return stream;
|
|
2115
|
+
}
|
|
2116
|
+
|
|
2117
|
+
// src/exchanges/binance/index.ts
|
|
2118
|
+
var BinanceClient = class {
|
|
2119
|
+
exchange = "binance";
|
|
2120
|
+
transformUrl;
|
|
2121
|
+
timeoutMs;
|
|
2122
|
+
constructor(opts = {}) {
|
|
2123
|
+
this.transformUrl = opts.transformUrl;
|
|
2124
|
+
this.timeoutMs = opts.timeoutMs;
|
|
2125
|
+
}
|
|
2126
|
+
fetchOrderbook(symbol, opts = {}) {
|
|
2127
|
+
const { pair, market } = parseSymbol(symbol);
|
|
2128
|
+
return fetchBinanceOrderbook({
|
|
2129
|
+
symbol: pair,
|
|
2130
|
+
market,
|
|
2131
|
+
depth: opts.depth,
|
|
2132
|
+
timeoutMs: this.timeoutMs,
|
|
2133
|
+
transformUrl: this.transformUrl
|
|
2134
|
+
});
|
|
2135
|
+
}
|
|
2136
|
+
streamOrderbook(symbol, opts = {}) {
|
|
2137
|
+
const { pair, market } = parseSymbol(symbol);
|
|
2138
|
+
return streamBinanceOrderbook({
|
|
2139
|
+
symbol: pair,
|
|
2140
|
+
market,
|
|
2141
|
+
depth: opts.depth,
|
|
2142
|
+
transformUrl: this.transformUrl,
|
|
2143
|
+
timeoutMs: this.timeoutMs
|
|
2144
|
+
});
|
|
2145
|
+
}
|
|
2146
|
+
};
|
|
2147
|
+
|
|
2148
|
+
// src/exchanges/hyperliquid/symbols.ts
|
|
2149
|
+
function toHyperliquidSymbol(symbol) {
|
|
2150
|
+
const [base, quote] = symbol.split("/");
|
|
2151
|
+
if (!base || !quote) {
|
|
2152
|
+
throw new Error(`invalid symbol "${symbol}" \u2014 expected "BASE/QUOTE"`);
|
|
2153
|
+
}
|
|
2154
|
+
if (quote.toUpperCase() !== "USDC") {
|
|
2155
|
+
throw new Error(
|
|
2156
|
+
`hyperliquid only supports USDC-quoted pairs; got "${quote}"`
|
|
2157
|
+
);
|
|
2158
|
+
}
|
|
2159
|
+
return base.toUpperCase();
|
|
2160
|
+
}
|
|
2161
|
+
function fromHyperliquidSymbol(coin) {
|
|
2162
|
+
return `${coin.toUpperCase()}/USDC`;
|
|
2163
|
+
}
|
|
2164
|
+
|
|
2165
|
+
// src/exchanges/hyperliquid/rest.ts
|
|
2166
|
+
var BASE7 = "https://api.hyperliquid.xyz";
|
|
2167
|
+
async function fetchHyperliquidOrderbook(opts) {
|
|
2168
|
+
if (opts.market !== "perpetual") {
|
|
2169
|
+
throw new Error(
|
|
2170
|
+
"hyperliquid: only perpetual markets are supported (spot uses @N-indexed coins)"
|
|
2171
|
+
);
|
|
2172
|
+
}
|
|
2173
|
+
const coin = toHyperliquidSymbol(opts.symbol);
|
|
2174
|
+
const data = await httpJson({
|
|
2175
|
+
url: `${BASE7}/info`,
|
|
2176
|
+
method: "POST",
|
|
2177
|
+
headers: { "Content-Type": "application/json" },
|
|
2178
|
+
body: JSON.stringify({ type: "l2Book", coin }),
|
|
2179
|
+
timeoutMs: opts.timeoutMs,
|
|
2180
|
+
transformUrl: opts.transformUrl
|
|
2181
|
+
});
|
|
2182
|
+
return {
|
|
2183
|
+
exchange: "hyperliquid",
|
|
2184
|
+
symbol: fromHyperliquidSymbol(data.coin),
|
|
2185
|
+
market: "perpetual",
|
|
2186
|
+
bids: data.levels[0].map((l) => ({
|
|
2187
|
+
price: Number(l.px),
|
|
2188
|
+
size: Number(l.sz)
|
|
2189
|
+
})),
|
|
2190
|
+
asks: data.levels[1].map((l) => ({
|
|
2191
|
+
price: Number(l.px),
|
|
2192
|
+
size: Number(l.sz)
|
|
2193
|
+
})),
|
|
2194
|
+
timestamp: data.time,
|
|
2195
|
+
sequence: data.time
|
|
2196
|
+
};
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
// src/exchanges/hyperliquid/ws.ts
|
|
2200
|
+
var WS_URL = "wss://api.hyperliquid.xyz/ws";
|
|
2201
|
+
function streamHyperliquidOrderbook(opts) {
|
|
2202
|
+
if (opts.market !== "perpetual") {
|
|
2203
|
+
const s = new OrderbookStream(() => void 0);
|
|
2204
|
+
queueMicrotask(
|
|
2205
|
+
() => s.emit(
|
|
2206
|
+
"error",
|
|
2207
|
+
new Error(
|
|
2208
|
+
"hyperliquid: only perpetual markets are supported (spot uses @N-indexed coins)"
|
|
2209
|
+
)
|
|
2210
|
+
)
|
|
2211
|
+
);
|
|
2212
|
+
return s;
|
|
2213
|
+
}
|
|
2214
|
+
const coin = toHyperliquidSymbol(opts.symbol);
|
|
2215
|
+
const merger = new BookMerger();
|
|
2216
|
+
let ws = null;
|
|
2217
|
+
const stream = new OrderbookStream(() => ws?.close());
|
|
2218
|
+
const subscribePayload = JSON.stringify({
|
|
2219
|
+
method: "subscribe",
|
|
2220
|
+
subscription: { type: "l2Book", coin }
|
|
2221
|
+
});
|
|
2222
|
+
const onMessage = (raw) => {
|
|
2223
|
+
if (typeof raw !== "string") return;
|
|
2224
|
+
let msg;
|
|
2225
|
+
try {
|
|
2226
|
+
msg = JSON.parse(raw);
|
|
2227
|
+
} catch {
|
|
2228
|
+
return;
|
|
2229
|
+
}
|
|
2230
|
+
if (msg.channel === "pong" || msg.channel === "subscriptionResponse") {
|
|
2231
|
+
return;
|
|
2232
|
+
}
|
|
2233
|
+
if (msg.channel !== "l2Book" || !msg.data) return;
|
|
2234
|
+
const book = msg.data;
|
|
2235
|
+
if (book.coin !== coin) return;
|
|
2236
|
+
const event = {
|
|
2237
|
+
kind: "snapshot",
|
|
2238
|
+
bids: book.levels[0].map((l) => ({
|
|
2239
|
+
price: Number(l.px),
|
|
2240
|
+
size: Number(l.sz)
|
|
2241
|
+
})),
|
|
2242
|
+
asks: book.levels[1].map((l) => ({
|
|
2243
|
+
price: Number(l.px),
|
|
2244
|
+
size: Number(l.sz)
|
|
2245
|
+
})),
|
|
2246
|
+
sequence: book.time,
|
|
2247
|
+
timestamp: book.time
|
|
2248
|
+
};
|
|
2249
|
+
const r = merger.apply(event);
|
|
2250
|
+
if (!r.ok) return;
|
|
2251
|
+
const out = {
|
|
2252
|
+
exchange: "hyperliquid",
|
|
2253
|
+
symbol: fromHyperliquidSymbol(coin),
|
|
2254
|
+
market: "perpetual",
|
|
2255
|
+
bids: r.bids,
|
|
2256
|
+
asks: r.asks,
|
|
2257
|
+
timestamp: r.timestamp,
|
|
2258
|
+
sequence: r.sequence
|
|
2259
|
+
};
|
|
2260
|
+
stream.emit("update", out);
|
|
2261
|
+
};
|
|
2262
|
+
ws = new WSClient({
|
|
2263
|
+
url: WS_URL,
|
|
2264
|
+
onOpen: (sock) => {
|
|
2265
|
+
merger.reset();
|
|
2266
|
+
sock.send(subscribePayload);
|
|
2267
|
+
},
|
|
2268
|
+
onMessage,
|
|
2269
|
+
pingIntervalMs: 5e4,
|
|
2270
|
+
pingPayload: () => JSON.stringify({ method: "ping" })
|
|
2271
|
+
});
|
|
2272
|
+
ws.on("open", () => stream.emit("connected"));
|
|
2273
|
+
ws.on("close", (r) => stream.emit("disconnected", r));
|
|
2274
|
+
ws.on("reconnecting", (a, w) => stream.emit("reconnecting", a, w));
|
|
2275
|
+
ws.on("error", (e) => stream.emit("error", e));
|
|
2276
|
+
ws.connect().catch((e) => stream.emit("error", e));
|
|
2277
|
+
return stream;
|
|
2278
|
+
}
|
|
2279
|
+
|
|
2280
|
+
// src/exchanges/hyperliquid/index.ts
|
|
2281
|
+
var HyperliquidClient = class {
|
|
2282
|
+
exchange = "hyperliquid";
|
|
2283
|
+
transformUrl;
|
|
2284
|
+
timeoutMs;
|
|
2285
|
+
constructor(opts = {}) {
|
|
2286
|
+
this.transformUrl = opts.transformUrl;
|
|
2287
|
+
this.timeoutMs = opts.timeoutMs;
|
|
2288
|
+
}
|
|
2289
|
+
fetchOrderbook(symbol, opts = {}) {
|
|
2290
|
+
const { pair, market } = parseSymbol(symbol);
|
|
2291
|
+
return fetchHyperliquidOrderbook({
|
|
2292
|
+
symbol: pair,
|
|
2293
|
+
market,
|
|
2294
|
+
depth: opts.depth,
|
|
2295
|
+
timeoutMs: this.timeoutMs,
|
|
2296
|
+
transformUrl: this.transformUrl
|
|
2297
|
+
});
|
|
2298
|
+
}
|
|
2299
|
+
streamOrderbook(symbol, opts = {}) {
|
|
2300
|
+
const { pair, market } = parseSymbol(symbol);
|
|
2301
|
+
return streamHyperliquidOrderbook({
|
|
2302
|
+
symbol: pair,
|
|
2303
|
+
market,
|
|
2304
|
+
depth: opts.depth,
|
|
2305
|
+
transformUrl: this.transformUrl,
|
|
2306
|
+
timeoutMs: this.timeoutMs
|
|
2307
|
+
});
|
|
2308
|
+
}
|
|
2309
|
+
};
|
|
2310
|
+
|
|
2311
|
+
// src/exchanges/kucoin/symbols.ts
|
|
2312
|
+
var FUTURES_QUOTES = ["USDT", "USDC", "USD"];
|
|
2313
|
+
function toKucoinSpotSymbol(symbol) {
|
|
2314
|
+
const [base, quote] = symbol.split("/");
|
|
2315
|
+
if (!base || !quote) {
|
|
2316
|
+
throw new Error(`invalid symbol "${symbol}" \u2014 expected "BASE/QUOTE"`);
|
|
2317
|
+
}
|
|
2318
|
+
return `${base.toUpperCase()}-${quote.toUpperCase()}`;
|
|
2319
|
+
}
|
|
2320
|
+
function fromKucoinSpotSymbol(s) {
|
|
2321
|
+
return s.toUpperCase().replace("-", "/");
|
|
2322
|
+
}
|
|
2323
|
+
function toKucoinFuturesSymbol(symbol) {
|
|
2324
|
+
const [base, quote] = symbol.split("/");
|
|
2325
|
+
if (!base || !quote) {
|
|
2326
|
+
throw new Error(`invalid symbol "${symbol}" \u2014 expected "BASE/QUOTE"`);
|
|
2327
|
+
}
|
|
2328
|
+
const b = base.toUpperCase() === "BTC" ? "XBT" : base.toUpperCase();
|
|
2329
|
+
return `${b}${quote.toUpperCase()}M`;
|
|
2330
|
+
}
|
|
2331
|
+
function fromKucoinFuturesSymbol(s) {
|
|
2332
|
+
const upper = s.toUpperCase();
|
|
2333
|
+
const stripped = upper.endsWith("M") ? upper.slice(0, -1) : upper;
|
|
2334
|
+
for (const q of FUTURES_QUOTES) {
|
|
2335
|
+
if (stripped.endsWith(q) && stripped.length > q.length) {
|
|
2336
|
+
let base = stripped.slice(0, -q.length);
|
|
2337
|
+
if (base === "XBT") base = "BTC";
|
|
2338
|
+
return `${base}/${q}`;
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
2341
|
+
return upper;
|
|
2342
|
+
}
|
|
2343
|
+
|
|
2344
|
+
// src/exchanges/kucoin/rest.ts
|
|
2345
|
+
var BASE_SPOT2 = "https://api.kucoin.com";
|
|
2346
|
+
var BASE_FUTURES = "https://api-futures.kucoin.com";
|
|
2347
|
+
async function fetchKucoinBullet(opts) {
|
|
2348
|
+
const base = opts.market === "spot" ? BASE_SPOT2 : BASE_FUTURES;
|
|
2349
|
+
const res = await httpJson({
|
|
2350
|
+
url: `${base}/api/v1/bullet-public`,
|
|
2351
|
+
method: "POST",
|
|
2352
|
+
timeoutMs: opts.timeoutMs,
|
|
2353
|
+
transformUrl: opts.transformUrl
|
|
2354
|
+
});
|
|
2355
|
+
if (res.code !== "200000") {
|
|
2356
|
+
throw new ExchangeError("kucoin", `bullet failed: ${res.code}`);
|
|
2357
|
+
}
|
|
2358
|
+
const server = res.data.instanceServers[0];
|
|
2359
|
+
if (!server) {
|
|
2360
|
+
throw new ExchangeError("kucoin", "bullet returned no instance servers");
|
|
2361
|
+
}
|
|
2362
|
+
return {
|
|
2363
|
+
endpoint: server.endpoint,
|
|
2364
|
+
token: res.data.token,
|
|
2365
|
+
pingInterval: server.pingInterval,
|
|
2366
|
+
pingTimeout: server.pingTimeout
|
|
2367
|
+
};
|
|
2368
|
+
}
|
|
2369
|
+
async function fetchKucoinOrderbook(opts) {
|
|
2370
|
+
const tier = (opts.depth ?? 100) <= 20 ? 20 : 100;
|
|
2371
|
+
if (opts.market === "spot") {
|
|
2372
|
+
const symbol2 = toKucoinSpotSymbol(opts.symbol);
|
|
2373
|
+
const url2 = `${BASE_SPOT2}/api/v1/market/orderbook/level2_${tier}?symbol=${symbol2}`;
|
|
2374
|
+
const res2 = await httpJson({
|
|
2375
|
+
url: url2,
|
|
2376
|
+
timeoutMs: opts.timeoutMs,
|
|
2377
|
+
transformUrl: opts.transformUrl
|
|
2378
|
+
});
|
|
2379
|
+
if (res2.code !== "200000") {
|
|
2380
|
+
throw new ExchangeError("kucoin", `orderbook failed: ${res2.code}`);
|
|
2381
|
+
}
|
|
2382
|
+
return {
|
|
2383
|
+
exchange: "kucoin",
|
|
2384
|
+
symbol: fromKucoinSpotSymbol(symbol2),
|
|
2385
|
+
market: "spot",
|
|
2386
|
+
bids: parseLevels(res2.data.bids),
|
|
2387
|
+
asks: parseLevels(res2.data.asks),
|
|
2388
|
+
timestamp: res2.data.time,
|
|
2389
|
+
sequence: Number(res2.data.sequence)
|
|
2390
|
+
};
|
|
2391
|
+
}
|
|
2392
|
+
const symbol = toKucoinFuturesSymbol(opts.symbol);
|
|
2393
|
+
const url = `${BASE_FUTURES}/api/v1/level2/depth${tier}?symbol=${symbol}`;
|
|
2394
|
+
const res = await httpJson({
|
|
2395
|
+
url,
|
|
2396
|
+
timeoutMs: opts.timeoutMs,
|
|
2397
|
+
transformUrl: opts.transformUrl
|
|
2398
|
+
});
|
|
2399
|
+
if (res.code !== "200000") {
|
|
2400
|
+
throw new ExchangeError("kucoin", `orderbook failed: ${res.code}`);
|
|
2401
|
+
}
|
|
2402
|
+
return {
|
|
2403
|
+
exchange: "kucoin",
|
|
2404
|
+
symbol: fromKucoinFuturesSymbol(symbol),
|
|
2405
|
+
market: "perpetual",
|
|
2406
|
+
bids: parseLevels(res.data.bids),
|
|
2407
|
+
asks: parseLevels(res.data.asks),
|
|
2408
|
+
timestamp: res.data.ts,
|
|
2409
|
+
sequence: res.data.sequence
|
|
2410
|
+
};
|
|
2411
|
+
}
|
|
2412
|
+
function parseLevels(rows) {
|
|
2413
|
+
return rows.map(([p, s]) => ({ price: Number(p), size: Number(s) }));
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
// src/exchanges/kucoin/ws.ts
|
|
2417
|
+
function streamKucoinOrderbook(opts) {
|
|
2418
|
+
const isSpot = opts.market === "spot";
|
|
2419
|
+
const symbol = isSpot ? toKucoinSpotSymbol(opts.symbol) : toKucoinFuturesSymbol(opts.symbol);
|
|
2420
|
+
const level = (opts.depth ?? 50) <= 5 ? 5 : 50;
|
|
2421
|
+
const topic = isSpot ? `/spotMarket/level2Depth${level}:${symbol}` : `/contractMarket/level2Depth${level}:${symbol}`;
|
|
2422
|
+
const merger = new BookMerger();
|
|
2423
|
+
let ws = null;
|
|
2424
|
+
const stream = new OrderbookStream(() => ws?.close());
|
|
2425
|
+
const urlProvider = async () => {
|
|
2426
|
+
const bullet = await fetchKucoinBullet({
|
|
2427
|
+
market: opts.market,
|
|
2428
|
+
timeoutMs: opts.timeoutMs,
|
|
2429
|
+
transformUrl: opts.transformUrl
|
|
2430
|
+
});
|
|
2431
|
+
const connectId = `xapy-${Date.now()}-${Math.floor(Math.random() * 1e6)}`;
|
|
2432
|
+
return `${bullet.endpoint}?token=${encodeURIComponent(bullet.token)}&connectId=${connectId}`;
|
|
2433
|
+
};
|
|
2434
|
+
const subscribePayload = () => JSON.stringify({
|
|
2435
|
+
id: `sub-${Date.now()}`,
|
|
2436
|
+
type: "subscribe",
|
|
2437
|
+
topic,
|
|
2438
|
+
privateChannel: false,
|
|
2439
|
+
response: true
|
|
2440
|
+
});
|
|
2441
|
+
const pingPayload = () => JSON.stringify({ id: `${Date.now()}`, type: "ping" });
|
|
2442
|
+
const handleDepth = (d) => {
|
|
2443
|
+
const ts = d.timestamp ?? d.ts ?? Date.now();
|
|
2444
|
+
const event = {
|
|
2445
|
+
kind: "snapshot",
|
|
2446
|
+
bids: (d.bids ?? []).map(([p, s]) => ({
|
|
2447
|
+
price: Number(p),
|
|
2448
|
+
size: Number(s)
|
|
2449
|
+
})),
|
|
2450
|
+
asks: (d.asks ?? []).map(([p, s]) => ({
|
|
2451
|
+
price: Number(p),
|
|
2452
|
+
size: Number(s)
|
|
2453
|
+
})),
|
|
2454
|
+
sequence: ts,
|
|
2455
|
+
timestamp: ts
|
|
2456
|
+
};
|
|
2457
|
+
const r = merger.apply(event);
|
|
2458
|
+
if (!r.ok) return;
|
|
2459
|
+
const book = {
|
|
2460
|
+
exchange: "kucoin",
|
|
2461
|
+
symbol: isSpot ? fromKucoinSpotSymbol(symbol) : fromKucoinFuturesSymbol(symbol),
|
|
2462
|
+
market: opts.market,
|
|
2463
|
+
bids: r.bids,
|
|
2464
|
+
asks: r.asks,
|
|
2465
|
+
timestamp: r.timestamp,
|
|
2466
|
+
sequence: r.sequence
|
|
2467
|
+
};
|
|
2468
|
+
stream.emit("update", book);
|
|
2469
|
+
};
|
|
2470
|
+
const onMessage = (raw) => {
|
|
2471
|
+
if (typeof raw !== "string") return;
|
|
2472
|
+
let msg;
|
|
2473
|
+
try {
|
|
2474
|
+
msg = JSON.parse(raw);
|
|
2475
|
+
} catch {
|
|
2476
|
+
return;
|
|
2477
|
+
}
|
|
2478
|
+
if (msg.type === "welcome" || msg.type === "ack" || msg.type === "pong") {
|
|
2479
|
+
return;
|
|
2480
|
+
}
|
|
2481
|
+
if (msg.type === "error") {
|
|
2482
|
+
stream.emit("error", new Error(`kucoin: ${JSON.stringify(msg)}`));
|
|
2483
|
+
return;
|
|
2484
|
+
}
|
|
2485
|
+
if (msg.type !== "message" || msg.topic !== topic || !msg.data) return;
|
|
2486
|
+
handleDepth(msg.data);
|
|
2487
|
+
};
|
|
2488
|
+
ws = new WSClient({
|
|
2489
|
+
url: urlProvider,
|
|
2490
|
+
onOpen: (sock) => {
|
|
2491
|
+
merger.reset();
|
|
2492
|
+
sock.send(subscribePayload());
|
|
2493
|
+
},
|
|
2494
|
+
onMessage,
|
|
2495
|
+
// KuCoin's documented bullet `pingInterval` is 18000ms — stay under it.
|
|
2496
|
+
pingIntervalMs: 15e3,
|
|
2497
|
+
pingPayload
|
|
2498
|
+
});
|
|
2499
|
+
ws.on("open", () => stream.emit("connected"));
|
|
2500
|
+
ws.on("close", (r) => stream.emit("disconnected", r));
|
|
2501
|
+
ws.on("reconnecting", (a, w) => stream.emit("reconnecting", a, w));
|
|
2502
|
+
ws.on("error", (e) => stream.emit("error", e));
|
|
2503
|
+
ws.connect().catch((e) => stream.emit("error", e));
|
|
2504
|
+
return stream;
|
|
2505
|
+
}
|
|
2506
|
+
|
|
2507
|
+
// src/exchanges/kucoin/index.ts
|
|
2508
|
+
var KucoinClient = class {
|
|
2509
|
+
exchange = "kucoin";
|
|
2510
|
+
transformUrl;
|
|
2511
|
+
timeoutMs;
|
|
2512
|
+
constructor(opts = {}) {
|
|
2513
|
+
this.transformUrl = opts.transformUrl;
|
|
2514
|
+
this.timeoutMs = opts.timeoutMs;
|
|
2515
|
+
}
|
|
2516
|
+
fetchOrderbook(symbol, opts = {}) {
|
|
2517
|
+
const { pair, market } = parseSymbol(symbol);
|
|
2518
|
+
return fetchKucoinOrderbook({
|
|
2519
|
+
symbol: pair,
|
|
2520
|
+
market,
|
|
2521
|
+
depth: opts.depth,
|
|
2522
|
+
timeoutMs: this.timeoutMs,
|
|
2523
|
+
transformUrl: this.transformUrl
|
|
2524
|
+
});
|
|
2525
|
+
}
|
|
2526
|
+
streamOrderbook(symbol, opts = {}) {
|
|
2527
|
+
const { pair, market } = parseSymbol(symbol);
|
|
2528
|
+
return streamKucoinOrderbook({
|
|
2529
|
+
symbol: pair,
|
|
2530
|
+
market,
|
|
2531
|
+
depth: opts.depth,
|
|
2532
|
+
transformUrl: this.transformUrl,
|
|
2533
|
+
timeoutMs: this.timeoutMs
|
|
2534
|
+
});
|
|
2535
|
+
}
|
|
2536
|
+
};
|
|
2537
|
+
|
|
2538
|
+
export { Backoff, BinanceClient, BingxClient, BitgetClient, BookMerger, BybitClient, CoinexClient, ExchangeError, GateClient, HuobiClient, HyperliquidClient, KucoinClient, OkxClient, OrderbookStream, RateLimitError, SequenceGapError, parseSymbol };
|
|
1916
2539
|
//# sourceMappingURL=index.js.map
|
|
1917
2540
|
//# sourceMappingURL=index.js.map
|