@orderly.network/hooks 0.0.24 → 0.0.25
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 +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +119 -95
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +120 -95
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.mjs
CHANGED
|
@@ -526,17 +526,93 @@ var useMarkPrice = (symbol) => {
|
|
|
526
526
|
});
|
|
527
527
|
};
|
|
528
528
|
|
|
529
|
+
// src/utils/createGetter.ts
|
|
530
|
+
function createGetter(data, depth = 2) {
|
|
531
|
+
return new Proxy(data || {}, {
|
|
532
|
+
get(target, property, receiver) {
|
|
533
|
+
if (depth === 1) {
|
|
534
|
+
return (defaultValue) => {
|
|
535
|
+
var _a;
|
|
536
|
+
if (!target)
|
|
537
|
+
return defaultValue;
|
|
538
|
+
return (_a = target[property]) != null ? _a : defaultValue;
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
return (key, defaultValue) => {
|
|
542
|
+
var _a, _b;
|
|
543
|
+
if (key) {
|
|
544
|
+
return (_b = (_a = target[property]) == null ? void 0 : _a[key]) != null ? _b : defaultValue;
|
|
545
|
+
} else {
|
|
546
|
+
return target[property];
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
var useSymbolsInfo = () => {
|
|
553
|
+
const { data = {} } = useQuery(`/v1/public/info`, {
|
|
554
|
+
focusThrottleInterval: 1e3 * 60 * 60 * 24,
|
|
555
|
+
revalidateOnFocus: false,
|
|
556
|
+
formatter(data2) {
|
|
557
|
+
var _a;
|
|
558
|
+
if (!(data2 == null ? void 0 : data2.rows) || !((_a = data2 == null ? void 0 : data2.rows) == null ? void 0 : _a.length)) {
|
|
559
|
+
return {};
|
|
560
|
+
}
|
|
561
|
+
const obj = /* @__PURE__ */ Object.create(null);
|
|
562
|
+
for (let index = 0; index < data2.rows.length; index++) {
|
|
563
|
+
const item = data2.rows[index];
|
|
564
|
+
const arr = item.symbol.split("_");
|
|
565
|
+
const base_dp = getPrecisionByNumber(item.base_tick);
|
|
566
|
+
const quote_dp = getPrecisionByNumber(item.quote_tick);
|
|
567
|
+
obj[item.symbol] = __spreadProps(__spreadValues({}, item), {
|
|
568
|
+
base_dp,
|
|
569
|
+
quote_dp,
|
|
570
|
+
base: arr[1],
|
|
571
|
+
quote: arr[2],
|
|
572
|
+
type: arr[0],
|
|
573
|
+
name: `${arr[1]}-${arr[0]}`
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
return obj;
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
return createGetter(data);
|
|
580
|
+
};
|
|
581
|
+
|
|
529
582
|
// src/orderly/useOrderbookStream.ts
|
|
530
583
|
var paddingFn = (len) => Array(len).fill([Number.NaN, Number.NaN, Number.NaN]);
|
|
531
584
|
var asksSortFn = (a, b) => a[0] - b[0];
|
|
532
585
|
var bidsSortFn = (a, b) => b[0] - a[0];
|
|
533
|
-
var reduceItems = (depth, level, data) => {
|
|
586
|
+
var reduceItems = (depth, level, data, asks = false) => {
|
|
534
587
|
if (!Array.isArray(data) || data.length === 0) {
|
|
535
588
|
return [];
|
|
536
589
|
}
|
|
590
|
+
let newData = [...data];
|
|
537
591
|
const result = [];
|
|
538
|
-
|
|
539
|
-
const
|
|
592
|
+
if (typeof depth !== "undefined") {
|
|
593
|
+
const prices = /* @__PURE__ */ new Map();
|
|
594
|
+
for (let i = 0; i < data.length; i++) {
|
|
595
|
+
const [price, quantity] = data[i];
|
|
596
|
+
if (isNaN(price) || isNaN(quantity))
|
|
597
|
+
continue;
|
|
598
|
+
let priceKey;
|
|
599
|
+
if (asks) {
|
|
600
|
+
priceKey = Math.ceil(price / depth) * depth;
|
|
601
|
+
} else {
|
|
602
|
+
priceKey = Math.floor(price / depth) * depth;
|
|
603
|
+
}
|
|
604
|
+
if (prices.has(priceKey)) {
|
|
605
|
+
const item = prices.get(priceKey);
|
|
606
|
+
const itemPrice = item[1] + quantity;
|
|
607
|
+
prices.set(priceKey, [priceKey, itemPrice]);
|
|
608
|
+
} else {
|
|
609
|
+
prices.set(priceKey, [priceKey, quantity]);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
newData = Array.from(prices.values());
|
|
613
|
+
}
|
|
614
|
+
for (let i = 0; i < newData.length; i++) {
|
|
615
|
+
const [price, quantity] = newData[i];
|
|
540
616
|
if (isNaN(price) || isNaN(quantity))
|
|
541
617
|
continue;
|
|
542
618
|
result.push([
|
|
@@ -551,7 +627,7 @@ var reduceItems = (depth, level, data) => {
|
|
|
551
627
|
return result;
|
|
552
628
|
};
|
|
553
629
|
var reduceOrderbook = (depth, level, data) => {
|
|
554
|
-
const asks = reduceItems(depth, level, data.asks).reverse();
|
|
630
|
+
const asks = reduceItems(depth, level, data.asks, true).reverse();
|
|
555
631
|
const bids = reduceItems(depth, level, data.bids);
|
|
556
632
|
return {
|
|
557
633
|
asks: asks.length < level ? paddingFn(level - asks.length).concat(asks) : asks,
|
|
@@ -565,10 +641,6 @@ var mergeItems = (data, update) => {
|
|
|
565
641
|
const item = update.shift();
|
|
566
642
|
if (item) {
|
|
567
643
|
const [price, quantity] = item;
|
|
568
|
-
if (price < data[0][0] && quantity > 0) {
|
|
569
|
-
data.unshift(item);
|
|
570
|
-
continue;
|
|
571
|
-
}
|
|
572
644
|
const index = data.findIndex(([p], index2) => p === price);
|
|
573
645
|
if (index === -1) {
|
|
574
646
|
data.push(item);
|
|
@@ -576,8 +648,9 @@ var mergeItems = (data, update) => {
|
|
|
576
648
|
if (quantity === 0) {
|
|
577
649
|
data.splice(index, 1);
|
|
578
650
|
continue;
|
|
651
|
+
} else {
|
|
652
|
+
data[index] = item;
|
|
579
653
|
}
|
|
580
|
-
data[index] = item;
|
|
581
654
|
}
|
|
582
655
|
}
|
|
583
656
|
}
|
|
@@ -596,11 +669,19 @@ var useOrderbookStream = (symbol, initial = { asks: [], bids: [] }, options) =>
|
|
|
596
669
|
const [requestData, setRequestData] = useState(null);
|
|
597
670
|
const [data, setData] = useState(initial);
|
|
598
671
|
const [isLoading, setIsLoading] = useState(true);
|
|
599
|
-
const [depth, setDepth] = useState(1e-3);
|
|
600
672
|
const [level, setLevel] = useState(() => {
|
|
601
673
|
var _a;
|
|
602
674
|
return (_a = options == null ? void 0 : options.level) != null ? _a : 10;
|
|
603
675
|
});
|
|
676
|
+
const config = useSymbolsInfo()[symbol];
|
|
677
|
+
const [depth, setDepth] = useState();
|
|
678
|
+
const depths = useMemo(() => {
|
|
679
|
+
const tick = config("quote_tick");
|
|
680
|
+
return [tick, tick * 10, tick * 100, tick * 1e3];
|
|
681
|
+
}, [config("quote_tick")]);
|
|
682
|
+
useEffect(() => {
|
|
683
|
+
setDepth(config("quote_tick"));
|
|
684
|
+
}, [config("quote_tick")]);
|
|
604
685
|
const ws = useWS();
|
|
605
686
|
const ticker = useTickerStream(symbol);
|
|
606
687
|
const eventEmitter = useEventEmitter();
|
|
@@ -615,7 +696,6 @@ var useOrderbookStream = (symbol, initial = { asks: [], bids: [] }, options) =>
|
|
|
615
696
|
},
|
|
616
697
|
{
|
|
617
698
|
onMessage: (message) => {
|
|
618
|
-
console.log("orderbook request message", message);
|
|
619
699
|
if (!!message) {
|
|
620
700
|
const reduceOrderbookData = reduceOrderbook(depth, level, message);
|
|
621
701
|
setRequestData(reduceOrderbookData);
|
|
@@ -628,7 +708,7 @@ var useOrderbookStream = (symbol, initial = { asks: [], bids: [] }, options) =>
|
|
|
628
708
|
return () => {
|
|
629
709
|
setRequestData(null);
|
|
630
710
|
};
|
|
631
|
-
}, [symbol]);
|
|
711
|
+
}, [symbol, depth]);
|
|
632
712
|
const { data: markPrice } = useMarkPrice(symbol);
|
|
633
713
|
useEffect(() => {
|
|
634
714
|
if (!requestData)
|
|
@@ -656,7 +736,7 @@ var useOrderbookStream = (symbol, initial = { asks: [], bids: [] }, options) =>
|
|
|
656
736
|
eventEmitter.emit("orderbook:item:click", item);
|
|
657
737
|
}, []);
|
|
658
738
|
const onDepthChange = useCallback((depth2) => {
|
|
659
|
-
|
|
739
|
+
setDepth(() => depth2);
|
|
660
740
|
}, []);
|
|
661
741
|
const middlePrice = useMemo(() => {
|
|
662
742
|
let asksFrist = 0, bidsFirst = 0;
|
|
@@ -672,63 +752,10 @@ var useOrderbookStream = (symbol, initial = { asks: [], bids: [] }, options) =>
|
|
|
672
752
|
}, [ticker, data]);
|
|
673
753
|
return [
|
|
674
754
|
__spreadProps(__spreadValues({}, data), { markPrice, middlePrice }),
|
|
675
|
-
{ onDepthChange, depth, isLoading, onItemClick }
|
|
755
|
+
{ onDepthChange, depth, allDepths: depths, isLoading, onItemClick }
|
|
676
756
|
];
|
|
677
757
|
};
|
|
678
758
|
|
|
679
|
-
// src/utils/createGetter.ts
|
|
680
|
-
function createGetter(data, depth = 2) {
|
|
681
|
-
return new Proxy(data || {}, {
|
|
682
|
-
get(target, property, receiver) {
|
|
683
|
-
if (depth === 1) {
|
|
684
|
-
return (defaultValue) => {
|
|
685
|
-
var _a;
|
|
686
|
-
if (!target)
|
|
687
|
-
return defaultValue;
|
|
688
|
-
return (_a = target[property]) != null ? _a : defaultValue;
|
|
689
|
-
};
|
|
690
|
-
}
|
|
691
|
-
return (key, defaultValue) => {
|
|
692
|
-
var _a, _b;
|
|
693
|
-
if (key) {
|
|
694
|
-
return (_b = (_a = target[property]) == null ? void 0 : _a[key]) != null ? _b : defaultValue;
|
|
695
|
-
} else {
|
|
696
|
-
return target[property];
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
}
|
|
700
|
-
});
|
|
701
|
-
}
|
|
702
|
-
var useSymbolsInfo = () => {
|
|
703
|
-
const { data = {} } = useQuery(`/v1/public/info`, {
|
|
704
|
-
focusThrottleInterval: 1e3 * 60 * 60 * 24,
|
|
705
|
-
revalidateOnFocus: false,
|
|
706
|
-
formatter(data2) {
|
|
707
|
-
var _a;
|
|
708
|
-
if (!(data2 == null ? void 0 : data2.rows) || !((_a = data2 == null ? void 0 : data2.rows) == null ? void 0 : _a.length)) {
|
|
709
|
-
return {};
|
|
710
|
-
}
|
|
711
|
-
const obj = /* @__PURE__ */ Object.create(null);
|
|
712
|
-
for (let index = 0; index < data2.rows.length; index++) {
|
|
713
|
-
const item = data2.rows[index];
|
|
714
|
-
const arr = item.symbol.split("_");
|
|
715
|
-
const base_dp = getPrecisionByNumber(item.base_tick);
|
|
716
|
-
const quote_dp = getPrecisionByNumber(item.quote_tick);
|
|
717
|
-
obj[item.symbol] = __spreadProps(__spreadValues({}, item), {
|
|
718
|
-
base_dp,
|
|
719
|
-
quote_dp,
|
|
720
|
-
base: arr[1],
|
|
721
|
-
quote: arr[2],
|
|
722
|
-
type: arr[0],
|
|
723
|
-
name: `${arr[1]}-${arr[0]}`
|
|
724
|
-
});
|
|
725
|
-
}
|
|
726
|
-
return obj;
|
|
727
|
-
}
|
|
728
|
-
});
|
|
729
|
-
return createGetter(data);
|
|
730
|
-
};
|
|
731
|
-
|
|
732
759
|
// src/orderly/useTokenInfo.ts
|
|
733
760
|
var useTokenInfo = () => {
|
|
734
761
|
const { data = {} } = useQuery(
|
|
@@ -986,10 +1013,7 @@ var usePositionStream = (symbol, options) => {
|
|
|
986
1013
|
item.symbol,
|
|
987
1014
|
markPrices
|
|
988
1015
|
);
|
|
989
|
-
const notional = positions.notional(
|
|
990
|
-
item.position_qty,
|
|
991
|
-
item.average_open_price
|
|
992
|
-
);
|
|
1016
|
+
const notional = positions.notional(item.position_qty, price);
|
|
993
1017
|
const unrealPnl = positions.unrealizedPnL({
|
|
994
1018
|
qty: item.position_qty,
|
|
995
1019
|
openPrice: item.average_open_price,
|
|
@@ -1659,6 +1683,7 @@ var useOrderStream = ({
|
|
|
1659
1683
|
side,
|
|
1660
1684
|
size = 100
|
|
1661
1685
|
} = {}) => {
|
|
1686
|
+
const ee = useEventEmitter();
|
|
1662
1687
|
const { data: markPrices = {} } = useMarkPricesStream();
|
|
1663
1688
|
const [doCancelOrder] = useMutation("/v1/order", "DELETE");
|
|
1664
1689
|
const [doUpdateOrder] = useMutation("/v1/order", "PUT");
|
|
@@ -1699,6 +1724,13 @@ var useOrderStream = ({
|
|
|
1699
1724
|
});
|
|
1700
1725
|
});
|
|
1701
1726
|
}, [ordersResponse.data, markPrices]);
|
|
1727
|
+
useEffect(() => {
|
|
1728
|
+
const handler = () => ordersResponse.mutate();
|
|
1729
|
+
ee.on("orders:changed", handler);
|
|
1730
|
+
return () => {
|
|
1731
|
+
ee.off("orders:changed", handler);
|
|
1732
|
+
};
|
|
1733
|
+
}, []);
|
|
1702
1734
|
const cancelAllOrders = useCallback(() => {
|
|
1703
1735
|
}, [ordersResponse.data]);
|
|
1704
1736
|
const updateOrder = useCallback((orderId, order2) => {
|
|
@@ -1732,38 +1764,31 @@ var useMarketTradeStream = (symbol, options = {}) => {
|
|
|
1732
1764
|
if (!symbol) {
|
|
1733
1765
|
throw new Error("useTradeStream: symbol is required");
|
|
1734
1766
|
}
|
|
1735
|
-
useState([]);
|
|
1767
|
+
const [trades, setTrades] = useState([]);
|
|
1736
1768
|
const { level = 20 } = options;
|
|
1737
|
-
const {
|
|
1738
|
-
`/v1/public/market_trades?symbol=${symbol}&limit=${level}
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
// }
|
|
1769
|
+
const { isLoading } = useQuery(
|
|
1770
|
+
`/v1/public/market_trades?symbol=${symbol}&limit=${level}`,
|
|
1771
|
+
{
|
|
1772
|
+
onSuccess: (data) => {
|
|
1773
|
+
if (Array.isArray(data)) {
|
|
1774
|
+
setTrades(() => data);
|
|
1775
|
+
}
|
|
1776
|
+
return data;
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1748
1779
|
);
|
|
1749
1780
|
const ws = useWS();
|
|
1750
1781
|
useEffect(() => {
|
|
1751
1782
|
const unsubscript = ws.subscribe(`@${symbol}/@trade`, {
|
|
1752
|
-
onMessage: (
|
|
1753
|
-
console.log("trade",
|
|
1783
|
+
onMessage: (data) => {
|
|
1784
|
+
console.log("ws: trade", data);
|
|
1754
1785
|
}
|
|
1755
1786
|
});
|
|
1756
1787
|
return () => {
|
|
1757
1788
|
unsubscript == null ? void 0 : unsubscript();
|
|
1758
1789
|
};
|
|
1759
1790
|
}, []);
|
|
1760
|
-
return { data, isLoading };
|
|
1761
|
-
};
|
|
1762
|
-
|
|
1763
|
-
// src/orderly/useTrades.tsx
|
|
1764
|
-
var useTradeStream = () => {
|
|
1765
|
-
const { data, isLoading } = usePrivateQuery("/v1/trades");
|
|
1766
|
-
return [data, { isLoading }];
|
|
1791
|
+
return { data: trades, isLoading };
|
|
1767
1792
|
};
|
|
1768
1793
|
var useMarginRatio = () => {
|
|
1769
1794
|
const [{ rows }] = usePositionStream();
|
|
@@ -1844,12 +1869,12 @@ var useBalance = () => {
|
|
|
1844
1869
|
var usePrivateDataObserver = () => {
|
|
1845
1870
|
const ws = useWS();
|
|
1846
1871
|
const { mutate: mutate2 } = useSWRConfig();
|
|
1872
|
+
const ee = useEventEmitter();
|
|
1847
1873
|
useEffect(() => {
|
|
1848
1874
|
console.log("subscribe: executionreport");
|
|
1849
1875
|
const unsubscribe = ws.privateSubscribe("executionreport", {
|
|
1850
1876
|
onMessage: (data) => {
|
|
1851
|
-
|
|
1852
|
-
console.log(data);
|
|
1877
|
+
ee.emit("orders:changed");
|
|
1853
1878
|
}
|
|
1854
1879
|
});
|
|
1855
1880
|
return () => unsubscribe == null ? void 0 : unsubscribe();
|
|
@@ -1910,6 +1935,6 @@ var useFundingRateBySymbol = (symbol) => {
|
|
|
1910
1935
|
return useQuery(`/public/funding_rate/${symbol}`);
|
|
1911
1936
|
};
|
|
1912
1937
|
|
|
1913
|
-
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,
|
|
1938
|
+
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 };
|
|
1914
1939
|
//# sourceMappingURL=out.js.map
|
|
1915
1940
|
//# sourceMappingURL=index.mjs.map
|