@orderly.network/hooks 0.0.37 → 0.0.39

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