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