@orderly.network/hooks 2.1.2 → 2.1.3
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/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +137 -134
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +137 -134
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/index.mjs
CHANGED
|
@@ -34,9 +34,9 @@ var __export = (target, all) => {
|
|
|
34
34
|
// src/version.ts
|
|
35
35
|
if (typeof window !== "undefined") {
|
|
36
36
|
window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
|
|
37
|
-
window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.1.
|
|
37
|
+
window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.1.3";
|
|
38
38
|
}
|
|
39
|
-
var version_default = "2.1.
|
|
39
|
+
var version_default = "2.1.3";
|
|
40
40
|
var fetcher = (url, init2 = {}, queryOptions) => get(url, init2, queryOptions?.formatter);
|
|
41
41
|
var OrderlyContext = createContext({
|
|
42
42
|
// configStore: new MemoryConfigStore(),
|
|
@@ -1921,6 +1921,112 @@ var useCalculatorService = () => {
|
|
|
1921
1921
|
});
|
|
1922
1922
|
return calculatorService;
|
|
1923
1923
|
};
|
|
1924
|
+
|
|
1925
|
+
// src/orderly/orderbook.service.ts
|
|
1926
|
+
var defaultRawOrderBook = {
|
|
1927
|
+
asks: [],
|
|
1928
|
+
bids: [],
|
|
1929
|
+
ts: 0
|
|
1930
|
+
};
|
|
1931
|
+
var OrderbookService = class _OrderbookService {
|
|
1932
|
+
constructor() {
|
|
1933
|
+
this.bufferedOrderBookUpdates = {};
|
|
1934
|
+
this.rawOrderBook = {};
|
|
1935
|
+
}
|
|
1936
|
+
static getInstance() {
|
|
1937
|
+
if (!this.instance) {
|
|
1938
|
+
this.instance = new _OrderbookService();
|
|
1939
|
+
}
|
|
1940
|
+
return this.instance;
|
|
1941
|
+
}
|
|
1942
|
+
sortBufferedOrderBookUpdates(symbol) {
|
|
1943
|
+
this.bufferedOrderBookUpdates[symbol]?.sort((a, b) => a.ts - b.ts);
|
|
1944
|
+
}
|
|
1945
|
+
applyUpdateToRawOrderBook(symbol, update) {
|
|
1946
|
+
const rawOrderBook = this.rawOrderBook[symbol];
|
|
1947
|
+
if (!rawOrderBook || rawOrderBook.ts > update.prevTs) {
|
|
1948
|
+
return;
|
|
1949
|
+
}
|
|
1950
|
+
const askMap = /* @__PURE__ */ new Map();
|
|
1951
|
+
const bidMap = /* @__PURE__ */ new Map();
|
|
1952
|
+
rawOrderBook.asks.forEach((ask) => askMap.set(ask[0], ask[1]));
|
|
1953
|
+
rawOrderBook.bids.forEach((bid) => bidMap.set(bid[0], bid[1]));
|
|
1954
|
+
update.asks.forEach((ask) => ask[1] === 0 ? askMap.delete(ask[0]) : askMap.set(ask[0], ask[1]));
|
|
1955
|
+
update.bids.forEach((bid) => bid[1] === 0 ? bidMap.delete(bid[0]) : bidMap.set(bid[0], bid[1]));
|
|
1956
|
+
rawOrderBook.asks = Array.from(askMap.entries()).sort((a, b) => a[0] - b[0]);
|
|
1957
|
+
rawOrderBook.bids = Array.from(bidMap.entries()).sort((a, b) => b[0] - a[0]);
|
|
1958
|
+
rawOrderBook.ts = update.ts;
|
|
1959
|
+
}
|
|
1960
|
+
applyBufferedUpdatesToRawOrderBooks(symbol) {
|
|
1961
|
+
this.bufferedOrderBookUpdates[symbol]?.forEach((update) => this.applyUpdateToRawOrderBook(symbol, update));
|
|
1962
|
+
}
|
|
1963
|
+
deleteBufferedOrderBookUpdates(symbol) {
|
|
1964
|
+
delete this.bufferedOrderBookUpdates[symbol];
|
|
1965
|
+
}
|
|
1966
|
+
commitOrderBook(symbol) {
|
|
1967
|
+
const rawOrderBook = this.rawOrderBook[symbol];
|
|
1968
|
+
if (!rawOrderBook) {
|
|
1969
|
+
return;
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
pushUpdateToBuffer(symbol, update) {
|
|
1973
|
+
if (this.bufferedOrderBookUpdates[symbol] === void 0) {
|
|
1974
|
+
this.bufferedOrderBookUpdates[symbol] = [];
|
|
1975
|
+
}
|
|
1976
|
+
const buffer = this.bufferedOrderBookUpdates[symbol];
|
|
1977
|
+
if (buffer.length > 0) {
|
|
1978
|
+
const lastUpdate = buffer[buffer.length - 1];
|
|
1979
|
+
if (lastUpdate.ts !== update.prevTs) {
|
|
1980
|
+
this.bufferedOrderBookUpdates[symbol] = [];
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
this.bufferedOrderBookUpdates[symbol].push(update);
|
|
1984
|
+
}
|
|
1985
|
+
isValidFullOrderBook(symbol, currentTs) {
|
|
1986
|
+
if ((this.bufferedOrderBookUpdates[symbol]?.length ?? 0) !== 0) {
|
|
1987
|
+
const earliestUpdates = this.bufferedOrderBookUpdates[symbol][0];
|
|
1988
|
+
return earliestUpdates.prevTs <= currentTs;
|
|
1989
|
+
}
|
|
1990
|
+
return true;
|
|
1991
|
+
}
|
|
1992
|
+
setFullOrderbook(symbol, rawOrderbook) {
|
|
1993
|
+
const { ts } = rawOrderbook;
|
|
1994
|
+
this.rawOrderBook[symbol] = rawOrderbook;
|
|
1995
|
+
this.sortBufferedOrderBookUpdates(symbol);
|
|
1996
|
+
if (this.isValidFullOrderBook(symbol, ts)) {
|
|
1997
|
+
this.applyBufferedUpdatesToRawOrderBooks(symbol);
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
updateOrderbook(symbol, update, callback) {
|
|
2001
|
+
const { asks, bids, prevTs, ts } = update;
|
|
2002
|
+
const rawOrderBook = this.rawOrderBook[symbol];
|
|
2003
|
+
if (!rawOrderBook) {
|
|
2004
|
+
return;
|
|
2005
|
+
}
|
|
2006
|
+
const currentTs = rawOrderBook.ts;
|
|
2007
|
+
if (currentTs === 0) {
|
|
2008
|
+
this.pushUpdateToBuffer(symbol, { asks, bids, prevTs, ts });
|
|
2009
|
+
return;
|
|
2010
|
+
}
|
|
2011
|
+
if (prevTs !== currentTs) {
|
|
2012
|
+
this.pushUpdateToBuffer(symbol, { asks, bids, prevTs, ts });
|
|
2013
|
+
if (callback) {
|
|
2014
|
+
callback();
|
|
2015
|
+
}
|
|
2016
|
+
return;
|
|
2017
|
+
}
|
|
2018
|
+
this.applyUpdateToRawOrderBook(symbol, update);
|
|
2019
|
+
this.deleteBufferedOrderBookUpdates(symbol);
|
|
2020
|
+
}
|
|
2021
|
+
getRawOrderbook(symbol) {
|
|
2022
|
+
return this.rawOrderBook[symbol];
|
|
2023
|
+
}
|
|
2024
|
+
resetOrderBook(symbol) {
|
|
2025
|
+
this.rawOrderBook[symbol] = defaultRawOrderBook;
|
|
2026
|
+
}
|
|
2027
|
+
};
|
|
2028
|
+
var orderBookService = OrderbookService.getInstance();
|
|
2029
|
+
var orderbook_service_default = orderBookService;
|
|
1924
2030
|
var useMarkPrice = (symbol) => {
|
|
1925
2031
|
const ws = useWS();
|
|
1926
2032
|
const [price, setPrice] = useState(0);
|
|
@@ -1936,6 +2042,13 @@ var useMarkPrice = (symbol) => {
|
|
|
1936
2042
|
}, [symbol]);
|
|
1937
2043
|
return { data: price };
|
|
1938
2044
|
};
|
|
2045
|
+
var useSymbolsInfo = () => {
|
|
2046
|
+
const symbolsInfo = useAppStore((state) => state.symbolsInfo);
|
|
2047
|
+
return useMemo(
|
|
2048
|
+
() => createGetter({ ...symbolsInfo }),
|
|
2049
|
+
[symbolsInfo]
|
|
2050
|
+
);
|
|
2051
|
+
};
|
|
1939
2052
|
var useIndexPrice = (symbol) => {
|
|
1940
2053
|
symbol = symbol.replace("PERP", "SPOT");
|
|
1941
2054
|
const ws = useWS();
|
|
@@ -2077,119 +2190,6 @@ var useTickerStream = (symbol) => {
|
|
|
2077
2190
|
}, [info, symbol, ticker, futures, openInterest]);
|
|
2078
2191
|
return value;
|
|
2079
2192
|
};
|
|
2080
|
-
var useSymbolsInfo = () => {
|
|
2081
|
-
const symbolsInfo = useAppStore((state) => state.symbolsInfo);
|
|
2082
|
-
return useMemo(
|
|
2083
|
-
() => createGetter({ ...symbolsInfo }),
|
|
2084
|
-
[symbolsInfo]
|
|
2085
|
-
);
|
|
2086
|
-
};
|
|
2087
|
-
|
|
2088
|
-
// src/orderly/orderbook.service.ts
|
|
2089
|
-
var defaultRawOrderBook = {
|
|
2090
|
-
asks: [],
|
|
2091
|
-
bids: [],
|
|
2092
|
-
ts: 0
|
|
2093
|
-
};
|
|
2094
|
-
var OrderbookService = class _OrderbookService {
|
|
2095
|
-
constructor() {
|
|
2096
|
-
this.bufferedOrderBookUpdates = {};
|
|
2097
|
-
this.rawOrderBook = {};
|
|
2098
|
-
}
|
|
2099
|
-
static getInstance() {
|
|
2100
|
-
if (!this.instance) {
|
|
2101
|
-
this.instance = new _OrderbookService();
|
|
2102
|
-
}
|
|
2103
|
-
return this.instance;
|
|
2104
|
-
}
|
|
2105
|
-
sortBufferedOrderBookUpdates(symbol) {
|
|
2106
|
-
this.bufferedOrderBookUpdates[symbol]?.sort((a, b) => a.ts - b.ts);
|
|
2107
|
-
}
|
|
2108
|
-
applyUpdateToRawOrderBook(symbol, update) {
|
|
2109
|
-
const rawOrderBook = this.rawOrderBook[symbol];
|
|
2110
|
-
if (!rawOrderBook || rawOrderBook.ts > update.prevTs) {
|
|
2111
|
-
return;
|
|
2112
|
-
}
|
|
2113
|
-
const askMap = /* @__PURE__ */ new Map();
|
|
2114
|
-
const bidMap = /* @__PURE__ */ new Map();
|
|
2115
|
-
rawOrderBook.asks.forEach((ask) => askMap.set(ask[0], ask[1]));
|
|
2116
|
-
rawOrderBook.bids.forEach((bid) => bidMap.set(bid[0], bid[1]));
|
|
2117
|
-
update.asks.forEach((ask) => ask[1] === 0 ? askMap.delete(ask[0]) : askMap.set(ask[0], ask[1]));
|
|
2118
|
-
update.bids.forEach((bid) => bid[1] === 0 ? bidMap.delete(bid[0]) : bidMap.set(bid[0], bid[1]));
|
|
2119
|
-
rawOrderBook.asks = Array.from(askMap.entries()).sort((a, b) => a[0] - b[0]);
|
|
2120
|
-
rawOrderBook.bids = Array.from(bidMap.entries()).sort((a, b) => b[0] - a[0]);
|
|
2121
|
-
rawOrderBook.ts = update.ts;
|
|
2122
|
-
}
|
|
2123
|
-
applyBufferedUpdatesToRawOrderBooks(symbol) {
|
|
2124
|
-
this.bufferedOrderBookUpdates[symbol]?.forEach((update) => this.applyUpdateToRawOrderBook(symbol, update));
|
|
2125
|
-
}
|
|
2126
|
-
deleteBufferedOrderBookUpdates(symbol) {
|
|
2127
|
-
delete this.bufferedOrderBookUpdates[symbol];
|
|
2128
|
-
}
|
|
2129
|
-
commitOrderBook(symbol) {
|
|
2130
|
-
const rawOrderBook = this.rawOrderBook[symbol];
|
|
2131
|
-
if (!rawOrderBook) {
|
|
2132
|
-
return;
|
|
2133
|
-
}
|
|
2134
|
-
}
|
|
2135
|
-
pushUpdateToBuffer(symbol, update) {
|
|
2136
|
-
if (this.bufferedOrderBookUpdates[symbol] === void 0) {
|
|
2137
|
-
this.bufferedOrderBookUpdates[symbol] = [];
|
|
2138
|
-
}
|
|
2139
|
-
const buffer = this.bufferedOrderBookUpdates[symbol];
|
|
2140
|
-
if (buffer.length > 0) {
|
|
2141
|
-
const lastUpdate = buffer[buffer.length - 1];
|
|
2142
|
-
if (lastUpdate.ts !== update.prevTs) {
|
|
2143
|
-
this.bufferedOrderBookUpdates[symbol] = [];
|
|
2144
|
-
}
|
|
2145
|
-
}
|
|
2146
|
-
this.bufferedOrderBookUpdates[symbol].push(update);
|
|
2147
|
-
}
|
|
2148
|
-
isValidFullOrderBook(symbol, currentTs) {
|
|
2149
|
-
if ((this.bufferedOrderBookUpdates[symbol]?.length ?? 0) !== 0) {
|
|
2150
|
-
const earliestUpdates = this.bufferedOrderBookUpdates[symbol][0];
|
|
2151
|
-
return earliestUpdates.prevTs <= currentTs;
|
|
2152
|
-
}
|
|
2153
|
-
return true;
|
|
2154
|
-
}
|
|
2155
|
-
setFullOrderbook(symbol, rawOrderbook) {
|
|
2156
|
-
const { ts } = rawOrderbook;
|
|
2157
|
-
this.rawOrderBook[symbol] = rawOrderbook;
|
|
2158
|
-
this.sortBufferedOrderBookUpdates(symbol);
|
|
2159
|
-
if (this.isValidFullOrderBook(symbol, ts)) {
|
|
2160
|
-
this.applyBufferedUpdatesToRawOrderBooks(symbol);
|
|
2161
|
-
}
|
|
2162
|
-
}
|
|
2163
|
-
updateOrderbook(symbol, update, callback) {
|
|
2164
|
-
const { asks, bids, prevTs, ts } = update;
|
|
2165
|
-
const rawOrderBook = this.rawOrderBook[symbol];
|
|
2166
|
-
if (!rawOrderBook) {
|
|
2167
|
-
return;
|
|
2168
|
-
}
|
|
2169
|
-
const currentTs = rawOrderBook.ts;
|
|
2170
|
-
if (currentTs === 0) {
|
|
2171
|
-
this.pushUpdateToBuffer(symbol, { asks, bids, prevTs, ts });
|
|
2172
|
-
return;
|
|
2173
|
-
}
|
|
2174
|
-
if (prevTs !== currentTs) {
|
|
2175
|
-
this.pushUpdateToBuffer(symbol, { asks, bids, prevTs, ts });
|
|
2176
|
-
if (callback) {
|
|
2177
|
-
callback();
|
|
2178
|
-
}
|
|
2179
|
-
return;
|
|
2180
|
-
}
|
|
2181
|
-
this.applyUpdateToRawOrderBook(symbol, update);
|
|
2182
|
-
this.deleteBufferedOrderBookUpdates(symbol);
|
|
2183
|
-
}
|
|
2184
|
-
getRawOrderbook(symbol) {
|
|
2185
|
-
return this.rawOrderBook[symbol];
|
|
2186
|
-
}
|
|
2187
|
-
resetOrderBook(symbol) {
|
|
2188
|
-
this.rawOrderBook[symbol] = defaultRawOrderBook;
|
|
2189
|
-
}
|
|
2190
|
-
};
|
|
2191
|
-
var orderBookService = OrderbookService.getInstance();
|
|
2192
|
-
var orderbook_service_default = orderBookService;
|
|
2193
2193
|
|
|
2194
2194
|
// src/orderly/useOrderbookStream.ts
|
|
2195
2195
|
var paddingFn = (len) => Array(len).fill([
|
|
@@ -2254,7 +2254,7 @@ var reduceOrderbook = (depth, level, padding, data) => {
|
|
|
2254
2254
|
const [price, qty, newQuantity, newAmount] = asks[0];
|
|
2255
2255
|
asks.shift();
|
|
2256
2256
|
asks.push([
|
|
2257
|
-
price + (depth === void 0 ? 0 : depth),
|
|
2257
|
+
price + (depth === void 0 ? 0 : Number(depth)),
|
|
2258
2258
|
qty,
|
|
2259
2259
|
newQuantity,
|
|
2260
2260
|
newAmount
|
|
@@ -2265,7 +2265,6 @@ var reduceOrderbook = (depth, level, padding, data) => {
|
|
|
2265
2265
|
const [askPrice, askQty, newQuantity, newAmount] = asks[0];
|
|
2266
2266
|
if (askPrice <= bidPrice) {
|
|
2267
2267
|
asks.shift();
|
|
2268
|
-
let logStr = "";
|
|
2269
2268
|
for (let index = 0; index < asks.length; index++) {
|
|
2270
2269
|
if (index === 0) {
|
|
2271
2270
|
const quantity = asks[index][1] + askQty;
|
|
@@ -2275,8 +2274,6 @@ var reduceOrderbook = (depth, level, padding, data) => {
|
|
|
2275
2274
|
} else {
|
|
2276
2275
|
asks[index][3] = asks[index][0] * asks[index][1] + asks[index - 1][3];
|
|
2277
2276
|
}
|
|
2278
|
-
logStr += `index: ${index} ${asks[index]}
|
|
2279
|
-
`;
|
|
2280
2277
|
}
|
|
2281
2278
|
} else {
|
|
2282
2279
|
break;
|
|
@@ -3197,8 +3194,6 @@ var useFundingRate = (symbol) => {
|
|
|
3197
3194
|
countDown
|
|
3198
3195
|
};
|
|
3199
3196
|
};
|
|
3200
|
-
|
|
3201
|
-
// src/orderly/useFundingRateHistory.ts
|
|
3202
3197
|
var calculatePositiveRate = (periodData, period) => {
|
|
3203
3198
|
if (!periodData || !period)
|
|
3204
3199
|
return 0;
|
|
@@ -3214,22 +3209,30 @@ var calculatePositiveRate = (periodData, period) => {
|
|
|
3214
3209
|
return periodData.positive / totalTimes;
|
|
3215
3210
|
};
|
|
3216
3211
|
var useFundingRateHistory = () => {
|
|
3217
|
-
const {
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3212
|
+
const { data: historyData, isLoading } = useQuery(
|
|
3213
|
+
"/v1/public/market_info/funding_history"
|
|
3214
|
+
);
|
|
3215
|
+
const getPositiveRates = useCallback(
|
|
3216
|
+
(data, period) => {
|
|
3217
|
+
if (!data?.length)
|
|
3218
|
+
return {};
|
|
3219
|
+
return data.reduce(
|
|
3220
|
+
(acc, item) => {
|
|
3221
|
+
acc[item.symbol] = calculatePositiveRate(
|
|
3222
|
+
item.funding[period],
|
|
3223
|
+
period
|
|
3224
|
+
);
|
|
3225
|
+
return acc;
|
|
3226
|
+
},
|
|
3227
|
+
{}
|
|
3228
|
+
);
|
|
3229
|
+
},
|
|
3230
|
+
[]
|
|
3231
|
+
);
|
|
3222
3232
|
return {
|
|
3223
3233
|
data: historyData ?? [],
|
|
3224
3234
|
isLoading,
|
|
3225
|
-
getPositiveRates
|
|
3226
|
-
if (!data?.length)
|
|
3227
|
-
return {};
|
|
3228
|
-
return data.reduce((acc, item) => {
|
|
3229
|
-
acc[item.symbol] = calculatePositiveRate(item.funding[period], period);
|
|
3230
|
-
return acc;
|
|
3231
|
-
}, {});
|
|
3232
|
-
}
|
|
3235
|
+
getPositiveRates
|
|
3233
3236
|
};
|
|
3234
3237
|
};
|
|
3235
3238
|
var findTPSLFromOrders = (orders, symbol) => {
|