@orderly.network/hooks 1.0.19 → 1.0.21

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
@@ -130,7 +130,7 @@ var useMutation = (url, method = "POST", options) => {
130
130
  fetcher2,
131
131
  options
132
132
  );
133
- const mutation = async (data2, params) => {
133
+ const mutation = async (data2, params, options2) => {
134
134
  let newUrl = url;
135
135
  if (typeof params === "object" && Object.keys(params).length) {
136
136
  let search = new URLSearchParams(params);
@@ -142,15 +142,18 @@ var useMutation = (url, method = "POST", options) => {
142
142
  data: data2
143
143
  };
144
144
  const signature = await signer.sign(payload);
145
- return trigger({
146
- data: data2,
147
- params,
148
- method,
149
- signature: {
150
- ...signature,
151
- "orderly-account-id": account5.accountId
152
- }
153
- });
145
+ return trigger(
146
+ {
147
+ data: data2,
148
+ params,
149
+ method,
150
+ signature: {
151
+ ...signature,
152
+ "orderly-account-id": account5.accountId
153
+ }
154
+ },
155
+ options2
156
+ );
154
157
  };
155
158
  return [
156
159
  mutation,
@@ -787,7 +790,9 @@ var reduceItems = (depth, level, data, asks = false) => {
787
790
  const decimal = priceStr.slice(index + 1);
788
791
  const decimalDepth = depth.toString().slice(2).length;
789
792
  const decimalStr = decimal.slice(0, min(decimal.length, decimalDepth));
790
- priceKey = new Decimal(priceStr.slice(0, index) + "." + decimalStr).toNumber();
793
+ priceKey = new Decimal(
794
+ priceStr.slice(0, index) + "." + decimalStr
795
+ ).toNumber();
791
796
  }
792
797
  if (prices.has(priceKey)) {
793
798
  const item = prices.get(priceKey);
@@ -879,10 +884,10 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
879
884
  if (!symbol) {
880
885
  throw new Error("useOrderbookStream requires a symbol");
881
886
  }
887
+ const level = options?.level ?? 10;
882
888
  const [requestData, setRequestData] = useState(null);
883
889
  const [data, setData] = useState(initial);
884
890
  const [isLoading, setIsLoading] = useState(true);
885
- const [level, setLevel] = useState(() => options?.level ?? 10);
886
891
  const config = useSymbolsInfo()[symbol];
887
892
  const [depth, setDepth] = useState();
888
893
  const depths = useMemo(() => {
@@ -913,9 +918,13 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
913
918
  return;
914
919
  if (!!message) {
915
920
  let bids = [...message.bids.sort(bidsSortFn)];
916
- bids = bids.filter((item) => !isNaN(item[0]) && item[1] > 0);
921
+ bids = bids.filter(
922
+ (item) => !isNaN(item[0]) && item[1] > 0
923
+ );
917
924
  let asks = [...message.asks.sort(asksSortFn)];
918
- asks = asks.filter((item) => !isNaN(item[0]) && item[1] > 0);
925
+ asks = asks.filter(
926
+ (item) => !isNaN(item[0]) && item[1] > 0
927
+ );
919
928
  setRequestData({ bids, asks });
920
929
  setData({ bids: [...bids], asks: [...asks] });
921
930
  }
@@ -1343,7 +1352,10 @@ var usePositionStream = (symbol, options) => {
1343
1352
  return [
1344
1353
  {
1345
1354
  rows: positionsRows,
1346
- aggregated: formatedPositions?.[1] ?? {},
1355
+ aggregated: {
1356
+ ...formatedPositions?.[1] ?? {},
1357
+ unrealPnlROI: totalUnrealizedROI
1358
+ },
1347
1359
  totalCollateral,
1348
1360
  totalValue,
1349
1361
  totalUnrealizedROI
@@ -1418,14 +1430,14 @@ var useHoldingStream = () => {
1418
1430
  var useOrderStream = (params) => {
1419
1431
  const { status, symbol, side, size = 100 } = params;
1420
1432
  const { data: markPrices = {} } = useMarkPricesStream();
1421
- const [doCancelOrder, { error: cancelOrderError }] = useMutation(
1422
- "/v1/order",
1423
- "DELETE"
1424
- );
1425
- const [doUpdateOrder, { error: updateOrderError }] = useMutation(
1426
- "/v1/order",
1427
- "PUT"
1428
- );
1433
+ const [
1434
+ doCancelOrder,
1435
+ { error: cancelOrderError, isMutating: cancelMutating }
1436
+ ] = useMutation("/v1/order", "DELETE");
1437
+ const [
1438
+ doUpdateOrder,
1439
+ { error: updateOrderError, isMutating: updateMutating }
1440
+ ] = useMutation("/v1/order", "PUT");
1429
1441
  const ordersResponse = usePrivateInfiniteQuery(
1430
1442
  (pageIndex, previousPageData) => {
1431
1443
  if (previousPageData && !previousPageData.rows?.length)
@@ -1478,7 +1490,9 @@ var useOrderStream = (params) => {
1478
1490
  order_id: orderId,
1479
1491
  symbol: symbol2
1480
1492
  }).then((res) => {
1481
- if (res.success) ; else {
1493
+ if (res.success) {
1494
+ return res;
1495
+ } else {
1482
1496
  throw new Error(res.message);
1483
1497
  }
1484
1498
  });
@@ -1498,6 +1512,10 @@ var useOrderStream = (params) => {
1498
1512
  errors: {
1499
1513
  cancelOrder: cancelOrderError,
1500
1514
  updateOrder: updateOrderError
1515
+ },
1516
+ submitting: {
1517
+ cancelOrder: cancelMutating,
1518
+ updateOrder: updateMutating
1501
1519
  }
1502
1520
  }
1503
1521
  ];
@@ -1795,7 +1813,7 @@ var OrderFactory = class {
1795
1813
 
1796
1814
  // src/orderly/useOrderEntry.ts
1797
1815
  var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1798
- const [doCreateOrder] = useMutation("/v1/order");
1816
+ const [doCreateOrder, { data, error, reset, isMutating }] = useMutation("/v1/order");
1799
1817
  const { freeCollateral } = useCollateral();
1800
1818
  const symbolInfo = useSymbolsInfo();
1801
1819
  const baseDP = useMemo(
@@ -1834,9 +1852,9 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1834
1852
  if (!symbol) {
1835
1853
  throw new Error("symbol is null");
1836
1854
  }
1837
- const data = orderCreator.create(values);
1855
+ const data2 = orderCreator.create(values);
1838
1856
  return doCreateOrder({
1839
- ...data,
1857
+ ...data2,
1840
1858
  symbol
1841
1859
  });
1842
1860
  });
@@ -1868,6 +1886,7 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1868
1886
  freeCollateral,
1869
1887
  markPrice,
1870
1888
  onSubmit,
1889
+ submitting: isMutating,
1871
1890
  helper: {
1872
1891
  calculate,
1873
1892
  validator
@@ -1915,13 +1934,17 @@ var useMarketsStream = () => {
1915
1934
  (t) => t.symbol === item.symbol
1916
1935
  );
1917
1936
  if (ticker) {
1918
- return {
1937
+ const data = {
1919
1938
  ...item,
1920
1939
  ["24h_close"]: ticker.close,
1921
1940
  ["24h_open"]: ticker.open,
1922
1941
  ["24h_volumn"]: ticker.volume,
1923
1942
  change: 0
1924
1943
  };
1944
+ if (ticker.close !== void 0 && ticker.open !== void 0) {
1945
+ data["change"] = new Decimal(ticker.close).minus(ticker.open).div(ticker.open).toNumber();
1946
+ }
1947
+ return data;
1925
1948
  }
1926
1949
  return item;
1927
1950
  });
@@ -1930,7 +1953,7 @@ var useMarketsStream = () => {
1930
1953
  };
1931
1954
  var useLeverage = () => {
1932
1955
  const { data, mutate: mutate2 } = usePrivateQuery("/v1/client/info");
1933
- const [update] = useMutation("/v1/client/leverage");
1956
+ const [update, { isMutating }] = useMutation("/v1/client/leverage");
1934
1957
  const { data: config } = useQuery("/v1/public/config");
1935
1958
  const updateLeverage = useCallback((data2) => {
1936
1959
  return update(data2).then((res) => {
@@ -1945,6 +1968,7 @@ var useLeverage = () => {
1945
1968
  prop("max_leverage", data),
1946
1969
  {
1947
1970
  update: updateLeverage,
1971
+ isMutating,
1948
1972
  // config: [1, 2, 3, 4, 5, 10, 15, 20],
1949
1973
  config: config ? config?.available_futures_leverage?.split(",").map((item) => parseInt(item)) : []
1950
1974
  }
@@ -3342,7 +3366,7 @@ var useChain = (token) => {
3342
3366
  var useWithdraw = () => {
3343
3367
  const { account: account5, state } = useAccount();
3344
3368
  const [isLoading, setIsLoading] = useState(false);
3345
- const { unsettledPnL, availableBalance } = useCollateral();
3369
+ const { unsettledPnL, availableBalance, freeCollateral } = useCollateral();
3346
3370
  const withdraw = useCallback(
3347
3371
  (inputs) => {
3348
3372
  return account5.assetsManager.withdraw(inputs).then((res) => {
@@ -3351,14 +3375,10 @@ var useWithdraw = () => {
3351
3375
  },
3352
3376
  [state]
3353
3377
  );
3354
- const { usdc } = useHoldingStream();
3378
+ useHoldingStream();
3355
3379
  const maxAmount = useMemo(() => {
3356
- if (!usdc || !usdc.holding)
3357
- return 0;
3358
- if (unsettledPnL >= 0)
3359
- return usdc?.holding ?? 0;
3360
- return new Decimal(usdc.holding).add(unsettledPnL).toNumber();
3361
- }, [usdc, unsettledPnL]);
3380
+ return freeCollateral;
3381
+ }, [freeCollateral]);
3362
3382
  return { withdraw, isLoading, maxAmount, availableBalance, unsettledPnL };
3363
3383
  };
3364
3384
  var useDeposit = (options) => {
@@ -3592,6 +3612,35 @@ var useSettleSubscription = (options) => {
3592
3612
  return () => unsubscribe();
3593
3613
  });
3594
3614
  };
3615
+ function useMediaQuery(query) {
3616
+ const getMatches = (query2) => {
3617
+ if (typeof window !== "undefined") {
3618
+ return window.matchMedia(query2).matches;
3619
+ }
3620
+ return false;
3621
+ };
3622
+ const [matches, setMatches] = useState(getMatches(query));
3623
+ function handleChange() {
3624
+ setMatches(getMatches(query));
3625
+ }
3626
+ useEffect(() => {
3627
+ const matchMedia = window.matchMedia(query);
3628
+ handleChange();
3629
+ if (matchMedia.addListener) {
3630
+ matchMedia.addListener(handleChange);
3631
+ } else {
3632
+ matchMedia.addEventListener("change", handleChange);
3633
+ }
3634
+ return () => {
3635
+ if (matchMedia.removeListener) {
3636
+ matchMedia.removeListener(handleChange);
3637
+ } else {
3638
+ matchMedia.removeEventListener("change", handleChange);
3639
+ }
3640
+ };
3641
+ }, [query]);
3642
+ return matches;
3643
+ }
3595
3644
  var useWooSwapQuery = () => {
3596
3645
  const { configStore } = useContext(OrderlyContext);
3597
3646
  const account5 = useAccountInstance();
@@ -4194,6 +4243,6 @@ var useSwap = () => {
4194
4243
  };
4195
4244
  };
4196
4245
 
4197
- export { OrderlyConfigProvider, OrderlyContext, OrderlyProvider, WalletConnectorContext, useAccount, useAccountInfo, useAccountInstance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useFundingRate, useHoldingStream, useIndexPrice, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateInfiniteQuery, usePrivateQuery, useQuery, useSessionStorage, useSettleSubscription, useSwap, useSymbolsInfo, useTickerStream, useWS, useWalletConnector, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery };
4246
+ export { OrderlyConfigProvider, OrderlyContext, OrderlyProvider, WalletConnectorContext, useAccount, useAccountInfo, useAccountInstance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useFundingRate, useHoldingStream, useIndexPrice, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMediaQuery, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateInfiniteQuery, usePrivateQuery, useQuery, useSessionStorage, useSettleSubscription, useSwap, useSymbolsInfo, useTickerStream, useWS, useWalletConnector, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery };
4198
4247
  //# sourceMappingURL=out.js.map
4199
4248
  //# sourceMappingURL=index.mjs.map