@orderly.network/hooks 1.1.3-alpha.6 → 1.1.3-alpha.7

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 CHANGED
@@ -25,7 +25,7 @@ declare global {
25
25
  };
26
26
  }
27
27
  }
28
- declare const _default: "1.1.3-alpha.6";
28
+ declare const _default: "1.1.3-alpha.7";
29
29
 
30
30
  type useQueryOptions<T> = SWRConfiguration & {
31
31
  formatter?: (data: any) => T;
package/dist/index.d.ts CHANGED
@@ -25,7 +25,7 @@ declare global {
25
25
  };
26
26
  }
27
27
  }
28
- declare const _default: "1.1.3-alpha.6";
28
+ declare const _default: "1.1.3-alpha.7";
29
29
 
30
30
  type useQueryOptions<T> = SWRConfiguration & {
31
31
  formatter?: (data: any) => T;
package/dist/index.js CHANGED
@@ -28,9 +28,9 @@ var useSWRSubscription__default = /*#__PURE__*/_interopDefault(useSWRSubscriptio
28
28
  // src/version.ts
29
29
  if (typeof window !== "undefined") {
30
30
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
31
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "1.1.3-alpha.6";
31
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "1.1.3-alpha.7";
32
32
  }
33
- var version_default = "1.1.3-alpha.6";
33
+ var version_default = "1.1.3-alpha.7";
34
34
  var fetcher = (url, init = {}, queryOptions) => net.get(url, init, queryOptions?.formatter);
35
35
  var OrderlyContext = React.createContext({
36
36
  // configStore: new MemoryConfigStore(),
@@ -502,13 +502,19 @@ var usePrivateDataObserver = (options) => {
502
502
  const { state } = useAccount();
503
503
  const updateOrders = useDebounce.useDebouncedCallback((data) => {
504
504
  const map = options.getKeysMap("orders");
505
+ const orderStatus = getSessionStorage(
506
+ "orderly_order_status",
507
+ "positions"
508
+ );
505
509
  map.forEach((getKey, key) => {
506
- mutate2(
507
- useSWRInfinite.unstable_serialize((index, prevData) => [
508
- getKey(index, prevData),
509
- state.accountId
510
- ])
511
- );
510
+ if (orderStatus === "history" && key === "orders" || orderStatus === "positions" && key === "orders:NEW" || key.includes(orderStatus)) {
511
+ mutate2(
512
+ useSWRInfinite.unstable_serialize((index, prevData) => [
513
+ getKey(index, prevData),
514
+ state.accountId
515
+ ])
516
+ );
517
+ }
512
518
  });
513
519
  }, 500);
514
520
  React.useEffect(() => {
@@ -529,7 +535,9 @@ var usePrivateDataObserver = (options) => {
529
535
  onMessage: (data) => {
530
536
  updateOrders(data);
531
537
  if (Array.isArray(data)) {
532
- data.forEach((item) => ee.emit("orders:changed", { ...item, status: item.algoStatus }));
538
+ data.forEach(
539
+ (item) => ee.emit("orders:changed", { ...item, status: item.algoStatus })
540
+ );
533
541
  } else {
534
542
  ee.emit("orders:changed", { ...data, status: data.algoStatus });
535
543
  }
@@ -592,6 +600,17 @@ var usePrivateDataObserver = (options) => {
592
600
  };
593
601
  }, [state.accountId]);
594
602
  };
603
+ var getSessionStorage = (key, initialValue) => {
604
+ if (typeof window === "undefined") {
605
+ return initialValue;
606
+ }
607
+ try {
608
+ const item = window.sessionStorage.getItem(key);
609
+ return item ? parseJSON(item) : initialValue;
610
+ } catch (error) {
611
+ return initialValue;
612
+ }
613
+ };
595
614
  var DataCenterContext = React.createContext(
596
615
  {}
597
616
  );
@@ -632,6 +651,11 @@ var OrderlyConfigProvider = (props) => {
632
651
  enableSwapDeposit,
633
652
  contracts
634
653
  } = props;
654
+ if (typeof configStore !== "undefined" && !configStore.get("brokerId")) {
655
+ throw new types.SDKError(
656
+ "if configStore is provided, brokerId is required in configStore"
657
+ );
658
+ }
635
659
  const innerConfigStore = useConstant4__default.default(() => {
636
660
  return configStore || new core.DefaultConfigStore({ brokerId, networkId });
637
661
  });
@@ -1674,7 +1698,10 @@ var useOrderStream = (params) => {
1674
1698
  return `/v1/orders?${search.toString()}`;
1675
1699
  };
1676
1700
  React.useEffect(() => {
1677
- const key = `orders:${status}:${symbol}:${side}`;
1701
+ const formatKey = (value) => value ? `:${value}` : "";
1702
+ const key = `orders${formatKey(status)}${formatKey(symbol)}${formatKey(
1703
+ side
1704
+ )}`;
1678
1705
  regesterKeyHandler(key, getKey);
1679
1706
  }, [status, symbol, side]);
1680
1707
  const ordersResponse = usePrivateInfiniteQuery(getKey, {
@@ -1710,55 +1737,61 @@ var useOrderStream = (params) => {
1710
1737
  }, [ordersResponse.data?.[0]?.meta?.total]);
1711
1738
  const cancelAllOrders = React.useCallback(() => {
1712
1739
  }, [ordersResponse.data]);
1713
- const _updateOrder = React.useCallback((orderId, order3, type) => {
1714
- switch (type) {
1715
- case "algoOrder":
1716
- return doUpdateAlgoOrder({
1717
- order_id: orderId,
1718
- price: order3["order_price"],
1719
- quantity: order3["order_quantity"],
1720
- trigger_price: order3["trigger_price"]
1721
- });
1722
- default:
1723
- return doUpdateOrder({ ...order3, order_id: orderId });
1724
- }
1725
- }, []);
1740
+ const _updateOrder = React.useCallback(
1741
+ (orderId, order3, type) => {
1742
+ switch (type) {
1743
+ case "algoOrder":
1744
+ return doUpdateAlgoOrder({
1745
+ order_id: orderId,
1746
+ price: order3["order_price"],
1747
+ quantity: order3["order_quantity"],
1748
+ trigger_price: order3["trigger_price"]
1749
+ });
1750
+ default:
1751
+ return doUpdateOrder({ ...order3, order_id: orderId });
1752
+ }
1753
+ },
1754
+ []
1755
+ );
1726
1756
  const updateOrder = React.useCallback((orderId, order3) => {
1727
1757
  return _updateOrder(orderId, order3, "normalOrder");
1728
1758
  }, []);
1729
1759
  const updateAlgoOrder = React.useCallback((orderId, order3) => {
1730
1760
  return _updateOrder(orderId, order3, "algoOrder");
1731
1761
  }, []);
1732
- const _cancelOrder = React.useCallback((orderId, type, symbol2) => {
1733
- switch (type) {
1734
- case "algoOrder":
1735
- return doCanceAlgolOrder(null, {
1736
- // @ts-ignore
1737
- order_id: orderId,
1738
- symbol: symbol2,
1739
- source: `SDK${version_default}`
1740
- }).then((res) => {
1741
- if (res.success) {
1742
- ordersResponse.mutate();
1743
- return res;
1744
- } else {
1745
- throw new Error(res.message);
1746
- }
1747
- });
1748
- default:
1749
- return doCancelOrder(null, {
1750
- order_id: orderId,
1751
- symbol: symbol2,
1752
- source: `SDK_${version_default}`
1753
- }).then((res) => {
1754
- if (res.success) {
1755
- return res;
1756
- } else {
1757
- throw new Error(res.message);
1758
- }
1759
- });
1760
- }
1761
- }, []);
1762
+ const _cancelOrder = React.useCallback(
1763
+ (orderId, type, symbol2) => {
1764
+ switch (type) {
1765
+ case "algoOrder":
1766
+ return doCanceAlgolOrder(null, {
1767
+ // @ts-ignore
1768
+ order_id: orderId,
1769
+ symbol: symbol2,
1770
+ source: `SDK${version_default}`
1771
+ }).then((res) => {
1772
+ if (res.success) {
1773
+ ordersResponse.mutate();
1774
+ return res;
1775
+ } else {
1776
+ throw new Error(res.message);
1777
+ }
1778
+ });
1779
+ default:
1780
+ return doCancelOrder(null, {
1781
+ order_id: orderId,
1782
+ symbol: symbol2,
1783
+ source: `SDK_${version_default}`
1784
+ }).then((res) => {
1785
+ if (res.success) {
1786
+ return res;
1787
+ } else {
1788
+ throw new Error(res.message);
1789
+ }
1790
+ });
1791
+ }
1792
+ },
1793
+ []
1794
+ );
1762
1795
  const cancelOrder = React.useCallback((orderId, symbol2) => {
1763
1796
  return _cancelOrder(orderId, "normalOrder", symbol2);
1764
1797
  }, []);
@@ -2331,21 +2364,29 @@ function useOrderEntry(symbolOrOrder, sideOrOptions, reduceOnly, options) {
2331
2364
  return { key, value };
2332
2365
  };
2333
2366
  const maxQty = useMaxQty(symbol, sideValue, isReduceOnly);
2367
+ const parseString2Number = (order3, key) => {
2368
+ if (typeof order3[key] !== "string")
2369
+ return;
2370
+ if (order3[key] && order3[key].startsWith(".")) {
2371
+ order3[key] = `0${order3[key]}`;
2372
+ }
2373
+ order3[key] = order3[key].replace(/,/g, "");
2374
+ };
2334
2375
  const parsedData = React.useMemo(() => {
2335
2376
  if (typeof symbolOrOrder === "string") {
2336
2377
  return null;
2337
2378
  }
2338
2379
  if (typeof symbolOrOrder.order_quantity === "string") {
2339
- symbolOrOrder.order_quantity = symbolOrOrder.order_quantity.replace(
2340
- /,/g,
2341
- ""
2342
- );
2380
+ parseString2Number(symbolOrOrder, "order_quantity");
2343
2381
  }
2344
2382
  if (typeof symbolOrOrder.order_price === "string") {
2345
- symbolOrOrder.order_price = symbolOrOrder.order_price.replace(/,/g, "");
2383
+ parseString2Number(symbolOrOrder, "order_price");
2346
2384
  }
2347
2385
  if (typeof symbolOrOrder.total === "string") {
2348
- symbolOrOrder.total = symbolOrOrder.total.replace(/,/g, "");
2386
+ parseString2Number(symbolOrOrder, "total");
2387
+ }
2388
+ if (typeof symbolOrOrder.trigger_price === "string") {
2389
+ parseString2Number(symbolOrOrder, "trigger_price");
2349
2390
  }
2350
2391
  if (typeof symbolOrOrder.order_quantity === "number") {
2351
2392
  symbolOrOrder.order_quantity = new utils.Decimal(symbolOrOrder.order_quantity).toDecimalPlaces(baseDP).toString();