@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.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
2
- export { SWRConfiguration, default as useSWR } from 'swr';
2
+ export { SWRConfig, SWRConfiguration, default as useSWR } from 'swr';
3
3
  import { SWRMutationConfiguration } from 'swr/mutation';
4
4
  import { Account, AccountState, ConfigStore, OrderlyKeyStore, WalletAdapter, IContract, EventEmitter } from '@orderly.network/core';
5
5
  import * as react from 'react';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
2
- export { SWRConfiguration, default as useSWR } from 'swr';
2
+ export { SWRConfig, SWRConfiguration, default as useSWR } from 'swr';
3
3
  import { SWRMutationConfiguration } from 'swr/mutation';
4
4
  import { Account, AccountState, ConfigStore, OrderlyKeyStore, WalletAdapter, IContract, EventEmitter } from '@orderly.network/core';
5
5
  import * as react from 'react';
package/dist/index.js CHANGED
@@ -601,6 +601,7 @@ function createGetter(data, depth = 2) {
601
601
  var useSymbolsInfo = () => {
602
602
  const { data = {} } = useQuery(`/v1/public/info`, {
603
603
  focusThrottleInterval: 1e3 * 60 * 60 * 24,
604
+ dedupingInterval: 1e3 * 60 * 60 * 24,
604
605
  revalidateOnFocus: false,
605
606
  formatter(data2) {
606
607
  var _a;
@@ -887,16 +888,21 @@ function quantityInputHandle(inputs) {
887
888
  quantity = quantity.toDecimalPlaces(config.baseDP);
888
889
  values.order_quantity = quantity.toNumber();
889
890
  }
890
- let price = markPrice;
891
- if (values.order_type === types.OrderType.LIMIT && !!values.order_price) {
892
- price = Number(values.order_price);
891
+ if (values.order_type === types.OrderType.MARKET) {
892
+ const price = markPrice;
893
+ values.total = quantity.mul(price).todp(2).toNumber();
894
+ }
895
+ if (values.order_type === types.OrderType.LIMIT) {
896
+ if (values.order_price) {
897
+ const price = Number(values.order_price);
898
+ const total = quantity.mul(price);
899
+ values.total = total.todp(2).toNumber();
900
+ } else {
901
+ values.total = "";
902
+ }
893
903
  }
894
- const total = quantity.mul(price);
895
- total.dp();
896
904
  return [
897
- __spreadProps(__spreadValues({}, values), {
898
- total: total.todp(2).toNumber()
899
- }),
905
+ __spreadValues({}, values),
900
906
  input,
901
907
  value,
902
908
  markPrice,
@@ -1023,7 +1029,7 @@ var parseHolding = (holding, markPrices) => {
1023
1029
  var usePositionStream = (symbol, options) => {
1024
1030
  var _a;
1025
1031
  const symbolInfo = useSymbolsInfo();
1026
- useAccount();
1032
+ useEventEmitter();
1027
1033
  const { data: accountInfo } = usePrivateQuery("/v1/client/info");
1028
1034
  const { data: holding } = usePrivateQuery(
1029
1035
  "/v1/client/holding",
@@ -1034,13 +1040,11 @@ var usePositionStream = (symbol, options) => {
1034
1040
  }
1035
1041
  );
1036
1042
  const fundingRates = useFundingRates();
1037
- const { data, error } = usePrivateQuery(`/v1/positions`, __spreadProps(__spreadValues({
1038
- // revalidateOnFocus: false,
1039
- // revalidateOnReconnect: false,
1040
- dedupingInterval: 5e3,
1041
- keepPreviousData: false,
1042
- revalidateIfStale: true
1043
- }, options), {
1043
+ const {
1044
+ data,
1045
+ error
1046
+ // mutate: updatePositions,
1047
+ } = usePrivateQuery(`/v1/positions`, __spreadProps(__spreadValues({}, options), {
1044
1048
  formatter: (data2) => data2,
1045
1049
  onError: (err) => {
1046
1050
  console.log("usePositionStream error", err);
@@ -1480,8 +1484,9 @@ var OrderFactory = class {
1480
1484
  }
1481
1485
  }
1482
1486
  };
1487
+
1488
+ // src/orderly/useOrderEntry.ts
1483
1489
  var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1484
- const { mutate: mutate2 } = useSWR.useSWRConfig();
1485
1490
  const [doCreateOrder] = useMutation("/v1/order");
1486
1491
  const { freeCollateral } = useCollateral();
1487
1492
  const symbolInfo = useSymbolsInfo();
@@ -1525,12 +1530,7 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1525
1530
  const data = orderCreator.create(values);
1526
1531
  return doCreateOrder(__spreadProps(__spreadValues({}, data), {
1527
1532
  symbol
1528
- })).then((res) => {
1529
- if (res.success) {
1530
- mutate2("/v1/orders?size=100&page=1$status=NEW");
1531
- }
1532
- return res;
1533
- });
1533
+ }));
1534
1534
  });
1535
1535
  };
1536
1536
  const calculate = React2.useCallback(
@@ -1782,7 +1782,6 @@ var useOrderStream = ({
1782
1782
  const cancelAllOrders = React2.useCallback(() => {
1783
1783
  }, [ordersResponse.data]);
1784
1784
  const updateOrder = React2.useCallback((orderId, order2) => {
1785
- console.log("updateOrder", order2, orderId);
1786
1785
  return doUpdateOrder(__spreadProps(__spreadValues({}, order2), { order_id: orderId }));
1787
1786
  }, []);
1788
1787
  const cancelOrder = React2.useCallback((orderId, symbol2) => {
@@ -1948,6 +1947,7 @@ var usePrivateDataObserver = () => {
1948
1947
  const ws = useWS();
1949
1948
  const { mutate: mutate2 } = useSWR.useSWRConfig();
1950
1949
  const ee = useEventEmitter();
1950
+ const { state } = useAccount();
1951
1951
  React2.useEffect(() => {
1952
1952
  console.log("subscribe: executionreport");
1953
1953
  const unsubscribe = ws.privateSubscribe("executionreport", {
@@ -1958,19 +1958,56 @@ var usePrivateDataObserver = () => {
1958
1958
  return () => unsubscribe == null ? void 0 : unsubscribe();
1959
1959
  }, []);
1960
1960
  React2.useEffect(() => {
1961
- console.log("subscribe: position");
1961
+ console.log("subscribe: position: %s", state.accountId);
1962
+ if (!state.accountId)
1963
+ return;
1964
+ const key = ["/v1/positions", state.accountId];
1962
1965
  const unsubscribe = ws.privateSubscribe("position", {
1963
1966
  onMessage: (data) => {
1964
1967
  const { positions: nextPostions } = data;
1965
- console.info("refresh positions", nextPostions);
1966
- mutate2("/v1/positions");
1968
+ console.info("refresh positions:", nextPostions, state.accountId);
1969
+ mutate2(key, (prevPositions) => {
1970
+ console.log("prevPositions:::::", prevPositions);
1971
+ if (!!prevPositions) {
1972
+ return __spreadProps(__spreadValues({}, prevPositions), {
1973
+ rows: prevPositions.rows.map((row) => {
1974
+ const item = nextPostions.find(
1975
+ (item2) => item2.symbol === row.symbol
1976
+ );
1977
+ if (item) {
1978
+ return {
1979
+ symbol: item.symbol,
1980
+ position_qty: item.positionQty,
1981
+ cost_position: item.costPosition,
1982
+ last_sum_unitary_funding: item.lastSumUnitaryFunding,
1983
+ pending_long_qty: item.pendingLongQty,
1984
+ pending_short_qty: item.pendingShortQty,
1985
+ settle_price: item.settlePrice,
1986
+ average_open_price: item.averageOpenPrice,
1987
+ unsettled_pnl: item.unsettledPnl,
1988
+ mark_price: item.markPrice,
1989
+ est_liq_price: item.estLiqPrice,
1990
+ timestamp: Date.now(),
1991
+ imr: item.imr,
1992
+ mmr: item.mmr,
1993
+ IMR_withdraw_orders: item.imrwithOrders,
1994
+ MMR_with_orders: item.mmrwithOrders,
1995
+ pnl_24_h: item.pnl24H,
1996
+ fee_24_h: item.fee24H
1997
+ };
1998
+ }
1999
+ return row;
2000
+ })
2001
+ });
2002
+ }
2003
+ });
1967
2004
  }
1968
2005
  });
1969
2006
  return () => {
1970
2007
  console.log("unsubscribe: private subscription position");
1971
2008
  unsubscribe == null ? void 0 : unsubscribe();
1972
2009
  };
1973
- }, []);
2010
+ }, [state.accountId]);
1974
2011
  };
1975
2012
  var useExecutionReport = () => {
1976
2013
  const ws = useWS();
@@ -2013,6 +2050,10 @@ var useFundingRateBySymbol = (symbol) => {
2013
2050
  return useQuery(`/public/funding_rate/${symbol}`);
2014
2051
  };
2015
2052
 
2053
+ Object.defineProperty(exports, 'SWRConfig', {
2054
+ enumerable: true,
2055
+ get: function () { return useSWR.SWRConfig; }
2056
+ });
2016
2057
  Object.defineProperty(exports, 'useSWR', {
2017
2058
  enumerable: true,
2018
2059
  get: function () { return useSWR__default.default; }