@orderly.network/hooks 0.0.36 → 0.0.38

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.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import React2, { createContext, useContext, useState, useEffect, useCallback, useRef, useMemo } from 'react';
2
2
  import useSWR, { useSWRConfig } from 'swr';
3
- export { default as useSWR } from 'swr';
3
+ export { SWRConfig, default as useSWR } from 'swr';
4
4
  import { WS, get, mutate } from '@orderly.network/net';
5
5
  import useSWRMutation from 'swr/mutation';
6
6
  import useConstant from 'use-constant';
@@ -592,6 +592,7 @@ function createGetter(data, depth = 2) {
592
592
  var useSymbolsInfo = () => {
593
593
  const { data = {} } = useQuery(`/v1/public/info`, {
594
594
  focusThrottleInterval: 1e3 * 60 * 60 * 24,
595
+ dedupingInterval: 1e3 * 60 * 60 * 24,
595
596
  revalidateOnFocus: false,
596
597
  formatter(data2) {
597
598
  var _a;
@@ -878,16 +879,21 @@ function quantityInputHandle(inputs) {
878
879
  quantity = quantity.toDecimalPlaces(config.baseDP);
879
880
  values.order_quantity = quantity.toNumber();
880
881
  }
881
- let price = markPrice;
882
- if (values.order_type === OrderType.LIMIT && !!values.order_price) {
883
- price = Number(values.order_price);
882
+ if (values.order_type === OrderType.MARKET) {
883
+ const price = markPrice;
884
+ values.total = quantity.mul(price).todp(2).toNumber();
885
+ }
886
+ if (values.order_type === OrderType.LIMIT) {
887
+ if (values.order_price) {
888
+ const price = Number(values.order_price);
889
+ const total = quantity.mul(price);
890
+ values.total = total.todp(2).toNumber();
891
+ } else {
892
+ values.total = "";
893
+ }
884
894
  }
885
- const total = quantity.mul(price);
886
- total.dp();
887
895
  return [
888
- __spreadProps(__spreadValues({}, values), {
889
- total: total.todp(2).toNumber()
890
- }),
896
+ __spreadValues({}, values),
891
897
  input,
892
898
  value,
893
899
  markPrice,
@@ -1014,7 +1020,7 @@ var parseHolding = (holding, markPrices) => {
1014
1020
  var usePositionStream = (symbol, options) => {
1015
1021
  var _a;
1016
1022
  const symbolInfo = useSymbolsInfo();
1017
- useAccount();
1023
+ useEventEmitter();
1018
1024
  const { data: accountInfo } = usePrivateQuery("/v1/client/info");
1019
1025
  const { data: holding } = usePrivateQuery(
1020
1026
  "/v1/client/holding",
@@ -1025,13 +1031,11 @@ var usePositionStream = (symbol, options) => {
1025
1031
  }
1026
1032
  );
1027
1033
  const fundingRates = useFundingRates();
1028
- const { data, error } = usePrivateQuery(`/v1/positions`, __spreadProps(__spreadValues({
1029
- // revalidateOnFocus: false,
1030
- // revalidateOnReconnect: false,
1031
- dedupingInterval: 5e3,
1032
- keepPreviousData: false,
1033
- revalidateIfStale: true
1034
- }, options), {
1034
+ const {
1035
+ data,
1036
+ error
1037
+ // mutate: updatePositions,
1038
+ } = usePrivateQuery(`/v1/positions`, __spreadProps(__spreadValues({}, options), {
1035
1039
  formatter: (data2) => data2,
1036
1040
  onError: (err) => {
1037
1041
  console.log("usePositionStream error", err);
@@ -1471,8 +1475,9 @@ var OrderFactory = class {
1471
1475
  }
1472
1476
  }
1473
1477
  };
1478
+
1479
+ // src/orderly/useOrderEntry.ts
1474
1480
  var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1475
- const { mutate: mutate2 } = useSWRConfig();
1476
1481
  const [doCreateOrder] = useMutation("/v1/order");
1477
1482
  const { freeCollateral } = useCollateral();
1478
1483
  const symbolInfo = useSymbolsInfo();
@@ -1516,12 +1521,7 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1516
1521
  const data = orderCreator.create(values);
1517
1522
  return doCreateOrder(__spreadProps(__spreadValues({}, data), {
1518
1523
  symbol
1519
- })).then((res) => {
1520
- if (res.success) {
1521
- mutate2("/v1/orders?size=100&page=1$status=NEW");
1522
- }
1523
- return res;
1524
- });
1524
+ }));
1525
1525
  });
1526
1526
  };
1527
1527
  const calculate = useCallback(
@@ -1773,7 +1773,6 @@ var useOrderStream = ({
1773
1773
  const cancelAllOrders = useCallback(() => {
1774
1774
  }, [ordersResponse.data]);
1775
1775
  const updateOrder = useCallback((orderId, order2) => {
1776
- console.log("updateOrder", order2, orderId);
1777
1776
  return doUpdateOrder(__spreadProps(__spreadValues({}, order2), { order_id: orderId }));
1778
1777
  }, []);
1779
1778
  const cancelOrder = useCallback((orderId, symbol2) => {
@@ -1939,6 +1938,7 @@ var usePrivateDataObserver = () => {
1939
1938
  const ws = useWS();
1940
1939
  const { mutate: mutate2 } = useSWRConfig();
1941
1940
  const ee = useEventEmitter();
1941
+ const { state } = useAccount();
1942
1942
  useEffect(() => {
1943
1943
  console.log("subscribe: executionreport");
1944
1944
  const unsubscribe = ws.privateSubscribe("executionreport", {
@@ -1949,19 +1949,56 @@ var usePrivateDataObserver = () => {
1949
1949
  return () => unsubscribe == null ? void 0 : unsubscribe();
1950
1950
  }, []);
1951
1951
  useEffect(() => {
1952
- console.log("subscribe: position");
1952
+ console.log("subscribe: position: %s", state.accountId);
1953
+ if (!state.accountId)
1954
+ return;
1955
+ const key = ["/v1/positions", state.accountId];
1953
1956
  const unsubscribe = ws.privateSubscribe("position", {
1954
1957
  onMessage: (data) => {
1955
1958
  const { positions: nextPostions } = data;
1956
- console.info("refresh positions", nextPostions);
1957
- mutate2("/v1/positions");
1959
+ console.info("refresh positions:", nextPostions, state.accountId);
1960
+ mutate2(key, (prevPositions) => {
1961
+ console.log("prevPositions:::::", prevPositions);
1962
+ if (!!prevPositions) {
1963
+ return __spreadProps(__spreadValues({}, prevPositions), {
1964
+ rows: prevPositions.rows.map((row) => {
1965
+ const item = nextPostions.find(
1966
+ (item2) => item2.symbol === row.symbol
1967
+ );
1968
+ if (item) {
1969
+ return {
1970
+ symbol: item.symbol,
1971
+ position_qty: item.positionQty,
1972
+ cost_position: item.costPosition,
1973
+ last_sum_unitary_funding: item.lastSumUnitaryFunding,
1974
+ pending_long_qty: item.pendingLongQty,
1975
+ pending_short_qty: item.pendingShortQty,
1976
+ settle_price: item.settlePrice,
1977
+ average_open_price: item.averageOpenPrice,
1978
+ unsettled_pnl: item.unsettledPnl,
1979
+ mark_price: item.markPrice,
1980
+ est_liq_price: item.estLiqPrice,
1981
+ timestamp: Date.now(),
1982
+ imr: item.imr,
1983
+ mmr: item.mmr,
1984
+ IMR_withdraw_orders: item.imrwithOrders,
1985
+ MMR_with_orders: item.mmrwithOrders,
1986
+ pnl_24_h: item.pnl24H,
1987
+ fee_24_h: item.fee24H
1988
+ };
1989
+ }
1990
+ return row;
1991
+ })
1992
+ });
1993
+ }
1994
+ });
1958
1995
  }
1959
1996
  });
1960
1997
  return () => {
1961
1998
  console.log("unsubscribe: private subscription position");
1962
1999
  unsubscribe == null ? void 0 : unsubscribe();
1963
2000
  };
1964
- }, []);
2001
+ }, [state.accountId]);
1965
2002
  };
1966
2003
  var useExecutionReport = () => {
1967
2004
  const ws = useWS();