@orderly.network/hooks 0.0.27 → 0.0.29
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 +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +89 -33
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +89 -34
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.mjs
CHANGED
|
@@ -341,6 +341,18 @@ var useEventEmitter = (channel) => {
|
|
|
341
341
|
return ee;
|
|
342
342
|
});
|
|
343
343
|
};
|
|
344
|
+
|
|
345
|
+
// src/utils/json.ts
|
|
346
|
+
function parseJSON(value) {
|
|
347
|
+
try {
|
|
348
|
+
return value === "undefined" ? void 0 : JSON.parse(value != null ? value : "");
|
|
349
|
+
} catch (e) {
|
|
350
|
+
console.log("parsing error on", { value });
|
|
351
|
+
return void 0;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// src/useSessionStorage.ts
|
|
344
356
|
function useSessionStorage(key, initialValue) {
|
|
345
357
|
const readValue = useCallback(() => {
|
|
346
358
|
if (typeof window === "undefined") {
|
|
@@ -392,13 +404,41 @@ function useSessionStorage(key, initialValue) {
|
|
|
392
404
|
);
|
|
393
405
|
return [storedValue, setValue];
|
|
394
406
|
}
|
|
395
|
-
function
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
407
|
+
function useLocalStorage(key, initialValue) {
|
|
408
|
+
const readValue = useCallback(() => {
|
|
409
|
+
if (typeof window === "undefined") {
|
|
410
|
+
return initialValue;
|
|
411
|
+
}
|
|
412
|
+
try {
|
|
413
|
+
const item = window.localStorage.getItem(key);
|
|
414
|
+
return item ? parseJSON(item) : initialValue;
|
|
415
|
+
} catch (error) {
|
|
416
|
+
console.warn(`Error reading localStorage key \u201C${key}\u201D:`, error);
|
|
417
|
+
return initialValue;
|
|
418
|
+
}
|
|
419
|
+
}, [initialValue, key]);
|
|
420
|
+
const [storedValue, setStoredValue] = useState(readValue);
|
|
421
|
+
const setValue = useCallback(
|
|
422
|
+
(value) => {
|
|
423
|
+
if (typeof window === "undefined") {
|
|
424
|
+
console.warn(
|
|
425
|
+
`Tried setting localStorage key \u201C${key}\u201D even though environment is not a client`
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
try {
|
|
429
|
+
const newValue = value instanceof Function ? value(storedValue) : value;
|
|
430
|
+
window.localStorage.setItem(key, JSON.stringify(newValue));
|
|
431
|
+
setStoredValue(() => newValue);
|
|
432
|
+
} catch (error) {
|
|
433
|
+
console.warn(`Error setting localStorage key \u201C${key}\u201D:`, error);
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
[storedValue]
|
|
437
|
+
);
|
|
438
|
+
useEffect(() => {
|
|
439
|
+
setStoredValue(readValue());
|
|
440
|
+
}, []);
|
|
441
|
+
return [storedValue, setValue];
|
|
402
442
|
}
|
|
403
443
|
var useRunOnce = ({ fn, sessionKey }) => {
|
|
404
444
|
const triggered = useRef(false);
|
|
@@ -577,8 +617,6 @@ var useSymbolsInfo = () => {
|
|
|
577
617
|
});
|
|
578
618
|
return createGetter(data);
|
|
579
619
|
};
|
|
580
|
-
|
|
581
|
-
// src/orderly/useOrderbookStream.ts
|
|
582
620
|
var paddingFn = (len) => Array(len).fill([Number.NaN, Number.NaN, Number.NaN]);
|
|
583
621
|
var asksSortFn = (a, b) => a[0] - b[0];
|
|
584
622
|
var bidsSortFn = (a, b) => b[0] - a[0];
|
|
@@ -596,13 +634,13 @@ var reduceItems = (depth, level, data, asks = false) => {
|
|
|
596
634
|
continue;
|
|
597
635
|
let priceKey;
|
|
598
636
|
if (asks) {
|
|
599
|
-
priceKey = Math.ceil(price / depth)
|
|
637
|
+
priceKey = new Decimal(Math.ceil(price / depth)).mul(depth).toNumber();
|
|
600
638
|
} else {
|
|
601
|
-
priceKey = Math.floor(price / depth)
|
|
639
|
+
priceKey = new Decimal(Math.floor(price / depth)).mul(depth).toNumber();
|
|
602
640
|
}
|
|
603
641
|
if (prices.has(priceKey)) {
|
|
604
642
|
const item = prices.get(priceKey);
|
|
605
|
-
const itemPrice = item[1]
|
|
643
|
+
const itemPrice = new Decimal(item[1]).add(quantity).toNumber();
|
|
606
644
|
prices.set(priceKey, [priceKey, itemPrice]);
|
|
607
645
|
} else {
|
|
608
646
|
prices.set(priceKey, [priceKey, quantity]);
|
|
@@ -614,11 +652,8 @@ var reduceItems = (depth, level, data, asks = false) => {
|
|
|
614
652
|
const [price, quantity] = newData[i];
|
|
615
653
|
if (isNaN(price) || isNaN(quantity))
|
|
616
654
|
continue;
|
|
617
|
-
result.
|
|
618
|
-
|
|
619
|
-
quantity,
|
|
620
|
-
quantity + (result.length > 0 ? result[result.length - 1][2] : 0)
|
|
621
|
-
]);
|
|
655
|
+
const newQuantity = new Decimal(quantity).add(result.length > 0 ? result[result.length - 1][2] : 0).toNumber();
|
|
656
|
+
result.push([price, quantity, newQuantity]);
|
|
622
657
|
if (i + 1 >= level) {
|
|
623
658
|
break;
|
|
624
659
|
}
|
|
@@ -636,6 +671,7 @@ var reduceOrderbook = (depth, level, data) => {
|
|
|
636
671
|
var mergeItems = (data, update) => {
|
|
637
672
|
if (data.length === 0)
|
|
638
673
|
return update;
|
|
674
|
+
data = data.filter(([price]) => !isNaN(price));
|
|
639
675
|
while (update.length > 0) {
|
|
640
676
|
const item = update.shift();
|
|
641
677
|
if (item) {
|
|
@@ -688,6 +724,7 @@ var useOrderbookStream = (symbol, initial = { asks: [], bids: [] }, options) =>
|
|
|
688
724
|
ws.onceSubscribe(
|
|
689
725
|
{
|
|
690
726
|
event: "request",
|
|
727
|
+
id: `${symbol}@orderbook`,
|
|
691
728
|
params: {
|
|
692
729
|
type: "orderbook",
|
|
693
730
|
symbol
|
|
@@ -817,10 +854,9 @@ function priceInputHandle(inputs) {
|
|
|
817
854
|
return [values, input, value, markPrice, config];
|
|
818
855
|
}
|
|
819
856
|
const total = price.mul(values.order_quantity);
|
|
820
|
-
const quantityDP = total.dp();
|
|
821
857
|
return [
|
|
822
858
|
__spreadProps(__spreadValues({}, values), {
|
|
823
|
-
total: total.todp(
|
|
859
|
+
total: total.todp(2).toString()
|
|
824
860
|
}),
|
|
825
861
|
input,
|
|
826
862
|
value,
|
|
@@ -844,10 +880,10 @@ function quantityInputHandle(inputs) {
|
|
|
844
880
|
price = Number(values.order_price);
|
|
845
881
|
}
|
|
846
882
|
const total = quantity.mul(price);
|
|
847
|
-
|
|
883
|
+
total.dp();
|
|
848
884
|
return [
|
|
849
885
|
__spreadProps(__spreadValues({}, values), {
|
|
850
|
-
total: total.todp(
|
|
886
|
+
total: total.todp(2).toNumber()
|
|
851
887
|
}),
|
|
852
888
|
input,
|
|
853
889
|
value,
|
|
@@ -1764,29 +1800,48 @@ var useMarketTradeStream = (symbol, options = {}) => {
|
|
|
1764
1800
|
throw new Error("useTradeStream: symbol is required");
|
|
1765
1801
|
}
|
|
1766
1802
|
const [trades, setTrades] = useState([]);
|
|
1767
|
-
const
|
|
1768
|
-
const {
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1803
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
1804
|
+
const { limit = 50 } = options;
|
|
1805
|
+
const ws = useWS();
|
|
1806
|
+
useEffect(() => {
|
|
1807
|
+
setIsLoading(true);
|
|
1808
|
+
setTrades(() => []);
|
|
1809
|
+
ws.onceSubscribe(
|
|
1810
|
+
{
|
|
1811
|
+
id: `${symbol}@trade`,
|
|
1812
|
+
event: "request",
|
|
1813
|
+
params: {
|
|
1814
|
+
type: "trade",
|
|
1815
|
+
symbol,
|
|
1816
|
+
limit
|
|
1817
|
+
}
|
|
1818
|
+
},
|
|
1819
|
+
{
|
|
1820
|
+
onMessage: (data) => {
|
|
1821
|
+
setIsLoading(false);
|
|
1773
1822
|
setTrades(() => data);
|
|
1774
1823
|
}
|
|
1775
|
-
return data;
|
|
1776
1824
|
}
|
|
1777
|
-
|
|
1778
|
-
);
|
|
1779
|
-
const ws = useWS();
|
|
1825
|
+
);
|
|
1826
|
+
}, [symbol]);
|
|
1780
1827
|
useEffect(() => {
|
|
1828
|
+
if (trades.length <= 0)
|
|
1829
|
+
return;
|
|
1781
1830
|
const unsubscript = ws.subscribe(`@${symbol}/@trade`, {
|
|
1782
1831
|
onMessage: (data) => {
|
|
1783
|
-
|
|
1832
|
+
setTrades((prev) => {
|
|
1833
|
+
const arr = [data, ...prev];
|
|
1834
|
+
if (arr.length > limit) {
|
|
1835
|
+
arr.pop();
|
|
1836
|
+
}
|
|
1837
|
+
return arr;
|
|
1838
|
+
});
|
|
1784
1839
|
}
|
|
1785
1840
|
});
|
|
1786
1841
|
return () => {
|
|
1787
1842
|
unsubscript == null ? void 0 : unsubscript();
|
|
1788
1843
|
};
|
|
1789
|
-
}, []);
|
|
1844
|
+
}, [symbol, trades]);
|
|
1790
1845
|
return { data: trades, isLoading };
|
|
1791
1846
|
};
|
|
1792
1847
|
var useMarginRatio = () => {
|
|
@@ -1934,6 +1989,6 @@ var useFundingRateBySymbol = (symbol) => {
|
|
|
1934
1989
|
return useQuery(`/public/funding_rate/${symbol}`);
|
|
1935
1990
|
};
|
|
1936
1991
|
|
|
1937
|
-
export { DataSourceProvider, OrderStatus, OrderlyContext, OrderlyProvider, apis_exports as apis, useAccount, useAccountInfo, useAccountInstance, useAppState, useBalance, useChains, useCollateral, useEventEmitter, useExecutionReport, useFetures, useFundingRate, useHolding, useLeverage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateObserve, usePrivateQuery, useQuery, useRunOnce, useSessionStorage, useSymbolsInfo, useTickerStream, useTokenInfo, useTopicObserve, useTradingView, useWS };
|
|
1992
|
+
export { DataSourceProvider, OrderStatus, OrderlyContext, OrderlyProvider, apis_exports as apis, useAccount, useAccountInfo, useAccountInstance, useAppState, useBalance, useChains, useCollateral, useEventEmitter, useExecutionReport, useFetures, useFundingRate, useHolding, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateObserve, usePrivateQuery, useQuery, useRunOnce, useSessionStorage, useSymbolsInfo, useTickerStream, useTokenInfo, useTopicObserve, useTradingView, useWS };
|
|
1938
1993
|
//# sourceMappingURL=out.js.map
|
|
1939
1994
|
//# sourceMappingURL=index.mjs.map
|